diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/ActionMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/ActionMapperTest.java new file mode 100644 index 0000000000..28d7bee8e3 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/ActionMapperTest.java @@ -0,0 +1,159 @@ +package ch.puzzle.okr.mapper; + +import ch.puzzle.okr.dto.ActionDto; +import ch.puzzle.okr.models.Action; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.models.keyresult.KeyResultMetric; +import ch.puzzle.okr.service.business.KeyResultBusinessService; +import ch.puzzle.okr.service.persistence.KeyResultPersistenceService; +import ch.puzzle.okr.service.validation.KeyResultValidationService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ActionMapperTest { + + private static final long ID = 0L; + private static final int VERSION = 1; + private static final String ACTION = "action"; + private static final int PRIORITY = 42; + private static final boolean IS_CHECKED = true; + private static final boolean IS_WRITEABLE = true; + public static final long KEY_RESULT_ID = 10L; + private final KeyResult keyResult = KeyResultMetric.Builder.builder().withId(KEY_RESULT_ID).build(); + + private ActionMapper actionMapper; + @Mock + private KeyResultBusinessService keyResultBusinessService; + @Mock + private KeyResultValidationService validator; + @Mock + private KeyResultPersistenceService keyResultPersistenceService; + + @BeforeEach + void setup() { + actionMapper = new ActionMapper(keyResultBusinessService); + } + + @DisplayName("toDto() should map Action to Dto") + @Test + void toDtoShouldMapActionToDto() { + // arrange + Action action = Action.Builder.builder() // + .withId(ID) // + .withVersion(VERSION) // + .withAction(ACTION) // + .withPriority(PRIORITY) // + .withIsChecked(IS_CHECKED) // + .withKeyResult(keyResult) // + .build(); + action.setWriteable(IS_WRITEABLE); + + // act + ActionDto actionDto = actionMapper.toDto(action); + + // assert + assertNotNull(actionDto); + assertActionDto(action, actionDto); + } + + private void assertActionDto(Action expected, ActionDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getAction(), actual.action()); + assertEquals(expected.getPriority(), actual.priority()); + assertEquals(expected.isChecked(), actual.isChecked()); + assertEquals(expected.getKeyResult().getId(), actual.keyResultId()); + assertEquals(expected.isWriteable(), actual.writeable()); + } + + @DisplayName("toActions() should map List of Dtos with KeyResultId to list of Actions.") + @Test + void toActionsShouldMapListOfDtosWithKeyResltIdToListOfActions() { + // arrange + when(keyResultBusinessService.getEntityById(keyResult.getId())).thenReturn(keyResult); + + ActionDto actionDto = new ActionDto( // + ID, // + VERSION, // + ACTION, // + PRIORITY, // + IS_CHECKED, // + keyResult.getId(), // + IS_WRITEABLE // + ); + + // act + List actionDtoList = List.of(actionDto); + List actionList = actionMapper.toActions(actionDtoList); + + // assert + assertNotNull(actionList); + assertListOfActionsWithKeyResultId(actionDtoList, actionList); + } + + @DisplayName("toActions() with KeyResult Parameter should map List of Dtos (without a KeyResultId) to list of Actions") + @Test + void toActionsWithKeyResultParameterShouldMapListOfDtosToListOfActions() { + // arrange + KeyResult keyResultParameter = KeyResultMetric.Builder.builder().withId(20L).build(); + ActionDto actionDtoWithKeyResultIdIsNull = new ActionDto(ID, // + VERSION, // + ACTION, // + PRIORITY, // + IS_CHECKED, // + null, // keyResultId + IS_WRITEABLE // + ); + + // act + List actionDtoList = List.of(actionDtoWithKeyResultIdIsNull); + List actionList = actionMapper.toActions(actionDtoList, keyResultParameter); + + // assert + assertNotNull(actionList); + + // the value of the KeyResultId in the ActionDto is null + // the value of the KeyResultId in Action is the value of keyResultParameter ( == 20) + assertListOfActionsAndKeyResultParameter(actionDtoList, actionList, keyResultParameter.getId()); + } + + private void assertListOfActionsWithKeyResultId(List expectedDtoList, List actualList) { + assertListsAndFirstAction(expectedDtoList, actualList); + assertEquals(expectedDtoList.get(0).keyResultId(), actualList.get(0).getKeyResult().getId()); + } + + private void assertListOfActionsAndKeyResultParameter(List expectedDtoList, List actualList, + Long keyResultId) { + assertListsAndFirstAction(expectedDtoList, actualList); + assertEquals(keyResultId, actualList.get(0).getKeyResult().getId()); + } + + private void assertListsAndFirstAction(List expectedDtoList, List actualList) { + assertEquals(expectedDtoList.size(), actualList.size()); + assertEquals(1, actualList.size()); + + ActionDto expectedActionDto = expectedDtoList.get(0); + Action actualAction = actualList.get(0); + assertNotNull(actualAction); + assertAction(expectedActionDto, actualAction); + } + + private void assertAction(ActionDto expected, Action actual) { + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.version(), actual.getVersion()); + assertEquals(expected.action(), actual.getAction()); + assertEquals(expected.priority(), actual.getPriority()); + assertEquals(expected.isChecked(), actual.isChecked()); + assertFalse(actual.isWriteable()); // TODO immer false? + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/CnAttributesMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/CnAttributesMapperTest.java new file mode 100644 index 0000000000..20be29cc7c --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/CnAttributesMapperTest.java @@ -0,0 +1,32 @@ +package ch.puzzle.okr.mapper; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttributes; + +public class CnAttributesMapperTest { + + private final CnAttributesMapper cnAttributesMapper = new CnAttributesMapper(); + + @DisplayName("for Cn AttributeId should return AttributeValue") + @Test + void forCnAttributeIdShouldReturnAttributeValue() throws NamingException { + Attributes attributes = new BasicAttributes(); + attributes.put("cn", "Mango"); + + Assertions.assertEquals("Mango", cnAttributesMapper.mapFromAttributes(attributes)); + } + + @DisplayName("for non Cn AttributeId should return null") + @Test + void forNonCnAttributeIdShouldReturnNull() throws NamingException { + Attributes attributes = new BasicAttributes(); + attributes.put("ou", "Juicy, Fruit"); + + Assertions.assertNull(cnAttributesMapper.mapFromAttributes(attributes)); + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/CompletedMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/CompletedMapperTest.java new file mode 100644 index 0000000000..141bb9f789 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/CompletedMapperTest.java @@ -0,0 +1,71 @@ +package ch.puzzle.okr.mapper; + +import ch.puzzle.okr.dto.CompletedDto; +import ch.puzzle.okr.models.Completed; +import ch.puzzle.okr.models.Objective; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@ExtendWith(MockitoExtension.class) +public class CompletedMapperTest { + + private static final long ID = 0L; + private static final String COMMENT = "some comment"; + + @InjectMocks + private CompletedMapper completedMapper; + + @DisplayName("toDo() should map Completed to Dto") + @Test + void toDtoShouldMapCompletedToDto() { + // arrange + Objective objective = Objective.Builder.builder().withId(23L).build(); + + Completed completed = Completed.Builder.builder() // + .withId(ID) // + .withComment(COMMENT) // + .withObjective(objective) // + .build(); + + // act + CompletedDto completedDto = completedMapper.toDto(completed); + + // assert + assertNotNull(completedDto); + assertCompletedDto(completed, completedDto); + } + + private void assertCompletedDto(Completed expected, CompletedDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getComment(), actual.comment()); + assertEquals(expected.getObjective().getId(), actual.objective().getId()); + } + + @DisplayName("toCompleted() should map Dto to Completed") + @Test + void toCompletedShouldMapDtoToCompleted() { + // arrange + Objective objective = Objective.Builder.builder().withId(23L).build(); + CompletedDto completedDto = new CompletedDto(ID, objective, COMMENT); + + // act + Completed completed = completedMapper.toCompleted(completedDto); + + // assert + assertNotNull(completed); + assertCompleted(completedDto, completed); + } + + private void assertCompleted(CompletedDto expected, Completed actual) { + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.comment(), actual.getComment()); + assertEquals(expected.objective().getId(), actual.getObjective().getId()); + } + +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/ObjectiveMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/ObjectiveMapperTest.java new file mode 100644 index 0000000000..0640142ffe --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/ObjectiveMapperTest.java @@ -0,0 +1,145 @@ +package ch.puzzle.okr.mapper; + +import ch.puzzle.okr.dto.ObjectiveDto; +import ch.puzzle.okr.models.Objective; +import ch.puzzle.okr.models.Quarter; +import ch.puzzle.okr.models.State; +import ch.puzzle.okr.models.Team; +import ch.puzzle.okr.service.business.QuarterBusinessService; +import ch.puzzle.okr.service.business.TeamBusinessService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.LocalDateTime; +import java.time.Month; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class ObjectiveMapperTest { + + private static final long TEAM_ID = 23L; + private static final long QUARTER_ID = 10L; + private static final String QUARTER_LABEL = "GJ 2024 Q1"; + private static final long ID = 0L; + private static final int VERSION = 1; + private static final String TITLE = "objective title"; + private static final String DESCRIPTION = "objective description"; + private static final State STATE = State.DRAFT; + private static final boolean IS_WRITEABLE = true; + + private static final LocalDateTime CREATE_DATE_TIME = LocalDateTime.of(2024, Month.MAY, 20, 12, 35, 0); + private static final LocalDateTime MODIFIED_DATE_TIME = LocalDateTime.of(2024, Month.MAY, 21, 8, 0, 0); + + private final Team team = Team.Builder.builder().withId(TEAM_ID).build(); + private final Quarter quarter = Quarter.Builder.builder() // + .withId(QUARTER_ID) // + .withLabel(QUARTER_LABEL) // + .build(); + + private ObjectiveMapper objectiveMapper; + + @Mock + private TeamBusinessService teamBusinessService; + + @Mock + private QuarterBusinessService quarterBusinessService; + + @BeforeEach + void setup() { + objectiveMapper = new ObjectiveMapper(teamBusinessService, quarterBusinessService); + } + + @DisplayName("toDto() should map Objective to Dto") + @Test + void toDtoShouldMapObjectiveToDto() { + // arrange + Objective objective = Objective.Builder.builder() // + .withId(ID) // + .withVersion(VERSION) // + .withTitle(TITLE) // + .withTeam(team) // + .withQuarter(quarter) // + .withDescription(DESCRIPTION) // + .withState(STATE) // + .withCreatedOn(CREATE_DATE_TIME) // + .withModifiedOn(MODIFIED_DATE_TIME) // + .build(); + objective.setWriteable(IS_WRITEABLE); + + // act + ObjectiveDto objectiveDto = objectiveMapper.toDto(objective); + + // assert + assertNotNull(objectiveDto); + assertObjectiveDto(objective, objectiveDto); + } + + private void assertObjectiveDto(Objective expected, ObjectiveDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getTitle(), actual.title()); + assertEquals(expected.getTeam().getId(), actual.teamId()); + assertEquals(expected.getQuarter().getId(), actual.quarterId()); + assertEquals(expected.getQuarter().getLabel(), actual.quarterLabel()); + assertEquals(expected.getDescription(), actual.description()); + assertEquals(expected.getState(), actual.state()); + assertEquals(expected.getCreatedOn(), actual.createdOn()); + assertEquals(expected.getModifiedOn(), actual.modifiedOn()); + } + + @DisplayName("toObjective() should map Dto to Objective") + @Test + void toObjectiveShouldMapDtoToObjective() { + // arrange + when(teamBusinessService.getTeamById(TEAM_ID)).thenReturn(team); + when(quarterBusinessService.getQuarterById(QUARTER_ID)).thenReturn(quarter); + + ObjectiveDto objectiveDto = new ObjectiveDto( // + ID, // + VERSION, // + TITLE, // + TEAM_ID, // + QUARTER_ID, // + QUARTER_LABEL, // + DESCRIPTION, // + STATE, // + CREATE_DATE_TIME, // + MODIFIED_DATE_TIME, // + IS_WRITEABLE // + ); + + // mock (LocalDateTime.now()) + act + assert + try (MockedStatic mockedStatic = Mockito.mockStatic(LocalDateTime.class)) { + mockedStatic.when(LocalDateTime::now).thenReturn(MODIFIED_DATE_TIME); + + // act + Objective objective = objectiveMapper.toObjective(objectiveDto); + + // assert + assertNotNull(objectiveDto); + assertObjective(objectiveDto, objective); + } + } + + private void assertObjective(ObjectiveDto expected, Objective actual) { + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.version(), actual.getVersion()); + assertEquals(expected.title(), actual.getTitle()); + assertEquals(expected.teamId(), actual.getTeam().getId()); + assertEquals(expected.quarterId(), actual.getQuarter().getId()); + assertEquals(expected.quarterLabel(), actual.getQuarter().getLabel()); + assertEquals(expected.description(), actual.getDescription()); + assertEquals(expected.state(), actual.getState()); + assertEquals(expected.createdOn(), actual.getCreatedOn()); + assertEquals(expected.modifiedOn(), actual.getModifiedOn()); + assertFalse(actual.isWriteable()); // TODO immer false? + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/TeamMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/TeamMapperTest.java new file mode 100644 index 0000000000..645f5f9f0b --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/TeamMapperTest.java @@ -0,0 +1,71 @@ +package ch.puzzle.okr.mapper; + +import ch.puzzle.okr.dto.TeamDto; +import ch.puzzle.okr.models.Team; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +public class TeamMapperTest { + + private static final long ID = 0L; + private static final int VERSION = 1; + private static final String NAME = "my team name"; + public static final boolean IS_WRITEABLE = true; + + @InjectMocks + private TeamMapper teamMapper; + + @DisplayName("toDto() should map Team to Dto") + @Test + void toDtoShouldMapTeamToDto() { + // arrange + Team team = Team.Builder.builder() // + .withId(ID) // + .withVersion(VERSION) // + .withName(NAME) // + .build(); + team.setWriteable(IS_WRITEABLE); + + // act + TeamDto teamDto = teamMapper.toDto(team); + + // assert + assertNotNull(teamDto); + assertTeamDto(team, teamDto); + } + + private static void assertTeamDto(Team expected, TeamDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getName(), actual.name()); + assertEquals(expected.isWriteable(), actual.writeable()); + } + + @DisplayName("toTeam() should map Dto to Team") + @Test + void toTeamShouldMapDtoToTeam() { + // arrange + TeamDto teamDto = new TeamDto(ID, VERSION, NAME, IS_WRITEABLE); + + // act + Team team = teamMapper.toTeam(teamDto); + + // assert + assertNotNull(team); + assertTeam(teamDto, team); + } + + private static void assertTeam(TeamDto expected, Team actual) { + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.version(), actual.getVersion()); + assertEquals(expected.name(), actual.getName()); + assertFalse(actual.isWriteable()); // TODO immer false? + } + +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/UserMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/UserMapperTest.java new file mode 100644 index 0000000000..aea07666b1 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/UserMapperTest.java @@ -0,0 +1,92 @@ +package ch.puzzle.okr.mapper; + +import ch.puzzle.okr.dto.UserDto; +import ch.puzzle.okr.models.Team; +import ch.puzzle.okr.models.User; +import ch.puzzle.okr.models.UserTeam; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +public class UserMapperTest { + + private static final long USER_TEAM_ID = 100L; + private static final long ID = 0L; + private static final int VERSION = 1; + private static final String FIRSTNAME = "firstname"; + private static final String LASTNAME = "lastname"; + private static final String EMAIL = "a@b.ch"; + private static final boolean IS_OKR_CHAMPION = true; + + private UserMapper userMapper; + + @InjectMocks + private TeamMapper teamMapper; + + @BeforeEach + void setup() { + userMapper = new UserMapper(teamMapper); + } + + @DisplayName("toDto() without TeamList throws NullPointerException") + @Test + void toDtoWithoutTeamListThrowsException() { + // arrange + User user = User.Builder.builder() // + .withId(ID) // + .withVersion(VERSION) // + .withFirstname(FIRSTNAME) // + .withLastname(LASTNAME) // + .withEmail(EMAIL) // + .withOkrChampion(IS_OKR_CHAMPION) // + .build(); + + // act + assert + assertThrows(NullPointerException.class, () -> userMapper.toDto(user)); + } + + @DisplayName("toDto() should map User to Dto") + @Test + void toDtoShouldMapUserToDto() { + // arrange + UserTeam userTeam = UserTeam.Builder.builder() // + .withId(USER_TEAM_ID) // + .withTeam(Team.Builder.builder().build()) // + .build(); + User user = User.Builder.builder() // + .withId(ID) // + .withVersion(VERSION) // + .withFirstname(FIRSTNAME) // + .withLastname(LASTNAME) // + .withEmail(EMAIL) // + .withUserTeamList(List.of(userTeam)) // + .withOkrChampion(IS_OKR_CHAMPION) // + .build(); + + // act + UserDto userDto = userMapper.toDto(user); + + // assert + assertNotNull(userDto); + assertUserDto(user, userDto); + } + + private void assertUserDto(User expected, UserDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getFirstname(), actual.firstname()); + assertEquals(expected.isOkrChampion(), actual.isOkrChampion()); + assertEquals(expected.getEmail(), actual.email()); + + assertEquals(1, actual.userTeamList().size()); + assertEquals(expected.getUserTeamList().get(0).getId(), actual.userTeamList().get(0).id()); + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInMapperTest.java new file mode 100644 index 0000000000..514d838491 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInMapperTest.java @@ -0,0 +1,131 @@ +package ch.puzzle.okr.mapper.checkin; + +import ch.puzzle.okr.dto.checkin.CheckInDto; +import ch.puzzle.okr.dto.checkin.CheckInMetricDto; +import ch.puzzle.okr.dto.checkin.CheckInOrdinalDto; +import ch.puzzle.okr.mapper.checkin.helper.TestDataDtoHelper; +import ch.puzzle.okr.mapper.checkin.helper.TestDataHelper; +import ch.puzzle.okr.models.checkin.CheckIn; +import ch.puzzle.okr.models.checkin.CheckInMetric; +import ch.puzzle.okr.models.checkin.CheckInOrdinal; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.service.business.KeyResultBusinessService; +import ch.puzzle.okr.service.persistence.KeyResultPersistenceService; +import ch.puzzle.okr.service.validation.KeyResultValidationService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class CheckInMapperTest { + + private CheckInMapper checkInMapper; + @Mock + private KeyResultBusinessService keyResultBusinessService; + @Mock + private KeyResultValidationService validator; + @Mock + private KeyResultPersistenceService keyResultPersistenceService; + + @BeforeEach + void setup() { + CheckInMetricMapper checkInMetricMapper = new CheckInMetricMapper(keyResultBusinessService); + CheckInOrdinalMapper checkInOrdinalMapper = new CheckInOrdinalMapper(keyResultBusinessService); + checkInMapper = new CheckInMapper(checkInMetricMapper, checkInOrdinalMapper); + } + + @DisplayName("toDto() should map CheckInMetric to CheckInMetricDto") + @Test + void toDtoShouldMapCheckInMetricToCheckInMetricDto() { + // arrange + CheckInMetric checkInMetric = TestDataHelper.checkInMetric(); + + // act + CheckInDto checkInDto = checkInMapper.toDto(checkInMetric); + + // assert + assertEquals(CheckInMetricDto.class, checkInDto.getClass()); + } + + @DisplayName("toDto() should map CheckInOrdinal to a CheckInOrdinalDto") + @Test + void toDtoShouldMapCheckInOrdinalToCheckInOrdinalDto() { + // arrange + CheckInOrdinal checkInOrdinal = TestDataHelper.checkInOrdinal(); + + // act + CheckInDto checkInDto = checkInMapper.toDto(checkInOrdinal); + + // assert + assertEquals(CheckInOrdinalDto.class, checkInDto.getClass()); + } + + @DisplayName("toCheckIn() should map Dto to CheckInMetric") + @Test + void toCheckInShouldMapDtoToCheckInMetric() { + // arrange + KeyResult keyResult = TestDataHelper.keyResult(); + when(keyResultBusinessService.getEntityById(keyResult.getId())).thenReturn(keyResult); + + CheckInMetricDto checkInMetricDto = TestDataDtoHelper.checkInMetricDto(); + + // act + CheckIn check = checkInMapper.toCheckIn(checkInMetricDto); + + // assert + assertEquals(CheckInMetric.class, check.getClass()); + } + + @DisplayName("toCheckIn() should map Dto to CheckInOrdinal") + @Test + void toCheckInShouldMapDtoToCheckInOrdinal() { + // arrange + KeyResult keyResult = TestDataHelper.keyResult(); + when(keyResultBusinessService.getEntityById(keyResult.getId())).thenReturn(keyResult); + + CheckInOrdinalDto checkInOrdinalDto = TestDataDtoHelper.checkInOrdinalDto(); + + // act + CheckIn check = checkInMapper.toCheckIn(checkInOrdinalDto); + + // assert + assertEquals(CheckInOrdinal.class, check.getClass()); + } + + @DisplayName("toDto() should throw Exception if CheckIn is not Metric or Ordinal") + @Test + void toDtoShouldThrowExceptionIfCheckInIsNotMetricOrOrdinal() { + // arrange + CheckIn checkIn = new CheckIn() { + }; + + // act + assert + ResponseStatusException responseStatusException = assertThrows( // + ResponseStatusException.class, // + () -> checkInMapper.toDto(checkIn)); + assertEquals(HttpStatus.BAD_REQUEST, responseStatusException.getStatusCode()); + } + + @DisplayName("toCheckIn() should throw Exception if CheckIn is not MetricDto or OrdinalDto") + @Test + void toCheckInShouldThrowExceptionIfCheckInIsNotMetricOrOrdinal() { + // arrange + CheckInDto checkInDto = new CheckInDto() { + }; + + // act + assert + ResponseStatusException responseStatusException = assertThrows( // + ResponseStatusException.class, // + () -> checkInMapper.toCheckIn(checkInDto)); + assertEquals(HttpStatus.BAD_REQUEST, responseStatusException.getStatusCode()); + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInMetricMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInMetricMapperTest.java new file mode 100644 index 0000000000..138391aa35 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInMetricMapperTest.java @@ -0,0 +1,72 @@ +package ch.puzzle.okr.mapper.checkin; + +import ch.puzzle.okr.dto.checkin.CheckInMetricDto; +import ch.puzzle.okr.mapper.checkin.helper.TestDataHelper; +import ch.puzzle.okr.models.checkin.CheckIn; +import ch.puzzle.okr.models.checkin.CheckInMetric; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.service.business.KeyResultBusinessService; +import ch.puzzle.okr.service.persistence.KeyResultPersistenceService; +import ch.puzzle.okr.service.validation.KeyResultValidationService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static ch.puzzle.okr.mapper.checkin.helper.AssertHelper.assertCheckInMetric; +import static ch.puzzle.okr.mapper.checkin.helper.AssertHelper.assertCheckInMetricDto; +import static ch.puzzle.okr.mapper.checkin.helper.TestDataDtoHelper.checkInMetricDto; +import static ch.puzzle.okr.mapper.checkin.helper.TestDataHelper.checkInMetric; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class CheckInMetricMapperTest { + + private CheckInMetricMapper checkInMetricMapper; + @Mock + private KeyResultBusinessService keyResultBusinessService; + @Mock + private KeyResultValidationService validator; + @Mock + private KeyResultPersistenceService keyResultPersistenceService; + + @BeforeEach + void setup() { + checkInMetricMapper = new CheckInMetricMapper(keyResultBusinessService); + } + + @DisplayName("toDto() should map CheckInMetric to Dto") + @Test + void toDtoShouldMapCheckInMetricToDto() { + // arrange + CheckInMetric checkInMetric = checkInMetric(); + + // act + CheckInMetricDto checkInMetricDto = checkInMetricMapper.toDto(checkInMetric); + + // assert + assertNotNull(checkInMetricDto); + assertCheckInMetricDto(checkInMetric, checkInMetricDto); + } + + @DisplayName("toCheckInMetric() should map Dto to checkInMetric") + @Test + void toCheckInMetricShouldMapDtoToCheckInMetric() { + // arrange + KeyResult keyResult = TestDataHelper.keyResult(); + when(keyResultBusinessService.getEntityById(keyResult.getId())).thenReturn(keyResult); + + CheckInMetricDto checkInMetricDto = checkInMetricDto(); + + // act + CheckIn checkIn = checkInMetricMapper.toCheckInMetric(checkInMetricDto); + CheckInMetric checkInMetric = (CheckInMetric) checkIn; + + // assert + assertNotNull(checkInMetricDto); + assertCheckInMetric(checkInMetricDto, checkInMetric); + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInOrdinalMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInOrdinalMapperTest.java new file mode 100644 index 0000000000..5715e9c57f --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/CheckInOrdinalMapperTest.java @@ -0,0 +1,73 @@ +package ch.puzzle.okr.mapper.checkin; + +import ch.puzzle.okr.dto.checkin.CheckInOrdinalDto; +import ch.puzzle.okr.mapper.checkin.helper.TestDataHelper; +import ch.puzzle.okr.models.checkin.CheckIn; +import ch.puzzle.okr.models.checkin.CheckInOrdinal; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.service.business.KeyResultBusinessService; +import ch.puzzle.okr.service.persistence.KeyResultPersistenceService; +import ch.puzzle.okr.service.validation.KeyResultValidationService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static ch.puzzle.okr.mapper.checkin.helper.AssertHelper.assertCheckInOrdianal; +import static ch.puzzle.okr.mapper.checkin.helper.AssertHelper.assertCheckInOrdinalDto; +import static ch.puzzle.okr.mapper.checkin.helper.TestDataDtoHelper.checkInOrdinalDto; +import static ch.puzzle.okr.mapper.checkin.helper.TestDataHelper.checkInOrdinal; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class CheckInOrdinalMapperTest { + + private CheckInOrdinalMapper checkInOrdinalMapper; + @Mock + private KeyResultBusinessService keyResultBusinessService; + @Mock + private KeyResultValidationService validator; + @Mock + private KeyResultPersistenceService keyResultPersistenceService; + + @BeforeEach + void setup() { + checkInOrdinalMapper = new CheckInOrdinalMapper(keyResultBusinessService); + } + + @DisplayName("toDto() should map CheckInOrdinal to Dto") + @Test + void toDtoShouldMapCheckInOrdinalToDto() { + // arrange + CheckInOrdinal checkInOrdinal = checkInOrdinal(); + + // act + CheckInOrdinalDto checkInOrdinalDto = checkInOrdinalMapper.toDto(checkInOrdinal); + + // assert + assertNotNull(checkInOrdinalDto); + assertCheckInOrdinalDto(checkInOrdinal, checkInOrdinalDto); + } + + @DisplayName("toCheckInOrdinal() should map Dto to checkInOrdinal") + @Test + void toCheckInOrdinalShouldMapDtoToCheckInOrdinal() { + // arrange + KeyResult keyResult = TestDataHelper.keyResult(); + when(keyResultBusinessService.getEntityById(keyResult.getId())).thenReturn(keyResult); + + CheckInOrdinalDto checkInOrdinalDto = checkInOrdinalDto(); + + // act + CheckIn checkIn = checkInOrdinalMapper.toCheckInOrdinal(checkInOrdinalDto); + CheckInOrdinal checkInOrdinal = (CheckInOrdinal) checkIn; + + // assert + assertNotNull(checkInOrdinal); + assertCheckInOrdianal(checkInOrdinalDto, checkInOrdinal); + } + +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/AssertHelper.java b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/AssertHelper.java new file mode 100644 index 0000000000..0c45423ea6 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/AssertHelper.java @@ -0,0 +1,70 @@ +package ch.puzzle.okr.mapper.checkin.helper; + +import ch.puzzle.okr.dto.checkin.CheckInMetricDto; +import ch.puzzle.okr.dto.checkin.CheckInOrdinalDto; +import ch.puzzle.okr.models.checkin.CheckInMetric; +import ch.puzzle.okr.models.checkin.CheckInOrdinal; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +public class AssertHelper { + + public static void assertCheckInMetricDto(CheckInMetric expected, CheckInMetricDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getChangeInfo(), actual.changeInfo()); + assertEquals(expected.getInitiatives(), actual.initiatives()); + assertEquals(expected.getConfidence(), actual.confidence()); + assertEquals(expected.getKeyResult().getId(), actual.keyResultId()); + assertEquals(expected.getCreatedOn(), actual.createdOn()); + assertEquals(expected.getModifiedOn(), actual.modifiedOn()); + assertEquals(expected.getValue(), actual.value()); + assertEquals(expected.isWriteable(), actual.writeable()); + } + + public static void assertCheckInMetric(CheckInMetricDto expected, CheckInMetric actual) { + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.version(), actual.getVersion()); + assertEquals(expected.changeInfo(), actual.getChangeInfo()); + assertEquals(expected.initiatives(), actual.getInitiatives()); + assertEquals(expected.confidence(), actual.getConfidence()); + assertEquals(expected.keyResultId(), actual.getKeyResult().getId()); + + // TODO CreatedOn + ModifiedOn are set in Dto but not in Obj + // Assertions.assertEquals(checkInMetricDto.createdOn(), checkInMetric.getCreatedOn()); + // Assertions.assertEquals(checkInMetricDto.modifiedOn(), checkInMetric.getModifiedOn()); + + assertEquals(expected.value(), actual.getValue()); + assertFalse(actual.isWriteable()); // TODO: immer false? + } + + public static void assertCheckInOrdinalDto(CheckInOrdinal expected, CheckInOrdinalDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getChangeInfo(), actual.changeInfo()); + assertEquals(expected.getInitiatives(), actual.initiatives()); + assertEquals(expected.getConfidence(), actual.confidence()); + assertEquals(expected.getKeyResult().getId(), actual.keyResultId()); + assertEquals(expected.getCreatedOn(), actual.createdOn()); + assertEquals(expected.getModifiedOn(), actual.modifiedOn()); + assertEquals(expected.getZone(), actual.value()); + assertEquals(expected.isWriteable(), actual.writeable()); + } + + public static void assertCheckInOrdianal(CheckInOrdinalDto checkInOrdinalDto, CheckInOrdinal checkInOrdinal) { + assertEquals(checkInOrdinalDto.id(), checkInOrdinal.getId()); + assertEquals(checkInOrdinalDto.version(), checkInOrdinal.getVersion()); + assertEquals(checkInOrdinalDto.changeInfo(), checkInOrdinal.getChangeInfo()); + assertEquals(checkInOrdinalDto.initiatives(), checkInOrdinal.getInitiatives()); + assertEquals(checkInOrdinalDto.confidence(), checkInOrdinal.getConfidence()); + assertEquals(checkInOrdinalDto.keyResultId(), checkInOrdinal.getKeyResult().getId()); + + // TODO CreatedOn + ModifiedOn are set in Dto but not in Obj + // Assertions.assertEquals(checkInMetricDto.createdOn(), checkInMetric.getCreatedOn()); + // Assertions.assertEquals(checkInMetricDto.modifiedOn(), checkInMetric.getModifiedOn()); + + assertEquals(checkInOrdinalDto.value(), checkInOrdinal.getZone()); // TODO: API - rename value -> zone in dto + assertFalse(checkInOrdinal.isWriteable()); // TODO: immer false? + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataConstants.java b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataConstants.java new file mode 100644 index 0000000000..a060a43c11 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataConstants.java @@ -0,0 +1,20 @@ +package ch.puzzle.okr.mapper.checkin.helper; + +import ch.puzzle.okr.models.checkin.Zone; + +import java.time.LocalDateTime; +import java.time.Month; + +public class TestDataConstants { + public static final boolean CHECK_IN_IS_WRITEABLE = true; + public static final long CHECK_IN_ID = 0L; + public static final int CHECK_IN_VERSION = 1; + public static final String CHECK_IN_CHANGE_INFO = "change info"; + public static final String CHECK_IN_INITIATIVES = "initiatives"; + public static final int CHECK_IN_CONFIDENCE = 30; + public static final double CHECK_IN_METRIC_VALUE = 23; + public static final Zone CHECK_IN_ORDINAL_ZONE = Zone.STRETCH; + public static final LocalDateTime CHECK_IN_CREATE_DATE_TIME = LocalDateTime.of(2024, Month.MAY, 20, 12, 35, 0); + public static final LocalDateTime CHECK_IN_MODIFIED_DATE_TIME = LocalDateTime.of(2024, Month.MAY, 21, 8, 0, 0); + public static final long KEY_RESULT_ID = 20L; +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataDtoHelper.java b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataDtoHelper.java new file mode 100644 index 0000000000..f7c7f2ff14 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataDtoHelper.java @@ -0,0 +1,41 @@ +package ch.puzzle.okr.mapper.checkin.helper; + +import ch.puzzle.okr.dto.checkin.CheckInMetricDto; +import ch.puzzle.okr.dto.checkin.CheckInOrdinalDto; + +import static ch.puzzle.okr.mapper.checkin.helper.TestDataConstants.*; + +public class TestDataDtoHelper { + + public static CheckInMetricDto checkInMetricDto() { + return checkInMetricDto; + } + + public static CheckInOrdinalDto checkInOrdinalDto() { + return checkInOrdinalDto; + } + + private static final CheckInMetricDto checkInMetricDto = new CheckInMetricDto(CHECK_IN_ID, // + CHECK_IN_VERSION, // + CHECK_IN_CHANGE_INFO, // + CHECK_IN_INITIATIVES, // + CHECK_IN_CONFIDENCE, // + KEY_RESULT_ID, // + CHECK_IN_CREATE_DATE_TIME, // + CHECK_IN_MODIFIED_DATE_TIME, // + CHECK_IN_METRIC_VALUE, // + CHECK_IN_IS_WRITEABLE // + ); + + private static final CheckInOrdinalDto checkInOrdinalDto = new CheckInOrdinalDto(CHECK_IN_ID, // + CHECK_IN_VERSION, // + CHECK_IN_CHANGE_INFO, // + CHECK_IN_INITIATIVES, // + CHECK_IN_CONFIDENCE, // + KEY_RESULT_ID, // + CHECK_IN_CREATE_DATE_TIME, // + CHECK_IN_MODIFIED_DATE_TIME, // + CHECK_IN_ORDINAL_ZONE, // + CHECK_IN_IS_WRITEABLE // + ); +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataHelper.java b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataHelper.java new file mode 100644 index 0000000000..dc2d43b9c8 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/checkin/helper/TestDataHelper.java @@ -0,0 +1,53 @@ +package ch.puzzle.okr.mapper.checkin.helper; + +import ch.puzzle.okr.models.checkin.CheckIn; +import ch.puzzle.okr.models.checkin.CheckInMetric; +import ch.puzzle.okr.models.checkin.CheckInOrdinal; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.models.keyresult.KeyResultMetric; + +import static ch.puzzle.okr.mapper.checkin.helper.TestDataConstants.*; + +public class TestDataHelper { + + public static KeyResult keyResult() { + return keyResult; + } + + public static CheckInMetric checkInMetric() { + checkInMetric.setWriteable(CHECK_IN_IS_WRITEABLE); + return (CheckInMetric) checkInMetric; + } + + public static CheckInOrdinal checkInOrdinal() { + checkInOrdinal.setWriteable(CHECK_IN_IS_WRITEABLE); + return (CheckInOrdinal) TestDataHelper.checkInOrdinal; + } + + private static final KeyResult keyResult = KeyResultMetric.Builder.builder() // + .withId(KEY_RESULT_ID) // + .build(); + + private static final CheckIn checkInMetric = CheckInMetric.Builder.builder() // + .withId(CHECK_IN_ID) // + .withVersion(CHECK_IN_VERSION) // + .withChangeInfo(CHECK_IN_CHANGE_INFO) // + .withInitiatives(CHECK_IN_INITIATIVES) // + .withConfidence(CHECK_IN_CONFIDENCE) // + .withKeyResult(keyResult) // + .withCreatedOn(CHECK_IN_CREATE_DATE_TIME) // + .withModifiedOn(CHECK_IN_MODIFIED_DATE_TIME) // + .withValue(CHECK_IN_METRIC_VALUE) // + .build(); + + private static final CheckIn checkInOrdinal = CheckInOrdinal.Builder.builder() // + .withId(CHECK_IN_ID) // + .withVersion(CHECK_IN_VERSION) // + .withChangeInfo(CHECK_IN_CHANGE_INFO) // + .withInitiatives(CHECK_IN_INITIATIVES) // + .withConfidence(CHECK_IN_CONFIDENCE) // + .withKeyResult(keyResult) // + .withCreatedOn(CHECK_IN_CREATE_DATE_TIME) // + .withModifiedOn(CHECK_IN_MODIFIED_DATE_TIME) // + .withZone(CHECK_IN_ORDINAL_ZONE).build(); +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultMapperTest.java new file mode 100644 index 0000000000..8ba2235adc --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultMapperTest.java @@ -0,0 +1,144 @@ +package ch.puzzle.okr.mapper.keyresult; + +import ch.puzzle.okr.dto.keyresult.KeyResultDto; +import ch.puzzle.okr.dto.keyresult.KeyResultMetricDto; +import ch.puzzle.okr.dto.keyresult.KeyResultOrdinalDto; +import ch.puzzle.okr.mapper.ActionMapper; +import ch.puzzle.okr.models.Action; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.models.keyresult.KeyResultMetric; +import ch.puzzle.okr.models.keyresult.KeyResultOrdinal; +import ch.puzzle.okr.service.business.CheckInBusinessService; +import ch.puzzle.okr.service.business.ObjectiveBusinessService; +import ch.puzzle.okr.service.business.UserBusinessService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; + +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataDtoHelper.keyResultMetricDto; +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataDtoHelper.keyResultOrdinalDto; +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataHelper.keyResultMetric; +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataHelper.keyResultOrdinal; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@ExtendWith(MockitoExtension.class) +public class KeyResultMapperTest { + + private KeyResultMapper keyResultMapper; + @Mock + UserBusinessService userBusinessService; + @Mock + private ObjectiveBusinessService objectiveBusinessService; + @Mock + private CheckInBusinessService checkInBusinessService; + @InjectMocks + private ActionMapper actionMapper; + + @BeforeEach + void setup() { + KeyResultMetricMapper keyResultMetricMapper = new KeyResultMetricMapper( // + userBusinessService, // + objectiveBusinessService, // + checkInBusinessService, // + actionMapper); + + KeyResultOrdinalMapper keyResultOrdinalMapper = new KeyResultOrdinalMapper( // + userBusinessService, // + objectiveBusinessService, // + checkInBusinessService, // + actionMapper); + + keyResultMapper = new KeyResultMapper(keyResultOrdinalMapper, keyResultMetricMapper); + } + + @DisplayName("toDto() should map KeyResultMetric to KeyResultMetricDto") + @Test + void toDtoShouldMapKeyResultMetricToKeyResultMetricDto() { + // arrange + KeyResultMetric keyResultMetric = keyResultMetric(); + List actions = List.of(); + + // act + KeyResultDto keyResultDto = keyResultMapper.toDto(keyResultMetric, actions); + + // assert + assertEquals(KeyResultMetricDto.class, keyResultDto.getClass()); + } + + @DisplayName("toDto() should map KeyResultOrdinal to KeyResultOrdinalDto") + @Test + void toDtoShouldMapKeyResultOrdinalToKeyResultOrdinalDto() { + // arrange + KeyResultOrdinal keyResultOrdinal = keyResultOrdinal(); + List actions = List.of(); + + // act + KeyResultDto keyResultDto = keyResultMapper.toDto(keyResultOrdinal, actions); + + // assert + assertEquals(KeyResultOrdinalDto.class, keyResultDto.getClass()); + } + + @DisplayName("toKeyResult() should map KeyResultMetricDto to KeyResultMetric") + @Test + void toKeyResultShouldMapKeyResultMetricDtoToKeyResultMetric() { + // arrange + KeyResultMetricDto keyResultMetricDto = keyResultMetricDto(); + + // act + KeyResult keyResult = keyResultMapper.toKeyResult(keyResultMetricDto); + + // assert + assertEquals(KeyResultMetric.class, keyResult.getClass()); + } + + @DisplayName("toKeyResult() should map KeyResultOrdinalDto to KeyResultOrdinal") + @Test + void toKeyResultShouldMapKeyResultOrdinalDtoToKeyResultOrdinal() { + // arrange + KeyResultOrdinalDto keyResultOrdinalDto = keyResultOrdinalDto(); + + // act + KeyResult keyResult = keyResultMapper.toKeyResult(keyResultOrdinalDto); + + // assert + assertEquals(KeyResultOrdinal.class, keyResult.getClass()); + } + + @DisplayName("toDto() should throw Exception if KeyResult is not Metric or Ordinal") + @Test + void toDtoShouldThrowExceptionIfKeyResultIsNotMetricOrOrdinal() { + // arrange + KeyResult keyResult = new KeyResult() { + }; + List actions = List.of(); + + // act + assert + ResponseStatusException responseStatusException = assertThrows( // + ResponseStatusException.class, // + () -> keyResultMapper.toDto(keyResult, actions)); + assertEquals(HttpStatus.BAD_REQUEST, responseStatusException.getStatusCode()); + } + + @DisplayName("toKeyResult() should throw Exception if KeyResultDto is not MetricDto or OrdinalDto") + @Test + void toKeyResultShouldThrowExceptionIfKeyResultDtoIsNotMetricOrOrdinal() { + // arrange + KeyResultDto keyResultDto = List::of; + + // act + assert + ResponseStatusException responseStatusException = assertThrows( // + ResponseStatusException.class, // + () -> keyResultMapper.toKeyResult(keyResultDto)); + assertEquals(HttpStatus.BAD_REQUEST, responseStatusException.getStatusCode()); + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultMetricMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultMetricMapperTest.java new file mode 100644 index 0000000000..9bcb25cbc8 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultMetricMapperTest.java @@ -0,0 +1,116 @@ +package ch.puzzle.okr.mapper.keyresult; + +import ch.puzzle.okr.dto.keyresult.KeyResultDto; +import ch.puzzle.okr.dto.keyresult.KeyResultMetricDto; +import ch.puzzle.okr.mapper.ActionMapper; +import ch.puzzle.okr.mapper.keyresult.helper.TestDataDtoHelper; +import ch.puzzle.okr.mapper.keyresult.helper.TestDataHelper; +import ch.puzzle.okr.models.Action; +import ch.puzzle.okr.models.checkin.CheckIn; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.models.keyresult.KeyResultMetric; +import ch.puzzle.okr.service.business.CheckInBusinessService; +import ch.puzzle.okr.service.business.KeyResultBusinessService; +import ch.puzzle.okr.service.business.ObjectiveBusinessService; +import ch.puzzle.okr.service.business.UserBusinessService; +import ch.puzzle.okr.service.persistence.KeyResultPersistenceService; +import ch.puzzle.okr.service.validation.KeyResultValidationService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static ch.puzzle.okr.mapper.keyresult.helper.AssertHelper.*; +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataHelper.*; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class KeyResultMetricMapperTest { + + private KeyResultMetricMapper keyResultMetricMapper; + @Mock + UserBusinessService userBusinessService; + @Mock + private ObjectiveBusinessService objectiveBusinessService; + @Mock + private CheckInBusinessService checkInBusinessService; + @InjectMocks + private ActionMapper actionMapper; + @Mock + private KeyResultBusinessService keyResultBusinessService; + @Mock + private KeyResultValidationService validator; + @Mock + private KeyResultPersistenceService keyResultPersistenceService; + + @BeforeEach + void setup() { + actionMapper = new ActionMapper(keyResultBusinessService); + keyResultMetricMapper = new KeyResultMetricMapper( // + userBusinessService, // + objectiveBusinessService, // + checkInBusinessService, // + actionMapper); + } + + @DisplayName("toDto() should map a KeyResultMetric to a Dto") + @Test + void toDtoShouldMapKeyResultMetricToDtoWithCheckIn() { + // arrange + CheckIn checkIn = TestDataHelper.checkInMetric(); + when(checkInBusinessService.getLastCheckInByKeyResultId(anyLong())).thenReturn(checkIn); + + KeyResultMetric keyResultMetric = keyResultMetric(); + List actions = List.of(action(keyResultMetric)); + + // act + // TODO naming toKeyResultMetricDto() -> toDto() + KeyResultDto keyResultDto = keyResultMetricMapper.toKeyResultMetricDto(keyResultMetric, actions); + KeyResultMetricDto keyResultMetricDto = (KeyResultMetricDto) keyResultDto; + + // assert + assertNotNull(keyResultMetricDto); + assertKeyResultMetricDtoWithCheckIn(keyResultMetric, keyResultMetricDto, actions); + } + + @DisplayName("toDto() should map KeyResultMetric to a Dto without CheckIn") + @Test + void toDtoShouldMapKeyResultMetricToDtoWithoutCheckIn() { + // arrange + KeyResultMetric keyResultMetric = keyResultMetric(); + List actions = List.of(action(keyResultMetric)); + + // act + // TODO naming toKeyResultMetricDto() -> toDto() + KeyResultDto keyResultDto = keyResultMetricMapper.toKeyResultMetricDto(keyResultMetric, actions); + KeyResultMetricDto keyResultMetricDto = (KeyResultMetricDto) keyResultDto; + + // assert + assertNotNull(keyResultMetricDto); + assertKeyResultMetricDtoWithoutCheckIn(keyResultMetric, keyResultMetricDto, actions); + } + + @DisplayName("toKeyResultMetric() should map Dto to KeyResultMetric") + @Test + void toKeyResultMetricShouldMapDtoToKeyResultMetric() { + // arrange + when(userBusinessService.getUserById(anyLong())).thenReturn(owner()); + when(objectiveBusinessService.getEntityById(anyLong())).thenReturn(objective()); + KeyResultMetricDto keyResultMetricDto = TestDataDtoHelper.keyResultMetricDto(); + + // act + KeyResult keyResult = keyResultMetricMapper.toKeyResultMetric(keyResultMetricDto); + KeyResultMetric keyResultMetric = (KeyResultMetric) keyResult; + + // assert + assertNotNull(keyResultMetric); + assertKeyResultMetric(keyResultMetricDto, keyResultMetric); + } +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultOrdinalMapperTest.java b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultOrdinalMapperTest.java new file mode 100644 index 0000000000..52d46ec183 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/KeyResultOrdinalMapperTest.java @@ -0,0 +1,118 @@ +package ch.puzzle.okr.mapper.keyresult; + +import ch.puzzle.okr.dto.keyresult.KeyResultDto; +import ch.puzzle.okr.dto.keyresult.KeyResultOrdinalDto; +import ch.puzzle.okr.mapper.ActionMapper; +import ch.puzzle.okr.mapper.keyresult.helper.TestDataDtoHelper; +import ch.puzzle.okr.mapper.keyresult.helper.TestDataHelper; +import ch.puzzle.okr.models.Action; +import ch.puzzle.okr.models.checkin.CheckIn; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.models.keyresult.KeyResultOrdinal; +import ch.puzzle.okr.service.business.CheckInBusinessService; +import ch.puzzle.okr.service.business.KeyResultBusinessService; +import ch.puzzle.okr.service.business.ObjectiveBusinessService; +import ch.puzzle.okr.service.business.UserBusinessService; +import ch.puzzle.okr.service.persistence.KeyResultPersistenceService; +import ch.puzzle.okr.service.validation.KeyResultValidationService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static ch.puzzle.okr.mapper.keyresult.helper.AssertHelper.*; +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataHelper.*; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class KeyResultOrdinalMapperTest { + + private KeyResultOrdinalMapper keyResultOrdinalMapper; + + @Mock + UserBusinessService userBusinessService; + @Mock + private ObjectiveBusinessService objectiveBusinessService; + @Mock + private CheckInBusinessService checkInBusinessService; + @InjectMocks + private ActionMapper actionMapper; + @Mock + private KeyResultBusinessService keyResultBusinessService; + @Mock + private KeyResultValidationService validator; + @Mock + private KeyResultPersistenceService keyResultPersistenceService; + + @BeforeEach + void setup() { + actionMapper = new ActionMapper(keyResultBusinessService); + keyResultOrdinalMapper = new KeyResultOrdinalMapper( // + userBusinessService, // + objectiveBusinessService, // + checkInBusinessService, // + actionMapper); + } + + @DisplayName("toDto() should map a KeyResultOrdinal with CheckIn to a Dto") + @Test + void toDtoShouldMapKeyResultWithCheckInOrdinalToDto() { + // arrange + CheckIn checkIn = TestDataHelper.checkInOrdinal(); + when(checkInBusinessService.getLastCheckInByKeyResultId(anyLong())).thenReturn(checkIn); + + KeyResultOrdinal keyResultOrdinal = keyResultOrdinal(); + List actions = List.of(action(keyResultOrdinal)); + + // act + // TODO naming toKeyResultOrdinalDto() -> toDto() + KeyResultDto keyResultDto = keyResultOrdinalMapper.toKeyResultOrdinalDto(keyResultOrdinal, actions); + KeyResultOrdinalDto keyResultOrdinalDto = (KeyResultOrdinalDto) keyResultDto; + + // assert + assertNotNull(keyResultOrdinalDto); + assertKeyResultOrdinalDtoWithCheckIn(keyResultOrdinal, keyResultOrdinalDto, actions); + } + + @DisplayName("toDto() should map a KeyResultOrdinal without CheckIn to a Dto") + @Test + void toDtoShouldMapKeyResultWithoutCheckInOrdinalToDto() { + // arrange + KeyResultOrdinal keyResultOrdinal = keyResultOrdinal(); + List actions = List.of(action(keyResultOrdinal)); + + // act + // TODO naming toKeyResultOrdinalDto() -> toDto() + KeyResultDto keyResultDto = keyResultOrdinalMapper.toKeyResultOrdinalDto(keyResultOrdinal, actions); + KeyResultOrdinalDto keyResultOrdinalDto = (KeyResultOrdinalDto) keyResultDto; + + // assert + assertNotNull(keyResultOrdinalDto); + assertKeyResultOrdinalDtoWithoutCheckIn(keyResultOrdinal, keyResultOrdinalDto, actions); + } + + @DisplayName("toKeyResultMetric() should map Dto to KeyResultMetric") + @Test + void toKeyResultMetricShouldMapDtoToKeyResultMetric() { + // arrange + when(userBusinessService.getUserById(anyLong())).thenReturn(owner()); + when(objectiveBusinessService.getEntityById(anyLong())).thenReturn(objective()); + KeyResultOrdinalDto keyResultOrdinalDto = TestDataDtoHelper.keyResultOrdinalDto(); + + // act + KeyResult keyResult = keyResultOrdinalMapper.toKeyResultOrdinal(keyResultOrdinalDto); + KeyResultOrdinal keyResultOrdinal = (KeyResultOrdinal) keyResult; + + // assert + assertNotNull(keyResultOrdinal); + assertKeyResultOrdinal(keyResultOrdinalDto, keyResultOrdinal); + } + +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/AssertHelper.java b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/AssertHelper.java new file mode 100644 index 0000000000..acd12da152 --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/AssertHelper.java @@ -0,0 +1,194 @@ +package ch.puzzle.okr.mapper.keyresult.helper; + +import ch.puzzle.okr.dto.ActionDto; +import ch.puzzle.okr.dto.keyresult.*; +import ch.puzzle.okr.models.Action; +import ch.puzzle.okr.models.Objective; +import ch.puzzle.okr.models.Quarter; +import ch.puzzle.okr.models.User; +import ch.puzzle.okr.models.keyresult.KeyResultMetric; +import ch.puzzle.okr.models.keyresult.KeyResultOrdinal; + +import java.util.List; + +import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_METRIC; +import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_ORDINAL; +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataDtoHelper.lastCheckInMetricDto; +import static org.junit.jupiter.api.Assertions.*; + +public class AssertHelper { + + public static void assertKeyResultMetricDtoWithCheckIn(KeyResultMetric expected, KeyResultMetricDto actual, + List actions) { + assertKeyResultMetricDto(expected, actual, actions); + // checkIn is only available on KeyResultMetricDto (and not on KeyResultMetric). + // So check actual against the checkInMetricDto from TestDataDtoHelper + assertNotNull(actual.lastCheckIn()); + assertCheckInMetricDto(lastCheckInMetricDto(), actual.lastCheckIn()); + } + + public static void assertKeyResultMetricDtoWithoutCheckIn(KeyResultMetric expected, KeyResultMetricDto actual, + List actions) { + assertKeyResultMetricDto(expected, actual, actions); + assertNull(actual.lastCheckIn()); + } + + private static void assertKeyResultMetricDto(KeyResultMetric expected, KeyResultMetricDto actual, + List actions) { + assertEquals(KEY_RESULT_TYPE_METRIC, actual.keyResultType()); + assertEquals(expected.getKeyResultType(), actual.keyResultType()); + + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getTitle(), actual.title()); + assertEquals(expected.getDescription(), actual.description()); + assertEquals(expected.getBaseline(), actual.baseline()); + assertEquals(expected.getStretchGoal(), actual.stretchGoal()); + assertEquals(expected.getUnit(), actual.unit()); + assertOwnerDto(expected.getOwner(), actual.owner()); + assertObjectiveDto(expected.getObjective(), actual.objective()); + assertEquals(expected.getCreatedOn(), actual.createdOn()); + assertEquals(expected.getModifiedOn(), actual.modifiedOn()); + + assertEquals(1, actual.getActionList().size()); + assertActionDto(actions.get(0), actual.getActionList().get(0)); + } + + public static void assertKeyResultMetric(KeyResultMetricDto expected, KeyResultMetric actual) { + assertEquals(KEY_RESULT_TYPE_METRIC, actual.getKeyResultType()); + assertEquals(expected.keyResultType(), actual.getKeyResultType()); + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.version(), actual.getVersion()); + assertEquals(expected.title(), actual.getTitle()); + assertEquals(expected.description(), actual.getDescription()); + assertEquals(expected.baseline(), actual.getBaseline()); + assertEquals(expected.stretchGoal(), actual.getStretchGoal()); + assertEquals(expected.unit(), actual.getUnit()); + assertOwner(expected.owner(), actual.getOwner()); + assertObjective(expected.objective(), actual.getObjective()); + // assertEquals(expected.createdOn(), actual.getCreatedOn()); // TODO actual == null + assertEquals(expected.modifiedOn(), actual.getModifiedOn()); + } + + public static void assertKeyResultOrdinalDtoWithCheckIn(KeyResultOrdinal expected, KeyResultOrdinalDto actual, + List actions) { + assertKeyResultOrdinalDto(expected, actual, actions); + assertNotNull(actual.lastCheckIn()); + assertCheckInOrdinalDto(TestDataDtoHelper.lastCheckInOrdinalDto(), actual.lastCheckIn()); + } + + public static void assertKeyResultOrdinalDtoWithoutCheckIn(KeyResultOrdinal expected, KeyResultOrdinalDto actual, + List actions) { + assertKeyResultOrdinalDto(expected, actual, actions); + // checkIn is only available on KeyResultOrdinalDto (and not on KeyResultOrdinal). + // So check actual against the KeyResultOrdinalDto from TestDataDtoHelper + assertNull(actual.lastCheckIn()); + } + + private static void assertKeyResultOrdinalDto(KeyResultOrdinal expected, KeyResultOrdinalDto actual, + List actions) { + assertEquals(KEY_RESULT_TYPE_ORDINAL, actual.keyResultType()); + assertEquals(expected.getKeyResultType(), actual.keyResultType()); + + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getTitle(), actual.title()); + assertEquals(expected.getDescription(), actual.description()); + assertEquals(expected.getCommitZone(), actual.commitZone()); + assertEquals(expected.getTargetZone(), actual.targetZone()); + assertEquals(expected.getStretchZone(), actual.stretchZone()); + assertOwnerDto(expected.getOwner(), actual.owner()); + assertObjectiveDto(expected.getObjective(), actual.objective()); + assertEquals(expected.getCreatedOn(), actual.createdOn()); + assertEquals(expected.getModifiedOn(), actual.modifiedOn()); + + assertEquals(1, actual.getActionList().size()); + assertActionDto(actions.get(0), actual.getActionList().get(0)); + } + + public static void assertKeyResultOrdinal(KeyResultOrdinalDto expected, KeyResultOrdinal actual) { + assertEquals(KEY_RESULT_TYPE_ORDINAL, actual.getKeyResultType()); + assertEquals(expected.keyResultType(), actual.getKeyResultType()); + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.version(), actual.getVersion()); + assertEquals(expected.title(), actual.getTitle()); + assertEquals(expected.description(), actual.getDescription()); + assertEquals(expected.commitZone(), actual.getCommitZone()); + assertEquals(expected.targetZone(), actual.getTargetZone()); + assertEquals(expected.stretchZone(), actual.getStretchZone()); + assertOwner(expected.owner(), actual.getOwner()); + assertObjective(expected.objective(), actual.getObjective()); + // assertEquals(expected.createdOn(), actual.getCreatedOn()); // TODO actual == null + assertEquals(expected.modifiedOn(), actual.getModifiedOn()); + } + + private static void assertActionDto(Action expected, ActionDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getVersion(), actual.version()); + assertEquals(expected.getAction(), actual.action()); + assertEquals(expected.getPriority(), actual.priority()); + assertEquals(expected.isChecked(), actual.isChecked()); + assertEquals(expected.getKeyResult().getId(), actual.keyResultId()); + } + + private static void assertObjectiveDto(Objective expected, KeyResultObjectiveDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getState().name(), actual.state()); + assertQuarterDto(expected.getQuarter(), actual.keyResultQuarterDto()); + } + + private static void assertQuarterDto(Quarter expected, KeyResultQuarterDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getLabel(), actual.label()); + assertEquals(expected.getStartDate(), actual.startDate()); + assertEquals(expected.getEndDate(), actual.endDate()); + } + + private static void assertObjective(KeyResultObjectiveDto expected, Objective actual) { + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.state(), actual.getState().name()); + assertQuarter(expected.keyResultQuarterDto(), actual.getQuarter()); + } + + private static void assertQuarter(KeyResultQuarterDto expected, Quarter actual) { + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.label(), actual.getLabel()); + assertEquals(expected.startDate(), actual.getStartDate()); + assertEquals(expected.endDate(), actual.getEndDate()); + } + + private static void assertOwnerDto(User expected, KeyResultUserDto actual) { + assertEquals(expected.getId(), actual.id()); + assertEquals(expected.getFirstname(), actual.firstname()); + assertEquals(expected.getLastname(), actual.lastname()); + } + + private static void assertOwner(KeyResultUserDto expected, User actual) { + assertEquals(expected.id(), actual.getId()); + assertEquals(expected.firstname(), actual.getFirstname()); + assertEquals(expected.lastname(), actual.getLastname()); + } + + private static void assertCheckInMetricDto(KeyResultLastCheckInMetricDto expected, + KeyResultLastCheckInMetricDto actual) { + assertEquals(expected.id(), actual.id()); + assertEquals(expected.version(), actual.version()); + assertEquals(expected.value(), actual.value()); + assertEquals(expected.confidence(), actual.confidence()); + assertEquals(expected.createdOn(), actual.createdOn()); + assertEquals(expected.changeInfo(), actual.changeInfo()); + assertEquals(expected.initiatives(), actual.initiatives()); + } + + private static void assertCheckInOrdinalDto(KeyResultLastCheckInOrdinalDto expected, + KeyResultLastCheckInOrdinalDto actual) { + assertEquals(expected.id(), actual.id()); + assertEquals(expected.version(), actual.version()); + assertEquals(expected.value(), actual.value()); // TODO rename: value -> zone + assertEquals(expected.confidence(), actual.confidence()); + assertEquals(expected.createdOn(), actual.createdOn()); + assertEquals(expected.changeInfo(), actual.changeInfo()); + assertEquals(expected.initiatives(), actual.initiatives()); + } + +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataConstants.java b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataConstants.java new file mode 100644 index 0000000000..eff5381a5a --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataConstants.java @@ -0,0 +1,56 @@ +package ch.puzzle.okr.mapper.keyresult.helper; + +import ch.puzzle.okr.models.State; +import ch.puzzle.okr.models.Unit; +import ch.puzzle.okr.models.checkin.Zone; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Month; + +public class TestDataConstants { + + public static final long CHECK_IN_ID = 100L; + public static final int CHECK_IN_VERSION = 1; + public static final double CHECK_IN_METRIC_VALUE = 23; + public static final Zone CHECK_IN_ORDINAL_ZONE = Zone.STRETCH; + public static final int CHECK_IN_CONFIDENCE = 5; + public static final String CHECK_IN_CHANGE_INFO = "checkin changeinfo"; + public static final String CHECK_IN_INITIATIVES = "checkin initiaitves"; + public static final LocalDateTime CHECK_IN_CREATED_ON = LocalDateTime.of(2024, Month.MAY, 20, 0, 0, 0); + public static final LocalDateTime CHECK_IN_MODIFIED_ON = LocalDateTime.of(2024, Month.MAY, 20, 12, 0, 0); + public static final boolean CHECK_IN_IS_WRITABLE = true; + + public static final long USER_ID = 101L; + public static final String USER_FIRSTNAME = "owner firstname"; + public static final String USER_LASTNAME = "owner lastname"; + + public static final long QUARTER_ID = 102L; + public static final String QUARTER_LABEL = "GJ 2024 Q1"; + public static final LocalDate QUARTER_START_DATE = LocalDate.of(2024, Month.MAY, 20); + public static final LocalDate QUARTER_END_DATE = LocalDate.of(2024, Month.MAY, 21); + + public static final long OBJECTIVE_ID = 103L; + public static final State OBJECTIVE_STATE = State.ONGOING; + + public static final long KEY_RESULT_ID = 23L; + public static final int KEY_RESULT_VERSION = 1; + public static final String KEY_RESULT_TITLE = "keyresult title"; + public static final String KEY_RESULT_DESCRIPTION = "keyresult description"; + public static final double KEY_RESULT_BASELINE = 55D; + public static final double KEY_RESULT_STRETCH_GOAL = 80D; + public static final Unit KEY_RESULT_UNIT = Unit.NUMBER; + + public static final String KEY_RESULT_COMMIT_ZONE = "commit zone"; + public static final String KEY_RESULT_TARGET_ZONE = "target zone"; + public static final String KEY_RESULT_STRETCH_ZONE = "stretch zone"; + + public static final boolean KEY_RESULT_IS_WRITABLE = true; + + public static final long ACTION_ID = 104L; + public static final int ACTION_VERSION = 1; + public static final String ACTION_ACTION = "action"; + public static final int ACTION_PRIORITY = 2; + public static final boolean ACTION_IS_CHECKED = true; + +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataDtoHelper.java b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataDtoHelper.java new file mode 100644 index 0000000000..ed836d442b --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataDtoHelper.java @@ -0,0 +1,101 @@ +package ch.puzzle.okr.mapper.keyresult.helper; + +import ch.puzzle.okr.dto.keyresult.*; + +import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_METRIC; +import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_ORDINAL; +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataConstants.*; + +public class TestDataDtoHelper { + public static KeyResultMetricDto keyResultMetricDto() { + return keyResultMetricDto; + } + + public static KeyResultOrdinalDto keyResultOrdinalDto() { + return keyResultOrdinalDto; + } + + public static KeyResultLastCheckInMetricDto lastCheckInMetricDto() { + return lastCheckInMetricDto; + } + + public static KeyResultLastCheckInOrdinalDto lastCheckInOrdinalDto() { + return lastCheckInOrdinalDto; + } + + private static final KeyResultUserDto ownerDto = new KeyResultUserDto( // + USER_ID, // + USER_FIRSTNAME, // + USER_LASTNAME // + ); + + private static final KeyResultQuarterDto quarterDto = new KeyResultQuarterDto( // + QUARTER_ID, // + QUARTER_LABEL, // + QUARTER_START_DATE, // + QUARTER_END_DATE // + ); + + private static final KeyResultObjectiveDto objectiveDto = new KeyResultObjectiveDto( // + OBJECTIVE_ID, // + OBJECTIVE_STATE.name(), // + quarterDto // + ); + + private static final KeyResultLastCheckInMetricDto lastCheckInMetricDto = new KeyResultLastCheckInMetricDto( // + CHECK_IN_ID, // + CHECK_IN_VERSION, // + CHECK_IN_METRIC_VALUE, // + CHECK_IN_CONFIDENCE, // + CHECK_IN_CREATED_ON, // + CHECK_IN_CHANGE_INFO, // + CHECK_IN_INITIATIVES // + ); + + private static final KeyResultLastCheckInOrdinalDto lastCheckInOrdinalDto = new KeyResultLastCheckInOrdinalDto( // + CHECK_IN_ID, // + CHECK_IN_VERSION, // + CHECK_IN_ORDINAL_ZONE, // + CHECK_IN_CONFIDENCE, // + CHECK_IN_CREATED_ON, // + CHECK_IN_CHANGE_INFO, // + CHECK_IN_INITIATIVES // + ); + + private static final KeyResultMetricDto keyResultMetricDto = new KeyResultMetricDto( // + KEY_RESULT_ID, // + KEY_RESULT_VERSION, // + KEY_RESULT_TYPE_METRIC, // + KEY_RESULT_TITLE, // + KEY_RESULT_DESCRIPTION, // + KEY_RESULT_BASELINE, // + KEY_RESULT_STRETCH_GOAL, // + KEY_RESULT_UNIT, // + ownerDto, // + objectiveDto, // + lastCheckInMetricDto, // + CHECK_IN_CREATED_ON, // + CHECK_IN_MODIFIED_ON, // + CHECK_IN_IS_WRITABLE, // + null // actionList; ony used for keyResultMetric + action -> dto BUT NOT FOR dto -> keyResultMetric + ); + + private static final KeyResultOrdinalDto keyResultOrdinalDto = new KeyResultOrdinalDto( // + KEY_RESULT_ID, // + KEY_RESULT_VERSION, // + KEY_RESULT_TYPE_ORDINAL, // + KEY_RESULT_TITLE, // + KEY_RESULT_DESCRIPTION, // + KEY_RESULT_COMMIT_ZONE, // + KEY_RESULT_TARGET_ZONE, // + KEY_RESULT_STRETCH_ZONE, // + ownerDto, // + objectiveDto, // + lastCheckInOrdinalDto, // + CHECK_IN_CREATED_ON, // + CHECK_IN_MODIFIED_ON, // + CHECK_IN_IS_WRITABLE, // + null // actionList; ony used for keyResultOrdinal + action -> dto BUT NOT FOR dto -> keyResultOrdinal + ); + +} diff --git a/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataHelper.java b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataHelper.java new file mode 100644 index 0000000000..365999229f --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/mapper/keyresult/helper/TestDataHelper.java @@ -0,0 +1,137 @@ +package ch.puzzle.okr.mapper.keyresult.helper; + +import ch.puzzle.okr.models.Action; +import ch.puzzle.okr.models.Objective; +import ch.puzzle.okr.models.Quarter; +import ch.puzzle.okr.models.User; +import ch.puzzle.okr.models.checkin.CheckIn; +import ch.puzzle.okr.models.checkin.CheckInMetric; +import ch.puzzle.okr.models.checkin.CheckInOrdinal; +import ch.puzzle.okr.models.keyresult.KeyResult; +import ch.puzzle.okr.models.keyresult.KeyResultMetric; +import ch.puzzle.okr.models.keyresult.KeyResultOrdinal; + +import static ch.puzzle.okr.mapper.keyresult.helper.TestDataConstants.*; + +public class TestDataHelper { + + public static KeyResultMetric keyResultMetric() { + KeyResult keyResult = TestDataHelper.keyResultMetric; + keyResult.setWriteable(KEY_RESULT_IS_WRITABLE); + return (KeyResultMetric) keyResult; + } + + public static KeyResultOrdinal keyResultOrdinal() { + KeyResult keyResult = TestDataHelper.keyResultOrdinal; + keyResult.setWriteable(KEY_RESULT_IS_WRITABLE); + return (KeyResultOrdinal) keyResult; + } + + public static CheckInMetric checkInMetric() { + return (CheckInMetric) checkInMetric; + } + + public static CheckInOrdinal checkInOrdinal() { + return (CheckInOrdinal) checkInOrdinal; + } + + public static Action action(KeyResult keyResult) { + if (keyResult instanceof KeyResultMetric) + return actionForKeyResultMetric; + if (keyResult instanceof KeyResultOrdinal) + return actionForKeyResultOrdinal; + return null; + } + + public static User owner() { + return owner; + } + + public static Objective objective() { + return objective; + } + + private static final CheckIn checkInMetric = CheckInMetric.Builder.builder() // + .withId(CHECK_IN_ID) // + .withVersion(CHECK_IN_VERSION) // + .withValue(CHECK_IN_METRIC_VALUE) // + .withConfidence(CHECK_IN_CONFIDENCE) // + .withCreatedOn(CHECK_IN_CREATED_ON) // + .withChangeInfo(CHECK_IN_CHANGE_INFO) // + .withInitiatives(CHECK_IN_INITIATIVES) // + .build(); + + private static final CheckIn checkInOrdinal = CheckInOrdinal.Builder.builder() // + .withId(CHECK_IN_ID) // + .withVersion(CHECK_IN_VERSION) // + .withZone(CHECK_IN_ORDINAL_ZONE) // + .withConfidence(CHECK_IN_CONFIDENCE) // + .withCreatedOn(CHECK_IN_CREATED_ON) // + .withChangeInfo(CHECK_IN_CHANGE_INFO) // + .withInitiatives(CHECK_IN_INITIATIVES) // + .build(); + + private static final User owner = User.Builder.builder() // + .withId(USER_ID) // + .withFirstname(USER_FIRSTNAME) // + .withLastname(USER_LASTNAME) // + .build(); + + private static final Quarter quarter = Quarter.Builder.builder() // + .withId(QUARTER_ID) // + .withLabel(QUARTER_LABEL) // + .withStartDate(QUARTER_START_DATE) // + .withEndDate(QUARTER_END_DATE) // + .build(); + + private static final Objective objective = Objective.Builder.builder() // + .withId(OBJECTIVE_ID) // + .withQuarter(quarter) // + .withState(OBJECTIVE_STATE) // + .build(); + + private static final KeyResult keyResultMetric = KeyResultMetric.Builder.builder() // + .withId(KEY_RESULT_ID) // + .withVersion(KEY_RESULT_VERSION) // + .withTitle(KEY_RESULT_TITLE) // + .withDescription(KEY_RESULT_DESCRIPTION) // + .withBaseline(KEY_RESULT_BASELINE) // + .withStretchGoal(KEY_RESULT_STRETCH_GOAL) // + .withUnit(KEY_RESULT_UNIT) // + .withOwner(owner) // + .withObjective(objective) // + .withCreatedOn(CHECK_IN_CREATED_ON) // + .withModifiedOn(CHECK_IN_MODIFIED_ON) // + .build(); + + private static final KeyResult keyResultOrdinal = KeyResultOrdinal.Builder.builder() // + .withId(KEY_RESULT_ID) // + .withVersion(KEY_RESULT_VERSION) // + .withTitle(KEY_RESULT_TITLE) // + .withDescription(KEY_RESULT_DESCRIPTION) // + .withCommitZone(KEY_RESULT_COMMIT_ZONE).withTargetZone(KEY_RESULT_TARGET_ZONE) + .withStretchZone(KEY_RESULT_STRETCH_ZONE).withOwner(owner) // + .withObjective(objective) // + .withCreatedOn(CHECK_IN_CREATED_ON) // + .withModifiedOn(CHECK_IN_MODIFIED_ON) // + .build(); + + private static final Action actionForKeyResultMetric = Action.Builder.builder() // + .withId(ACTION_ID) // + .withVersion(ACTION_VERSION) // + .withAction(ACTION_ACTION) // + .withPriority(ACTION_PRIORITY) // + .withIsChecked(ACTION_IS_CHECKED) // + .withKeyResult(keyResultMetric) // + .build(); + + private static final Action actionForKeyResultOrdinal = Action.Builder.builder() // + .withId(ACTION_ID) // + .withVersion(ACTION_VERSION) // + .withAction(ACTION_ACTION) // + .withPriority(ACTION_PRIORITY) // + .withIsChecked(ACTION_IS_CHECKED) // + .withKeyResult(keyResultOrdinal) // + .build(); + +}