@PostConstruct protected void postConstruct() { DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest().withDomainNames(getDomainName()); List<DomainStatus> list = amazonCloudSearch.describeDomains(describeDomainsRequest).getDomainStatusList(); if (list.isEmpty()) { throw new InternalServerErrorException("Could not find CloudSearch domain: " + getDomainName()); } ServiceEndpoint searchService = list.get(0).getSearchService(); if (searchService == null || searchService.getEndpoint() == null) { throw new InternalServerErrorException("Could not find SearchService for: " + getDomainName()); } domain = createDomain(); domain.setEndpoint(searchService.getEndpoint()); }
@Test public void testThrowsExecptionWhenSearchServiceDoesNotExist() { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint())))); try { // TODO suppress exception stacktrace getService(ModelIndexer.class); } catch (MultiException e) { assertEquals("Could not find SearchService for: test-model", e.getCause().getMessage()); return; } fail("Was expection an exception"); }
@Test public void testSearchCenter() { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost"))))); HashMap<String, List<String>> map = Maps.newHashMap(); map.put("property", Lists.newArrayList("value")); SearchResult expected = new SearchResult().withHits(new Hits().withHit(new Hit().withFields(map))); ArgumentCaptor<SearchRequest> requestCaptor = ArgumentCaptor.forClass(SearchRequest.class); when(domain.search(requestCaptor.capture())).thenReturn(expected); List<ObjectNode> result = getService(ModelIndexer.class).searchCenter("0,0"); SearchRequest request = requestCaptor.getValue(); assertEquals("value", result.get(0).get("property").asText()); assertEquals("latlon:['0.1,-0.1','-0.1,0.1']", request.getQuery()); assertEquals("{\"distance\":\"haversin(0.0,0.0,latlon.latitude,latlon.longitude)\"}", request.getExpr()); assertEquals("distance asc", request.getSort()); assertEquals(Long.valueOf(30L), request.getSize()); }
@Bean public Map<String, AmazonCloudSearchDomainAsyncClient> cloudSearchDomainAsyncClients( AmazonCloudSearchClient cloudSearchClient, AWSCredentialsProvider awsCredentialsProvider) { DescribeDomainsResult describeDomainsResult = cloudSearchClient.describeDomains(); List<DomainStatus> domainStatusList = describeDomainsResult.getDomainStatusList(); Map<String, AmazonCloudSearchDomainAsyncClient> domainClients = new HashMap<>(domainStatusList.size()); for (DomainStatus domainStatus : domainStatusList) { log.debug("domainStatus: {}", domainStatus); String domainName = domainStatus.getDomainName(); if (domainStatus.isCreated() && !domainStatus.isDeleted()) { log.info("creating AmazonCloudSearchDomainClient for {} domain", domainName); ServiceEndpoint serviceEndpoint = domainStatus.getDocService(); AmazonCloudSearchDomainAsyncClient domainClient = new AmazonCloudSearchDomainAsyncClient( awsCredentialsProvider, client) .withEndpoint(serviceEndpoint.getEndpoint()); domainClients.put(domainName, domainClient); } else { log.info("skipping domain {}: created = {}, deleted = {}", domainName, domainStatus.isCreated(), domainStatus.isDeleted()); } } return domainClients; }
@Test public void testCreateDomain() { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost"))))); AmazonCloudSearchDomain domain = getService(ModelIndexer2.class).createDomain(); assertNotNull(domain); assertTrue(domain instanceof AmazonCloudSearchDomain); }
private DescribeDomainsResult getDescribeDomainsResult(final String domainName, final String documentServiceEndpoint, final String searchServiceEndpoint) { return new DescribeDomainsResult() .withDomainStatusList(new DomainStatus().withDomainName(domainName).withCreated(true).withDeleted(false) .withDocService(new ServiceEndpoint().withEndpoint(documentServiceEndpoint)) .withSearchService(new ServiceEndpoint().withEndpoint(searchServiceEndpoint))); }
@Test public void testIndexJsonProcessingException() throws Exception { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost"))))); Model model = new Model(); model.setGuid("00000000-0000-0000-0000-000000000000"); ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); getService(ModelIndexer.class).index(Lists.newArrayList(model)); verify(executor).submit(runnableCaptor.capture()); when(objectMapper.writeValueAsString(any())).thenThrow(new StubJsonProcessingException()); runnableCaptor.getValue().run(); verify(domain, never()).uploadDocuments(any()); }
@Test public void testIndex() throws Exception { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost"))))); objectMapper = new ObjectMapper(); Model model = new Model(); model.setGuid("00000000-0000-0000-0000-000000000000"); ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); getService(ModelIndexer.class).index(model); verify(executor).submit(runnableCaptor.capture()); runnableCaptor.getValue().run(); ArgumentCaptor<UploadDocumentsRequest> requestCaptor = ArgumentCaptor.forClass(UploadDocumentsRequest.class); verify(domain).uploadDocuments(requestCaptor.capture()); Scanner scanner = new Scanner(requestCaptor.getValue().getDocuments()); JsonNode json = new ObjectMapper().readTree(scanner.useDelimiter("\\A").next()).get(0); assertEquals("add", json.get("type").asText()); assertEquals("00000000-0000-0000-0000-000000000000", json.get("id").asText()); assertEquals("00000000-0000-0000-0000-000000000000", json.get("fields").get("guid").asText()); scanner.close(); }