diff --git a/backend/src/test/java/ch/puzzle/okr/service/business/AlignmentBusinessServiceTest.java b/backend/src/test/java/ch/puzzle/okr/service/business/AlignmentBusinessServiceTest.java index f28487cec1..abc3d008b6 100644 --- a/backend/src/test/java/ch/puzzle/okr/service/business/AlignmentBusinessServiceTest.java +++ b/backend/src/test/java/ch/puzzle/okr/service/business/AlignmentBusinessServiceTest.java @@ -16,6 +16,7 @@ import ch.puzzle.okr.service.persistence.KeyResultPersistenceService; import ch.puzzle.okr.service.persistence.ObjectivePersistenceService; import ch.puzzle.okr.service.validation.AlignmentValidationService; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -455,4 +456,49 @@ void shouldCorrectFilterWhenNoMatchingObjectiveSearch() { assertEquals(40, dividedAlignmentViewLists.wrongAlignments().get(2).getId()); assertEquals(41, dividedAlignmentViewLists.wrongAlignments().get(3).getId()); } + + @Test + void shouldThrowErrorWhenPersistenceServiceReturnsIncorrectData() { + AlignmentView alignmentView5 = AlignmentView.Builder.builder().withUniqueId("23TkeyResultkeyResult").withId(20L) + .withTitle("Dies hat kein Gegenstück").withTeamId(5L).withTeamName("Puzzle ITC").withQuarterId(2L) + .withObjectType("keyResult").withConnectionItem("target").withRefId(37L).withRefType("objective") + .build(); + List finalList = List.of(alignmentView1, alignmentView2, alignmentView3, alignmentView4, + alignmentView5); + + doNothing().when(validator).validateOnAlignmentGet(anyLong(), anyList()); + when(alignmentViewPersistenceService.getAlignmentViewListByQuarterId(2L)).thenReturn(finalList); + + OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class, + () -> alignmentBusinessService.getAlignmentsByFilters(2L, List.of(5L), "")); + + List expectedErrors = List + .of(new ErrorDto("ALIGNMENT_DATA_FAIL", List.of("alignmentData", "2", "[5]", ""))); + + assertEquals(BAD_REQUEST, exception.getStatusCode()); + Assertions.assertThat(expectedErrors).hasSameElementsAs(exception.getErrors()); + assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason())); + } + + @Test + void shouldNotThrowErrorWhenSameAmountOfSourceAndTarget() { + List finalList = List.of(alignmentView1, alignmentView2, alignmentView3, alignmentView4); + + assertDoesNotThrow(() -> alignmentBusinessService.validateFinalList(finalList, 2L, List.of(5L), "")); + } + + @Test + void shouldThrowErrorWhenNotSameAmountOfSourceAndTarget() { + List finalList = List.of(alignmentView1, alignmentView2, alignmentView3); + + OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class, + () -> alignmentBusinessService.validateFinalList(finalList, 2L, List.of(5L), "")); + + List expectedErrors = List + .of(new ErrorDto("ALIGNMENT_DATA_FAIL", List.of("alignmentData", "2", "[5]", ""))); + + assertEquals(BAD_REQUEST, exception.getStatusCode()); + Assertions.assertThat(expectedErrors).hasSameElementsAs(exception.getErrors()); + assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason())); + } }