Projekt

Obecné

Profil

Stáhnout (18.5 KB) Statistiky
| Větev: | Tag: | Revize:
1
package cz.zcu.kiv.backendapi.catalog;
2

    
3
import cz.zcu.kiv.backendapi.alternativename.CatalogItemName;
4
import cz.zcu.kiv.backendapi.country.Country;
5
import cz.zcu.kiv.backendapi.type.Type;
6
import cz.zcu.kiv.backendapi.type.TypeServiceImpl;
7
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
8
import org.junit.jupiter.api.BeforeEach;
9
import org.junit.jupiter.api.Test;
10
import org.junit.jupiter.api.extension.ExtendWith;
11
import org.mockito.Mock;
12
import org.mockito.junit.jupiter.MockitoExtension;
13

    
14
import java.util.LinkedHashSet;
15
import java.util.List;
16
import java.util.UUID;
17
import java.util.stream.Collectors;
18
import java.util.stream.Stream;
19

    
20
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
21
import static org.junit.jupiter.api.Assertions.assertTrue;
22
import static org.mockito.BDDMockito.given;
23
import static org.mockito.Mockito.verify;
24

    
25
@ExtendWith(MockitoExtension.class)
26
class CatalogItemServiceImplFilterTest {
27
    @Mock
28
    private CatalogItemRepository catalogItemRepository;
29

    
30
    @Mock
31
    private TypeServiceImpl typeService;
32

    
33
    private CatalogItemServiceImpl underTest;
34

    
35
    private CatalogItemDto catalogItemDto1;
36
    private CatalogItemDto catalogItemDto2;
37
    private CatalogItemDto catalogItemDto3;
38
    private CatalogItemDto catalogItemDto4;
39
    private CatalogItemDto catalogItemDto5;
40

    
41
    @BeforeEach
42
    void setUp() {
43
        underTest = new CatalogItemServiceImpl(catalogItemRepository, typeService);
44
        // given
45
        String country = "country";
46
        String city = "city";
47
        String capitalCityNew = "capital city new";
48
        String cityUpper = "City";
49
        String countri = "countri";
50

    
51
        Type typeCountry = new Type(country);
52
        Type typeCity = new Type(city);
53
        Type typeCapitalCity = new Type(capitalCityNew);
54
        Type typeCityUpper = new Type(cityUpper);
55
        Type typeCountri = new Type(countri);
56

    
57
        String nameFirst = "first";
58
        String nameFirstUpper = "First";
59
        String nameSecond = "second";
60
        String nameSedond = "sedond";
61
        String nameThird = "third";
62
        String nameTwelve = "twelve";
63
        String nameTwentyUpper = "TWENTY";
64

    
65
        String countryAaa = "aaa";
66
        String countryAaaUpper = "AAA";
67
        String countryBbb = "bbb";
68
        String countryBccb = "bccb";
69
        String countryCcc = "ccc";
70
        String countryDdd = "ddd";
71
        String countryDcd = "dcd";
72

    
73
        String writtenForm = "written";
74
        String writtenFormUpper = "WRITTEN";
75
        String writtenFormForOr = "wratten";
76

    
77
        CatalogItem catalogItem1 = new CatalogItem();
78
        CatalogItem catalogItem2 = new CatalogItem();
79
        CatalogItem catalogItem3 = new CatalogItem();
80
        CatalogItem catalogItem4 = new CatalogItem();
81
        CatalogItem catalogItem5 = new CatalogItem();
82

    
83
        UUID catalogItemId1 = UUID.randomUUID();
84
        UUID catalogItemId2 = UUID.randomUUID();
85
        UUID catalogItemId3 = UUID.randomUUID();
86
        UUID catalogItemId4 = UUID.randomUUID();
87
        UUID catalogItemId5 = UUID.randomUUID();
88

    
89
        catalogItem1.setId(catalogItemId1);
90
        catalogItem2.setId(catalogItemId2);
91
        catalogItem3.setId(catalogItemId3);
92
        catalogItem4.setId(catalogItemId4);
93
        catalogItem5.setId(catalogItemId5);
94

    
95
        catalogItem1.setName(nameFirst);
96
        catalogItem1.setTypes(Stream.of(typeCountry, typeCity).collect(Collectors.toCollection(LinkedHashSet::new)));
97
        catalogItem1.setCountries(Stream.of(new Country(countryAaa, catalogItem1, 1), new Country(countryDcd, catalogItem1, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
98
        catalogItem1.setWrittenForms(Stream.of(new WrittenForm(writtenForm, catalogItem1, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
99

    
100
        catalogItemDto1 = new CatalogItemDto(catalogItemId1, nameFirst, new LinkedHashSet<>(), Stream.of(writtenForm).collect(Collectors.toCollection(LinkedHashSet::new)),
101
                Stream.of(country, city).collect(Collectors.toCollection(LinkedHashSet::new)), Stream.of(countryAaa, countryDcd).collect(Collectors.toCollection(LinkedHashSet::new)),
102
                new LinkedHashSet<>(), null, null, null, "");
103

    
104
        catalogItem2.setName(nameSecond);
105
        catalogItem2.setAllNames(Stream.of(new CatalogItemName(nameTwelve, catalogItem2, 1), new CatalogItemName(nameThird, catalogItem2, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
106
        catalogItem2.setTypes(Stream.of(typeCountry, typeCityUpper).collect(Collectors.toCollection(LinkedHashSet::new)));
107
        catalogItem2.setCountries(Stream.of(new Country(countryAaa, catalogItem2, 1), new Country(countryBbb, catalogItem2, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
108
        catalogItem2.setWrittenForms(Stream.of(new WrittenForm(writtenFormUpper, catalogItem2, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
109

    
110
        catalogItemDto2 = new CatalogItemDto(catalogItemId2, nameSecond, Stream.of(nameTwelve, nameThird).collect(Collectors.toCollection(LinkedHashSet::new)),
111
                Stream.of(writtenFormUpper).collect(Collectors.toCollection(LinkedHashSet::new)), Stream.of(country, cityUpper).collect(Collectors.toCollection(LinkedHashSet::new)),
112
                Stream.of(countryAaa, countryBbb).collect(Collectors.toCollection(LinkedHashSet::new)), new LinkedHashSet<>(),
113
                null, null, null, "");
114

    
115
        catalogItem3.setName(nameThird);
116
        catalogItem3.setAllNames(Stream.of(new CatalogItemName(nameTwentyUpper, catalogItem3, 1), new CatalogItemName(nameSedond, catalogItem3, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
117
        catalogItem3.setTypes(Stream.of(typeCountri, typeCapitalCity).collect(Collectors.toCollection(LinkedHashSet::new)));
118
        catalogItem3.setCountries(Stream.of(new Country(countryAaaUpper, catalogItem3, 1), new Country(countryCcc, catalogItem3, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
119
        catalogItem3.setWrittenForms(Stream.of(new WrittenForm(writtenFormForOr, catalogItem3, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
120

    
121
        catalogItemDto3 = new CatalogItemDto(catalogItemId3, nameThird, Stream.of(nameTwentyUpper, nameSedond).collect(Collectors.toCollection(LinkedHashSet::new)),
122
                Stream.of(writtenFormForOr).collect(Collectors.toCollection(LinkedHashSet::new)), Stream.of(countri, capitalCityNew).collect(Collectors.toCollection(LinkedHashSet::new)),
123
                Stream.of(countryAaaUpper, countryCcc).collect(Collectors.toCollection(LinkedHashSet::new)), new LinkedHashSet<>(),
124
                null, null, null, "");
125

    
126
        catalogItem4.setAllNames(Stream.of(new CatalogItemName(nameTwelve, catalogItem4, 1), new CatalogItemName(nameFirstUpper, catalogItem4, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
127
        catalogItem4.setTypes(Stream.of(typeCountri, typeCountry).collect(Collectors.toCollection(LinkedHashSet::new)));
128
        catalogItem4.setCountries(Stream.of(new Country(countryBccb, catalogItem4, 1), new Country(countryDdd, catalogItem4, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
129
        catalogItem4.setWrittenForms(Stream.of(new WrittenForm(writtenForm, catalogItem4, 1), new WrittenForm(writtenFormForOr, catalogItem4, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
130

    
131
        catalogItemDto4 = new CatalogItemDto(catalogItemId4, "", Stream.of(nameTwelve, nameFirstUpper).collect(Collectors.toCollection(LinkedHashSet::new)),
132
                Stream.of(writtenForm, writtenFormForOr).collect(Collectors.toCollection(LinkedHashSet::new)), Stream.of(countri, country).collect(Collectors.toCollection(LinkedHashSet::new)),
133
                Stream.of(countryBccb, countryDdd).collect(Collectors.toCollection(LinkedHashSet::new)), new LinkedHashSet<>(),
134
                null, null, null, "");
135

    
136
        catalogItem5.setName(nameSedond);
137
        catalogItem5.setAllNames(Stream.of(new CatalogItemName(nameThird, catalogItem5, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
138
        catalogItem5.setTypes(Stream.of(typeCountri).collect(Collectors.toCollection(LinkedHashSet::new)));
139

    
140
        catalogItemDto5 = new CatalogItemDto(catalogItemId5, nameSedond, Stream.of(nameThird).collect(Collectors.toCollection(LinkedHashSet::new)),
141
                new LinkedHashSet<>(), Stream.of(countri).collect(Collectors.toCollection(LinkedHashSet::new)),
142
                new LinkedHashSet<>(), new LinkedHashSet<>(),
143
                null, null, null, "");
144

    
145
        given(catalogItemRepository.findAll()).willReturn(List.of(catalogItem1, catalogItem2, catalogItem3, catalogItem4, catalogItem5));
146
    }
147

    
148
    @Test
149
    void itShouldReturnAll() {
150
        // given
151
        String name = "";
152
        String country = "";
153
        String type = "";
154
        String writtenForm = "";
155

    
156
        // when
157
        List<CatalogItemDto> results = underTest.getCatalog(name, country, type, writtenForm);
158

    
159
        // then
160
        assertThat(results.size()).isEqualTo(5);
161
        assertTrue(results.contains(catalogItemDto1));
162
        assertTrue(results.contains(catalogItemDto2));
163
        assertTrue(results.contains(catalogItemDto3));
164
        assertTrue(results.contains(catalogItemDto4));
165
        assertTrue(results.contains(catalogItemDto5));
166

    
167
        verify(catalogItemRepository).findAll();
168
    }
169

    
170
    @Test
171
    void testName() {
172
        // given
173
        String name = "first";
174
        String country = "";
175
        String type = "";
176
        String writtenForm = "";
177

    
178
        // when
179
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
180

    
181
        // then
182
        assertThat(filterResult.size()).isEqualTo(2);
183
        assertTrue(filterResult.contains(catalogItemDto1));
184
        assertTrue(filterResult.contains(catalogItemDto4));
185
    }
186

    
187
    @Test
188
    void testWildcardCharacterName() {
189
        // given
190
        String name = "se?ond";
191
        String country = "";
192
        String type = "";
193
        String writtenForm = "";
194

    
195
        // when
196
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
197

    
198
        // then
199
        assertThat(filterResult.size()).isEqualTo(3);
200
        assertTrue(filterResult.contains(catalogItemDto2));
201
        assertTrue(filterResult.contains(catalogItemDto3));
202
        assertTrue(filterResult.contains(catalogItemDto5));
203
    }
204

    
205
    @Test
206
    void testWildcardCharactersName() {
207
        // given
208
        String name = "twe*";
209
        String country = "";
210
        String type = "";
211
        String writtenForm = "";
212

    
213

    
214
        // when
215
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
216

    
217
        // then
218
        assertThat(filterResult.size()).isEqualTo(3);
219
        assertTrue(filterResult.contains(catalogItemDto2));
220
        assertTrue(filterResult.contains(catalogItemDto3));
221
        assertTrue(filterResult.contains(catalogItemDto4));
222
    }
223

    
224
    @Test
225
    void testCountry() {
226
        // given
227
        String name = "";
228
        String country = "aaa";
229
        String type = "";
230
        String writtenForm = "";
231

    
232

    
233
        // when
234
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
235

    
236
        // then
237
        assertThat(filterResult.size()).isEqualTo(3);
238
        assertTrue(filterResult.contains(catalogItemDto1));
239
        assertTrue(filterResult.contains(catalogItemDto2));
240
        assertTrue(filterResult.contains(catalogItemDto3));
241
    }
242

    
243
    @Test
244
    void testWildcardCharacterCountry() {
245
        // given
246
        String name = "";
247
        String country = "d?d";
248
        String type = "";
249
        String writtenForm = "";
250

    
251
        // when
252
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
253

    
254
        // then
255
        assertThat(filterResult.size()).isEqualTo(2);
256
        assertTrue(filterResult.contains(catalogItemDto1));
257
        assertTrue(filterResult.contains(catalogItemDto4));
258
    }
259

    
260
    @Test
261
    void testWildcardCharactersCountry() {
262
        // given
263
        String name = "";
264
        String country = "b*b";
265
        String type = "";
266
        String writtenForm = "";
267

    
268
        // when
269
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
270

    
271
        // then
272
        assertThat(filterResult.size()).isEqualTo(2);
273
        assertTrue(filterResult.contains(catalogItemDto2));
274
        assertTrue(filterResult.contains(catalogItemDto4));
275
    }
276

    
277

    
278
    @Test
279
    void testType() {
280
        // given
281
        String name = "";
282
        String country = "";
283
        String type = "city";
284
        String writtenForm = "";
285

    
286
        // when
287
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
288

    
289
        // then
290
        assertThat(filterResult.size()).isEqualTo(2);
291
        assertTrue(filterResult.contains(catalogItemDto1));
292
        assertTrue(filterResult.contains(catalogItemDto2));
293
    }
294

    
295
    @Test
296
    void testWildcardCharacterType() {
297
        // given
298
        String name = "";
299
        String country = "";
300
        String type = "countr?";
301
        String writtenForm = "";
302

    
303
        // when
304
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
305

    
306
        // then
307
        assertThat(filterResult.size()).isEqualTo(5);
308
        assertTrue(filterResult.contains(catalogItemDto1));
309
        assertTrue(filterResult.contains(catalogItemDto2));
310
        assertTrue(filterResult.contains(catalogItemDto3));
311
        assertTrue(filterResult.contains(catalogItemDto4));
312
        assertTrue(filterResult.contains(catalogItemDto5));
313
    }
314

    
315
    @Test
316
    void testWildcardCharactersType() {
317
        // given
318
        String name = "";
319
        String country = "";
320
        String type = "*city*";
321
        String writtenForm = "";
322

    
323
        // when
324
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
325

    
326
        // then
327
        assertThat(filterResult.size()).isEqualTo(3);
328
        assertTrue(filterResult.contains(catalogItemDto1));
329
        assertTrue(filterResult.contains(catalogItemDto2));
330
        assertTrue(filterResult.contains(catalogItemDto3));
331
    }
332

    
333
    @Test
334
    void testWrittenForm() {
335
        // given
336
        String name = "";
337
        String country = "";
338
        String type = "";
339
        String writtenForm = "written";
340

    
341
        // when
342
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
343

    
344
        // then
345
        assertThat(filterResult.size()).isEqualTo(3);
346
        assertTrue(filterResult.contains(catalogItemDto1));
347
        assertTrue(filterResult.contains(catalogItemDto2));
348
        assertTrue(filterResult.contains(catalogItemDto4));
349
    }
350

    
351
    @Test
352
    void testWrittenFormWildcardCharacter() {
353
        // given
354
        String name = "";
355
        String country = "";
356
        String type = "";
357
        String writtenForm = "wr?tten";
358

    
359
        // when
360
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
361

    
362
        // then
363
        assertThat(filterResult.size()).isEqualTo(4);
364
        assertTrue(filterResult.contains(catalogItemDto1));
365
        assertTrue(filterResult.contains(catalogItemDto2));
366
        assertTrue(filterResult.contains(catalogItemDto3));
367
        assertTrue(filterResult.contains(catalogItemDto4));
368
    }
369

    
370
    @Test
371
    void testWrittenFormWildcardCharacters() {
372
        // given
373
        String name = "";
374
        String country = "";
375
        String type = "";
376
        String writtenForm = "wri*tten";
377

    
378
        // when
379
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
380

    
381
        // then
382
        assertThat(filterResult.size()).isEqualTo(3);
383
        assertTrue(filterResult.contains(catalogItemDto1));
384
        assertTrue(filterResult.contains(catalogItemDto2));
385
        assertTrue(filterResult.contains(catalogItemDto4));
386
    }
387

    
388
    @Test
389
    void testWrittenFormOr() {
390
        // given
391
        String name = "";
392
        String country = "";
393
        String type = "";
394
        String writtenForm = "wr(i|a)tten";
395

    
396
        // when
397
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
398

    
399
        // then
400
        assertThat(filterResult.size()).isEqualTo(4);
401
        assertTrue(filterResult.contains(catalogItemDto1));
402
        assertTrue(filterResult.contains(catalogItemDto2));
403
        assertTrue(filterResult.contains(catalogItemDto3));
404
        assertTrue(filterResult.contains(catalogItemDto4));
405
    }
406

    
407
    @Test
408
    void testNameAndCountry() {
409
        // given
410
        String name = "third";
411
        String country = "aaa";
412
        String type = "";
413
        String writtenForm = "";
414

    
415
        // when
416
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
417

    
418
        // then
419
        assertThat(filterResult.size()).isEqualTo(2);
420
        assertTrue(filterResult.contains(catalogItemDto2));
421
        assertTrue(filterResult.contains(catalogItemDto3));
422
    }
423

    
424
    @Test
425
    void testNameAndType() {
426
        // given
427
        String name = "third";
428
        String country = "";
429
        String type = "countri";
430
        String writtenForm = "";
431

    
432
        // when
433
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
434

    
435
        // then
436
        assertThat(filterResult.size()).isEqualTo(2);
437
        assertTrue(filterResult.contains(catalogItemDto3));
438
        assertTrue(filterResult.contains(catalogItemDto5));
439
    }
440

    
441
    @Test
442
    void testNameAndWrittenForm() {
443
        // given
444
        String name = "first";
445
        String country = "";
446
        String type = "";
447
        String writtenForm = "written";
448

    
449
        // when
450
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
451

    
452
        // then
453
        assertThat(filterResult.size()).isEqualTo(2);
454
        assertTrue(filterResult.contains(catalogItemDto1));
455
        assertTrue(filterResult.contains(catalogItemDto4));
456
    }
457

    
458
    @Test
459
    void testCountryAndType() {
460
        // given
461
        String name = "";
462
        String country = "ddd";
463
        String type = "country";
464
        String writtenForm = "";
465

    
466
        // when
467
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
468

    
469
        // then
470
        assertThat(filterResult.size()).isEqualTo(1);
471
        assertTrue(filterResult.contains(catalogItemDto4));
472
    }
473

    
474
    @Test
475
    void testAll() {
476
        // given
477
        String name = "third";
478
        String country = "AAA";
479
        String type = "countri";
480
        String writtenForm = "wratten";
481

    
482
        // when
483
        List<CatalogItemDto> filterResult = underTest.getCatalog(name, country, type, writtenForm);
484

    
485
        // then
486
        assertThat(filterResult.size()).isEqualTo(1);
487
        assertTrue(filterResult.contains(catalogItemDto3));
488
    }
489
}
(2-2/3)