diff --git a/pom.xml b/pom.xml index f5095e9b..2c8be3ae 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ fr.insee Pogues-BO jar - 4.9.2 + 4.9.2-SNAPSHOT.1 Pogues-Back-Office diff --git a/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClient.java b/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClient.java index 9777176a..951e4502 100644 --- a/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClient.java +++ b/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClient.java @@ -1,36 +1,32 @@ package fr.insee.pogues.api.remote.eno.transforms; import fr.insee.pogues.exception.EnoException; +import fr.insee.pogues.webservice.rest.PoguesException; import java.io.IOException; import java.net.URISyntaxException; import java.util.Map; +/** + * Client with methods to call Eno-WS external web-service. + */ public interface EnoClient { - /** - * - * Call the Eno API to convert ddi 3.2 data in ddi 3.3 - * - * @param inputAsString - * @return String - * @throws Exception - */ - String getDDI32ToDDI33 (String inputAsString) throws Exception; - - String getDDIToODT (String inputAsString) throws Exception; - - String getXMLPoguesToDDI (String inputAsString) throws Exception; + + /** Only used as a health-check for the Eno external web-service. */ + void getParameters(); + + String getPoguesXmlToDDI(String inputAsString) throws EnoException, PoguesException; + + String getDDIToODT (String inputAsString) throws EnoException, PoguesException; String getDDIToFO(String inputAsString) throws URISyntaxException, IOException, EnoException; - - String getDDITOLunaticXML(String inputAsString) throws URISyntaxException, IOException, EnoException; - - String getDDITOLunaticJSON(String inputAsString, Map params) throws URISyntaxException, IOException, EnoException; - - String getDDITOXForms(String inputAsString) throws URISyntaxException, IOException, EnoException; - - String getJSONPoguesToLunaticJson(String inputAsString, Map params) throws URISyntaxException, IOException, EnoException; - - void getParameters () throws Exception; + + String getDDIToXForms(String inputAsString) throws URISyntaxException, IOException, EnoException; + + /** @deprecated Use Pogues to Lunatic method instead. */ + @Deprecated(since = "4.9.2") + String getDDIToLunaticJSON(String inputAsString, Map params) throws URISyntaxException, IOException, EnoException; + + String getPoguesJsonToLunaticJson(String inputAsString, Map params) throws URISyntaxException, IOException, EnoException; } 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 deleted file mode 100644 index 10e13f9c..00000000 --- a/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImpl.java +++ /dev/null @@ -1,180 +0,0 @@ -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; -import org.springframework.http.MediaType; -import org.springframework.http.client.MultipartBodyBuilder; -import org.springframework.stereotype.Service; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.reactive.function.BodyInserters; -import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.reactive.function.client.WebClientResponseException; -import org.springframework.web.util.UriComponentsBuilder; - -import java.io.IOException; -import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.util.Map; - -@Service -@Slf4j -public class EnoClientImpl implements EnoClient{ - - @Value("${application.eno.host}") - String enoHost; - - @Autowired - 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) { - this.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 { - return callEnoApi(inputAsString, BASE_PATH+"/fo"); - } - - @Override - public String getDDITOLunaticXML(String inputAsString) throws EnoException, PoguesException { - return callEnoApi(inputAsString, BASE_PATH+"/lunatic-xml"); - } - - @Override - public String getDDITOLunaticJSON(String inputAsString, Map params) throws EnoException, PoguesException { - MultiValueMap queryParams = new LinkedMultiValueMap<>(); - String modePathParam = params.get("mode") != null ? params.get("mode").toString() : MODE; - - 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); - } - - /** - * 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 - public String getDDITOXForms(String inputAsString) throws EnoException, PoguesException { - return callEnoApi(inputAsString, BASE_PATH+"/xforms"); - } - - @Override - 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", - DEFAULT_CONTEXT, - modePathParam); - log.info("WSPath : {} ",wsPath); - queryParams.add(DSFR_QUERY_PARAM, Boolean.TRUE.equals(params.get("dsfr")) ? "true" : "false"); - return callEnoApiWithParams(inputAsString, wsPath, queryParams); - } - - @Override - public String getDDIToODT (String inputAsString) throws EnoException, PoguesException { - return callEnoApi(inputAsString, BASE_PATH+"/fodt"); - } - - @Override - public void getParameters () { - URI uri = UriComponentsBuilder - .fromHttpUrl(enoHost) - .path("/parameters/xml/all") - .build().toUri(); - - String xmlParams = webClient.get() - .uri(uri) - .accept(MediaType.ALL) - .retrieve().bodyToMono(String.class).block(); - - log.debug(xmlParams); - } - - private String callEnoApi(String inputAsString, String wsPath) throws EnoException, PoguesException { - MultiValueMap queryParams = new LinkedMultiValueMap<>(); - return callEnoApiWithParams(inputAsString, wsPath, queryParams); - } - - private String callEnoApiWithParams(String inputAsString, String wsPath, MultiValueMap params) throws EnoException, PoguesException { - URI uri = UriComponentsBuilder - .fromHttpUrl(enoHost) - .path(wsPath) - .queryParams(params) - .build().toUri(); - - log.info("Call Eno with URI : {}", uri); - - MultipartBodyBuilder builder = new MultipartBodyBuilder(); - builder.part("in", - new ByteArrayResourceWithFileName( - "form.xml", - inputAsString.getBytes(StandardCharsets.UTF_8)) - ); - - try { - return webClient.post() - .uri(uri) - .accept(MediaType.APPLICATION_OCTET_STREAM) - .contentType(MediaType.MULTIPART_FORM_DATA) - .body(BodyInserters.fromMultipartData(builder.build())) - .retrieve() - .bodyToMono(String.class) - .block(); - } catch (WebClientResponseException e) { - log.error(e.getMessage()); - throw new EnoException(e.getResponseBodyAsString(), null); - } catch (Exception e){ - throw new PoguesException(500, "Unknow error during generation", ""); - } - } - -} - diff --git a/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoHttpClient.java b/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoHttpClient.java new file mode 100644 index 00000000..8a7eaf6b --- /dev/null +++ b/src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoHttpClient.java @@ -0,0 +1,168 @@ +package fr.insee.pogues.api.remote.eno.transforms; + +import fr.insee.pogues.exception.EnoException; +import fr.insee.pogues.webservice.model.EnoContext; +import fr.insee.pogues.webservice.rest.PoguesException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; +import org.springframework.http.client.MultipartBodyBuilder; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClientResponseException; +import org.springframework.web.util.UriComponentsBuilder; + +import java.io.IOException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +/** + * Implementation of EnoClient using http. + */ +@Service +@RequiredArgsConstructor +@Slf4j +public class EnoHttpClient implements EnoClient { + + @Value("${application.eno.host}") + String enoHost; + + private final WebClient webClient; + + private static final String DEFAULT_CONTEXT = "DEFAULT"; + private static final String DEFAULT_CONTEXT_PATH = "/questionnaire/" + DEFAULT_CONTEXT; + private static final String DDI_FILE_NAME = "ddi.xml"; + private static final String POGUES_XML_FILE_NAME = "pogues.xml"; + private static final String POGUES_JSON_FILE_NAME = "pogues.json"; + private static final String DSFR_QUERY_PARAM = "dsfr"; + + + /** {@link EnoClient#getParameters()} */ + @Override + public void getParameters() { + URI uri = UriComponentsBuilder + .fromHttpUrl(enoHost) + .path("/parameters/xml/all") + .build().toUri(); + String xmlParams = webClient.get() + .uri(uri) + .accept(MediaType.ALL) + .retrieve() + .bodyToMono(String.class).block(); + log.debug("Xml parameters received from the Eno external API:{}{}", System.lineSeparator(), xmlParams); + } + + @Override + public String getPoguesXmlToDDI(String inputAsString) throws EnoException, PoguesException { + return callEnoApi(inputAsString, POGUES_XML_FILE_NAME, "/questionnaire/poguesxml-2-ddi"); + } + + @Override + public String getDDIToODT (String inputAsString) throws EnoException, PoguesException { + return callEnoApi(inputAsString, DDI_FILE_NAME, DEFAULT_CONTEXT_PATH +"/fodt"); + } + + @Override + public String getDDIToFO(String inputAsString) throws EnoException, PoguesException { + return callEnoApi(inputAsString, DDI_FILE_NAME, DEFAULT_CONTEXT_PATH +"/fo"); + } + + @Override + public String getDDIToXForms(String inputAsString) throws EnoException, PoguesException { + return callEnoApi(inputAsString, DDI_FILE_NAME, DEFAULT_CONTEXT_PATH +"/xforms"); + } + + /** + * @deprecated {@link EnoClient#getDDIToLunaticJSON(String, Map)} + */ + @Override + @Deprecated(since = "4.9.2") + public String getDDIToLunaticJSON(String inputAsString, Map params) throws EnoException, PoguesException { + log.info("getDDITOLunaticJSON - started"); + + EnoContext context = getContextParam(params); + String mode = getModeParam(params); + String wsPath = "questionnaire/" + context + "/lunatic-json/" + mode; + + MultiValueMap queryParams = new LinkedMultiValueMap<>(); + queryParams.add(DSFR_QUERY_PARAM, getDsfrParam(params)); + + return callEnoApiWithParams(inputAsString, DDI_FILE_NAME, wsPath, queryParams); + } + + @Override + public String getPoguesJsonToLunaticJson(String inputAsString, Map params) throws IOException, EnoException { + log.info("getJSONPoguesToLunaticJson - started"); + + EnoContext context = getContextParam(params); + String mode = getModeParam(params); + String wsPath = "/questionnaire/pogues-2-lunatic/" + context + "/" + mode; + + MultiValueMap queryParams = new LinkedMultiValueMap<>(); + queryParams.add(DSFR_QUERY_PARAM, getDsfrParam(params)); + + return callEnoApiWithParams(inputAsString, POGUES_JSON_FILE_NAME, wsPath, queryParams); + } + + /** Returns the Eno context from the params map. Default value is the 'DEFAULT' context. */ + static EnoContext getContextParam(Map params) { + EnoContext enoContext = (EnoContext) params.get("context"); + if (enoContext == null) + log.warn("null context sent in a Eno request."); + return (enoContext != null) ? enoContext : EnoContext.DEFAULT; + } + + static String getModeParam(Map params) { + Object modePathParam = params.get("mode"); + if (modePathParam == null) + throw new IllegalStateException("No 'mode' defined in params."); + return modePathParam.toString(); + } + + static String getDsfrParam(Map params) { + return Boolean.TRUE.equals(params.get("dsfr")) ? "true" : "false"; + } + + private String callEnoApi(String inputAsString, String fileName, String wsPath) throws EnoException, PoguesException { + MultiValueMap emptyParams = new LinkedMultiValueMap<>(); + return callEnoApiWithParams(inputAsString, fileName, wsPath, emptyParams); + } + + private String callEnoApiWithParams(String inputAsString, String fileName, String wsPath, MultiValueMap params) + throws EnoException, PoguesException { + URI uri = UriComponentsBuilder + .fromHttpUrl(enoHost) + .path(wsPath) + .queryParams(params) + .build().toUri(); + + log.info("Call Eno API with URI: {}", uri); + + MultipartBodyBuilder builder = new MultipartBodyBuilder(); + builder.part("in", new ByteArrayResourceWithFileName( + fileName, inputAsString.getBytes(StandardCharsets.UTF_8))); + + try { + return webClient.post() + .uri(uri) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .contentType(MediaType.MULTIPART_FORM_DATA) + .body(BodyInserters.fromMultipartData(builder.build())) + .retrieve() + .bodyToMono(String.class) + .block(); + } catch (WebClientResponseException e) { + log.error(e.getMessage()); + throw new EnoException(e.getResponseBodyAsString(), null); + } catch (Exception e) { + log.error(e.getMessage()); + throw new PoguesException(500, "Unknown error during generation", ""); + } + } + +} diff --git a/src/main/java/fr/insee/pogues/transforms/visualize/eno/DDIToLunaticJSONImpl.java b/src/main/java/fr/insee/pogues/transforms/visualize/eno/DDIToLunaticJSONImpl.java index 9453d94f..917abcf5 100644 --- a/src/main/java/fr/insee/pogues/transforms/visualize/eno/DDIToLunaticJSONImpl.java +++ b/src/main/java/fr/insee/pogues/transforms/visualize/eno/DDIToLunaticJSONImpl.java @@ -25,7 +25,7 @@ public ByteArrayOutputStream transform(InputStream inputStream, Map params) throws Exception { - return enoClient.getDDITOLunaticJSON(inputAsString, params); + return enoClient.getDDIToLunaticJSON(inputAsString, params); } } diff --git a/src/main/java/fr/insee/pogues/transforms/visualize/eno/DDIToXFormsImpl.java b/src/main/java/fr/insee/pogues/transforms/visualize/eno/DDIToXFormsImpl.java index f3a48425..884b2c39 100644 --- a/src/main/java/fr/insee/pogues/transforms/visualize/eno/DDIToXFormsImpl.java +++ b/src/main/java/fr/insee/pogues/transforms/visualize/eno/DDIToXFormsImpl.java @@ -25,6 +25,6 @@ public ByteArrayOutputStream transform(InputStream inputStream, Map params) throws Exception { - return enoClient.getJSONPoguesToLunaticJson(inputAsString, params); + return enoClient.getPoguesJsonToLunaticJson(inputAsString, params); } } diff --git a/src/main/java/fr/insee/pogues/transforms/visualize/eno/PoguesXMLToDDIImpl.java b/src/main/java/fr/insee/pogues/transforms/visualize/eno/PoguesXMLToDDIImpl.java index d0ac18b2..45db8219 100644 --- a/src/main/java/fr/insee/pogues/transforms/visualize/eno/PoguesXMLToDDIImpl.java +++ b/src/main/java/fr/insee/pogues/transforms/visualize/eno/PoguesXMLToDDIImpl.java @@ -30,6 +30,6 @@ public ByteArrayOutputStream transform(InputStream inputStream, MapEno's context of generation. + * A context determines a specific set of parameters in Eno.

+ *

Note: this notion of "business/household" contexts is an Insee concept that is currently hard-coded in many + * services of the survey channel. In Pogues/Eno, this concept is used to trigger specific set of parameters. + * This should be generalised in a way that does not adhere to Insee internal concepts.

+ * */ +@Getter +public enum EnoContext { + + /** + * @deprecated The default context should not be used anymore. If a generation doesn't need context, + * simply don't provide a context in the Eno request. */ + @Deprecated + DEFAULT("default"), + + /** Eno "business" generation context. */ + BUSINESS("business"), + + /** Eno "household" generation context. */ + HOUSEHOLD("household"); + + /** Requests use a lower caps value. */ + private final String value; + + EnoContext(String value) { + this.value = value; + } +} diff --git a/src/main/java/fr/insee/pogues/webservice/model/StudyUnitEnum.java b/src/main/java/fr/insee/pogues/webservice/model/StudyUnitEnum.java index 072bd27d..c03a1966 100644 --- a/src/main/java/fr/insee/pogues/webservice/model/StudyUnitEnum.java +++ b/src/main/java/fr/insee/pogues/webservice/model/StudyUnitEnum.java @@ -2,14 +2,21 @@ import lombok.Getter; +/** + * Old vocabulary for the "context" concept. + * @see EnoContext + * @deprecated Use EnoContext instead. + */ +@Deprecated(since = "4.9.2") +@Getter public enum StudyUnitEnum { DEFAULT("default"),BUSINESS("business"),HOUSEHOLD("household"); @Getter - private String studyUnit; + private final String studyUnit; - private StudyUnitEnum(String studyUnit) { + StudyUnitEnum(String studyUnit) { this.studyUnit = studyUnit; } diff --git a/src/main/java/fr/insee/pogues/webservice/rest/RestExceptionHandler.java b/src/main/java/fr/insee/pogues/webservice/rest/RestExceptionHandler.java index 9d4c31ed..68477916 100644 --- a/src/main/java/fr/insee/pogues/webservice/rest/RestExceptionHandler.java +++ b/src/main/java/fr/insee/pogues/webservice/rest/RestExceptionHandler.java @@ -13,9 +13,9 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(value = { PoguesException.class }) - public ResponseEntity handlePoguesException(PoguesException pe) { - log.error(pe.getMessage(), pe); - RestMessage message = pe.toRestMessage(); + public ResponseEntity handlePoguesException(PoguesException poguesException) { + log.error(poguesException.getMessage(), poguesException); + RestMessage message = poguesException.toRestMessage(); return new ResponseEntity<>(message, HttpStatusCode.valueOf(message.getStatus())); } @ExceptionHandler(value = { Exception.class }) 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 313e4a90..c0e06a5d 100644 --- a/src/main/java/fr/insee/pogues/webservice/rest/VisualizeWithURI.java +++ b/src/main/java/fr/insee/pogues/webservice/rest/VisualizeWithURI.java @@ -3,7 +3,6 @@ import fr.insee.pogues.transforms.PipeLine; import fr.insee.pogues.transforms.visualize.PoguesJSONToPoguesJSONDeref; import fr.insee.pogues.transforms.visualize.PoguesJSONToPoguesXML; -import fr.insee.pogues.transforms.visualize.eno.DDIToLunaticJSON; import fr.insee.pogues.transforms.visualize.eno.DDIToXForms; import fr.insee.pogues.transforms.visualize.eno.PoguesJSONToLunaticJSON; import fr.insee.pogues.transforms.visualize.eno.PoguesXMLToDDI; @@ -12,13 +11,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 fr.insee.pogues.webservice.model.EnoContext; 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.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -37,37 +36,19 @@ @RestController @RequestMapping("/api/transform") @Tag(name = "6. Visualization with URI") +@AllArgsConstructor @Slf4j public class VisualizeWithURI { - @Autowired PoguesJSONToPoguesXML jsonToXML; - - @Autowired PoguesXMLToDDI poguesXMLToDDI; - - @Autowired DDIToXForms ddiToXForm; - - @Autowired XFormsToURIStromaeV1 xformToUri; - - @Autowired PoguesJSONToLunaticJSON poguesJSONToLunaticJSON; - - @Autowired LunaticJSONToUriQueen lunaticJSONToUriQueen; - - @Autowired LunaticJSONToUriStromaeV2 lunaticJSONToUriStromaeV2; - - @Autowired LunaticJSONToUriStromaeV3 lunaticJSONToUriStromaeV3; - - @Autowired PoguesJSONToPoguesJSONDeref jsonToJsonDeref; - - @Autowired SuggesterVisuService suggesterVisuService; @PostMapping(path = "visualize/{dataCollection}/{questionnaire}", consumes = MediaType.APPLICATION_JSON_VALUE) @@ -164,7 +145,7 @@ 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 { + @RequestParam(defaultValue = "DEFAULT") EnoContext context) throws Exception { PipeLine pipeline = new PipeLine(); Map params = new HashMap<>(); 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 deleted file mode 100644 index 58083f90..00000000 --- a/src/test/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImplTest.java +++ /dev/null @@ -1,74 +0,0 @@ -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); - } - -} diff --git a/src/test/java/fr/insee/pogues/api/remote/eno/transforms/EnoHttpClientTest.java b/src/test/java/fr/insee/pogues/api/remote/eno/transforms/EnoHttpClientTest.java new file mode 100644 index 00000000..d383d159 --- /dev/null +++ b/src/test/java/fr/insee/pogues/api/remote/eno/transforms/EnoHttpClientTest.java @@ -0,0 +1,74 @@ +package fr.insee.pogues.api.remote.eno.transforms; + +import fr.insee.pogues.webservice.model.EnoContext; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class EnoHttpClientTest { + + @Nested + class ContextParamTests { + @Test + void getContextParam_withContextKey() { + Map params = Map.of("context", EnoContext.BUSINESS); + EnoContext result = EnoHttpClient.getContextParam(params); + assertEquals(EnoContext.BUSINESS, result); + } + @Test + void getContextParam_withoutContextKey() { + Map params = new HashMap<>(); + EnoContext result = EnoHttpClient.getContextParam(params); + assertEquals(EnoContext.DEFAULT, result); + } + @Test + void getContextParam_withIncorrectContextKey() { + Map params = Map.of("notAContext", "notAValue"); + EnoContext result = EnoHttpClient.getContextParam(params); + assertEquals(EnoContext.DEFAULT, result); + } + } + + @Nested + class ModeParamTests { + @Test + void getModeParam_withModeKey() { + Map params = Map.of("mode", "CAPI"); + String mode = EnoHttpClient.getModeParam(params); + assertEquals("CAPI", mode); + } + @Test + void getModeParam_withoutModeKey() { + Map params = new HashMap<>(); + assertThrows(IllegalStateException.class, () -> EnoHttpClient.getModeParam(params)); + } + } + + @Nested + class DsfrParamTests { + @Test + void getDsfrParam_true() { + Map params = Map.of("dsfr", true); + String value = EnoHttpClient.getDsfrParam(params); + assertEquals("true", value); + } + @Test + void getDsfrParam_false() { + Map params = Map.of("dsfr", false); + String value = EnoHttpClient.getDsfrParam(params); + assertEquals("false", value); + } + @Test + void getDsfrParam_defaultValue() { + Map params = new HashMap<>(); + String value = EnoHttpClient.getDsfrParam(params); + assertEquals("false", value); + } + } + +} diff --git a/src/test/resources/simple-questionnaire.json b/src/test/resources/simple-questionnaire.json new file mode 100644 index 00000000..98e09c23 --- /dev/null +++ b/src/test/resources/simple-questionnaire.json @@ -0,0 +1,136 @@ +{ + "owner": "ENO-INTEGRATION-TESTS", + "final": false, + "id": "lmyoceix", + "Label": [ + "Eno - Simple questionnaire" + ], + "Name": "ENOSIMPLE", + "lastUpdatedDate": "Tue Nov 19 2024 14:26:13 GMT+0100 (heure normale d’Europe centrale)", + "DataCollection": [ + { + "id": "TCM", + "uri": "http://ddi:fr.insee:DataCollection.TCM" + } + ], + "genericName": "QUESTIONNAIRE", + "ComponentGroup": [ + { + "id": "lmynyac5", + "Name": "PAGE_1", + "Label": [ + "Components for page 1" + ], + "MemberReference": [ + "lmynuv39", + "lmyo3e0y", + "idendquest" + ] + } + ], + "agency": "fr.insee", + "TargetMode": [ + "CAPI", + "CATI", + "PAPI", + "CAWI" + ], + "flowLogic": "FILTER", + "formulasLanguage": "VTL", + "childQuestionnaireRef": [], + "Child": [ + { + "id": "lmynuv39", + "depth": 1, + "Name": "S1", + "Label": [ + "\"Unique sequence\"" + ], + "Declaration": [], + "Control": [], + "FlowControl": [], + "TargetMode": [ + "CAPI", + "CATI", + "PAPI", + "CAWI" + ], + "type": "SequenceType", + "genericName": "MODULE", + "Child": [ + { + "id": "lmyo3e0y", + "depth": 2, + "Name": "Q1", + "Label": [ + "\"Unique question\"" + ], + "Declaration": [], + "Control": [], + "FlowControl": [], + "TargetMode": [ + "CAPI", + "CATI", + "PAPI", + "CAWI" + ], + "type": "QuestionType", + "questionType": "SIMPLE", + "Response": [ + { + "id": "lmynvhl8", + "Datatype": { + "typeName": "TEXT", + "type": "TextDatatypeType", + "MaxLength": 249, + "Pattern": "" + }, + "CollectedVariableReference": "lmyo22nw", + "mandatory": false + } + ] + } + ] + }, + { + "id": "idendquest", + "depth": 1, + "Name": "QUESTIONNAIRE_END", + "Label": [ + "QUESTIONNAIRE_END" + ], + "Declaration": [], + "genericName": "MODULE", + "Control": [], + "FlowControl": [], + "type": "SequenceType", + "Child": [], + "TargetMode": [ + "CAPI", + "CATI", + "PAPI", + "CAWI" + ] + } + ], + "CodeLists": { + "CodeList": [] + }, + "Variables": { + "Variable": [ + { + "id": "lmyo22nw", + "Name": "Q1", + "Label": "Q1 label", + "type": "CollectedVariableType", + "Datatype": { + "typeName": "TEXT", + "type": "TextDatatypeType", + "MaxLength": 249, + "Pattern": "" + } + } + ] + }, + "FlowControl": [] +} \ No newline at end of file