commit c5787e2d7aca790dea59aae6679a10efe6fafb75
Author: Jakub Smid <jaksmid@seznam.cz>
Date:   Fri Apr 15 09:15:40 2022 +0200

    Added test for path
    
    re #9490

diff --git a/backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepositoryTest.java b/backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepositoryTest.java
index 9b3aa1e..3d32d7b 100644
--- a/backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepositoryTest.java
+++ b/backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepositoryTest.java
@@ -14,7 +14,7 @@ import java.util.List;
 import java.util.Set;
 
 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 @DataJpaTest
 class CatalogItemRepositoryTest {
@@ -323,4 +323,18 @@ class CatalogItemRepositoryTest {
         assertThat(filterResult.size()).isEqualTo(1);
         assertTrue(filterResult.contains(catalogItem3));
     }
+
+    @Test
+    void testItemsByName() {
+        // given
+        String name = "twelve";
+
+        // when
+        List<CatalogItem> res = underTest.getItemsByName(name);
+
+        // then
+        assertThat(res.size()).isEqualTo(2);
+        assertTrue(res.contains(catalogItem2));
+        assertTrue(res.contains(catalogItem4));
+    }
 }
\ No newline at end of file
diff --git a/backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImplTest.java b/backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImplTest.java
index 51bf71a..2562276 100644
--- a/backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImplTest.java
+++ b/backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImplTest.java
@@ -4,19 +4,20 @@ import cz.zcu.kiv.backendapi.alternativename.AlternativeName;
 import cz.zcu.kiv.backendapi.bibliography.Bibliography;
 import cz.zcu.kiv.backendapi.country.Country;
 import cz.zcu.kiv.backendapi.exception.ApiRequestException;
+import cz.zcu.kiv.backendapi.path.PathDto;
 import cz.zcu.kiv.backendapi.type.Type;
 import cz.zcu.kiv.backendapi.type.TypeServiceImpl;
 import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.*;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 
-import java.util.List;
-import java.util.Optional;
-import java.util.Set;
-import java.util.UUID;
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
 import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
@@ -128,6 +129,38 @@ class CatalogItemServiceImplTest {
 
     }
 
