Polishing.

Use synchronous API for ES test data setup to avoid threading during the bean container startup.

See #636
This commit is contained in:
Mark Paluch
2022-04-26 11:48:37 +02:00
committed by Greg L. Turnquist
parent 13c0dea55c
commit 83cd9e2d5a
5 changed files with 32 additions and 43 deletions

View File

@@ -17,6 +17,9 @@ package example.springdata.elasticsearch.conference;
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
import lombok.Builder;
import lombok.Data;
import java.util.List;
import org.springframework.data.annotation.Id;
@@ -24,9 +27,6 @@ import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import lombok.Builder;
import lombok.Data;
/**
* @author Artur Konczak
* @author Oliver Gierke
@@ -43,16 +43,4 @@ public class Conference {
private GeoPoint location;
private List<String> keywords;
// do not remove it
public Conference() {}
// do not remove it - work around for lombok generated constructor for all params
public Conference(String id, String name, String date, GeoPoint location, List<String> keywords) {
this.id = id;
this.name = name;
this.date = date;
this.location = location;
this.keywords = keywords;
}
}

View File

@@ -74,7 +74,7 @@ class ElasticsearchOperationsTest {
var query = new CriteriaQuery(
new Criteria("keywords").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate)));
var result = operations.search(query, Conference.class, IndexCoordinates.of("conference-index"));
var result = operations.search(query, Conference.class);
assertThat(result).hasSize(3);

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-elasticsearch-reactive-example</artifactId>
@@ -17,6 +18,11 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
@@ -25,6 +31,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

View File

@@ -15,23 +15,14 @@
*/
package example.springdata.elasticsearch.conference;
import reactor.test.StepVerifier;
import java.io.IOException;
import java.util.Arrays;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
/**
@@ -40,27 +31,20 @@ import org.springframework.data.elasticsearch.core.geo.GeoPoint;
@SpringBootApplication
class ApplicationConfiguration {
@Autowired ReactiveElasticsearchOperations operations;
@Autowired RestHighLevelClient client;
@Autowired ElasticsearchOperations operations;
@Autowired ConferenceRepository repository;
@PreDestroy
public void deleteIndex() {
try {
client.indices().delete(new DeleteIndexRequest("conference-index"), RequestOptions.DEFAULT);
} catch (IOException | ElasticsearchStatusException e) {
// just ignore it
}
operations.indexOps(Conference.class).delete();
}
@PostConstruct
public void insertDataSample() {
try {
client.indices().create(new CreateIndexRequest("conference-index"), RequestOptions.DEFAULT);
} catch (IOException | ElasticsearchStatusException e) {
// just ignore it
}
operations.indexOps(Conference.class).refresh();
// Save data sample
var documents = Arrays.asList(
Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
@@ -75,7 +59,8 @@ class ApplicationConfiguration {
Conference.builder().date("2014-10-04").name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
.location(new GeoPoint(50.0646501D, 19.9449799)).build());
// Remove all documents
repository.deleteAll().then(repository.saveAll(documents).then()).as(StepVerifier::create).verifyComplete();
operations.save(documents);
operations.indexOps(Conference.class).refresh(); // ensure we have all documents properly refreshed to avoid races
// between tests.
}
}

View File

@@ -71,7 +71,7 @@ class ReactiveElasticsearchOperationsTest {
var expectedDate = "2014-10-29";
var expectedWord = "java";
var query = new CriteriaQuery(
new Criteria("keywords").contains(expectedWord).and("date").greaterThanEqual(expectedDate));
new Criteria("keywords").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate)));
operations.search(query, Conference.class) //
.as(StepVerifier::create) //