Projekt

Obecné

Profil

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

    
3
import cz.zcu.kiv.backendapi.alternativename.AlternativeName;
4
import cz.zcu.kiv.backendapi.country.Country;
5
import cz.zcu.kiv.backendapi.type.Type;
6
import cz.zcu.kiv.backendapi.type.TypeRepository;
7
import org.junit.jupiter.api.AfterEach;
8
import org.junit.jupiter.api.BeforeEach;
9
import org.junit.jupiter.api.Test;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
12

    
13
import java.util.List;
14
import java.util.Set;
15

    
16
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
17
import static org.junit.jupiter.api.Assertions.assertTrue;
18

    
19
@DataJpaTest
20
class CatalogItemRepositoryTest {
21

    
22
    @Autowired
23
    private CatalogItemRepository underTest;
24

    
25
    @Autowired
26
    private TypeRepository typeRepository;
27

    
28
    private CatalogItem catalogItem1;
29
    private CatalogItem catalogItem2;
30
    private CatalogItem catalogItem3;
31
    private CatalogItem catalogItem4;
32
    private CatalogItem catalogItem5;
33

    
34
    @BeforeEach
35
    void setUp() {
36
        Type typeCountry = new Type("country");
37
        Type typeCity = new Type("city");
38
        Type typeCapitalCity = new Type("capital city new");
39
        Type typeCityUpper = new Type("City");
40
        Type typeCountri = new Type("countri");
41
        typeRepository.saveAll(List.of(typeCountry, typeCity, typeCapitalCity, typeCityUpper, typeCountri));
42

    
43
        String nameFirst = "first";
44
        String nameFirstUpper = "First";
45
        String nameSecond = "second";
46
        String nameSedond = "sedond";
47
        String nameThird = "third";
48
        String nameTwelve = "twelve";
49
        String nameTwentyUpper = "TWENTY";
50

    
51
        String countryAaa = "aaa";
52
        String countryAaaUpper = "AAA";
53
        String countryBbb = "bbb";
54
        String countryBccb = "bccb";
55
        String countryCcc = "ccc";
56
        String countryDdd = "ddd";
57
        String countryDcd = "dcd";
58

    
59
        catalogItem1 = new CatalogItem();
60
        catalogItem2 = new CatalogItem();
61
        catalogItem3 = new CatalogItem();
62
        catalogItem4 = new CatalogItem();
63
        catalogItem5 = new CatalogItem();
64

    
65
        catalogItem1.setName(nameFirst);
66
        catalogItem1.setTypes(Set.of(typeCountry, typeCity));
67
        catalogItem1.setCountries(Set.of(new Country(countryAaa, catalogItem1), new Country(countryDcd, catalogItem1)));
68

    
69
        catalogItem2.setName(nameSecond);
70
        catalogItem2.setAlternativeNames(Set.of(new AlternativeName(nameTwelve, catalogItem2), new AlternativeName(nameThird, catalogItem2)));
71
        catalogItem2.setTypes(Set.of(typeCountry, typeCityUpper));
72
        catalogItem2.setCountries(Set.of(new Country(countryAaa, catalogItem2), new Country(countryBbb, catalogItem2)));
73

    
74
        catalogItem3.setName(nameThird);
75
        catalogItem3.setAlternativeNames(Set.of(new AlternativeName(nameTwentyUpper, catalogItem3), new AlternativeName(nameSedond, catalogItem3)));
76
        catalogItem3.setTypes(Set.of(typeCountri, typeCapitalCity));
77
        catalogItem3.setCountries(Set.of(new Country(countryAaaUpper, catalogItem3), new Country(countryCcc, catalogItem3)));
78

    
79

    
80
        catalogItem4.setAlternativeNames(Set.of(new AlternativeName(nameTwelve, catalogItem4), new AlternativeName(nameFirstUpper, catalogItem4)));
81
        catalogItem4.setTypes(Set.of(typeCountri, typeCountry));
82
        catalogItem4.setCountries(Set.of(new Country(countryBccb, catalogItem4), new Country(countryDdd, catalogItem4)));
83

    
84

    
85
        catalogItem5.setName(nameSedond);
86
        catalogItem5.setAlternativeNames(Set.of(new AlternativeName(nameThird, catalogItem5)));
87
        catalogItem5.setTypes(Set.of(typeCountri));
88

    
89
        underTest.saveAll(List.of(catalogItem1, catalogItem2, catalogItem3, catalogItem4, catalogItem5));
90

    
91

    
92
    }
93

    
94
    @AfterEach
95
    void tearDown() {
96
        underTest.deleteAll();
97
    }
98

    
99
    @Test
100
    void itShouldReturnAll() {
101
        // given
102
        String name = "";
103
        String country = "";
104
        String type = "";
105

    
106
        // when
107
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
108

    
109
        // then
110
        assertThat(filterResult.size()).isEqualTo(5);
111
    }
112

    
113
    @Test
114
    void testName() {
115
        // given
116
        String name = "first";
117
        String country = "";
118
        String type = "";
119

    
120
        // when
121
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
122

    
123
        // then
124
        assertThat(filterResult.size()).isEqualTo(2);
125
        assertTrue(filterResult.contains(catalogItem1));
126
        assertTrue(filterResult.contains(catalogItem4));
127
    }
128

    
129
    @Test
130
    void testWildcardCharacterName() {
131
        // given
132
        String name = "se_ond";
133
        String country = "";
134
        String type = "";
135

    
136
        // when
137
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
138

    
139
        // then
140
        assertThat(filterResult.size()).isEqualTo(3);
141
        assertTrue(filterResult.contains(catalogItem2));
142
        assertTrue(filterResult.contains(catalogItem3));
143
        assertTrue(filterResult.contains(catalogItem5));
144
    }
145

    
146
    @Test
147
    void testWildcardCharactersName() {
148
        // given
149
        String name = "twe%";
150
        String country = "";
151
        String type = "";
152

    
153
        // when
154
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
155

    
156
        // then
157
        assertThat(filterResult.size()).isEqualTo(3);
158
        assertTrue(filterResult.contains(catalogItem2));
159
        assertTrue(filterResult.contains(catalogItem3));
160
        assertTrue(filterResult.contains(catalogItem4));
161
    }
162

    
163
    @Test
164
    void testCountry() {
165
        // given
166
        String name = "";
167
        String country = "aaa";
168
        String type = "";
169

    
170
        // when
171
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
172

    
173
        // then
174
        assertThat(filterResult.size()).isEqualTo(3);
175
        assertTrue(filterResult.contains(catalogItem1));
176
        assertTrue(filterResult.contains(catalogItem2));
177
        assertTrue(filterResult.contains(catalogItem3));
178
    }
179

    
180
    @Test
181
    void testWildcardCharacterCountry() {
182
        // given
183
        String name = "";
184
        String country = "d_d";
185
        String type = "";
186

    
187
        // when
188
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
189

    
190
        // then
191
        assertThat(filterResult.size()).isEqualTo(2);
192
        assertTrue(filterResult.contains(catalogItem1));
193
        assertTrue(filterResult.contains(catalogItem4));
194
    }
195

    
196
    @Test
197
    void testWildcardCharactersCountry() {
198
        // given
199
        String name = "";
200
        String country = "b%b";
201
        String type = "";
202

    
203
        // when
204
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
205

    
206
        // then
207
        assertThat(filterResult.size()).isEqualTo(2);
208
        assertTrue(filterResult.contains(catalogItem2));
209
        assertTrue(filterResult.contains(catalogItem4));
210
    }
211

    
212

    
213
    @Test
214
    void testType() {
215
        // given
216
        String name = "";
217
        String country = "";
218
        String type = "city";
219

    
220
        // when
221
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
222

    
223
        // then
224
        assertThat(filterResult.size()).isEqualTo(2);
225
        assertTrue(filterResult.contains(catalogItem1));
226
        assertTrue(filterResult.contains(catalogItem2));
227
    }
228

    
229
    @Test
230
    void testWildcardCharacterType() {
231
        // given
232
        String name = "";
233
        String country = "";
234
        String type = "countr_";
235

    
236
        // when
237
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
238

    
239
        // then
240
        assertThat(filterResult.size()).isEqualTo(5);
241
        assertTrue(filterResult.contains(catalogItem1));
242
        assertTrue(filterResult.contains(catalogItem2));
243
        assertTrue(filterResult.contains(catalogItem3));
244
        assertTrue(filterResult.contains(catalogItem4));
245
        assertTrue(filterResult.contains(catalogItem5));
246
    }
247

    
248
    @Test
249
    void testWildcardCharactersType() {
250
        // given
251
        String name = "";
252
        String country = "";
253
        String type = "%city%";
254

    
255
        // when
256
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
257

    
258
        // then
259
        assertThat(filterResult.size()).isEqualTo(3);
260
        assertTrue(filterResult.contains(catalogItem1));
261
        assertTrue(filterResult.contains(catalogItem2));
262
        assertTrue(filterResult.contains(catalogItem3));
263
    }
264

    
265
    @Test
266
    void testNameAndCountry() {
267
        // given
268
        String name = "third";
269
        String country = "aaa";
270
        String type = "";
271

    
272
        // when
273
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
274

    
275
        // then
276
        assertThat(filterResult.size()).isEqualTo(2);
277
        assertTrue(filterResult.contains(catalogItem2));
278
        assertTrue(filterResult.contains(catalogItem3));
279
    }
280

    
281
    @Test
282
    void testNameAndType() {
283
        // given
284
        String name = "third";
285
        String country = "";
286
        String type = "countri";
287

    
288
        // when
289
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
290

    
291
        // then
292
        assertThat(filterResult.size()).isEqualTo(2);
293
        assertTrue(filterResult.contains(catalogItem3));
294
        assertTrue(filterResult.contains(catalogItem5));
295
    }
296

    
297
    @Test
298
    void testCountryAndType() {
299
        // given
300
        String name = "";
301
        String country = "ddd";
302
        String type = "country";
303

    
304
        // when
305
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
306

    
307
        // then
308
        assertThat(filterResult.size()).isEqualTo(1);
309
        assertTrue(filterResult.contains(catalogItem4));
310
    }
311

    
312
    @Test
313
    void testAll() {
314
        // given
315
        String name = "third";
316
        String country = "AAA";
317
        String type = "countri";
318

    
319
        // when
320
        Set<CatalogItem> filterResult = underTest.filterCatalog(name, country, type);
321

    
322
        // then
323
        assertThat(filterResult.size()).isEqualTo(1);
324
        assertTrue(filterResult.contains(catalogItem3));
325
    }
326

    
327
    @Test
328
    void testItemsByName() {
329
        // given
330
        String name = "twelve";
331

    
332
        // when
333
        List<CatalogItem> res = underTest.getItemsByName(name);
334

    
335
        // then
336
        assertThat(res.size()).isEqualTo(2);
337
        assertTrue(res.contains(catalogItem2));
338
        assertTrue(res.contains(catalogItem4));
339
    }
340
}
(1-1/2)