Skip to content

Commit

Permalink
Merge pull request #261 from InseeFr/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
davdarras authored Jul 15, 2024
2 parents 2c22f97 + acfe9f7 commit 8435a74
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<description>Modules for queen back-office</description>

<properties>
<revision>4.3.13</revision>
<revision>4.3.14</revision>
<changelist></changelist>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private ResponseEntity<ApiError> generateResponseError(Exception ex, HttpStatus
* @return the apierror object with linked status code
*/
private ResponseEntity<ApiError> generateResponseError(Exception ex, HttpStatus status, WebRequest request, String overrideErrorMessage) {
log.error(ex.getMessage(), ex);
String errorMessage = ex.getMessage();
if (overrideErrorMessage != null) {
errorMessage = overrideErrorMessage;
Expand All @@ -77,7 +78,6 @@ private ResponseEntity<ApiError> generateResponseError(Exception ex, HttpStatus

@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<ApiError> noHandlerFoundException(NoHandlerFoundException e, WebRequest request) {
log.error(e.getMessage());
return generateResponseError(e, HttpStatus.NOT_FOUND, request);
}

Expand All @@ -90,22 +90,19 @@ public ResponseEntity<ApiError> accessDeniedException(AccessDeniedException e, W
public ResponseEntity<ApiError> handleMethodArgumentNotValid(
MethodArgumentNotValidException e,
WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.BAD_REQUEST, request, "Invalid parameters");
}

@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<ApiError> handleConstraintViolation(
ConstraintViolationException e,
WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.BAD_REQUEST, request, "Invalid data");
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ApiError> handleHttpMessageNotReadableException(
HttpMessageNotReadableException e, WebRequest request) {
log.error(e.getMessage(), e);

Throwable rootCause = e.getRootCause();

Expand All @@ -123,7 +120,6 @@ public ResponseEntity<ApiError> handleHttpMessageNotReadableException(

@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ApiError> noEntityFoundException(EntityNotFoundException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.NOT_FOUND, request);
}

Expand All @@ -134,49 +130,41 @@ public ResponseEntity<ApiError> updateCollectedDataException(UpdateCollectedData

@ExceptionHandler(AuthenticationTokenException.class)
public ResponseEntity<ApiError> authenticationTokenExceptionException(AuthenticationTokenException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.INTERNAL_SERVER_ERROR, request, ERROR_OCCURRED_LABEL);
}

@ExceptionHandler(HabilitationException.class)
public ResponseEntity<ApiError> habilitationException(HabilitationException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.FORBIDDEN, request);
}

@ExceptionHandler(QuestionnaireInvalidException.class)
public ResponseEntity<ApiError> questionnaireInvalidException(QuestionnaireInvalidException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.BAD_REQUEST, request);
}

@ExceptionHandler(CampaignDeletionException.class)
public ResponseEntity<ApiError> campaignDeletionException(CampaignDeletionException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.UNPROCESSABLE_ENTITY, request);
}

@ExceptionHandler(CampaignNotLinkedToQuestionnaireException.class)
public ResponseEntity<ApiError> campaignDeletionException(CampaignNotLinkedToQuestionnaireException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.BAD_REQUEST, request);
}

@ExceptionHandler(EntityAlreadyExistException.class)
public ResponseEntity<ApiError> entityAlreadyExistException(EntityAlreadyExistException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.BAD_REQUEST, request);
}

@ExceptionHandler(IntegrationComponentException.class)
public ResponseEntity<ApiError> integrationComponentException(IntegrationComponentException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.BAD_REQUEST, request);
}

@ExceptionHandler(JsonValidatorComponentInitializationException.class)
public ResponseEntity<ApiError> integrationComponentException(JsonValidatorComponentInitializationException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.BAD_REQUEST, request, ERROR_INVALID_DATA);
}

Expand All @@ -192,8 +180,7 @@ public ResponseEntity<ApiError> depositProofException(DepositProofException e, W

@ExceptionHandler(MetadataValueNotFoundException.class)
public ResponseEntity<ApiError> metadataValueNotFoundException(MetadataValueNotFoundException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.INTERNAL_SERVER_ERROR, request);
return generateResponseError(e, HttpStatus.NOT_FOUND, request);
}