+    @Test
+    void testCanGetCatalogItem() {
+        // given
+        UUID id = UUID.randomUUID();
+        CatalogItem catalogItem = new CatalogItem();
+        catalogItem.setId(id);
+        CatalogItemDto catalogItemDto = new CatalogItemDto();
+        catalogItemDto.setId(id);
+        given(catalogItemRepository.findById(id)).willReturn(Optional.of(catalogItem));
+
+        // when
+        CatalogItemDto retrievedItemDto = underTest.getCatalogItem(id);
+
+        // then
+        verify(catalogItemRepository).findById(id);
+        assertThat(retrievedItemDto).isEqualTo(catalogItemDto);
+    }
+
+    @Test
+    void testCanNotGetCatalogItem() {
+        // given
+        UUID id = UUID.randomUUID();
+
+        // when
+        // then
+        assertThatThrownBy(() -> underTest.getCatalogItem(id))
+                .isInstanceOf(ApiRequestException.class)
+                .hasMessageContaining("Catalog item not found");
+
+        verify(catalogItemRepository).findById(id);
+    }
+
     @Test
     void testCanUpdateCatalogItem() {
         // given
@@ -310,4 +343,111 @@ class CatalogItemServiceImplTest {
 
         verify(catalogItemRepository).filterCatalog(nameChanged, countryChanged, typeChanged);
     }
+
+    @Test
+    void testSearchedTest() {
+        // given
+        String text = "The city of Azbar was found in first century near the ocean. " +
+                "⌈U-re-me-re⌉, another town, was found " +
+                "in 1956. The location of Huga and Duga is not known, but it was probably near Puga.";
+        String highlightedText = "The <b>city</b> of <span style='color:green'>Azbar</span> was found in first century near the <b>ocean</b>. " +
+                "⌈<span style='color:green'>U-re-me-re</span>⌉, another town, was found " +
+                "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.";
+
+        CatalogItem catalogItem1 = new CatalogItem();
+        catalogItem1.setAlternativeNames(Stream.of(new AlternativeName("Azbar", catalogItem1)).collect(Collectors.toSet()));
+        catalogItem1.setLatitude(5);
+        catalogItem1.setLongitude(5);
+
+        CatalogItem catalogItem2 = new CatalogItem();
+        catalogItem2.setAlternativeNames(Stream.of(new AlternativeName("Azbar", catalogItem2), new AlternativeName("Azbarasdf", catalogItem2)).collect(Collectors.toSet()));
+        catalogItem2.setLatitude(0.0);
+        catalogItem2.setLongitude(0.0);
+
+        CatalogItem catalogItem3 = new CatalogItem();
+        catalogItem3.setAlternativeNames(Stream.of(new AlternativeName("U-re-me-re", catalogItem3)).collect(Collectors.toSet()));
+        catalogItem3.setLatitude(55);
+        catalogItem3.setLongitude(68);
+
+        CatalogItem catalogItem4 = new CatalogItem();
+        catalogItem4.setAlternativeNames(Stream.of(new AlternativeName("Huga", catalogItem4)).collect(Collectors.toSet()));
+        catalogItem4.setLatitude(0.0);
+        catalogItem4.setLongitude(0.0);
+
+        CatalogItem catalogItem5 = new CatalogItem();
+        catalogItem5.setAlternativeNames(Stream.of(new AlternativeName("Huga", catalogItem5), new AlternativeName("Hugaa", catalogItem5)).collect(Collectors.toSet()));
+        catalogItem5.setLatitude(0.0);
+        catalogItem5.setLongitude(0.0);
+
+        CatalogItem catalogItem6 = new CatalogItem();
+        catalogItem6.setAlternativeNames(Stream.of(new AlternativeName("Duga", catalogItem6)).collect(Collectors.toSet()));
+        catalogItem6.setLatitude(0.0);
+        catalogItem6.setLongitude(0.0);
+
+        CatalogItemDto catalogItemDto1 = new CatalogItemDto();
+        catalogItemDto1.setAlternativeNames(Stream.of("Azbar").collect(Collectors.toSet()));
+        catalogItemDto1.setLatitude(5);
+        catalogItemDto1.setLongitude(5);
+
+        CatalogItemDto catalogItemDto2 = new CatalogItemDto();
+        catalogItemDto2.setAlternativeNames(Stream.of("Azbar", "Azbarasdf").collect(Collectors.toSet()));
+        catalogItemDto2.setLatitude(0.0);
+        catalogItemDto2.setLongitude(0.0);
+
+        CatalogItemDto catalogItemDto3 = new CatalogItemDto();
+        catalogItemDto3.setAlternativeNames(Stream.of("U-re-me-re").collect(Collectors.toSet()));
+        catalogItemDto3.setLatitude(55);
+        catalogItemDto3.setLongitude(68);
+
+        CatalogItemDto catalogItemDto4 = new CatalogItemDto();
+        catalogItemDto4.setAlternativeNames(Stream.of("Huga").collect(Collectors.toSet()));
+        catalogItemDto4.setLatitude(0.0);
+        catalogItemDto4.setLongitude(0.0);
+
+        CatalogItemDto catalogItemDto5 = new CatalogItemDto();
+        catalogItemDto5.setAlternativeNames(Stream.of("Huga", "Hugaa").collect(Collectors.toSet()));
+        catalogItemDto5.setLatitude(0.0);
+        catalogItemDto5.setLongitude(0.0);
+
+        CatalogItemDto catalogItemDto6 = new CatalogItemDto();
+        catalogItemDto6.setAlternativeNames(Stream.of("Duga").collect(Collectors.toSet()));
+        catalogItemDto6.setLatitude(0.0);
+        catalogItemDto6.setLongitude(0.0);
+
+
+        given(typeService.getAllTypesAsString()).willReturn(Stream.of("city", "ocean").collect(Collectors.toSet()));
+        given(catalogItemRepository.getItemsByName(anyString())).willReturn(Collections.emptyList());
+        given(catalogItemRepository.getItemsByName("Azbar")).willReturn(Stream.of(catalogItem1, catalogItem2).collect(Collectors.toList()));
+        given(catalogItemRepository.getItemsByName("U-re-me-re")).willReturn(Stream.of(catalogItem3).collect(Collectors.toList()));
+        given(catalogItemRepository.getItemsByName("Huga")).willReturn(Stream.of(catalogItem4, catalogItem5).collect(Collectors.toList()));
+        given(catalogItemRepository.getItemsByName("Duga")).willReturn(Stream.of(catalogItem6).collect(Collectors.toList()));
+
+        // when
+        PathDto pathDto = underTest.getPath(text);
+
+        // then
+        verify(catalogItemRepository).getItemsByName("Azbar");
+        verify(catalogItemRepository).getItemsByName("U-re-me-re");
+        verify(catalogItemRepository).getItemsByName("Huga");
+        verify(catalogItemRepository).getItemsByName("Duga");
+        verify(catalogItemRepository).getItemsByName("Puga");
+
+        assertThat(pathDto.getText()).isEqualTo(highlightedText);
+
+        assertThat(pathDto.getFoundCatalogItems().size()).isEqualTo(4);
+
+        assertThat(pathDto.getFoundCatalogItems().get(0).size()).isEqualTo(2);
+        assertThat(pathDto.getFoundCatalogItems().get(0).get(0)).isEqualTo(catalogItemDto1);
+        assertThat(pathDto.getFoundCatalogItems().get(0).get(1)).isEqualTo(catalogItemDto2);
+
+        assertThat(pathDto.getFoundCatalogItems().get(1).size()).isEqualTo(1);
+        assertThat(pathDto.getFoundCatalogItems().get(1).get(0)).isEqualTo(catalogItemDto3);
+
+        assertThat(pathDto.getFoundCatalogItems().get(2).size()).isEqualTo(2);
+        assertThat(pathDto.getFoundCatalogItems().get(2).get(0)).isEqualTo(catalogItemDto4);
+        assertThat(pathDto.getFoundCatalogItems().get(2).get(1)).isEqualTo(catalogItemDto5);
+
+        assertThat(pathDto.getFoundCatalogItems().get(3).size()).isEqualTo(1);
+        assertThat(pathDto.getFoundCatalogItems().get(3).get(0)).isEqualTo(catalogItemDto6);
+    }
 }
