Skip to content

Commit

Permalink
tests for mapper package with 100% line coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed May 24, 2024
1 parent f198972 commit ed437d3
Show file tree
Hide file tree
Showing 20 changed files with 1,896 additions and 0 deletions.
159 changes: 159 additions & 0 deletions backend/src/test/java/ch/puzzle/okr/mapper/ActionMapperTest.java
Original file line number Diff line number Diff line change
@@ -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<ActionDto> actionDtoList = List.of(actionDto);
List<Action> 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<ActionDto> actionDtoList = List.of(actionDtoWithKeyResultIdIsNull);
List<Action> 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<ActionDto> expectedDtoList, List<Action> actualList) {
assertListsAndFirstAction(expectedDtoList, actualList);
assertEquals(expectedDtoList.get(0).keyResultId(), actualList.get(0).getKeyResult().getId());
}

private void assertListOfActionsAndKeyResultParameter(List<ActionDto> expectedDtoList, List<Action> actualList,
Long keyResultId) {
assertListsAndFirstAction(expectedDtoList, actualList);
assertEquals(keyResultId, actualList.get(0).getKeyResult().getId());
}

private void assertListsAndFirstAction(List<ActionDto> expectedDtoList, List<Action> 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?
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Loading

0 comments on commit ed437d3

Please sign in to comment.