From 13090e532cb6970e9db305c6d486946a2c0f8744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20G=C3=A4chter?= Date: Thu, 3 Oct 2024 10:40:17 +0200 Subject: [PATCH] fix backend review items --- .../controller/ClientConfigController.java | 4 +- .../h2-db/data-test-h2/V100_0_0__TestData.sql | 24 ++++--- .../ObjectivePersistenceServiceIT.java | 9 +-- .../QuarterPersistenceServiceIT.java | 67 ++++++++++++++++--- 4 files changed, 78 insertions(+), 26 deletions(-) diff --git a/backend/src/main/java/ch/puzzle/okr/controller/ClientConfigController.java b/backend/src/main/java/ch/puzzle/okr/controller/ClientConfigController.java index abc24fe798..ec45e2f782 100644 --- a/backend/src/main/java/ch/puzzle/okr/controller/ClientConfigController.java +++ b/backend/src/main/java/ch/puzzle/okr/controller/ClientConfigController.java @@ -1,13 +1,13 @@ package ch.puzzle.okr.controller; -import ch.puzzle.okr.ForwardFilter; import ch.puzzle.okr.dto.ClientConfigDto; import ch.puzzle.okr.service.clientconfig.ClientConfigService; import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ClientConfigController { diff --git a/backend/src/main/resources/db/h2-db/data-test-h2/V100_0_0__TestData.sql b/backend/src/main/resources/db/h2-db/data-test-h2/V100_0_0__TestData.sql index 81e181ed52..a884cf56c3 100644 --- a/backend/src/main/resources/db/h2-db/data-test-h2/V100_0_0__TestData.sql +++ b/backend/src/main/resources/db/h2-db/data-test-h2/V100_0_0__TestData.sql @@ -33,17 +33,21 @@ VALUES (1, 1, 'peggimann@puzzle.ch', 'Paco', 'Eggimann', FALSE), (51, 1, 'papierer@puzzle.ch', 'Robin', 'Papierer', FALSE), (61, 1, 'gl@gl.com', 'Jaya', 'Norris', TRUE); +CREATE ALIAS INIT_QUARTER_DATA FOR 'ch.puzzle.okr.util.quarter.generate.h2.QuarterFunction.initQuarterData'; +CREATE ALIAS CURRENT_QUARTER_LABEL FOR 'ch.puzzle.okr.util.quarter.generate.h2.QuarterFunction.currentQuarterLabel'; +CREATE ALIAS CURRENT_QUARTER_START_DATE FOR 'ch.puzzle.okr.util.quarter.generate.h2.QuarterFunction.currentQuarterStartDate'; +CREATE ALIAS CURRENT_QUARTER_END_DATE FOR 'ch.puzzle.okr.util.quarter.generate.h2.QuarterFunction.currentQuarterEndDate'; +CREATE ALIAS NEXT_QUARTER_LABEL FOR 'ch.puzzle.okr.util.quarter.generate.h2.QuarterFunction.nextQuarterLabel'; +CREATE ALIAS NEXT_QUARTER_START_DATE FOR 'ch.puzzle.okr.util.quarter.generate.h2.QuarterFunction.nextQuarterStartDate'; +CREATE ALIAS NEXT_QUARTER_END_DATE FOR 'ch.puzzle.okr.util.quarter.generate.h2.QuarterFunction.nextQuarterEndDate'; + +call INIT_QUARTER_DATA(); + insert into quarter (id, label, start_date, end_date) -values (1, 'GJ 22/23-Q4', '2023-04-01', '2023-06-30'), - (2, 'GJ 23/24-Q1', '2023-07-01', '2023-09-30'), - (3, 'GJ 22/23-Q3', '2023-01-01', '2023-03-31'), - (4, 'GJ 22/23-Q2', '2022-10-01', '2022-12-31'), - (5, 'GJ 22/23-Q1', '2022-07-01', '2022-09-30'), - (6, 'GJ 21/22-Q4', '2022-04-01', '2022-06-30'), - (7, 'GJ 23/24-Q2', '2023-10-01', '2023-12-31'), - (8, 'GJ 23/24-Q3', '2024-01-01', '2024-03-31'), - (9, 'GJ 24/25-Q2', '2024-10-01', '2024-12-31'), - (199, 'Backlog', null, null); +values (2, CURRENT_QUARTER_LABEL(), CURRENT_QUARTER_START_DATE(), CURRENT_QUARTER_END_DATE()), + (3, NEXT_QUARTER_LABEL(), NEXT_QUARTER_START_DATE(), NEXT_QUARTER_END_DATE()), + (99, 'GJ ForTests', '2000-07-01', '2000-09-30'), + (999, 'Backlog', null, null); insert into team (id, version, name) values (4, 1, '/BBT'), diff --git a/backend/src/test/java/ch/puzzle/okr/service/persistence/ObjectivePersistenceServiceIT.java b/backend/src/test/java/ch/puzzle/okr/service/persistence/ObjectivePersistenceServiceIT.java index 7764d61b9b..f6fed4a394 100644 --- a/backend/src/test/java/ch/puzzle/okr/service/persistence/ObjectivePersistenceServiceIT.java +++ b/backend/src/test/java/ch/puzzle/okr/service/persistence/ObjectivePersistenceServiceIT.java @@ -1,12 +1,12 @@ package ch.puzzle.okr.service.persistence; -import ch.puzzle.okr.test.TestHelper; import ch.puzzle.okr.dto.ErrorDto; import ch.puzzle.okr.exception.OkrResponseStatusException; import ch.puzzle.okr.models.*; import ch.puzzle.okr.models.authorization.AuthorizationUser; import ch.puzzle.okr.multitenancy.TenantContext; import ch.puzzle.okr.test.SpringIntegrationTest; +import ch.puzzle.okr.test.TestHelper; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -29,6 +29,7 @@ class ObjectivePersistenceServiceIT { private static final String MODEL_WITH_ID_NOT_FOUND = "MODEL_WITH_ID_NOT_FOUND"; private static final String OBJECTIVE = "Objective"; private static final String ATTRIBUTE_NULL = "ATTRIBUTE_NULL"; + private static final long GJ_FOR_TESTS_QUARTER_ID = 99L; private final AuthorizationUser authorizationUser = defaultAuthorizationUser(); private Objective createdObjective; @@ -47,9 +48,9 @@ private static Objective createObjective(Long id, int version) { return Objective.Builder.builder().withId(id).withVersion(version).withTitle("title") .withCreatedBy(User.Builder.builder().withId(1L).build()) .withTeam(Team.Builder.builder().withId(5L).build()) - .withQuarter(Quarter.Builder.builder().withId(1L).build()).withDescription("This is our description") - .withState(State.DRAFT).withCreatedOn(LocalDateTime.MAX).withModifiedOn(LocalDateTime.MAX) - .withModifiedBy(User.Builder.builder().withId(1L).build()).build(); + .withQuarter(Quarter.Builder.builder().withId(GJ_FOR_TESTS_QUARTER_ID).build()) + .withDescription("This is our description").withState(State.DRAFT).withCreatedOn(LocalDateTime.MAX) + .withModifiedOn(LocalDateTime.MAX).withModifiedBy(User.Builder.builder().withId(1L).build()).build(); } @BeforeEach diff --git a/backend/src/test/java/ch/puzzle/okr/service/persistence/QuarterPersistenceServiceIT.java b/backend/src/test/java/ch/puzzle/okr/service/persistence/QuarterPersistenceServiceIT.java index 27978af948..589ea9e4c3 100644 --- a/backend/src/test/java/ch/puzzle/okr/service/persistence/QuarterPersistenceServiceIT.java +++ b/backend/src/test/java/ch/puzzle/okr/service/persistence/QuarterPersistenceServiceIT.java @@ -1,13 +1,15 @@ package ch.puzzle.okr.service.persistence; -import ch.puzzle.okr.test.TestHelper; import ch.puzzle.okr.dto.ErrorDto; import ch.puzzle.okr.exception.OkrResponseStatusException; import ch.puzzle.okr.models.Quarter; import ch.puzzle.okr.multitenancy.TenantContext; import ch.puzzle.okr.test.SpringIntegrationTest; +import ch.puzzle.okr.test.TestHelper; +import ch.puzzle.okr.util.quarter.check.QuarterRangeChecker; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -37,12 +39,12 @@ void tearDown() { @Test void shouldReturnSingleQuarterWhenFindingByValidId() { - Quarter returnedQuarter = quarterPersistenceService.findById(1L); + Quarter returnedQuarter = quarterPersistenceService.findById(99L); - assertEquals(1L, returnedQuarter.getId()); - assertEquals("GJ 22/23-Q4", returnedQuarter.getLabel()); - assertEquals(LocalDate.of(2023, 4, 1), returnedQuarter.getStartDate()); - assertEquals(LocalDate.of(2023, 6, 30), returnedQuarter.getEndDate()); + assertEquals(99L, returnedQuarter.getId()); + assertEquals("GJ ForTests", returnedQuarter.getLabel()); + assertEquals(LocalDate.of(2000, 7, 1), returnedQuarter.getStartDate()); + assertEquals(LocalDate.of(2000, 9, 30), returnedQuarter.getEndDate()); } @Test @@ -69,13 +71,26 @@ void shouldThrowExceptionWhenFindingQuarterWithIdNull() { assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason())); } + @DisplayName("getMostCurrentQuarters() should return current quarter and future quarter and GJForTests quarter") @Test - void shouldReturnCurrentQuarterFutureQuarterAnd4PastQuarters() { + void getMostCurrentQuartersShouldReturnCurrentQuarterAndFutureQuarterAndGJForTestsQuarter() { List quarterListFromFunction = quarterPersistenceService.getMostCurrentQuarters(); - assertEquals(6, quarterListFromFunction.size()); - assertTrue( - quarterListFromFunction.get(0).getStartDate().isAfter(quarterListFromFunction.get(5).getStartDate())); + assertEquals(3, quarterListFromFunction.size()); + assertGJForTestsQuarterIsFoundOnce(quarterListFromFunction); + assertCurrentQuarterIsFoundOnce(quarterListFromFunction); + } + + private void assertGJForTestsQuarterIsFoundOnce(List quarters) { + long foundGJForTestsQuartersCount = quarters.stream() + .filter(quarter -> quarter.getLabel().equals("GJ ForTests")).count(); + assertEquals(1, foundGJForTestsQuartersCount); + } + + private void assertCurrentQuarterIsFoundOnce(List quarters) { + long foundCurrentQuartersCount = quarters.stream() + .filter(quarter -> QuarterRangeChecker.nowIsInQuarter(LocalDate.now(), quarter)).count(); + assertEquals(1, foundCurrentQuartersCount); } @Test @@ -92,4 +107,36 @@ void shouldReturnCurrentQuarter() { assertNotNull(quarter.getLabel()); } + @DisplayName("findByLabel() should return single Quarter when label is valid") + @Test + void findByLabelShouldReturnSingleQuarterWhenLabelIsValid() { + // arrange + act + Quarter returnedQuarter = quarterPersistenceService.findByLabel("GJ ForTests"); + + // assert + assertEquals(99L, returnedQuarter.getId()); + assertEquals("GJ ForTests", returnedQuarter.getLabel()); + assertEquals(LocalDate.of(2000, 7, 1), returnedQuarter.getStartDate()); + assertEquals(LocalDate.of(2000, 9, 30), returnedQuarter.getEndDate()); + } + + @DisplayName("findByLabel() should return null when label is not valid") + @Test + void findByLabelShouldReturnNullWhenLabelIsNotValid() { + // arrange + act + Quarter returnedQuarter = quarterPersistenceService.findByLabel("a_not_valid_label"); + + // assert + assertNull(returnedQuarter); + } + + @DisplayName("findByLabel() should return null when label is null") + @Test + void findByLabelShouldReturnNullWhenLabelIsNull() { + // arrange + act + Quarter returnedQuarter = quarterPersistenceService.findByLabel(null); + + // assert + assertNull(returnedQuarter); + } }