Revize bf502b70
Přidáno uživatelem Jakub Šmíd před téměř 3 roky(ů)
backend/src/main/java/cz/zcu/kiv/backendapi/config/DataInitiator.java | ||
---|---|---|
3 | 3 |
import cz.zcu.kiv.backendapi.catalog.CatalogItem; |
4 | 4 |
import cz.zcu.kiv.backendapi.catalog.CatalogItemRepository; |
5 | 5 |
import cz.zcu.kiv.backendapi.catalog.ICatalogItemService; |
6 |
import cz.zcu.kiv.backendapi.sources.ISourcesService; |
|
7 |
import cz.zcu.kiv.backendapi.sources.Sources; |
|
6 | 8 |
import cz.zcu.kiv.backendapi.titlepage.ITitlePageService; |
7 | 9 |
import cz.zcu.kiv.backendapi.titlepage.TitlePage; |
8 | 10 |
import cz.zcu.kiv.backendapi.type.ITypeService; |
... | ... | |
41 | 43 |
private final UserRepository userRepository; |
42 | 44 |
|
43 | 45 |
private final ITitlePageService titlePageService; |
46 |
private final ISourcesService sourcesService; |
|
44 | 47 |
|
45 | 48 |
private final BCryptPasswordEncoder encoder; |
46 | 49 |
|
... | ... | |
54 | 57 |
List<CatalogItem> catalog = loadCatalog(); |
55 | 58 |
catalogService.saveCatalog(catalog); |
56 | 59 |
|
57 |
titlePageService.updateTitlePage(new TitlePage(1L, "", "")); |
|
60 |
titlePageService.updateTitlePage(new TitlePage(1L, "")); |
|
61 |
sourcesService.updateSources(new Sources(1L, "")); |
|
58 | 62 |
|
59 | 63 |
UserEntity user1 = new UserEntity("admin", "admin", encoder.encode("password"), (byte) 7, true); |
60 | 64 |
userRepository.save(user1); |
backend/src/main/java/cz/zcu/kiv/backendapi/security/SecurityConfig.java | ||
---|---|---|
63 | 63 |
PERMITTED_ENDPOINTS.put("/catalog-items", HttpMethod.GET); |
64 | 64 |
PERMITTED_ENDPOINTS.put("/catalog-items/**", HttpMethod.GET); |
65 | 65 |
PERMITTED_ENDPOINTS.put("/title-page", HttpMethod.GET); |
66 |
PERMITTED_ENDPOINTS.put("/sources", HttpMethod.GET); |
|
66 | 67 |
} |
67 | 68 |
|
68 | 69 |
/** |
... | ... | |
92 | 93 |
.antMatchers(HttpMethod.POST, "/catalog-items").hasAuthority(Permission.WRITE.name()) |
93 | 94 |
.antMatchers(HttpMethod.PUT, "/catalog-items/*").hasAuthority(Permission.WRITE.name()) |
94 | 95 |
.antMatchers(HttpMethod.DELETE, "/catalog-items/*").hasAuthority(Permission.DELETE.name()) |
95 |
.antMatchers(HttpMethod.POST, "/title-page").hasRole(Role.ADMIN.name()) |
|
96 |
.antMatchers(HttpMethod.POST, "/title-page", "/sources").hasRole(Role.ADMIN.name())
|
|
96 | 97 |
.anyRequest() |
97 | 98 |
.authenticated(); |
98 | 99 |
} |
backend/src/main/java/cz/zcu/kiv/backendapi/sources/ISourcesService.java | ||
---|---|---|
1 |
package cz.zcu.kiv.backendapi.sources; |
|
2 |
|
|
3 |
/** |
|
4 |
* Sources service interface |
|
5 |
*/ |
|
6 |
public interface ISourcesService { |
|
7 |
/** |
|
8 |
* Returns sources |
|
9 |
* |
|
10 |
* @return sources |
|
11 |
*/ |
|
12 |
Sources getSources(); |
|
13 |
|
|
14 |
/** |
|
15 |
* Adds or updates sources |
|
16 |
* |
|
17 |
* @param sources sources |
|
18 |
*/ |
|
19 |
void updateSources(Sources sources); |
|
20 |
|
|
21 |
} |
backend/src/main/java/cz/zcu/kiv/backendapi/sources/Sources.java | ||
---|---|---|
1 |
package cz.zcu.kiv.backendapi.sources; |
|
2 |
|
|
3 |
import com.fasterxml.jackson.annotation.JsonIgnore; |
|
4 |
import io.swagger.v3.oas.annotations.media.Schema; |
|
5 |
import lombok.AllArgsConstructor; |
|
6 |
import lombok.Data; |
|
7 |
import lombok.NoArgsConstructor; |
|
8 |
|
|
9 |
import javax.persistence.Column; |
|
10 |
import javax.persistence.Entity; |
|
11 |
import javax.persistence.Id; |
|
12 |
import javax.persistence.Table; |
|
13 |
|
|
14 |
/** |
|
15 |
* Class representing sources |
|
16 |
*/ |
|
17 |
@Data |
|
18 |
@NoArgsConstructor |
|
19 |
@AllArgsConstructor |
|
20 |
@Entity |
|
21 |
@Table(name = "sources") |
|
22 |
@Schema(name = "SourcesDto") |
|
23 |
public class Sources { |
|
24 |
/** |
|
25 |
* ID |
|
26 |
*/ |
|
27 |
@Id |
|
28 |
@JsonIgnore |
|
29 |
private Long id; |
|
30 |
|
|
31 |
/** |
|
32 |
* Sources |
|
33 |
*/ |
|
34 |
@Column(length = 100000) |
|
35 |
private String sources; |
|
36 |
} |
backend/src/main/java/cz/zcu/kiv/backendapi/sources/SourcesController.java | ||
---|---|---|
1 |
package cz.zcu.kiv.backendapi.sources; |
|
2 |
|
|
3 |
import io.swagger.v3.oas.annotations.Operation; |
|
4 |
import lombok.RequiredArgsConstructor; |
|
5 |
import org.springframework.http.HttpStatus; |
|
6 |
import org.springframework.http.ResponseEntity; |
|
7 |
import org.springframework.web.bind.annotation.*; |
|
8 |
|
|
9 |
/** |
|
10 |
* Controller for sources |
|
11 |
*/ |
|
12 |
@RequiredArgsConstructor |
|
13 |
@RestController |
|
14 |
@RequestMapping("/sources") |
|
15 |
public class SourcesController { |
|
16 |
/** |
|
17 |
* Sources service |
|
18 |
*/ |
|
19 |
private final ISourcesService sourcesService; |
|
20 |
|
|
21 |
/** |
|
22 |
* Returns sources |
|
23 |
* |
|
24 |
* @return sources |
|
25 |
*/ |
|
26 |
@GetMapping("") |
|
27 |
@Operation(summary = "returns sources") |
|
28 |
public ResponseEntity<Sources> getTitlePage() { |
|
29 |
return new ResponseEntity<>(sourcesService.getSources(), HttpStatus.OK); |
|
30 |
} |
|
31 |
|
|
32 |
/** |
|
33 |
* Updates sources |
|
34 |
* |
|
35 |
* @param sources sources |
|
36 |
*/ |
|
37 |
@PostMapping("") |
|
38 |
@Operation(summary = "updates/creates sources") |
|
39 |
public void updateTitlePage(@RequestBody Sources sources) { |
|
40 |
sourcesService.updateSources(sources); |
|
41 |
} |
|
42 |
|
|
43 |
} |
backend/src/main/java/cz/zcu/kiv/backendapi/sources/SourcesRepository.java | ||
---|---|---|
1 |
package cz.zcu.kiv.backendapi.sources; |
|
2 |
|
|
3 |
import org.springframework.data.jpa.repository.JpaRepository; |
|
4 |
import org.springframework.stereotype.Repository; |
|
5 |
|
|
6 |
/** |
|
7 |
* Sources repository |
|
8 |
*/ |
|
9 |
@Repository |
|
10 |
public interface SourcesRepository extends JpaRepository<Sources, Long> { |
|
11 |
} |
backend/src/main/java/cz/zcu/kiv/backendapi/sources/SourcesServiceImplementation.java | ||
---|---|---|
1 |
package cz.zcu.kiv.backendapi.sources; |
|
2 |
|
|
3 |
import lombok.RequiredArgsConstructor; |
|
4 |
import lombok.extern.slf4j.Slf4j; |
|
5 |
import org.springframework.stereotype.Service; |
|
6 |
import org.springframework.transaction.annotation.Transactional; |
|
7 |
|
|
8 |
import java.util.Optional; |
|
9 |
|
|
10 |
/** |
|
11 |
* Sources service implementation |
|
12 |
*/ |
|
13 |
@Service |
|
14 |
@Transactional |
|
15 |
@RequiredArgsConstructor |
|
16 |
@Slf4j |
|
17 |
public class SourcesServiceImplementation implements ISourcesService { |
|
18 |
/** |
|
19 |
* ID of the sources |
|
20 |
*/ |
|
21 |
private static final long SOURCES_ID = 1; |
|
22 |
|
|
23 |
/** |
|
24 |
* Sources repository |
|
25 |
*/ |
|
26 |
private final SourcesRepository sourcesRepository; |
|
27 |
|
|
28 |
@Override |
|
29 |
public Sources getSources() { |
|
30 |
Optional<Sources> optionalSources = sourcesRepository.findById(SOURCES_ID); |
|
31 |
if (optionalSources.isEmpty()) { |
|
32 |
Sources sources = new Sources(SOURCES_ID, ""); |
|
33 |
return sourcesRepository.save(sources); |
|
34 |
} |
|
35 |
return optionalSources.get(); |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public void updateSources(Sources sources) { |
|
40 |
sources.setId(SOURCES_ID); |
|
41 |
sourcesRepository.save(sources); |
|
42 |
} |
|
43 |
} |
backend/src/main/java/cz/zcu/kiv/backendapi/titlepage/ITitlePageService.java | ||
---|---|---|
12 | 12 |
TitlePage getTitlePage(); |
13 | 13 |
|
14 | 14 |
/** |
15 |
* Adds or updates title page with given ID
|
|
15 |
* Adds or updates title page |
|
16 | 16 |
* |
17 | 17 |
* @param titlePage title page |
18 | 18 |
*/ |
backend/src/main/java/cz/zcu/kiv/backendapi/titlepage/TitlePage.java | ||
---|---|---|
28 | 28 |
@JsonIgnore |
29 | 29 |
private Long id; |
30 | 30 |
|
31 |
/** |
|
32 |
* Title |
|
33 |
*/ |
|
34 |
private String title; |
|
35 |
|
|
36 | 31 |
/** |
37 | 32 |
* Content of the title page |
38 | 33 |
*/ |
backend/src/main/java/cz/zcu/kiv/backendapi/titlepage/TitlePageServiceImplementation.java | ||
---|---|---|
29 | 29 |
public TitlePage getTitlePage() { |
30 | 30 |
Optional<TitlePage> optionalTitlePage = titlePageRepository.findById(TITLE_PAGE_ID); |
31 | 31 |
if (optionalTitlePage.isEmpty()) { |
32 |
TitlePage titlePage = new TitlePage(TITLE_PAGE_ID, "", "");
|
|
32 |
TitlePage titlePage = new TitlePage(TITLE_PAGE_ID, ""); |
|
33 | 33 |
return titlePageRepository.save(titlePage); |
34 | 34 |
} |
35 | 35 |
return optionalTitlePage.get(); |
backend/src/test/java/cz/zcu/kiv/backendapi/sources/SourcesServiceImplementationTest.java | ||
---|---|---|
1 |
package cz.zcu.kiv.backendapi.sources; |
|
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 |
import java.util.Optional; |
|
11 |
|
|
12 |
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
|
13 |
import static org.mockito.ArgumentMatchers.any; |
|
14 |
import static org.mockito.BDDMockito.given; |
|
15 |
import static org.mockito.Mockito.never; |
|
16 |
import static org.mockito.Mockito.verify; |
|
17 |
|
|
18 |
@ExtendWith(MockitoExtension.class) |
|
19 |
class SourcesServiceImplementationTest { |
|
20 |
|
|
21 |
@Mock |
|
22 |
private SourcesRepository sourcesRepository; |
|
23 |
|
|
24 |
private SourcesServiceImplementation underTest; |
|
25 |
|
|
26 |
@BeforeEach |
|
27 |
void setUp() { |
|
28 |
underTest = new SourcesServiceImplementation(sourcesRepository); |
|
29 |
} |
|
30 |
|
|
31 |
@Test |
|
32 |
void testAddSources() { |
|
33 |
// given |
|
34 |
long id = 1; |
|
35 |
Sources sources = new Sources(); |
|
36 |
sources.setSources("sources"); |
|
37 |
|
|
38 |
// when |
|
39 |
underTest.updateSources(sources); |
|
40 |
|
|
41 |
// then |
|
42 |
ArgumentCaptor<Sources> argumentCaptor = ArgumentCaptor.forClass(Sources.class); |
|
43 |
|
|
44 |
verify(sourcesRepository).save(argumentCaptor.capture()); |
|
45 |
|
|
46 |
Sources capturedSources = argumentCaptor.getValue(); |
|
47 |
|
|
48 |
assertThat(capturedSources.getId()).isEqualTo(id); |
|
49 |
|
|
50 |
assertThat(capturedSources).isEqualTo(sources); |
|
51 |
|
|
52 |
} |
|
53 |
|
|
54 |
@Test |
|
55 |
void testGetSourcesWhenExists() { |
|
56 |
// given |
|
57 |
long id = 1; |
|
58 |
Sources sources = new Sources(id, "sources"); |
|
59 |
|
|
60 |
given(sourcesRepository.findById(id)).willReturn(Optional.of(sources)); |
|
61 |
|
|
62 |
// when |
|
63 |
Sources result = underTest.getSources(); |
|
64 |
|
|
65 |
// then |
|
66 |
verify(sourcesRepository).findById(id); |
|
67 |
verify(sourcesRepository, never()).save(any()); |
|
68 |
|
|
69 |
assertThat(result).isEqualTo(sources); |
|
70 |
|
|
71 |
} |
|
72 |
|
|
73 |
@Test |
|
74 |
void testGetTitlePageWhenNotExists() { |
|
75 |
// given |
|
76 |
long id = 1; |
|
77 |
Sources sources = new Sources(id, ""); |
|
78 |
|
|
79 |
given(sourcesRepository.findById(id)).willReturn(Optional.empty()); |
|
80 |
|
|
81 |
// when |
|
82 |
underTest.getSources(); |
|
83 |
|
|
84 |
// then |
|
85 |
verify(sourcesRepository).findById(id); |
|
86 |
|
|
87 |
ArgumentCaptor<Sources> argumentCaptor = ArgumentCaptor.forClass(Sources.class); |
|
88 |
|
|
89 |
verify(sourcesRepository).save(argumentCaptor.capture()); |
|
90 |
|
|
91 |
Sources capturedSources = argumentCaptor.getValue(); |
|
92 |
|
|
93 |
assertThat(capturedSources).isEqualTo(sources); |
|
94 |
|
|
95 |
} |
|
96 |
} |
backend/src/test/java/cz/zcu/kiv/backendapi/titlepage/TitlePageServiceImplementationTest.java | ||
---|---|---|
33 | 33 |
// given |
34 | 34 |
long id = 1; |
35 | 35 |
TitlePage titlePage = new TitlePage(); |
36 |
titlePage.setTitle("title"); |
|
37 | 36 |
titlePage.setContent("content"); |
38 | 37 |
|
39 | 38 |
// when |
... | ... | |
56 | 55 |
void testGetTitlePageWhenExists() { |
57 | 56 |
// given |
58 | 57 |
long id = 1; |
59 |
TitlePage titlePage = new TitlePage(id, "title", "content");
|
|
58 |
TitlePage titlePage = new TitlePage(id, "content"); |
|
60 | 59 |
|
61 | 60 |
given(titlePageRepository.findById(id)).willReturn(Optional.of(titlePage)); |
62 | 61 |
|
... | ... | |
75 | 74 |
void testGetTitlePageWhenNotExists() { |
76 | 75 |
// given |
77 | 76 |
long id = 1; |
78 |
TitlePage titlePage = new TitlePage(id, "", "");
|
|
77 |
TitlePage titlePage = new TitlePage(id, ""); |
|
79 | 78 |
|
80 | 79 |
given(titlePageRepository.findById(id)).willReturn(Optional.empty()); |
81 | 80 |
|
Také k dispozici: Unified diff
Added sources (can add/edit bibliography page)
re #9788