Skip to content

Commit

Permalink
Merge pull request #57 from InseeFr/devGetQuestionnaires
Browse files Browse the repository at this point in the history
Added get questionnaires by campaignId
  • Loading branch information
alexisszmundy authored May 3, 2024
2 parents 1ed9953 + c3fdd46 commit 8395477
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ public ResponseEntity<List<Mode>> getModesByQuestionnaire(@RequestParam("idQuest
return ResponseEntity.ok(modes);
}

@Operation(summary = "List questionnaires used for a given campaign")
@GetMapping(path = "/get-questionnaires/by-campaign")
public ResponseEntity<List<String>> getQuestionnairesByCampaign(@RequestParam("idCampaign") String idCampaign) {
List<String> questionnaires = surveyUnitService.findIdQuestionnairesByIdCampaign(idCampaign);
return ResponseEntity.ok(questionnaires);
}

//Utilities

private void treatCampaignWithMode(String campaignName, Mode mode, List<GenesisError> errors) throws IOException, ParserConfigurationException, SAXException, XMLStreamException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public interface SurveyUnitUpdateApiPort {
Long deleteByIdQuestionnaire(String idQuestionnaire);

long countResponses();

List<String> findIdQuestionnairesByIdCampaign(String idCampaign);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.insee.genesis.domain.ports.spi;

import com.fasterxml.jackson.core.JsonProcessingException;
import fr.insee.genesis.domain.dtos.SurveyUnitDto;
import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto;

Expand All @@ -23,4 +24,6 @@ public interface SurveyUnitUpdatePersistencePort {
Long deleteByIdQuestionnaire(String idQuestionnaire);

long count();

List<String> findIdQuestionnairesByIdCampaign(String idCampaign);
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public long countResponses() {
return surveyUnitUpdatePersistencePort.count();
}

@Override
public List<String> findIdQuestionnairesByIdCampaign(String idCampaign) {
List<String> idQuestionnaireList = surveyUnitUpdatePersistencePort.findIdQuestionnairesByIdCampaign(idCampaign);
return idQuestionnaireList.stream().distinct().toList();
}

private static List<Mode> getDistinctsModes(List<SurveyUnitUpdateDto> surveyUnits) {
List<Mode> sources = new ArrayList<>();
surveyUnits.forEach(surveyUnitDto -> sources.add(surveyUnitDto.getMode()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package fr.insee.genesis.infrastructure.adapter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.insee.genesis.domain.dtos.SurveyUnitDto;
import fr.insee.genesis.domain.dtos.SurveyUnitUpdateDto;
import fr.insee.genesis.domain.ports.spi.SurveyUnitUpdatePersistencePort;
Expand Down Expand Up @@ -71,6 +74,25 @@ public long count() {
return mongoRepository.count();
}

@Override
public List<String> findIdQuestionnairesByIdCampaign(String idCampaign){
List<String> mongoResponse = mongoRepository.findIdQuestionnairesByIdCampaign(idCampaign).stream().distinct().toList();

//Extract idQuestionnaires from JSON response
List<String> idQuestionnaires = new ArrayList<>();
for(String line : mongoResponse){
ObjectMapper objectMapper = new ObjectMapper();
try{
JsonNode jsonNode = objectMapper.readTree(line);
idQuestionnaires.add(jsonNode.get("idQuestionnaire").asText());
}catch (JsonProcessingException e){
log.error(e.getMessage());
}
}

return idQuestionnaires;
}

public List<SurveyUnitDto> findIdUEsByIdQuestionnaire(String idQuestionnaire) {
List<SurveyUnitDocument> surveyUnits = mongoRepository.findIdUEsByIdQuestionnaire(idQuestionnaire);
return surveyUnits.isEmpty() ? Collections.emptyList() : SurveyUnitDocumentMapper.INSTANCE.listDocumentToListDto(surveyUnits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface SurveyUnitUpdateMongoDBRepository extends MongoRepository<Surve

List<SurveyUnitUpdateDocument> findByIdUEAndIdQuestionnaire(String idUE, String idQuestionnaire);

@Query(value = "{ 'idQuestionnaire' : ?0 }", fields = "{ 'idUE' : 1, 'mode' : 1}")
@Query(value = "{ 'idQuestionnaire' : ?0 }", fields = "{ 'idUE' : 1, 'mode' : 1 }")
List<SurveyUnitDocument> findIdUEsByIdQuestionnaire(String idQuestionnaire);

Long deleteByIdQuestionnaire(String idQuestionnaire);
Expand All @@ -26,4 +26,7 @@ public interface SurveyUnitUpdateMongoDBRepository extends MongoRepository<Surve
Stream<SurveyUnitUpdateDocument> findByIdQuestionnaire(String idQuestionnaire);

long count();

@Query(value = "{ 'idCampaign' : ?0 }", fields = "{ _id : 0, 'idQuestionnaire' : 1 }")
List<String> findIdQuestionnairesByIdCampaign(String idCampaign);
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,16 @@ void getModesByQuestionnaireTest() {
Assertions.assertThat(response.getBody().getFirst()).isEqualTo(Mode.WEB);
}

@Test
void getQuestionnairesByCampaignTest() {
addAdditionnalDtoToMongoStub("TESTQUESTIONNAIRE2");

ResponseEntity<List<String>> response = responseControllerStatic.getQuestionnairesByCampaign("TESTIDCAMPAIGN");

Assertions.assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
Assertions.assertThat(response.getBody()).isNotNull().isNotEmpty().hasSize(2);
}

// Utilities
private void addAdditionnalDtoToMongoStub() {
List<VariableDto> externalVariableDtoList = new ArrayList<>();
Expand All @@ -347,4 +357,27 @@ private void addAdditionnalDtoToMongoStub() {
.build();
surveyUnitUpdatePersistencePortStub.getMongoStub().add(recentDTO);
}

private void addAdditionnalDtoToMongoStub(String idQuestionnaire) {
List<VariableDto> externalVariableDtoList = new ArrayList<>();
VariableDto variableDto = VariableDto.builder().idVar("TESTIDVAR").values(List.of(new String[]{"V1", "V2"})).build();
externalVariableDtoList.add(variableDto);

List<CollectedVariableDto> collectedVariableDtoList = new ArrayList<>();
CollectedVariableDto collectedVariableDto = new CollectedVariableDto("TESTIDVAR", List.of(new String[]{"V1", "V2"}), "TESTIDLOOP", "TESTIDPARENT");
collectedVariableDtoList.add(collectedVariableDto);

SurveyUnitUpdateDto recentDTO = SurveyUnitUpdateDto.builder()
.idCampaign("TESTIDCAMPAIGN")
.mode(Mode.WEB)
.idUE("TESTIDUE")
.idQuest(idQuestionnaire)
.state(DataState.COLLECTED)
.fileDate(LocalDateTime.of(2023, 2, 2, 0, 0, 0))
.recordDate(LocalDateTime.of(2024, 2, 2, 0, 0, 0))
.externalVariables(externalVariableDtoList)
.collectedVariables(collectedVariableDtoList)
.build();
surveyUnitUpdatePersistencePortStub.getMongoStub().add(recentDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.ResponseEntity;

import java.time.LocalDateTime;
import java.time.Month;
Expand Down Expand Up @@ -153,6 +154,14 @@ void findIdUEsByIdQuestionnaireTest(){
).isNotEmpty();
}

@Test
void getQuestionnairesByCampaignTest() {
addAdditionnalDtoToMongoStub("TESTQUESTIONNAIRE2");

Assertions.assertThat(surveyUnitUpdateImplStatic.findIdQuestionnairesByIdCampaign("TESTIDCAMPAIGN")).isNotEmpty().hasSize(2);

}

private void addAdditionnalDtoToMongoStub(){
List<VariableDto> externalVariableDtoList = new ArrayList<>();
VariableDto variableDto = VariableDto.builder().idVar("TESTIDVAR").values(List.of(new String[]{"V1", "V2"})).build();
Expand All @@ -176,4 +185,27 @@ private void addAdditionnalDtoToMongoStub(){
surveyUnitUpdatePersistencePortStub.getMongoStub().add(recentDTO);
}

private void addAdditionnalDtoToMongoStub(String idQuestionnaire) {
List<VariableDto> externalVariableDtoList = new ArrayList<>();
VariableDto variableDto = VariableDto.builder().idVar("TESTIDVAR").values(List.of(new String[]{"V1", "V2"})).build();
externalVariableDtoList.add(variableDto);

List<CollectedVariableDto> collectedVariableDtoList = new ArrayList<>();
CollectedVariableDto collectedVariableDto = new CollectedVariableDto("TESTIDVAR", List.of(new String[]{"V1", "V2"}), "TESTIDLOOP", "TESTIDPARENT");
collectedVariableDtoList.add(collectedVariableDto);

SurveyUnitUpdateDto recentDTO = SurveyUnitUpdateDto.builder()
.idCampaign("TESTIDCAMPAIGN")
.mode(Mode.WEB)
.idUE("TESTIDUE")
.idQuest(idQuestionnaire)
.state(DataState.COLLECTED)
.fileDate(LocalDateTime.of(2023, 2, 2, 0, 0, 0))
.recordDate(LocalDateTime.of(2024, 2, 2, 0, 0, 0))
.externalVariables(externalVariableDtoList)
.collectedVariables(collectedVariableDtoList)
.build();
surveyUnitUpdatePersistencePortStub.getMongoStub().add(recentDTO);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.insee.genesis.stubs;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import fr.insee.genesis.domain.dtos.SurveyUnitDto;
Expand Down Expand Up @@ -87,5 +89,14 @@ public long count() {
return mongoStub.size();
}

@Override
public List<String> findIdQuestionnairesByIdCampaign(String idCampaign) {
Set<String> idQuestionnaireSet = new HashSet<>();
for(SurveyUnitUpdateDto surveyUnitUpdateDto : mongoStub){
if(surveyUnitUpdateDto.getIdCampaign().equals(idCampaign))
idQuestionnaireSet.add(surveyUnitUpdateDto.getIdQuest());
}

return idQuestionnaireSet.stream().toList();
}
}

0 comments on commit 8395477

Please sign in to comment.