Skip to content

Commit

Permalink
Merge pull request #36 from InseeFr/devJsonWithJackson
Browse files Browse the repository at this point in the history
Remove json-simple and replace by Jackson
  • Loading branch information
loichenninger authored Feb 29, 2024
2 parents c3c3674 + 1dd8646 commit 6caf419
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 289 deletions.
23 changes: 6 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<springdoc.version>2.3.0</springdoc.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<cucumber.version>7.15.0</cucumber.version>
<karate.version>1.4.0.RC3</karate.version>
<!-- Proprietes sonar -->
<jacoco.version>0.8.11</jacoco.version>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
Expand All @@ -29,6 +28,7 @@
<!-- Pi Test-->
<pitest.version>1.15.8</pitest.version>
<pitest.junit.version>1.2.1</pitest.junit.version>
<jackson.version>2.16.1</jackson.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -85,11 +85,6 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<!-- XML libraries -->
<!-- XML-XSLT with Saxon -->
Expand All @@ -99,12 +94,14 @@
<version>12.4</version>
</dependency>

<!-- JSON -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- generate implementation auto -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
Expand All @@ -131,14 +128,6 @@
<scope>test</scope>
</dependency>

<!-- Karate -->
<!-- https://www.baeldung.com/karate-rest-api-testing -->
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>${karate.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -269,9 +268,9 @@ public ResponseEntity<SurveyUnitUpdateSimplified> 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);
Expand All @@ -297,9 +296,9 @@ public ResponseEntity<List<SurveyUnitUpdateSimplified>> 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)
Expand Down

This file was deleted.

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

Expand All @@ -20,12 +21,4 @@ public CollectedVariableDto(String idVar, List<String> values, String idLoop, St
this.idParent = idParent;
}

public JSONObject toJSONObject(){
JSONObject jsonObject = super.toJSONObject();
jsonObject.put("idLoop", idLoop);
jsonObject.put("idParent", idParent);

return jsonObject;
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<CollectedVariableDto> collectedVariables;
private List<VariableDto> 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;
}
}
12 changes: 3 additions & 9 deletions src/main/java/fr/insee/genesis/domain/dtos/VariableDto.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,11 +14,4 @@ public class VariableDto {
private String idVar;
private List<String> values;

public JSONObject toJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("idVar", idVar);
jsonObject.put("values",values);

return jsonObject;
}
}
36 changes: 4 additions & 32 deletions src/main/java/fr/insee/genesis/infrastructure/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -219,11 +189,13 @@ public void appendJSONFile(Path filePath, JSONArray jsonArray) {
*/
public void writeSuUpdatesInFile(Path filePath, Stream<SurveyUnitUpdateDto> 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);
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/fr/insee/genesis/infrastructure/utils/JSONUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(){
Expand Down
Loading

0 comments on commit 6caf419

Please sign in to comment.