diff --git a/pom.xml b/pom.xml index 99303154..18d79f46 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ fr.insee Pogues-BO jar - 4.8.2-SNAPSHOT + 4.9.0-SNAPSHOT Pogues-Back-Office diff --git a/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImpl.java b/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImpl.java index b9038a38..10e13f9c 100644 --- a/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImpl.java +++ b/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImpl.java @@ -1,7 +1,9 @@ package fr.insee.pogues.api.remote.eno.transforms; import fr.insee.pogues.exception.EnoException; +import fr.insee.pogues.webservice.model.StudyUnitEnum; import fr.insee.pogues.webservice.rest.PoguesException; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -17,7 +19,6 @@ import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.Map; @@ -29,13 +30,11 @@ public class EnoClientImpl implements EnoClient{ String enoHost; @Autowired - private WebClient webClient; - - private static String DEFAULT_CONTEXT = "DEFAULT"; - - private static final String DSFR_QUERY_PARAM = "dsfr"; + private final WebClient webClient; + private static final String DEFAULT_CONTEXT = "DEFAULT"; private static final String BASE_PATH = "/questionnaire/" + DEFAULT_CONTEXT; + private static final String DSFR_QUERY_PARAM = "dsfr"; private static final String MODE = "CAWI"; public EnoClientImpl(WebClient webClient) { @@ -45,14 +44,12 @@ public EnoClientImpl(WebClient webClient) { @Override public String getDDI32ToDDI33 (String inputAsString) throws EnoException, PoguesException { return callEnoApi(inputAsString, "/questionnaire/ddi32-2-ddi33"); - }; - + } @Override public String getXMLPoguesToDDI (String inputAsString) throws EnoException, PoguesException { return callEnoApi(inputAsString, "/questionnaire/poguesxml-2-ddi"); - }; - + } @Override public String getDDIToFO(String inputAsString) throws EnoException, PoguesException { @@ -66,11 +63,41 @@ public String getDDITOLunaticXML(String inputAsString) throws EnoException, Pogu @Override public String getDDITOLunaticJSON(String inputAsString, Map params) throws EnoException, PoguesException { - MultiValueMap queryParams = new LinkedMultiValueMap<>(); + MultiValueMap queryParams = new LinkedMultiValueMap<>(); String modePathParam = params.get("mode") != null ? params.get("mode").toString() : MODE; - String WSPath = BASE_PATH + "/lunatic-json/" + modePathParam; + + StudyUnitEnum contextRequestParam = getContextParam(params); + String wsPath = buildWSPath(contextRequestParam, modePathParam); + queryParams.add(DSFR_QUERY_PARAM, Boolean.TRUE.equals(params.get("dsfr")) ? "true" : "false"); - return callEnoApiWithParams(inputAsString, WSPath, queryParams); + return callEnoApiWithParams(inputAsString, wsPath, queryParams); + } + + /** + * Retrieves the context parameter from the provided map of parameters. + * This method attempts to fetch the "context" parameter from the specified map + * and casts it to a {@code StudyUnitEnum}. If the parameter is not present or is {@code null}, + * it returns a default value {@code StudyUnitEnum.DEFAULT}. + * @param params a map of parameters where the "context" key may be present, + * associated with a {@code StudyUnitEnum} value. + * @return the {@code StudyUnitEnum} value associated with the "context" key, + * or {@code StudyUnitEnum.DEFAULT} if not found or {@code null}. + */ + static StudyUnitEnum getContextParam(Map params) { + StudyUnitEnum context = (StudyUnitEnum) params.get("context"); + return (context != null) ? context : StudyUnitEnum.DEFAULT; + } + + /** + * Constructs the WSPath for the questionnaire based on the context and mode. + * + * @param context The context of type {@link StudyUnitEnum} used in the path. Cannot be null. + * @param mode The specified mode for the path. Cannot be null. + * @return The WSPath as a string, structured as "questionnaire/{context}/lunatic-json/{mode}". + * @throws NullPointerException if {@code context} or {@code mode} is null. + */ + static String buildWSPath(@NonNull StudyUnitEnum context, @NonNull String mode) { + return "questionnaire/" + context + "/lunatic-json/" + mode; } @Override @@ -79,26 +106,25 @@ public String getDDITOXForms(String inputAsString) throws EnoException, PoguesEx } @Override - public String getJSONPoguesToLunaticJson(String inputAsString, Map params) throws URISyntaxException, IOException, EnoException { + public String getJSONPoguesToLunaticJson(String inputAsString, Map params) throws IOException, EnoException { log.info("getJSONPoguesToLunaticJson - started"); MultiValueMap queryParams = new LinkedMultiValueMap<>(); String modePathParam = params.get("mode") != null ? params.get("mode").toString() : MODE; - String WSPath = String.format("/questionnaire/pogues-2-lunatic/%s/%s", + String wsPath = String.format("/questionnaire/pogues-2-lunatic/%s/%s", DEFAULT_CONTEXT, modePathParam); - log.info("WSPath : {} ",WSPath); + log.info("WSPath : {} ",wsPath); queryParams.add(DSFR_QUERY_PARAM, Boolean.TRUE.equals(params.get("dsfr")) ? "true" : "false"); - return callEnoApiWithParams(inputAsString, WSPath, queryParams); + return callEnoApiWithParams(inputAsString, wsPath, queryParams); } @Override public String getDDIToODT (String inputAsString) throws EnoException, PoguesException { return callEnoApi(inputAsString, BASE_PATH+"/fodt"); - }; - + } @Override - public void getParameters () throws Exception{ + public void getParameters () { URI uri = UriComponentsBuilder .fromHttpUrl(enoHost) .path("/parameters/xml/all") @@ -110,17 +136,17 @@ public void getParameters () throws Exception{ .retrieve().bodyToMono(String.class).block(); log.debug(xmlParams); - }; + } - private String callEnoApi(String inputAsString, String WSPath) throws EnoException, PoguesException { + private String callEnoApi(String inputAsString, String wsPath) throws EnoException, PoguesException { MultiValueMap queryParams = new LinkedMultiValueMap<>(); - return callEnoApiWithParams(inputAsString, WSPath, queryParams); + return callEnoApiWithParams(inputAsString, wsPath, queryParams); } - private String callEnoApiWithParams(String inputAsString, String WSPath, MultiValueMap params) throws EnoException, PoguesException { + private String callEnoApiWithParams(String inputAsString, String wsPath, MultiValueMap params) throws EnoException, PoguesException { URI uri = UriComponentsBuilder .fromHttpUrl(enoHost) - .path(WSPath) + .path(wsPath) .queryParams(params) .build().toUri(); diff --git a/src/main/java/fr/insee/pogues/webservice/rest/VisualizeWithURI.java b/src/main/java/fr/insee/pogues/webservice/rest/VisualizeWithURI.java index 91de4af0..313e4a90 100644 --- a/src/main/java/fr/insee/pogues/webservice/rest/VisualizeWithURI.java +++ b/src/main/java/fr/insee/pogues/webservice/rest/VisualizeWithURI.java @@ -12,13 +12,13 @@ import fr.insee.pogues.transforms.visualize.uri.LunaticJSONToUriStromaeV3; import fr.insee.pogues.transforms.visualize.uri.XFormsToURIStromaeV1; import fr.insee.pogues.utils.suggester.SuggesterVisuService; +import fr.insee.pogues.webservice.model.StudyUnitEnum; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -73,10 +73,11 @@ public class VisualizeWithURI { @PostMapping(path = "visualize/{dataCollection}/{questionnaire}", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Get visualization URI from JSON serialized Pogues entity", description = "dataCollection MUST refer to the name attribute owned by the nested DataCollectionObject") @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "JSON representation of the Pogues Model") - public ResponseEntity visualizeFromBody(@RequestBody String request, - @PathVariable(value = "dataCollection") String dataCollection, - @PathVariable(value = "questionnaire") String questionnaire, - @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { + public ResponseEntity visualizeFromBody( + @RequestBody String request, + @PathVariable(value = "dataCollection") String dataCollection, + @PathVariable(value = "questionnaire") String questionnaire, + @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { PipeLine pipeline = new PipeLine(); Map params = new HashMap<>(); params.put("dataCollection", dataCollection.toLowerCase()); @@ -96,9 +97,10 @@ public ResponseEntity visualizeFromBody(@RequestBody String request, @PostMapping(path = "visualize-queen-telephone/{questionnaire}", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Get visualization URI CATI Queen from JSON serialized Pogues entity", description = "Get visualization URI CATI Queen from JSON serialized Pogues entity") - public ResponseEntity visualizeCatiQueenFromBody(@RequestBody String request, - @PathVariable(value = "questionnaire") String questionnaireName, - @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { + public ResponseEntity visualizeCatiQueenFromBody( + @RequestBody String request, + @PathVariable(value = "questionnaire") String questionnaireName, + @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { PipeLine pipeline = new PipeLine(); Map params = new HashMap<>(); params.put("mode", "CATI"); @@ -115,9 +117,10 @@ public ResponseEntity visualizeCatiQueenFromBody(@RequestBody String req @PostMapping(path = "visualize-queen/{questionnaire}", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Get visualization URI CAPI Queen from JSON serialized Pogues entity", description = "Get visualization URI CAPI Queen from JSON serialized Pogues entity") - public ResponseEntity visualizeQueenFromBody(@RequestBody String request, - @PathVariable(value = "questionnaire") String questionnaireName, - @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { + public ResponseEntity visualizeQueenFromBody( + @RequestBody String request, + @PathVariable(value = "questionnaire") String questionnaireName, + @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { PipeLine pipeline = new PipeLine(); Map params = new HashMap<>(); params.put("mode", "CAPI"); @@ -134,9 +137,10 @@ public ResponseEntity visualizeQueenFromBody(@RequestBody String request @PostMapping(path = "visualize-stromae-v2/{questionnaire}", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Get visualization URI Stromae V2 from JSON serialized Pogues entity", description = "Get visualization URI Stromae V2 from JSON serialized Pogues entity") - public ResponseEntity visualizeStromaeV2FromBody(@RequestBody String request, - @PathVariable(value = "questionnaire") String questionnaireName, - @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { + public ResponseEntity visualizeStromaeV2FromBody( + @RequestBody String request, + @PathVariable(value = "questionnaire") String questionnaireName, + @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { PipeLine pipeline = new PipeLine(); Map params = new HashMap<>(); params.put("questionnaire", questionnaireName.toLowerCase()); @@ -156,9 +160,12 @@ public ResponseEntity visualizeStromaeV2FromBody(@RequestBody String req @PostMapping(path = "visualize-stromae-v3/{questionnaire}", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Get visualization URI Stromae V3 from JSON serialized Pogues entity", description = "Get visualization URI Stromae V3 from JSON serialized Pogues entity") - public ResponseEntity visualizeStromaeV3FromBody(@RequestBody String request, - @PathVariable(value = "questionnaire") String questionnaireName, - @RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception { + public ResponseEntity visualizeStromaeV3FromBody( + @RequestBody String request, + @PathVariable(value = "questionnaire") String questionnaireName, + @RequestParam(name = "references", defaultValue = "false") Boolean ref, + @RequestParam(defaultValue = "DEFAULT") StudyUnitEnum context) throws Exception { + PipeLine pipeline = new PipeLine(); Map params = new HashMap<>(); params.put("questionnaire", questionnaireName.toLowerCase()); @@ -166,6 +173,7 @@ public ResponseEntity visualizeStromaeV3FromBody(@RequestBody String req params.put("mode", "CAWI"); params.put("nomenclatureIds", suggesterVisuService.getNomenclatureIdsFromQuestionnaire(request)); params.put("dsfr", true); + params.put("context", context); URI uri; ByteArrayOutputStream outputStream = pipeline.from(string2InputStream(request)) .map(jsonToJsonDeref::transform, params, questionnaireName.toLowerCase()) @@ -178,9 +186,10 @@ public ResponseEntity visualizeStromaeV3FromBody(@RequestBody String req @PostMapping(path = "visualize-from-ddi/{dataCollection}/{questionnaire}", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Get visualization URI from DDI questionnaire", description = "dataCollection MUST refer to the name attribute owned by the nested DataCollectionObject") - public ResponseEntity visualizeFromDDIBody(@RequestBody String request, - @PathVariable(value = "dataCollection") String dataCollection, - @PathVariable(value = "questionnaire") String questionnaire) throws Exception { + public ResponseEntity visualizeFromDDIBody( + @RequestBody String request, + @PathVariable(value = "dataCollection") String dataCollection, + @PathVariable(value = "questionnaire") String questionnaire) throws Exception { PipeLine pipeline = new PipeLine(); Map params = new HashMap<>(); params.put("dataCollection", dataCollection.toLowerCase()); @@ -199,9 +208,10 @@ public ResponseEntity visualizeFromDDIBody(@RequestBody String request, @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "500", description = "Error") }) - public String xForm2URI(@RequestBody String questXforms, - @PathVariable(value = "dataCollection") String dataCollection, - @PathVariable(value = "questionnaire") String questionnaire) throws Exception { + public String xForm2URI( + @RequestBody String questXforms, + @PathVariable(value = "dataCollection") String dataCollection, + @PathVariable(value = "questionnaire") String questionnaire) throws Exception { Map params = new HashMap<>(); params.put("dataCollection", dataCollection.toLowerCase()); diff --git a/src/test/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImplTest.java b/src/test/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImplTest.java new file mode 100644 index 00000000..58083f90 --- /dev/null +++ b/src/test/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImplTest.java @@ -0,0 +1,74 @@ +package fr.insee.pogues.api.remote.eno.transforms; + +import fr.insee.pogues.webservice.model.StudyUnitEnum; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class EnoClientImplTest { + @Test + void testWSPathWhenContextIsDefault() { + + // Call the method + String wsPath = EnoClientImpl.buildWSPath(StudyUnitEnum.DEFAULT, "CAWI"); + + // Check that the default context is being used + assertEquals("questionnaire/DEFAULT/lunatic-json/CAWI", wsPath); + } + + @ParameterizedTest + @EnumSource(StudyUnitEnum.class) // Test with all values of StudyUnitEnum + void testWSPathWithDifferentContexts(StudyUnitEnum context) { + + // Call the method + String wsPath = EnoClientImpl.buildWSPath(context, "CAWI"); + + // Check that the correct context is being used + assertEquals("questionnaire/" + context + "/lunatic-json/CAWI", wsPath); + } + + @Test + void testGetContextParam_withContextKey() { + Map params = new HashMap<>(); + params.put("context", StudyUnitEnum.BUSINESS); + + StudyUnitEnum result = EnoClientImpl.getContextParam(params); + + assertEquals(StudyUnitEnum.BUSINESS, result); + } + + @Test + void testGetContextParam_withoutContextKey() { + Map params = new HashMap<>(); + + StudyUnitEnum result = EnoClientImpl.getContextParam(params); + + assertEquals(StudyUnitEnum.DEFAULT, result); + } + + @Test + void testGetContextParam_withIncorrectContextKey() { + Map params = new HashMap<>(); + params.put("notAContext","notAValue"); + + StudyUnitEnum result = EnoClientImpl.getContextParam(params); + + assertEquals(StudyUnitEnum.DEFAULT, result); + } + + @Test + void testGetContextParam_withNullKey() { + Map params = new HashMap<>(); + params.put("context", null); + + StudyUnitEnum result = EnoClientImpl.getContextParam(params); + + assertEquals(StudyUnitEnum.DEFAULT, result); + } + +}