Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 15f5a407

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

Added first unit tests
- for catalog and user repository
- for type and title page service
re #9363

Zobrazit rozdíly:

backend/pom.xml
33 33
            <scope>test</scope>
34 34
        </dependency>
35 35

  
36
        <dependency>
37
            <groupId>com.h2database</groupId>
38
            <artifactId>h2</artifactId>
39
            <scope>test</scope>
40
        </dependency>
36 41

  
37 42
        <dependency>
38 43
            <groupId>org.postgresql</groupId>
......
82 87
        <dependency>
83 88
            <groupId>org.springframework.boot</groupId>
84 89
            <artifactId>spring-boot-configuration-processor</artifactId>
85
            <version>2.4.2</version>
90
            <version>2.6.5</version>
86 91
            <optional>true</optional>
87 92
        </dependency>
88 93

  
backend/src/main/java/cz/zcu/kiv/backendapi/alternativename/AlternativeName.java
3 3
import cz.zcu.kiv.backendapi.catalog.CatalogEntry;
4 4
import lombok.AllArgsConstructor;
5 5
import lombok.Data;
6
import lombok.EqualsAndHashCode;
6 7
import lombok.NoArgsConstructor;
7 8

  
8 9
import javax.persistence.*;
......
12 13
 * Alternative name entity representing alternative name of geographic entry
13 14
 */
14 15
@Data
16
@EqualsAndHashCode(exclude = "catalog")
15 17
@NoArgsConstructor
16 18
@AllArgsConstructor
17 19
@Entity
backend/src/main/java/cz/zcu/kiv/backendapi/bibliography/Bibliography.java
3 3
import cz.zcu.kiv.backendapi.catalog.CatalogEntry;
4 4
import lombok.AllArgsConstructor;
5 5
import lombok.Data;
6
import lombok.EqualsAndHashCode;
6 7
import lombok.NoArgsConstructor;
7 8

  
8 9
import javax.persistence.*;
......
12 13
 * Bibliography entity representing bibliography
13 14
 */
14 15
@Data
16
@EqualsAndHashCode(exclude = "catalog")
15 17
@NoArgsConstructor
16 18
@AllArgsConstructor
17 19
@Entity
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogEntry.java
5 5
import cz.zcu.kiv.backendapi.country.Country;
6 6
import cz.zcu.kiv.backendapi.type.Type;
7 7
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
8
import lombok.Getter;
9
import lombok.NoArgsConstructor;
10
import lombok.Setter;
8
import lombok.*;
11 9
import org.hibernate.annotations.LazyCollection;
12 10
import org.hibernate.annotations.LazyCollectionOption;
13 11

  
......
20 18
/**
21 19
 * Catalog entity representing catalog entry
22 20
 */
23
//@Data
24
@Getter
25
@Setter
21
@Data
26 22
@NoArgsConstructor
27 23
@Entity
28 24
@Table(name = "catalog")
......
104 100

  
105 101
        this.name = csvFields.get(1);
106 102
        List<String> stringList = processListField(csvFields.get(2));
107
        this.alternativeNames = stringList.stream().map(s->new AlternativeName(s, this)).collect(Collectors.toSet());
103
        this.alternativeNames = stringList.stream().map(s -> new AlternativeName(s, this)).collect(Collectors.toSet());
108 104

  
109 105
        this.certainty = processIntField(csvFields.get(3));
110 106
        this.latitude = processDoubleField(csvFields.get(4));
111 107
        this.longitude = processDoubleField(csvFields.get(5));
112 108

  
113 109
        stringList = processListField(csvFields.get(6));
114
        this.writtenForms = stringList.stream().map(s->new WrittenForm(s, this)).collect(Collectors.toSet());
110
        this.writtenForms = stringList.stream().map(s -> new WrittenForm(s, this)).collect(Collectors.toSet());
115 111

  
116 112
        stringList = processListField(csvFields.get(7));
117 113
        this.types = stringList.stream().map(Type::new).collect(Collectors.toSet());
118 114

  
119 115
        stringList = processListField(csvFields.get(8));
120
        this.countries = stringList.stream().map(s->new Country(s, this)).collect(Collectors.toSet());
116
        this.countries = stringList.stream().map(s -> new Country(s, this)).collect(Collectors.toSet());
121 117

  
122 118
        stringList = processListField(csvFields.get(9));
123
        this.bibliography = stringList.stream().map(s->new Bibliography(s, this)).collect(Collectors.toSet());
