Skip to content

Commit

Permalink
#948: test for duplicateEntity()
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed Jun 19, 2024
1 parent 92b1cfe commit be5f476
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.puzzle.okr.models.Objective;
import ch.puzzle.okr.models.authorization.AuthorizationUser;
import ch.puzzle.okr.service.business.ObjectiveBusinessService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -138,4 +139,49 @@ void deleteEntityByIdShouldThrowExceptionWhenNotAuthorized() {
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}

@DisplayName("duplicateEntity() should throw exception when not authorized")
@Test
void duplicateEntityShouldThrowExceptionWhenNotAuthorized() {
// arrange
Long idExistingObjective = 13L;
String reason = "junit test reason";
Objective objective = Objective.Builder.builder().build();

when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED, reason)).when(authorizationService)
.hasRoleCreateOrUpdate(objective, authorizationUser);

// act
ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> objectiveAuthorizationService.duplicateEntity(idExistingObjective, objective));

// assert
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}

@DisplayName("duplicateEntity() should return duplicated Objective when authorized")
@Test
void duplicateEntityShouldReturnDuplicatedObjectiveWhenAuthorized() {
// arrange
Long idExistingObjective = 13L;

Objective newObjectiveWithoutKeyResults = Objective.Builder.builder() //
.withTitle("Objective without KeyResults").build();

Objective newObjectiveWithKeyResults = Objective.Builder.builder() //
.withId(42L).withTitle("Objective with Id and KeyResults").build();

when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
when(objectiveBusinessService.duplicateObjective(idExistingObjective, newObjectiveWithoutKeyResults,
authorizationUser)).thenReturn(newObjectiveWithKeyResults);

// act
Objective objective = objectiveAuthorizationService.duplicateEntity(idExistingObjective,
newObjectiveWithoutKeyResults);

// assert
assertEquals(newObjectiveWithKeyResults, objective);
}
}

0 comments on commit be5f476

Please sign in to comment.