1
|
package cz.zcu.kiv.backendapi.catalog;
|
2
|
|
3
|
import cz.zcu.kiv.backendapi.alternativename.AlternativeName;
|
4
|
import cz.zcu.kiv.backendapi.alternativename.AlternativeNameRepository;
|
5
|
import cz.zcu.kiv.backendapi.bibliography.Bibliography;
|
6
|
import cz.zcu.kiv.backendapi.bibliography.BibliographyRepository;
|
7
|
import cz.zcu.kiv.backendapi.country.Country;
|
8
|
import cz.zcu.kiv.backendapi.country.CountryRepository;
|
9
|
import cz.zcu.kiv.backendapi.type.Type;
|
10
|
import cz.zcu.kiv.backendapi.type.TypeRepository;
|
11
|
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
|
12
|
import cz.zcu.kiv.backendapi.writtenform.WrittenFormRepository;
|
13
|
import org.junit.jupiter.api.AfterEach;
|
14
|
import org.junit.jupiter.api.BeforeEach;
|
15
|
import org.junit.jupiter.api.Test;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
18
|
|
19
|
import java.util.List;
|
20
|
import java.util.stream.Collectors;
|
21
|
import java.util.stream.Stream;
|
22
|
|
23
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
24
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
25
|
|
26
|
@DataJpaTest
|
27
|
class CatalogItemRepositoryTest {
|
28
|
|
29
|
@Autowired
|
30
|
private CatalogItemRepository underTest;
|
31
|
|
32
|
@Autowired
|
33
|
private TypeRepository typeRepository;
|
34
|
|
35
|
@Autowired
|
36
|
private CountryRepository countryRepository;
|
37
|
|
38
|
@Autowired
|
39
|
private WrittenFormRepository writtenFormRepository;
|
40
|
|
41
|
@Autowired
|
42
|
private AlternativeNameRepository alternativeNameRepository;
|
43
|
|
44
|
@Autowired
|
45
|
private BibliographyRepository bibliographyRepository;
|
46
|
|
47
|
private CatalogItem catalogItem1;
|
48
|
private CatalogItem catalogItem2;
|
49
|
private CatalogItem catalogItem3;
|
50
|
private CatalogItem catalogItem4;
|
51
|
private CatalogItem catalogItem5;
|
52
|
|
53
|
@BeforeEach
|
54
|
void setUp() {
|
55
|
Type typeCountry = new Type("country");
|
56
|
Type typeCity = new Type("city");
|
57
|
Type typeCapitalCity = new Type("capital city new");
|
58
|
Type typeCityUpper = new Type("City");
|
59
|
Type typeCountri = new Type("countri");
|
60
|
typeRepository.saveAll(List.of(typeCountry, typeCity, typeCapitalCity, typeCityUpper, typeCountri));
|
61
|
|
62
|
String nameFirst = "first";
|
63
|
String nameFirstUpper = "First";
|
64
|
String nameSecond = "second";
|
65
|
String nameSedond = "sedond";
|
66
|
String nameThird = "third";
|
67
|
String nameTwelve = "twelve";
|
68
|
String nameTwentyUpper = "TWENTY";
|
69
|
|
70
|
String countryAaa = "aaa";
|
71
|
String countryAaaUpper = "AAA";
|
72
|
String countryBbb = "bbb";
|
73
|
String countryBccb = "bccb";
|
74
|
String countryCcc = "ccc";
|
75
|
String countryDdd = "ddd";
|
76
|
String countryDcd = "dcd";
|
77
|
|
78
|
catalogItem1 = new CatalogItem();
|
79
|
catalogItem2 = new CatalogItem();
|
80
|
catalogItem3 = new CatalogItem();
|
81
|
catalogItem4 = new CatalogItem();
|
82
|
catalogItem5 = new CatalogItem();
|
83
|
|
84
|
catalogItem1.setName(nameFirst);
|
85
|
catalogItem1.setTypes(Stream.of(typeCountry, typeCity).collect(Collectors.toSet()));
|
86
|
catalogItem1.setCountries(Stream.of(new Country(countryAaa, catalogItem1), new Country(countryDcd, catalogItem1)).collect(Collectors.toSet()));
|
87
|
|
88
|
catalogItem2.setName(nameSecond);
|
89
|
catalogItem2.setAlternativeNames(Stream.of(new AlternativeName(nameTwelve, catalogItem2), new AlternativeName(nameThird, catalogItem2)).collect(Collectors.toSet()));
|
90
|
catalogItem2.setTypes(Stream.of(typeCountry, typeCityUpper).collect(Collectors.toSet()));
|
91
|
catalogItem2.setCountries(Stream.of(new Country(countryAaa, catalogItem2), new Country(countryBbb, catalogItem2)).collect(Collectors.toSet()));
|
92
|
catalogItem2.setBibliography(Stream.of(new Bibliography("bibl", catalogItem2)).collect(Collectors.toSet()));
|
93
|
catalogItem2.setWrittenForms(Stream.of(new WrittenForm("wr", catalogItem2), new WrittenForm("wr2", catalogItem2)).collect(Collectors.toSet()));
|
94
|
|
95
|
catalogItem3.setName(nameThird);
|
96
|
catalogItem3.setAlternativeNames(Stream.of(new AlternativeName(nameTwentyUpper, catalogItem3), new AlternativeName(nameSedond, catalogItem3)).collect(Collectors.toSet()));
|
97
|
catalogItem3.setTypes(Stream.of(typeCountri, typeCapitalCity).collect(Collectors.toSet()));
|
98
|
catalogItem3.setCountries(Stream.of(new Country(countryAaaUpper, catalogItem3), new Country(countryCcc, catalogItem3)).collect(Collectors.toSet()));
|
99
|
|
100
|
|
101
|
catalogItem4.setAlternativeNames(Stream.of(new AlternativeName(nameTwelve, catalogItem4), new AlternativeName(nameFirstUpper, catalogItem4)).collect(Collectors.toSet()));
|
102
|
catalogItem4.setTypes(Stream.of(typeCountri, typeCountry).collect(Collectors.toSet()));
|
103
|
catalogItem4.setCountries(Stream.of(new Country(countryBccb, catalogItem4), new Country(countryDdd, catalogItem4)).collect(Collectors.toSet()));
|
104
|
|
105
|
|
106
|
catalogItem5.setName(nameSedond);
|
107
|
catalogItem5.setAlternativeNames(Stream.of(new AlternativeName(nameThird, catalogItem5)).collect(Collectors.toSet()));
|
108
|
catalogItem5.setTypes(Stream.of(typeCountri).collect(Collectors.toSet()));
|
109
|
|
110
|
underTest.saveAll(List.of(catalogItem1, catalogItem2, catalogItem3, catalogItem4, catalogItem5));
|
111
|
|
112
|
|
113
|
}
|
114
|
|
115
|
@AfterEach
|
116
|
void tearDown() {
|
117
|
underTest.deleteAll();
|
118
|
}
|
119
|
|
120
|
@Test
|
121
|
void itShouldReturnAll() {
|
122
|
// given
|
123
|
String name = "";
|
124
|
String country = "";
|
125
|
String type = "";
|
126
|
|
127
|
// when
|
128
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
129
|
|
130
|
// then
|
131
|
assertThat(filterResult.size()).isEqualTo(5);
|
132
|
}
|
133
|
|
134
|
@Test
|
135
|
void testName() {
|
136
|
// given
|
137
|
String name = "first";
|
138
|
String country = "";
|
139
|
String type = "";
|
140
|
|
141
|
// when
|
142
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
143
|
|
144
|
// then
|
145
|
assertThat(filterResult.size()).isEqualTo(2);
|
146
|
assertTrue(filterResult.contains(catalogItem1));
|
147
|
assertTrue(filterResult.contains(catalogItem4));
|
148
|
}
|
149
|
|
150
|
@Test
|
151
|
void testWildcardCharacterName() {
|
152
|
// given
|
153
|
String name = "se_ond";
|
154
|
String country = "";
|
155
|
String type = "";
|
156
|
|
157
|
// when
|
158
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
159
|
|
160
|
// then
|
161
|
assertThat(filterResult.size()).isEqualTo(3);
|
162
|
assertTrue(filterResult.contains(catalogItem2));
|
163
|
assertTrue(filterResult.contains(catalogItem3));
|
164
|
assertTrue(filterResult.contains(catalogItem5));
|
165
|
}
|
166
|
|
167
|
@Test
|
168
|
void testWildcardCharactersName() {
|
169
|
// given
|
170
|
String name = "twe%";
|
171
|
String country = "";
|
172
|
String type = "";
|
173
|
|
174
|
// when
|
175
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
176
|
|
177
|
// then
|
178
|
assertThat(filterResult.size()).isEqualTo(3);
|
179
|
assertTrue(filterResult.contains(catalogItem2));
|
180
|
assertTrue(filterResult.contains(catalogItem3));
|
181
|
assertTrue(filterResult.contains(catalogItem4));
|
182
|
}
|
183
|
|
184
|
@Test
|
185
|
void testCountry() {
|
186
|
// given
|
187
|
String name = "";
|
188
|
String country = "aaa";
|
189
|
String type = "";
|
190
|
|
191
|
// when
|
192
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
193
|
|
194
|
// then
|
195
|
assertThat(filterResult.size()).isEqualTo(3);
|
196
|
assertTrue(filterResult.contains(catalogItem1));
|
197
|
assertTrue(filterResult.contains(catalogItem2));
|
198
|
assertTrue(filterResult.contains(catalogItem3));
|
199
|
}
|
200
|
|
201
|
@Test
|
202
|
void testWildcardCharacterCountry() {
|
203
|
// given
|
204
|
String name = "";
|
205
|
String country = "d_d";
|
206
|
String type = "";
|
207
|
|
208
|
// when
|
209
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
210
|
|
211
|
// then
|
212
|
assertThat(filterResult.size()).isEqualTo(2);
|
213
|
assertTrue(filterResult.contains(catalogItem1));
|
214
|
assertTrue(filterResult.contains(catalogItem4));
|
215
|
}
|
216
|
|
217
|
@Test
|
218
|
void testWildcardCharactersCountry() {
|
219
|
// given
|
220
|
String name = "";
|
221
|
String country = "b%b";
|
222
|
String type = "";
|
223
|
|
224
|
// when
|
225
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
226
|
|
227
|
// then
|
228
|
assertThat(filterResult.size()).isEqualTo(2);
|
229
|
assertTrue(filterResult.contains(catalogItem2));
|
230
|
assertTrue(filterResult.contains(catalogItem4));
|
231
|
}
|
232
|
|
233
|
|
234
|
@Test
|
235
|
void testType() {
|
236
|
// given
|
237
|
String name = "";
|
238
|
String country = "";
|
239
|
String type = "city";
|
240
|
|
241
|
// when
|
242
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
243
|
|
244
|
// then
|
245
|
assertThat(filterResult.size()).isEqualTo(2);
|
246
|
assertTrue(filterResult.contains(catalogItem1));
|
247
|
assertTrue(filterResult.contains(catalogItem2));
|
248
|
}
|
249
|
|
250
|
@Test
|
251
|
void testWildcardCharacterType() {
|
252
|
// given
|
253
|
String name = "";
|
254
|
String country = "";
|
255
|
String type = "countr_";
|
256
|
|
257
|
// when
|
258
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
259
|
|
260
|
// then
|
261
|
assertThat(filterResult.size()).isEqualTo(5);
|
262
|
assertTrue(filterResult.contains(catalogItem1));
|
263
|
assertTrue(filterResult.contains(catalogItem2));
|
264
|
assertTrue(filterResult.contains(catalogItem3));
|
265
|
assertTrue(filterResult.contains(catalogItem4));
|
266
|
assertTrue(filterResult.contains(catalogItem5));
|
267
|
}
|
268
|
|
269
|
@Test
|
270
|
void testWildcardCharactersType() {
|
271
|
// given
|
272
|
String name = "";
|
273
|
String country = "";
|
274
|
String type = "%city%";
|
275
|
|
276
|
// when
|
277
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
278
|
|
279
|
// then
|
280
|
assertThat(filterResult.size()).isEqualTo(3);
|
281
|
assertTrue(filterResult.contains(catalogItem1));
|
282
|
assertTrue(filterResult.contains(catalogItem2));
|
283
|
assertTrue(filterResult.contains(catalogItem3));
|
284
|
}
|
285
|
|
286
|
@Test
|
287
|
void testNameAndCountry() {
|
288
|
// given
|
289
|
String name = "third";
|
290
|
String country = "aaa";
|
291
|
String type = "";
|
292
|
|
293
|
// when
|
294
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
295
|
|
296
|
// then
|
297
|
assertThat(filterResult.size()).isEqualTo(2);
|
298
|
assertTrue(filterResult.contains(catalogItem2));
|
299
|
assertTrue(filterResult.contains(catalogItem3));
|
300
|
}
|
301
|
|
302
|
@Test
|
303
|
void testNameAndType() {
|
304
|
// given
|
305
|
String name = "third";
|
306
|
String country = "";
|
307
|
String type = "countri";
|
308
|
|
309
|
// when
|
310
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
311
|
|
312
|
// then
|
313
|
assertThat(filterResult.size()).isEqualTo(2);
|
314
|
assertTrue(filterResult.contains(catalogItem3));
|
315
|
assertTrue(filterResult.contains(catalogItem5));
|
316
|
}
|
317
|
|
318
|
@Test
|
319
|
void testCountryAndType() {
|
320
|
// given
|
321
|
String name = "";
|
322
|
String country = "ddd";
|
323
|
String type = "country";
|
324
|
|
325
|
// when
|
326
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
327
|
|
328
|
// then
|
329
|
assertThat(filterResult.size()).isEqualTo(1);
|
330
|
assertTrue(filterResult.contains(catalogItem4));
|
331
|
}
|
332
|
|
333
|
@Test
|
334
|
void testAll() {
|
335
|
// given
|
336
|
String name = "third";
|
337
|
String country = "AAA";
|
338
|
String type = "countri";
|
339
|
|
340
|
// when
|
341
|
List<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
|
342
|
|
343
|
// then
|
344
|
assertThat(filterResult.size()).isEqualTo(1);
|
345
|
assertTrue(filterResult.contains(catalogItem3));
|
346
|
}
|
347
|
|
348
|
@Test
|
349
|
void testUpdate() {
|
350
|
// given
|
351
|
int originalCountriesCount = countryRepository.findAll().size();
|
352
|
int originalNamesCount = alternativeNameRepository.findAll().size();
|
353
|
int originalWrittenFormsCount = writtenFormRepository.findAll().size();
|
354
|
int originalTypesCount = typeRepository.findAll().size();
|
355
|
int originalBibliographyCount = bibliographyRepository.findAll().size();
|
356
|
|
357
|
// when
|
358
|
catalogItem2.getCountries().clear();
|
359
|
catalogItem2.getCountries().addAll(Stream.of(new Country("aaa", catalogItem2)).collect(Collectors.toSet()));
|
360
|
|
361
|
catalogItem2.getAlternativeNames().clear();
|
362
|
catalogItem2.getAlternativeNames().addAll(Stream.of(new AlternativeName("aaa", catalogItem2)).collect(Collectors.toSet()));
|
363
|
|
364
|
catalogItem2.getWrittenForms().clear();
|
365
|
catalogItem2.getWrittenForms().addAll(Stream.of(new WrittenForm("aaa", catalogItem2), new WrittenForm("aaaa", catalogItem2), new WrittenForm("aaaaa", catalogItem2)).collect(Collectors.toSet()));
|
366
|
|
367
|
catalogItem2.getBibliography().clear();
|
368
|
catalogItem2.getBibliography().addAll(Stream.of(new Bibliography("aaa", catalogItem2), new Bibliography("aaaa", catalogItem2)).collect(Collectors.toSet()));
|
369
|
|
370
|
catalogItem2.getTypes().clear();
|
371
|
catalogItem2.getTypes().addAll(Stream.of(new Type("country")).collect(Collectors.toSet()));
|
372
|
|
373
|
underTest.save(catalogItem2);
|
374
|
|
375
|
// then
|
376
|
assertThat(typeRepository.findAll().size()).isEqualTo(originalTypesCount);
|
377
|
assertThat(countryRepository.findAll().size()).isEqualTo(originalCountriesCount - 1);
|
378
|
assertThat(alternativeNameRepository.findAll().size()).isEqualTo(originalNamesCount - 1);
|
379
|
assertThat(bibliographyRepository.findAll().size()).isEqualTo(originalBibliographyCount + 1);
|
380
|
assertThat(writtenFormRepository.findAll().size()).isEqualTo(originalWrittenFormsCount + 1);
|
381
|
|
382
|
}
|
383
|
|
384
|
@Test
|
385
|
void testDelete() {
|
386
|
// given
|
387
|
int expectedCountriesCount = countryRepository.findAll().size() - catalogItem2.getCountries().size();
|
388
|
int expectedNamesCount = alternativeNameRepository.findAll().size() - catalogItem2.getAlternativeNames().size();
|
389
|
int expectedWrittenFormsCount = writtenFormRepository.findAll().size() - catalogItem2.getWrittenForms().size();
|
390
|
int expectedTypesCount = typeRepository.findAll().size();
|
391
|
int expectedBibliographyCount = bibliographyRepository.findAll().size() - catalogItem2.getBibliography().size();
|
392
|
|
393
|
// when
|
394
|
underTest.delete(catalogItem2);
|
395
|
|
396
|
// then
|
397
|
assertThat(typeRepository.findAll().size()).isEqualTo(expectedTypesCount);
|
398
|
assertThat(countryRepository.findAll().size()).isEqualTo(expectedCountriesCount);
|
399
|
assertThat(alternativeNameRepository.findAll().size()).isEqualTo(expectedNamesCount);
|
400
|
assertThat(bibliographyRepository.findAll().size()).isEqualTo(expectedBibliographyCount);
|
401
|
assertThat(writtenFormRepository.findAll().size()).isEqualTo(expectedWrittenFormsCount);
|
402
|
|
403
|
}
|
404
|
}
|