Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0598e57d

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

Added batch endpoint for saving multiple catalog items, POST and PUT endpoint for catalog items merged

re #9753

Zobrazit rozdíly:

backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogController.java
23 23
    private final ICatalogItemService catalogService;
24 24

  
25 25
    /**
26
     * Saves new catalog item
26
     * Creates new or updates catalog item
27 27
     *
28 28
     * @param catalogItemDto catalog item DTO
29 29
     */
30 30
    @PostMapping("")
31
    @Operation(summary = "creates new catalog item")
32
    public void addCatalogItem(@RequestBody CatalogItemDto catalogItemDto) {
31
    @Operation(summary = "creates new or updates catalog item")
32
    public void saveCatalogItem(@RequestBody CatalogItemDto catalogItemDto) {
33 33
        catalogService.saveCatalogItem(catalogItemDto);
34 34
    }
35 35

  
36
    /**
37
     * Saves list of catalog items
38
     *
39
     * @param catalogItemDtoList list of catalog items DTO
40
     */
41
    @PostMapping("/batch")
42
    @Operation(summary = "saves list of catalog items")
43
    public void saveCatalogItems(@RequestBody List<CatalogItemDto> catalogItemDtoList) {
44
        catalogService.saveCatalogItems(catalogItemDtoList);
45
    }
46

  
36 47
    /**
37 48
     * Returns catalog items satisfying given filter
38 49
     *
......
47 58
        return new ResponseEntity<>(catalogService.getCatalog(name, country, type, writtenForm), HttpStatus.OK);
48 59
    }
49 60

  
50
    /**
51
     * Updates catalog item with given ID
52
     *
53
     * @param id             ID
54
     * @param catalogItemDto catalog item DTO
55
     */
56
    @PutMapping("/{id}")
57
    @Operation(summary = "updates catalog item with given ID")
58
    public void updateCatalogItem(@PathVariable UUID id, @RequestBody CatalogItemDto catalogItemDto) {
59
        catalogService.updateCatalogItem(id, catalogItemDto);
60
    }
61 61

  
62 62
    /**
63 63
     * Deletes catalog item with given ID
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemDto.java
1 1
package cz.zcu.kiv.backendapi.catalog;
2 2

  
3
import com.fasterxml.jackson.annotation.JsonProperty;
4 3
import lombok.AllArgsConstructor;
5 4
import lombok.Data;
6 5
import lombok.NoArgsConstructor;
......
18 17
    /**
19 18
     * Id
20 19
     */
21
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
22 20
    private UUID id;
23 21

  
24 22
    /**
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImpl.java
87 87
    private final ITypeService typeService;
88 88

  
89 89
    @Override
90
    public void saveCatalog(List<CatalogItem> catalogItems) {
90
    public void saveCatalogItems(List<CatalogItemDto> catalogItemDtoList) {
91 91
        log.info("Saving catalog");
92
        catalogItems.forEach(this::saveCatalogEntity);
92
        catalogItemDtoList.forEach(this::saveCatalogItem);
93
        log.info("Catalog saved");
93 94
    }
94 95

  
95 96
    @Override
96 97
    public void saveCatalogItem(CatalogItemDto catalogItemDto) {
97 98
        log.info("Saving catalog item");
98
        CatalogItem catalogItem = new CatalogItem();
99
        convertDtoToEntity(catalogItemDto, catalogItem);
100
        saveCatalogEntity(catalogItem);
101
    }
102

  
103
    @Override
104
    public void updateCatalogItem(UUID id, CatalogItemDto catalogItemDto) {
105
        CatalogItem catalogItem = catalogItemRepository.findById(id).orElseThrow(() -> {
106
            log.error(CATALOG_ITEM_NOT_FOUND);
107
            throw new ApiRequestException(CATALOG_ITEM_NOT_FOUND, HttpStatus.NOT_FOUND);
108
        });
99
        CatalogItem catalogItem;
100
        Optional<CatalogItem> optionalCatalogItem;
101
        if (catalogItemDto.getId() != null && (optionalCatalogItem = catalogItemRepository.findById(catalogItemDto.getId())).isPresent()) {
102
            catalogItem = optionalCatalogItem.get();
103
        } else {
104
            catalogItem = new CatalogItem();
105
        }
109 106
        convertDtoToEntity(catalogItemDto, catalogItem);
110 107
        saveCatalogEntity(catalogItem);
111
        log.info("Catalog item updated");
108
        log.info("Catalog item saved");
112 109
    }
113 110

  
114 111
    @Override
......
133 130

  
134 131
    @Override
135 132
    public PathDto getPath(String text) {
133
        log.info("Searching path");
136 134
        PathDto pathDto = new PathDto();
137 135
        StringBuilder highlightedText = new StringBuilder();
138 136
        List<List<CatalogItemDto>> foundCatalogItems = new ArrayList<>();
......
186 184

  
187 185
        pathDto.setText(highlightedText.toString());
188 186
        pathDto.setFoundCatalogItems(foundCatalogItems);
187
        log.info("Path searched");
189 188
        return pathDto;
190 189
    }
191 190

  
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/ICatalogItemService.java
10 10
 */
11 11
public interface ICatalogItemService {
12 12
    /**
13
     * Saves list of catalog items
13
     * Saves list of catalog items DTO
14 14
     *
15
     * @param catalogItems catalog items
15
     * @param catalogItemDtoList list of catalog items DTO
16 16
     */
17
    void saveCatalog(List<CatalogItem> catalogItems);
17
    void saveCatalogItems(List<CatalogItemDto> catalogItemDtoList);
18 18

  
19 19
    /**
20
     * Saves one catalog item
20
     * Saves one catalog item (creates new if ID is null or does not exist, updates catalog item otherwise)
21 21
     *
22 22
     * @param catalogItemDto catalog item DTO
23 23
     */
24 24
    void saveCatalogItem(CatalogItemDto catalogItemDto);
25 25

  
26
    /**
27
     * Updates catalog item with given ID
28
     *
29
     * @param id             ID
30
     * @param catalogItemDto catalog item DTO
31
     */
32
    void updateCatalogItem(UUID id, CatalogItemDto catalogItemDto);
33

  
34 26
    /**
35 27
     * Deletes catalog item with given ID
36 28
     *
backend/src/main/java/cz/zcu/kiv/backendapi/config/CorsConfig.java
1 1
package cz.zcu.kiv.backendapi.config;
2 2

  
3 3
import org.springframework.context.annotation.Configuration;
4
import org.springframework.http.HttpMethod;
5 4
import org.springframework.web.servlet.config.annotation.CorsRegistry;
6 5
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7 6
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
backend/src/main/java/cz/zcu/kiv/backendapi/security/SecurityConfig.java
21 21
import org.springframework.web.cors.CorsConfigurationSource;
22 22
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
23 23

  
24
import java.util.Arrays;
25
import java.util.Collections;
26 24
import java.util.HashMap;
25
import java.util.List;
27 26
import java.util.Map;
28 27

  
28

  
29 29
/**
30 30
 * Security config class
31 31
 */
......
65 65
        PERMITTED_ENDPOINTS.put("/catalog-items", HttpMethod.GET);
66 66
        PERMITTED_ENDPOINTS.put("/catalog-items/**", HttpMethod.GET);
67 67
        PERMITTED_ENDPOINTS.put("/title-page", HttpMethod.GET);
68
        PERMITTED_ENDPOINTS.put("/sources", HttpMethod.GET);
68 69
    }
69 70

  
70 71
    /**
......
73 74
     * @param http http security
74 75
     * @throws Exception exception
75 76
     */
76
    // TODO configure and check rights
77 77
    @Override
78 78
    protected void configure(HttpSecurity http) throws Exception {
79 79
        http.csrf().disable()
......
86 86
                .authorizeRequests()
87 87
                .antMatchers(HttpMethod.GET, PERMITTED_ENDPOINTS.keySet().stream().filter(k -> PERMITTED_ENDPOINTS.get(k).equals(HttpMethod.GET)).toArray(String[]::new)).permitAll()
88 88
                .antMatchers(HttpMethod.POST, "/login").permitAll()
89
                .antMatchers("/external-catalog-items").hasRole(Role.ADMIN.name())
89
                .antMatchers(HttpMethod.POST, "/users", "/external-catalog-items").hasRole(Role.ADMIN.name())
90 90
                .antMatchers(HttpMethod.PATCH, "/users/*/permissions", "/users/*/password").hasRole(Role.ADMIN.name())
91 91
                .antMatchers(HttpMethod.DELETE, "/users/**").hasRole(Role.ADMIN.name())
92 92
                .antMatchers(HttpMethod.GET, "/users").hasRole(Role.ADMIN.name())
93
                .antMatchers(HttpMethod.GET, "/path").hasAuthority(Permission.READ.name())
94
                .antMatchers(HttpMethod.POST, "/catalog-items").hasAuthority(Permission.WRITE.name())
95
                .antMatchers(HttpMethod.PUT, "/catalog-items/*").hasAuthority(Permission.WRITE.name())
93
                .antMatchers(HttpMethod.GET, "/path", "/external-catalog-items").hasAuthority(Permission.READ.name())
94
                .antMatchers(HttpMethod.POST, "/catalog-items", "/catalog-items/batch").hasAuthority(Permission.WRITE.name())
96 95
                .antMatchers(HttpMethod.DELETE, "/catalog-items/*").hasAuthority(Permission.DELETE.name())
97
                .antMatchers(HttpMethod.POST, "/title-page").hasRole(Role.ADMIN.name())
96
                .antMatchers(HttpMethod.POST, "/title-page", "/sources").hasRole(Role.ADMIN.name())
98 97
                .anyRequest()
99 98
                .authenticated();
100 99
    }
......
117 116
     */
118 117
    @Bean
119 118
    public DaoAuthenticationProvider authenticationProvider() {
120
        final DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
119
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
121 120
        provider.setUserDetailsService(userDetailsService);
122 121
        provider.setPasswordEncoder(bCryptPasswordEncoder);
123 122
        return provider;
......
125 124

  
126 125
    @Bean
127 126
    CorsConfigurationSource corsConfigurationSource() {
128
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
127
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
129 128
        CorsConfiguration corsConfiguration = new CorsConfiguration().applyPermitDefaultValues();
130
        corsConfiguration.setAllowedMethods(java.util.List.of(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name(), HttpMethod.PUT.name()));
129
        corsConfiguration.setAllowedMethods(List.of(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name(),
130
                HttpMethod.DELETE.name(), HttpMethod.PATCH.name(), HttpMethod.PUT.name()));
131 131
        source.registerCorsConfiguration("/**", corsConfiguration);
132 132
        return source;
133 133
    }
backend/src/main/java/cz/zcu/kiv/backendapi/user/UserServiceImpl.java
66 66

  
67 67
    @Override
68 68
    public void registerNewUser(UserDto userDto) {
69
        log.info("Registering new user");
69 70
        if (userRepository.findByEmail(userDto.getEmail()).isPresent()) {
70 71
            log.error("Trying to register new user with username that is already taken: " + userDto.getEmail());
71 72
            throw new ApiRequestException(String.format("User with username %s already exists", userDto.getEmail()), HttpStatus.CONFLICT);
......
73 74
        UserEntity userEntity = new UserEntity();
74 75
        convertDtoToEntity(userDto, userEntity);
75 76
        userRepository.save(userEntity);
77
        log.info("New user registered");
76 78
    }
77 79

  
78 80
    @Override
79 81
    public void updatePermissions(String username, PermissionDto permissionDto) {
82
        log.info("Updating permissions to user: " + username);
80 83
        UserEntity userEntity = userRepository.findByEmail(username).orElseThrow(() -> {
81 84
            log.error(String.format(USER_NOT_FOUND, username));
82 85
            throw new UsernameNotFoundException(String.format(USER_NOT_FOUND, username));
......
87 90
        }
88 91
        userEntity.setPermissions(getPermissionsFromDto(permissionDto));
89 92
        userRepository.save(userEntity);
93
        log.info("Permissions to user " + username + " updated");
90 94
    }
91 95

  
92 96
    @Override
93 97
    public void resetPassword(String username, String newPassword) {
98
        log.info("Resetting password to user " + username);
94 99
        if (username.equals(SUPER_ADMIN_NAME)) {
95 100
            log.error("Password for SUPER ADMIN can not be changed");
96 101
            throw new ApiRequestException("Password for SUPER ADMIN can not be changed", HttpStatus.BAD_REQUEST);
......
101 106
        });
102 107
        userEntity.setPassword(bCryptPasswordEncoder.encode(newPassword));
103 108
        userRepository.save(userEntity);
109
        log.info("Password to user " + username + " reset");
104 110
    }
105 111

  
106 112
    @Override
107 113
    public void deleteUser(String username) {
114
        log.info("Deleting user " + username);
108 115
        UserEntity userEntity = userRepository.findByEmail(username).orElseThrow(() -> {
109 116
            log.error(String.format(USER_NOT_FOUND, username));
110 117
            throw new UsernameNotFoundException(String.format(USER_NOT_FOUND, username));
......
114 121
            throw new ApiRequestException("User with ADMIN rights can not be deleted", HttpStatus.BAD_REQUEST);
115 122
        }
116 123
        userRepository.delete(userEntity);
124
        log.info("User " + username + " deleted");
117 125
    }
118 126

  
119 127
    @Override
......
123 131

  
124 132
    @Override
125 133
    public void changePassword(String oldPassword, String newPassword) {
134
        log.info("Changing password to logged user");
126 135
        UserEntity loggedUser = getUserByName((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
127 136
        if (loggedUser.getEmail().equals(SUPER_ADMIN_NAME)) {
128 137
            throw new ApiRequestException("Can not change password for SUPER ADMIN", HttpStatus.BAD_REQUEST);
......
132 141
        }
133 142
        loggedUser.setPassword(bCryptPasswordEncoder.encode(newPassword));
134 143
        userRepository.save(loggedUser);
144
        log.info("Password to logged user changed");
135 145
    }
136 146

  
137 147

  
backend/src/test/java/cz/zcu/kiv/backendapi/catalog/CatalogItemServiceImplTest.java
59 59
        catalogItem.setLatitude(0.1);
60 60
        catalogItem.setLongitude(0.2);
61 61

  
62
        CatalogItemDto catalogItemDto = new CatalogItemDto();
63
        catalogItemDto.setId(catalogItem.getId());
64
        catalogItemDto.setName("aaacbbbbbbbaa");
65
        catalogItemDto.setBibliography(Stream.of("bibl").collect(Collectors.toCollection(LinkedHashSet::new)));
66
        catalogItemDto.setTypes(Stream.of(type.getType()).collect(Collectors.toCollection(LinkedHashSet::new)));
67
        catalogItemDto.setAllNames(Stream.of("altName").collect(Collectors.toCollection(LinkedHashSet::new)));
68
        catalogItemDto.setCountries(Stream.of("aaaabbaa").collect(Collectors.toCollection(LinkedHashSet::new)));
69
        catalogItemDto.setWrittenForms(Stream.of("written").collect(Collectors.toCollection(LinkedHashSet::new)));
70
        catalogItemDto.setCertainty(0);
71
        catalogItemDto.setLatitude(0.1);
72
        catalogItemDto.setLongitude(0.2);
73

  
62 74
        CatalogItem catalogItem2 = new CatalogItem();
63 75
        catalogItem2.setName("name");
64 76
        catalogItem2.setBibliography(Stream.of(new Bibliography("bibl", catalogItem2, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
......
70 82
        catalogItem2.setLatitude(1.1);
71 83
        catalogItem2.setLongitude(1.2);
72 84

  
73
        List<CatalogItem> catalog = List.of(catalogItem, catalogItem2);
85
        CatalogItemDto catalogItemDto2 = new CatalogItemDto();
86
        catalogItemDto2.setId(catalogItem2.getId());
87
        catalogItemDto2.setName("name");
88
        catalogItemDto2.setBibliography(Stream.of("bibl").collect(Collectors.toCollection(LinkedHashSet::new)));
89
        catalogItemDto2.setTypes(Stream.of(type.getType()).collect(Collectors.toCollection(LinkedHashSet::new)));
90
        catalogItemDto2.setAllNames(Stream.of("aaaabbbbbbaa").collect(Collectors.toCollection(LinkedHashSet::new)));
91
        catalogItemDto2.setCountries(Stream.of("aaaabbcccefaa").collect(Collectors.toCollection(LinkedHashSet::new)));
92
        catalogItemDto2.setWrittenForms(Stream.of("written").collect(Collectors.toCollection(LinkedHashSet::new)));
93
        catalogItemDto2.setCertainty(1);
94
        catalogItemDto2.setLatitude(1.1);
95
        catalogItemDto2.setLongitude(1.2);
96

  
97
        List<CatalogItemDto> catalog = List.of(catalogItemDto, catalogItemDto2);
74 98

  
75 99
        given(typeService.getTypeByName(anyString())).willReturn(Optional.of(type));
100
        given(catalogItemRepository.findById(catalogItemDto.getId())).willReturn(Optional.of(catalogItem));
101
        given(catalogItemRepository.findById(catalogItemDto2.getId())).willReturn(Optional.of(catalogItem2));
76 102

  
77 103
        // when
78
        underTest.saveCatalog(catalog);
104
        underTest.saveCatalogItems(catalog);
79 105

  
80 106
        // then
81 107
        verify(typeService, never()).saveType(type);
......
125 151

  
126 152
        CatalogItem capturedItem = argumentCaptor.getValue();
127 153

  
154
        assertThat(capturedItem.getId()).isNotEqualTo(catalogItemDto.getId());
128 155
        assertThat(capturedItem.getName()).isEqualTo(catalogItem.getName());
129 156
        assertThat(capturedItem.getLongitude()).isEqualTo(catalogItem.getLongitude());
130 157
        assertThat(capturedItem.getLatitude()).isEqualTo(catalogItem.getLatitude());
......
139 166
    }
140 167

  
141 168
    @Test
142
    void testCanGetCatalogItem() {
169
    void testSaveCatalogItemIdNotPresent() {
143 170
        // given
144
        UUID id = UUID.randomUUID();
145
        CatalogItem catalogItem = new CatalogItem();
146
        catalogItem.setId(id);
147 171
        CatalogItemDto catalogItemDto = new CatalogItemDto();
148
        catalogItemDto.setId(id);
149
        given(catalogItemRepository.findById(id)).willReturn(Optional.of(catalogItem));
172
        catalogItemDto.setId(UUID.randomUUID());
173
        catalogItemDto.setName("name");
174
        catalogItemDto.setBibliography(Stream.of("bibl").collect(Collectors.toCollection(LinkedHashSet::new)));
175
        catalogItemDto.setTypes(Stream.of("type").collect(Collectors.toCollection(LinkedHashSet::new)));
176
        catalogItemDto.setAllNames(Stream.of("altName").collect(Collectors.toCollection(LinkedHashSet::new)));
177
        catalogItemDto.setCountries(Stream.of("country").collect(Collectors.toCollection(LinkedHashSet::new)));
178
        catalogItemDto.setWrittenForms(Stream.of("written").collect(Collectors.toCollection(LinkedHashSet::new)));
179
        catalogItemDto.setCertainty(0);
180
        catalogItemDto.setLatitude(0.1);
181
        catalogItemDto.setLongitude(0.2);
182
        catalogItemDto.setDescription("description");
150 183

  
151
        // when
152
        CatalogItemDto retrievedItemDto = underTest.getCatalogItem(id);
184
        Type type = new Type("type");
153 185

  
154
        // then
155
        verify(catalogItemRepository).findById(id);
156
        assertThat(retrievedItemDto).isEqualTo(catalogItemDto);
157
    }
186
        CatalogItem catalogItem = new CatalogItem();
187
        catalogItem.setName("name");
188
        catalogItem.setBibliography(Stream.of(new Bibliography("bibl", catalogItem, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
189
        catalogItem.setTypes(Stream.of(type).collect(Collectors.toCollection(LinkedHashSet::new)));
190
        catalogItem.setAllNames(Stream.of(new CatalogItemName("altName", catalogItem, 1), new CatalogItemName("name", catalogItem, 2)).collect(Collectors.toCollection(LinkedHashSet::new)));
191
        catalogItem.setCountries(Stream.of(new Country("country", catalogItem, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
192
        catalogItem.setWrittenForms(Stream.of(new WrittenForm("written", catalogItem, 1)).collect(Collectors.toCollection(LinkedHashSet::new)));
193
        catalogItem.setCertainty(0);
194
        catalogItem.setLatitude(0.1);
195
        catalogItem.setLongitude(0.2);
196
        catalogItem.setDescription("description");
158 197

  
159
    @Test
160
    void testCanNotGetCatalogItem() {
161
        // given
162
        UUID id = UUID.randomUUID();
198
        given(typeService.getTypeByName(anyString())).willReturn(Optional.of(type));
199
        given(catalogItemRepository.findById(catalogItemDto.getId())).willReturn(Optional.empty());
163 200

  
164 201
        // when
202
        underTest.saveCatalogItem(catalogItemDto);
203

  
165 204
        // then
166
        assertThatThrownBy(() -> underTest.getCatalogItem(id))
167
                .isInstanceOf(ApiRequestException.class)
168
                .hasMessageContaining("Catalog item not found");
205
        ArgumentCaptor<CatalogItem> argumentCaptor = ArgumentCaptor.forClass(CatalogItem.class);
206

  
207
        verify(catalogItemRepository).save(argumentCaptor.capture());
208
        verify(typeService, never()).saveType(type);
209

  
210
        CatalogItem capturedItem = argumentCaptor.getValue();
211

  
212
        assertThat(capturedItem.getId()).isNotEqualTo(catalogItemDto.getId());
213
        assertThat(capturedItem.getName()).isEqualTo(catalogItem.getName());
214
        assertThat(capturedItem.getLongitude()).isEqualTo(catalogItem.getLongitude());
215
        assertThat(capturedItem.getLatitude()).isEqualTo(catalogItem.getLatitude());
216
        assertThat(capturedItem.getCertainty()).isEqualTo(catalogItem.getCertainty());
217
        assertThat(capturedItem.getAllNames().size()).isEqualTo(catalogItem.getAllNames().size());
218
        assertThat(capturedItem.getBibliography().size()).isEqualTo(catalogItem.getBibliography().size());
219
        assertThat(capturedItem.getCountries().size()).isEqualTo(catalogItem.getCountries().size());
220
        assertThat(capturedItem.getWrittenForms().size()).isEqualTo(catalogItem.getWrittenForms().size());
221
        assertThat(capturedItem.getTypes().size()).isEqualTo(catalogItem.getTypes().size());
222
        assertThat(capturedItem.getDescription()).isEqualTo(catalogItem.getDescription());
169 223

  
170
        verify(catalogItemRepository).findById(id);
171 224
    }
172 225

  
173 226
    @Test
174
    void testCanUpdateCatalogItem() {
227
    void testSaveCatalogItemIdPresent() {
175 228
        // given
176 229
        UUID id = UUID.randomUUID();
177 230

  
178 231
        CatalogItemDto catalogItemDto = new CatalogItemDto();
232
        catalogItemDto.setId(id);
179 233
        catalogItemDto.setName("name");
180 234
        catalogItemDto.setBibliography(Stream.of("bibl").collect(Collectors.toCollection(LinkedHashSet::new)));
181 235
        catalogItemDto.setTypes(Stream.of("type").collect(Collectors.toCollection(LinkedHashSet::new)));
......
220 274

  
221 275
        // when
222 276

  
223
        underTest.updateCatalogItem(id, catalogItemDto);
277
        underTest.saveCatalogItem(catalogItemDto);
224 278

  
225 279
        // then
226 280
        verify(typeService).saveType(type);
......
245 299
        assertThat(capturedCatalogItem.getWrittenForms().stream().map(WrittenForm::getForm).collect(Collectors.toSet())).isEqualTo(catalogItem.getWrittenForms().stream().map(WrittenForm::getForm).collect(Collectors.toCollection(LinkedHashSet::new)));
246 300
    }
247 301

  
302

  
303

  
248 304
    @Test
249
    void testCanNotUpdateCatalogItem() {
305
    void testCanGetCatalogItem() {
250 306
        // given
251 307
        UUID id = UUID.randomUUID();
252

  
308
        CatalogItem catalogItem = new CatalogItem();
309
        catalogItem.setId(id);
253 310
        CatalogItemDto catalogItemDto = new CatalogItemDto();
254
        catalogItemDto.setName("name");
255
        catalogItemDto.setBibliography(Stream.of("bibl").collect(Collectors.toCollection(LinkedHashSet::new)));
256
        catalogItemDto.setTypes(Stream.of("type").collect(Collectors.toCollection(LinkedHashSet::new)));
257
        catalogItemDto.setAllNames(Stream.of("altName").collect(Collectors.toCollection(LinkedHashSet::new)));
258
        catalogItemDto.setCountries(Stream.of("country").collect(Collectors.toCollection(LinkedHashSet::new)));
259
        catalogItemDto.setWrittenForms(Stream.of("written").collect(Collectors.toCollection(LinkedHashSet::new)));
260
        catalogItemDto.setCertainty(0);
261
        catalogItemDto.setLatitude(0.1);
262
        catalogItemDto.setLongitude(0.2);
311
        catalogItemDto.setId(id);
312
        given(catalogItemRepository.findById(id)).willReturn(Optional.of(catalogItem));
313

  
314
        // when
315
        CatalogItemDto retrievedItemDto = underTest.getCatalogItem(id);
316

  
317
        // then
318
        verify(catalogItemRepository).findById(id);
319
        assertThat(retrievedItemDto).isEqualTo(catalogItemDto);
320
    }
321

  
322
    @Test
323
    void testCanNotGetCatalogItem() {
324
        // given
325
        UUID id = UUID.randomUUID();
263 326

  
264 327
        // when
265 328
        // then
266
        assertThatThrownBy(() -> underTest.updateCatalogItem(id, catalogItemDto))
329
        assertThatThrownBy(() -> underTest.getCatalogItem(id))
267 330
                .isInstanceOf(ApiRequestException.class)
268 331
                .hasMessageContaining("Catalog item not found");
269 332

  
270
        verify(catalogItemRepository, never()).save(any());
271
        verify(typeService, never()).saveType(any());
333
        verify(catalogItemRepository).findById(id);
272 334
    }
273 335

  
274 336
    @Test

Také k dispozici: Unified diff