Skip to content

Commit

Permalink
test: add coverage for survey service
Browse files Browse the repository at this point in the history
  • Loading branch information
davdarras committed Jan 22, 2025
1 parent a3652f9 commit 0370d5a
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Optional;

public interface SurveyService {

Page<Survey> findBySourceIdYearPeriodicity(Pageable pageable, String sourceId, Integer year, String periodicity);

/**
* @deprecated use findOptionalById instead
* @param id survey id
* @return survey
*/
@Deprecated(forRemoval = true)
Survey findById(String id);

Optional<Survey> findOptionalById(String id);

Page<Survey> findAll(Pageable pageable);

Survey insertOrUpdateSurvey(Survey survey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service
Expand All @@ -28,10 +28,15 @@ public Page<Survey> findBySourceIdYearPeriodicity(Pageable pageable, String sour

@Override
public Survey findById(String id) {
return surveyRepository.findById(id)
return findOptionalById(id)
.orElseThrow(() -> new NotFoundException(String.format("Survey %s not found", id)));
}

@Override
public Optional<Survey> findOptionalById(String id) {
return surveyRepository.findById(id);
}

@Override
public Page<Survey> findAll(Pageable pageable) {
return surveyRepository.findAll(pageable);
Expand Down Expand Up @@ -59,20 +64,19 @@ public void deleteSurveyById(String id) {
public Survey addCampaignToSurvey(Survey survey, Campaign campaign) {

Set<Campaign> campaigns;
try {
Survey surveyBase = findById(survey.getId());
Optional<Survey> optionalSurveyBase = findOptionalById(survey.getId());

if(optionalSurveyBase.isPresent()) {
Survey surveyBase = optionalSurveyBase.get();
campaigns = surveyBase.getCampaigns();
if(!isCampaignPresent(campaign, surveyBase)) {
if (!isCampaignPresent(campaign, surveyBase)) {
campaigns.add(campaign);
}
}
catch (NotFoundException e){
} else {
campaigns = Set.of(campaign);

}
survey.setCampaigns(campaigns);
return survey;

}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package fr.insee.survey.datacollectionmanagement.metadata.service.impl;

import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Campaign;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Survey;
import fr.insee.survey.datacollectionmanagement.metadata.service.impl.stub.SurveyRepositoryStub;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class SurveyServiceImplTest {
private SurveyServiceImpl surveyService;
private SurveyRepositoryStub surveyRepositoryStub;

@BeforeEach
void init() {
surveyRepositoryStub = new SurveyRepositoryStub();
surveyService = new SurveyServiceImpl(surveyRepositoryStub);
}

@Test
@DisplayName("Should return survey")
void findById_should_return_survey() {
// given
String surveyId = "survey-id";
Survey survey = new Survey();
survey.setId(surveyId);
surveyRepositoryStub.setSurveys(List.of(survey));

// when
Survey surveyResult = surveyService.findById(surveyId);

// then
assertThat(surveyResult).isNotNull();
assertThat(surveyResult.getId()).isEqualTo(surveyId);
}

@Test
@DisplayName("Should throw exception")
void findById_should_throw_exception() {
// given
String surveyId = "survey-id";
Survey survey = new Survey();
survey.setId(surveyId);
surveyRepositoryStub.setSurveys(List.of(survey));

// when & then
assertThatThrownBy(() -> surveyService.findById("not-exist-id"))
.isInstanceOf(NotFoundException.class);
}

@Test
@DisplayName("Should insert survey")
void insertOrUpdateSupport_should_insert_survey() {
// given
String surveyId = "survey-id";
Survey surveyToInsert = new Survey();
surveyToInsert.setId(surveyId);

// when
Survey surveyResult = surveyService.insertOrUpdateSurvey(surveyToInsert);

// then
assertThat(surveyResult).isNotNull();
assertThat(surveyResult.getCampaigns()).isNull();
}

@Test
@DisplayName("Should update survey")
void insertOrUpdateSupport_should_update_survey() {
// given
String campaignId = "campaign-id";
Campaign campaign = new Campaign();
campaign.setId(campaignId);
String surveyId = "survey-id";
Survey surveyInRepo = new Survey();
surveyInRepo.setId(surveyId);
surveyInRepo.setCampaigns(Set.of(campaign));
surveyRepositoryStub.setSurveys(List.of(surveyInRepo));

Survey surveyToUpdate = new Survey();
surveyToUpdate.setId(surveyId);

// when
Survey surveyResult = surveyService.insertOrUpdateSurvey(surveyToUpdate);

// then
assertThat(surveyResult).isNotNull();
assertThat(surveyResult.getCampaigns())
.isNotNull()
.hasSize(1)
.first()
.extracting(Campaign::getId)
.isEqualTo(campaignId);
}

@Test
@DisplayName("Should add campaign to survey")
void addCampaignToSurvey_should_add_campaign_to_survey() {
// given
String campaign1Id = "campaign-id1";
Campaign campaign1 = new Campaign();
campaign1.setId(campaign1Id);

String campaign2Id = "campaign-id2";
Campaign campaign2 = new Campaign();
campaign2.setId(campaign2Id);

String campaignToAddId = "campaign-id3";
Campaign campaignToAdd = new Campaign();
campaignToAdd.setId(campaignToAddId);

String surveyId = "survey-id";
Survey surveyInRepo = new Survey();
surveyInRepo.setId(surveyId);
HashSet<Campaign> campaignsInRepo = new HashSet<>();
campaignsInRepo.add(campaign1);
campaignsInRepo.add(campaign2);
surveyInRepo.setCampaigns(campaignsInRepo);

surveyRepositoryStub.setSurveys(List.of(surveyInRepo));

Survey survey = new Survey();
survey.setId(surveyId);

// when
Survey surveyResult = surveyService.addCampaignToSurvey(survey, campaignToAdd);

// then
assertThat(surveyResult).isNotNull();
assertThat(surveyResult.getCampaigns())
.isNotNull()
.hasSize(3)
.containsExactlyInAnyOrder(campaign1, campaign2, campaignToAdd);
}

@Test
@DisplayName("Should not add campaign if campaign already exist in survey")
void addCampaignToSurvey_should_not_add_campaign_if_campaign_already_exist_in_survey() {
// given
String campaign1Id = "campaign-id1";
Campaign campaign1 = new Campaign();
campaign1.setId(campaign1Id);

String surveyId = "survey-id";
Survey surveyInRepo = new Survey();
surveyInRepo.setId(surveyId);
HashSet<Campaign> campaignsInRepo = new HashSet<>();
campaignsInRepo.add(campaign1);
surveyInRepo.setCampaigns(campaignsInRepo);

surveyRepositoryStub.setSurveys(List.of(surveyInRepo));

Survey survey = new Survey();
survey.setId(surveyId);

// when
Survey surveyResult = surveyService.addCampaignToSurvey(survey, campaign1);

// then
assertThat(surveyResult).isNotNull();
assertThat(surveyResult.getCampaigns())
.isNotNull()
.hasSize(1)
.first()
.extracting(Campaign::getId)
.isEqualTo(campaign1Id);
}

@Test
@DisplayName("Should add campaign list if survey unit in db does not exist")
void addCampaignToSurvey_should_create_campaign_list_with_campaign_if_empty_campaign_for_survey() {
// given
String campaignToAddId = "campaign-id";
Campaign campaignToAdd = new Campaign();
campaignToAdd.setId(campaignToAddId);

String surveyId = "survey-id";
Survey survey = new Survey();
survey.setId(surveyId);
survey.setCampaigns(new HashSet<>());

// when
Survey surveyResult = surveyService.addCampaignToSurvey(survey, campaignToAdd);

// then
assertThat(surveyResult).isNotNull();
assertThat(surveyResult.getCampaigns())
.isNotNull()
.hasSize(1)
.first()
.extracting(Campaign::getId)
.isEqualTo(campaignToAdd.getId());
}
}
Loading

0 comments on commit 0370d5a

Please sign in to comment.