Skip to content

Commit 18d6b3a

Browse files
committed
fix: review improvements #34
1 parent 4d122b5 commit 18d6b3a

9 files changed

Lines changed: 52 additions & 34 deletions

File tree

backend/src/main/java/ch/puzzle/pcts/controller/RoleController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ResponseEntity<RoleDto> updateRole(@PathVariable Long id, @RequestBody Ro
7272
@ApiResponse(responseCode = "204", description = "Role deleted successfully", content = @Content),
7373
@ApiResponse(responseCode = "404", description = "Role not found", content = @Content) })
7474
@DeleteMapping("{id}")
75-
public ResponseEntity deleteRole(@PathVariable Long id) {
75+
public ResponseEntity<Void> deleteRole(@PathVariable Long id) {
7676
service.delete(id);
7777
return ResponseEntity.status(204).build();
7878
}

backend/src/main/java/ch/puzzle/pcts/model/role/Role.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
@SQLRestriction("deleted_at IS NULL")
1010
public class Role {
1111
@Id
12-
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_role")
13-
@SequenceGenerator(name = "sequence_role", allocationSize = 1)
12+
@GeneratedValue(strategy = GenerationType.IDENTITY)
1413
private Long id;
1514

1615
private String name;

backend/src/main/java/ch/puzzle/pcts/service/business/RoleBusinessService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public Role update(Long id, Role role) {
4444

4545
public void delete(Long id) {
4646
validationService.validateOnDelete(id);
47-
Role deletedRole = this.getById(id);
4847
persistenceService.delete(id);
4948
}
5049
}

backend/src/main/resources/db/dev-data-migration/afterMigrate__0_0_initialData.sql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ SELECT setval('sequence_example', (SELECT MAX(id) FROM example));
1111

1212
TRUNCATE TABLE role CASCADE;
1313

14-
INSERT INTO role (id, name, deleted_at, is_management)
14+
INSERT INTO role (name, is_management)
1515
VALUES
16-
(1, 'Administrator', '1970-01-01 00:00:00', TRUE),
17-
(2, 'Manager', null, TRUE),
18-
(3, 'Team Lead', null, TRUE),
19-
(4, 'Developer', null, FALSE),
20-
(5, 'Intern', null, FALSE);
21-
22-
SELECT setval('sequence_role', (SELECT MAX(id) FROM role));
16+
('Administrator', TRUE),
17+
('Manager', TRUE),
18+
('Team Lead', TRUE),
19+
('Developer', FALSE),
20+
('Intern', FALSE),
21+
('Extern', FALSE),
22+
('Consultant', FALSE);
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
CREATE SEQUENCE IF NOT EXISTS sequence_role;
21
CREATE TABLE IF NOT EXISTS role
32
(
4-
id BIGINT NOT NUll PRIMARY KEY,
3+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
54
name TEXT NOT NULL,
6-
deleted_at TIMESTAMP,
5+
deleted_at TIMESTAMP DEFAULT NULL,
76
is_management BOOLEAN NOT NULL
87
);

backend/src/test/java/ch/puzzle/pcts/controller/RoleControllerIT.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ void shouldGetRoleById() throws Exception {
8585
BDDMockito.given(service.getById(anyLong())).willReturn(role);
8686
BDDMockito.given(mapper.toDto(any(Role.class))).willReturn(expectedDto);
8787

88-
mvc.perform(get(BASEURL + "/1").with(SecurityMockMvcRequestPostProcessors.csrf())).andExpect(status().isOk());
88+
mvc
89+
.perform(get(BASEURL + "/1").with(SecurityMockMvcRequestPostProcessors.csrf()))
90+
.andExpect(status().isOk())
91+
.andExpect(jsonPath("$.id").isNumber())
92+
.andExpect(jsonPath("$.isManagement").value(false))
93+
.andExpect(jsonPath("$.name").value("Role 1"));
8994

9095
verify(service, times(1)).getById(eq(1L));
9196
verify(mapper, times(1)).toDto(any(Role.class));
@@ -139,18 +144,14 @@ void shouldUpdateRole() throws Exception {
139144
@Test
140145
void shouldDeleteRole() throws Exception {
141146
BDDMockito.willDoNothing().given(service).delete(anyLong());
142-
BDDMockito.given(mapper.toDto(any(Role.class))).willReturn(expectedDto);
143147

144148
mvc
145149
.perform(delete(BASEURL + "/" + id)
146150
.contentType(MediaType.APPLICATION_JSON)
147151
.with(SecurityMockMvcRequestPostProcessors.csrf()))
148152
.andExpect(status().is(204))
149-
.andExpect(jsonPath("$.id").isNumber())
150-
.andExpect(jsonPath("$.isManagement").value(false))
151-
.andExpect(jsonPath("$.name").value("Role 1"));
153+
.andExpect(jsonPath("$").doesNotExist());
152154

153155
verify(service, times(1)).delete(any(Long.class));
154-
verify(mapper, times(1)).toDto(any(Role.class));
155156
}
156157
}

backend/src/test/java/ch/puzzle/pcts/service/business/RoleBusinessServiceTest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package ch.puzzle.pcts.service.business;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.*;
54
import static org.mockito.Mockito.verify;
65
import static org.mockito.Mockito.when;
76

7+
import ch.puzzle.pcts.exception.PCTSException;
8+
import ch.puzzle.pcts.model.error.ErrorKey;
89
import ch.puzzle.pcts.model.role.Role;
910
import ch.puzzle.pcts.service.persistence.RolePersistenceService;
1011
import ch.puzzle.pcts.service.validation.RoleValidationService;
12+
import java.util.ArrayList;
1113
import java.util.List;
1214
import java.util.Optional;
1315
import org.junit.jupiter.api.BeforeEach;
@@ -45,6 +47,18 @@ void shouldGetById() {
4547
verify(persistenceService).getById(1L);
4648
}
4749

50+
@DisplayName("Should throw exception")
51+
@Test
52+
void shouldThrowException() {
53+
when(persistenceService.getById(1L)).thenReturn(Optional.empty());
54+
55+
PCTSException exception = assertThrows(PCTSException.class, () -> businessService.getById(1L));
56+
57+
assertEquals("Role with id: " + 1 + " does not exist.", exception.getReason());
58+
assertEquals(ErrorKey.NOT_FOUND, exception.getErrorKey());
59+
verify(persistenceService).getById(1L);
60+
}
61+
4862
@DisplayName("Should get all roles")
4963
@Test
5064
void shouldGetAll() {
@@ -58,6 +72,16 @@ void shouldGetAll() {
5872
verify(persistenceService).getAll();
5973
}
6074

75+
@DisplayName("Should get empty list")
76+
@Test
77+
void shouldGetEmptyList() {
78+
when(persistenceService.getAll()).thenReturn(new ArrayList<>());
79+
80+
List<Role> result = businessService.getAll();
81+
82+
assertEquals(0, result.size());
83+
}
84+
6185
@DisplayName("Should create role")
6286
@Test
6387
void shouldCreate() {
@@ -89,13 +113,10 @@ void shouldUpdate() {
89113
@Test
90114
void shouldDelete() {
91115
Long id = 1L;
92-
Role role = new Role(id, "Role1", false);
93-
when(persistenceService.getById(id)).thenReturn(Optional.of(role));
94116

95117
businessService.delete(id);
96118

97119
verify(validationService).validateOnDelete(id);
98-
verify(persistenceService).getById(id);
99120
verify(persistenceService).delete(id);
100121
}
101122
}

backend/src/test/java/ch/puzzle/pcts/service/persistence/RolePersistenceServiceIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ void shouldGetAllRoles() {
7070
void shouldCreate() {
7171
Role role = new Role(null, "Role 3", false);
7272

73-
persistenceService.create(role);
74-
Optional<Role> result = persistenceService.getById(3L);
73+
Role result = persistenceService.create(role);
7574

76-
assertThat(result.isPresent()).isTrue();
77-
assertThat(role.getId()).isEqualTo(3L);
75+
assertThat(result.getId()).isEqualTo(3L);
76+
assertThat(result.getName()).isEqualTo(role.getName());
77+
assertThat(result.getIsManagement()).isEqualTo(role.getIsManagement());
7878
}
7979

8080
@DisplayName("Should update role")

backend/src/test/resources/db/test-data-migration/afterMigrate__0_0_initialData.sql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ SELECT setval('sequence_example', (SELECT MAX(id) FROM example));
88

99
TRUNCATE TABLE role CASCADE;
1010

11-
INSERT INTO role (id, name, deleted_at, is_management)
11+
INSERT INTO role (name, deleted_at, is_management)
1212
VALUES
13-
(1, 'Role 1', '1970-01-01 00:00:00', TRUE),
14-
(2, 'Role 2', null, false);
13+
('Role 1', '1970-01-01 00:00:00', TRUE),
14+
('Role 2', null, FALSE);
1515

16-
SELECT setval('sequence_role', (SELECT MAX(id) FROM role));
1716

1817

0 commit comments

Comments
 (0)