@ExceptionHandler(StateDataInvalidDateException.class)
Expand All @@ -203,13 +190,11 @@ public ResponseEntity<ApiError> stateDataException(StateDataInvalidDateException

@ExceptionHandler(RestClientException.class)
public ResponseEntity<ApiError> exceptions(RestClientException e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.INTERNAL_SERVER_ERROR, request, ERROR_OCCURRED_LABEL);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ApiError> exceptions(Exception e, WebRequest request) {
log.error(e.getMessage(), e);
return generateResponseError(e, HttpStatus.INTERNAL_SERVER_ERROR, request, ERROR_OCCURRED_LABEL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,13 @@ void on_get_survey_unit_metadata_return_survey_unit_metadata() throws Exception
}

@Test
void on_get_survey_unit_metadata_when_invalid_metadata_return_500() throws Exception {
void on_get_survey_unit_metadata_when_invalid_metadata_return_404() throws Exception {
String surveyUnitId = "11";
mockMvc.perform(get("/api/survey-unit/" + surveyUnitId + "/metadata")
.accept(MediaType.APPLICATION_JSON)
.with(authentication(authenticatedUserTestHelper.getAdminUser()))
)
.andExpect(status().isInternalServerError());
.andExpect(status().isNotFound());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.insee.queen.domain.campaign.service.CampaignExistenceService;
import fr.insee.queen.domain.campaign.service.QuestionnaireModelService;
import fr.insee.queen.domain.common.cache.CacheName;
import fr.insee.queen.domain.common.exception.EntityNotFoundException;
import fr.insee.queen.domain.pilotage.service.exception.PilotageApiException;
import fr.insee.queen.domain.pilotage.gateway.PilotageRepository;
import fr.insee.queen.domain.pilotage.model.PilotageCampaign;
Expand All @@ -15,10 +16,7 @@
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -135,7 +133,15 @@ public List<PilotageCampaign> getInterviewerCampaigns() {

return campaigns.stream()
.map(PilotageCampaign::id)
.map(campaignId -> new PilotageCampaign(campaignId, questionnaireModelService.getQuestionnaireIds(campaignId)))
.map(campaignId -> {
try {
return new PilotageCampaign(campaignId, questionnaireModelService.getQuestionnaireIds(campaignId));
} catch (EntityNotFoundException ex) {
log.error("Campaign id {} from pilotage API was not found in the DB", campaignId);
return null;
}
})
.filter(Objects::nonNull)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import fr.insee.queen.domain.campaign.model.QuestionnaireModel;
import fr.insee.queen.domain.campaign.service.QuestionnaireModelService;
import fr.insee.queen.domain.common.exception.EntityNotFoundException;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,9 +17,14 @@ public class QuestionnaireModelFakeService implements QuestionnaireModelService
private boolean updated = false;
@Getter
private boolean created = false;
@Setter
private String campaignIdNotFound;

@Override
public List<String> getQuestionnaireIds(String campaignId) {
if(campaignIdNotFound != null && campaignIdNotFound.equals(campaignId)) {
throw new EntityNotFoundException("Entity not found");
}
return new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class PilotageServiceTest {
private PilotageApiService pilotageService;
private CampaignExistenceFakeService campaignExistenceService;
private PilotageFakeRepository pilotageRepository;
private QuestionnaireModelFakeService questionnaireModelFakeService;

@BeforeEach
public void init() {
SurveyUnitFakeService surveyUnitService = new SurveyUnitFakeService();
pilotageRepository = new PilotageFakeRepository();
campaignExistenceService = new CampaignExistenceFakeService();
QuestionnaireModelFakeService questionnaireModelFakeService = new QuestionnaireModelFakeService();
questionnaireModelFakeService = new QuestionnaireModelFakeService();
pilotageService = new PilotageApiService(surveyUnitService, campaignExistenceService, pilotageRepository, questionnaireModelFakeService);
}

Expand All @@ -51,7 +52,16 @@ void testGetInterviewerCampaigns01() {
void testGetInterviewerCampaigns02() {
List<PilotageCampaign> campaigns = pilotageService.getInterviewerCampaigns();
assertThat(campaigns).hasSize(2);
assertThat(campaigns.get(0).id()).isEqualTo(PilotageFakeRepository.INTERVIEWER_CAMPAIGN1_ID);
assertThat(campaigns.getFirst().id()).isEqualTo(PilotageFakeRepository.INTERVIEWER_CAMPAIGN1_ID);
}

@Test
@DisplayName("Should not retrieve unexisting campaigns in DB")
void testGetInterviewerCampaigns03() {
questionnaireModelFakeService.setCampaignIdNotFound(PilotageFakeRepository.INTERVIEWER_CAMPAIGN1_ID);
List<PilotageCampaign> campaigns = pilotageService.getInterviewerCampaigns();
assertThat(campaigns).hasSize(1);
assertThat(campaigns.getFirst().id()).isNotEqualTo(PilotageFakeRepository.INTERVIEWER_CAMPAIGN1_ID);
}

@Test
Expand Down

0 comments on commit 8435a74

Please sign in to comment.