Skip to content

Commit

Permalink
Fixed error 400 when invalid json request + add test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisszmundy committed Dec 12, 2024
1 parent 9bdb6fa commit 93585c7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package fr.insee.genesis.domain.service.rawdata;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.model.surveyunit.rawdata.LunaticJsonDataModel;
import fr.insee.genesis.domain.ports.api.LunaticJsonRawDataApiPort;
Expand All @@ -21,7 +25,10 @@ public LunaticJsonRawDataService(LunaticJsonPersistancePort lunaticJsonPersistan
}

@Override
public void saveData(String campaignName, String dataJson, Mode mode){
public void saveData(String campaignName, String dataJson, Mode mode) throws JsonParseException {
if(!isJsonValid(dataJson)){
throw new JsonParseException("Invalid JSON synthax");
}
LunaticJsonDataModel lunaticJsonDataModel = LunaticJsonDataModel.builder()
.campaignId(campaignName)
.mode(mode)
Expand All @@ -31,4 +38,16 @@ public void saveData(String campaignName, String dataJson, Mode mode){

lunaticJsonPersistancePort.save(lunaticJsonDataModel);
}


private boolean isJsonValid(String json) {
ObjectMapper mapper = new ObjectMapper()
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
try {
mapper.readTree(json);
} catch (JacksonException e) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Mapper(componentModel = "spring")
public interface LunaticJsonDocumentMapper {
Expand All @@ -32,21 +29,14 @@ public interface LunaticJsonDocumentMapper {


@Named(value = "fromJsonToMap")
default Map<String, Object> fromJsonToMap(String dataJson) throws IOException {
if (Objects.nonNull(dataJson)) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(dataJson, new TypeReference<>() {
});
}
return Collections.emptyMap();
default Map<String, Object> fromJsonToMap(String dataJson) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(dataJson, new TypeReference<>(){});
}

@Named(value = "fromMapToJson")
default String fromMapToJson(Map<String, Object> dataMap) throws JsonProcessingException {
if (Objects.nonNull(dataMap)) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(dataMap);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.insee.genesis.domain.service.rawdata;

import com.fasterxml.jackson.core.JsonParseException;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.stubs.LunaticJsonPersistanceStub;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

class LunaticJsonRawDataServiceTest {
LunaticJsonPersistanceStub lunaticJsonPersistanceStub = new LunaticJsonPersistanceStub();
LunaticJsonRawDataService lunaticJsonRawDataService = new LunaticJsonRawDataService(lunaticJsonPersistanceStub);

@Test
void saveDataTest_Invalid(){
lunaticJsonPersistanceStub.getMongoStub().clear();
String campaignId = "SAMPLETEST-PARADATA-v1";

Assertions.assertThatThrownBy(() -> {
lunaticJsonRawDataService.saveData(
campaignId
,"{\"testdata\": \"ERROR"
, Mode.WEB
);
}).isInstanceOf(JsonParseException.class);
}
}

0 comments on commit 93585c7

Please sign in to comment.