11package 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 .*;
54import static org .mockito .Mockito .verify ;
65import static org .mockito .Mockito .when ;
76
7+ import ch .puzzle .pcts .exception .PCTSException ;
8+ import ch .puzzle .pcts .model .error .ErrorKey ;
89import ch .puzzle .pcts .model .role .Role ;
910import ch .puzzle .pcts .service .persistence .RolePersistenceService ;
1011import ch .puzzle .pcts .service .validation .RoleValidationService ;
12+ import java .util .ArrayList ;
1113import java .util .List ;
1214import java .util .Optional ;
1315import 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}
0 commit comments