Skip to content

Commit

Permalink
update backend tests for get completed logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel7373 committed Jan 14, 2025
1 parent 8a468fd commit 855485b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void deleteCompletedByObjectiveId(Long objectiveId) {
}

public Completed getCompletedByObjectiveId(Long objectiveId) {
validator.validateOnGet(objectiveId);
Completed completed = completedPersistenceService.getCompletedByObjectiveId(objectiveId);
// Must exist in business service in order to prevent error while deleting
// ongoing objectives
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ch.puzzle.okr.service.business;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.http.HttpStatus.NOT_FOUND;

import ch.puzzle.okr.exception.OkrResponseStatusException;
import ch.puzzle.okr.models.Completed;
import ch.puzzle.okr.models.Objective;
import ch.puzzle.okr.service.persistence.CompletedPersistenceService;
Expand Down Expand Up @@ -84,7 +86,7 @@ void shouldDeleteKeyResultAndAssociatedCheckIns() {
verify(this.completedPersistenceService, times(1)).deleteById(1L);
}

@DisplayName("Should do nothing if completed is null")
@DisplayName("Should do nothing if completed to delete is null")
@Test
void shouldDoNothingIfCompletedIsNull() {
when(completedPersistenceService.getCompletedByObjectiveId(anyLong())).thenReturn(null);
Expand All @@ -104,4 +106,15 @@ void shouldGetCompleted() {
verify(this.completedPersistenceService, times(1)).getCompletedByObjectiveId(1L);
}

@DisplayName("Should throw exception if completed is null")
@Test
void shouldThrowExceptionIfCompletedIsNull() {
when(completedPersistenceService.getCompletedByObjectiveId(-1L)).thenReturn(null);
when(completedPersistenceService.getModelName()).thenCallRealMethod();

assertThrows(OkrResponseStatusException.class, () -> completedBusinessService.getCompletedByObjectiveId(-1L));

verify(this.completedPersistenceService, times(1)).getCompletedByObjectiveId(-1L);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

@SpringIntegrationTest
class CompletedPersistenceServiceIT {
private static final Logger log = LoggerFactory.getLogger(CompletedPersistenceServiceIT.class);
@Autowired
private CompletedPersistenceService completedPersistenceService;
private Completed createdCompleted;
Expand Down Expand Up @@ -124,16 +123,7 @@ void getCompletedShouldGetCompletedByObjectiveId() {
@DisplayName("Should throw exception on getCompletedByObjectiveId() when id does not exist")
@Test
void getCompletedShouldThrowExceptionWhenCompletedNotFound() {
OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
() -> completedPersistenceService
.getCompletedByObjectiveId(-1L));

List<ErrorDto> expectedErrors = List
.of(new ErrorDto("MODEL_WITH_ID_NOT_FOUND", List.of(COMPLETED, String.valueOf(-1))));

assertEquals(NOT_FOUND, exception.getStatusCode());
assertThat(expectedErrors).hasSameElementsAs(exception.getErrors());
assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason()));
}

@DisplayName("Should delete entity on deleteById()")
Expand Down

0 comments on commit 855485b

Please sign in to comment.