Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping(path = "/responses/raw" )
public class RawResponseController {

private static final String SUCCESS_MESSAGE = "Interrogation %s saved";
private static final String PARTITION_ID = "partitionId";
private static final String INTERROGATION_ID = "interrogationId";
private final LunaticJsonRawDataApiPort lunaticJsonRawDataApiPort;

public RawResponseController(LunaticJsonRawDataApiPort lunaticJsonRawDataApiPort) {
Expand All @@ -52,7 +55,7 @@ public ResponseEntity<String> saveRawResponsesFromJsonBody(

@RequestParam("campaignName") String campaignName,
@RequestParam("questionnaireId") String questionnaireId,
@RequestParam("interrogationId") String interrogationId,
@RequestParam(INTERROGATION_ID) String interrogationId,
@RequestParam(value = "surveyUnitId", required = false) String idUE,
@RequestParam(value = "mode") Mode modeSpecified,
@RequestBody Map<String, Object> dataJson
Expand Down Expand Up @@ -96,12 +99,8 @@ public ResponseEntity<String> saveRawResponsesFromJsonBodyWithValidation(
new ObjectMapper().writeValueAsString(body)
)
);
if(!errors.isEmpty()){
throw new GenesisException(400, "Input data json is not valid \n%s".formatted(
Arrays.toString(errors.toArray()))
);
}

// Throw Genesis exception if errors are present
validate(errors);
//Check required ids
checkRequiredIds(body);
}catch (JsonProcessingException jpe){
Expand All @@ -111,9 +110,9 @@ public ResponseEntity<String> saveRawResponsesFromJsonBodyWithValidation(
}

LunaticJsonRawDataModel rawData = LunaticJsonRawDataModel.builder()
.campaignId(body.get("partitionId").toString())
.campaignId(body.get(PARTITION_ID).toString())
.questionnaireId(body.get("questionnaireModelId").toString())
.interrogationId(body.get("interrogationId").toString())
.interrogationId(body.get(INTERROGATION_ID).toString())
.idUE(body.get("surveyUnitId").toString())
.mode(Mode.getEnumFromJsonName(body.get("mode").toString()))
.data(body)
Expand All @@ -125,23 +124,9 @@ public ResponseEntity<String> saveRawResponsesFromJsonBodyWithValidation(
return ResponseEntity.status(500).body("Unexpected error");
}

log.info("Data saved for interrogationId {} and partition {}",body.get("interrogationId").toString(),
body.get("partitionId").toString());
return ResponseEntity.status(201).body(String.format(SUCCESS_MESSAGE,body.get("interrogationId").toString()));
}

private void checkRequiredIds(Map<String, Object> body) throws GenesisException {
for(String requiredKey : List.of(
"partitionId",
"questionnaireModelId",
"interrogationId",
"surveyUnitId",
"mode"
)){
if(body.get(requiredKey) == null){
throw new GenesisException(400, "No %s found in body".formatted(requiredKey));
}
}
log.info("Data saved for interrogationId {} and partition {}",body.get(INTERROGATION_ID).toString(),
body.get(PARTITION_ID).toString());
return ResponseEntity.status(201).body(String.format(SUCCESS_MESSAGE,body.get(INTERROGATION_ID).toString()));
}

//GET unprocessed
Expand All @@ -157,7 +142,7 @@ public ResponseEntity<List<LunaticJsonRawDataUnprocessedDto>> getUnproccessedJso
@GetMapping(path= "/lunatic-json/get/by-interrogation-mode-and-campaign")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<LunaticJsonRawDataModel> getJsonRawData(
@RequestParam("interrogationId") String interrogationId,
@RequestParam(INTERROGATION_ID) String interrogationId,
@RequestParam("campaignName") String campaignName,
@RequestParam(value = "mode") Mode modeSpecified
){
Expand Down Expand Up @@ -188,4 +173,31 @@ public ResponseEntity<String> processJsonRawData(
}
}

private void validate(Set<ValidationMessage> errors) throws GenesisException {
if(!errors.isEmpty()){
String errorMessage = errors.stream()
.map(ValidationMessage::getMessage)
.collect(Collectors.joining(System.lineSeparator() + " - "));

throw new GenesisException(
400,
"Input data JSON is not valid: %n - %s".formatted(errorMessage)
);
}
}

private void checkRequiredIds(Map<String, Object> body) throws GenesisException {
for(String requiredKey : List.of(
PARTITION_ID,
"questionnaireModelId",
INTERROGATION_ID,
"surveyUnitId",
"mode"
)){
if(body.get(requiredKey) == null){
throw new GenesisException(400, "No %s found in body".formatted(requiredKey));
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import fr.insee.genesis.controller.dto.QuestionnaireWithCampaign;
import fr.insee.genesis.controller.dto.SurveyUnitDto;
import fr.insee.genesis.controller.dto.SurveyUnitInputDto;
import fr.insee.genesis.controller.services.MetadataService;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.exceptions.GenesisException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public DataProcessingContextModel getContext(String interrogationId) throws Gene
return null;
}
if(partitionIds.size() > 1){
throw new GenesisException(500,"Multiple partitions for interrogation %s\n%s".formatted(
throw new GenesisException(500,"Multiple partitions for interrogation %s %n%s".formatted(
interrogationId,
Arrays.toString(partitionIds.toArray())
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,8 @@ public List<List<SurveyUnitModel>> findLatestByIdAndByQuestionnaireIdAndModeOrde
List<List<SurveyUnitModel>> listLatestUpdatesbyVariables = new ArrayList<>();

//1) QUERY
// => convertion of "List<InterrogationId>" -> "List<String>" for query using lamda
/*
List<String> queryInParam = new ArrayList<String>();
for(InterrogationId interrId : interrogationIds) {
queryInParam.add(interrId.getInterrogationId());
}
*/
List<String> queryInParam = interrogationIds.stream().map(InterrogationId::getInterrogationId).collect(Collectors.toList());
// => conversion of "List<InterrogationId>" -> "List<String>" for query using lamda
List<String> queryInParam = interrogationIds.stream().map(InterrogationId::getInterrogationId).toList();

//Get !!!all versions!!! of a set of "interrogationIds"
List<SurveyUnitModel> allResponsesVersionsSet = surveyUnitPersistencePort.findBySetOfIdsAndQuestionnaireIdAndMode(questionnaireId, mode, queryInParam);
Expand All @@ -174,10 +168,10 @@ public List<List<SurveyUnitModel>> findLatestByIdAndByQuestionnaireIdAndModeOrde
.sorted((o1, o2) -> o2.getRecordDate().compareTo(o1.getRecordDate())) //Sorting update by date (latest updates first by date of upload in database)
.toList();

List<SurveyUnitModel> LatestUpdates = extractLatestUpdates(allResponsesVersionsForSingleInterrId);
List<SurveyUnitModel> latestUpdates = extractLatestUpdates(allResponsesVersionsForSingleInterrId);

//3) add to result (: keep same existing process)
listLatestUpdatesbyVariables.add(LatestUpdates);
listLatestUpdatesbyVariables.add(latestUpdates);
});

return listLatestUpdatesbyVariables;
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/cucumber/functional_tests/MainDefinitions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cucumber.functional_tests;

import com.fasterxml.jackson.databind.ObjectMapper;
import cucumber.TestConstants;
import fr.insee.bpm.exceptions.MetadataParserException;
import fr.insee.bpm.metadata.model.VariablesMap;
Expand Down Expand Up @@ -328,12 +327,11 @@ public void check_external_variable_content_in_mongo(
}

@Then("If we get latest states for {string} in collected variable {string}, survey unit {string} we should have {string} for iteration {int}")
public void check_latest_state_collected(String questionnaireId, String variableName, String interrogationId, String expectedValue, int iteration) throws GenesisException, IOException {
public void check_latest_state_collected(String questionnaireId, String variableName, String interrogationId, String expectedValue, int iteration) throws GenesisException {
ResponseEntity<Object> response =
responseController.findResponsesByInterrogationAndQuestionnaireLatestStates(interrogationId, questionnaireId);
Assertions.assertThat(response.getStatusCode().value()).isEqualTo(200);

ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
SurveyUnitQualityToolDto surveyUnitQualityToolDto = (SurveyUnitQualityToolDto) response.getBody();

List<VariableQualityToolDto> variableQualityToolDtos = surveyUnitQualityToolDto.getCollectedVariables().stream().filter(
Expand All @@ -353,7 +351,7 @@ public void check_latest_state_collected(String questionnaireId, String variable
}

@Then("If we get latest states for {string} in external variable {string}, survey unit {string} we should have {string} for iteration {int}")
public void check_latest_state_external(String questionnaireId, String variableName, String interrogationId, String expectedValue, int iteration) throws IOException, GenesisException {
public void check_latest_state_external(String questionnaireId, String variableName, String interrogationId, String expectedValue, int iteration) throws GenesisException {
ResponseEntity<Object> response =
responseController.findResponsesByInterrogationAndQuestionnaireLatestStates(interrogationId, questionnaireId);
Assertions.assertThat(response.getStatusCode().value()).isEqualTo(200);
Expand Down Expand Up @@ -435,7 +433,7 @@ public void check_su_latest_states_collected_variables_volumetry(String interrog
@Then("The extracted survey unit latest states response should have a survey unit DTO has interrogationId " +
"{string}" +
" with {int} external variables")
public void check_su_latest_states_external_variables_volumetry(String interrogationId, int expectedVolumetry) throws IOException {
public void check_su_latest_states_external_variables_volumetry(String interrogationId, int expectedVolumetry) {
Assertions.assertThat(surveyUnitLatestStatesResponse).isNotNull();
Assertions.assertThat(surveyUnitLatestStatesResponse.getBody()).isNotNull();
SurveyUnitQualityToolDto surveyUnitQualityToolDto = (SurveyUnitQualityToolDto) surveyUnitLatestStatesResponse.getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void reset() throws IOException {


//When + Then
@Test
@Test
void getAllInterrogationIdsByQuestionnaireTest() {
ResponseEntity<List<InterrogationId>> response = interrogationControllerStatic.getAllInterrogationIdsByQuestionnaire(DEFAULT_QUESTIONNAIRE_ID);

Expand All @@ -53,6 +53,13 @@ void getAllInterrogationIdsByQuestionnaireTest() {
Assertions.assertThat(response.getBody().getFirst().getInterrogationId()).isEqualTo(DEFAULT_INTERROGATION_ID);
}

@Test
void countAllInterrogationIdsByQuestionnaireTest() {
ResponseEntity<Long> response = interrogationControllerStatic.countAllInterrogationIdsByQuestionnaire(DEFAULT_QUESTIONNAIRE_ID);

Assertions.assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
Assertions.assertThat(response.getBody()).isNotNull();
Assertions.assertThat(response.getBody()).isEqualTo(1L);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void getLatestForUEListTest() {

// Perret tests
@Test
void getLatestByStatesSurveyDataTest() throws GenesisException, IOException {
void getLatestByStatesSurveyDataTest() throws GenesisException {
//GIVEN
//Recent Collected already in stub
//Old Collected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.insee.genesis.domain.service.context;

import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.domain.model.context.schedule.ServiceToCall;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.exceptions.GenesisException;
import fr.insee.genesis.infrastructure.document.context.DataProcessingContextDocument;
import fr.insee.genesis.stubs.DataProcessingContextPersistancePortStub;
Expand All @@ -15,6 +17,8 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertThrows;

class DataProcessingContextServiceTest {
//Given
static SurveyUnitPersistencePortStub surveyUnitPersistencePortStub;
Expand All @@ -32,6 +36,7 @@ static void init(){
@BeforeEach
void reset(){
dataProcessingContextPersistencePortStub.getMongoStub().clear();
surveyUnitPersistencePortStub.getMongoStub().clear();

List<KraftwerkExecutionSchedule> kraftwerkExecutionScheduleList = new ArrayList<>();
kraftwerkExecutionScheduleList.add(new KraftwerkExecutionSchedule(
Expand Down Expand Up @@ -182,4 +187,57 @@ void removeExpiredSchedules_test_delete_document() throws GenesisException {
).toList().getFirst().getKraftwerkExecutionScheduleList()).isEmpty();

}

@Test
void getContext_shouldThrow500IfMultiplePartitions() {
// Given
SurveyUnitModel su1 = SurveyUnitModel.builder()
.campaignId("CAMPAIGN1")
.interrogationId("00001")
.build();
SurveyUnitModel su2 = SurveyUnitModel.builder()
.campaignId("CAMPAIGN2")
.interrogationId("00001")
.build();
List<SurveyUnitModel> sus = new ArrayList<>();
sus.add(su1);
sus.add(su2);
surveyUnitPersistencePortStub.saveAll(sus);

// When & Then
GenesisException ex = assertThrows(GenesisException.class, () -> dataProcessingContextService.getContext("00001"));
//To ensure test is portable on Unix/Linux/macOS and windows systems
String normalizedMessage = ex.getMessage().replaceAll("\\r?\\n", "");
Assertions.assertThat(ex.getStatus()).isEqualTo(500);
Assertions.assertThat(normalizedMessage).isEqualTo("Multiple partitions for interrogation 00001 [CAMPAIGN2, CAMPAIGN1]");
}

@Test
void getContext_shouldThrow404IfNoInterrogations() {
// When & Then
GenesisException ex = assertThrows(GenesisException.class, () -> dataProcessingContextService.getContext("00001"));
Assertions.assertThat(ex.getStatus()).isEqualTo(404);
Assertions.assertThat(ex.getMessage()).isEqualTo("No interrogation in database with id 00001");
}

@Test
void getContext_shouldReturnContextIfOnePartition() throws GenesisException {
// Given
SurveyUnitModel su1 = SurveyUnitModel.builder()
.campaignId("TEST")
.interrogationId("00001")
.build();
List<SurveyUnitModel> sus = new ArrayList<>();
sus.add(su1);
surveyUnitPersistencePortStub.saveAll(sus);
DataProcessingContextModel result = dataProcessingContextService.getContext("00001");

// When & Then
Assertions.assertThat(result).isNotNull();
Assertions.assertThat(result.getPartitionId()).isEqualTo("TEST");
Assertions.assertThat(result.isWithReview()).isFalse();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public List<SurveyUnitModel> findByIds(String interrogationId, String questionna
* @author Adrien Marchal
*/
public List<SurveyUnitModel> findBySetOfIdsAndQuestionnaireIdAndMode(String questionnaireId, String mode, List<String> interrogationIdSet) {
//TODO : TO BE IMPLEMENTED
return new ArrayList<SurveyUnitModel>();
}
//========= OPTIMISATIONS PERFS (START) ==========
Expand Down Expand Up @@ -102,11 +101,7 @@ public long countInterrogationIdsByQuestionnaireId(String questionnaireId) {
* @author Adrien Marchal
*/
public List<SurveyUnitModel> findPageableInterrogationIdsByQuestionnaireId(String questionnaireId, Long skip, Long limit) {
List<SurveyUnitModel> surveyUnitModelList = new ArrayList<>();
if(skip < mongoStub.size()) {

}
return surveyUnitModelList;
return List.of();
}
//======= OPTIMISATIONS PERFS (END) =========

Expand Down
Loading