From 36e750d8528897808a27187afc9096a5c699ba9b Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Thu, 29 Feb 2024 15:46:45 +0100 Subject: [PATCH 1/2] Remove json-simple and replace by Jackson --- pom.xml | 23 +++----- .../controller/rest/ResponseController.java | 19 ++++--- .../domain/dtos/CollectedVariableDto.java | 13 ++--- .../domain/dtos/SurveyUnitUpdateDto.java | 40 +++----------- .../genesis/domain/dtos/VariableDto.java | 12 ++--- .../infrastructure/utils/FileUtils.java | 36 ++----------- .../infrastructure/utils/JSONUtils.java | 26 --------- .../rest/ResponseControllerTest.java | 10 ++-- .../domain/dtos/CollectedVariableDtoTest.java | 15 ++++-- .../domain/dtos/SurveyUnitUpdateDtoTest.java | 15 ++++-- .../genesis/domain/dtos/VariableDtoTest.java | 14 +++-- .../infrastructure/utils/FileUtilsTest.java | 42 --------------- .../infrastructure/utils/JSONUtilsTest.java | 53 ------------------- 13 files changed, 66 insertions(+), 252 deletions(-) delete mode 100644 src/main/java/fr/insee/genesis/infrastructure/utils/JSONUtils.java delete mode 100644 src/test/java/fr/insee/genesis/infrastructure/utils/JSONUtilsTest.java diff --git a/pom.xml b/pom.xml index 0f819c58..bf43b6d2 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,6 @@ 2.3.0 1.5.5.Final 7.15.0 - 1.4.0.RC3 0.8.11 jacoco @@ -29,6 +28,7 @@ 1.15.8 1.2.1 + 2.16.1 @@ -85,11 +85,6 @@ springdoc-openapi-starter-webmvc-ui ${springdoc.version} - - org.postgresql - postgresql - runtime - @@ -99,12 +94,14 @@ 12.4 + - com.googlecode.json-simple - json-simple - 1.1.1 + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + org.mapstruct mapstruct @@ -131,14 +128,6 @@ test - - - - com.intuit.karate - karate-junit4 - ${karate.version} - test - org.junit.vintage diff --git a/src/main/java/fr/insee/genesis/controller/rest/ResponseController.java b/src/main/java/fr/insee/genesis/controller/rest/ResponseController.java index f7fdeeb4..6f13a08d 100644 --- a/src/main/java/fr/insee/genesis/controller/rest/ResponseController.java +++ b/src/main/java/fr/insee/genesis/controller/rest/ResponseController.java @@ -17,18 +17,17 @@ import fr.insee.genesis.exceptions.GenesisException; import fr.insee.genesis.exceptions.NoDataError; import fr.insee.genesis.infrastructure.utils.FileUtils; -import fr.insee.genesis.infrastructure.utils.JSONUtils; import io.swagger.v3.oas.annotations.Operation; import lombok.extern.slf4j.Slf4j; -import org.json.simple.JSONArray; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import java.io.*; -import java.nio.file.Files; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDateTime; @@ -269,9 +268,9 @@ public ResponseEntity getLatestByUEOneObject ( @Requ outputExternalVariables.addAll(response.getExternalVariables()); }); return new ResponseEntity<>(SurveyUnitUpdateSimplified.builder() - .idQuest(responses.get(0).getIdQuest()) - .idCampaign(responses.get(0).getIdCampaign()) - .idUE(responses.get(0).getIdUE()) + .idQuest(responses.getFirst().getIdQuest()) + .idCampaign(responses.getFirst().getIdCampaign()) + .idUE(responses.getFirst().getIdUE()) .variablesUpdate(outputVariables) .externalVariables(outputExternalVariables) .build(), HttpStatus.OK); @@ -297,9 +296,9 @@ public ResponseEntity> getLatestForUEList( @Req }); if (!outputVariables.isEmpty() || !outputExternalVariables.isEmpty()) { results.add(SurveyUnitUpdateSimplified.builder() - .idQuest(responses.get(0).getIdQuest()) - .idCampaign(responses.get(0).getIdCampaign()) - .idUE(responses.get(0).getIdUE()) + .idQuest(responses.getFirst().getIdQuest()) + .idCampaign(responses.getFirst().getIdCampaign()) + .idUE(responses.getFirst().getIdUE()) .mode(mode) .variablesUpdate(outputVariables) .externalVariables(outputExternalVariables) diff --git a/src/main/java/fr/insee/genesis/domain/dtos/CollectedVariableDto.java b/src/main/java/fr/insee/genesis/domain/dtos/CollectedVariableDto.java index 4585cc05..8ab307e4 100644 --- a/src/main/java/fr/insee/genesis/domain/dtos/CollectedVariableDto.java +++ b/src/main/java/fr/insee/genesis/domain/dtos/CollectedVariableDto.java @@ -1,7 +1,8 @@ package fr.insee.genesis.domain.dtos; -import lombok.*; -import org.json.simple.JSONObject; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; import java.util.List; @@ -20,12 +21,4 @@ public CollectedVariableDto(String idVar, List values, String idLoop, St this.idParent = idParent; } - public JSONObject toJSONObject(){ - JSONObject jsonObject = super.toJSONObject(); - jsonObject.put("idLoop", idLoop); - jsonObject.put("idParent", idParent); - - return jsonObject; - } - } diff --git a/src/main/java/fr/insee/genesis/domain/dtos/SurveyUnitUpdateDto.java b/src/main/java/fr/insee/genesis/domain/dtos/SurveyUnitUpdateDto.java index 33af9e60..e7b9f194 100644 --- a/src/main/java/fr/insee/genesis/domain/dtos/SurveyUnitUpdateDto.java +++ b/src/main/java/fr/insee/genesis/domain/dtos/SurveyUnitUpdateDto.java @@ -1,12 +1,11 @@ package fr.insee.genesis.domain.dtos; -import java.time.LocalDateTime; -import java.util.List; - +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Builder; import lombok.Data; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; + +import java.time.LocalDateTime; +import java.util.List; @Builder @Data @@ -17,38 +16,11 @@ public class SurveyUnitUpdateDto { private String idUE; private DataState state; private Mode mode; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'hh:mm") private LocalDateTime recordDate; + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'hh:mm") private LocalDateTime fileDate; private List collectedVariables; private List externalVariables; - public JSONObject toJSONObject(){ - JSONObject jsonObject = new JSONObject(); - jsonObject.put("idQuest",idQuest); - jsonObject.put("idCampaign",idCampaign); - jsonObject.put("idUE",idUE); - jsonObject.put("state",state.toString()); - jsonObject.put("mode",mode.getModeName()); - jsonObject.put("recordDate",recordDate.toString()); - jsonObject.put("fileDate",fileDate.toString()); - - JSONArray collectedVariablesJSONArray = new JSONArray(); - if(collectedVariables != null){ - for(CollectedVariableDto collectedVariableDto : collectedVariables){ - collectedVariablesJSONArray.add(collectedVariableDto.toJSONObject()); - } - } - - JSONArray externalVariablesJSONArray = new JSONArray(); - if(externalVariables != null) { - for (VariableDto variableDto : externalVariables){ - externalVariablesJSONArray.add(variableDto.toJSONObject()); - } - } - - jsonObject.put("collectedVariables",collectedVariablesJSONArray); - jsonObject.put("externalVariables",externalVariablesJSONArray); - - return jsonObject; - } } diff --git a/src/main/java/fr/insee/genesis/domain/dtos/VariableDto.java b/src/main/java/fr/insee/genesis/domain/dtos/VariableDto.java index d0fb559c..d60d9040 100644 --- a/src/main/java/fr/insee/genesis/domain/dtos/VariableDto.java +++ b/src/main/java/fr/insee/genesis/domain/dtos/VariableDto.java @@ -1,7 +1,8 @@ package fr.insee.genesis.domain.dtos; -import lombok.*; -import org.json.simple.JSONObject; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; import java.util.List; @@ -13,11 +14,4 @@ public class VariableDto { private String idVar; private List values; - public JSONObject toJSONObject(){ - JSONObject jsonObject = new JSONObject(); - jsonObject.put("idVar", idVar); - jsonObject.put("values",values); - - return jsonObject; - } } diff --git a/src/main/java/fr/insee/genesis/infrastructure/utils/FileUtils.java b/src/main/java/fr/insee/genesis/infrastructure/utils/FileUtils.java index 7040b3cf..f52b0a86 100644 --- a/src/main/java/fr/insee/genesis/infrastructure/utils/FileUtils.java +++ b/src/main/java/fr/insee/genesis/infrastructure/utils/FileUtils.java @@ -1,14 +1,13 @@ package fr.insee.genesis.infrastructure.utils; +import com.fasterxml.jackson.databind.ObjectMapper; import fr.insee.genesis.configuration.Config; import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto; import lombok.Getter; import lombok.extern.slf4j.Slf4j; -import org.json.simple.JSONArray; import org.springframework.stereotype.Component; import java.io.*; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -182,35 +181,6 @@ public void writeFile(Path filePath, String fileContent) { } } - /** - * Appends a JSON object array into file. - * Creates the files if it doesn't exist - * @param filePath Path to the file. - * @param jsonArray JSON objects to write - */ - public void appendJSONFile(Path filePath, JSONArray jsonArray) { - String content = jsonArray.toJSONString(); - File myFile; - try { - Files.createDirectories(filePath.getParent()); - myFile = filePath.toFile(); - - try (RandomAccessFile raf = new RandomAccessFile(myFile, "rw")) { - if(myFile.length() == 0) { - raf.write("[]".getBytes(StandardCharsets.UTF_8)); - } - raf.seek(myFile.length()-1); - - if(myFile.length() != 2) { - raf.write(",".getBytes(StandardCharsets.UTF_8)); - } - raf.write(content.substring(1).getBytes(StandardCharsets.UTF_8)); - } - }catch (IOException e) { - log.warn(String.format("Error occurred when trying to append into file: %s", filePath), e); - } - } - /** * Appends a JSON object array into file. * Creates the files if it doesn't exist @@ -219,11 +189,13 @@ public void appendJSONFile(Path filePath, JSONArray jsonArray) { */ public void writeSuUpdatesInFile(Path filePath, Stream responsesStream) throws IOException { Files.createDirectories(filePath.getParent()); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.findAndRegisterModules(); try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath.toFile(), true))) { writer.write("["); responsesStream.forEach(response -> { try { - writer.write(response.toJSONObject().toJSONString()); + writer.write(objectMapper.writeValueAsString(response)); writer.write(","); } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/src/main/java/fr/insee/genesis/infrastructure/utils/JSONUtils.java b/src/main/java/fr/insee/genesis/infrastructure/utils/JSONUtils.java deleted file mode 100644 index fd59615f..00000000 --- a/src/main/java/fr/insee/genesis/infrastructure/utils/JSONUtils.java +++ /dev/null @@ -1,26 +0,0 @@ -package fr.insee.genesis.infrastructure.utils; - -import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto; -import lombok.extern.slf4j.Slf4j; -import org.json.simple.JSONArray; - -import java.util.List; - -@Slf4j -public class JSONUtils { - private JSONUtils() { - throw new IllegalStateException("Utility class"); - } - /** - * Convert a list of survey units DTOs to a JSONArray ready to write - * @param surveyUnitUpdateDtos survey units to convert - * @return a JSONArray containing all objects - */ - public static JSONArray getJSONArrayFromResponses(List surveyUnitUpdateDtos) { - JSONArray jsonArray = new JSONArray(); - for(SurveyUnitUpdateDto surveyUnitUpdateDto : surveyUnitUpdateDtos){ - jsonArray.add(surveyUnitUpdateDto.toJSONObject()); - } - return jsonArray; - } -} diff --git a/src/test/java/fr/insee/genesis/controller/rest/ResponseControllerTest.java b/src/test/java/fr/insee/genesis/controller/rest/ResponseControllerTest.java index 83e6f179..d1f85151 100644 --- a/src/test/java/fr/insee/genesis/controller/rest/ResponseControllerTest.java +++ b/src/test/java/fr/insee/genesis/controller/rest/ResponseControllerTest.java @@ -146,8 +146,8 @@ void findResponsesByUEAndQuestionnaireTest(){ Assertions.assertThat(response.getStatusCode().is2xxSuccessful()).isTrue(); Assertions.assertThat(response.getBody()).isNotNull().isNotEmpty(); - Assertions.assertThat(response.getBody().get(0).getIdUE()).isEqualTo("TESTIDUE"); - Assertions.assertThat(response.getBody().get(0).getIdQuest()).isEqualTo("TESTIDQUESTIONNAIRE"); + Assertions.assertThat(response.getBody().getFirst().getIdUE()).isEqualTo("TESTIDUE"); + Assertions.assertThat(response.getBody().getFirst().getIdQuest()).isEqualTo("TESTIDQUESTIONNAIRE"); } @Test @@ -238,7 +238,7 @@ void getLatestForUEListTest(){ Assertions.assertThat(response.getStatusCode().is2xxSuccessful()).isTrue(); Assertions.assertThat(response.getBody()).isNotNull().isNotEmpty(); - Assertions.assertThat(response.getBody().get(0).getIdUE()).isEqualTo("TESTIDUE"); + Assertions.assertThat(response.getBody().getFirst().getIdUE()).isEqualTo("TESTIDUE"); } @Test @@ -247,7 +247,7 @@ void getAllIdUEsByQuestionnaireTest(){ Assertions.assertThat(response.getStatusCode().is2xxSuccessful()).isTrue(); Assertions.assertThat(response.getBody()).isNotNull().isNotEmpty(); - Assertions.assertThat(response.getBody().get(0).getIdUE()).isEqualTo("TESTIDUE"); + Assertions.assertThat(response.getBody().getFirst().getIdUE()).isEqualTo("TESTIDUE"); } @Test @@ -256,7 +256,7 @@ void getModesByQuestionnaireTest(){ Assertions.assertThat(response.getStatusCode().is2xxSuccessful()).isTrue(); Assertions.assertThat(response.getBody()).isNotNull().isNotEmpty().hasSize(1); - Assertions.assertThat(response.getBody().get(0)).isEqualTo(Mode.WEB); + Assertions.assertThat(response.getBody().getFirst()).isEqualTo(Mode.WEB); } private void addAdditionnalDtoToMongoStub(){ diff --git a/src/test/java/fr/insee/genesis/domain/dtos/CollectedVariableDtoTest.java b/src/test/java/fr/insee/genesis/domain/dtos/CollectedVariableDtoTest.java index aed2473c..0de1e6a0 100644 --- a/src/test/java/fr/insee/genesis/domain/dtos/CollectedVariableDtoTest.java +++ b/src/test/java/fr/insee/genesis/domain/dtos/CollectedVariableDtoTest.java @@ -1,6 +1,8 @@ package fr.insee.genesis.domain.dtos; -import org.assertj.core.api.Assertions; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -8,7 +10,7 @@ class CollectedVariableDtoTest { @Test - void toJSONTest(){ + void toJSONTest() throws JsonProcessingException { CollectedVariableDto collectedVariableDto = CollectedVariableDto.collectedVariableBuilder() .idVar("TESTIDVAR") .idParent("TESTIDPARENT") @@ -16,8 +18,11 @@ void toJSONTest(){ .values(new ArrayList<>(List.of(new String[]{"V1", "V2"}))) .build(); - Assertions.assertThat(collectedVariableDto.toJSONObject().toJSONString()).isEqualTo( - "{\"values\":[\"V1\",\"V2\"],\"idVar\":\"TESTIDVAR\",\"idLoop\":\"TESTIDLOOP\",\"idParent\":\"TESTIDPARENT\"}" - ); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.findAndRegisterModules(); + + Assertions.assertEquals(objectMapper.readTree(objectMapper.writeValueAsString(collectedVariableDto)), + objectMapper.readTree("{\"values\":[\"V1\",\"V2\"],\"idVar\":\"TESTIDVAR\",\"idLoop\":\"TESTIDLOOP\",\"idParent\":\"TESTIDPARENT\"}")); + } } diff --git a/src/test/java/fr/insee/genesis/domain/dtos/SurveyUnitUpdateDtoTest.java b/src/test/java/fr/insee/genesis/domain/dtos/SurveyUnitUpdateDtoTest.java index 3986e17e..375af3c0 100644 --- a/src/test/java/fr/insee/genesis/domain/dtos/SurveyUnitUpdateDtoTest.java +++ b/src/test/java/fr/insee/genesis/domain/dtos/SurveyUnitUpdateDtoTest.java @@ -1,7 +1,9 @@ package fr.insee.genesis.domain.dtos; -import org.assertj.core.api.Assertions; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import java.time.LocalDateTime; import java.util.ArrayList; @@ -9,7 +11,7 @@ public class SurveyUnitUpdateDtoTest { @Test - public void toJSONTest(){ + public void toJSONTest() throws JsonProcessingException { List externalVariableDtoList = new ArrayList<>(); VariableDto variableDto = VariableDto.builder().idVar("TESTIDVAR").values(List.of(new String[]{"V1", "V2"})).build(); externalVariableDtoList.add(variableDto); @@ -30,10 +32,13 @@ public void toJSONTest(){ .externalVariables(externalVariableDtoList) .build(); - Assertions.assertThat(surveyUnitUpdateDto).isNotNull(); + Assertions.assertNotNull(surveyUnitUpdateDto); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.findAndRegisterModules(); - Assertions.assertThat(surveyUnitUpdateDto.toJSONObject().toJSONString()).isEqualTo( - "{\"mode\":\"WEB\",\"idCampaign\":\"TESTIDCAMPAIGN\",\"externalVariables\":[{\"values\":[\"V1\",\"V2\"],\"idVar\":\"TESTIDVAR\"}],\"collectedVariables\":[{\"values\":[\"V1\",\"V2\"],\"idVar\":\"TESTIDVAR\",\"idLoop\":\"TESTIDLOOP\",\"idParent\":\"TESTIDPARENT\"}],\"idQuest\":\"TESTIDQUEST\",\"recordDate\":\"2000-01-01T00:00\",\"idUE\":\"TESTIDUE\",\"state\":\"COLLECTED\",\"fileDate\":\"2000-01-01T00:00\"}" + Assertions.assertEquals( + objectMapper.readTree("{\"idQuest\":\"TESTIDQUEST\",\"idCampaign\":\"TESTIDCAMPAIGN\",\"idUE\":\"TESTIDUE\",\"state\":\"COLLECTED\",\"mode\":\"WEB\",\"recordDate\":\"2000-01-01T12:00\",\"fileDate\":\"2000-01-01T12:00\",\"collectedVariables\":[{\"idVar\":\"TESTIDVAR\",\"values\":[\"V1\",\"V2\"],\"idLoop\":\"TESTIDLOOP\",\"idParent\":\"TESTIDPARENT\"}],\"externalVariables\":[{\"idVar\":\"TESTIDVAR\",\"values\":[\"V1\",\"V2\"]}]}"), + objectMapper.readTree(objectMapper.writeValueAsString(surveyUnitUpdateDto)) ); } diff --git a/src/test/java/fr/insee/genesis/domain/dtos/VariableDtoTest.java b/src/test/java/fr/insee/genesis/domain/dtos/VariableDtoTest.java index fb00bae7..46c4126a 100644 --- a/src/test/java/fr/insee/genesis/domain/dtos/VariableDtoTest.java +++ b/src/test/java/fr/insee/genesis/domain/dtos/VariableDtoTest.java @@ -1,6 +1,8 @@ package fr.insee.genesis.domain.dtos; -import org.assertj.core.api.Assertions; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -8,11 +10,15 @@ class VariableDtoTest { @Test - void toJSONTest(){ + void toJSONTest() throws JsonProcessingException { VariableDto variableDto = new VariableDto("TESTIDVAR", new ArrayList<>(List.of(new String[]{"V1", "V2"}))); - - Assertions.assertThat(variableDto.toJSONObject().toJSONString()).isEqualTo( + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.findAndRegisterModules(); + Assertions.assertEquals(objectMapper.readTree(objectMapper.writeValueAsString(variableDto)), + objectMapper.readTree("{\"values\":[\"V1\",\"V2\"],\"idVar\":\"TESTIDVAR\"}")); +/* Assertions.assertThat(objectMapper.writeValueAsString(variableDto)).isEqualTo( "{\"values\":[\"V1\",\"V2\"],\"idVar\":\"TESTIDVAR\"}" ); +*/ } } diff --git a/src/test/java/fr/insee/genesis/infrastructure/utils/FileUtilsTest.java b/src/test/java/fr/insee/genesis/infrastructure/utils/FileUtilsTest.java index f8918f62..b443ad28 100644 --- a/src/test/java/fr/insee/genesis/infrastructure/utils/FileUtilsTest.java +++ b/src/test/java/fr/insee/genesis/infrastructure/utils/FileUtilsTest.java @@ -5,8 +5,6 @@ import fr.insee.genesis.stubs.ConfigStub; import lombok.extern.slf4j.Slf4j; import org.assertj.core.api.Assertions; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -14,7 +12,6 @@ import java.nio.file.Path; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; @Slf4j class FileUtilsTest { @@ -54,43 +51,4 @@ void writeFileTest() throws IOException { Files.deleteIfExists(testFilePath); } - @Test - void appendFileTest() throws IOException { - //Given + When - Path testFilePath = Path.of(TestConstants.TEST_RESOURCES_DIRECTORY,"test.json"); - Files.deleteIfExists(testFilePath); - - JSONArray jsonArray = new JSONArray(); - JSONObject jsonObject = new JSONObject(); - jsonObject.put("id", 1); - jsonArray.add(jsonObject); - fileUtils.appendJSONFile(testFilePath, jsonArray); - - jsonArray = new JSONArray(); - jsonObject = new JSONObject(); - jsonObject.put("id", 2); - jsonArray.add(jsonObject); - fileUtils.appendJSONFile(testFilePath, jsonArray); - - jsonArray = new JSONArray(); - jsonObject = new JSONObject(); - jsonObject.put("id", 3); - jsonArray.add(jsonObject); - jsonObject = new JSONObject(); - jsonObject.put("id", 4); - jsonArray.add(jsonObject); - fileUtils.appendJSONFile(testFilePath, jsonArray); - - //Then - Assertions.assertThat(testFilePath.toFile()).exists(); - - try { - Assertions.assertThat(testFilePath.toFile()).hasContent("[{\"id\":1},{\"id\":2},{\"id\":3},{\"id\":4}]"); - }catch (AssertionError e){ - log.info(e.getMessage()); - Files.deleteIfExists(testFilePath); - fail(); - } - Files.deleteIfExists(testFilePath); - } } \ No newline at end of file diff --git a/src/test/java/fr/insee/genesis/infrastructure/utils/JSONUtilsTest.java b/src/test/java/fr/insee/genesis/infrastructure/utils/JSONUtilsTest.java deleted file mode 100644 index 0f8336be..00000000 --- a/src/test/java/fr/insee/genesis/infrastructure/utils/JSONUtilsTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package fr.insee.genesis.infrastructure.utils; - -import fr.insee.genesis.domain.dtos.CollectedVariableDto; -import fr.insee.genesis.domain.dtos.DataState; -import fr.insee.genesis.domain.dtos.Mode; -import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto; -import fr.insee.genesis.domain.dtos.VariableDto; -import org.assertj.core.api.Assertions; - -import org.json.simple.JSONArray; -import org.junit.Test; - -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; - -public class JSONUtilsTest { - @Test - public void getJSONArrayFromResponsesTest(){ - //Given - List responses = new ArrayList<>(); - - for(int i = 0; i < 2; i++) { - List externalVariableDtoList = new ArrayList<>(); - VariableDto variableDto = VariableDto.builder().idVar("TESTIDVAR").values(List.of(new String[]{"V1", "V2"})).build(); - externalVariableDtoList.add(variableDto); - - List collectedVariableDtoList = new ArrayList<>(); - CollectedVariableDto collectedVariableDto = new CollectedVariableDto("TESTIDVAR", List.of(new String[]{"V1", "V2"}), "TESTIDLOOP", "TESTIDPARENT"); - collectedVariableDtoList.add(collectedVariableDto); - - SurveyUnitUpdateDto surveyUnitUpdateDto = SurveyUnitUpdateDto.builder() - .idCampaign("TESTIDCAMPAIGN") - .idQuest("TESTIDQUEST") - .idUE("TESTIDUE" + i) - .mode(Mode.WEB) - .state(DataState.COLLECTED) - .fileDate(LocalDateTime.of(2000, 1, 1, 0, 0, 0)) - .recordDate(LocalDateTime.of(2000, 1, 1, 0, 0, 0)) - .collectedVariables(collectedVariableDtoList) - .externalVariables(externalVariableDtoList) - .build(); - - responses.add(surveyUnitUpdateDto); - } - - //When - JSONArray jsonArray = JSONUtils.getJSONArrayFromResponses(responses); - - //Then - Assertions.assertThat(jsonArray).hasSize(2); - } -} From 1dd864633913ff236f7bbff0f1af77ebef08b30a Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Thu, 29 Feb 2024 17:25:41 +0100 Subject: [PATCH 2/2] Delete ClasspathURIResolver.java --- .../utils/ClasspathURIResolver.java | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 src/main/java/fr/insee/genesis/controller/utils/ClasspathURIResolver.java diff --git a/src/main/java/fr/insee/genesis/controller/utils/ClasspathURIResolver.java b/src/main/java/fr/insee/genesis/controller/utils/ClasspathURIResolver.java deleted file mode 100644 index e1ca576f..00000000 --- a/src/main/java/fr/insee/genesis/controller/utils/ClasspathURIResolver.java +++ /dev/null @@ -1,37 +0,0 @@ -package fr.insee.genesis.controller.utils; - -import lombok.extern.slf4j.Slf4j; - -import javax.xml.transform.Source; -import javax.xml.transform.TransformerException; -import javax.xml.transform.URIResolver; -import javax.xml.transform.stream.StreamSource; - -@Slf4j -public class ClasspathURIResolver implements URIResolver { - - @Override - public Source resolve(String href, String base) throws TransformerException { - log.debug("Resolving URI with href: " + href + " and base: " + base); - String resolvedHref = getResolvedHref(href); - return new StreamSource(ClasspathURIResolver.class.getResourceAsStream(resolvedHref)); - } - - private static String getResolvedHref(String href) { - String resolvedHref; - if (href.startsWith("..")) { - if (href.startsWith("../..")) { - resolvedHref = href.replaceFirst("../..", "/xslt"); - log.debug("Resolved URI is: " + resolvedHref); - } else { - resolvedHref = href.replaceFirst("..", "/xslt"); - log.debug("Resolved XSLT URI is: " + resolvedHref); - } - } else { - resolvedHref = href; - log.debug("Resolved URI href is: " + resolvedHref); - } - return resolvedHref; - } - -}