Skip to content

Commit

Permalink
Feat/visualize household business (#321)
Browse files Browse the repository at this point in the history
* Issue #317 visualize links : separate web business/household

* style: readability in visualize methods

* feat : visualize-household-business

* fix: resolve conflict in EnoClientImpl.java

* fix: resolve conflict #2 in EnoClientImpl.java

* fix: resolve conflict #3 in EnoClientImpl.java

* fix: resolve conflict #4 in EnoClientImpl.java

* fix: resolve conflict #5 in EnoClientImpl.java

* chore: bump version

---------

Co-authored-by: Nicolas Senave <[email protected]>
  • Loading branch information
RemiVerriez and nsenave authored Oct 30, 2024
1 parent a9b9d66 commit c699c6b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<groupId>fr.insee</groupId>
<artifactId>Pogues-BO</artifactId>
<packaging>jar</packaging>
<version>4.8.2-SNAPSHOT</version>
<version>4.9.0-SNAPSHOT</version>
<name>Pogues-Back-Office</name>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -66,11 +63,41 @@ public String getDDITOLunaticXML(String inputAsString) throws EnoException, Pogu

@Override
public String getDDITOLunaticJSON(String inputAsString, Map<String, Object> params) throws EnoException, PoguesException {
MultiValueMap<String,String> queryParams = new LinkedMultiValueMap<>();
MultiValueMap<String, String> 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<String, Object> 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
Expand All @@ -79,26 +106,25 @@ public String getDDITOXForms(String inputAsString) throws EnoException, PoguesEx
}

@Override
public String getJSONPoguesToLunaticJson(String inputAsString, Map<String, Object> params) throws URISyntaxException, IOException, EnoException {
public String getJSONPoguesToLunaticJson(String inputAsString, Map<String, Object> params) throws IOException, EnoException {
log.info("getJSONPoguesToLunaticJson - started");
MultiValueMap<String,String> 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")
Expand 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<String,String> queryParams = new LinkedMultiValueMap<>();
return callEnoApiWithParams(inputAsString, WSPath, queryParams);
return callEnoApiWithParams(inputAsString, wsPath, queryParams);
}

private String callEnoApiWithParams(String inputAsString, String WSPath, MultiValueMap<String,String> params) throws EnoException, PoguesException {
private String callEnoApiWithParams(String inputAsString, String wsPath, MultiValueMap<String,String> params) throws EnoException, PoguesException {
URI uri = UriComponentsBuilder
.fromHttpUrl(enoHost)
.path(WSPath)
.path(wsPath)
.queryParams(params)
.build().toUri();

Expand Down
56 changes: 33 additions & 23 deletions src/main/java/fr/insee/pogues/webservice/rest/VisualizeWithURI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> 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<String, Object> params = new HashMap<>();
params.put("dataCollection", dataCollection.toLowerCase());
Expand All @@ -96,9 +97,10 @@ public ResponseEntity<String> 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<String> visualizeCatiQueenFromBody(@RequestBody String request,
@PathVariable(value = "questionnaire") String questionnaireName,
@RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception {
public ResponseEntity<String> visualizeCatiQueenFromBody(
@RequestBody String request,
@PathVariable(value = "questionnaire") String questionnaireName,
@RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception {
PipeLine pipeline = new PipeLine();
Map<String, Object> params = new HashMap<>();
params.put("mode", "CATI");
Expand All @@ -115,9 +117,10 @@ public ResponseEntity<String> 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<String> visualizeQueenFromBody(@RequestBody String request,
@PathVariable(value = "questionnaire") String questionnaireName,
@RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception {
public ResponseEntity<String> visualizeQueenFromBody(
@RequestBody String request,
@PathVariable(value = "questionnaire") String questionnaireName,
@RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception {
PipeLine pipeline = new PipeLine();
Map<String, Object> params = new HashMap<>();
params.put("mode", "CAPI");
Expand All @@ -134,9 +137,10 @@ public ResponseEntity<String> 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<String> visualizeStromaeV2FromBody(@RequestBody String request,
@PathVariable(value = "questionnaire") String questionnaireName,
@RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception {
public ResponseEntity<String> visualizeStromaeV2FromBody(
@RequestBody String request,
@PathVariable(value = "questionnaire") String questionnaireName,
@RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception {
PipeLine pipeline = new PipeLine();
Map<String, Object> params = new HashMap<>();
params.put("questionnaire", questionnaireName.toLowerCase());
Expand All @@ -156,16 +160,20 @@ public ResponseEntity<String> 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<String> visualizeStromaeV3FromBody(@RequestBody String request,
@PathVariable(value = "questionnaire") String questionnaireName,
@RequestParam(name = "references", defaultValue = "false") Boolean ref) throws Exception {
public ResponseEntity<String> 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<String, Object> params = new HashMap<>();
params.put("questionnaire", questionnaireName.toLowerCase());
params.put("needDeref", ref);
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())
Expand All @@ -178,9 +186,10 @@ public ResponseEntity<String> 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<String> visualizeFromDDIBody(@RequestBody String request,
@PathVariable(value = "dataCollection") String dataCollection,
@PathVariable(value = "questionnaire") String questionnaire) throws Exception {
public ResponseEntity<String> visualizeFromDDIBody(
@RequestBody String request,
@PathVariable(value = "dataCollection") String dataCollection,
@PathVariable(value = "questionnaire") String questionnaire) throws Exception {
PipeLine pipeline = new PipeLine();
Map<String, Object> params = new HashMap<>();
params.put("dataCollection", dataCollection.toLowerCase());
Expand All @@ -199,9 +208,10 @@ public ResponseEntity<String> 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<String, Object> params = new HashMap<>();
params.put("dataCollection", dataCollection.toLowerCase());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> params = new HashMap<>();
params.put("context", StudyUnitEnum.BUSINESS);

StudyUnitEnum result = EnoClientImpl.getContextParam(params);

assertEquals(StudyUnitEnum.BUSINESS, result);
}

@Test
void testGetContextParam_withoutContextKey() {
Map<String, Object> params = new HashMap<>();

StudyUnitEnum result = EnoClientImpl.getContextParam(params);

assertEquals(StudyUnitEnum.DEFAULT, result);
}

@Test
void testGetContextParam_withIncorrectContextKey() {
Map<String, Object> params = new HashMap<>();
params.put("notAContext","notAValue");

StudyUnitEnum result = EnoClientImpl.getContextParam(params);

assertEquals(StudyUnitEnum.DEFAULT, result);
}

@Test
void testGetContextParam_withNullKey() {
Map<String, Object> params = new HashMap<>();
params.put("context", null);

StudyUnitEnum result = EnoClientImpl.getContextParam(params);

assertEquals(StudyUnitEnum.DEFAULT, result);
}

}

0 comments on commit c699c6b

Please sign in to comment.