Skip to content

Commit

Permalink
Merge pull request #35 from InseeFr/endpoint_moog_readonly_url
Browse files Browse the repository at this point in the history
Endpoint moog readonly url
  • Loading branch information
EricThuaud authored Nov 6, 2023
2 parents 2bfab88 + 1f468d5 commit 38201ef
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>fr.insee.survey</groupId>
<artifactId>platine-management</artifactId>
<version>1.0.27</version>
<version>1.0.28-rc</version>
<name>platine-management</name>
<description>REST API for communication between DB and Platine-Management UI and Platine-My-Surveys UI</description>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ private Constants() {
public static final String MOOG_API_CAMPAIGN_UPLOADS = "/api/moog/campaigns/{idCampaign}/uploads";
public static final String MOOG_API_CAMPAIGN_EXTRACTION = "/api/moog/campaigns/{idCampaign}/extraction";
public static final String MOOG_API_CAMPAIGN_SURVEYUNITS_FOLLOWUP = "/api/moog/campaigns/{idCampaign}/survey-units/follow-up";

public static final String MOOG_API_READONLY_URL = "/api/moog/readonly/campaigns/{idCampaign}/survey-unit/{surveyUnitId}";
public static final String API_MOOG_DELETE_QUESTIONING_EVENT="/api/moog/management-monitoring-infos/{id}";
public static final String API_CONTACTS_SEARCH = "/api/contacts/search";
public static final String API_CONTACTS_ACCREDITATIONS = "/api/contacts/{id}/accreditations";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public class MonitoringController {
@Autowired
MonitoringService monitoringService;

/*
* @Autowired
* QuestioningEventService questioningEventService;
*/

@Autowired
QuestioningService questioningService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.webjars.NotFoundException;

@RestController
@PreAuthorize("@AuthorizeMethodDecider.isInternalUser() "
Expand All @@ -44,9 +45,7 @@
@Tag(name = "5 - Moog", description = "Enpoints for moog")
@Slf4j
public class MoogController {

static final Logger LOGGER = LoggerFactory.getLogger(MoogController.class);


@Autowired
private MoogService moogService;

Expand Down Expand Up @@ -112,15 +111,30 @@ public ResponseEntity<?> getMoogQuestioningEvents(@PathVariable("campaign") Stri

@GetMapping(value = Constants.MOOG_API_CAMPAIGN_EXTRACTION, produces = "application/json")
public JSONCollectionWrapper<MoogExtractionRowDto> provideDataForExtraction(@PathVariable String idCampaign) {
LOGGER.info("Request GET for extraction of campaign : {}", idCampaign);
log.info("Request GET for extraction of campaign : {}", idCampaign);
return moogService.getExtraction(idCampaign);
}

@GetMapping(value = Constants.MOOG_API_CAMPAIGN_SURVEYUNITS_FOLLOWUP, produces = "application/json")
public JSONCollectionWrapper<MoogExtractionRowDto> displaySurveyUnitsToFollowUp(@PathVariable String idCampaign) {
LOGGER.info("Request GET for su to follow up - campaign {}", idCampaign);
log.info("Request GET for su to follow up - campaign {}", idCampaign);
return new JSONCollectionWrapper<MoogExtractionRowDto>(moogService.getSurveyUnitsToFollowUp(idCampaign));
}

@GetMapping(value = Constants.MOOG_API_READONLY_URL, produces = "application/json")
public ResponseEntity<String> getReadOnlyUrl(@PathVariable String idCampaign, @PathVariable String surveyUnitId){
log.info("Request READONLY url for su {} and campaign {}",surveyUnitId, idCampaign);
String url;
try {
url = moogService.getReadOnlyUrl(idCampaign, surveyUnitId);
return ResponseEntity.ok().body(url);
}
catch (NotFoundException e){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
}
catch (Exception e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import fr.insee.survey.datacollectionmanagement.query.dto.MoogQuestioningEventDto;
import fr.insee.survey.datacollectionmanagement.query.dto.MoogSearchDto;
import fr.insee.survey.datacollectionmanagement.view.domain.View;
import org.webjars.NotFoundException;

@Service
public interface MoogService {
Expand All @@ -23,4 +24,6 @@ public interface MoogService {
JSONCollectionWrapper<MoogExtractionRowDto> getExtraction(String idCampaign);

Collection<MoogExtractionRowDto> getSurveyUnitsToFollowUp(String idCampaign);

public String getReadOnlyUrl(String idCampaign, String surveyUnitId) throws NotFoundException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
import java.sql.SQLException;
import java.util.*;

import fr.insee.survey.datacollectionmanagement.config.ApplicationConfig;
import fr.insee.survey.datacollectionmanagement.config.JSONCollectionWrapper;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;
import fr.insee.survey.datacollectionmanagement.metadata.service.PartitioningService;
import fr.insee.survey.datacollectionmanagement.query.dto.MoogExtractionRowDto;
import fr.insee.survey.datacollectionmanagement.questioning.domain.Questioning;
import fr.insee.survey.datacollectionmanagement.questioning.service.QuestioningService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
Expand All @@ -21,10 +27,14 @@
import fr.insee.survey.datacollectionmanagement.query.service.MoogService;
import fr.insee.survey.datacollectionmanagement.view.domain.View;
import fr.insee.survey.datacollectionmanagement.view.service.ViewService;
import org.webjars.NotFoundException;

@Service
@Slf4j
public class MoogServiceImpl implements MoogService {

public static final String READONLY_QUESTIONNAIRE = "/readonly/questionnaire/";
public static final String UNITE_ENQUETEE = "/unite-enquetee/";
@Autowired
private ViewService viewService;

Expand All @@ -37,6 +47,15 @@ public class MoogServiceImpl implements MoogService {
@Autowired
private MoogRepository moogRepository;

@Autowired
QuestioningService questioningService;

@Autowired
PartitioningService partitioningService;

@Autowired
ApplicationConfig applicationConfig;

@Override
public List<View> moogSearch(String field) {

Expand Down Expand Up @@ -107,4 +126,29 @@ public Collection<MoogExtractionRowDto> getSurveyUnitsToFollowUp(String idCampai
return moogRepository.getSurveyUnitToFollowUp(idCampaign);
}

@Override
public String getReadOnlyUrl(String idCampaign, String surveyUnitId) throws NotFoundException {
Optional<Campaign> campaign = campaignService.findById(idCampaign);
if (!campaign.isPresent()) {
throw new NotFoundException("Campaign not found");
}
Set<Partitioning> setParts = campaign.get().getPartitionings();
Questioning questioning = null;
for (Partitioning part : setParts){
Questioning qTemp = questioningService.findByIdPartitioningAndSurveyUnitIdSu(part.getId(), surveyUnitId);
if(qTemp!=null){
questioning =qTemp;
break;
}
}
if(questioning!=null) {
return applicationConfig.getQuestioningUrl() + READONLY_QUESTIONNAIRE + questioning.getModelName()
+ UNITE_ENQUETEE + surveyUnitId;
}
String msg = "0 questioning found for campaign "+idCampaign+" and survey unit "+ surveyUnitId;
log.error(msg);
throw new NotFoundException(msg);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.Optional;

import fr.insee.survey.datacollectionmanagement.questioning.service.QuestioningService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -24,10 +26,9 @@
import fr.insee.survey.datacollectionmanagement.questioning.util.TypeQuestioningEvent;

@Service
@Slf4j
public class MySurveysServiceImpl implements MySurveysService {

private static final Logger LOGGER = LogManager.getLogger(MySurveysServiceImpl.class);

@Autowired
private QuestioningAccreditationService questioningAccreditationService;

Expand All @@ -38,7 +39,7 @@ public class MySurveysServiceImpl implements MySurveysService {
private QuestioningEventService questioningEventService;

@Autowired
ApplicationConfig applicationConfig;
QuestioningService questioningService;


@Override
Expand All @@ -56,8 +57,7 @@ public List<MyQuestioningDto> getListMySurveys(String id) {
surveyDto.setSurveyWording(survey.getLongWording());
surveyDto.setSurveyObjectives(survey.getLongObjectives());
surveyDto.setAccessUrl(
applicationConfig.getQuestioningUrl() + "/questionnaire/" + questioning.getModelName()
+ "/unite-enquetee/" + surveyUnitId);
questioningService.getAccessUrl(questioning, surveyUnitId));
surveyDto.setIdentificationCode(surveyUnitId);
surveyDto.setOpeningDate(new Timestamp(part.get().getOpeningDate().getTime()));
surveyDto.setClosingDate(new Timestamp(part.get().getClosingDate().getTime()));
Expand All @@ -70,16 +70,18 @@ public List<MyQuestioningDto> getListMySurveys(String id) {
surveyDto.setQuestioningStatus(questioningEvent.get().getType().name());
surveyDto.setQuestioningDate(new Timestamp(questioningEvent.get().getDate().getTime()));
} else {
LOGGER.debug("No questioningEvents found for questioning {} for identifier {}",
log.debug("No questioningEvents found for questioning {} for identifier {}",
questioning.getId(), id);
}

}
listSurveys.add(surveyDto);

}
LOGGER.info("Get my questionings for id {} - nb results: {}", id, listSurveys.size());
log.info("Get my questionings for id {} - nb results: {}", id, listSurveys.size());
return listSurveys;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public interface QuestioningService {
public int deleteQuestioningsOfOnePartitioning(Partitioning partitioning);

public Set<Questioning> findBySurveyUnitIdSu(String idSu);

public String getAccessUrl(Questioning questioning, String surveyUnitId);



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Optional;
import java.util.Set;

import fr.insee.survey.datacollectionmanagement.config.ApplicationConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -32,6 +33,9 @@ public class QuestioningServiceImpl implements QuestioningService {
@Autowired
private QuestioningAccreditationService questioningAccreditationService;

@Autowired
ApplicationConfig applicationConfig;

@Override
public Page<Questioning> findAll(Pageable pageable) {
return questioningRepository.findAll(pageable);
Expand Down Expand Up @@ -79,6 +83,13 @@ public Set<Questioning> findBySurveyUnitIdSu(String idSu) {
return questioningRepository.findBySurveyUnitIdSu(idSu);
}

@Override
public String getAccessUrl(Questioning questioning, String surveyUnitId) {
return applicationConfig.getQuestioningUrl() + "/questionnaire/" + questioning.getModelName()
+ "/unite-enquetee/" + surveyUnitId;
}


@Override
public Questioning findByIdPartitioningAndSurveyUnitIdSu(String idPartitioning,
String surveyUnitIdSu) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package fr.insee.survey.datacollectionmanagement.query.controller;

import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.query.service.CheckHabilitationService;
import fr.insee.survey.datacollectionmanagement.query.service.MoogService;
import fr.insee.survey.datacollectionmanagement.questioning.domain.SurveyUnit;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@SpringBootTest
@ActiveProfiles("test")
public class MoogControllerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private MoogService moogService;

@Test
public void getMoogReadOnlyUrl() throws Exception {
String idCampaign= "SOURCE12023T01";
String surveyUnitId= "100000000";
this.mockMvc.perform(get(Constants.MOOG_API_READONLY_URL, idCampaign,surveyUnitId)).andDo(print()).andExpect(status().isOk())
.andExpect(content().string("http://localhost:8081/readonly/questionnaire/m0/unite-enquetee/100000000"));
}


@Test
public void getMoogReadOnlyUrlCampaignNotFound() throws Exception {
String idCampaign= "SOURCE12023T00";
String surveyUnitId= "100000000";
this.mockMvc.perform(get(Constants.MOOG_API_READONLY_URL, idCampaign,surveyUnitId)).andDo(print()).andExpect(status().isNotFound());
}

@Test
public void getMoogReadOnlyUrlQuestioningNotFound() throws Exception {
String idCampaign= "SOURCE12023T01";
String surveyUnitId= "10";
this.mockMvc.perform(get(Constants.MOOG_API_READONLY_URL, idCampaign,surveyUnitId)).andDo(print()).andExpect(status().isNotFound());
}
}

0 comments on commit 38201ef

Please sign in to comment.