From 81b8e721feefacee0a436804bba955dd57b5130f Mon Sep 17 00:00:00 2001 From: davdarras Date: Mon, 30 Sep 2024 10:59:36 +0200 Subject: [PATCH] fix: create ready com request status when creaing com requests --- pom.xml | 2 +- .../impl/SurveyUnitUpdateServiceImpl.java | 5 ++++- .../dto/CommunicationRequestCreateDto.java | 5 +++-- .../communication/CommunicationRequest.java | 8 +++++--- .../CommunicationRequestStatus.java | 5 +++-- .../api/authKeycloak/TestAuthKeyCloak.java | 4 ++-- .../insee/pearljam/api/noAuth/TestNoAuth.java | 4 ++-- ...ava => FixedDateServiceConfiguration.java} | 2 +- .../model/CommunicationRequestTest.java | 6 ++++-- .../SurveyUnitUpdateServiceImplTest.java | 16 ++++++++++++--- .../integration/campaign/CampaignIT.java | 2 +- .../campaign/CommunicationInformationIT.java | 2 +- .../campaign/CommunicationTemplateIT.java | 2 +- .../integration/campaign/VisibilityIT.java | 2 +- .../integration/surveyunit/SurveyUnitIT.java | 20 ++++++++++++++++++- 15 files changed, 61 insertions(+), 24 deletions(-) rename src/test/java/fr/insee/pearljam/config/{DateServiceConfiguration.java => FixedDateServiceConfiguration.java} (92%) diff --git a/pom.xml b/pom.xml index cfab81e6..707cf2ed 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ fr.insee.pearljam pearljam-back-office - 5.1.2 + 5.1.3 Pearl-Jam-Back-Office Back-office services for PearlJam diff --git a/src/main/java/fr/insee/pearljam/api/service/impl/SurveyUnitUpdateServiceImpl.java b/src/main/java/fr/insee/pearljam/api/service/impl/SurveyUnitUpdateServiceImpl.java index e89b59eb..bdc5df97 100644 --- a/src/main/java/fr/insee/pearljam/api/service/impl/SurveyUnitUpdateServiceImpl.java +++ b/src/main/java/fr/insee/pearljam/api/service/impl/SurveyUnitUpdateServiceImpl.java @@ -5,6 +5,7 @@ import fr.insee.pearljam.api.surveyunit.dto.CommunicationRequestCreateDto; import fr.insee.pearljam.api.surveyunit.dto.IdentificationDto; import fr.insee.pearljam.api.surveyunit.dto.SurveyUnitUpdateDto; +import fr.insee.pearljam.domain.campaign.port.userside.DateService; import fr.insee.pearljam.domain.surveyunit.model.Comment; import fr.insee.pearljam.domain.surveyunit.model.Identification; import fr.insee.pearljam.domain.surveyunit.model.communication.CommunicationRequest; @@ -24,6 +25,7 @@ public class SurveyUnitUpdateServiceImpl implements SurveyUnitUpdateService { private final CommunicationRequestRepository communicationRequestRepository; + private final DateService dateService; @Transactional @Override @@ -36,7 +38,8 @@ public void updateSurveyUnitInfos(SurveyUnit surveyUnit, SurveyUnitUpdateDto sur surveyUnit.updateComments(commentsToUpdate); } if(surveyUnitUpdateDto.communicationRequests() != null) { - List communicationRequestsToCreate = CommunicationRequestCreateDto.toModel(surveyUnitUpdateDto.communicationRequests()); + Long timestamp = dateService.getCurrentTimestamp(); + List communicationRequestsToCreate = CommunicationRequestCreateDto.toModel(surveyUnitUpdateDto.communicationRequests(), timestamp); communicationRequestRepository.addCommunicationRequests(surveyUnit, communicationRequestsToCreate); } diff --git a/src/main/java/fr/insee/pearljam/api/surveyunit/dto/CommunicationRequestCreateDto.java b/src/main/java/fr/insee/pearljam/api/surveyunit/dto/CommunicationRequestCreateDto.java index 68c4de22..e2279fc0 100644 --- a/src/main/java/fr/insee/pearljam/api/surveyunit/dto/CommunicationRequestCreateDto.java +++ b/src/main/java/fr/insee/pearljam/api/surveyunit/dto/CommunicationRequestCreateDto.java @@ -28,12 +28,13 @@ public record CommunicationRequestCreateDto( * Converts a list of communication request DTOs into a list of communication request models. * * @param requests the list of communication request DTOs + * @param readyTimestamp ready timestamp of the communication requests (when does the communication request are created in the back) * @return the list of communication request models */ - public static List toModel(List requests) { + public static List toModel(List requests, Long readyTimestamp) { return requests.stream() .map(request -> - CommunicationRequest.create(request.communicationTemplateId(), request.creationTimestamp(), request.reason())) + CommunicationRequest.create(request.communicationTemplateId(), request.creationTimestamp(), readyTimestamp, request.reason())) .toList(); } } diff --git a/src/main/java/fr/insee/pearljam/domain/surveyunit/model/communication/CommunicationRequest.java b/src/main/java/fr/insee/pearljam/domain/surveyunit/model/communication/CommunicationRequest.java index 7116f3f9..548c853c 100644 --- a/src/main/java/fr/insee/pearljam/domain/surveyunit/model/communication/CommunicationRequest.java +++ b/src/main/java/fr/insee/pearljam/domain/surveyunit/model/communication/CommunicationRequest.java @@ -13,13 +13,15 @@ public record CommunicationRequest( /** * Create a communication request for messhugah * @param communicationTemplateId communication configuration id - * @param creationTimestamp creation date of the communication request + * @param creationTimestamp creation date of the communication request (coming from the front) + * @param readyTimestamp ready timestamp of the communication request (when does the communication request is created in the back) * @param reason reason why the communication request is created * @return {@link CommunicationRequest} communication request object */ - public static CommunicationRequest create(Long communicationTemplateId, Long creationTimestamp, CommunicationRequestReason reason) { + public static CommunicationRequest create(Long communicationTemplateId, Long creationTimestamp, Long readyTimestamp, CommunicationRequestReason reason) { List status = new ArrayList<>(); - status.add(CommunicationRequestStatus.create(creationTimestamp)); + status.add(CommunicationRequestStatus.create(creationTimestamp, CommunicationStatusType.INITIATED)); + status.add(CommunicationRequestStatus.create(readyTimestamp, CommunicationStatusType.READY)); return new CommunicationRequest(null, communicationTemplateId, reason, CommunicationRequestEmitter.INTERVIEWER, status); } } diff --git a/src/main/java/fr/insee/pearljam/domain/surveyunit/model/communication/CommunicationRequestStatus.java b/src/main/java/fr/insee/pearljam/domain/surveyunit/model/communication/CommunicationRequestStatus.java index 6d02ac84..5520d32c 100644 --- a/src/main/java/fr/insee/pearljam/domain/surveyunit/model/communication/CommunicationRequestStatus.java +++ b/src/main/java/fr/insee/pearljam/domain/surveyunit/model/communication/CommunicationRequestStatus.java @@ -8,9 +8,10 @@ public record CommunicationRequestStatus( /** * Create a CommunicationRequestStatus * @param timestamp status creation date + * @param statusType status type * @return {@link CommunicationRequestStatus} communication request status object */ - public static CommunicationRequestStatus create(Long timestamp) { - return new CommunicationRequestStatus(null, timestamp, CommunicationStatusType.INITIATED); + public static CommunicationRequestStatus create(Long timestamp, CommunicationStatusType statusType) { + return new CommunicationRequestStatus(null, timestamp, statusType); } } diff --git a/src/test/java/fr/insee/pearljam/api/authKeycloak/TestAuthKeyCloak.java b/src/test/java/fr/insee/pearljam/api/authKeycloak/TestAuthKeyCloak.java index 7f0ef5e0..eff2c46b 100644 --- a/src/test/java/fr/insee/pearljam/api/authKeycloak/TestAuthKeyCloak.java +++ b/src/test/java/fr/insee/pearljam/api/authKeycloak/TestAuthKeyCloak.java @@ -25,7 +25,7 @@ import fr.insee.pearljam.api.utils.AuthenticatedUserTestHelper; import fr.insee.pearljam.api.utils.MockMvcTestUtils; import fr.insee.pearljam.api.utils.ScriptConstants; -import fr.insee.pearljam.config.DateServiceConfiguration; +import fr.insee.pearljam.config.FixedDateServiceConfiguration; import fr.insee.pearljam.domain.surveyunit.model.CommentType; import fr.insee.pearljam.infrastructure.campaign.entity.VisibilityDB; import fr.insee.pearljam.infrastructure.campaign.jpa.VisibilityJpaRepository; @@ -77,7 +77,7 @@ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) @RequiredArgsConstructor -@Import(DateServiceConfiguration.class) +@Import(FixedDateServiceConfiguration.class) class TestAuthKeyCloak { private final SurveyUnitService surveyUnitService; diff --git a/src/test/java/fr/insee/pearljam/api/noAuth/TestNoAuth.java b/src/test/java/fr/insee/pearljam/api/noAuth/TestNoAuth.java index 69d79578..048ef24b 100644 --- a/src/test/java/fr/insee/pearljam/api/noAuth/TestNoAuth.java +++ b/src/test/java/fr/insee/pearljam/api/noAuth/TestNoAuth.java @@ -8,7 +8,7 @@ import fr.insee.pearljam.api.repository.ClosingCauseRepository; import fr.insee.pearljam.api.repository.MessageRepository; import fr.insee.pearljam.api.utils.ScriptConstants; -import fr.insee.pearljam.config.DateServiceConfiguration; +import fr.insee.pearljam.config.FixedDateServiceConfiguration; import lombok.RequiredArgsConstructor; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Test; @@ -40,7 +40,7 @@ @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) @RequiredArgsConstructor @TestMethodOrder(MethodOrderer.OrderAnnotation.class) -@Import(DateServiceConfiguration.class) +@Import(FixedDateServiceConfiguration.class) class TestNoAuth { private final MessageRepository messageRepository; diff --git a/src/test/java/fr/insee/pearljam/config/DateServiceConfiguration.java b/src/test/java/fr/insee/pearljam/config/FixedDateServiceConfiguration.java similarity index 92% rename from src/test/java/fr/insee/pearljam/config/DateServiceConfiguration.java rename to src/test/java/fr/insee/pearljam/config/FixedDateServiceConfiguration.java index e8c8cfd8..f406b871 100644 --- a/src/test/java/fr/insee/pearljam/config/DateServiceConfiguration.java +++ b/src/test/java/fr/insee/pearljam/config/FixedDateServiceConfiguration.java @@ -9,7 +9,7 @@ @TestConfiguration @Profile("test") -public class DateServiceConfiguration { +public class FixedDateServiceConfiguration { @Bean @Primary diff --git a/src/test/java/fr/insee/pearljam/domain/surveyunit/model/CommunicationRequestTest.java b/src/test/java/fr/insee/pearljam/domain/surveyunit/model/CommunicationRequestTest.java index 1db96216..84f40b34 100644 --- a/src/test/java/fr/insee/pearljam/domain/surveyunit/model/CommunicationRequestTest.java +++ b/src/test/java/fr/insee/pearljam/domain/surveyunit/model/CommunicationRequestTest.java @@ -13,10 +13,11 @@ void testCreateCommunicationRequest() { // Given Long communicationTemplateId = 1L; Long creationDate = System.currentTimeMillis(); + Long readyDate = System.currentTimeMillis(); CommunicationRequestReason reason = CommunicationRequestReason.REFUSAL; // When - CommunicationRequest communicationRequest = CommunicationRequest.create(communicationTemplateId, creationDate, reason); + CommunicationRequest communicationRequest = CommunicationRequest.create(communicationTemplateId, creationDate, readyDate, reason); // Then assertNotNull(communicationRequest); @@ -25,7 +26,8 @@ void testCreateCommunicationRequest() { assertEquals(reason, communicationRequest.reason()); assertEquals(CommunicationRequestEmitter.INTERVIEWER, communicationRequest.emitter()); assertNotNull(communicationRequest.status()); - assertEquals(1, communicationRequest.status().size()); + assertEquals(2, communicationRequest.status().size()); assertEquals(creationDate, communicationRequest.status().getFirst().date()); + assertEquals(readyDate, communicationRequest.status().getLast().date()); } } diff --git a/src/test/java/fr/insee/pearljam/domain/surveyunit/service/SurveyUnitUpdateServiceImplTest.java b/src/test/java/fr/insee/pearljam/domain/surveyunit/service/SurveyUnitUpdateServiceImplTest.java index b0b51e15..c9d14417 100644 --- a/src/test/java/fr/insee/pearljam/domain/surveyunit/service/SurveyUnitUpdateServiceImplTest.java +++ b/src/test/java/fr/insee/pearljam/domain/surveyunit/service/SurveyUnitUpdateServiceImplTest.java @@ -3,6 +3,8 @@ import fr.insee.pearljam.api.domain.*; import fr.insee.pearljam.api.service.impl.SurveyUnitUpdateServiceImpl; import fr.insee.pearljam.api.surveyunit.dto.*; +import fr.insee.pearljam.domain.campaign.port.userside.DateService; +import fr.insee.pearljam.domain.campaign.service.dummy.FixedDateService; import fr.insee.pearljam.domain.surveyunit.model.CommentType; import fr.insee.pearljam.domain.surveyunit.model.communication.*; import fr.insee.pearljam.domain.surveyunit.model.question.*; @@ -28,11 +30,13 @@ class SurveyUnitUpdateServiceImplTest { private SurveyUnit surveyUnit; private SurveyUnitUpdateDto surveyUnitDto; private CommunicationTemplateDB communicationTemplate; + private DateService dateService; @BeforeEach void setup() { + dateService = new FixedDateService(); communicationRequestFakeRepository = new CommunicationRequestFakeRepository(); - surveyUnitService = new SurveyUnitUpdateServiceImpl(communicationRequestFakeRepository); + surveyUnitService = new SurveyUnitUpdateServiceImpl(communicationRequestFakeRepository, dateService); surveyUnit = new SurveyUnit("id", true, true, null, null, null, null, null, null); @@ -74,13 +78,19 @@ void testUpdateCommunication01() { 1L, CommunicationRequestReason.UNREACHABLE, CommunicationRequestEmitter.INTERVIEWER, - List.of(tuple(null, 12345678910L, CommunicationStatusType.INITIATED)) + List.of( + tuple(null, 12345678910L, CommunicationStatusType.INITIATED), + tuple(null, dateService.getCurrentTimestamp(), CommunicationStatusType.READY) + ) ), tuple(null, 2L, CommunicationRequestReason.REFUSAL, CommunicationRequestEmitter.INTERVIEWER, - List.of(tuple(null, 1234567891011L, CommunicationStatusType.INITIATED)) + List.of( + tuple(null, 1234567891011L, CommunicationStatusType.INITIATED), + tuple(null, dateService.getCurrentTimestamp(), CommunicationStatusType.READY) + ) ) ); } diff --git a/src/test/java/fr/insee/pearljam/integration/campaign/CampaignIT.java b/src/test/java/fr/insee/pearljam/integration/campaign/CampaignIT.java index 7bcfd6f8..7a0691b9 100644 --- a/src/test/java/fr/insee/pearljam/integration/campaign/CampaignIT.java +++ b/src/test/java/fr/insee/pearljam/integration/campaign/CampaignIT.java @@ -33,7 +33,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@ActiveProfiles("auth") +@ActiveProfiles(profiles = {"auth", "test"}) @AutoConfigureMockMvc @ContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) diff --git a/src/test/java/fr/insee/pearljam/integration/campaign/CommunicationInformationIT.java b/src/test/java/fr/insee/pearljam/integration/campaign/CommunicationInformationIT.java index 53a0fcce..45b631a7 100644 --- a/src/test/java/fr/insee/pearljam/integration/campaign/CommunicationInformationIT.java +++ b/src/test/java/fr/insee/pearljam/integration/campaign/CommunicationInformationIT.java @@ -27,7 +27,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@ActiveProfiles("auth") +@ActiveProfiles(profiles = {"auth", "test"}) @AutoConfigureMockMvc @ContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) diff --git a/src/test/java/fr/insee/pearljam/integration/campaign/CommunicationTemplateIT.java b/src/test/java/fr/insee/pearljam/integration/campaign/CommunicationTemplateIT.java index a0c3aacb..973a110f 100644 --- a/src/test/java/fr/insee/pearljam/integration/campaign/CommunicationTemplateIT.java +++ b/src/test/java/fr/insee/pearljam/integration/campaign/CommunicationTemplateIT.java @@ -17,7 +17,7 @@ import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -@ActiveProfiles("auth") +@ActiveProfiles(profiles = {"auth", "test"}) @AutoConfigureMockMvc @ContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) diff --git a/src/test/java/fr/insee/pearljam/integration/campaign/VisibilityIT.java b/src/test/java/fr/insee/pearljam/integration/campaign/VisibilityIT.java index 5f522c27..02a42360 100644 --- a/src/test/java/fr/insee/pearljam/integration/campaign/VisibilityIT.java +++ b/src/test/java/fr/insee/pearljam/integration/campaign/VisibilityIT.java @@ -28,7 +28,7 @@ import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD; -@ActiveProfiles("auth") +@ActiveProfiles(profiles = {"auth", "test"}) @AutoConfigureMockMvc @ContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) diff --git a/src/test/java/fr/insee/pearljam/integration/surveyunit/SurveyUnitIT.java b/src/test/java/fr/insee/pearljam/integration/surveyunit/SurveyUnitIT.java index bb57cb16..5b53817a 100644 --- a/src/test/java/fr/insee/pearljam/integration/surveyunit/SurveyUnitIT.java +++ b/src/test/java/fr/insee/pearljam/integration/surveyunit/SurveyUnitIT.java @@ -3,6 +3,8 @@ import fr.insee.pearljam.api.service.SurveyUnitService; import fr.insee.pearljam.api.utils.AuthenticatedUserTestHelper; import fr.insee.pearljam.api.utils.ScriptConstants; +import fr.insee.pearljam.config.FixedDateServiceConfiguration; +import fr.insee.pearljam.domain.campaign.port.userside.DateService; import org.json.JSONException; import org.junit.jupiter.api.Test; import org.skyscreamer.jsonassert.JSONAssert; @@ -10,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; @@ -24,10 +27,11 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@ActiveProfiles("auth") +@ActiveProfiles(profiles = {"auth", "test"}) @AutoConfigureMockMvc @ContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Import(FixedDateServiceConfiguration.class) @Transactional class SurveyUnitIT { @@ -37,6 +41,9 @@ class SurveyUnitIT { @Autowired private SurveyUnitService surveyUnitService; + @Autowired + private DateService dateService; + @Test void testGetAllSurveyUnits() throws Exception { MvcResult mvcResult = mockMvc.perform(get("/api/survey-units") @@ -283,6 +290,7 @@ void testGetSurveyUnitDetail() throws Exception { @Test @Sql(value = ScriptConstants.REINIT_SQL_SCRIPT, executionPhase = AFTER_TEST_METHOD) void testPutSurveyUnitDetail() throws Exception { + long currentTimestamp = dateService.getCurrentTimestamp(); String updateJson = """ { "id":"20", @@ -494,6 +502,11 @@ void testPutSurveyUnitDetail() throws Exception { { "date":1721903754305, "status":"INITIATED" + }, + { + "date":""" + currentTimestamp + """ + , + "status":"READY" } ] }, @@ -516,6 +529,11 @@ void testPutSurveyUnitDetail() throws Exception { { "date":1721903754405, "status":"INITIATED" + }, + { + "date":""" + currentTimestamp + """ + , + "status":"READY" } ] },