@Test public void shouldDeleteAll() { // when repository.deleteAll(); // then SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build(); Page<SampleEntity> sampleEntities = repository.search(searchQuery); assertThat(sampleEntities.getTotalElements(), equalTo(0L)); }
@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 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 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 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); }