22
22
import java .util .Map ;
23
23
24
24
import org .elasticsearch .index .query .QueryBuilders ;
25
- import org .junit .jupiter .api .AfterEach ;
26
25
import org .junit .jupiter .api .BeforeEach ;
26
+ import org .junit .jupiter .api .Order ;
27
27
import org .junit .jupiter .api .Test ;
28
28
import org .springframework .beans .factory .annotation .Autowired ;
29
+ import org .springframework .context .annotation .Bean ;
29
30
import org .springframework .context .annotation .Configuration ;
30
31
import org .springframework .context .annotation .Import ;
31
32
import org .springframework .data .annotation .Id ;
42
43
import org .springframework .data .elasticsearch .junit .jupiter .SpringIntegrationTest ;
43
44
import org .springframework .data .elasticsearch .repository .ElasticsearchRepository ;
44
45
import org .springframework .data .elasticsearch .repository .config .EnableElasticsearchRepositories ;
45
- import org .springframework .data .elasticsearch .utils .IndexInitializer ;
46
+ import org .springframework .data .elasticsearch .utils .IndexNameProvider ;
46
47
import org .springframework .test .context .ContextConfiguration ;
47
48
48
49
/**
@@ -59,31 +60,34 @@ public class DynamicSettingAndMappingEntityRepositoryTests {
59
60
@ Configuration
60
61
@ Import ({ ElasticsearchRestTemplateConfiguration .class })
61
62
@ EnableElasticsearchRepositories (considerNestedRepositories = true )
62
- static class Config {}
63
+ static class Config {
64
+ @ Bean
65
+ IndexNameProvider indexNameProvider () {
66
+ return new IndexNameProvider ();
67
+ }
68
+ }
63
69
64
70
@ Autowired private ElasticsearchOperations operations ;
65
71
private IndexOperations indexOperations ;
66
-
72
+ @ Autowired IndexNameProvider indexNameProvider ;
67
73
@ Autowired private DynamicSettingAndMappingEntityRepository repository ;
68
74
69
75
@ BeforeEach
70
76
public void before () {
77
+ indexNameProvider .increment ();
71
78
indexOperations = operations .indexOps (DynamicSettingAndMappingEntity .class );
72
- IndexInitializer . init ( indexOperations );
79
+ indexOperations . createWithMapping ( );
73
80
}
74
81
75
- @ AfterEach
76
- void after () {
77
- indexOperations .delete ();
82
+ @ Test
83
+ @ Order (Integer .MAX_VALUE )
84
+ void cleanup () {
85
+ operations .indexOps (IndexCoordinates .of ("*" )).delete ();
78
86
}
79
87
80
88
@ Test // DATAES-64
81
89
public void shouldCreateGivenDynamicSettingsForGivenIndex () {
82
90
83
- // given
84
- // delete , create and apply mapping in before method
85
-
86
- // then
87
91
assertThat (indexOperations .exists ()).isTrue ();
88
92
Map <String , Object > map = indexOperations .getSettings ();
89
93
assertThat (map .containsKey ("index.number_of_replicas" )).isTrue ();
@@ -97,7 +101,6 @@ public void shouldCreateGivenDynamicSettingsForGivenIndex() {
97
101
@ Test // DATAES-64
98
102
public void shouldSearchOnGivenTokenizerUsingGivenDynamicSettingsForGivenIndex () {
99
103
100
- // given
101
104
DynamicSettingAndMappingEntity dynamicSettingAndMappingEntity1 = new DynamicSettingAndMappingEntity ();
102
105
dynamicSettingAndMappingEntity1 .setId (nextIdAsString ());
103
106
dynamicSettingAndMappingEntity1 .setName ("test-setting1" );
@@ -112,16 +115,14 @@ public void shouldSearchOnGivenTokenizerUsingGivenDynamicSettingsForGivenIndex()
112
115
113
116
repository .save (dynamicSettingAndMappingEntity2 );
114
117
115
- // when
116
118
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder ()
117
119
.withQuery (QueryBuilders .termQuery ("email" , dynamicSettingAndMappingEntity1 .getEmail ())).build ();
118
120
119
- IndexCoordinates index = IndexCoordinates .of ("test-index-dynamic-setting-and-mapping" );
121
+ IndexCoordinates index = IndexCoordinates .of (indexNameProvider . indexName () );
120
122
long count = operations .count (searchQuery , DynamicSettingAndMappingEntity .class , index );
121
123
SearchHits <DynamicSettingAndMappingEntity > entityList = operations .search (searchQuery ,
122
124
DynamicSettingAndMappingEntity .class , index );
123
125
124
- // then
125
126
assertThat (count ).isEqualTo (1L );
126
127
assertThat (entityList ).isNotNull ().hasSize (1 );
127
128
assertThat (entityList .getSearchHit (0 ).getContent ().getEmail ())
@@ -131,13 +132,8 @@ public void shouldSearchOnGivenTokenizerUsingGivenDynamicSettingsForGivenIndex()
131
132
@ Test
132
133
public void shouldGetMappingForGivenIndexAndType () {
133
134
134
- // given
135
- // delete , create and apply mapping in before method
136
-
137
- // when
138
135
Map <String , Object > mapping = indexOperations .getMapping ();
139
136
140
- // then
141
137
Map <String , Object > properties = (Map <String , Object >) mapping .get ("properties" );
142
138
assertThat (mapping ).isNotNull ();
143
139
assertThat (properties ).isNotNull ();
@@ -176,9 +172,6 @@ public void shouldCreateMappingWithSpecifiedMappings() {
176
172
@ Test // DATAES-86
177
173
public void shouldCreateMappingWithUsingMappingAnnotation () {
178
174
179
- // given
180
-
181
- // then
182
175
Map <String , Object > mapping = indexOperations .getMapping ();
183
176
Map <String , Object > properties = (Map <String , Object >) mapping .get ("properties" );
184
177
assertThat (mapping ).isNotNull ();
@@ -188,10 +181,7 @@ public void shouldCreateMappingWithUsingMappingAnnotation() {
188
181
assertThat (emailProperties .get ("analyzer" )).isEqualTo ("emailAnalyzer" );
189
182
}
190
183
191
- /**
192
- * @author Mohsin Husen
193
- */
194
- @ Document (indexName = "test-index-dynamic-setting-and-mapping" )
184
+ @ Document (indexName = "#{@indexNameProvider.indexName()}" )
195
185
@ Setting (settingPath = "/settings/test-settings.json" )
196
186
@ Mapping (mappingPath = "/mappings/test-mappings.json" )
197
187
static class DynamicSettingAndMappingEntity {
@@ -225,9 +215,6 @@ public void setEmail(String email) {
225
215
}
226
216
}
227
217
228
- /**
229
- * @author Mohsin Husen
230
- */
231
218
public interface DynamicSettingAndMappingEntityRepository
232
219
extends ElasticsearchRepository <DynamicSettingAndMappingEntity , String > {}
233
220
0 commit comments