diff --git a/queen-application/src/main/java/fr/insee/queen/application/configuration/auth/AuthorityRoleEnum.java b/queen-application/src/main/java/fr/insee/queen/application/configuration/auth/AuthorityRoleEnum.java index 998c4bf5..0ebd1255 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/configuration/auth/AuthorityRoleEnum.java +++ b/queen-application/src/main/java/fr/insee/queen/application/configuration/auth/AuthorityRoleEnum.java @@ -13,4 +13,11 @@ public enum AuthorityRoleEnum { public String securityRole() { return ROLE_PREFIX + this.name(); } + + public static AuthorityRoleEnum fromAuthority(String authority) { + if(authority != null && authority.startsWith(ROLE_PREFIX)) { + authority = authority.split(ROLE_PREFIX)[1]; + } + return AuthorityRoleEnum.valueOf(authority); + } } diff --git a/queen-application/src/main/java/fr/insee/queen/application/pilotage/controller/PilotageApiComponent.java b/queen-application/src/main/java/fr/insee/queen/application/pilotage/controller/PilotageApiComponent.java index 245d7ef4..f766e065 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/pilotage/controller/PilotageApiComponent.java +++ b/queen-application/src/main/java/fr/insee/queen/application/pilotage/controller/PilotageApiComponent.java @@ -51,7 +51,7 @@ public List getInterviewerSurveyUnits() { @Override public void checkHabilitations(String surveyUnitId, PilotageRole... rolesToCheck) { - SurveyUnitSummary surveyUnit = surveyUnitService.getSurveyUnitWithCampaignById(surveyUnitId); + SurveyUnitSummary surveyUnit = surveyUnitService.getSummaryById(surveyUnitId); Authentication auth = authHelper.getAuthenticationPrincipal(); List userRoles = auth.getAuthorities().stream() diff --git a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/DataController.java b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/DataController.java index 3d36d9cc..e9b39e75 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/DataController.java +++ b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/DataController.java @@ -1,13 +1,23 @@ package fr.insee.queen.application.surveyunit.controller; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import fr.insee.queen.application.configuration.auth.AuthorityPrivileges; +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; import fr.insee.queen.application.pilotage.controller.PilotageComponent; +import fr.insee.queen.application.surveyunit.controller.exception.LockedResourceException; +import fr.insee.queen.application.web.authentication.AuthenticationHelper; import fr.insee.queen.application.web.validation.IdValid; import fr.insee.queen.application.web.validation.json.JsonValid; import fr.insee.queen.application.web.validation.json.SchemaType; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.pilotage.service.PilotageRole; +import fr.insee.queen.domain.surveyunit.model.StateData; +import fr.insee.queen.domain.surveyunit.model.StateDataType; +import fr.insee.queen.domain.surveyunit.model.SurveyUnitSummary; import fr.insee.queen.domain.surveyunit.service.DataService; +import fr.insee.queen.domain.surveyunit.service.StateDataService; +import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; @@ -16,10 +26,13 @@ import jakarta.validation.constraints.NotNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.Optional; + /** * These endpoints handle the questionnaire form data of a survey unit */ @@ -32,6 +45,9 @@ public class DataController { private final DataService dataService; private final PilotageComponent pilotageComponent; + private final StateDataService stateDataService; + private final SurveyUnitService surveyUnitService; + private final AuthenticationHelper authenticationUserHelper; /** * Retrieve the questionnaire form data of a survey unit @@ -45,7 +61,37 @@ public class DataController { @ApiResponse(responseCode = "200", content = {@Content(mediaType = "application/json", schema = @Schema(ref = SchemaType.Names.DATA))}) public ObjectNode getDataBySurveyUnit(@IdValid @PathVariable(value = "id") String surveyUnitId) { pilotageComponent.checkHabilitations(surveyUnitId, PilotageRole.INTERVIEWER); - return dataService.getData(surveyUnitId); + SurveyUnitSummary surveyUnitSummary = surveyUnitService.getSummaryById(surveyUnitId); + + // if campaign sensitivity is OFF, return data + if(surveyUnitSummary.campaign().getSensitivity().equals(CampaignSensitivity.NORMAL)) { + return dataService.getData(surveyUnitId); + } + + // here, campaign sensitivity is ON ! + + // admin can see everything + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.ADMIN, AuthorityRoleEnum.WEBCLIENT)){ + return dataService.getData(surveyUnitId); + } + + // interviewer retrieve the dto with filled or empty data + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.INTERVIEWER, AuthorityRoleEnum.SURVEY_UNIT)){ + Optional validatedState = stateDataService + .findStateData(surveyUnitId) + .map(StateData::state) + .filter(state -> StateDataType.EXTRACTED.equals(state) + || StateDataType.VALIDATED.equals(state)); + + if (validatedState.isPresent()) { + return JsonNodeFactory.instance.objectNode(); + } + // if no state data or if state not extracted/validated + return dataService.getData(surveyUnitId); + } + + // reviewer cannot see data + throw new AccessDeniedException("Not authorized to see survey unit data"); } @@ -67,8 +113,40 @@ public void updateData( ObjectNode dataValue, @IdValid @PathVariable(value = "id") - String surveyUnitId) { + String surveyUnitId) throws LockedResourceException { pilotageComponent.checkHabilitations(surveyUnitId, PilotageRole.INTERVIEWER); - dataService.saveData(surveyUnitId, dataValue); + + SurveyUnitSummary surveyUnitSummary = surveyUnitService.getSummaryById(surveyUnitId); + + // if campaign sensitivity is OFF, update data + if(surveyUnitSummary.campaign().getSensitivity().equals(CampaignSensitivity.NORMAL)) { + dataService.saveData(surveyUnitId, dataValue); + return; + } + + // here, campaign sensitivity is ON ! + + // admin can do everything + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.ADMIN, AuthorityRoleEnum.WEBCLIENT)){ + dataService.saveData(surveyUnitId, dataValue); + return; + } + + // interviewer/survey-unit can update data if survey is not ended + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.INTERVIEWER, AuthorityRoleEnum.SURVEY_UNIT)){ + Optional validatedState = stateDataService + .findStateData(surveyUnitId) + .map(StateData::state) + .filter(state -> StateDataType.EXTRACTED.equals(state) + || StateDataType.VALIDATED.equals(state)); + + if (validatedState.isEmpty()) { + dataService.saveData(surveyUnitId, dataValue); + return; + } + + throw new LockedResourceException(surveyUnitId); + } + throw new AccessDeniedException("Not authorized to update survey unit data"); } } diff --git a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/StateDataController.java b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/StateDataController.java index deb8cc1e..aa5d9575 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/StateDataController.java +++ b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/StateDataController.java @@ -1,14 +1,21 @@ package fr.insee.queen.application.surveyunit.controller; import fr.insee.queen.application.configuration.auth.AuthorityPrivileges; +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; import fr.insee.queen.application.pilotage.controller.PilotageComponent; +import fr.insee.queen.application.surveyunit.controller.exception.LockedResourceException; import fr.insee.queen.application.surveyunit.dto.input.StateDataInput; import fr.insee.queen.application.surveyunit.dto.output.StateDataDto; import fr.insee.queen.application.surveyunit.dto.output.SurveyUnitDto; import fr.insee.queen.application.surveyunit.dto.output.SurveyUnitOkNokDto; +import fr.insee.queen.application.web.authentication.AuthenticationHelper; import fr.insee.queen.application.web.validation.IdValid; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.pilotage.service.PilotageRole; +import fr.insee.queen.domain.surveyunit.model.StateData; +import fr.insee.queen.domain.surveyunit.model.StateDataType; import fr.insee.queen.domain.surveyunit.model.SurveyUnitState; +import fr.insee.queen.domain.surveyunit.model.SurveyUnitSummary; import fr.insee.queen.domain.surveyunit.service.StateDataService; import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import fr.insee.queen.domain.surveyunit.service.exception.StateDataInvalidDateException; @@ -18,11 +25,13 @@ import jakarta.validation.constraints.NotEmpty; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; /** * Handle the data state of a survey unit. @@ -37,6 +46,7 @@ public class StateDataController { private final StateDataService stateDataService; private final SurveyUnitService surveyUnitService; private final PilotageComponent pilotageComponent; + private final AuthenticationHelper authenticationUserHelper; /** * Retrieve the data linked of a survey unit @@ -62,9 +72,39 @@ public StateDataDto getStateDataBySurveyUnit(@IdValid @PathVariable(value = "id" @PutMapping(path = "/survey-unit/{id}/state-data") @PreAuthorize(AuthorityPrivileges.HAS_SURVEY_UNIT_PRIVILEGES) public void setStateData(@IdValid @PathVariable(value = "id") String surveyUnitId, - @Valid @RequestBody StateDataInput stateDataInputDto) throws StateDataInvalidDateException { + @Valid @RequestBody StateDataInput stateDataInputDto) throws StateDataInvalidDateException, LockedResourceException { pilotageComponent.checkHabilitations(surveyUnitId, PilotageRole.INTERVIEWER); - stateDataService.saveStateData(surveyUnitId, StateDataInput.toModel(stateDataInputDto)); + SurveyUnitSummary surveyUnitSummary = surveyUnitService.getSummaryById(surveyUnitId); + + // if campaign sensitivity is OFF, update data + if(surveyUnitSummary.campaign().getSensitivity().equals(CampaignSensitivity.NORMAL)) { + stateDataService.saveStateData(surveyUnitId, StateDataInput.toModel(stateDataInputDto)); + return; + } + + // here, campaign sensitivity is ON ! + + // admin can do everything + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.ADMIN, AuthorityRoleEnum.WEBCLIENT)){ + stateDataService.saveStateData(surveyUnitId, StateDataInput.toModel(stateDataInputDto)); + return; + } + + // interviewer/survey-unit can update data if survey is not ended + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.INTERVIEWER, AuthorityRoleEnum.SURVEY_UNIT)){ + Optional validatedState = stateDataService + .findStateData(surveyUnitId) + .map(StateData::state) + .filter(state -> StateDataType.EXTRACTED.equals(state) + || StateDataType.VALIDATED.equals(state)); + + if (validatedState.isEmpty()) { + stateDataService.saveStateData(surveyUnitId, StateDataInput.toModel(stateDataInputDto)); + return; + } + throw new LockedResourceException(surveyUnitId); + } + throw new AccessDeniedException("Not authorized to update survey unit data"); } /** diff --git a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitController.java b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitController.java index be4e1038..bc413b91 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitController.java +++ b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitController.java @@ -2,18 +2,21 @@ import fr.insee.queen.application.campaign.component.MetadataConverter; import fr.insee.queen.application.configuration.auth.AuthorityPrivileges; +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; import fr.insee.queen.application.pilotage.controller.PilotageComponent; +import fr.insee.queen.application.surveyunit.controller.exception.LockedResourceException; import fr.insee.queen.application.surveyunit.dto.input.StateDataInput; import fr.insee.queen.application.surveyunit.dto.input.SurveyUnitCreationInput; import fr.insee.queen.application.surveyunit.dto.input.SurveyUnitDataStateDataUpdateInput; import fr.insee.queen.application.surveyunit.dto.input.SurveyUnitUpdateInput; import fr.insee.queen.application.surveyunit.dto.output.SurveyUnitDto; import fr.insee.queen.application.surveyunit.dto.output.SurveyUnitMetadataDto; +import fr.insee.queen.application.web.authentication.AuthenticationHelper; import fr.insee.queen.application.web.validation.IdValid; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.pilotage.service.PilotageRole; -import fr.insee.queen.domain.surveyunit.model.StateData; -import fr.insee.queen.domain.surveyunit.model.SurveyUnit; -import fr.insee.queen.domain.surveyunit.model.SurveyUnitMetadata; +import fr.insee.queen.domain.surveyunit.model.*; +import fr.insee.queen.domain.surveyunit.service.StateDataService; import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import fr.insee.queen.domain.surveyunit.service.exception.StateDataInvalidDateException; import io.swagger.v3.oas.annotations.Operation; @@ -23,11 +26,13 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; /** * Handle survey units @@ -42,6 +47,8 @@ public class SurveyUnitController { private final SurveyUnitService surveyUnitService; private final PilotageComponent pilotageComponent; private final MetadataConverter metadataConverter; + private final StateDataService stateDataService; + private final AuthenticationHelper authenticationUserHelper; /** * Retrieve all survey units id @@ -66,7 +73,39 @@ public List getSurveyUnitIds() { @PreAuthorize(AuthorityPrivileges.HAS_USER_PRIVILEGES) public SurveyUnitDto getSurveyUnitById(@IdValid @PathVariable(value = "id") String surveyUnitId) { pilotageComponent.checkHabilitations(surveyUnitId, PilotageRole.INTERVIEWER, PilotageRole.REVIEWER); - return SurveyUnitDto.fromModel(surveyUnitService.getSurveyUnit(surveyUnitId)); + SurveyUnitSummary surveyUnitSummary = surveyUnitService.getSummaryById(surveyUnitId); + + // if campaign sensitivity is OFF, return data + if(surveyUnitSummary.campaign().getSensitivity().equals(CampaignSensitivity.NORMAL)) { + return SurveyUnitDto.fromModel(surveyUnitService.getSurveyUnit(surveyUnitId)); + } + + // here, campaign sensitivity is ON ! + + // admin can see everything + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.ADMIN, AuthorityRoleEnum.WEBCLIENT)){ + return SurveyUnitDto.fromModel(surveyUnitService.getSurveyUnit(surveyUnitId)); + } + + // interviewer retrieve the dto with filled or empty data + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.INTERVIEWER, AuthorityRoleEnum.SURVEY_UNIT)){ + SurveyUnit su = surveyUnitService.getSurveyUnit(surveyUnitId); + StateData stateData = su.stateData(); + if(stateData == null) { + return SurveyUnitDto.fromModel(su); + } + + // survey is finished, not returning survey unit data + if(StateDataType.EXTRACTED.equals(stateData.state()) + || StateDataType.VALIDATED.equals(stateData.state())) { + return SurveyUnitDto.fromSensitiveModel(su); + } + + return SurveyUnitDto.fromModel(su); + } + + // reviewer cannot see data + throw new AccessDeniedException("Not authorized to see survey unit data"); } /** @@ -94,10 +133,44 @@ public SurveyUnitMetadataDto getSurveyUnitMetadataById(@IdValid @PathVariable(va @PutMapping(path = {"/survey-unit/{id}"}) @PreAuthorize(AuthorityPrivileges.HAS_INTERVIEWER_PRIVILEGES) public void updateSurveyUnitById(@IdValid @PathVariable(value = "id") String surveyUnitId, - @Valid @RequestBody SurveyUnitUpdateInput surveyUnitUpdateInput) { + @Valid @RequestBody SurveyUnitUpdateInput surveyUnitUpdateInput) throws LockedResourceException { pilotageComponent.checkHabilitations(surveyUnitId, PilotageRole.INTERVIEWER); - SurveyUnit surveyUnit = SurveyUnitUpdateInput.toModel(surveyUnitId, surveyUnitUpdateInput); - surveyUnitService.updateSurveyUnit(surveyUnit); + + SurveyUnitSummary surveyUnitSummary = surveyUnitService.getSummaryById(surveyUnitId); + + // if campaign sensitivity is OFF, update data + if(surveyUnitSummary.campaign().getSensitivity().equals(CampaignSensitivity.NORMAL)) { + SurveyUnit surveyUnit = SurveyUnitUpdateInput.toModel(surveyUnitId, surveyUnitUpdateInput); + surveyUnitService.updateSurveyUnit(surveyUnit); + return; + } + + // here, campaign sensitivity is ON ! + + // admin can see everything + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.ADMIN, AuthorityRoleEnum.WEBCLIENT)){ + SurveyUnit surveyUnit = SurveyUnitUpdateInput.toModel(surveyUnitId, surveyUnitUpdateInput); + surveyUnitService.updateSurveyUnit(surveyUnit); + return; + } + + // interviewer can update data if survey is not ended + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.INTERVIEWER)){ + Optional validatedState = stateDataService + .findStateData(surveyUnitId) + .map(StateData::state) + .filter(state -> StateDataType.EXTRACTED.equals(state) + || StateDataType.VALIDATED.equals(state)); + + if (validatedState.isEmpty()) { + SurveyUnit surveyUnit = SurveyUnitUpdateInput.toModel(surveyUnitId, surveyUnitUpdateInput); + surveyUnitService.updateSurveyUnit(surveyUnit); + return; + } + throw new LockedResourceException(surveyUnitId); + } + + throw new AccessDeniedException("Not authorized to update survey unit data"); } /** @@ -126,10 +199,43 @@ public ResponseEntity createUpdateSurveyUnit(@IdValid @PathVariable(value @PatchMapping(path = {"/survey-unit/{id}"}) @PreAuthorize(AuthorityPrivileges.HAS_SURVEY_UNIT_PRIVILEGES) public void updateSurveyUnitDataStateDataById(@IdValid @PathVariable(value = "id") String surveyUnitId, - @Valid @RequestBody SurveyUnitDataStateDataUpdateInput surveyUnitUpdateInput) { + @Valid @RequestBody SurveyUnitDataStateDataUpdateInput surveyUnitUpdateInput) throws LockedResourceException { pilotageComponent.checkHabilitations(surveyUnitId, PilotageRole.INTERVIEWER); - StateData stateData = StateDataInput.toModel(surveyUnitUpdateInput.stateData()); - surveyUnitService.updateSurveyUnit(surveyUnitId, surveyUnitUpdateInput.data(), stateData); + + SurveyUnitSummary surveyUnitSummary = surveyUnitService.getSummaryById(surveyUnitId); + + // if campaign sensitivity is OFF, update data + if(surveyUnitSummary.campaign().getSensitivity().equals(CampaignSensitivity.NORMAL)) { + StateData stateData = StateDataInput.toModel(surveyUnitUpdateInput.stateData()); + surveyUnitService.updateSurveyUnit(surveyUnitId, surveyUnitUpdateInput.data(), stateData); + return; + } + + // here, campaign sensitivity is ON ! + + // admin can do everything + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.ADMIN, AuthorityRoleEnum.WEBCLIENT)){ + StateData stateData = StateDataInput.toModel(surveyUnitUpdateInput.stateData()); + surveyUnitService.updateSurveyUnit(surveyUnitId, surveyUnitUpdateInput.data(), stateData); + return; + } + + // interviewer/survey-unit can update data if survey is not ended + if(authenticationUserHelper.hasRole(AuthorityRoleEnum.INTERVIEWER, AuthorityRoleEnum.SURVEY_UNIT)){ + Optional validatedState = stateDataService + .findStateData(surveyUnitId) + .map(StateData::state) + .filter(state -> StateDataType.EXTRACTED.equals(state) + || StateDataType.VALIDATED.equals(state)); + + if (validatedState.isEmpty()) { + StateData stateData = StateDataInput.toModel(surveyUnitUpdateInput.stateData()); + surveyUnitService.updateSurveyUnit(surveyUnitId, surveyUnitUpdateInput.data(), stateData); + return; + } + throw new LockedResourceException(surveyUnitId); + } + throw new AccessDeniedException("Not authorized to update survey unit data"); } diff --git a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/exception/LockedResourceException.java b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/exception/LockedResourceException.java new file mode 100644 index 00000000..d77c1da6 --- /dev/null +++ b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/controller/exception/LockedResourceException.java @@ -0,0 +1,9 @@ +package fr.insee.queen.application.surveyunit.controller.exception; + +public class LockedResourceException extends Exception { + public static final String MESSAGE = "Cannot process operation, survey has ended for survey unit %s. Resource is locked"; + + public LockedResourceException(String surveyUnitId) { + super(String.format(MESSAGE, surveyUnitId)); + } +} diff --git a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/dto/output/SurveyUnitDto.java b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/dto/output/SurveyUnitDto.java index 0ad3b662..892bf651 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/surveyunit/dto/output/SurveyUnitDto.java +++ b/queen-application/src/main/java/fr/insee/queen/application/surveyunit/dto/output/SurveyUnitDto.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import fr.insee.queen.application.web.validation.json.SchemaType; import fr.insee.queen.domain.surveyunit.model.StateData; @@ -40,4 +41,12 @@ public static SurveyUnitDto fromModel(SurveyUnit surveyUnit) { surveyUnit.comment(), StateDataDto.fromModel(surveyUnit.stateData())); } + + public static SurveyUnitDto fromSensitiveModel(SurveyUnit surveyUnit) { + return new SurveyUnitDto(surveyUnit.id(), surveyUnit.questionnaireId(), + surveyUnit.personalization(), + JsonNodeFactory.instance.objectNode(), + surveyUnit.comment(), + StateDataDto.fromModel(surveyUnit.stateData())); + } } diff --git a/queen-application/src/main/java/fr/insee/queen/application/web/authentication/AuthenticationHelper.java b/queen-application/src/main/java/fr/insee/queen/application/web/authentication/AuthenticationHelper.java index 6a8eea4b..d83a75d0 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/web/authentication/AuthenticationHelper.java +++ b/queen-application/src/main/java/fr/insee/queen/application/web/authentication/AuthenticationHelper.java @@ -1,5 +1,6 @@ package fr.insee.queen.application.web.authentication; +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; import org.springframework.security.core.Authentication; public interface AuthenticationHelper { @@ -16,4 +17,11 @@ public interface AuthenticationHelper { * @return {@link Authentication} the authentication user object */ Authentication getAuthenticationPrincipal(); + + /** + * + * @param roles roles to check + * @return true if current user has a specific role, false otherwise + */ + boolean hasRole(AuthorityRoleEnum... roles); } \ No newline at end of file diff --git a/queen-application/src/main/java/fr/insee/queen/application/web/authentication/AuthenticationUserHelper.java b/queen-application/src/main/java/fr/insee/queen/application/web/authentication/AuthenticationUserHelper.java index 0031a1a3..82a71683 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/web/authentication/AuthenticationUserHelper.java +++ b/queen-application/src/main/java/fr/insee/queen/application/web/authentication/AuthenticationUserHelper.java @@ -1,12 +1,16 @@ package fr.insee.queen.application.web.authentication; +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; import org.springframework.stereotype.Component; +import java.util.Arrays; + @Slf4j @Component @RequiredArgsConstructor @@ -23,5 +27,14 @@ public String getUserToken() { public Authentication getAuthenticationPrincipal() { return SecurityContextHolder.getContext().getAuthentication(); } + + public boolean hasRole(AuthorityRoleEnum... roles) { + return getAuthenticationPrincipal() + .getAuthorities() + .stream() + .map(GrantedAuthority::getAuthority) + .map(AuthorityRoleEnum::fromAuthority) + .anyMatch(role -> Arrays.asList(roles).contains(role)); + } } diff --git a/queen-application/src/main/java/fr/insee/queen/application/web/exception/ExceptionControllerAdvice.java b/queen-application/src/main/java/fr/insee/queen/application/web/exception/ExceptionControllerAdvice.java index 72db1ee4..a364078b 100644 --- a/queen-application/src/main/java/fr/insee/queen/application/web/exception/ExceptionControllerAdvice.java +++ b/queen-application/src/main/java/fr/insee/queen/application/web/exception/ExceptionControllerAdvice.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import fr.insee.queen.application.integration.component.exception.IntegrationComponentException; +import fr.insee.queen.application.surveyunit.controller.exception.LockedResourceException; import fr.insee.queen.application.web.authentication.AuthenticationTokenException; import fr.insee.queen.application.web.validation.exception.JsonValidatorComponentInitializationException; import fr.insee.queen.domain.campaign.service.exception.CampaignDeletionException; @@ -133,6 +134,11 @@ public ResponseEntity authenticationTokenExceptionException(Authentica return generateResponseError(e, HttpStatus.INTERNAL_SERVER_ERROR, request, ERROR_OCCURRED_LABEL); } + @ExceptionHandler(LockedResourceException.class) + public ResponseEntity lockedResourceException(LockedResourceException e, WebRequest request) { + return generateResponseError(e, HttpStatus.LOCKED, request); + } + @ExceptionHandler(HabilitationException.class) public ResponseEntity habilitationException(HabilitationException e, WebRequest request) { return generateResponseError(e, HttpStatus.FORBIDDEN, request); diff --git a/queen-application/src/test/java/fr/insee/queen/application/campaign/service/dummy/CampaignFakeService.java b/queen-application/src/test/java/fr/insee/queen/application/campaign/service/dummy/CampaignFakeService.java index 196dea02..2fe7932d 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/campaign/service/dummy/CampaignFakeService.java +++ b/queen-application/src/test/java/fr/insee/queen/application/campaign/service/dummy/CampaignFakeService.java @@ -1,6 +1,7 @@ package fr.insee.queen.application.campaign.service.dummy; import fr.insee.queen.domain.campaign.model.Campaign; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.campaign.model.CampaignSummary; import fr.insee.queen.domain.campaign.service.CampaignService; import lombok.Getter; @@ -16,8 +17,8 @@ public class CampaignFakeService implements CampaignService { private boolean created = false; public static final String CAMPAIGN1_ID = "allCampaigns1"; public static final List CAMPAIGN_SUMMARY_LIST = List.of( - new CampaignSummary(CAMPAIGN1_ID, "label", Set.of("questionnaireId1", "questionnaireId2")), - new CampaignSummary("allCampaigns2", "label", Set.of("questionnaireId1", "questionnaireId2")) + new CampaignSummary(CAMPAIGN1_ID, "label", CampaignSensitivity.NORMAL, Set.of("questionnaireId1", "questionnaireId2")), + new CampaignSummary("allCampaigns2", "label", CampaignSensitivity.SENSITIVE, Set.of("questionnaireId1", "questionnaireId2")) ); @Override diff --git a/queen-application/src/test/java/fr/insee/queen/application/pilotage/controller/PilotageApiComponentTest.java b/queen-application/src/test/java/fr/insee/queen/application/pilotage/controller/PilotageApiComponentTest.java index 498fe61b..ab762142 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/pilotage/controller/PilotageApiComponentTest.java +++ b/queen-application/src/test/java/fr/insee/queen/application/pilotage/controller/PilotageApiComponentTest.java @@ -36,7 +36,7 @@ void init() { void testCheckHabilitations02() { authHelper = new AuthenticationFakeHelper(authenticatedUserTestHelper.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER, AuthorityRoleEnum.ADMIN)); pilotageComponent = new PilotageApiComponent(pilotageService, authHelper, surveyUnitService); - pilotageComponent.checkHabilitations("11", PilotageRole.INTERVIEWER); + pilotageComponent.checkHabilitations(SurveyUnitFakeService.SURVEY_UNIT1_ID, PilotageRole.INTERVIEWER); assertThat(pilotageService.getWentThroughHasHabilitation()).isZero(); } @@ -45,7 +45,7 @@ void testCheckHabilitations02() { void testCheckHabilitations03() { authHelper = new AuthenticationFakeHelper(authenticatedUserTestHelper.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER, AuthorityRoleEnum.WEBCLIENT)); pilotageComponent = new PilotageApiComponent(pilotageService, authHelper, surveyUnitService); - pilotageComponent.checkHabilitations("11", PilotageRole.INTERVIEWER); + pilotageComponent.checkHabilitations(SurveyUnitFakeService.SURVEY_UNIT1_ID, PilotageRole.INTERVIEWER); assertThat(pilotageService.getWentThroughHasHabilitation()).isZero(); } @@ -54,7 +54,7 @@ void testCheckHabilitations03() { void testCheckHabilitations04() { authHelper = new AuthenticationFakeHelper(authenticatedUserTestHelper.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER, AuthorityRoleEnum.REVIEWER)); pilotageComponent = new PilotageApiComponent(pilotageService, authHelper, surveyUnitService); - pilotageComponent.checkHabilitations("11", PilotageRole.INTERVIEWER); + pilotageComponent.checkHabilitations(SurveyUnitFakeService.SURVEY_UNIT1_ID, PilotageRole.INTERVIEWER); assertThat(pilotageService.getWentThroughHasHabilitation()).isEqualTo(1); } @@ -66,7 +66,7 @@ void testCheckHabilitations05() { authHelper = new AuthenticationFakeHelper(authenticatedUser); pilotageService.setHasHabilitation(false); pilotageComponent = new PilotageApiComponent(pilotageService, authHelper, surveyUnitService); - assertThatThrownBy(() -> pilotageComponent.checkHabilitations("11", PilotageRole.INTERVIEWER, PilotageRole.REVIEWER)) + assertThatThrownBy(() -> pilotageComponent.checkHabilitations(SurveyUnitFakeService.SURVEY_UNIT1_ID, PilotageRole.INTERVIEWER, PilotageRole.REVIEWER)) .isInstanceOf(HabilitationException.class); assertThat(pilotageService.getWentThroughHasHabilitation()).isEqualTo(2); } diff --git a/queen-application/src/test/java/fr/insee/queen/application/pilotage/controller/dummy/PilotageFakeComponent.java b/queen-application/src/test/java/fr/insee/queen/application/pilotage/controller/dummy/PilotageFakeComponent.java index 770d37f5..ced32bf4 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/pilotage/controller/dummy/PilotageFakeComponent.java +++ b/queen-application/src/test/java/fr/insee/queen/application/pilotage/controller/dummy/PilotageFakeComponent.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory; import fr.insee.queen.application.pilotage.controller.PilotageComponent; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.campaign.model.CampaignSummary; import fr.insee.queen.domain.pilotage.model.PilotageCampaign; import fr.insee.queen.domain.pilotage.service.PilotageRole; import fr.insee.queen.domain.surveyunit.model.StateData; @@ -43,9 +45,10 @@ public List getSurveyUnitsByCampaign(String campaignId) { if (this.hasEmptySurveyUnits) { return new ArrayList<>(); } + CampaignSummary campaignSummary = new CampaignSummary("campaign-id", "label", CampaignSensitivity.NORMAL); return List.of( - new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", "campaign-id"), - new SurveyUnitSummary("s2", "questionnaire-id", "campaign-id") + new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", campaignSummary), + new SurveyUnitSummary("s2", "questionnaire-id", campaignSummary) ); } diff --git a/queen-application/src/test/java/fr/insee/queen/application/pilotage/service/dummy/PilotageFakeService.java b/queen-application/src/test/java/fr/insee/queen/application/pilotage/service/dummy/PilotageFakeService.java index 36f674a6..9caad95a 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/pilotage/service/dummy/PilotageFakeService.java +++ b/queen-application/src/test/java/fr/insee/queen/application/pilotage/service/dummy/PilotageFakeService.java @@ -1,6 +1,8 @@ package fr.insee.queen.application.pilotage.service.dummy; import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.campaign.model.CampaignSummary; import fr.insee.queen.domain.pilotage.model.PilotageCampaign; import fr.insee.queen.domain.pilotage.service.PilotageRole; import fr.insee.queen.domain.pilotage.service.PilotageService; @@ -33,10 +35,12 @@ public class PilotageFakeService implements PilotageService { public static final String SURVEY_UNIT1_ID = "pilotage-s1"; public static final String SURVEY_UNIT2_ID = "pilotage-s2"; + public final CampaignSummary campaignSummary = new CampaignSummary("campaign-id", "label", CampaignSensitivity.NORMAL); + @Getter private final List surveyUnitSummaries = List.of( - new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", "campaign-id"), - new SurveyUnitSummary("s2", "questionnaire-id", "campaign-id") + new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", campaignSummary), + new SurveyUnitSummary("s2", "questionnaire-id", campaignSummary) ); @Getter diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/DataControllerTest.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/DataControllerTest.java new file mode 100644 index 00000000..08f54d57 --- /dev/null +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/DataControllerTest.java @@ -0,0 +1,280 @@ +package fr.insee.queen.application.surveyunit.controller; + +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; +import fr.insee.queen.application.pilotage.controller.dummy.PilotageFakeComponent; +import fr.insee.queen.application.surveyunit.controller.exception.LockedResourceException; +import fr.insee.queen.application.surveyunit.service.dummy.DataFakeService; +import fr.insee.queen.application.surveyunit.service.dummy.StateDataFakeService; +import fr.insee.queen.application.surveyunit.service.dummy.SurveyUnitFakeService; +import fr.insee.queen.application.utils.AuthenticatedUserTestHelper; +import fr.insee.queen.application.utils.dummy.AuthenticationFakeHelper; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.surveyunit.model.StateData; +import fr.insee.queen.domain.surveyunit.model.StateDataType; +import fr.insee.queen.domain.surveyunit.model.SurveyUnitSummary; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; + +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class DataControllerTest { + + private DataController dataController; + private PilotageFakeComponent pilotageFakeComponent; + private SurveyUnitFakeService surveyUnitFakeService; + private StateDataFakeService stateDataFakeService; + private DataFakeService dataFakeService; + private AuthenticationFakeHelper authenticationFakeHelper; + private AuthenticatedUserTestHelper authenticationUserProvider; + + @BeforeEach + public void init() { + authenticationUserProvider = new AuthenticatedUserTestHelper(); + pilotageFakeComponent = new PilotageFakeComponent(); + surveyUnitFakeService = new SurveyUnitFakeService(); + stateDataFakeService = new StateDataFakeService(); + dataFakeService = new DataFakeService(); + authenticationFakeHelper = new AuthenticationFakeHelper(); + dataController = new DataController(dataFakeService, pilotageFakeComponent, stateDataFakeService, surveyUnitFakeService, authenticationFakeHelper); + } + + + @Test + @DisplayName("Should throw exception when role is reviewer and campaign is sensitive") + void testUpdateSurveyUnitException() { + // given + ObjectNode dataInput = JsonNodeFactory.instance.objectNode(); + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.REVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when & then + assertThatThrownBy(() -> dataController.updateData(dataInput, SurveyUnitFakeService.SURVEY_UNIT3_ID)) + .isInstanceOf(AccessDeniedException.class); + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideAdminAndWebclientUsers") + @DisplayName("Should update data when campaign is sensitive and role is admin/webclient") + void testUpdateSurveyUnit04() throws LockedResourceException { + // given + ObjectNode dataInput = JsonNodeFactory.instance.objectNode(); + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAdminUser()); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + dataController.updateData(dataInput, SurveyUnitFakeService.SURVEY_UNIT3_ID); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(dataFakeService.isCheckUpdateData()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsers") + @DisplayName("Should update data when campaign is sensitive and role is interviewer/survey-unit") + void testUpdateSurveyUnit05(Authentication auth) throws LockedResourceException { + // given + ObjectNode dataInput = JsonNodeFactory.instance.objectNode(); + authenticationFakeHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + dataController.updateData(dataInput, SurveyUnitFakeService.SURVEY_UNIT3_ID); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(dataFakeService.isCheckUpdateData()).isTrue(); + } + + @Test + @DisplayName("Should update data when campaign is sensitive, role is interviewer and state is not EXTRACTED/VALIDATED") + void testUpdateSurveyUnit06() throws LockedResourceException { + // given + ObjectNode dataInput = JsonNodeFactory.instance.objectNode(); + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + StateData stateData = stateDataFakeService.getStateData(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(stateData.state()).isNotIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when + dataController.updateData(dataInput, SurveyUnitFakeService.SURVEY_UNIT3_ID); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(dataFakeService.isCheckUpdateData()).isTrue(); + } + + @ParameterizedTest + @CsvSource(value = {SurveyUnitFakeService.SURVEY_UNIT4_ID, SurveyUnitFakeService.SURVEY_UNIT5_ID}) + @DisplayName("Should throw exception when campaign is sensitive, role is interviewer and state is EXTRACTED/VALIDATED") + void testUpdateSurveyUnitException02(String surveyUnitId) { + // given + ObjectNode dataInput = JsonNodeFactory.instance.objectNode(); + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(surveyUnitId); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + StateData stateData = stateDataFakeService.getStateData(surveyUnitId); + assertThat(stateData.state()).isIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when + + assertThatThrownBy(() -> dataController.updateData(dataInput, surveyUnitId)) + .isInstanceOf(LockedResourceException.class); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(dataFakeService.isCheckUpdateData()).isFalse(); + } + + @Test + @DisplayName("Should return data when campaign is not sensitive") + void testGetSurveyUnit01() { + // given + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT1_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.NORMAL); + + // when + ObjectNode data = dataController.getDataBySurveyUnit(SurveyUnitFakeService.SURVEY_UNIT1_ID); + + // then + ObjectNode surveyUnitData = dataFakeService.getData(SurveyUnitFakeService.SURVEY_UNIT1_ID); + assertThat(data).isEqualTo(surveyUnitData); + } + + @Test + @DisplayName("Should throw exception when role is reviewer and campaign is sensitive") + void testGetSurveyUnitException() { + // given + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.REVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when & then + assertThatThrownBy(() -> dataController.getDataBySurveyUnit(SurveyUnitFakeService.SURVEY_UNIT3_ID)) + .isInstanceOf(AccessDeniedException.class); + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideAdminAndWebclientUsers") + @DisplayName("Should return data when campaign is sensitive and role is admin/webclient") + void testGetSurveyUnit02() { + // given + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAdminUser()); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + ObjectNode data = dataController.getDataBySurveyUnit(SurveyUnitFakeService.SURVEY_UNIT3_ID); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + ObjectNode surveyUnitData = dataFakeService.getData(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(data).isEqualTo(surveyUnitData); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsers") + @DisplayName("Should return data when campaign is sensitive and role is interviewer/survey-unit") + void testGetSurveyUnit03(Authentication auth) { + // given + authenticationFakeHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + ObjectNode data = dataController.getDataBySurveyUnit(SurveyUnitFakeService.SURVEY_UNIT3_ID); + + // then + ObjectNode surveyUnitData = dataFakeService.getData(SurveyUnitFakeService.SURVEY_UNIT3_ID); + + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(data).isEqualTo(surveyUnitData); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsersWithExtractedOrValidatedStates") + @DisplayName("Should return empty data when campaign is sensitive, role is interviewer/survey-unit and state is EXTRACTED/VALIDATED") + void testGetSurveyUnit04(Authentication auth, String surveyUnitId) { + // given + authenticationFakeHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(surveyUnitId); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + ObjectNode surveyUnitData = dataFakeService.getData(surveyUnitId); + StateData stateData = stateDataFakeService.getStateData(surveyUnitId); + assertThat(stateData.state()).isIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when + ObjectNode data = dataController.getDataBySurveyUnit(surveyUnitId); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(data) + .isNotEqualTo(surveyUnitData) + .isEqualTo(JsonNodeFactory.instance.objectNode()); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsers") + @DisplayName("Should return data when campaign is sensitive, role is interviewer/survey-unit, and state data is null") + void testGetSurveyUnit05(Authentication auth) { + // given + authenticationFakeHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT6_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + ObjectNode surveyUnitData = dataFakeService.getData(SurveyUnitFakeService.SURVEY_UNIT6_ID); + StateData stateData = stateDataFakeService.getStateData(SurveyUnitFakeService.SURVEY_UNIT6_ID); + assertThat(stateData).isNull(); + + // when + ObjectNode data = dataController.getDataBySurveyUnit(SurveyUnitFakeService.SURVEY_UNIT6_ID); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(data).isEqualTo(surveyUnitData); + } + + private static Stream provideInterviewerAndSuUsers() { + AuthenticatedUserTestHelper provider = new AuthenticatedUserTestHelper(); + return Stream.of( + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.SURVEY_UNIT))); + } + + private static Stream provideAdminAndWebclientUsers() { + AuthenticatedUserTestHelper provider = new AuthenticatedUserTestHelper(); + return Stream.of( + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.WEBCLIENT)), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.ADMIN))); + } + + private static Stream provideInterviewerAndSuUsersWithExtractedOrValidatedStates() { + AuthenticatedUserTestHelper provider = new AuthenticatedUserTestHelper(); + return Stream.of( + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER), SurveyUnitFakeService.SURVEY_UNIT4_ID), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER), SurveyUnitFakeService.SURVEY_UNIT5_ID), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.SURVEY_UNIT), SurveyUnitFakeService.SURVEY_UNIT4_ID), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.SURVEY_UNIT), SurveyUnitFakeService.SURVEY_UNIT5_ID)); + } +} diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/StateDataControllerTest.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/StateDataControllerTest.java new file mode 100644 index 00000000..dcebc71e --- /dev/null +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/StateDataControllerTest.java @@ -0,0 +1,156 @@ +package fr.insee.queen.application.surveyunit.controller; + +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; +import fr.insee.queen.application.pilotage.controller.dummy.PilotageFakeComponent; +import fr.insee.queen.application.surveyunit.controller.exception.LockedResourceException; +import fr.insee.queen.application.surveyunit.dto.input.StateDataInput; +import fr.insee.queen.application.surveyunit.dto.input.StateDataTypeInput; +import fr.insee.queen.application.surveyunit.service.dummy.StateDataFakeService; +import fr.insee.queen.application.surveyunit.service.dummy.SurveyUnitFakeService; +import fr.insee.queen.application.utils.AuthenticatedUserTestHelper; +import fr.insee.queen.application.utils.dummy.AuthenticationFakeHelper; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.surveyunit.model.StateData; +import fr.insee.queen.domain.surveyunit.model.StateDataType; +import fr.insee.queen.domain.surveyunit.model.SurveyUnitSummary; +import fr.insee.queen.domain.surveyunit.service.exception.StateDataInvalidDateException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; + +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class StateDataControllerTest { + + private StateDataController stateDataController; + private PilotageFakeComponent pilotageFakeComponent; + private SurveyUnitFakeService surveyUnitFakeService; + private StateDataFakeService stateDataFakeService; + private AuthenticationFakeHelper authenticationFakeHelper; + private AuthenticatedUserTestHelper authenticationUserProvider; + + @BeforeEach + public void init() { + authenticationUserProvider = new AuthenticatedUserTestHelper(); + pilotageFakeComponent = new PilotageFakeComponent(); + surveyUnitFakeService = new SurveyUnitFakeService(); + stateDataFakeService = new StateDataFakeService(); + authenticationFakeHelper = new AuthenticationFakeHelper(); + stateDataController = new StateDataController(stateDataFakeService, surveyUnitFakeService, pilotageFakeComponent, authenticationFakeHelper); + } + + + @Test + @DisplayName("Should throw exception when role is reviewer and campaign is sensitive") + void testUpdateStateDataException() { + // given + StateDataInput stateDataInput = new StateDataInput(StateDataTypeInput.INIT, 1L, "1.0"); + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.REVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when & then + assertThatThrownBy(() -> stateDataController.setStateData(SurveyUnitFakeService.SURVEY_UNIT3_ID, stateDataInput)) + .isInstanceOf(AccessDeniedException.class); + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideAdminAndWebclientUsers") + @DisplayName("Should update data when campaign is sensitive and role is admin/webclient") + void testUpdateStateData04() throws StateDataInvalidDateException, LockedResourceException { + // given + StateDataInput stateDataInput = new StateDataInput(StateDataTypeInput.INIT, 1L, "1.0"); + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAdminUser()); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + stateDataController.setStateData(SurveyUnitFakeService.SURVEY_UNIT3_ID, stateDataInput); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(StateDataInput.toModel(stateDataInput)).isEqualTo(stateDataFakeService.getStateDataSaved()); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsers") + @DisplayName("Should update data when campaign is sensitive and role is interviewer/survey-unit") + void testUpdateStateData05(Authentication auth) throws StateDataInvalidDateException, LockedResourceException { + // given + StateDataInput stateDataInput = new StateDataInput(StateDataTypeInput.INIT, 1L, "1.0"); + authenticationFakeHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + stateDataController.setStateData(SurveyUnitFakeService.SURVEY_UNIT3_ID, stateDataInput); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(StateDataInput.toModel(stateDataInput)).isEqualTo(stateDataFakeService.getStateDataSaved()); + } + + @Test + @DisplayName("Should update data when campaign is sensitive, role is interviewer and state is not EXTRACTED/VALIDATED") + void testUpdateStateData06() throws StateDataInvalidDateException, LockedResourceException { + // given + StateDataInput stateDataInput = new StateDataInput(StateDataTypeInput.INIT, 1L, "1.0"); + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + StateData stateData = stateDataFakeService.getStateData(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(stateData.state()).isNotIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when + stateDataController.setStateData(SurveyUnitFakeService.SURVEY_UNIT3_ID, stateDataInput); + + // then + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(StateDataInput.toModel(stateDataInput)).isEqualTo(stateDataFakeService.getStateDataSaved()); + } + + @ParameterizedTest + @CsvSource(value = {SurveyUnitFakeService.SURVEY_UNIT4_ID, SurveyUnitFakeService.SURVEY_UNIT5_ID}) + @DisplayName("Should update data when campaign is sensitive, role is interviewer and state is EXTRACTED/VALIDATED") + void testUpdateStateDataException02(String surveyUnitId) { + // given + StateDataInput stateDataInput = new StateDataInput(StateDataTypeInput.INIT, 1L, "1.0"); + authenticationFakeHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(surveyUnitId); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + StateData stateData = stateDataFakeService.getStateData(surveyUnitId); + assertThat(stateData.state()).isIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when & then + assertThatThrownBy(() -> stateDataController.setStateData(surveyUnitId, stateDataInput)) + .isInstanceOf(LockedResourceException.class); + assertThat(pilotageFakeComponent.isChecked()).isTrue(); + assertThat(stateDataFakeService.getStateDataSaved()).isNull(); + } + + private static Stream provideInterviewerAndSuUsers() { + AuthenticatedUserTestHelper provider = new AuthenticatedUserTestHelper(); + return Stream.of( + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.SURVEY_UNIT))); + } + + private static Stream provideAdminAndWebclientUsers() { + AuthenticatedUserTestHelper provider = new AuthenticatedUserTestHelper(); + return Stream.of( + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.WEBCLIENT)), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.ADMIN))); + } +} diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitControllerTest.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitControllerTest.java index d0a1d461..7d48595f 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitControllerTest.java +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/controller/SurveyUnitControllerTest.java @@ -1,14 +1,32 @@ package fr.insee.queen.application.surveyunit.controller; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; import fr.insee.queen.application.pilotage.controller.dummy.PilotageFakeComponent; import fr.insee.queen.application.surveyunit.controller.dummy.MetadataFakeConverter; -import fr.insee.queen.application.surveyunit.dto.input.StateDataForSurveyUnitUpdateInput; -import fr.insee.queen.application.surveyunit.dto.input.SurveyUnitUpdateInput; +import fr.insee.queen.application.surveyunit.controller.exception.LockedResourceException; +import fr.insee.queen.application.surveyunit.dto.input.*; +import fr.insee.queen.application.surveyunit.dto.output.SurveyUnitDto; +import fr.insee.queen.application.surveyunit.service.dummy.StateDataFakeService; import fr.insee.queen.application.surveyunit.service.dummy.SurveyUnitFakeService; +import fr.insee.queen.application.utils.AuthenticatedUserTestHelper; +import fr.insee.queen.application.utils.dummy.AuthenticationFakeHelper; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.surveyunit.model.StateDataType; import fr.insee.queen.domain.surveyunit.model.SurveyUnit; +import fr.insee.queen.domain.surveyunit.model.SurveyUnitSummary; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.security.core.Authentication; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.springframework.security.access.AccessDeniedException; +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -17,31 +35,401 @@ class SurveyUnitControllerTest { private PilotageFakeComponent pilotageComponent; private SurveyUnitFakeService surveyUnitFakeService; private MetadataFakeConverter metadataConverter; + private StateDataFakeService stateDataService; + private AuthenticationFakeHelper authenticatedUserHelper; + private AuthenticatedUserTestHelper authenticationUserProvider; @BeforeEach public void init() { + authenticationUserProvider = new AuthenticatedUserTestHelper(); metadataConverter = new MetadataFakeConverter(); pilotageComponent = new PilotageFakeComponent(); surveyUnitFakeService = new SurveyUnitFakeService(); - surveyUnitController = new SurveyUnitController(surveyUnitFakeService, pilotageComponent, metadataConverter); + stateDataService = new StateDataFakeService(); + authenticatedUserHelper = new AuthenticationFakeHelper(); + surveyUnitController = new SurveyUnitController(surveyUnitFakeService, pilotageComponent, metadataConverter, stateDataService, authenticatedUserHelper); + } + + @Test + @DisplayName("Should throw exception when role is reviewer and campaign is sensitive") + void testDiffUpdateSurveyUnitException() { + // given + SurveyUnitDataStateDataUpdateInput suInput = new SurveyUnitDataStateDataUpdateInput(null, new StateDataInput(StateDataTypeInput.INIT, 1L, "2.0")); + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.REVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when & then + assertThatThrownBy(() -> surveyUnitController.updateSurveyUnitDataStateDataById(SurveyUnitFakeService.SURVEY_UNIT3_ID, suInput)) + .isInstanceOf(AccessDeniedException.class); + assertThat(pilotageComponent.isChecked()).isTrue(); + } + + @ParameterizedTest + @CsvSource(value = {SurveyUnitFakeService.SURVEY_UNIT4_ID, SurveyUnitFakeService.SURVEY_UNIT5_ID}) + @DisplayName("Should throw exception when campaign is sensitive, role is interviewer and state is EXTRACTED/VALIDATED") + void testDiffUpdateSurveyUnitException02(String surveyUnitId) { + + // given + SurveyUnitDataStateDataUpdateInput suInput = new SurveyUnitDataStateDataUpdateInput(null, new StateDataInput(StateDataTypeInput.INIT, 1L, "2.0")); + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(surveyUnitId); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(surveyUnitId); + assertThat(surveyUnit.stateData().state()).isIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when & then + assertThatThrownBy(() -> surveyUnitController.updateSurveyUnitDataStateDataById(surveyUnitId, suInput)) + .isInstanceOf(LockedResourceException.class); + assertThat(pilotageComponent.isChecked()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideAdminAndWebclientUsers") + @DisplayName("Should update survey unit when campaign is sensitive and role is admin/webclient") + void testDiffUpdateSurveyUnit04() throws LockedResourceException { + // given + SurveyUnitDataStateDataUpdateInput suInput = new SurveyUnitDataStateDataUpdateInput(null, new StateDataInput(StateDataTypeInput.INIT, 1L, "2.0")); + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAdminUser()); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + surveyUnitController.updateSurveyUnitDataStateDataById(SurveyUnitFakeService.SURVEY_UNIT3_ID, suInput); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); + SurveyUnit surveyUnitUpdated = surveyUnitFakeService.getSurveyUnitUpdated(); + assertThat(surveyUnitUpdated.id()).isEqualTo(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitUpdated.stateData().state()).isEqualTo(StateDataType.valueOf(suInput.stateData().state().name())); + assertThat(surveyUnitUpdated.stateData().currentPage()).isEqualTo(suInput.stateData().currentPage()); + assertThat(surveyUnitUpdated.stateData().date()).isEqualTo(suInput.stateData().date()); + assertThat(surveyUnitUpdated.data()).isEqualTo(suInput.data()); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsers") + @DisplayName("Should update survey unit when campaign is sensitive and role is interviewer/survey-unit") + void testDiffUpdateSurveyUnit05(Authentication auth) throws LockedResourceException { + // given + SurveyUnitDataStateDataUpdateInput suInput = new SurveyUnitDataStateDataUpdateInput(null, new StateDataInput(StateDataTypeInput.INIT, 1L, "2.0")); + authenticatedUserHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + surveyUnitController.updateSurveyUnitDataStateDataById(SurveyUnitFakeService.SURVEY_UNIT3_ID, suInput); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); + SurveyUnit surveyUnitUpdated = surveyUnitFakeService.getSurveyUnitUpdated(); + assertThat(surveyUnitUpdated.id()).isEqualTo(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitUpdated.stateData().state()).isEqualTo(StateDataType.valueOf(suInput.stateData().state().name())); + assertThat(surveyUnitUpdated.stateData().currentPage()).isEqualTo(suInput.stateData().currentPage()); + assertThat(surveyUnitUpdated.stateData().date()).isEqualTo(suInput.stateData().date()); + assertThat(surveyUnitUpdated.data()).isEqualTo(suInput.data()); } @Test - @DisplayName("On update survey-unit, should transform state-data to null if state from input state data is null") - void testUpdateSurveyUnit01() { + @DisplayName("Should update survey unit when campaign is sensitive, role is interviewer and state is not EXTRACTED/VALIDATED") + void testDiffUpdateSurveyUnit06() throws LockedResourceException { + // given + SurveyUnitDataStateDataUpdateInput suInput = new SurveyUnitDataStateDataUpdateInput(null, new StateDataInput(StateDataTypeInput.INIT, 1L, "2.0")); + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnit.stateData().state()).isNotIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when + surveyUnitController.updateSurveyUnitDataStateDataById(SurveyUnitFakeService.SURVEY_UNIT3_ID, suInput); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); + SurveyUnit surveyUnitUpdated = surveyUnitFakeService.getSurveyUnitUpdated(); + assertThat(surveyUnitUpdated.id()).isEqualTo(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitUpdated.stateData().state()).isEqualTo(StateDataType.valueOf(suInput.stateData().state().name())); + assertThat(surveyUnitUpdated.stateData().currentPage()).isEqualTo(suInput.stateData().currentPage()); + assertThat(surveyUnitUpdated.stateData().date()).isEqualTo(suInput.stateData().date()); + assertThat(surveyUnitUpdated.data()).isEqualTo(suInput.data()); + } + + @Test + @DisplayName("Should update survey unit and transform state-data to null if state from input state data is null") + void testUpdateSurveyUnit01() throws LockedResourceException { + // given StateDataForSurveyUnitUpdateInput stateData = new StateDataForSurveyUnitUpdateInput(null, 123456789L, "2.3"); SurveyUnitUpdateInput suInput = new SurveyUnitUpdateInput(null, null, null, stateData); - surveyUnitController.updateSurveyUnitById("11", suInput); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT1_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.NORMAL); + + // when + surveyUnitController.updateSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT1_ID, suInput); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); SurveyUnit surveyUnitUpdated = surveyUnitFakeService.getSurveyUnitUpdated(); assertThat(surveyUnitUpdated.stateData()).isNull(); } @Test - @DisplayName("On update survey-unit, should transform state-data to null if input state data is null") - void testUpdateSurveyUnit02() { + @DisplayName("Should update survey unit and transform state-data to null if input state data is null") + void testUpdateSurveyUnit02() throws LockedResourceException { + // given SurveyUnitUpdateInput suInput = new SurveyUnitUpdateInput(null, null, null, null); - surveyUnitController.updateSurveyUnitById("11", suInput); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT1_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.NORMAL); + + // when + surveyUnitController.updateSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT1_ID, suInput); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); SurveyUnit surveyUnitUpdated = surveyUnitFakeService.getSurveyUnitUpdated(); assertThat(surveyUnitUpdated.stateData()).isNull(); } + + @Test + @DisplayName("Should throw exception when role is reviewer and campaign is sensitive") + void testUpdateSurveyUnitException() { + // given + SurveyUnitUpdateInput suInput = new SurveyUnitUpdateInput(null, null, null, null); + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.REVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when & then + assertThatThrownBy(() -> surveyUnitController.updateSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT3_ID, suInput)) + .isInstanceOf(AccessDeniedException.class); + assertThat(pilotageComponent.isChecked()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideAdminAndWebclientUsers") + @DisplayName("Should update survey unit when campaign is sensitive and role is admin/webclient") + void testUpdateSurveyUnit04() throws LockedResourceException { + // given + SurveyUnitUpdateInput suInput = new SurveyUnitUpdateInput(null, null, null, null); + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAdminUser()); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + surveyUnitController.updateSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT3_ID, suInput); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); + SurveyUnit surveyUnitUpdated = surveyUnitFakeService.getSurveyUnitUpdated(); + assertThat(surveyUnitUpdated.stateData()).isNull(); + } + + @Test + @DisplayName("Should update survey unit when campaign is sensitive, role is interviewer and state is not EXTRACTED/VALIDATED") + void testUpdateSurveyUnit06() throws LockedResourceException { + // given + SurveyUnitUpdateInput suInput = new SurveyUnitUpdateInput(null, null, null, null); + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnit.stateData().state()).isNotIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when + surveyUnitController.updateSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT3_ID, suInput); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); + SurveyUnit surveyUnitUpdated = surveyUnitFakeService.getSurveyUnitUpdated(); + assertThat(surveyUnitUpdated.stateData()).isNull(); + } + + @ParameterizedTest + @CsvSource(value = {SurveyUnitFakeService.SURVEY_UNIT4_ID, SurveyUnitFakeService.SURVEY_UNIT5_ID}) + @DisplayName("Should throw exception when campaign is sensitive, role is interviewer and state is EXTRACTED/VALIDATED") + void testUpdateSurveyUnitException02(String surveyUnitId) { + // given + SurveyUnitUpdateInput suInput = new SurveyUnitUpdateInput(null, null, null, null); + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(surveyUnitId); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(surveyUnitId); + assertThat(surveyUnit.stateData().state()).isIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when & then + assertThatThrownBy(() -> surveyUnitController.updateSurveyUnitById(surveyUnitId, suInput)) + .isInstanceOf(LockedResourceException.class); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); + SurveyUnit surveyUnitUpdated = surveyUnitFakeService.getSurveyUnitUpdated(); + assertThat(surveyUnitUpdated).isNull(); + } + + @Test + @DisplayName("Should return survey unit with data when campaign is not sensitive") + void testGetSurveyUnit01() { + // given + // when + SurveyUnitDto surveyUnitDto = surveyUnitController.getSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT1_ID); + + // then + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(SurveyUnitFakeService.SURVEY_UNIT1_ID); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT1_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.NORMAL); + assertThat(pilotageComponent.isChecked()).isTrue(); + assertThat(surveyUnitDto.id()).isEqualTo(surveyUnit.id()); + assertThat(surveyUnitDto.data()).isEqualTo(surveyUnit.data()); + assertThat(surveyUnitDto.comment()).isEqualTo(surveyUnit.comment()); + assertThat(surveyUnitDto.personalization()).isEqualTo(surveyUnit.personalization()); + assertThat(surveyUnitDto.stateData().state()).isEqualTo(surveyUnit.stateData().state()); + assertThat(surveyUnitDto.stateData().currentPage()).isEqualTo(surveyUnit.stateData().currentPage()); + assertThat(surveyUnitDto.stateData().date()).isEqualTo(surveyUnit.stateData().date()); + assertThat(surveyUnitDto.questionnaireId()).isEqualTo(surveyUnit.questionnaireId()); + } + + @Test + @DisplayName("Should throw exception when role is reviewer and campaign is sensitive") + void testGetSurveyUnitException() { + // given + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAuthenticatedUser(AuthorityRoleEnum.REVIEWER)); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when & then + assertThatThrownBy(() -> surveyUnitController.getSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT3_ID)) + .isInstanceOf(AccessDeniedException.class); + assertThat(pilotageComponent.isChecked()).isTrue(); + } + + @ParameterizedTest + @MethodSource("provideAdminAndWebclientUsers") + @DisplayName("Should return survey unit with data when campaign is sensitive and role is admin/webclient") + void testGetSurveyUnit02() { + // given + authenticatedUserHelper.setAuthenticationUser(authenticationUserProvider.getAdminUser()); + + // when + SurveyUnitDto surveyUnitDto = surveyUnitController.getSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + + // then + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(SurveyUnitFakeService.SURVEY_UNIT3_ID); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + assertThat(pilotageComponent.isChecked()).isTrue(); + assertThat(surveyUnitDto.id()).isEqualTo(surveyUnit.id()); + assertThat(surveyUnitDto.data()).isEqualTo(surveyUnit.data()); + assertThat(surveyUnitDto.comment()).isEqualTo(surveyUnit.comment()); + assertThat(surveyUnitDto.personalization()).isEqualTo(surveyUnit.personalization()); + assertThat(surveyUnitDto.stateData().state()).isEqualTo(surveyUnit.stateData().state()); + assertThat(surveyUnitDto.stateData().currentPage()).isEqualTo(surveyUnit.stateData().currentPage()); + assertThat(surveyUnitDto.stateData().date()).isEqualTo(surveyUnit.stateData().date()); + assertThat(surveyUnitDto.questionnaireId()).isEqualTo(surveyUnit.questionnaireId()); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsers") + @DisplayName("Should return survey unit with data when campaign is sensitive and role is interviewer/survey-unit") + void testGetSurveyUnit03(Authentication auth) { + // given + authenticatedUserHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + // when + SurveyUnitDto surveyUnitDto = surveyUnitController.getSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT3_ID); + + // then + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(SurveyUnitFakeService.SURVEY_UNIT3_ID); + + assertThat(pilotageComponent.isChecked()).isTrue(); + assertThat(surveyUnitDto.id()).isEqualTo(surveyUnit.id()); + assertThat(surveyUnitDto.data()).isEqualTo(surveyUnit.data()); + assertThat(surveyUnitDto.comment()).isEqualTo(surveyUnit.comment()); + assertThat(surveyUnitDto.personalization()).isEqualTo(surveyUnit.personalization()); + assertThat(surveyUnitDto.stateData().state()).isEqualTo(surveyUnit.stateData().state()); + assertThat(surveyUnitDto.stateData().currentPage()).isEqualTo(surveyUnit.stateData().currentPage()); + assertThat(surveyUnitDto.stateData().date()).isEqualTo(surveyUnit.stateData().date()); + assertThat(surveyUnitDto.questionnaireId()).isEqualTo(surveyUnit.questionnaireId()); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsersWithExtractedOrValidatedStates") + @DisplayName("Should return survey unit without data when campaign is sensitive, role is interviewer/survey-unit and state is EXTRACTED/VALIDATED") + void testGetSurveyUnit04(Authentication auth, String surveyUnitId) { + // given + authenticatedUserHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(surveyUnitId); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(surveyUnitId); + assertThat(surveyUnit.stateData().state()).isIn(StateDataType.EXTRACTED, StateDataType.VALIDATED); + + // when + SurveyUnitDto surveyUnitDto = surveyUnitController.getSurveyUnitById(surveyUnitId); + + // then + + assertThat(pilotageComponent.isChecked()).isTrue(); + assertThat(surveyUnitDto.id()).isEqualTo(surveyUnit.id()); + assertThat(surveyUnitDto.data()).isNotEqualTo(surveyUnit.data()); + assertThat(surveyUnitDto.data()).isEqualTo(JsonNodeFactory.instance.objectNode()); + assertThat(surveyUnitDto.comment()).isEqualTo(surveyUnit.comment()); + assertThat(surveyUnitDto.personalization()).isEqualTo(surveyUnit.personalization()); + assertThat(surveyUnitDto.stateData().state()).isEqualTo(surveyUnit.stateData().state()); + assertThat(surveyUnitDto.stateData().currentPage()).isEqualTo(surveyUnit.stateData().currentPage()); + assertThat(surveyUnitDto.stateData().date()).isEqualTo(surveyUnit.stateData().date()); + assertThat(surveyUnitDto.questionnaireId()).isEqualTo(surveyUnit.questionnaireId()); + } + + @ParameterizedTest + @MethodSource("provideInterviewerAndSuUsers") + @DisplayName("Should return survey unit with data when campaign is sensitive, role is interviewer/survey-unit, and state data is null") + void testGetSurveyUnit05(Authentication auth) { + // given + authenticatedUserHelper.setAuthenticationUser(auth); + SurveyUnitSummary surveyUnitSummary = surveyUnitFakeService.getSummaryById(SurveyUnitFakeService.SURVEY_UNIT6_ID); + assertThat(surveyUnitSummary.campaign().getSensitivity()).isEqualTo(CampaignSensitivity.SENSITIVE); + + SurveyUnit surveyUnit = surveyUnitFakeService.getSurveyUnit(SurveyUnitFakeService.SURVEY_UNIT6_ID); + assertThat(surveyUnit.stateData()).isNull(); + + // when + SurveyUnitDto surveyUnitDto = surveyUnitController.getSurveyUnitById(SurveyUnitFakeService.SURVEY_UNIT6_ID); + + // then + assertThat(pilotageComponent.isChecked()).isTrue(); + assertThat(surveyUnitDto.id()).isEqualTo(surveyUnit.id()); + assertThat(surveyUnitDto.data()).isEqualTo(surveyUnit.data()); + assertThat(surveyUnitDto.comment()).isEqualTo(surveyUnit.comment()); + assertThat(surveyUnitDto.personalization()).isEqualTo(surveyUnit.personalization()); + assertThat(surveyUnitDto.stateData()).isNull(); + assertThat(surveyUnitDto.questionnaireId()).isEqualTo(surveyUnit.questionnaireId()); + } + + private static Stream provideInterviewerAndSuUsers() { + AuthenticatedUserTestHelper provider = new AuthenticatedUserTestHelper(); + return Stream.of( + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER)), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.SURVEY_UNIT))); + } + + private static Stream provideAdminAndWebclientUsers() { + AuthenticatedUserTestHelper provider = new AuthenticatedUserTestHelper(); + return Stream.of( + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.WEBCLIENT)), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.ADMIN))); + } + + private static Stream provideInterviewerAndSuUsersWithExtractedOrValidatedStates() { + AuthenticatedUserTestHelper provider = new AuthenticatedUserTestHelper(); + return Stream.of( + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER), SurveyUnitFakeService.SURVEY_UNIT4_ID), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.INTERVIEWER), SurveyUnitFakeService.SURVEY_UNIT5_ID), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.SURVEY_UNIT), SurveyUnitFakeService.SURVEY_UNIT4_ID), + Arguments.of(provider.getAuthenticatedUser(AuthorityRoleEnum.SURVEY_UNIT), SurveyUnitFakeService.SURVEY_UNIT5_ID)); + } } diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/integration/cache/SurveyUnitCacheTests.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/integration/cache/SurveyUnitCacheTests.java index be83a584..d954ffbf 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/integration/cache/SurveyUnitCacheTests.java +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/integration/cache/SurveyUnitCacheTests.java @@ -116,7 +116,7 @@ void check_surveyUnit_campaign_cache_01() throws StateDataInvalidDateException { String surveyUnitId = "survey-unit-campaign-cache-id"; // check cache is null at beginning - assertThatThrownBy(() -> surveyUnitService.getSurveyUnitWithCampaignById(surveyUnitId)).isInstanceOf(EntityNotFoundException.class); + assertThatThrownBy(() -> surveyUnitService.getSummaryById(surveyUnitId)).isInstanceOf(EntityNotFoundException.class); assertThat(Objects.requireNonNull(cacheManager.getCache(CacheName.SURVEY_UNIT_SUMMARY)).get(surveyUnitId)).isNull(); SurveyUnit surveyUnit = new SurveyUnit(surveyUnitId, @@ -131,7 +131,7 @@ void check_surveyUnit_campaign_cache_01() throws StateDataInvalidDateException { // not retrieving yet so no survey unit in cache assertThat(Objects.requireNonNull(cacheManager.getCache(CacheName.SURVEY_UNIT_SUMMARY)).get(surveyUnitId)).isNull(); - SurveyUnitSummary expectedSurveyUnitSummary = surveyUnitService.getSurveyUnitWithCampaignById(surveyUnitId); + SurveyUnitSummary expectedSurveyUnitSummary = surveyUnitService.getSummaryById(surveyUnitId); // now survey unit is in cache SurveyUnitSummary surveyUnitSummary = Objects.requireNonNull(cacheManager.getCache(CacheName.SURVEY_UNIT_SUMMARY).get(surveyUnitId, SurveyUnitSummary.class)); @@ -150,7 +150,7 @@ void check_surveyUnit_campaign_cache_02() throws StateDataInvalidDateException { String surveyUnitId = "survey-unit-campaign-cache-id"; String campaignId = "LOG2021X11Tel"; // check cache is null at beginning - assertThatThrownBy(() -> surveyUnitService.getSurveyUnitWithCampaignById(surveyUnitId)).isInstanceOf(EntityNotFoundException.class); + assertThatThrownBy(() -> surveyUnitService.getSummaryById(surveyUnitId)).isInstanceOf(EntityNotFoundException.class); assertThat(Objects.requireNonNull(cacheManager.getCache(CacheName.SURVEY_UNIT_SUMMARY)).get(surveyUnitId)).isNull(); SurveyUnit surveyUnit = new SurveyUnit(surveyUnitId, @@ -165,7 +165,7 @@ void check_surveyUnit_campaign_cache_02() throws StateDataInvalidDateException { // not retrieving yet so no survey unit in cache assertThat(Objects.requireNonNull(cacheManager.getCache(CacheName.SURVEY_UNIT_SUMMARY)).get(surveyUnitId)).isNull(); - SurveyUnitSummary expectedSurveyUnitSummary = surveyUnitService.getSurveyUnitWithCampaignById(surveyUnitId); + SurveyUnitSummary expectedSurveyUnitSummary = surveyUnitService.getSummaryById(surveyUnitId); // now survey unit is in cache SurveyUnitSummary surveyUnitSummary = Objects.requireNonNull(cacheManager.getCache(CacheName.SURVEY_UNIT_SUMMARY).get(surveyUnitId, SurveyUnitSummary.class)); diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/DataFakeService.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/DataFakeService.java index dee93d4c..dc1ae2df 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/DataFakeService.java +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/DataFakeService.java @@ -1,25 +1,28 @@ package fr.insee.queen.application.surveyunit.service.dummy; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import fr.insee.queen.domain.surveyunit.service.DataService; import lombok.Getter; public class DataFakeService implements DataService { @Getter - private boolean checkUpdateCollectedData = false; + private boolean checkUpdateData = false; @Override public ObjectNode getData(String surveyUnitId) { - return null; + ObjectNode data = JsonNodeFactory.instance.objectNode(); + data.put("data", "data-value"); + return data; } @Override public void saveData(String surveyUnitId, ObjectNode dataValue) { - // not used for test at this moment + checkUpdateData = true; } @Override public void updateCollectedData(String surveyUnitId, ObjectNode collectedData) { - checkUpdateCollectedData = true; + checkUpdateData = true; } } diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/StateDataFakeService.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/StateDataFakeService.java index 3daddf4c..7f2f7857 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/StateDataFakeService.java +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/StateDataFakeService.java @@ -1,11 +1,16 @@ package fr.insee.queen.application.surveyunit.service.dummy; import fr.insee.queen.domain.surveyunit.model.StateData; +import fr.insee.queen.domain.surveyunit.model.StateDataType; import fr.insee.queen.domain.surveyunit.service.StateDataService; import fr.insee.queen.domain.surveyunit.service.exception.StateDataInvalidDateException; import lombok.Getter; import lombok.Setter; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + public class StateDataFakeService implements StateDataService { public static final String ERROR_MESSAGE = "error"; @@ -15,9 +20,21 @@ public class StateDataFakeService implements StateDataService { @Setter private boolean isDateInvalid = false; + private final Map stateDatas; + + public StateDataFakeService() { + stateDatas = new HashMap<>(); + stateDatas.put(SurveyUnitFakeService.SURVEY_UNIT1_ID, new StateData(StateDataType.INIT, 123456789L, "1.0")); + stateDatas.put(SurveyUnitFakeService.SURVEY_UNIT2_ID, new StateData(StateDataType.VALIDATED, 12345678910L, "2.0")); + stateDatas.put(SurveyUnitFakeService.SURVEY_UNIT3_ID, new StateData(StateDataType.INIT, 1234567891011L, "3.0")); + stateDatas.put(SurveyUnitFakeService.SURVEY_UNIT4_ID, new StateData(StateDataType.VALIDATED, 1234567891011L, "3.0")); + stateDatas.put(SurveyUnitFakeService.SURVEY_UNIT5_ID, new StateData(StateDataType.EXTRACTED, 1234567891011L, "3.0")); + stateDatas.put(SurveyUnitFakeService.SURVEY_UNIT6_ID, null); + } + @Override public StateData getStateData(String surveyUnitId) { - return null; + return stateDatas.getOrDefault(surveyUnitId, null); } @Override @@ -27,4 +44,9 @@ public void saveStateData(String surveyUnitId, StateData stateData) throws State } stateDataSaved = stateData; } + + @Override + public Optional findStateData(String surveyUnitId) { + return Optional.ofNullable(stateDatas.getOrDefault(surveyUnitId, null)); + } } diff --git a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/SurveyUnitFakeService.java b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/SurveyUnitFakeService.java index 6ea13144..636066dd 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/SurveyUnitFakeService.java +++ b/queen-application/src/test/java/fr/insee/queen/application/surveyunit/service/dummy/SurveyUnitFakeService.java @@ -1,20 +1,32 @@ package fr.insee.queen.application.surveyunit.service.dummy; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.campaign.model.CampaignSummary; +import fr.insee.queen.domain.common.exception.EntityNotFoundException; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import lombok.Getter; import lombok.Setter; -import java.util.ArrayList; import java.util.List; import java.util.Optional; public class SurveyUnitFakeService implements SurveyUnitService { + private final StateDataFakeService stateDataFakeService; public static final String SURVEY_UNIT1_ID = "survey-unit1"; public static final String SURVEY_UNIT2_ID = "survey-unit2"; + public static final String SURVEY_UNIT3_ID = "survey-unit3"; + public static final String SURVEY_UNIT4_ID = "survey-unit4"; + public static final String SURVEY_UNIT5_ID = "survey-unit5"; + public static final String SURVEY_UNIT6_ID = "survey-unit6"; + @Getter + private final List surveyUnits; + @Getter + private final List surveyUnitSummaries; @Setter private boolean surveyUnitExist = true; @Getter @@ -30,12 +42,44 @@ public class SurveyUnitFakeService implements SurveyUnitService { @Getter private SurveyUnit surveyUnitUpdated; - @Getter - private final List surveyUnitSummaries = List.of( - new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", "campaign-id"), - new SurveyUnitSummary("survey-unit2", "questionnaire-id", "campaign-id"), - new SurveyUnitSummary("survey-unit3", "questionnaire-id", "campaign-id") - ); + public SurveyUnitFakeService() { + stateDataFakeService = new StateDataFakeService(); + + CampaignSummary normalCampaign = new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL); + CampaignSummary sensitiveCampaign = new CampaignSummary("campaign-id2", "campaign-label2", CampaignSensitivity.SENSITIVE); + surveyUnitSummaries = List.of( + new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", normalCampaign), + new SurveyUnitSummary(SURVEY_UNIT2_ID, "questionnaire-id", normalCampaign), + new SurveyUnitSummary(SURVEY_UNIT3_ID, "questionnaire-id", sensitiveCampaign), + new SurveyUnitSummary(SURVEY_UNIT4_ID, "questionnaire-id", sensitiveCampaign), + new SurveyUnitSummary(SURVEY_UNIT5_ID, "questionnaire-id", sensitiveCampaign), + new SurveyUnitSummary(SURVEY_UNIT6_ID, "questionnaire-id", sensitiveCampaign) + ); + + ObjectNode data = JsonNodeFactory.instance.objectNode(); + data.put("data", "data-value"); + + surveyUnits = List.of( + new SurveyUnit(SURVEY_UNIT1_ID, normalCampaign.getId(), "questionnaire-id", + JsonNodeFactory.instance.arrayNode(), data, + JsonNodeFactory.instance.objectNode(), stateDataFakeService.getStateData(SURVEY_UNIT1_ID)), + new SurveyUnit(SURVEY_UNIT2_ID, normalCampaign.getId(), "questionnaire-id", + JsonNodeFactory.instance.arrayNode(), data, + JsonNodeFactory.instance.objectNode(), stateDataFakeService.getStateData(SURVEY_UNIT2_ID)), + new SurveyUnit(SURVEY_UNIT3_ID, sensitiveCampaign.getId(), "questionnaire-id", + JsonNodeFactory.instance.arrayNode(), data, + JsonNodeFactory.instance.objectNode(), stateDataFakeService.getStateData(SURVEY_UNIT3_ID)), + new SurveyUnit(SURVEY_UNIT4_ID, sensitiveCampaign.getId(), "questionnaire-id", + JsonNodeFactory.instance.arrayNode(), data, + JsonNodeFactory.instance.objectNode(), stateDataFakeService.getStateData(SURVEY_UNIT4_ID)), + new SurveyUnit(SURVEY_UNIT5_ID, sensitiveCampaign.getId(), "questionnaire-id", + JsonNodeFactory.instance.arrayNode(), data, + JsonNodeFactory.instance.objectNode(), stateDataFakeService.getStateData(SURVEY_UNIT5_ID)), + new SurveyUnit(SURVEY_UNIT6_ID, sensitiveCampaign.getId(), "questionnaire-id", + JsonNodeFactory.instance.arrayNode(), data, + JsonNodeFactory.instance.objectNode(), stateDataFakeService.getStateData(SURVEY_UNIT6_ID)) + ); + } @Override public void throwExceptionIfSurveyUnitNotExist(String surveyUnitId) { @@ -54,7 +98,11 @@ public boolean existsById(String surveyUnitId) { @Override public SurveyUnit getSurveyUnit(String id) { - return null; + return surveyUnits + .stream() + .filter(surveyUnit -> surveyUnit.id().equals(id)) + .findFirst() + .orElseThrow(() -> new EntityNotFoundException("survey unit not found")); } @Override @@ -76,6 +124,7 @@ public void updateSurveyUnit(SurveyUnit surveyUnit) { @Override public void updateSurveyUnit(String surveyUnitId, ObjectNode data, StateData stateData) { checkSurveyUnitUpdate = true; + surveyUnitUpdated = new SurveyUnit(surveyUnitId, null, null, null, data, null, stateData); } @Override @@ -85,16 +134,18 @@ public void createSurveyUnit(SurveyUnit surveyUnit) { @Override public List findSummariesByIds(List surveyUnitIds) { - List surveyUnits = new ArrayList<>(); - - surveyUnitIds.forEach(surveyUnitId -> surveyUnits.add(new SurveyUnitSummary(surveyUnitId, "questionnaire-id", "campaign-id"))); - return surveyUnits; + return surveyUnitSummaries + .stream() + .filter(surveyUnitSummary -> surveyUnitIds.contains(surveyUnitSummary.id())) + .toList(); } @Override public Optional findSummaryById(String surveyUnitId) { - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(surveyUnitId, "questionnaire-id", "campaign-id"); - return Optional.of(surveyUnit); + return surveyUnitSummaries + .stream() + .filter(surveyUnitSummary -> surveyUnitSummary.id().equals(surveyUnitId)) + .findFirst(); } @Override @@ -118,8 +169,12 @@ public SurveyUnitMetadata getSurveyUnitMetadata(String surveyUnitId) { } @Override - public SurveyUnitSummary getSurveyUnitWithCampaignById(String surveyUnitId) { - return new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", "campaign-id"); + public SurveyUnitSummary getSummaryById(String surveyUnitId) { + return surveyUnitSummaries + .stream() + .filter(surveyUnitSummary -> surveyUnitSummary.id().equals(surveyUnitId)) + .findFirst() + .orElseThrow(() -> new EntityNotFoundException("survey unit not found")); } @Override diff --git a/queen-application/src/test/java/fr/insee/queen/application/utils/dummy/AuthenticationFakeHelper.java b/queen-application/src/test/java/fr/insee/queen/application/utils/dummy/AuthenticationFakeHelper.java index 8cdea6e0..81238cae 100644 --- a/queen-application/src/test/java/fr/insee/queen/application/utils/dummy/AuthenticationFakeHelper.java +++ b/queen-application/src/test/java/fr/insee/queen/application/utils/dummy/AuthenticationFakeHelper.java @@ -1,13 +1,21 @@ package fr.insee.queen.application.utils.dummy; +import fr.insee.queen.application.configuration.auth.AuthorityRoleEnum; import fr.insee.queen.application.web.authentication.AuthenticationHelper; -import lombok.RequiredArgsConstructor; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import lombok.Setter; import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; -@RequiredArgsConstructor +import java.util.Arrays; + +@AllArgsConstructor +@NoArgsConstructor public class AuthenticationFakeHelper implements AuthenticationHelper { - private final Authentication authenticationUser; + @Setter + private Authentication authenticationUser; @Override public String getUserToken() { @@ -21,4 +29,13 @@ public String getUserToken() { public Authentication getAuthenticationPrincipal() { return authenticationUser; } + + @Override + public boolean hasRole(AuthorityRoleEnum... roles) { + return getAuthenticationPrincipal().getAuthorities() + .stream() + .map(GrantedAuthority::getAuthority) + .map(AuthorityRoleEnum::fromAuthority) + .anyMatch(role -> Arrays.asList(roles).contains(role)); + } } diff --git a/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/pilotage/infrastructure/dummy/PilotageFakeRepository.java b/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/pilotage/infrastructure/dummy/PilotageFakeRepository.java index 08f251ca..d10d97d9 100644 --- a/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/pilotage/infrastructure/dummy/PilotageFakeRepository.java +++ b/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/pilotage/infrastructure/dummy/PilotageFakeRepository.java @@ -13,7 +13,7 @@ public class PilotageFakeRepository implements PilotageRepository { public static final String INTERVIEWER_CAMPAIGN1_ID = "interviewer-campaign1"; - public static final String CURRENT_SU_CAMPAIGN1_ID = "su-campaign1"; + public static final String CURRENT_SU_CAMPAIGN1_ID = "campaign-id"; public static final String SURVEY_UNIT1_ID = "pilotage-su-1"; public static final String SURVEY_UNIT2_ID = "pilotage-su-2"; @@ -41,7 +41,7 @@ public List getSurveyUnits() { } return List.of( new PilotageSurveyUnit(SURVEY_UNIT1_ID, CURRENT_SU_CAMPAIGN1_ID), - new PilotageSurveyUnit(SURVEY_UNIT2_ID, "su-campaign2"), + new PilotageSurveyUnit(SURVEY_UNIT2_ID, "campaign-id2"), new PilotageSurveyUnit(SURVEY_UNIT3_ID, CURRENT_SU_CAMPAIGN1_ID) ); } diff --git a/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/pilotage/service/PilotageServiceTest.java b/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/pilotage/service/PilotageServiceTest.java index d730327a..60d664c8 100644 --- a/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/pilotage/service/PilotageServiceTest.java +++ b/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/pilotage/service/PilotageServiceTest.java @@ -1,5 +1,7 @@ package fr.insee.queen.domain.pilotage.service; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.campaign.model.CampaignSummary; import fr.insee.queen.domain.campaign.service.dummy.CampaignExistenceFakeService; import fr.insee.queen.domain.campaign.service.dummy.QuestionnaireModelFakeService; import fr.insee.queen.domain.pilotage.infrastructure.dummy.PilotageFakeRepository; @@ -67,7 +69,7 @@ void testGetInterviewerCampaigns03() { @Test @DisplayName("On check habilitation, when role == INTERVIEWER return true") void testHasHabilitation_01() { - SurveyUnitSummary su = new SurveyUnitSummary("su-id", "questionnaire-id", "campaign-id"); + SurveyUnitSummary su = new SurveyUnitSummary("su-id", "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)); boolean hasHabilitation = pilotageService.hasHabilitation(su, PilotageRole.INTERVIEWER, "idep"); assertThat(hasHabilitation).isTrue(); } @@ -75,7 +77,7 @@ void testHasHabilitation_01() { @Test @DisplayName("On check habilitation, when role == REVIEWER return true") void testHasHabilitation_02() { - SurveyUnitSummary su = new SurveyUnitSummary("su-id", "questionnaire-id", "campaign-id"); + SurveyUnitSummary su = new SurveyUnitSummary("su-id", "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)); boolean hasHabilitation = pilotageService.hasHabilitation(su, PilotageRole.REVIEWER, "idep"); assertThat(hasHabilitation).isTrue(); } diff --git a/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java b/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java index cf0e4da7..aa0cfe02 100644 --- a/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java +++ b/queen-domain-pilotage/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java @@ -1,6 +1,8 @@ package fr.insee.queen.domain.surveyunit.service.dummy; import com.fasterxml.jackson.databind.node.ObjectNode; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.campaign.model.CampaignSummary; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import lombok.Getter; @@ -25,9 +27,9 @@ public class SurveyUnitFakeService implements SurveyUnitService { @Getter private final List surveyUnitSummaries = List.of( - new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", "campaign-id"), - new SurveyUnitSummary("survey-unit2", "questionnaire-id", "campaign-id"), - new SurveyUnitSummary("survey-unit3", "questionnaire-id", "campaign-id") + new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)), + new SurveyUnitSummary("survey-unit2", "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)), + new SurveyUnitSummary("survey-unit3", "questionnaire-id", new CampaignSummary("campaign-id2", "campaign-label", CampaignSensitivity.SENSITIVE)) ); @Override @@ -79,13 +81,13 @@ public void createSurveyUnit(SurveyUnit surveyUnit) { public List findSummariesByIds(List surveyUnitIds) { List surveyUnits = new ArrayList<>(); - surveyUnitIds.forEach(surveyUnitId -> surveyUnits.add(new SurveyUnitSummary(surveyUnitId, "questionnaire-id", "campaign-id"))); + surveyUnitIds.forEach(surveyUnitId -> surveyUnits.add(new SurveyUnitSummary(surveyUnitId, "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)))); return surveyUnits; } @Override public Optional findSummaryById(String surveyUnitId) { - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(surveyUnitId, "questionnaire-id", "campaign-id"); + SurveyUnitSummary surveyUnit = new SurveyUnitSummary(surveyUnitId, "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)); return Optional.of(surveyUnit); } @@ -110,10 +112,11 @@ public SurveyUnitMetadata getSurveyUnitMetadata(String surveyUnitId) { } @Override - public SurveyUnitSummary getSurveyUnitWithCampaignById(String surveyUnitId) { - return new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", "campaign-id"); + public SurveyUnitSummary getSummaryById(String surveyUnitId) { + return new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)); } + @Override public List findByIds(List surveyUnitIds) { return null; diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/campaign/model/CampaignSummary.java b/queen-domain/src/main/java/fr/insee/queen/domain/campaign/model/CampaignSummary.java index c1d7ffba..e1f264d7 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/campaign/model/CampaignSummary.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/campaign/model/CampaignSummary.java @@ -9,17 +9,20 @@ public class CampaignSummary { private final String id; private final String label; + private final CampaignSensitivity sensitivity; private final Set questionnaireIds; - public CampaignSummary(String id, String label, Set questionnaireIds) { + public CampaignSummary(String id, String label, CampaignSensitivity sensitivity, Set questionnaireIds) { this.id = id; this.label = label; + this.sensitivity = sensitivity; this.questionnaireIds = questionnaireIds; } - public CampaignSummary(String id, String label) { + public CampaignSummary(String id, String label, CampaignSensitivity sensitivity) { this.id = id; this.label = label; + this.sensitivity = sensitivity; this.questionnaireIds = new HashSet<>(); } } \ No newline at end of file diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/model/SurveyUnitSummary.java b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/model/SurveyUnitSummary.java index aaf697e1..e3854a62 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/model/SurveyUnitSummary.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/model/SurveyUnitSummary.java @@ -1,7 +1,9 @@ package fr.insee.queen.domain.surveyunit.model; +import fr.insee.queen.domain.campaign.model.CampaignSummary; + public record SurveyUnitSummary( String id, String questionnaireId, - String campaignId) { + CampaignSummary campaign) { } diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/StateDataApiService.java b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/StateDataApiService.java index 97fe8a5e..1ecd3b98 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/StateDataApiService.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/StateDataApiService.java @@ -23,10 +23,15 @@ public class StateDataApiService implements StateDataService { @Override public StateData getStateData(String surveyUnitId) { - return stateDataRepository.find(surveyUnitId) + return findStateData(surveyUnitId) .orElseThrow(() -> new EntityNotFoundException(String.format(NOT_FOUND_MESSAGE, surveyUnitId))); } + @Override + public Optional findStateData(String surveyUnitId) { + return stateDataRepository.find(surveyUnitId); + } + @Override @Transactional public void saveStateData(String surveyUnitId, StateData stateData) throws StateDataInvalidDateException { diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/StateDataService.java b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/StateDataService.java index 989cb6e3..a028b919 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/StateDataService.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/StateDataService.java @@ -3,8 +3,12 @@ import fr.insee.queen.domain.surveyunit.service.exception.StateDataInvalidDateException; import fr.insee.queen.domain.surveyunit.model.StateData; +import java.util.Optional; + public interface StateDataService { StateData getStateData(String surveyUnitId); void saveStateData(String surveyUnitId, StateData stateData) throws StateDataInvalidDateException; + + Optional findStateData(String surveyUnitId); } diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitApiService.java b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitApiService.java index 04710f28..b2883dd7 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitApiService.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitApiService.java @@ -190,7 +190,7 @@ public SurveyUnitMetadata getSurveyUnitMetadata(String surveyUnitId) { } @Override - public SurveyUnitSummary getSurveyUnitWithCampaignById(String surveyUnitId) { + public SurveyUnitSummary getSummaryById(String surveyUnitId) { return findSummaryById(surveyUnitId) .orElseThrow(() -> new EntityNotFoundException(String.format(NOT_FOUND_MESSAGE, surveyUnitId))); } diff --git a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitService.java b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitService.java index 269ab8ff..f8adb761 100644 --- a/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitService.java +++ b/queen-domain/src/main/java/fr/insee/queen/domain/surveyunit/service/SurveyUnitService.java @@ -38,7 +38,7 @@ public interface SurveyUnitService { SurveyUnitMetadata getSurveyUnitMetadata(String surveyUnitId); - SurveyUnitSummary getSurveyUnitWithCampaignById(String surveyUnitId); + SurveyUnitSummary getSummaryById(String surveyUnitId); List findByIds(List surveyUnitIds); diff --git a/queen-domain/src/test/java/fr/insee/queen/domain/campaign/infrastructure/dummy/CampaignFakeRepository.java b/queen-domain/src/test/java/fr/insee/queen/domain/campaign/infrastructure/dummy/CampaignFakeRepository.java index 9297468f..3a68f81a 100644 --- a/queen-domain/src/test/java/fr/insee/queen/domain/campaign/infrastructure/dummy/CampaignFakeRepository.java +++ b/queen-domain/src/test/java/fr/insee/queen/domain/campaign/infrastructure/dummy/CampaignFakeRepository.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import fr.insee.queen.domain.campaign.gateway.CampaignRepository; import fr.insee.queen.domain.campaign.model.Campaign; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.campaign.model.CampaignSummary; import lombok.Setter; @@ -47,6 +48,7 @@ public Optional findWithQuestionnaireIds(String campaignId) { if(campaignExists) { return Optional.of(new CampaignSummary(CAMPAIGN_ID, "label", + CampaignSensitivity.NORMAL, Set.of(QUESTIONNAIRE_LINKED_ID))); } return Optional.empty(); diff --git a/queen-domain/src/test/java/fr/insee/queen/domain/campaign/service/dummy/CampaignFakeService.java b/queen-domain/src/test/java/fr/insee/queen/domain/campaign/service/dummy/CampaignFakeService.java index 0771b885..bf33c447 100644 --- a/queen-domain/src/test/java/fr/insee/queen/domain/campaign/service/dummy/CampaignFakeService.java +++ b/queen-domain/src/test/java/fr/insee/queen/domain/campaign/service/dummy/CampaignFakeService.java @@ -1,5 +1,6 @@ package fr.insee.queen.domain.campaign.service.dummy; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; import fr.insee.queen.domain.campaign.service.CampaignService; import fr.insee.queen.domain.campaign.model.Campaign; import fr.insee.queen.domain.campaign.model.CampaignSummary; @@ -16,8 +17,8 @@ public class CampaignFakeService implements CampaignService { private boolean created = false; public static final String CAMPAIGN1_ID = "allCampaigns1"; public static final List CAMPAIGN_SUMMARY_LIST = List.of( - new CampaignSummary(CAMPAIGN1_ID, "label", Set.of("questionnaireId1", "questionnaireId2")), - new CampaignSummary("allCampaigns2", "label", Set.of("questionnaireId1", "questionnaireId2")) + new CampaignSummary(CAMPAIGN1_ID, "label", CampaignSensitivity.NORMAL, Set.of("questionnaireId1", "questionnaireId2")), + new CampaignSummary("allCampaigns2", "label", CampaignSensitivity.SENSITIVE, Set.of("questionnaireId1", "questionnaireId2")) ); @Override diff --git a/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/StateDataFakeService.java b/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/StateDataFakeService.java index 69ab3990..c8c46f60 100644 --- a/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/StateDataFakeService.java +++ b/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/StateDataFakeService.java @@ -6,6 +6,8 @@ import lombok.Getter; import lombok.Setter; +import java.util.Optional; + public class StateDataFakeService implements StateDataService { public static final String ERROR_MESSAGE = "error"; @@ -27,4 +29,9 @@ public void saveStateData(String surveyUnitId, StateData stateData) throws State } stateDataSaved = stateData; } + + @Override + public Optional findStateData(String surveyUnitId) { + return Optional.empty(); + } } diff --git a/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java b/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java index cf0e4da7..c8ec5a10 100644 --- a/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java +++ b/queen-domain/src/test/java/fr/insee/queen/domain/surveyunit/service/dummy/SurveyUnitFakeService.java @@ -1,12 +1,14 @@ package fr.insee.queen.domain.surveyunit.service.dummy; import com.fasterxml.jackson.databind.node.ObjectNode; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.campaign.model.CampaignSummary; +import fr.insee.queen.domain.common.exception.EntityNotFoundException; import fr.insee.queen.domain.surveyunit.model.*; import fr.insee.queen.domain.surveyunit.service.SurveyUnitService; import lombok.Getter; import lombok.Setter; -import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -25,9 +27,9 @@ public class SurveyUnitFakeService implements SurveyUnitService { @Getter private final List surveyUnitSummaries = List.of( - new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", "campaign-id"), - new SurveyUnitSummary("survey-unit2", "questionnaire-id", "campaign-id"), - new SurveyUnitSummary("survey-unit3", "questionnaire-id", "campaign-id") + new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)), + new SurveyUnitSummary("survey-unit2", "questionnaire-id", new CampaignSummary("campaign-id", "campaign-label", CampaignSensitivity.NORMAL)), + new SurveyUnitSummary("survey-unit3", "questionnaire-id", new CampaignSummary("campaign-id2", "campaign-label", CampaignSensitivity.SENSITIVE)) ); @Override @@ -77,16 +79,18 @@ public void createSurveyUnit(SurveyUnit surveyUnit) { @Override public List findSummariesByIds(List surveyUnitIds) { - List surveyUnits = new ArrayList<>(); - - surveyUnitIds.forEach(surveyUnitId -> surveyUnits.add(new SurveyUnitSummary(surveyUnitId, "questionnaire-id", "campaign-id"))); - return surveyUnits; + return surveyUnitSummaries + .stream() + .filter(surveyUnitSummary -> surveyUnitIds.contains(surveyUnitSummary.id())) + .toList(); } @Override public Optional findSummaryById(String surveyUnitId) { - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(surveyUnitId, "questionnaire-id", "campaign-id"); - return Optional.of(surveyUnit); + return surveyUnitSummaries + .stream() + .filter(surveyUnitSummary -> surveyUnitSummary.id().equals(surveyUnitId)) + .findFirst(); } @Override @@ -110,8 +114,9 @@ public SurveyUnitMetadata getSurveyUnitMetadata(String surveyUnitId) { } @Override - public SurveyUnitSummary getSurveyUnitWithCampaignById(String surveyUnitId) { - return new SurveyUnitSummary(SURVEY_UNIT1_ID, "questionnaire-id", "campaign-id"); + public SurveyUnitSummary getSummaryById(String surveyUnitId) { + return findSummaryById(surveyUnitId) + .orElseThrow(() -> new EntityNotFoundException("survey unit not found")); } @Override diff --git a/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/campaign/repository/CampaignDao.java b/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/campaign/repository/CampaignDao.java index 5895c1af..1b09c437 100644 --- a/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/campaign/repository/CampaignDao.java +++ b/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/campaign/repository/CampaignDao.java @@ -53,6 +53,7 @@ public List getAllWithQuestionnaireIds() { .map(campaign -> new CampaignSummary( campaign.getId(), campaign.getLabel(), + campaign.getSensitivity(), campaign.getQuestionnaireModels() .stream() .map(QuestionnaireModelDB::getId) @@ -76,6 +77,7 @@ public Optional findWithQuestionnaireIds(String campaignId) { return Optional.of(new CampaignSummary( campaign.getId(), campaign.getLabel(), + campaign.getSensitivity(), campaign.getQuestionnaireModels() .stream() .map(QuestionnaireModelDB::getId) diff --git a/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/jpa/SurveyUnitJpaRepository.java b/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/jpa/SurveyUnitJpaRepository.java index c599e7e1..db315ab5 100644 --- a/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/jpa/SurveyUnitJpaRepository.java +++ b/queen-infra-db/src/main/java/fr/insee/queen/infrastructure/db/surveyunit/repository/jpa/SurveyUnitJpaRepository.java @@ -28,7 +28,10 @@ public interface SurveyUnitJpaRepository extends JpaRepository findSummaryById(String surveyUnitId); @@ -58,7 +61,10 @@ public interface SurveyUnitJpaRepository extends JpaRepository findAllSummaryByCampaignId(String campaignId); @@ -73,7 +79,10 @@ public interface SurveyUnitJpaRepository extends JpaRepository findAllSummaryByIdIn(List surveyUnitIds); @@ -130,7 +139,8 @@ public interface SurveyUnitJpaRepository extends JpaRepository getInterviewerCampaigns() { @Override public boolean hasHabilitation(SurveyUnitSummary surveyUnit, PilotageRole role, String idep) { StringBuilder uriPilotageFilter = new StringBuilder(); - String campaignId = surveyUnit.campaignId(); + String campaignId = surveyUnit.campaign().getId(); if (Pattern.matches(campaignIdRegexWithAlternativeHabilitationService, campaignId)) { log.debug("Current campaignId {} requires an alternative habilitation service {} ", campaignId, alternativeHabilitationServiceURL); diff --git a/queen-infra-pilotage/src/test/java/fr/insee/queen/infrastructure/pilotage/repository/PilotageHttpRepositoryTest.java b/queen-infra-pilotage/src/test/java/fr/insee/queen/infrastructure/pilotage/repository/PilotageHttpRepositoryTest.java index 90af021b..b58d33ee 100644 --- a/queen-infra-pilotage/src/test/java/fr/insee/queen/infrastructure/pilotage/repository/PilotageHttpRepositoryTest.java +++ b/queen-infra-pilotage/src/test/java/fr/insee/queen/infrastructure/pilotage/repository/PilotageHttpRepositoryTest.java @@ -1,5 +1,7 @@ package fr.insee.queen.infrastructure.pilotage.repository; +import fr.insee.queen.domain.campaign.model.CampaignSensitivity; +import fr.insee.queen.domain.campaign.model.CampaignSummary; import fr.insee.queen.domain.pilotage.model.PilotageCampaign; import fr.insee.queen.domain.pilotage.model.PilotageSurveyUnit; import fr.insee.queen.domain.pilotage.service.PilotageRole; @@ -253,7 +255,7 @@ void testHabilitation01(String status) { String idSu = "id-su"; String idep = "idep"; PilotageRole role = PilotageRole.INTERVIEWER; - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", campaignId); + SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", new CampaignSummary(campaignId, "label", CampaignSensitivity.NORMAL)); String habilitationResponse = "{ \"habilitated\": \"" + status + "\" }"; mockServer.expect(request -> @@ -282,7 +284,7 @@ void testHabilitation02() { String idep = "idep"; String matchedRegexCampaignId = "EDT-campaign-id"; PilotageRole role = PilotageRole.REVIEWER; - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", matchedRegexCampaignId); + SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", new CampaignSummary(matchedRegexCampaignId, "label", CampaignSensitivity.NORMAL)); String habilitationResponse = "{ \"habilitated\": \"true\" }"; mockServer.expect(request -> @@ -309,7 +311,7 @@ void testHabilitation03() { String idSu = "id-su"; String idep = "idep"; PilotageRole role = PilotageRole.INTERVIEWER; - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", campaignId); + SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", new CampaignSummary(campaignId, "label", CampaignSensitivity.NORMAL)); mockServer.expect(request -> assertThat(pilotageUrl).isEqualTo(request.getURI().getScheme() + "://" + request.getURI().getHost())) @@ -335,7 +337,7 @@ void testHabilitation04() { String idSu = "id-su"; String idep = "idep"; PilotageRole role = PilotageRole.INTERVIEWER; - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", campaignId); + SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", new CampaignSummary(campaignId, "label", CampaignSensitivity.NORMAL)); mockServer.expect(request -> assertThat(pilotageUrl).isEqualTo(request.getURI().getScheme() + "://" + request.getURI().getHost())) @@ -361,7 +363,7 @@ void testHabilitation05() { String idSu = "id-su"; String idep = "idep"; PilotageRole role = PilotageRole.INTERVIEWER; - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", campaignId); + SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", new CampaignSummary(campaignId, "label", CampaignSensitivity.NORMAL)); mockServer.expect(request -> assertThat(pilotageUrl).isEqualTo(request.getURI().getScheme() + "://" + request.getURI().getHost())) @@ -387,7 +389,7 @@ void testHabilitationNetworkError() { String idSu = "id-su"; String idep = "idep"; PilotageRole role = PilotageRole.INTERVIEWER; - SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", campaignId); + SurveyUnitSummary surveyUnit = new SurveyUnitSummary(idSu, "questionnaire-id", new CampaignSummary(campaignId, "label", CampaignSensitivity.NORMAL)); mockServer.expect(method(HttpMethod.GET)) .andRespond(withException(new IOException("message")));