Projekt

Obecné

Profil

« Předchozí | Další » 

Revize abaa5f46

Přidáno uživatelem Jakub Šmíd před asi 2 roky(ů)

  • ID abaa5f461242c640ae1e7daaddb6bfc1b1419db9
  • Rodič c1df89f5

Experiment with database

Zobrazit rozdíly:

backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepositoryTest.java
1 1
package cz.zcu.kiv.backendapi.catalog;
2 2

  
3 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;
4 7
import cz.zcu.kiv.backendapi.country.Country;
8
import cz.zcu.kiv.backendapi.country.CountryRepository;
5 9
import cz.zcu.kiv.backendapi.type.Type;
6 10
import cz.zcu.kiv.backendapi.type.TypeRepository;
11
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
12
import cz.zcu.kiv.backendapi.writtenform.WrittenFormRepository;
7 13
import org.junit.jupiter.api.AfterEach;
8 14
import org.junit.jupiter.api.BeforeEach;
9 15
import org.junit.jupiter.api.Test;
......
11 17
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
12 18

  
13 19
import java.util.List;
14
import java.util.Set;
20
import java.util.stream.Collectors;
21
import java.util.stream.Stream;
15 22

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

  
19 26
@DataJpaTest
20 27
class CatalogItemRepositoryTest {
......
25 32
    @Autowired
26 33
    private TypeRepository typeRepository;
27 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

  
28 47
    private CatalogItem catalogItem1;
29 48
    private CatalogItem catalogItem2;
30 49
    private CatalogItem catalogItem3;
......
63 82
        catalogItem5 = new CatalogItem();
64 83

  
65 84
        catalogItem1.setName(nameFirst);
66
        catalogItem1.setTypes(Set.of(typeCountry, typeCity));
67
        catalogItem1.setCountries(Set.of(new Country(countryAaa, catalogItem1), new Country(countryDcd, catalogItem1)));
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()));
68 87

  
69 88
        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)));
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()));
73 94

  
74 95
        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)));
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()));
78 99

  
79 100

  
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)));
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()));
83 104

  
84 105

  
85 106
        catalogItem5.setName(nameSedond);
86
        catalogItem5.setAlternativeNames(Set.of(new AlternativeName(nameThird, catalogItem5)));
87
        catalogItem5.setTypes(Set.of(typeCountri));
107
        catalogItem5.setAlternativeNames(Stream.of(new AlternativeName(nameThird, catalogItem5)).collect(Collectors.toSet()));
108
        catalogItem5.setTypes(Stream.of(typeCountri).collect(Collectors.toSet()));
88 109

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

  
......
104 125
        String type = "";
105 126

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

  
109 130
        // then
110 131
        assertThat(filterResult.size()).isEqualTo(5);
......
118 139
        String type = "";
119 140

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

  
123 144
        // then
124 145
        assertThat(filterResult.size()).isEqualTo(2);
......
134 155
        String type = "";
135 156

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

  
139 160
        // then
140 161
        assertThat(filterResult.size()).isEqualTo(3);
......
151 172
        String type = "";
152 173

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

  
156 177
        // then
157 178
        assertThat(filterResult.size()).isEqualTo(3);
......
168 189
        String type = "";
169 190

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

  
173 194
        // then
174 195
        assertThat(filterResult.size()).isEqualTo(3);
......
185 206
        String type = "";
186 207

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

  
190 211
        // then
191 212
        assertThat(filterResult.size()).isEqualTo(2);
......
201 222
        String type = "";
202 223

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

  
206 227
        // then
207 228
        assertThat(filterResult.size()).isEqualTo(2);
......
218 239
        String type = "city";
219 240

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

  
223 244
        // then
224 245
        assertThat(filterResult.size()).isEqualTo(2);
......
234 255
        String type = "countr_";
235 256

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

  
239 260
        // then
240 261
        assertThat(filterResult.size()).isEqualTo(5);
......
253 274
        String type = "%city%";
254 275

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

  
258 279
        // then
259 280
        assertThat(filterResult.size()).isEqualTo(3);
......
270 291
        String type = "";
271 292

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

  
275 296
        // then
276 297
        assertThat(filterResult.size()).isEqualTo(2);
......
286 307
        String type = "countri";
287 308

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

  
291 312
        // then
292 313
        assertThat(filterResult.size()).isEqualTo(2);
......
302 323
        String type = "country";
303 324

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

  
307 328
        // then
308 329
        assertThat(filterResult.size()).isEqualTo(1);
......
317 338
        String type = "countri";
318 339

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

  
322 343
        // then
323 344
        assertThat(filterResult.size()).isEqualTo(1);
324 345
        assertTrue(filterResult.contains(catalogItem3));
325 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
    }
326 404
}

Také k dispozici: Unified diff