Java 类com.amazonaws.services.cloudsearchv2.model.DescribeDomainsResult 实例源码

项目:mosquito-report-api    文件:IndexerTest.java   
@Test
public void testThrowsExecptionWhenSearchDomainDoesNotExist() {
    when(amazonCloudSearch.describeDomains(any()))
            .thenReturn(new DescribeDomainsResult().withDomainStatusList(Lists.newArrayList()));

    try {
        // TODO suppress exception stacktrace
        getService(ModelIndexer.class);
    } catch (MultiException e) {
        assertEquals("Could not find CloudSearch domain: test-model", e.getCause().getMessage());

        return;
    }

    fail("Was expection an exception");
}
项目:mosquito-report-api    文件:IndexerTest.java   
@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");
}
项目:mosquito-report-api    文件:IndexerTest.java   
@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());
}
项目:spring-boot-aws-cloudsearch    文件:CloudSearchConfig.java   
@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;
}
项目:mosquito-report-api    文件:IndexerTest.java   
@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);
}
项目:Cheddar    文件:CloudSearchEngine.java   
private void cacheDomainEndpoints() {
    if (cloudSearchClient != null && !domainEndpointsCached) {
        final Set<String> managedDomains = new HashSet<>();
        for (final DocumentConfiguration documentConfiguration : documentConfigurations.values()) {
            final String domainName = documentConfigurationHolder.schemaName() + "-"
                    + documentConfiguration.namespace();
            managedDomains.add(domainName);
        }
        final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest();
        describeDomainsRequest.setDomainNames(managedDomains);
        final DescribeDomainsResult describeDomainsResult = cloudSearchClient
                .describeDomains(describeDomainsRequest);
        final List<DomainStatus> domainStatusList = describeDomainsResult.getDomainStatusList();
        if (domainStatusList.size() != managedDomains.size()) {
            logger.info("Unable to cache CloudSearch document/search endpoints for: " + managedDomains);
        } else {
            for (final DomainStatus domainStatus : domainStatusList) {
                if (domainStatus.isCreated() && !domainStatus.isDeleted()) {
                    final String documentServiceEndpoint = domainStatus.getDocService().getEndpoint();
                    final String searchServiceEndpoint = domainStatus.getSearchService().getEndpoint();
                    if (documentServiceEndpoint == null || searchServiceEndpoint == null) {
                        domainEndpointsCached = false;
                        return;
                    }
                    final AmazonCloudSearchDomain documentServiceClient = AmazonCloudSearchDomainClientBuilder
                            .build(documentServiceEndpoint);
                    final AmazonCloudSearchDomain searchServiceClient = AmazonCloudSearchDomainClientBuilder
                            .build(searchServiceEndpoint);
                    documentServiceClients.put(domainStatus.getDomainName(), documentServiceClient);
                    searchServiceClients.put(domainStatus.getDomainName(), searchServiceClient);
                }
            }
            domainEndpointsCached = true;
        }
    }
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldInitializeCloudSearchEngine_withDocumentConfigurationHolderAndClient() throws Exception {
    // Given
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);

    final String namespace = randomString(10);
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final DocumentConfigurationHolder documentConfigurationHolder = new DocumentConfigurationHolder(schemaName,
            documentConfigurations);
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(schemaName + "-" + namespace));
    final DescribeDomainsResult describeDomainsResult = new DescribeDomainsResult().withDomainStatusList();
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);

    // When
    Exception actualException = null;
    try {
        new CloudSearchEngine(documentConfigurationHolder, mockAmazonCloudSearch);
    } catch (final Exception e) {
        actualException = e;
    }

    // Then
    assertNull(actualException);
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
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)));
}
项目:GeoCrawler    文件:CloudSearchIndexWriter.java   
@Override
public void open(JobConf job, String name) throws IOException {
  LOG.debug("CloudSearchIndexWriter.open() name={} ", name);

  maxDocsInBatch = job.getInt(CloudSearchConstants.MAX_DOCS_BATCH, -1);

  buffer = new StringBuffer(MAX_SIZE_BATCH_BYTES).append('[');

  dumpBatchFilesToTemp = job.getBoolean(CloudSearchConstants.BATCH_DUMP,
      false);

  if (dumpBatchFilesToTemp) {
    // only dumping to local file
    // no more config required
    return;
  }

  String endpoint = job.get(CloudSearchConstants.ENDPOINT);

  if (StringUtils.isBlank(endpoint)) {
    throw new RuntimeException("endpoint not set for CloudSearch");
  }

  AmazonCloudSearchClient cl = new AmazonCloudSearchClient();
  if (StringUtils.isNotBlank(regionName)) {
    cl.setRegion(RegionUtils.getRegion(regionName));
  }

  String domainName = null;

  // retrieve the domain name
  DescribeDomainsResult domains = cl
      .describeDomains(new DescribeDomainsRequest());

  Iterator<DomainStatus> dsiter = domains.getDomainStatusList().iterator();
  while (dsiter.hasNext()) {
    DomainStatus ds = dsiter.next();
    if (ds.getDocService().getEndpoint().equals(endpoint)) {
      domainName = ds.getDomainName();
      break;
    }
  }

  // check domain name
  if (StringUtils.isBlank(domainName)) {
    throw new RuntimeException(
        "No domain name found for CloudSearch endpoint");
  }

  DescribeIndexFieldsResult indexDescription = cl.describeIndexFields(
      new DescribeIndexFieldsRequest().withDomainName(domainName));
  for (IndexFieldStatus ifs : indexDescription.getIndexFields()) {
    String indexname = ifs.getOptions().getIndexFieldName();
    String indextype = ifs.getOptions().getIndexFieldType();
    LOG.info("CloudSearch index name {} of type {}", indexname, indextype);
    csfields.put(indexname, indextype);
  }

  client = new AmazonCloudSearchDomainClient();
  client.setEndpoint(endpoint);

}
项目:mosquito-report-api    文件:IndexerTest.java   
@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());
}
项目:spring-boot-aws-cloudsearch    文件:CloudSearchClient.java   
public DescribeDomainsResult describeDomain(String domainName) {
    DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest().withDomainNames(domainName);
    return cloudSearchAsyncClient.describeDomains(describeDomainsRequest);
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldUpdate_withDocument() throws Exception {
    // Given
    final StubDocument document = randomStubDocument();
    final String namespace = randomString(10);
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final DocumentConfigurationHolder documentConfigurationHolder = new DocumentConfigurationHolder(schemaName,
            documentConfigurations);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final byte[] jsonBytes = randomString().getBytes(Charset.forName("UTF-8"));
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final String documentServiceEndpoint = randomString();
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName,
            documentServiceEndpoint, randomString());
    final AmazonCloudSearchDomain mockDocumentServiceClient = mock(AmazonCloudSearchDomain.class);
    mockStatic(JsonDocumentUpdateMarshaller.class);
    when(JsonDocumentUpdateMarshaller.marshall(anyCollection())).thenReturn(new String(jsonBytes));
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(documentServiceEndpoint)).thenReturn(mockDocumentServiceClient);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    cloudSearchEngine.update(document);

    // Then
    final ArgumentCaptor<Collection> documentUpdateCollectionCaptor = ArgumentCaptor.forClass(Collection.class);
    final ArgumentCaptor<UploadDocumentsRequest> uploadDocumentsRequestCaptor = ArgumentCaptor
            .forClass(UploadDocumentsRequest.class);
    PowerMockito.verifyStatic();
    JsonDocumentUpdateMarshaller.marshall(documentUpdateCollectionCaptor.capture());
    verify(mockDocumentServiceClient).uploadDocuments(uploadDocumentsRequestCaptor.capture());
    final DocumentUpdate documentUpdate = (DocumentUpdate) documentUpdateCollectionCaptor.getValue().iterator()
            .next();
    assertEquals(document.getId(), documentUpdate.getId());
    assertEquals(Type.ADD, documentUpdate.getType());
    for (final Field field : documentUpdate.getFields()) {
        if (field.getName().equals("stringProperty")) {
            assertEquals(document.getStringProperty(), field.getValue());
        }
    }
    final UploadDocumentsRequest uploadDocumentsRequest = uploadDocumentsRequestCaptor.getValue();
    assertInputStreamEquals(jsonBytes, uploadDocumentsRequest.getDocuments());
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldUpdate_withDocumentThatHasAttributesWithWhitespace() throws Exception {
    // Given
    final StubDocument document = new StubDocument();
    final String whitespaceCharset = " \t\n\f\r";
    final String propertyValue = randomString(10);
    final String namespace = randomString(10);
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final DocumentConfigurationHolder documentConfigurationHolder = new DocumentConfigurationHolder(schemaName,
            documentConfigurations);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final byte[] jsonBytes = randomString().getBytes(Charset.forName("UTF-8"));
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final String documentServiceEndpoint = randomString();
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName,
            documentServiceEndpoint, randomString());
    final AmazonCloudSearchDomain mockDocumentServiceClient = mock(AmazonCloudSearchDomain.class);
    mockStatic(JsonDocumentUpdateMarshaller.class);
    when(JsonDocumentUpdateMarshaller.marshall(anyCollection())).thenReturn(new String(jsonBytes));
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(documentServiceEndpoint)).thenReturn(mockDocumentServiceClient);

    document.setId(randomString(10));
    document.setStringProperty(whitespaceCharset.charAt(Randoms.randomInt(whitespaceCharset.length()))
            + propertyValue + Randoms.randomInt(whitespaceCharset.length()));

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    cloudSearchEngine.update(document);

    // Then
    final ArgumentCaptor<Collection> documentUpdateCollectionCaptor = ArgumentCaptor.forClass(Collection.class);
    final ArgumentCaptor<UploadDocumentsRequest> uploadDocumentsRequestCaptor = ArgumentCaptor
            .forClass(UploadDocumentsRequest.class);
    PowerMockito.verifyStatic();
    JsonDocumentUpdateMarshaller.marshall(documentUpdateCollectionCaptor.capture());
    verify(mockDocumentServiceClient).uploadDocuments(uploadDocumentsRequestCaptor.capture());
    final DocumentUpdate documentUpdate = (DocumentUpdate) documentUpdateCollectionCaptor.getValue().iterator()
            .next();
    assertEquals(document.getId(), documentUpdate.getId());
    assertEquals(Type.ADD, documentUpdate.getType());
    for (final Field field : documentUpdate.getFields()) {
        if (field.getName().equals("stringProperty")) {
            assertEquals(propertyValue, field.getValue());
        }
    }
    final UploadDocumentsRequest uploadDocumentsRequest = uploadDocumentsRequestCaptor.getValue();
    assertInputStreamEquals(jsonBytes, uploadDocumentsRequest.getDocuments());
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldUpdate_withDocuments() throws Exception {
    // Given
    final int numberOfDocumentsToUpdate = Randoms.randomIntInRange(1, 3000);
    final Collection<StubDocument> documents = randomCollectionOfStubDocuments(numberOfDocumentsToUpdate);
    final String namespace = randomString(10);
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final DocumentConfigurationHolder documentConfigurationHolder = new DocumentConfigurationHolder(schemaName,
            documentConfigurations);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final byte[] jsonBytes = randomString().getBytes(Charset.forName("UTF-8"));
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final String documentServiceEndpoint = randomString();
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName,
            documentServiceEndpoint, randomString());
    final AmazonCloudSearchDomain mockDocumentServiceClient = mock(AmazonCloudSearchDomain.class);
    mockStatic(JsonDocumentUpdateMarshaller.class);
    when(JsonDocumentUpdateMarshaller.marshall(anyCollection())).thenReturn(new String(jsonBytes));
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(documentServiceEndpoint)).thenReturn(mockDocumentServiceClient);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    cloudSearchEngine.update(documents);

    // Then
    final ArgumentCaptor<Collection> documentUpdateCollectionCaptor = ArgumentCaptor.forClass(Collection.class);
    final ArgumentCaptor<UploadDocumentsRequest> uploadDocumentsRequestCaptor = ArgumentCaptor
            .forClass(UploadDocumentsRequest.class);
    final int numberOfBatches = (numberOfDocumentsToUpdate / 1000) + 1;
    PowerMockito.verifyStatic(times(numberOfBatches));
    JsonDocumentUpdateMarshaller.marshall(documentUpdateCollectionCaptor.capture());
    verify(mockDocumentServiceClient, times(numberOfBatches))
            .uploadDocuments(uploadDocumentsRequestCaptor.capture());

    for (final Collection<DocumentUpdate> documentUpdates : documentUpdateCollectionCaptor.getAllValues()) {
        assertTrue(documentUpdates.size() <= 1000);
        for (final DocumentUpdate documentUpdate : documentUpdates) {
            assertEquals(Type.ADD, documentUpdate.getType());
            for (final Field field : documentUpdate.getFields()) {
                if (field.getName().equals("stringProperty")) {
                    assertTrue(documents.stream().anyMatch(d -> d.getStringProperty().equals(field.getValue())));
                }
            }
        }
    }

    for (final UploadDocumentsRequest uploadDocumentsRequest : uploadDocumentsRequestCaptor.getAllValues()) {
        assertInputStreamEquals(jsonBytes, uploadDocumentsRequest.getDocuments());
    }
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldNotUpdate_withDocumentsNotInstancesOfTheSameClass() throws Exception {
    // Given
    final Collection<Document> documents = Arrays.asList(mock(Document.class), randomStubDocument());
    final String namespace = randomString(10);
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final DocumentConfigurationHolder documentConfigurationHolder = new DocumentConfigurationHolder(schemaName,
            documentConfigurations);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final byte[] jsonBytes = randomString().getBytes(Charset.forName("UTF-8"));
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final String documentServiceEndpoint = randomString();
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName,
            documentServiceEndpoint, randomString());
    final AmazonCloudSearchDomain mockDocumentServiceClient = mock(AmazonCloudSearchDomain.class);
    mockStatic(JsonDocumentUpdateMarshaller.class);
    when(JsonDocumentUpdateMarshaller.marshall(anyCollection())).thenReturn(new String(jsonBytes));
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(documentServiceEndpoint)).thenReturn(mockDocumentServiceClient);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    IllegalArgumentException thrownException = null;
    try {
        cloudSearchEngine.update(documents);
    } catch (final IllegalArgumentException e) {
        thrownException = e;
    }

    // Then
    assertNotNull(thrownException);
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldDelete_withDocument() throws Exception {
    // Given
    final StubDocument document = randomStubDocument();
    final String namespace = randomString(10);
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final DocumentConfigurationHolder documentConfigurationHolder = new DocumentConfigurationHolder(schemaName,
            documentConfigurations);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final byte[] jsonBytes = randomString().getBytes(Charset.forName("UTF-8"));
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final String documentServiceEndpoint = randomString();
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName,
            documentServiceEndpoint, randomString());
    final AmazonCloudSearchDomain mockDocumentServiceClient = mock(AmazonCloudSearchDomain.class);
    mockStatic(JsonDocumentUpdateMarshaller.class);
    when(JsonDocumentUpdateMarshaller.marshall(anyCollection())).thenReturn(new String(jsonBytes));
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(documentServiceEndpoint)).thenReturn(mockDocumentServiceClient);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    cloudSearchEngine.delete(document);

    // Then
    final ArgumentCaptor<Collection> documentUpdateCollectionCaptor = ArgumentCaptor.forClass(Collection.class);
    final ArgumentCaptor<UploadDocumentsRequest> uploadDocumentsRequestCaptor = ArgumentCaptor
            .forClass(UploadDocumentsRequest.class);
    PowerMockito.verifyStatic();
    JsonDocumentUpdateMarshaller.marshall(documentUpdateCollectionCaptor.capture());
    verify(mockDocumentServiceClient).uploadDocuments(uploadDocumentsRequestCaptor.capture());
    final DocumentUpdate documentUpdate = (DocumentUpdate) documentUpdateCollectionCaptor.getValue().iterator()
            .next();
    assertEquals(document.getId(), documentUpdate.getId());
    assertEquals(Type.DELETE, documentUpdate.getType());
    final UploadDocumentsRequest uploadDocumentsRequest = uploadDocumentsRequestCaptor.getValue();
    assertInputStreamEquals(jsonBytes, uploadDocumentsRequest.getDocuments());
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldNotDelete_withDocumentsNotInstancesOfTheSameClass() throws Exception {
    // Given
    final Collection<Document> documents = Arrays.asList(mock(Document.class), randomStubDocument());
    final String namespace = randomString(10);
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final DocumentConfigurationHolder documentConfigurationHolder = new DocumentConfigurationHolder(schemaName,
            documentConfigurations);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final byte[] jsonBytes = randomString().getBytes(Charset.forName("UTF-8"));
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final String documentServiceEndpoint = randomString();
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName,
            documentServiceEndpoint, randomString());
    final AmazonCloudSearchDomain mockDocumentServiceClient = mock(AmazonCloudSearchDomain.class);
    mockStatic(JsonDocumentUpdateMarshaller.class);
    when(JsonDocumentUpdateMarshaller.marshall(anyCollection())).thenReturn(new String(jsonBytes));
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(documentServiceEndpoint)).thenReturn(mockDocumentServiceClient);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    IllegalArgumentException thrownException = null;
    try {
        cloudSearchEngine.delete(documents);
    } catch (final IllegalArgumentException e) {
        thrownException = e;
    }

    // Then
    assertNotNull(thrownException);
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldDelete_withDocuments() throws Exception {
    // Given
    final Collection<StubDocument> documents = randomCollectionOfStubDocuments(Randoms.randomIntInRange(1, 10));
    final String namespace = randomString(10);
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final DocumentConfigurationHolder documentConfigurationHolder = new DocumentConfigurationHolder(schemaName,
            documentConfigurations);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final byte[] jsonBytes = randomString().getBytes(Charset.forName("UTF-8"));
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final String documentServiceEndpoint = randomString();
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName,
            documentServiceEndpoint, randomString());
    final AmazonCloudSearchDomain mockDocumentServiceClient = mock(AmazonCloudSearchDomain.class);
    mockStatic(JsonDocumentUpdateMarshaller.class);
    when(JsonDocumentUpdateMarshaller.marshall(anyCollection())).thenReturn(new String(jsonBytes));
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(documentServiceEndpoint)).thenReturn(mockDocumentServiceClient);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    cloudSearchEngine.delete(documents);

    // Then
    final ArgumentCaptor<Collection> documentUpdateCollectionCaptor = ArgumentCaptor.forClass(Collection.class);
    final ArgumentCaptor<UploadDocumentsRequest> uploadDocumentsRequestCaptor = ArgumentCaptor
            .forClass(UploadDocumentsRequest.class);
    PowerMockito.verifyStatic();
    JsonDocumentUpdateMarshaller.marshall(documentUpdateCollectionCaptor.capture());
    verify(mockDocumentServiceClient).uploadDocuments(uploadDocumentsRequestCaptor.capture());
    final Collection<DocumentUpdate> documentUpdates = documentUpdateCollectionCaptor.getValue();
    assertEquals(documents.size(), documentUpdates.size());
    for (final DocumentUpdate documentUpdate : documentUpdates) {
        assertEquals(Type.DELETE, documentUpdate.getType());
        assertTrue(documents.stream().anyMatch(d -> d.getId().equals(documentUpdate.getId())));
    }
    final UploadDocumentsRequest uploadDocumentsRequest = uploadDocumentsRequestCaptor.getValue();
    assertInputStreamEquals(jsonBytes, uploadDocumentsRequest.getDocuments());
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldSearch_withQuery() throws Exception {
    // Given
    final StubDocument document = randomStubDocument();
    final String documentId = document.getId();
    final DocumentConfigurationHolder documentConfigurationHolder = mock(DocumentConfigurationHolder.class);
    final String namespace = documentId;
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final Query query = mock(Query.class);
    final QueryType queryType = randomEnum(QueryType.class);
    final AmazonCloudSearchDomain mockCloudSearchClient = mock(AmazonCloudSearchDomain.class);
    final Integer start = Randoms.randomInt(100);
    final Integer size = Randoms.randomInt(100);
    final String searchServiceEndpoint = randomString();
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName, randomString(),
            searchServiceEndpoint);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final SearchRequest searchRequest = buildSearchRequest(queryType, query);
    final SearchResult searchResult = new SearchResult().withHits(getExpectedHits(document));
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(searchServiceEndpoint)).thenReturn(mockCloudSearchClient);
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    when(documentConfigurationHolder.schemaName()).thenReturn(schemaName);
    when(documentConfigurationHolder.documentConfigurations()).thenReturn(documentConfigurations);
    when(query.queryType()).thenReturn(queryType);
    searchRequest.withStart((long) start);
    searchRequest.withSize((long) size);
    when(mockCloudSearchClient.search(searchRequest)).thenReturn(searchResult);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    final DocumentSearchResponse<StubDocument> returnedDocuments = cloudSearchEngine.search(query, start, size,
            StubDocument.class);

    // Then
    assertNotNull(returnedDocuments);
    assertEquals(document, returnedDocuments.getHits().get(0));
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldSearch_withExpression() throws Exception {
    // Given
    final StubDocument document = randomStubDocument();
    final String documentId = document.getId();
    final DocumentConfigurationHolder documentConfigurationHolder = mock(DocumentConfigurationHolder.class);
    final String namespace = documentId;
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final Query query = mock(Query.class);
    final QueryType queryType = randomEnum(QueryType.class);
    final AmazonCloudSearchDomain mockCloudSearchClient = mock(AmazonCloudSearchDomain.class);
    final Integer start = Randoms.randomInt(100);
    final Integer size = Randoms.randomInt(100);
    final String searchServiceEndpoint = randomString();
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName, randomString(),
            searchServiceEndpoint);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final SearchResult searchResult = new SearchResult().withHits(getExpectedHits(document));
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(searchServiceEndpoint)).thenReturn(mockCloudSearchClient);
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    when(documentConfigurationHolder.schemaName()).thenReturn(schemaName);
    when(documentConfigurationHolder.documentConfigurations()).thenReturn(documentConfigurations);
    when(query.queryType()).thenReturn(queryType);
    when(mockCloudSearchClient.search(any(SearchRequest.class))).thenReturn(searchResult);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    final Map<String, String> expressions = new HashMap<String, String>();
    expressions.put("key", "expression");

    final SearchOptions options = new SearchOptions().withExpressions(expressions);
    // When
    cloudSearchEngine.search(query, start, size, StubDocument.class, options);

    // Then
    final ArgumentCaptor<SearchRequest> searchRequestCaptor = ArgumentCaptor.forClass(SearchRequest.class);

    verify(mockCloudSearchClient).search(searchRequestCaptor.capture());

    final SearchRequest request = searchRequestCaptor.getValue();
    assertEquals(request.getExpr(), "{\"key\":\"expression\"}");
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldSearch_withSortInQuery() throws Exception {
    // Given
    final StubDocument document = randomStubDocument();
    final String documentId = document.getId();
    final DocumentConfigurationHolder documentConfigurationHolder = mock(DocumentConfigurationHolder.class);
    final String namespace = documentId;
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final Query query = mock(Query.class);
    final QueryType queryType = Randoms.randomEnum(QueryType.class);
    final AmazonCloudSearchDomain mockCloudSearchClient = mock(AmazonCloudSearchDomain.class);
    final Integer start = Randoms.randomInt(100);
    final Integer size = Randoms.randomInt(100);
    final String searchServiceEndpoint = randomString();
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName, randomString(),
            searchServiceEndpoint);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final SearchRequest searchRequest = buildSearchRequest(queryType, query);
    final SearchResult searchResult = new SearchResult().withHits(getExpectedHits(document));
    final SortOrder sortOrder = new SortOrder();
    sortOrder.addSortingOption(new SortingOption(Randoms.randomString(), Direction.DESCENDING));
    sortOrder.addSortingOption(new SortingOption(Randoms.randomString(), Direction.ASCENDING));

    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(searchServiceEndpoint)).thenReturn(mockCloudSearchClient);
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    when(documentConfigurationHolder.schemaName()).thenReturn(schemaName);
    when(documentConfigurationHolder.documentConfigurations()).thenReturn(documentConfigurations);
    when(query.queryType()).thenReturn(queryType);

    searchRequest.withStart((long) start);
    searchRequest.withSize((long) size);
    final StringBuilder sort = new StringBuilder();
    String direction = null;
    int count = 0;
    for (final SortingOption sortingOption : sortOrder.sortingOptions()) {
        count++;
        sort.append(sortingOption.key() + " ");
        switch (sortingOption.direction()) {
            case ASCENDING:
            default:
                direction = "asc";
                break;
            case DESCENDING:
                direction = "desc";
                break;

        }
        sort.append(direction);
        if (count < sortOrder.sortingOptions().size()) {
            sort.append(", ");
        }
    }
    searchRequest.setSort(sort.toString());
    when(mockCloudSearchClient.search(searchRequest)).thenReturn(searchResult);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    final DocumentSearchResponse<StubDocument> returnedDocuments = cloudSearchEngine.search(query, start, size,
            StubDocument.class, new SearchOptions().withSortOrder(sortOrder));

    // Then
    assertNotNull(returnedDocuments);
    assertEquals(document, returnedDocuments.getHits().get(0));
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldSearch_withDefaultSortInQuery() throws Exception {
    // Given
    final StubDocument document = randomStubDocument();
    final String documentId = document.getId();
    final DocumentConfigurationHolder documentConfigurationHolder = mock(DocumentConfigurationHolder.class);
    final String namespace = documentId;
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final Query query = mock(Query.class);
    final QueryType queryType = Randoms.randomEnum(QueryType.class);
    final AmazonCloudSearchDomain mockCloudSearchClient = mock(AmazonCloudSearchDomain.class);
    final Integer start = Randoms.randomInt(100);
    final Integer size = Randoms.randomInt(100);
    final String searchServiceEndpoint = randomString();
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName, randomString(),
            searchServiceEndpoint);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final SearchRequest searchRequest = buildSearchRequest(queryType, query);
    final SearchResult searchResult = new SearchResult().withHits(getExpectedHits(document));

    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(searchServiceEndpoint)).thenReturn(mockCloudSearchClient);
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    when(documentConfigurationHolder.schemaName()).thenReturn(schemaName);
    when(documentConfigurationHolder.documentConfigurations()).thenReturn(documentConfigurations);
    when(query.queryType()).thenReturn(queryType);

    searchRequest.withStart((long) start);
    searchRequest.withSize((long) size);

    when(mockCloudSearchClient.search(searchRequest)).thenReturn(searchResult);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    final DocumentSearchResponse<StubDocument> returnedDocuments = cloudSearchEngine.search(query, start, size,
            StubDocument.class, SearchOptions.DEFAULT);

    // Then
    assertNotNull(returnedDocuments);
    assertEquals(document, returnedDocuments.getHits().get(0));
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shouldNotSearch_withNullSearchOptions() throws Exception {
    // Given
    final StubDocument document = randomStubDocument();
    final String documentId = document.getId();
    final DocumentConfigurationHolder documentConfigurationHolder = mock(DocumentConfigurationHolder.class);
    final String namespace = documentId;
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final AmazonCloudSearchDomain mockCloudSearchClient = mock(AmazonCloudSearchDomain.class);
    final String searchServiceEndpoint = randomString();
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName, randomString(),
            searchServiceEndpoint);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(searchServiceEndpoint)).thenReturn(mockCloudSearchClient);
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    when(documentConfigurationHolder.schemaName()).thenReturn(schemaName);
    when(documentConfigurationHolder.documentConfigurations()).thenReturn(documentConfigurations);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    IllegalArgumentException actualException = null;
    try {
        cloudSearchEngine.search(mock(Query.class), Randoms.randomInt(100), Randoms.randomInt(100),
                StubDocument.class, null);
    } catch (final IllegalArgumentException e) {
        actualException = e;
    }

    // Then
    assertNotNull(actualException);
    assertEquals("SearchOptions cannot be null", actualException.getMessage());
}
项目:Cheddar    文件:CloudSearchEngineTest.java   
@Test
public void shoulNotSearch_withQuery() throws Exception {
    // Given
    final StubDocument document = randomStubDocument();
    final String documentId = document.getId();
    final DocumentConfigurationHolder documentConfigurationHolder = mock(DocumentConfigurationHolder.class);
    final String namespace = documentId;
    final DocumentConfiguration mockStubDocumentConfiguration = mock(DocumentConfiguration.class);
    final Map<String, PropertyDescriptor> properties = getStubDocumentPropertyDescriptors();
    final Collection<DocumentConfiguration> documentConfigurations = Arrays.asList(mockStubDocumentConfiguration);
    final String schemaName = randomString(10);
    final Query query = mock(Query.class);
    final QueryType queryType = randomEnum(QueryType.class);
    final AmazonCloudSearchDomain mockCloudSearchClient = mock(AmazonCloudSearchDomain.class);
    final Integer start = Randoms.randomInt(100);
    final Integer size = Randoms.randomInt(100);
    final String searchServiceEndpoint = randomString();
    final String domainName = schemaName + "-" + namespace;
    final DescribeDomainsResult describeDomainsResult = getDescribeDomainsResult(domainName, randomString(),
            searchServiceEndpoint);
    final AmazonCloudSearch mockAmazonCloudSearch = mock(AmazonCloudSearch.class);
    final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest()
            .withDomainNames(Arrays.asList(domainName));
    final SearchRequest searchRequest = buildSearchRequest(queryType, query);
    mockStatic(AmazonCloudSearchDomainClientBuilder.class);
    when(AmazonCloudSearchDomainClientBuilder.build(searchServiceEndpoint)).thenReturn(mockCloudSearchClient);
    doReturn(StubDocument.class).when(mockStubDocumentConfiguration).documentClass();
    when(mockStubDocumentConfiguration.properties()).thenReturn(properties);
    when(mockStubDocumentConfiguration.namespace()).thenReturn(namespace);
    when(mockAmazonCloudSearch.describeDomains(describeDomainsRequest)).thenReturn(describeDomainsResult);
    when(documentConfigurationHolder.schemaName()).thenReturn(schemaName);
    when(documentConfigurationHolder.documentConfigurations()).thenReturn(documentConfigurations);
    when(query.queryType()).thenReturn(queryType);
    searchRequest.withStart((long) start);
    searchRequest.withSize((long) size);
    when(mockCloudSearchClient.search(searchRequest)).thenThrow(AmazonServiceException.class);

    final CloudSearchEngine cloudSearchEngine = new CloudSearchEngine(documentConfigurationHolder,
            mockAmazonCloudSearch);

    // When
    PersistenceResourceFailureException actualException = null;
    try {
        cloudSearchEngine.search(query, start, size, StubDocument.class);
    } catch (final PersistenceResourceFailureException e) {
        actualException = e;
    }

    // Then
    assertNotNull(actualException);
}
项目:storm-crawler    文件:CloudSearchIndexerBolt.java   
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void prepare(Map conf, TopologyContext context,
        OutputCollector collector) {
    super.prepare(conf, context, collector);
    _collector = collector;

    this.eventCounter = context.registerMetric("CloudSearchIndexer",
            new MultiCountMetric(), 10);

    maxTimeBuffered = ConfUtils.getInt(conf,
            CloudSearchConstants.MAX_TIME_BUFFERED, 10);

    maxDocsInBatch = ConfUtils.getInt(conf,
            CloudSearchConstants.MAX_DOCS_BATCH, -1);

    buffer = new StringBuffer(MAX_SIZE_BATCH_BYTES).append('[');

    dumpBatchFilesToTemp = ConfUtils.getBoolean(conf,
            "cloudsearch.batch.dump", false);

    if (dumpBatchFilesToTemp) {
        // only dumping to local file
        // no more config required
        return;
    }

    String endpoint = ConfUtils.getString(conf, "cloudsearch.endpoint");

    if (StringUtils.isBlank(endpoint)) {
        String message = "Missing CloudSearch endpoint";
        LOG.error(message);
        throw new RuntimeException(message);
    }

    String regionName = ConfUtils.getString(conf,
            CloudSearchConstants.REGION);

    AmazonCloudSearchClient cl = new AmazonCloudSearchClient();
    if (StringUtils.isNotBlank(regionName)) {
        cl.setRegion(RegionUtils.getRegion(regionName));
    }

    String domainName = null;

    // retrieve the domain name
    DescribeDomainsResult domains = cl
            .describeDomains(new DescribeDomainsRequest());

    Iterator<DomainStatus> dsiter = domains.getDomainStatusList()
            .iterator();
    while (dsiter.hasNext()) {
        DomainStatus ds = dsiter.next();
        if (ds.getDocService().getEndpoint().equals(endpoint)) {
            domainName = ds.getDomainName();
            break;
        }
    }
    // check domain name
    if (StringUtils.isBlank(domainName)) {
        throw new RuntimeException(
                "No domain name found for CloudSearch endpoint");
    }

    DescribeIndexFieldsResult indexDescription = cl
            .describeIndexFields(new DescribeIndexFieldsRequest()
                    .withDomainName(domainName));
    for (IndexFieldStatus ifs : indexDescription.getIndexFields()) {
        String indexname = ifs.getOptions().getIndexFieldName();
        String indextype = ifs.getOptions().getIndexFieldType();
        LOG.info("CloudSearch index name {} of type {}", indexname,
                indextype);
        csfields.put(indexname, indextype);
    }

    client = new AmazonCloudSearchDomainClient();
    client.setEndpoint(endpoint);
}
项目:mosquito-report-api    文件:IndexerTest.java   
@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();
}