119
        this.bibliography = stringList.stream().map(s -> new Bibliography(s, this)).collect(Collectors.toSet());
124 120
    }
125 121

  
126 122
    private int processIntField(String field) {
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogEntryRepository.java
24 24
    @Query("SELECT DISTINCT e FROM CatalogEntry e LEFT JOIN AlternativeName a ON e = a.catalog " +
25 25
            "LEFT JOIN Country c ON e = c.catalog " +
26 26
            "INNER JOIN e.types t " +
27
            "WHERE (?1 = '' OR e.name LIKE ?1 OR a.name LIKE ?1) " +
28
            "AND (?2 = '' OR c.name LIKE ?2) " +
29
            "AND (?3 = '' OR t.type LIKE ?3)")
27
            "WHERE (?1 = '' OR UPPER(e.name) LIKE UPPER(?1) OR UPPER(a.name) LIKE UPPER(?1)) " +
28
            "AND (?2 = '' OR UPPER(c.name) LIKE UPPER(?2)) " +
29
            "AND (?3 = '' OR UPPER(t.type) LIKE UPPER(?3))")
30 30
    Set<CatalogEntry> filterCatalog(String name, String country, String type);
31 31
}
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogEntryServiceImpl.java
26 26
@RequiredArgsConstructor
27 27
@Slf4j
28 28
public class CatalogEntryServiceImpl implements ICatalogEntryService {
29
    /**
30
     * Regex for one arbitrary character in string
31
     */
32
    private static final String WILDCARD_CHARACTER_REGEX = "\\?";
33

  
34
    /**
35
     * Regex for one arbitrary character in string used in SQL language
36
     */
37
    private static final String WILDCARD_CHARACTER_REGEX_SQL = "_";
38

  
39
    /**
40
     * Regex for any number of arbitrary characters in string
41
     */
42
    private static final String WILDCARD_CHARACTERS_REGEX = "\\*";
43

  
44
    /**
45
     * Regex for any number of arbitrary characters in string used in SQL language
46
     */
47
    private static final String WILDCARD_CHARACTERS_REGEX_SQL = "%";
48

  
29 49
    /**
30 50
     * Message for exception when catalog entry is not found by id
31 51
     */
32 52
    private static final String CATALOG_ENTRY_NOT_FOUND = "Catalog entry not found";
53

  
33 54
    /**
34 55
     * Catalog repository
35 56
     */
......
78 99
    @Override
79 100
    public List<CatalogEntryDto> getCatalog(String name, String country, String type) {
80 101
        log.info("Retrieving catalog");
102
        name = name.replaceAll(WILDCARD_CHARACTER_REGEX, WILDCARD_CHARACTER_REGEX_SQL);
103
        name = name.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
104
        country = country.replaceAll(WILDCARD_CHARACTER_REGEX, WILDCARD_CHARACTER_REGEX_SQL);
105
        country = country.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
106
        type = type.replaceAll(WILDCARD_CHARACTER_REGEX, WILDCARD_CHARACTER_REGEX_SQL);
107
        type = type.replaceAll(WILDCARD_CHARACTERS_REGEX, WILDCARD_CHARACTERS_REGEX_SQL);
81 108
        Set<CatalogEntry> entities = catalogEntryRepository.filterCatalog(name, country, type);
82 109
        return entities.stream().map(this::convertEntityToDto).collect(Collectors.toList());
83 110
    }
backend/src/main/java/cz/zcu/kiv/backendapi/config/DataInitiator.java
43 43
    @Transactional
44 44
    public void onApplicationEvent(ContextRefreshedEvent event) {
45 45
        List<Type> types = loadTypes();
46
        typeService.saveTypes(types);
46
        for (Type type : types) {
47
            typeService.saveType(type);
48
        }
47 49
        List<CatalogEntry> catalog = loadCatalog();
48 50
        catalogService.saveCatalog(catalog);
49 51

  
backend/src/main/java/cz/zcu/kiv/backendapi/country/Country.java
3 3
import cz.zcu.kiv.backendapi.catalog.CatalogEntry;
4 4
import lombok.AllArgsConstructor;
5 5
import lombok.Data;
6
import lombok.EqualsAndHashCode;
6 7
import lombok.NoArgsConstructor;
7 8

  
8 9
import javax.persistence.*;
......
12 13
 * Country entity representing country of geographic entry
13 14
 */
14 15
@Data
16
@EqualsAndHashCode(exclude = "catalog")
15 17
@NoArgsConstructor
16 18
@AllArgsConstructor
17 19
@Entity
backend/src/main/java/cz/zcu/kiv/backendapi/password/PasswordDto.java
1
package cz.zcu.kiv.backendapi.password;
2

  
3
import com.fasterxml.jackson.annotation.JsonIgnore;
4
import cz.zcu.kiv.backendapi.validation.PasswordMatches;
5
import lombok.Data;
6
import lombok.NoArgsConstructor;
7

  
8
import javax.validation.constraints.NotEmpty;
9
import javax.validation.constraints.NotNull;
10
import javax.validation.constraints.Size;
11

  
12
/**
13
 * Password data object
14
 */
15
@Data
16
@PasswordMatches
17
@NoArgsConstructor
18
public class PasswordDto {
19
    /**
20
     * Minimal length for password
21
     */
22
    @JsonIgnore
23
    private  static final int MIN_PASSWORD_LENGTH = 8;
24

  
25
    /**
26
     * New password - must be strong enough
27
     */
28
    @NotNull(message = "Password must not be null")
29
    @NotEmpty(message = "Password must not be empty")
30
    @Size(min = MIN_PASSWORD_LENGTH, message = "Password must be at least " + MIN_PASSWORD_LENGTH + " characters long")
31
    private String password;
32

  
33
    /**
34
     * New password confirmation - must match new password
35
     */
36
    @NotNull(message = "Confirmation password must not be null")
37
    @NotEmpty(message = "Confirmation password must not be empty")
38
    private String confirmationPassword;
39

  
40
}
backend/src/main/java/cz/zcu/kiv/backendapi/permission/PermissionDto.java
1
package cz.zcu.kiv.backendapi.permission;
2

  
3
import lombok.Data;
4

  
5
/**
6
 * Permission DTO
7
 */
8
@Data
9
public class PermissionDto {
10
    /**
11
     * Whether user has 'READ' authority
12
     */
13
    private boolean canRead;
14

  
15
    /**
16
     * Whether user has 'WRITE' authority
17
     */
18
    private boolean canWrite;
19

  
20
    /**
21
     * Whether user has 'DELETE' authority
22
     */
23
    private boolean canDelete;
24
}
backend/src/main/java/cz/zcu/kiv/backendapi/security/jwt/JwtUtils.java
68 68
     */
69 69
    @Bean
70 70
    public Algorithm getAlgorithm() {
71
        return Algorithm.HMAC256(secretKey.getBytes());
71
        return Algorithm.HMAC512(secretKey.getBytes());
72 72
    }
73 73

  
74 74
    /**
backend/src/main/java/cz/zcu/kiv/backendapi/type/ITypeService.java
1 1
package cz.zcu.kiv.backendapi.type;
2 2

  
3
import java.util.List;
4 3
import java.util.Optional;
5 4

  
6 5
/**
......
8 7
 */
9 8
public interface ITypeService {
10 9

  
10
    /**
11
     * Saves on type
12
     *
13
     * @param type type
14
     */
11 15
    void saveType(Type type);
12 16

  
13
    void saveTypes(List<Type> typesEntities);
14

  
17
    /**
18
     * Returns type by name (optional)
19
     *
20
     * @param type type name
21
     * @return type by name (optional)
22
     */
15 23
    Optional<Type> getTypeByName(String type);
16 24
}
backend/src/main/java/cz/zcu/kiv/backendapi/type/TypeServiceImpl.java
4 4
import org.springframework.stereotype.Service;
5 5
import org.springframework.transaction.annotation.Transactional;
6 6

  
7
import java.util.List;
8 7
import java.util.Optional;
9 8

  
10 9
/**
......
24 23
        typeRepository.save(type);
25 24
    }
26 25

  
27
    @Override
28
    public void saveTypes(List<Type> typesEntities) {
29
        typeRepository.saveAll(typesEntities);
30
    }
31

  
32 26
    @Override
33 27
    public Optional<Type> getTypeByName(String type) {
34 28
        return typeRepository.findById(type);
backend/src/main/java/cz/zcu/kiv/backendapi/user/IUserService.java
1 1
package cz.zcu.kiv.backendapi.user;
2 2

  
3
import cz.zcu.kiv.backendapi.password.PasswordDto;
4
import cz.zcu.kiv.backendapi.permission.PermissionDto;
3
import cz.zcu.kiv.backendapi.user.password.PasswordDto;
4
import cz.zcu.kiv.backendapi.user.permission.PermissionDto;
5 5

  
6 6
import java.util.List;
7 7

  
backend/src/main/java/cz/zcu/kiv/backendapi/user/UserController.java
6 6
import com.auth0.jwt.interfaces.DecodedJWT;
7 7
import com.google.common.base.Strings;
8 8
import cz.zcu.kiv.backendapi.exception.ApiRequestException;
9
import cz.zcu.kiv.backendapi.password.PasswordDto;
9
import cz.zcu.kiv.backendapi.user.password.PasswordDto;
10 10
import cz.zcu.kiv.backendapi.security.jwt.JwtUtils;
11 11
import lombok.RequiredArgsConstructor;
12 12
import lombok.extern.slf4j.Slf4j;
......
15 15
import org.springframework.http.ResponseEntity;
16 16
import org.springframework.security.core.GrantedAuthority;
17 17
import org.springframework.web.bind.annotation.*;
18
import cz.zcu.kiv.backendapi.permission.PermissionDto;
18
import cz.zcu.kiv.backendapi.user.permission.PermissionDto;
19 19

  
20 20
import javax.servlet.http.HttpServletRequest;
21 21
import javax.servlet.http.HttpServletResponse;
backend/src/main/java/cz/zcu/kiv/backendapi/user/UserDto.java
1 1
package cz.zcu.kiv.backendapi.user;
2 2

  
3
import cz.zcu.kiv.backendapi.password.PasswordDto;
3
import cz.zcu.kiv.backendapi.user.password.PasswordDto;
4 4
import cz.zcu.kiv.backendapi.validation.PasswordMatches;
5 5
import cz.zcu.kiv.backendapi.validation.ValidEmail;
6 6
import lombok.Data;
7
import cz.zcu.kiv.backendapi.permission.PermissionDto;
7
import cz.zcu.kiv.backendapi.user.permission.PermissionDto;
8 8

  
9 9
import javax.validation.Valid;
10 10
import javax.validation.constraints.NotEmpty;
backend/src/main/java/cz/zcu/kiv/backendapi/user/UserEntity.java
1 1
package cz.zcu.kiv.backendapi.user;
2 2

  
3
import lombok.AllArgsConstructor;
3 4
import lombok.Data;
4 5
import lombok.NoArgsConstructor;
5 6
import org.hibernate.annotations.Type;
backend/src/main/java/cz/zcu/kiv/backendapi/user/UserServiceImpl.java
1 1
package cz.zcu.kiv.backendapi.user;
2 2

  
3 3
import cz.zcu.kiv.backendapi.exception.ApiRequestException;
4
import cz.zcu.kiv.backendapi.password.PasswordDto;
4
import cz.zcu.kiv.backendapi.user.password.PasswordDto;
5 5
import lombok.RequiredArgsConstructor;
6 6
import lombok.extern.slf4j.Slf4j;
7 7
import org.springframework.http.HttpStatus;
......
12 12
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
13 13
import org.springframework.stereotype.Service;
14 14
import org.springframework.transaction.annotation.Transactional;
15
import cz.zcu.kiv.backendapi.permission.PermissionDto;
15
import cz.zcu.kiv.backendapi.user.permission.PermissionDto;
16 16

  
17 17
import java.util.List;
18 18
import java.util.stream.Collectors;
backend/src/main/java/cz/zcu/kiv/backendapi/user/password/PasswordDto.java
1
package cz.zcu.kiv.backendapi.user.password;
2

  
3
import com.fasterxml.jackson.annotation.JsonIgnore;
4
import cz.zcu.kiv.backendapi.validation.PasswordMatches;
5
import lombok.Data;
6
import lombok.NoArgsConstructor;
7

  
8
import javax.validation.constraints.NotEmpty;
9
import javax.validation.constraints.NotNull;
10
import javax.validation.constraints.Size;
11

  
12
/**
13
 * Password data object
14
 */
15
@Data
16
@PasswordMatches
17
@NoArgsConstructor
18
public class PasswordDto {
19
    /**
20
     * Minimal length for password
21
     */
22
    @JsonIgnore
23
    private  static final int MIN_PASSWORD_LENGTH = 8;
24

  
25
    /**
26
     * New password - must be strong enough
27
     */
28
    @NotNull(message = "Password must not be null")
29
    @NotEmpty(message = "Password must not be empty")
30
    @Size(min = MIN_PASSWORD_LENGTH, message = "Password must be at least " + MIN_PASSWORD_LENGTH + " characters long")
31
    private String password;
32

  
33
    /**
34
     * New password confirmation - must match new password
35
     */
36
    @NotNull(message = "Confirmation password must not be null")
37
    @NotEmpty(message = "Confirmation password must not be empty")
38
    private String confirmationPassword;
39

  
40
}
backend/src/main/java/cz/zcu/kiv/backendapi/user/permission/PermissionDto.java
1
package cz.zcu.kiv.backendapi.user.permission;
2

  
3
import lombok.Data;
4

  
5
/**
6
 * Permission DTO
7
 */
8
@Data
9
public class PermissionDto {
10
    /**
11
     * Whether user has 'READ' authority
12
     */
13
    private boolean canRead;
14

  
15
    /**
16
     * Whether user has 'WRITE' authority
17
     */
18
    private boolean canWrite;
19

  
20
    /**
21
     * Whether user has 'DELETE' authority
22
     */
23
    private boolean canDelete;
24
}
backend/src/main/java/cz/zcu/kiv/backendapi/validation/PasswordMatchesValidator.java
1 1
package cz.zcu.kiv.backendapi.validation;
2 2

  
3
import cz.zcu.kiv.backendapi.password.PasswordDto;
3
import cz.zcu.kiv.backendapi.user.password.PasswordDto;
4 4

  
5 5
import javax.validation.ConstraintValidator;
6 6
import javax.validation.ConstraintValidatorContext;
backend/src/main/java/cz/zcu/kiv/backendapi/writtenform/WrittenForm.java
3 3
import cz.zcu.kiv.backendapi.catalog.CatalogEntry;
4 4
import lombok.AllArgsConstructor;
5 5
import lombok.Data;
6
import lombok.EqualsAndHashCode;
6 7
import lombok.NoArgsConstructor;
7 8

  
8 9
import javax.persistence.*;
......
12 13
 * Written form entity representing written form of geographic entry
13 14
 */
14 15
@Data
16
@EqualsAndHashCode(exclude = "catalog")
15 17
@NoArgsConstructor
16 18
@AllArgsConstructor
17 19
@Entity
backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogEntryRepositoryTest.java
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 cz.zcu.kiv.backendapi.user.UserEntity;
8
import org.junit.jupiter.api.AfterEach;
9
import org.junit.jupiter.api.BeforeEach;
10
import org.junit.jupiter.api.Test;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
13

  
14
import java.util.Collections;
15
import java.util.List;
16
import java.util.Optional;
17
import java.util.Set;
18

  
19
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
20
import static org.junit.jupiter.api.Assertions.*;
21

  
22
@DataJpaTest
23
class CatalogEntryRepositoryTest {
24

  
25
    @Autowired
26
    private CatalogEntryRepository underTest;
27

  
28
    @Autowired
29
    private TypeRepository typeRepository;
30

  
31
    private CatalogEntry catalogEntry1;
32
    private CatalogEntry catalogEntry2;
33
    private CatalogEntry catalogEntry3;
34
    private CatalogEntry catalogEntry4;
35
    private CatalogEntry catalogEntry5;
36

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

  
46
        String nameFirst = "first";
47
        String nameFirstUpper = "First";
48
        String nameSecond = "second";
49
        String nameSedond = "sedond";
50
        String nameThird = "third";
51
        String nameTwelve = "twelve";
52
        String nameTwentyUpper = "TWENTY";
53

  
54
        String countryAaa = "aaa";
55
        String countryAaaUpper = "AAA";
56
        String countryBbb = "bbb";
57
        String countryBccb = "bccb";
58
        String countryCcc = "ccc";
59
        String countryDdd = "ddd";
60
        String countryDcd = "dcd";
61

  
62
        catalogEntry1 = new CatalogEntry();
63
        catalogEntry2 = new CatalogEntry();
64
        catalogEntry3 = new CatalogEntry();
65
        catalogEntry4 = new CatalogEntry();
66
        catalogEntry5 = new CatalogEntry();
67

  
68
        catalogEntry1.setName(nameFirst);
69
        catalogEntry1.setTypes(Set.of(typeCountry, typeCity));
70
        catalogEntry1.setCountries(Set.of(new Country(countryAaa, catalogEntry1), new Country(countryDcd, catalogEntry1)));
71

  
72
        catalogEntry2.setName(nameSecond);
73
        catalogEntry2.setAlternativeNames(Set.of(new AlternativeName(nameTwelve, catalogEntry2), new AlternativeName(nameThird, catalogEntry2)));
74
        catalogEntry2.setTypes(Set.of(typeCountry, typeCityUpper));
75
        catalogEntry2.setCountries(Set.of(new Country(countryAaa, catalogEntry2), new Country(countryBbb, catalogEntry2)));
76

  
77
        catalogEntry3.setName(nameThird);
78
        catalogEntry3.setAlternativeNames(Set.of(new AlternativeName(nameTwentyUpper, catalogEntry3), new AlternativeName(nameSedond, catalogEntry3)));
79
        catalogEntry3.setTypes(Set.of(typeCountri, typeCapitalCity));
80
        catalogEntry3.setCountries(Set.of(new Country(countryAaaUpper, catalogEntry3), new Country(countryCcc, catalogEntry3)));
81

  
82

  
83
        catalogEntry4.setAlternativeNames(Set.of(new AlternativeName(nameTwelve, catalogEntry4), new AlternativeName(nameFirstUpper, catalogEntry4)));
84
        catalogEntry4.setTypes(Set.of(typeCountri, typeCountry));
85
        catalogEntry4.setCountries(Set.of(new Country(countryBccb, catalogEntry4), new Country(countryDdd, catalogEntry4)));
86

  
87

  
88
        catalogEntry5.setName(nameSedond);
89
        catalogEntry5.setAlternativeNames(Set.of(new AlternativeName(nameThird, catalogEntry5)));
90
        catalogEntry5.setTypes(Set.of(typeCountri));
91

  
92
        underTest.saveAll(List.of(catalogEntry1, catalogEntry2, catalogEntry3, catalogEntry4, catalogEntry5));
93

  
94

  
95
    }
96

  
97
    @AfterEach
98
    void tearDown() {
99
        underTest.deleteAll();
100
    }
101

  
102
    @Test
103
    void itShouldReturnAll() {
104
        // given
105
        String name = "";
106
        String country = "";
107
        String type = "";
108

  
109
        // when
110
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
111

  
112
        // then
113
        assertThat(filterResult.size()).isEqualTo(5);
114
    }
115

  
116
    @Test
117
    void testName() {
118
        // given
119
        String name = "first";
120
        String country = "";
121
        String type = "";
122

  
123
        // when
124
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
125

  
126
        // then
127
        assertThat(filterResult.size()).isEqualTo(2);
128
        assertTrue(filterResult.contains(catalogEntry1));
129
        assertTrue(filterResult.contains(catalogEntry4));
130
    }
131

  
132
    @Test
133
    void testWildcardCharacterName() {
134
        // given
135
        String name = "se_ond";
136
        String country = "";
137
        String type = "";
138

  
139
        // when
140
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
141

  
142
        // then
143
        assertThat(filterResult.size()).isEqualTo(3);
144
        assertTrue(filterResult.contains(catalogEntry2));
145
        assertTrue(filterResult.contains(catalogEntry3));
146
        assertTrue(filterResult.contains(catalogEntry5));
147
    }
148

  
149
    @Test
150
    void testWildcardCharactersName() {
151
        // given
152
        String name = "twe%";
153
        String country = "";
154
        String type = "";
155

  
156
        // when
157
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
158

  
159
        // then
160
        assertThat(filterResult.size()).isEqualTo(3);
161
        assertTrue(filterResult.contains(catalogEntry2));
162
        assertTrue(filterResult.contains(catalogEntry3));
163
        assertTrue(filterResult.contains(catalogEntry4));
164
    }
165

  
166
    @Test
167
    void testCountry() {
168
        // given
169
        String name = "";
170
        String country = "aaa";
171
        String type = "";
172

  
173
        // when
174
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
175

  
176
        // then
177
        assertThat(filterResult.size()).isEqualTo(3);
178
        assertTrue(filterResult.contains(catalogEntry1));
179
        assertTrue(filterResult.contains(catalogEntry2));
180
        assertTrue(filterResult.contains(catalogEntry3));
181
    }
182

  
183
    @Test
184
    void testWildcardCharacterCountry() {
185
        // given
186
        String name = "";
187
        String country = "d_d";
188
        String type = "";
189

  
190
        // when
191
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
192

  
193
        // then
194
        assertThat(filterResult.size()).isEqualTo(2);
195
        assertTrue(filterResult.contains(catalogEntry1));
196
        assertTrue(filterResult.contains(catalogEntry4));
197
    }
198

  
199
    @Test
200
    void testWildcardCharactersCountry() {
201
        // given
202
        String name = "";
203
        String country = "b%b";
204
        String type = "";
205

  
206
        // when
207
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
208

  
209
        // then
210
        assertThat(filterResult.size()).isEqualTo(2);
211
        assertTrue(filterResult.contains(catalogEntry2));
212
        assertTrue(filterResult.contains(catalogEntry4));
213
    }
214

  
215

  
216
    @Test
217
    void testType() {
218
        // given
219
        String name = "";
220
        String country = "";
221
        String type = "city";
222

  
223
        // when
224
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
225

  
226
        // then
227
        assertThat(filterResult.size()).isEqualTo(2);
228
        assertTrue(filterResult.contains(catalogEntry1));
229
        assertTrue(filterResult.contains(catalogEntry2));
230
    }
231

  
232
    @Test
233
    void testWildcardCharacterType() {
234
        // given
235
        String name = "";
236
        String country = "";
237
        String type = "countr_";
238

  
239
        // when
240
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
241

  
242
        // then
243
        assertThat(filterResult.size()).isEqualTo(5);
244
        assertTrue(filterResult.contains(catalogEntry1));
245
        assertTrue(filterResult.contains(catalogEntry2));
246
        assertTrue(filterResult.contains(catalogEntry3));
247
        assertTrue(filterResult.contains(catalogEntry4));
248
        assertTrue(filterResult.contains(catalogEntry5));
249
    }
250

  
251
    @Test
252
    void testWildcardCharactersType() {
253
        // given
254
        String name = "";
255
        String country = "";
256
        String type = "%city%";
257

  
258
        // when
259
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
260

  
261
        // then
262
        assertThat(filterResult.size()).isEqualTo(3);
263
        assertTrue(filterResult.contains(catalogEntry1));
264
        assertTrue(filterResult.contains(catalogEntry2));
265
        assertTrue(filterResult.contains(catalogEntry3));
266
    }
267

  
268
    @Test
269
    void testNameAndCountry() {
270
        // given
271
        String name = "third";
272
        String country = "aaa";
273
        String type = "";
274

  
275
        // when
276
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
277

  
278
        // then
279
        assertThat(filterResult.size()).isEqualTo(2);
280
        assertTrue(filterResult.contains(catalogEntry2));
281
        assertTrue(filterResult.contains(catalogEntry3));
282
    }
283

  
284
    @Test
285
    void testNameAndType() {
286
        // given
287
        String name = "third";
288
        String country = "";
289
        String type = "countri";
290

  
291
        // when
292
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
293

  
294
        // then
295
        assertThat(filterResult.size()).isEqualTo(2);
296
        assertTrue(filterResult.contains(catalogEntry3));
297
        assertTrue(filterResult.contains(catalogEntry5));
298
    }
299

  
300
    @Test
301
    void testCountryAndType() {
302
        // given
303
        String name = "";
304
        String country = "ddd";
305
        String type = "country";
306

  
307
        // when
308
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
309

  
310
        // then
311
        assertThat(filterResult.size()).isEqualTo(1);
312
        assertTrue(filterResult.contains(catalogEntry4));
313
    }
314

  
315
    @Test
316
    void testAll() {
317
        // given
318
        String name = "third";
319
        String country = "AAA";
320
        String type = "countri";
321

  
322
        // when
323
        Set<CatalogEntry> filterResult = underTest.filterCatalog(name, country, type);
324

  
325
        // then
326
        assertThat(filterResult.size()).isEqualTo(1);
327
        assertTrue(filterResult.contains(catalogEntry3));
328
    }
329
}
backend/src/test/java/cz/zcu/kiv/backendapi/titlepage/TitlePageServiceImplementationTest.java
1
package cz.zcu.kiv.backendapi.titlepage;
2

  
3
import cz.zcu.kiv.backendapi.type.Type;
4
import org.junit.jupiter.api.BeforeEach;
5
import org.junit.jupiter.api.Test;
6
import org.junit.jupiter.api.extension.ExtendWith;
7
import org.mockito.ArgumentCaptor;
8
import org.mockito.Mock;
9
import org.mockito.junit.jupiter.MockitoExtension;
10

  
11
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
12
import static org.junit.jupiter.api.Assertions.*;
13
import static org.mockito.Mockito.verify;
14

  
15
@ExtendWith(MockitoExtension.class)
16
class TitlePageServiceImplementationTest {
17

  
18
    @Mock
19
    private TitlePageRepository titlePageRepository;
20

  
21
    private TitlePageServiceImplementation underTest;
22

  
23
    @BeforeEach
24
    void setUp() {
25
        underTest = new TitlePageServiceImplementation(titlePageRepository);
26
    }
27

  
28
    @Test
29
    void testAddTitlePage() {
30
        // given
31
        long id = 1;
32
        TitlePage titlePage = new TitlePage();
33
        titlePage.setTitle("title");
34
        titlePage.setContent("content");
35

  
36
        // when
37

  
38
        underTest.addOrUpdateTitlePage(id, titlePage);
39

  
40
        // then
41
        ArgumentCaptor<TitlePage> argumentCaptor = ArgumentCaptor.forClass(TitlePage.class);
42

  
43
        verify(titlePageRepository).save(argumentCaptor.capture());
44

  
45
        TitlePage capturedTitlePage = argumentCaptor.getValue();
46

  
47
        titlePage.setId(id);
48

  
49
        assertThat(capturedTitlePage).isEqualTo(titlePage);
50

  
51
    }
52
}
backend/src/test/java/cz/zcu/kiv/backendapi/type/TypeServiceImplTest.java
1
package cz.zcu.kiv.backendapi.type;
2

  
3
import org.junit.jupiter.api.BeforeEach;
4
import org.junit.jupiter.api.Test;
5
import org.junit.jupiter.api.extension.ExtendWith;
6
import org.mockito.ArgumentCaptor;
7
import org.mockito.Mock;
8
import org.mockito.junit.jupiter.MockitoExtension;
9

  
10

  
11
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
12
import static org.mockito.Mockito.verify;
13

  
14
@ExtendWith(MockitoExtension.class)
15
class TypeServiceImplTest {
16

  
17
    @Mock
18
    private TypeRepository typeRepository;
19

  
20
    private TypeServiceImpl underTest;
21

  
22
    @BeforeEach
23
    void setUp() {
24
        underTest = new TypeServiceImpl(typeRepository);
25
    }
26

  
27
    @Test
28
    void saveType() {
29
        // when
30
        Type type = new Type("type");
31
        underTest.saveType(type);
32

  
33
        // then
34
        ArgumentCaptor<Type> argumentCaptor = ArgumentCaptor.forClass(Type.class);
35

  
36
        verify(typeRepository).save(argumentCaptor.capture());
37

  
38
        Type capturedType = argumentCaptor.getValue();
39

  
40
        assertThat(capturedType).isEqualTo(type);
41
    }
42

  
43
    @Test
44
    void getTypeByName() {
45
        // when
46
        String typeName = "type";
47
        underTest.getTypeByName(typeName);
48

  
49
        // then
50
        verify(typeRepository).findById(typeName);
51
    }
52
}
backend/src/test/java/cz/zcu/kiv/backendapi/user/UserRepositoryTest.java
1
package cz.zcu.kiv.backendapi.user;
2

  
3
import org.junit.jupiter.api.AfterEach;
4
import org.junit.jupiter.api.Test;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
7

  
8
import java.util.Optional;
9

  
10
import static org.junit.jupiter.api.Assertions.assertTrue;
11

  
12
@DataJpaTest
13
class UserRepositoryTest {
14

  
15
    @Autowired
16
    private UserRepository underTest;
17

  
18
    @AfterEach
19
    void tearDown() {
20
        underTest.deleteAll();
21
    }
22

  
23
    @Test
24
    void itShouldFindByEmail() {
25
        // given
26
        String email = "user@user.com";
27
        UserEntity userEntity = new UserEntity("User", email, "password", (byte) 0, false);
28
        underTest.save(userEntity);
29

  
30
        // when
31
        Optional<UserEntity> userFoundByEmail = underTest.findByEmail(email);
32

  
33
        // then
34
        assertTrue(userFoundByEmail.isPresent());
35
    }
36

  
37
    @Test
38
    void itShouldNotFindByEmail() {
39
        // given
40
        String email = "user@user.com";
41

  
42
        // when
43
        Optional<UserEntity> userFoundByEmail = underTest.findByEmail(email);
44

  
45
        // then
46
        assertTrue(userFoundByEmail.isEmpty());
47
    }
48
}
backend/src/test/resources/application.properties
1
spring.datasource.url=jdbc:h2://mem:db;DB_CLOSE_DELAY=-1
2
spring.datasource.username=sa
3
spring.datasource.password=sa
4
spring.datasource.driver-class-name=org.h2.Driver
5

  
6
spring.jpa.hibernate.ddl-auto=create-drop
7
spring.jpa.generate-ddl=true
8
spring.jpa.open-in-view=false
9
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL10Dialect
10
spring.jpa.properties.hibernate.format_sql=true
11
spring.jpa.show-sql=true
12

  

Také k dispozici: Unified diff