-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update dependency org.springframework.boot:spring-boot-starter-parent…
… to v3.4.0 (#1194) * Update dependency org.springframework.boot:spring-boot-starter-parent to v3.4.0 * improve security filter chain * Replace sout with logger * Reset id before recreating an entity, making it effectively a new entity * Remove old debug statements * Fix unit tests * Update alignment when key result changes type * Run formatter * Add missing mocks for unit test * Add tests for AlignmentValidationService * Add tests for KeyResult * Add tests for AlignmentBusinessService * Remove unused import * Add E2E test to check if keyResultType can not be changed after a checkin --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: peggimann <[email protected]> Co-authored-by: Giannin <[email protected]>
- Loading branch information
1 parent
f999fa8
commit c146cfb
Showing
15 changed files
with
381 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
backend/src/main/java/ch/puzzle/okr/service/business/AlignmentBusinessService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ch.puzzle.okr.service.business; | ||
|
||
import ch.puzzle.okr.models.alignment.Alignment; | ||
import ch.puzzle.okr.models.alignment.KeyResultAlignment; | ||
import ch.puzzle.okr.models.keyresult.KeyResult; | ||
import ch.puzzle.okr.service.persistence.AlignmentPersistenceService; | ||
import ch.puzzle.okr.service.validation.AlignmentValidationService; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class AlignmentBusinessService { | ||
private final AlignmentPersistenceService alignmentPersistenceService; | ||
private final AlignmentValidationService validation; | ||
|
||
public AlignmentBusinessService(AlignmentPersistenceService alignmentPersistenceService, | ||
AlignmentValidationService validation) { | ||
this.alignmentPersistenceService = alignmentPersistenceService; | ||
this.validation = validation; | ||
} | ||
|
||
public Alignment updateEntity(Long id, Alignment entity) { | ||
validation.validateOnUpdate(id, entity); | ||
return alignmentPersistenceService.save(entity); | ||
} | ||
|
||
public void updateKeyResultId(Long oldId, KeyResult newKeyResult) { | ||
List<KeyResultAlignment> alignments = alignmentPersistenceService.findByKeyResultAlignmentId(oldId); | ||
|
||
alignments.forEach(a -> { | ||
a.setAlignmentTarget(newKeyResult); | ||
this.updateEntity(a.getId(), a); | ||
}); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/ch/puzzle/okr/service/validation/AlignmentValidationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ch.puzzle.okr.service.validation; | ||
|
||
import ch.puzzle.okr.models.alignment.Alignment; | ||
import ch.puzzle.okr.repository.AlignmentRepository; | ||
import ch.puzzle.okr.service.persistence.AlignmentPersistenceService; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AlignmentValidationService | ||
extends ValidationBase<Alignment, Long, AlignmentRepository, AlignmentPersistenceService> { | ||
|
||
AlignmentValidationService(AlignmentPersistenceService persistenceService) { | ||
super(persistenceService); | ||
} | ||
|
||
@Override | ||
public void validateOnCreate(Alignment model) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void validateOnUpdate(Long id, Alignment model) { | ||
throwExceptionWhenIdIsNull(id); | ||
throwExceptionWhenIdIsNull(model.getId()); | ||
throwExceptionWhenIdHasChanged(id, model.getId()); | ||
validate(model); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
backend/src/test/java/ch/puzzle/okr/models/keyresult/KeyResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ch.puzzle.okr.models.keyresult; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class KeyResultTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideKeyResults") | ||
void resetIdShouldSetIdToNull(KeyResult keyResult) { | ||
|
||
keyResult.resetId(); | ||
|
||
assertNull(keyResult.getId()); | ||
} | ||
|
||
private static Stream<Arguments> provideKeyResults() { | ||
return Stream.of(Arguments.of(KeyResultMetric.Builder.builder().withId(1L).build()), | ||
Arguments.of(KeyResultOrdinal.Builder.builder().withId(1L).build())); | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
backend/src/test/java/ch/puzzle/okr/service/business/AlignmentBusinessServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package ch.puzzle.okr.service.business; | ||
|
||
import ch.puzzle.okr.exception.OkrResponseStatusException; | ||
import ch.puzzle.okr.models.alignment.Alignment; | ||
import ch.puzzle.okr.models.alignment.KeyResultAlignment; | ||
import ch.puzzle.okr.models.keyresult.KeyResult; | ||
import ch.puzzle.okr.models.keyresult.KeyResultMetric; | ||
import ch.puzzle.okr.service.persistence.AlignmentPersistenceService; | ||
import ch.puzzle.okr.service.validation.AlignmentValidationService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
|
||
import javax.swing.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AlignmentBusinessServiceTest { | ||
@MockitoBean | ||
AlignmentPersistenceService alignmentPersistenceService = mock(AlignmentPersistenceService.class); | ||
@MockitoBean | ||
AlignmentValidationService alignmentValidationService = mock(AlignmentValidationService.class); | ||
|
||
KeyResult keyResult; | ||
List<KeyResultAlignment> alignments; | ||
|
||
@InjectMocks | ||
AlignmentBusinessService alignmentBusinessService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.keyResult = KeyResultMetric.Builder.builder().withId(1L).withBaseline(10.0) | ||
.withDescription("Awesome Keyresult").withStretchGoal(100.0).build(); | ||
|
||
this.alignments = new ArrayList<>(); | ||
this.alignments.add(KeyResultAlignment.Builder.builder().withId(12L).withTargetKeyResult(keyResult).build()); | ||
this.alignments.add(KeyResultAlignment.Builder.builder().withId(132L).withTargetKeyResult(keyResult).build()); | ||
this.alignments.add(KeyResultAlignment.Builder.builder().withId(9L).withTargetKeyResult(keyResult).build()); | ||
} | ||
|
||
@Test | ||
void updateEntityShouldThrowExceptionWhenValidationFails() { | ||
doThrow(new OkrResponseStatusException(HttpStatus.BAD_REQUEST, "Error Message")) | ||
.when(alignmentValidationService).validateOnUpdate(eq(1L), any(KeyResultAlignment.class)); | ||
|
||
assertThrows(OkrResponseStatusException.class, | ||
() -> alignmentBusinessService.updateEntity(1L, new KeyResultAlignment())); | ||
} | ||
|
||
@Test | ||
void updateEntityShouldSaveNewEntity() { | ||
Alignment mockedAlignment = mock(Alignment.class); | ||
when(alignmentPersistenceService.save(any(Alignment.class))).thenAnswer(i -> i.getArguments()[0]); | ||
|
||
Alignment alignment = alignmentBusinessService.updateEntity(1L, mockedAlignment); | ||
|
||
verify(alignmentPersistenceService, times(1)).save(mockedAlignment); | ||
assertEquals(mockedAlignment, alignment); | ||
} | ||
|
||
@Test | ||
void updateKeyResultIdShouldUpdateKeyResult() { | ||
KeyResult mockedKeyresult = mock(KeyResult.class); | ||
when(alignmentPersistenceService.findByKeyResultAlignmentId(1L)).thenReturn(this.alignments); | ||
|
||
alignmentBusinessService.updateKeyResultId(1L, mockedKeyresult); | ||
|
||
ArgumentCaptor<KeyResultAlignment> captor = ArgumentCaptor.forClass(KeyResultAlignment.class); | ||
verify(alignmentPersistenceService, times(3)).save(captor.capture()); | ||
captor.getAllValues().forEach(c -> assertEquals(c.getAlignmentTarget(), mockedKeyresult)); | ||
} | ||
|
||
@Test | ||
void updateKeyResultIdShouldUpdateNothingIfNoKeyResultAreFound() { | ||
KeyResult mockedKeyresult = mock(KeyResult.class); | ||
when(alignmentPersistenceService.findByKeyResultAlignmentId(1L)).thenReturn(List.of()); | ||
|
||
alignmentBusinessService.updateKeyResultId(1L, mockedKeyresult); | ||
|
||
verify(alignmentPersistenceService, never()).save(any()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.