Skip to content

Commit

Permalink
fix backend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jan 21, 2025
1 parent 2464067 commit 9088531
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package ch.puzzle.okr.dto.keyresult;

import ch.puzzle.okr.dto.ActionDto;
import ch.puzzle.okr.models.Unit;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.time.LocalDateTime;
import java.util.List;

@JsonDeserialize(as = KeyResultMetricDto.class)
public record KeyResultMetricDto(Long id, int version, String keyResultType, String title, String description,
Double baseline, Double stretchGoal, String unit, KeyResultUserDto owner, KeyResultObjectiveDto objective,
KeyResultLastCheckInMetricDto lastCheckIn, LocalDateTime createdOn, LocalDateTime modifiedOn,
boolean isWriteable, List<ActionDto> actionList) implements KeyResultDto {
Double baseline, Double stretchGoal, String unit, KeyResultUserDto owner, KeyResultObjectiveDto objective,
KeyResultLastCheckInMetricDto lastCheckIn, LocalDateTime createdOn, LocalDateTime modifiedOn,
boolean isWriteable, List<ActionDto> actionList) implements KeyResultDto {
@Override
public List<ActionDto> getActionList() {
return actionList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ch.puzzle.okr.dto.keyresult.*;
import ch.puzzle.okr.mapper.ActionMapper;
import ch.puzzle.okr.models.Action;
import ch.puzzle.okr.models.Unit;
import ch.puzzle.okr.models.checkin.CheckIn;
import ch.puzzle.okr.models.checkin.CheckInMetric;
import ch.puzzle.okr.models.keyresult.KeyResult;
Expand Down Expand Up @@ -35,7 +34,6 @@ public KeyResultMetricMapper(UserBusinessService userBusinessService,
this.unitBusinessService = unitBusinessService;
}


public KeyResultDto toDto(KeyResultMetric keyResult, List<Action> actionList) {
KeyResultUserDto ownerDto = new KeyResultUserDto( //
keyResult.getOwner().getId(), //
Expand Down
23 changes: 19 additions & 4 deletions backend/src/main/java/ch/puzzle/okr/models/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.Objects;

@Entity(name = "unit")
public class Unit {
Expand All @@ -11,6 +12,19 @@ public class Unit {
@SequenceGenerator(name = "sequence_unit", allocationSize = 1)
private Long id;

@Override
public boolean equals(Object o) {
if (!(o instanceof Unit unit)) {
return false;
}
return Objects.equals(getUnitName(), unit.getUnitName());
}

@Override
public int hashCode() {
return Objects.hashCode(getUnitName());
}

@Version
private int version;

Expand Down Expand Up @@ -64,13 +78,14 @@ private Unit(Builder builder) {
setCreatedBy(builder.createdBy);
}


public static final class Builder {
private Long id;
private int version;
private @NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL)
@Size(max = 4096, min = 3, message = MessageKey.ATTRIBUTE_SIZE_BETWEEN) String unitName;
private @NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL) User createdBy;
@Size(max = 4096, min = 3, message = MessageKey.ATTRIBUTE_SIZE_BETWEEN)
String unitName;
private @NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL)
User createdBy;

private Builder() {
}
Expand All @@ -90,7 +105,7 @@ public Builder version(int val) {
}

public Builder unitName(@NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL)
@Size(max = 4096, min = 3, message = MessageKey.ATTRIBUTE_SIZE_BETWEEN) String val) {
@Size(max = 4096, min = 3, message = MessageKey.ATTRIBUTE_SIZE_BETWEEN) String val) {
unitName = val;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static ch.puzzle.okr.Constants.KEY_RESULT_TYPE_METRIC;

import ch.puzzle.okr.models.Unit;
import ch.puzzle.okr.models.MessageKey;
import ch.puzzle.okr.models.Unit;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package ch.puzzle.okr.repository;

import ch.puzzle.okr.models.Unit;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UnitRepository extends CrudRepository<Unit, Long> {

Optional<Unit> findUnitByUnitName(String name);
Optional<Unit> findUnitByUnitName(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

import ch.puzzle.okr.ErrorKey;
import ch.puzzle.okr.exception.OkrResponseStatusException;
import ch.puzzle.okr.models.Action;
import ch.puzzle.okr.models.Unit;
import ch.puzzle.okr.models.keyresult.KeyResult;
import ch.puzzle.okr.service.persistence.ActionPersistenceService;
import ch.puzzle.okr.service.persistence.UnitPersistenceService;
import ch.puzzle.okr.service.validation.ActionValidationService;
import ch.puzzle.okr.service.validation.UnitValidationService;
import jakarta.transaction.Transactional;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class UnitBusinessService {
Expand All @@ -28,15 +21,16 @@ public UnitBusinessService(UnitPersistenceService unitPersistenceService, UnitVa
}

public Unit findUnitByName(String unitName) {
return unitPersistenceService.findUnitByUnitName(unitName).orElseThrow(() -> OkrResponseStatusException.of(ErrorKey.UNIT_NOT_FOUND, unitName));
return unitPersistenceService
.findUnitByUnitName(unitName)
.orElseThrow(() -> OkrResponseStatusException.of(ErrorKey.UNIT_NOT_FOUND, unitName));
}

public Unit getEntityById(Long id) {
validator.validateOnGet(id);
return unitPersistenceService.findById(id);
}


@Transactional
public Unit createEntity(Unit action) {
validator.validateOnCreate(action);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package ch.puzzle.okr.service.persistence;

import ch.puzzle.okr.models.Action;
import static ch.puzzle.okr.Constants.UNIT;

import ch.puzzle.okr.models.Unit;
import ch.puzzle.okr.repository.ActionRepository;
import ch.puzzle.okr.repository.UnitRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

import static ch.puzzle.okr.Constants.ACTION;
import static ch.puzzle.okr.Constants.UNIT;
import org.springframework.stereotype.Service;

@Service
public class UnitPersistenceService extends PersistenceBase<Unit, Long, UnitRepository> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ch.puzzle.okr.models.checkin.Zone;
import ch.puzzle.okr.test.CheckInTestHelpers;
import ch.puzzle.okr.test.KeyResultTestHelpers;
import ch.puzzle.okr.test.TestHelper;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
Expand Down Expand Up @@ -171,7 +172,7 @@ private void assertKeyResultMetricDto(KeyResultMetricDto keyResultMetricDto) {
assertEquals("BESCHREIBUNG", keyResultMetricDto.description());
assertEquals(1.0, keyResultMetricDto.baseline());
assertEquals(5.0, keyResultMetricDto.stretchGoal());
assertEquals(Unit.NUMBER, keyResultMetricDto.unit());
assertEquals(TestHelper.NUMBER_UNIT.getUnitName(), keyResultMetricDto.unit());

KeyResultUserDto owner = keyResultMetricDto.owner();
assertOwner(owner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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.UnitBusinessService;
import ch.puzzle.okr.service.business.UserBusinessService;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -42,13 +43,17 @@ class KeyResultMapperTest {
@InjectMocks
private ActionMapper actionMapper;

@Mock
UnitBusinessService unitBusinessService;

@BeforeEach
void setup() {
KeyResultMetricMapper keyResultMetricMapper = new KeyResultMetricMapper( //
userBusinessService, //
objectiveBusinessService, //
checkInBusinessService, //
actionMapper);
actionMapper,
unitBusinessService);

KeyResultOrdinalMapper keyResultOrdinalMapper = new KeyResultOrdinalMapper( //
userBusinessService, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
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.business.*;
import ch.puzzle.okr.service.persistence.KeyResultPersistenceService;
import ch.puzzle.okr.service.validation.KeyResultValidationService;
import ch.puzzle.okr.test.TestHelper;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -48,6 +46,8 @@ class KeyResultMetricMapperTest {
private KeyResultValidationService validator;
@Mock
private KeyResultPersistenceService keyResultPersistenceService;
@Mock
private UnitBusinessService unitBusinessService;

@BeforeEach
void setup() {
Expand All @@ -56,7 +56,8 @@ void setup() {
userBusinessService, //
objectiveBusinessService, //
checkInBusinessService, //
actionMapper);
actionMapper,
unitBusinessService);
}

@DisplayName("Should map a KeyResultMetric to a Dto with CheckIn when toDto() is called")
Expand Down Expand Up @@ -100,6 +101,7 @@ void shouldMapDtoToKeyResultMetricWhenToKeyResultMetricIsCalled() {
// arrange
when(userBusinessService.getUserById(anyLong())).thenReturn(owner());
when(objectiveBusinessService.getEntityById(anyLong())).thenReturn(objective());
when(unitBusinessService.findUnitByName("NUMBER")).thenReturn(TestHelper.NUMBER_UNIT);
KeyResultMetricDto keyResultMetricDto = TestDataDtoHelper.keyResultMetricDto();

// act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static void assertKeyResultMetricDto(KeyResultMetric expected, KeyResult
assertEquals(expected.getDescription(), actual.description());
assertEquals(expected.getBaseline(), actual.baseline());
assertEquals(expected.getStretchGoal(), actual.stretchGoal());
assertEquals(expected.getUnit(), actual.unit());
assertEquals(expected.getUnit().getUnitName(), actual.unit());
assertOwnerDto(expected.getOwner(), actual.owner());
assertObjectiveDto(expected.getObjective(), actual.objective());
assertEquals(expected.getCreatedOn(), actual.createdOn());
Expand All @@ -62,7 +62,7 @@ public static void assertKeyResultMetric(KeyResultMetricDto expected, KeyResultM
assertEquals(expected.description(), actual.getDescription());
assertEquals(expected.baseline(), actual.getBaseline());
assertEquals(expected.stretchGoal(), actual.getStretchGoal());
assertEquals(expected.unit(), actual.getUnit());
assertEquals(expected.unit(), actual.getUnit().getUnitName());
assertOwner(expected.owner(), actual.getOwner());
assertObjective(expected.objective(), actual.getObjective());
assertEquals(expected.createdOn(), actual.getCreatedOn());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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 ch.puzzle.okr.test.TestHelper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
Expand Down Expand Up @@ -37,7 +39,7 @@ public class TestDataConstants {
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 Unit KEY_RESULT_UNIT = TestHelper.NUMBER_UNIT;

public static final String KEY_RESULT_COMMIT_ZONE = "commit zone";
public static final String KEY_RESULT_TARGET_ZONE = "target zone";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static KeyResultLastCheckInOrdinalDto lastCheckInOrdinalDto() {
KEY_RESULT_DESCRIPTION, //
KEY_RESULT_BASELINE, //
KEY_RESULT_STRETCH_GOAL, //
KEY_RESULT_UNIT, //
KEY_RESULT_UNIT.getUnitName(), //
ownerDto, //
objectiveDto, //
lastCheckInMetricDto, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ch.puzzle.okr.models.keyresult.KeyResultMetric;
import ch.puzzle.okr.service.persistence.ActionPersistenceService;
import ch.puzzle.okr.service.validation.ActionValidationService;
import ch.puzzle.okr.test.TestHelper;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -32,7 +33,7 @@ class ActionBusinessServiceTest {
.builder()
.withBaseline(10D)
.withStretchGoal(50D)
.withUnit(Unit.CHF)
.withUnit(TestHelper.CHF_UNIT)
.withId(8L)
.withTitle("Keyresult Metric")
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.puzzle.okr.service.business;

import static ch.puzzle.okr.test.TestHelper.CHF_UNIT;
import static ch.puzzle.okr.test.TestHelper.defaultAuthorizationUser;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -53,7 +54,7 @@ class CheckInBusinessServiceTest {
.builder()
.withBaseline(10D)
.withStretchGoal(50D)
.withUnit(Unit.CHF)
.withUnit(CHF_UNIT)
.withId(8L)
.withTitle("Keyresult Metric")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.test.TestHelper.FTE_UNIT;
import static ch.puzzle.okr.test.TestHelper.defaultAuthorizationUser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -59,7 +60,7 @@ private static KeyResult createKeyResultMetric(Long id) {
.builder()
.withBaseline(3.0)
.withStretchGoal(5.0)
.withUnit(Unit.FTE)
.withUnit(FTE_UNIT)
.withId(id)
.withTitle("Title")
.withCreatedBy(User.Builder.builder().withId(1L).build())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch.puzzle.okr.service.business;

import static ch.puzzle.okr.test.TestHelper.defaultAuthorizationUser;
import static ch.puzzle.okr.test.TestHelper.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -482,7 +482,7 @@ void shouldSuccessfullyDuplicateMetricKeyResult() {
.withTitle("Metric KeyResult")
.withDescription("Description of metric key result")
.withOwner(User.Builder.builder().build())
.withUnit(Unit.NUMBER)
.withUnit(NUMBER_UNIT)
.withBaseline(10.0)
.withStretchGoal(50.0)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.puzzle.okr.service.business;

import static ch.puzzle.okr.models.State.DRAFT;
import static ch.puzzle.okr.test.TestHelper.FTE_UNIT;
import static ch.puzzle.okr.test.TestHelper.defaultAuthorizationUser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -220,7 +221,7 @@ void shouldDuplicateObjective() {
.withId(2L) //
.withTitle("Metric 1") //
.withObjective(sourceObjective) //
.withUnit(Unit.FTE) //
.withUnit(FTE_UNIT) //
.build();

List<KeyResult> keyResults = new ArrayList<>();
Expand Down
Loading

0 comments on commit 9088531

Please sign in to comment.