/*
  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 shouldSortByGivenField() {
    // todo
    // given
    String documentId = randomNumeric(5);
    SampleEntity sampleEntity = new SampleEntity();
    sampleEntity.setId(documentId);
    sampleEntity.setMessage("world");
    repository.save(sampleEntity);

    String documentId2 = randomNumeric(5);
    SampleEntity sampleEntity2 = new SampleEntity();
    sampleEntity2.setId(documentId2);
    sampleEntity2.setMessage("hello");
    repository.save(sampleEntity2);
    // when
    Iterable<SampleEntity> sampleEntities =
        repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC, "message")));
    // then
    assertThat(sampleEntities, 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()));
 }
 @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 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)));
  }
  @Test
  public void shouldReturnSimilarEntities() {
    // given
    String sampleMessage =
        "So we build a web site or an application and want to add search to it, "
            + "and then it hits us: getting search working is hard. We want our search solution to be fast,"
            + " we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, "
            + "we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, "
            + "we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";

    List<SampleEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
    repository.save(sampleEntities);

    // when
    Page<SampleEntity> results =
        repository.searchSimilar(
            sampleEntities.get(0), new String[] {"message"}, new PageRequest(0, 5));

    // then
    assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));
  }
  @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 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()));
  }