\ No newline at end of file
diff --git a/backend/src/test/java/cz/zcu/kiv/backendapi/type/TypeServiceImplTest.java b/backend/src/test/java/cz/zcu/kiv/backendapi/type/TypeServiceImplTest.java
index 51fcda5..2c977b0 100644
--- a/backend/src/test/java/cz/zcu/kiv/backendapi/type/TypeServiceImplTest.java
+++ b/backend/src/test/java/cz/zcu/kiv/backendapi/type/TypeServiceImplTest.java
@@ -7,8 +7,12 @@ import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.mockito.BDDMockito.given;
 import static org.mockito.Mockito.verify;
 
 @ExtendWith(MockitoExtension.class)
@@ -44,9 +48,42 @@ class TypeServiceImplTest {
     void getTypeByName() {
         // when
         String typeName = "type";
+
+        // given
         underTest.getTypeByName(typeName);
 
         // then
         verify(typeRepository).findById(typeName);
     }
+
+    @Test
+    void getTypesAsString() {
+        // when
+        String stringType1 = "type1";
+        String stringType2 = "type2";
+        String stringType3 = "type3";
+        String stringType4 = "type4";
+        String stringType5 = "type5";
+
+        Type type1 = new Type(stringType1);
+        Type type2 = new Type(stringType2);
+        Type type3 = new Type(stringType3);
+        Type type4 = new Type(stringType4);
+        Type type5 = new Type(stringType5);
+
+        // given
+        given(typeRepository.findAll()).willReturn(Stream.of(type1, type2, type3, type4, type5).collect(Collectors.toList()));
+        Set<String> res = underTest.getAllTypesAsString();
+
+        // then
+        verify(typeRepository).findAll();
+
+        assertThat(res.size()).isEqualTo(5);
+
+        assertThat(res.contains(stringType1)).isTrue();
+        assertThat(res.contains(stringType2)).isTrue();
+        assertThat(res.contains(stringType3)).isTrue();
+        assertThat(res.contains(stringType4)).isTrue();
+        assertThat(res.contains(stringType5)).isTrue();
+    }
 }
\ No newline at end of file
