Skip to content

Commit 4fa058f

Browse files
David Porcherontinesoft
David Porcheron
authored andcommitted
feat(core): replace ElasticsearchTemplate by ElasticsearchOperations to support upcoming ElasticsearchRestTemplate as well
1 parent f0d6185 commit 4fa058f

File tree

7 files changed

+46
-46
lines changed

7 files changed

+46
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
`spring-esdata-loader` is a Java testing library to help you write integration tests for your [spring-data elasticsearch](https://spring.io/projects/spring-data-elasticsearch)-based projects, by allowing you to easily load data into Elasticsearch, using entity mappings (i.e domain classes annotated with `@Document`, `@Field`, etc) and via specific **Junit 4**'s Rules or **JUnit Jupiter**'s Extensions.
66

7-
The library reads all the metadata it needs from the entity classes (index name, index type, etc) , uses them to create/refresh the index on the ES server and feeds it with the data using the `ElasticsearchTemplate` present in your test application context.
7+
The library reads all the metadata it needs from the entity classes (index name, index type, etc) , uses them to create/refresh the index on the ES server and feeds it with the data using the `ElasticsearchOperations` present in your test application context.
88

99
## Features
1010

core/src/main/java/com/github/spring/esdata/loader/core/SpringEsDataLoader.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
8+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
99
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
1010
import org.springframework.data.elasticsearch.core.query.IndexQuery;
1111
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
@@ -17,7 +17,7 @@
1717
import java.util.zip.GZIPInputStream;
1818

1919
/**
20-
* Loader that use Spring Data's {@link ElasticsearchTemplate} to load data into Elasticsearch.
20+
* Loader that use Spring Data's {@link ElasticsearchOperations} to load data into Elasticsearch.
2121
*
2222
* @author tinesoft
2323
*
@@ -27,15 +27,15 @@ public class SpringEsDataLoader implements EsDataLoader {
2727
private static final Logger LOGGER = LoggerFactory.getLogger(SpringEsDataLoader.class);
2828
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
2929

30-
private final ElasticsearchTemplate esTemplate;
30+
private final ElasticsearchOperations esOperations;
3131

3232
/**.
33-
* Data loader that use Spring's {@link ElasticsearchTemplate} to load data into Elasticsearch
34-
* @param esTemplate the {@link ElasticsearchTemplate}
33+
* Data loader that use Spring's {@link ElasticsearchOperations} to load data into Elasticsearch
34+
* @param esOperations the {@link ElasticsearchOperations}
3535
*/
3636
@Autowired
37-
public SpringEsDataLoader(final ElasticsearchTemplate esTemplate) {
38-
this.esTemplate = esTemplate;
37+
public SpringEsDataLoader(final ElasticsearchOperations esOperations) {
38+
this.esOperations = esOperations;
3939
}
4040

4141
/**
@@ -46,10 +46,10 @@ public SpringEsDataLoader(final ElasticsearchTemplate esTemplate) {
4646
@Override
4747
public void delete(Class<?> esEntityClass) {
4848
LOGGER.debug("Dropping data in Index '{}'...", esEntityClass.getSimpleName());
49-
this.esTemplate.deleteIndex(esEntityClass);
50-
this.esTemplate.createIndex(esEntityClass);
51-
this.esTemplate.putMapping(esEntityClass);
52-
this.esTemplate.refresh(esEntityClass);
49+
this.esOperations.deleteIndex(esEntityClass);
50+
this.esOperations.createIndex(esEntityClass);
51+
this.esOperations.putMapping(esEntityClass);
52+
this.esOperations.refresh(esEntityClass);
5353

5454
}
5555

@@ -62,12 +62,12 @@ public void load(final IndexData d) {
6262

6363
// first recreate the index
6464
LOGGER.debug("Recreating Index for '{}'...", d.getEsEntityClass().getSimpleName());
65-
this.esTemplate.deleteIndex(d.esEntityClass);
66-
this.esTemplate.createIndex(d.esEntityClass);
67-
this.esTemplate.putMapping(d.esEntityClass);
68-
this.esTemplate.refresh(d.esEntityClass);
65+
this.esOperations.deleteIndex(d.esEntityClass);
66+
this.esOperations.createIndex(d.esEntityClass);
67+
this.esOperations.putMapping(d.esEntityClass);
68+
this.esOperations.refresh(d.esEntityClass);
6969

70-
ElasticsearchPersistentEntity<?> esEntityInfo = this.esTemplate.getPersistentEntityFor(d.esEntityClass);
70+
ElasticsearchPersistentEntity<?> esEntityInfo = this.esOperations.getPersistentEntityFor(d.esEntityClass);
7171

7272
LOGGER.debug("Inserting data in Index of '{}'. Please wait...", d.getEsEntityClass().getSimpleName());
7373

@@ -84,8 +84,8 @@ public void load(final IndexData d) {
8484
.limit(d.nbMaxItems)//
8585
.collect(Collectors.toList());
8686

87-
this.esTemplate.bulkIndex(indexQueries);
88-
this.esTemplate.refresh(d.esEntityClass);
87+
this.esOperations.bulkIndex(indexQueries);
88+
this.esOperations.refresh(d.esEntityClass);
8989

9090
LOGGER.debug("Insertion successfully done");
9191
} catch (IOException e) {

core/src/main/java/com/github/spring/esdata/loader/core/SpringUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55
import org.springframework.context.ApplicationContext;
6-
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
6+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
77

88
import java.util.Map;
99

@@ -44,7 +44,7 @@ public static <T> T getBeanOfType(ApplicationContext applicationContext, final C
4444
}
4545

4646
/**
47-
* Retrieve a {@link ElasticsearchTemplate} from the {@link ApplicationContext} and creates a {@link EsDataLoader} from it.
47+
* Retrieve a {@link ElasticsearchOperations} from the {@link ApplicationContext} and creates a {@link EsDataLoader} from it.
4848
*
4949
* @param appContext the Spring {@link ApplicationContext}
5050
* @return a {@link EsDataLoader} that can insert or remove data from the underlying ES Server.
@@ -57,15 +57,15 @@ public static EsDataLoader getDataLoader(final ApplicationContext appContext) {
5757
throw new IllegalStateException("Missing 'ApplicationContext' field in class under test");
5858
}
5959

60-
ElasticsearchTemplate esTemplate = SpringUtils.getBeanOfType(appContext, ElasticsearchTemplate.class);
60+
ElasticsearchOperations esOperations = SpringUtils.getBeanOfType(appContext, ElasticsearchOperations.class);
6161

62-
if (esTemplate == null) {
63-
LOGGER.error("No Spring's bean of type 'ElasticsearchTemplate' was found!");
62+
if (esOperations == null) {
63+
LOGGER.error("No Spring's bean of type 'ElasticsearchOperations' was found!");
6464
throw new IllegalStateException(
65-
"Missing bean of type 'ElasticsearchTemplate' in your Spring configuration");
65+
"Missing bean of type 'ElasticsearchOperations' in your Spring configuration");
6666
}
6767

68-
return new SpringEsDataLoader(esTemplate);
68+
return new SpringEsDataLoader(esOperations);
6969
}
7070

7171
}

demo/src/test/java/com/github/spring/esdata/loader/demo/junit/jupiter/DeleteEsDataExtensionTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.springframework.boot.test.mock.mockito.SpyBean;
1515
import org.springframework.context.ApplicationContextInitializer;
1616
import org.springframework.context.ConfigurableApplicationContext;
17-
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
17+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
1818
import org.springframework.test.context.ContextConfiguration;
1919
import org.testcontainers.elasticsearch.ElasticsearchContainer;
2020
import org.testcontainers.junit.jupiter.Container;
@@ -50,24 +50,24 @@ public class DeleteEsDataExtensionTest {
5050
public static final ElasticsearchContainer ES_CONTAINER = new ElasticsearchContainer(DemoTestPropertyValues.ES_DOCKER_IMAGE_VERSION);
5151

5252
@SpyBean(reset = MockReset.NONE)
53-
private ElasticsearchTemplate esMockTemplate;
53+
private ElasticsearchOperations esMockOperations;
5454

5555
@Test
5656
public void dataDeletedAtClassLevel() {
5757

58-
verify(this.esMockTemplate).deleteIndex(AuthorEsEntity.class);
59-
verify(this.esMockTemplate).deleteIndex(BookEsEntity.class);
60-
verify(this.esMockTemplate, never()).deleteIndex(LibraryEsEntity.class);//removed at method level, see below #dataDeleteedAtMethodLevel()
58+
verify(this.esMockOperations).deleteIndex(AuthorEsEntity.class);
59+
verify(this.esMockOperations).deleteIndex(BookEsEntity.class);
60+
verify(this.esMockOperations, never()).deleteIndex(LibraryEsEntity.class);//removed at method level, see below #dataDeleteedAtMethodLevel()
6161
}
6262

6363
@Test
6464
// the following data will be removed for this test only
6565
@DeleteEsData(esEntityClasses = {LibraryEsEntity.class})
6666
public void dataDeletedAtMethodLevel() {
6767

68-
verify(this.esMockTemplate).deleteIndex(AuthorEsEntity.class);//removed at class level, before all tests
69-
verify(this.esMockTemplate).deleteIndex(BookEsEntity.class);//removed at class level, before all tests
70-
verify(this.esMockTemplate).deleteIndex(LibraryEsEntity.class);
68+
verify(this.esMockOperations).deleteIndex(AuthorEsEntity.class);//removed at class level, before all tests
69+
verify(this.esMockOperations).deleteIndex(BookEsEntity.class);//removed at class level, before all tests
70+
verify(this.esMockOperations).deleteIndex(LibraryEsEntity.class);
7171
}
7272

7373
public static class ExposedDockerizedEsConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {

demo/src/test/java/com/github/spring/esdata/loader/demo/junit4/DeleteEsDataRuleTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.springframework.boot.test.mock.mockito.SpyBean;
2020
import org.springframework.context.ApplicationContextInitializer;
2121
import org.springframework.context.ConfigurableApplicationContext;
22-
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
22+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
2323
import org.springframework.test.context.ContextConfiguration;
2424
import org.springframework.test.context.junit4.SpringRunner;
2525
import org.testcontainers.elasticsearch.ElasticsearchContainer;
@@ -62,24 +62,24 @@ public class DeleteEsDataRuleTest {
6262
public final static TestRule RUlE = RuleChain.outerRule(ES_CONTAINER).around(ES_DATA_REMOVER_RULE);
6363

6464
@SpyBean(reset = MockReset.NONE)
65-
private ElasticsearchTemplate esMockTemplate;
65+
private ElasticsearchOperations esMockOperations;
6666

6767
@Test
6868
public void dataDeletedAtClassLevel() {
6969

70-
verify(this.esMockTemplate).deleteIndex(AuthorEsEntity.class);
71-
verify(this.esMockTemplate).deleteIndex(BookEsEntity.class);
72-
verify(this.esMockTemplate, never()).deleteIndex(LibraryEsEntity.class);//removed at method level, see below #dataDeleteedAtMethodLevel()
70+
verify(this.esMockOperations).deleteIndex(AuthorEsEntity.class);
71+
verify(this.esMockOperations).deleteIndex(BookEsEntity.class);
72+
verify(this.esMockOperations, never()).deleteIndex(LibraryEsEntity.class);//removed at method level, see below #dataDeleteedAtMethodLevel()
7373
}
7474

7575
@Test
7676
// the following data will be removed for this test only
7777
@DeleteEsData(esEntityClasses = {LibraryEsEntity.class})
7878
public void dataDeletedAtMethodLevel() {
7979

80-
verify(this.esMockTemplate).deleteIndex(AuthorEsEntity.class);//removed at class level, before all tests
81-
verify(this.esMockTemplate).deleteIndex(BookEsEntity.class);//removed at class level, before all tests
82-
verify(this.esMockTemplate).deleteIndex(LibraryEsEntity.class);
80+
verify(this.esMockOperations).deleteIndex(AuthorEsEntity.class);//removed at class level, before all tests
81+
verify(this.esMockOperations).deleteIndex(BookEsEntity.class);//removed at class level, before all tests
82+
verify(this.esMockOperations).deleteIndex(LibraryEsEntity.class);
8383
}
8484

8585
public static class ExposedDockerizedEsConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {

junit-jupiter/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.github.spring.esdata.loader.core.LoadEsData;
1313
import com.github.spring.esdata.loader.junit.jupiter.LoadEsDataConfig;
1414
import org.junit.jupiter.api.Test;
1515

16-
//@SpringBootTest or any @ContextConfiguration(..) to initialize the Spring context that contains the ElasticsearchTemplate
16+
//@SpringBootTest or any @ContextConfiguration(..) to initialize the Spring context that contains the ElasticsearchOperations
1717

1818
@LoadEsDataConfig({ // @LoadEsDataConfig is a meta annotation that is itself annotated with @ExtendWith(LoadEsDataExtension.class)
1919
@LoadEsData(esEntityClass=MyEsEntity1.class, location="/path/to/data1.json"),
@@ -46,7 +46,7 @@ import com.github.spring.esdata.loader.core.LoadEsData;
4646
import com.github.spring.esdata.loader.junit.jupiter.LoadEsDataConfig;
4747
import org.junit.jupiter.api.Test;
4848

49-
//@SpringBootTest or any @ContextConfiguration(..) to initialize the Spring context that contains the ElasticsearchTemplate
49+
//@SpringBootTest or any @ContextConfiguration(..) to initialize the Spring context that contains the ElasticsearchOperations
5050

5151
@DeleteEsDataConfig({ // @DeleteEsDataConfig is a meta annotation that is itself annotated with @ExtendWith(LoadEsDataExtension.class)
5252
MyEsEntity1.class,

junit4/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import org.junit.Test;
1717
import org.junit.runner.RunWith;
1818

1919
@RunWith(SpringRunner.class) // required to run JUnit 4 tests with Spring magic
20-
//@SpringBootTest or any @ContextConfiguration(..) to initialize the Spring context that contains the ElasticsearchTemplate
20+
//@SpringBootTest or any @ContextConfiguration(..) to initialize the Spring context that contains the ElasticsearchOperations
2121

2222
@LoadEsData(esEntityClass=MyEsEntity1.class, location="/path/to/data1.json")
2323
@LoadEsData(esEntityClass=MyEsEntity2.class, location="/path/to/data2.json")
@@ -60,7 +60,7 @@ import org.junit.Test;
6060
import org.junit.runner.RunWith;
6161

6262
@RunWith(SpringRunner.class) // required to run JUnit 4 tests with Spring magic
63-
//@SpringBootTest or any @ContextConfiguration(..) to initialize the Spring context that contains the ElasticsearchTemplate
63+
//@SpringBootTest or any @ContextConfiguration(..) to initialize the Spring context that contains the ElasticsearchOperations
6464

6565
@DeleteEsData({MyEsEntity1.class, MyEsEntity2.class})
6666
public class MyJunit4TestClass{

0 commit comments

Comments
 (0)