/*
  DATAES-82
  */
  @Test
  public void shouldFindAllByIdQuery() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("hello world.");
    sampleEntity.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity);

    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("hello world.");
    sampleEntity2.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity2);

    // when
    Iterable<SampleEntity> sampleEntities =
        repository.findAll(Arrays.asList(documentId, documentId2));

    // then
    assertNotNull("sample entities cant be null..", sampleEntities);
    List<SampleEntity> entities = Lists.newArrayList(sampleEntities);
    assertThat(entities.size(), is(2));
  }
 @Test
 public void shouldSaveDocumentWithoutId() {
   // given
   SampleEntity sampleEntity = new SampleEntity();
   sampleEntity.setMessage("some message");
   sampleEntity.setVersion(System.currentTimeMillis());
   // when
   repository.save(sampleEntity);
   // then
   assertThat(sampleEntity.getId(), is(notNullValue()));
 }
  @Test
  public void shouldReturnIterableEntities() {
    // given
    String documentId1 = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId1);
    sampleEntity1.setMessage("hello world.");
    sampleEntity1.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity1);

    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("hello world.");
    sampleEntity2.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity2);

    // when
    Iterable<SampleEntity> sampleEntities = repository.search(termQuery("id", documentId1));
    // then
    assertNotNull("sample entities cant be null..", sampleEntities);
  }
 @Test
 public void shouldSaveDocument() {
   // given
   String documentId = randomNumeric(5);
   SampleEntity sampleEntity = new SampleEntity();
   sampleEntity.setId(documentId);
   sampleEntity.setMessage("some message");
   sampleEntity.setVersion(System.currentTimeMillis());
   // when
   repository.save(sampleEntity);
   // then
   SampleEntity entityFromElasticSearch = repository.findOne(documentId);
   assertThat(entityFromElasticSearch, is(notNullValue()));
 }
 private static List<SampleEntity> createSampleEntitiesWithMessage(
     String message, int numberOfEntities) {
   List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
   for (int i = 0; i < numberOfEntities; i++) {
     String documentId = randomNumeric(5);
     SampleEntity sampleEntity = new SampleEntity();
     sampleEntity.setId(documentId);
     sampleEntity.setMessage(message);
     sampleEntity.setRate(2);
     sampleEntity.setVersion(System.currentTimeMillis());
     sampleEntities.add(sampleEntity);
   }
   return sampleEntities;
 }
  @Test
  public void shouldDeleteIterableEntities() {
    // given
    String documentId1 = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId1);
    sampleEntity1.setMessage("hello world.");
    sampleEntity1.setVersion(System.currentTimeMillis());

    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("hello world.");
    sampleEntity2.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity2);

    Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity2, sampleEntity2);
    // when
    repository.delete(sampleEntities);
    // then
    assertThat(repository.findOne(documentId1), is(nullValue()));
    assertThat(repository.findOne(documentId2), is(nullValue()));
  }
  @Test
  public void shouldSaveIterableEntities() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId);
    sampleEntity1.setMessage("hello world.");
    sampleEntity1.setVersion(System.currentTimeMillis());

    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("hello world.");
    sampleEntity2.setVersion(System.currentTimeMillis());

    Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity1, sampleEntity2);
    // when
    repository.save(sampleEntities);
    // then
    Page<SampleEntity> entities =
        repository.search(termQuery("id", documentId), new PageRequest(0, 50));
    assertNotNull(entities);
  }
 @Test
 public void shouldReturnCountOfDocuments() {
   // given
   String documentId = randomNumeric(5);
   SampleEntity sampleEntity = new SampleEntity();
   sampleEntity.setId(documentId);
   sampleEntity.setMessage("some message");
   sampleEntity.setVersion(System.currentTimeMillis());
   repository.save(sampleEntity);
   // when
   Long count = repository.count();
   // then
   assertThat(count, is(greaterThanOrEqualTo(1L)));
 }
  @Test
  public void shouldDoBulkIndexDocument() {
    // given
    String documentId1 = randomNumeric(5);
    SampleEntity sampleEntity1 = new SampleEntity();
    sampleEntity1.setId(documentId1);
    sampleEntity1.setMessage("some message");
    sampleEntity1.setVersion(System.currentTimeMillis());

    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("some message");
    sampleEntity2.setVersion(System.currentTimeMillis());

    // when
    repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
    // then
    SampleEntity entity1FromElasticSearch = repository.findOne(documentId1);
    assertThat(entity1FromElasticSearch, is(notNullValue()));

    SampleEntity entity2FromElasticSearch = repository.findOne(documentId2);
    assertThat(entity2FromElasticSearch, is(notNullValue()));
  }
 @Test
 public void shouldIndexEntity() {
   // given
   String documentId = randomNumeric(5);
   SampleEntity sampleEntity = new SampleEntity();
   sampleEntity.setId(documentId);
   sampleEntity.setVersion(System.currentTimeMillis());
   sampleEntity.setMessage("some message");
   // when
   repository.index(sampleEntity);
   // then
   Page<SampleEntity> entities =
       repository.search(termQuery("id", documentId), new PageRequest(0, 50));
   assertThat(entities.getTotalElements(), equalTo(1L));
 }
 @Test
 public void shouldReturnResultsForGivenSearchQuery() {
   // given
   String documentId = randomNumeric(5);
   SampleEntity sampleEntity = new SampleEntity();
   sampleEntity.setId(documentId);
   sampleEntity.setMessage("hello world.");
   sampleEntity.setVersion(System.currentTimeMillis());
   repository.save(sampleEntity);
   // when
   SearchQuery searchQuery =
       new NativeSearchQueryBuilder().withQuery(termQuery("id", documentId)).build();
   Page<SampleEntity> sampleEntities = repository.search(searchQuery);
   // then
   assertThat(sampleEntities.getTotalElements(), equalTo(1L));
 }
  @Test
  public void shouldReturnTrueGivenDocumentWithIdExists() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("hello world.");
    sampleEntity.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity);

    // when
    boolean exist = repository.exists(documentId);

    // then
    assertEquals(exist, true);
  }
 @Test
 public void shouldSearchDocumentsGivenElasticsearchQuery() {
   // given
   String documentId = randomNumeric(5);
   SampleEntity sampleEntity = new SampleEntity();
   sampleEntity.setId(documentId);
   sampleEntity.setMessage("hello world.");
   sampleEntity.setVersion(System.currentTimeMillis());
   repository.save(sampleEntity);
   // when
   Page<SampleEntity> page =
       repository.search(termQuery("message", "world"), new PageRequest(0, 50));
   // then
   assertThat(page, is(notNullValue()));
   assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
 }
  @Test
  public void shouldSearchDocumentsGivenSearchQuery() {
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("some test message");
    sampleEntity.setVersion(System.currentTimeMillis());
    repository.save(sampleEntity);

    SearchQuery query =
        new NativeSearchQueryBuilder().withQuery(termQuery("message", "test")).build();
    // when
    Page<SampleEntity> page = repository.search(query);
    // then
    assertThat(page, is(notNullValue()));
    assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
  }