Projekt

Obecné

Profil

Stáhnout (17.3 KB) Statistiky
| Větev: | Tag: | Revize:
1 b30f120b Jakub Smid
package cz.zcu.kiv.backendapi.catalog;
2
3
import cz.zcu.kiv.backendapi.alternativename.AlternativeName;
4
import cz.zcu.kiv.backendapi.bibliography.Bibliography;
5
import cz.zcu.kiv.backendapi.country.Country;
6
import cz.zcu.kiv.backendapi.exception.ApiRequestException;
7 c5787e2d Jakub Smid
import cz.zcu.kiv.backendapi.path.PathDto;
8 b30f120b Jakub Smid
import cz.zcu.kiv.backendapi.type.Type;
9
import cz.zcu.kiv.backendapi.type.TypeServiceImpl;
10
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
11
import org.junit.jupiter.api.BeforeEach;
12
import org.junit.jupiter.api.Test;
13
import org.junit.jupiter.api.extension.ExtendWith;
14 c5787e2d Jakub Smid
import org.mockito.ArgumentCaptor;
15
import org.mockito.Mock;
16 b30f120b Jakub Smid
import org.mockito.junit.jupiter.MockitoExtension;
17
18 c5787e2d Jakub Smid
import java.util.*;
19
import java.util.stream.Collectors;
20
import java.util.stream.Stream;
21 b30f120b Jakub Smid
22
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
23
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
24
import static org.mockito.ArgumentMatchers.any;
25
import static org.mockito.ArgumentMatchers.anyString;
26
import static org.mockito.BDDMockito.given;
27
import static org.mockito.Mockito.never;
28
import static org.mockito.Mockito.verify;
29
30
@ExtendWith(MockitoExtension.class)
31
class CatalogItemServiceImplTest {
32
33
    @Mock
34
    private CatalogItemRepository catalogItemRepository;
35
36
    @Mock
37
    private TypeServiceImpl typeService;
38
39
    private CatalogItemServiceImpl underTest;
40
41
    @BeforeEach
42
    void setUp() {
43
        underTest = new CatalogItemServiceImpl(catalogItemRepository, typeService);
44
    }
45
46
    @Test
47
    void saveCatalog() {
48
        // given
49
        Type type = new Type("type");
50
51
        CatalogItem catalogItem = new CatalogItem();
52
        catalogItem.setName("aaacbbbbbbbaa");
53
        catalogItem.setBibliography(Set.of(new Bibliography("bibl", catalogItem)));
54
        catalogItem.setTypes(Set.of(type));
55
        catalogItem.setAlternativeNames(Set.of(new AlternativeName("altName", catalogItem)));
56
        catalogItem.setCountries(Set.of(new Country("aaaabbaa", catalogItem)));
57
        catalogItem.setWrittenForms(Set.of(new WrittenForm("written", catalogItem)));
58
        catalogItem.setCertainty(0);
59
        catalogItem.setLatitude(0.1);
60
        catalogItem.setLongitude(0.2);
61
62
        CatalogItem catalogItem2 = new CatalogItem();
63
        catalogItem2.setName("name");
64
        catalogItem2.setBibliography(Set.of(new Bibliography("bibl", catalogItem2)));
65
        catalogItem2.setTypes(Set.of(type));
66
        catalogItem2.setAlternativeNames(Set.of(new AlternativeName("aaaabbbbbbaa", catalogItem2)));
67
        catalogItem2.setCountries(Set.of(new Country("aaaabbcccefaa", catalogItem2)));
68
        catalogItem2.setWrittenForms(Set.of(new WrittenForm("written", catalogItem2)));
69
        catalogItem2.setCertainty(1);
70
        catalogItem2.setLatitude(1.1);
71
        catalogItem2.setLongitude(1.2);
72
73
        List<CatalogItem> catalog = List.of(catalogItem, catalogItem2);
74
75
        given(typeService.getTypeByName(anyString())).willReturn(Optional.of(type));
76
77
        // when
78
        underTest.saveCatalog(catalog);
79
80
        // then
81
        verify(typeService, never()).saveType(type);
82
        verify(catalogItemRepository).save(catalogItem);
83
        verify(catalogItemRepository).save(catalogItem2);
84
    }
85
86
    @Test
87
    void testSaveCatalogItem() {
88
        // given
89
        CatalogItemDto catalogItemDto = new CatalogItemDto();
90
        catalogItemDto.setName("name");
91
        catalogItemDto.setBibliography(Set.of("bibl"));
92
        catalogItemDto.setTypes(Set.of("type"));
93
        catalogItemDto.setAlternativeNames(Set.of("altName"));
94
        catalogItemDto.setCountries(Set.of("country"));
95
        catalogItemDto.setWrittenForms(Set.of("written"));
96
        catalogItemDto.setCertainty(0);
97
        catalogItemDto.setLatitude(0.1);
98
        catalogItemDto.setLongitude(0.2);
99 541abced Jakub Smid
        catalogItemDto.setDescription("description");
100 b30f120b Jakub Smid
101
        Type type = new Type("type");
102
103
        CatalogItem catalogItem = new CatalogItem();
104
        catalogItem.setName("name");
105
        catalogItem.setBibliography(Set.of(new Bibliography("bibl", catalogItem)));
106
        catalogItem.setTypes(Set.of(type));
107
        catalogItem.setAlternativeNames(Set.of(new AlternativeName("altName", catalogItem)));
108
        catalogItem.setCountries(Set.of(new Country("country", catalogItem)));
109
        catalogItem.setWrittenForms(Set.of(new WrittenForm("written", catalogItem)));
110
        catalogItem.setCertainty(0);
111
        catalogItem.setLatitude(0.1);
112
        catalogItem.setLongitude(0.2);
113 541abced Jakub Smid
        catalogItem.setDescription("description");
114 b30f120b Jakub Smid
115
        given(typeService.getTypeByName(anyString())).willReturn(Optional.of(type));
116
117
        // when
118
        underTest.saveCatalogItem(catalogItemDto);
119
120
        // then
121
        ArgumentCaptor<CatalogItem> argumentCaptor = ArgumentCaptor.forClass(CatalogItem.class);
122
123
        verify(catalogItemRepository).save(argumentCaptor.capture());
124
        verify(typeService, never()).saveType(type);
125
126
        CatalogItem capturedItem = argumentCaptor.getValue();
127
128 d6451c0a Jakub Smid
        assertThat(capturedItem.getName()).isEqualTo(catalogItem.getName());
129
        assertThat(capturedItem.getLongitude()).isEqualTo(catalogItem.getLongitude());
130
        assertThat(capturedItem.getLatitude()).isEqualTo(catalogItem.getLatitude());
131
        assertThat(capturedItem.getCertainty()).isEqualTo(catalogItem.getCertainty());
132
        assertThat(capturedItem.getAlternativeNames().size()).isEqualTo(catalogItem.getAlternativeNames().size());
133
        assertThat(capturedItem.getBibliography().size()).isEqualTo(catalogItem.getBibliography().size());
134
        assertThat(capturedItem.getCountries().size()).isEqualTo(catalogItem.getCountries().size());
135
        assertThat(capturedItem.getWrittenForms().size()).isEqualTo(catalogItem.getWrittenForms().size());
136
        assertThat(capturedItem.getTypes().size()).isEqualTo(catalogItem.getTypes().size());
137
        assertThat(capturedItem.getDescription()).isEqualTo(catalogItem.getDescription());
138 b30f120b Jakub Smid
139
    }
140
141 c5787e2d Jakub Smid
    @Test
142
    void testCanGetCatalogItem() {
143
        // given
144
        UUID id = UUID.randomUUID();
145
        CatalogItem catalogItem = new CatalogItem();
146
        catalogItem.setId(id);
147
        CatalogItemDto catalogItemDto = new CatalogItemDto();
148
        catalogItemDto.setId(id);
149
        given(catalogItemRepository.findById(id)).willReturn(Optional.of(catalogItem));
150
151
        // when
152
        CatalogItemDto retrievedItemDto = underTest.getCatalogItem(id);
153
154
        // then
155
        verify(catalogItemRepository).findById(id);
156
        assertThat(retrievedItemDto).isEqualTo(catalogItemDto);
157
    }
158
159
    @Test
160
    void testCanNotGetCatalogItem() {
161
        // given
162
        UUID id = UUID.randomUUID();
163
164
        // when
165
        // then
166
        assertThatThrownBy(() -> underTest.getCatalogItem(id))
167
                .isInstanceOf(ApiRequestException.class)
168
                .hasMessageContaining("Catalog item not found");
169
170
        verify(catalogItemRepository).findById(id);
171
    }
172
173 b30f120b Jakub Smid
    @Test
174
    void testCanUpdateCatalogItem() {
175
        // given
176
        UUID id = UUID.randomUUID();
177
178
        CatalogItemDto catalogItemDto = new CatalogItemDto();
179
        catalogItemDto.setName("name");
180
        catalogItemDto.setBibliography(Set.of("bibl"));
181
        catalogItemDto.setTypes(Set.of("type"));
182
        catalogItemDto.setAlternativeNames(Set.of("altName"));
183
        catalogItemDto.setCountries(Set.of("country"));
184
        catalogItemDto.setWrittenForms(Set.of("written"));
185
        catalogItemDto.setCertainty(0);
186
        catalogItemDto.setLatitude(0.1);
187
        catalogItemDto.setLongitude(0.2);
188 541abced Jakub Smid
        catalogItemDto.setDescription("description");
189 b30f120b Jakub Smid
190
        Type type = new Type("type");
191
        Type typeOld = new Type("old");
192
193
        CatalogItem catalogItem = new CatalogItem();
194 d6451c0a Jakub Smid
        catalogItem.setId(id);
195 b30f120b Jakub Smid
        catalogItem.setName("name");
196
        catalogItem.setBibliography(Set.of(new Bibliography("bibl", catalogItem)));
197
        catalogItem.setTypes(Set.of(type));
198
        catalogItem.setAlternativeNames(Set.of(new AlternativeName("altName", catalogItem)));
199
        catalogItem.setCountries(Set.of(new Country("country", catalogItem)));
200
        catalogItem.setWrittenForms(Set.of(new WrittenForm("written", catalogItem)));
201
        catalogItem.setCertainty(0);
202
        catalogItem.setLatitude(0.1);
203
        catalogItem.setLongitude(0.2);
204 541abced Jakub Smid
        catalogItem.setDescription("description");
205 b30f120b Jakub Smid
206
        CatalogItem oldCatalogItem = new CatalogItem();
207 d6451c0a Jakub Smid
        oldCatalogItem.setId(id);
208 b30f120b Jakub Smid
        oldCatalogItem.setName("old");
209
        oldCatalogItem.setBibliography(Set.of(new Bibliography("old", oldCatalogItem)));
210
        oldCatalogItem.setTypes(Set.of(typeOld));
211
        oldCatalogItem.setAlternativeNames(Set.of(new AlternativeName("old", oldCatalogItem)));
212
        oldCatalogItem.setCountries(Set.of(new Country("old", oldCatalogItem)));
213
        oldCatalogItem.setWrittenForms(Set.of(new WrittenForm("old", catalogItem)));
214
        oldCatalogItem.setCertainty(10);
215
        oldCatalogItem.setLatitude(10.1);
216
        oldCatalogItem.setLongitude(10.2);
217
218
        given(catalogItemRepository.findById(id)).willReturn(Optional.of(oldCatalogItem));
219
        given(typeService.getTypeByName(anyString())).willReturn(Optional.empty());
220
221
        // when
222
223
        underTest.updateCatalogItem(id, catalogItemDto);
224
225
        // then
226
        verify(typeService).saveType(type);
227
228
        ArgumentCaptor<CatalogItem> argumentCaptor = ArgumentCaptor.forClass(CatalogItem.class);
229
230
        verify(catalogItemRepository).save(argumentCaptor.capture());
231
232
        CatalogItem capturedCatalogItem = argumentCaptor.getValue();
233
234
        assertThat(capturedCatalogItem).isEqualTo(catalogItem);
235
    }
236
237
    @Test
238
    void testCanNotUpdateCatalogItem() {
239
        // given
240
        UUID id = UUID.randomUUID();
241
242
        CatalogItemDto catalogItemDto = new CatalogItemDto();
243
        catalogItemDto.setName("name");
244
        catalogItemDto.setBibliography(Set.of("bibl"));
245
        catalogItemDto.setTypes(Set.of("type"));
246
        catalogItemDto.setAlternativeNames(Set.of("altName"));
247
        catalogItemDto.setCountries(Set.of("country"));
248
        catalogItemDto.setWrittenForms(Set.of("written"));
249
        catalogItemDto.setCertainty(0);
250
        catalogItemDto.setLatitude(0.1);
251
        catalogItemDto.setLongitude(0.2);
252
253
        // when
254
        // then
255
        assertThatThrownBy(() -> underTest.updateCatalogItem(id, catalogItemDto))
256
                .isInstanceOf(ApiRequestException.class)
257
                .hasMessageContaining("Catalog item not found");
258
259
        verify(catalogItemRepository, never()).save(any());
260
        verify(typeService, never()).saveType(any());
261
    }
262
263
    @Test
264
    void testCanDeleteCatalogItem() {
265
        // given
266
        UUID id = UUID.randomUUID();
267
        given(catalogItemRepository.existsById(id)).willReturn(true);
268
269
        // when
270
        underTest.deleteCatalogItem(id);
271
272
        // then
273
        verify(catalogItemRepository).deleteById(id);
274
    }
275
276
    @Test
277
    void testCanNotDeleteCatalogItem() {
278
        // given
279
        UUID id = UUID.randomUUID();
280
        given(catalogItemRepository.existsById(id)).willReturn(false);
281
282
        // when
283
        // then
284
        assertThatThrownBy(() -> underTest.deleteCatalogItem(id))
285
                .isInstanceOf(ApiRequestException.class)
286
                .hasMessageContaining("Catalog item not found");
287
288
        verify(catalogItemRepository, never()).deleteById(any());
289
    }
290
291
292 c5787e2d Jakub Smid
293
    @Test
294
    void testSearchedTest() {
295
        // given
296
        String text = "The city of Azbar was found in first century near the ocean. " +
297
                "⌈U-re-me-re⌉, another town, was found " +
298
                "in 1956. The location of Huga and Duga is not known, but it was probably near Puga.";
299
        String highlightedText = "The <b>city</b> of <span style='color:green'>Azbar</span> was found in first century near the <b>ocean</b>. " +
300
                "⌈<span style='color:green'>U-re-me-re</span>⌉, another town, was found " +
301
                "in 1956. The location of <span style='color:red'>Huga</span> and <span style='color:red'>Duga</span> is not known, but it was probably near Puga.";
302
303
        CatalogItem catalogItem1 = new CatalogItem();
304
        catalogItem1.setAlternativeNames(Stream.of(new AlternativeName("Azbar", catalogItem1)).collect(Collectors.toSet()));
305
        catalogItem1.setLatitude(5);
306
        catalogItem1.setLongitude(5);
307
308
        CatalogItem catalogItem2 = new CatalogItem();
309
        catalogItem2.setAlternativeNames(Stream.of(new AlternativeName("Azbar", catalogItem2), new AlternativeName("Azbarasdf", catalogItem2)).collect(Collectors.toSet()));
310
        catalogItem2.setLatitude(0.0);
311
        catalogItem2.setLongitude(0.0);
312
313
        CatalogItem catalogItem3 = new CatalogItem();
314
        catalogItem3.setAlternativeNames(Stream.of(new AlternativeName("U-re-me-re", catalogItem3)).collect(Collectors.toSet()));
315
        catalogItem3.setLatitude(55);
316
        catalogItem3.setLongitude(68);
317
318
        CatalogItem catalogItem4 = new CatalogItem();
319
        catalogItem4.setAlternativeNames(Stream.of(new AlternativeName("Huga", catalogItem4)).collect(Collectors.toSet()));
320
        catalogItem4.setLatitude(0.0);
321
        catalogItem4.setLongitude(0.0);
322
323
        CatalogItem catalogItem5 = new CatalogItem();
324
        catalogItem5.setAlternativeNames(Stream.of(new AlternativeName("Huga", catalogItem5), new AlternativeName("Hugaa", catalogItem5)).collect(Collectors.toSet()));
325
        catalogItem5.setLatitude(0.0);
326
        catalogItem5.setLongitude(0.0);
327
328
        CatalogItem catalogItem6 = new CatalogItem();
329
        catalogItem6.setAlternativeNames(Stream.of(new AlternativeName("Duga", catalogItem6)).collect(Collectors.toSet()));
330
        catalogItem6.setLatitude(0.0);
331
        catalogItem6.setLongitude(0.0);
332
333
        CatalogItemDto catalogItemDto1 = new CatalogItemDto();
334 d6451c0a Jakub Smid
        catalogItemDto1.setId(catalogItem1.getId());
335 c5787e2d Jakub Smid
        catalogItemDto1.setAlternativeNames(Stream.of("Azbar").collect(Collectors.toSet()));
336
        catalogItemDto1.setLatitude(5);
337
        catalogItemDto1.setLongitude(5);
338
339
        CatalogItemDto catalogItemDto2 = new CatalogItemDto();
340 d6451c0a Jakub Smid
        catalogItemDto2.setId(catalogItem2.getId());
341 c5787e2d Jakub Smid
        catalogItemDto2.setAlternativeNames(Stream.of("Azbar", "Azbarasdf").collect(Collectors.toSet()));
342
        catalogItemDto2.setLatitude(0.0);
343
        catalogItemDto2.setLongitude(0.0);
344
345
        CatalogItemDto catalogItemDto3 = new CatalogItemDto();
346 d6451c0a Jakub Smid
        catalogItemDto3.setId(catalogItem3.getId());
347 c5787e2d Jakub Smid
        catalogItemDto3.setAlternativeNames(Stream.of("U-re-me-re").collect(Collectors.toSet()));
348
        catalogItemDto3.setLatitude(55);
349
        catalogItemDto3.setLongitude(68);
350
351
        CatalogItemDto catalogItemDto4 = new CatalogItemDto();
352 d6451c0a Jakub Smid
        catalogItemDto4.setId(catalogItem4.getId());
353 c5787e2d Jakub Smid
        catalogItemDto4.setAlternativeNames(Stream.of("Huga").collect(Collectors.toSet()));
354
        catalogItemDto4.setLatitude(0.0);
355
        catalogItemDto4.setLongitude(0.0);
356
357
        CatalogItemDto catalogItemDto5 = new CatalogItemDto();
358 d6451c0a Jakub Smid
        catalogItemDto5.setId(catalogItem5.getId());
359 c5787e2d Jakub Smid
        catalogItemDto5.setAlternativeNames(Stream.of("Huga", "Hugaa").collect(Collectors.toSet()));
360
        catalogItemDto5.setLatitude(0.0);
361
        catalogItemDto5.setLongitude(0.0);
362
363
        CatalogItemDto catalogItemDto6 = new CatalogItemDto();
364 d6451c0a Jakub Smid
        catalogItemDto6.setId(catalogItem6.getId());
365 c5787e2d Jakub Smid
        catalogItemDto6.setAlternativeNames(Stream.of("Duga").collect(Collectors.toSet()));
366
        catalogItemDto6.setLatitude(0.0);
367
        catalogItemDto6.setLongitude(0.0);
368
369
370
        given(typeService.getAllTypesAsString()).willReturn(Stream.of("city", "ocean").collect(Collectors.toSet()));
371
        given(catalogItemRepository.getItemsByName(anyString())).willReturn(Collections.emptyList());
372
        given(catalogItemRepository.getItemsByName("Azbar")).willReturn(Stream.of(catalogItem1, catalogItem2).collect(Collectors.toList()));
373
        given(catalogItemRepository.getItemsByName("U-re-me-re")).willReturn(Stream.of(catalogItem3).collect(Collectors.toList()));
374
        given(catalogItemRepository.getItemsByName("Huga")).willReturn(Stream.of(catalogItem4, catalogItem5).collect(Collectors.toList()));
375
        given(catalogItemRepository.getItemsByName("Duga")).willReturn(Stream.of(catalogItem6).collect(Collectors.toList()));
376
377
        // when
378
        PathDto pathDto = underTest.getPath(text);
379
380
        // then
381
        verify(catalogItemRepository).getItemsByName("Azbar");
382
        verify(catalogItemRepository).getItemsByName("U-re-me-re");
383
        verify(catalogItemRepository).getItemsByName("Huga");
384
        verify(catalogItemRepository).getItemsByName("Duga");
385
        verify(catalogItemRepository).getItemsByName("Puga");
386
387
        assertThat(pathDto.getText()).isEqualTo(highlightedText);
388
389
        assertThat(pathDto.getFoundCatalogItems().size()).isEqualTo(4);
390
391
        assertThat(pathDto.getFoundCatalogItems().get(0).size()).isEqualTo(2);
392
        assertThat(pathDto.getFoundCatalogItems().get(0).get(0)).isEqualTo(catalogItemDto1);
393
        assertThat(pathDto.getFoundCatalogItems().get(0).get(1)).isEqualTo(catalogItemDto2);
394
395
        assertThat(pathDto.getFoundCatalogItems().get(1).size()).isEqualTo(1);
396
        assertThat(pathDto.getFoundCatalogItems().get(1).get(0)).isEqualTo(catalogItemDto3);
397
398
        assertThat(pathDto.getFoundCatalogItems().get(2).size()).isEqualTo(2);
399
        assertThat(pathDto.getFoundCatalogItems().get(2).get(0)).isEqualTo(catalogItemDto4);
400
        assertThat(pathDto.getFoundCatalogItems().get(2).get(1)).isEqualTo(catalogItemDto5);
401
402
        assertThat(pathDto.getFoundCatalogItems().get(3).size()).isEqualTo(1);
403
        assertThat(pathDto.getFoundCatalogItems().get(3).get(0)).isEqualTo(catalogItemDto6);
404
    }
405 b30f120b Jakub Smid
}