Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gap 2532/grant validation clean up #287

Merged
merged 4 commits into from
Apr 17, 2024
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
@@ -1,7 +1,7 @@
package gov.cabinetoffice.gap.adminbackend.constants;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -26,6 +26,7 @@ public class ValidationMaps {
new AbstractMap.SimpleEntry<String, Object>("allowedTypes",
new String[] { "DOC", "DOCX", "ODT", "PDF", "XLS", "XLSX", "ZIP" })));

public final static Map<String, Object> NO_VALIDATION = Collections.emptyMap();
public final static Map<String, Object> NO_VALIDATION = new HashMap<>();


}
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ void updateApplicationEntityFromPatchDto(ApplicationFormPatchDTO patchDTO,
// add/update a single value
default void updateGenericQuestionPatchToQuestionDto(QuestionGenericPatchDTO questionGenericPatchDTO,
@MappingTarget ApplicationFormQuestionDTO questionDto) {

if (questionGenericPatchDTO == null) {
return;
}

boolean responseTypeChanging = false;

if (questionGenericPatchDTO.getProfileField() != null) {
questionDto.setProfileField(questionGenericPatchDTO.getProfileField());
}
Expand All @@ -81,19 +84,18 @@ default void updateGenericQuestionPatchToQuestionDto(QuestionGenericPatchDTO que
questionDto.setQuestionSuffix(questionGenericPatchDTO.getQuestionSuffix());
}
if (questionGenericPatchDTO.getResponseType() != null) {
responseTypeChanging = true;
questionDto.setResponseType(questionGenericPatchDTO.getResponseType());
}

Map<String, Object> map = questionGenericPatchDTO.getValidation();
if (questionDto.getValidation() != null) {
if (questionDto.getValidation() != null && !responseTypeChanging) {
if (map != null) {
questionDto.getValidation().putAll(map);
questionDto.getValidation().putAll(map);
}
}
else {
if (map != null) {
else if (map != null) {
questionDto.setValidation(map);
}
}
}

Expand All @@ -103,6 +105,8 @@ default void updateOptionsQuestionPatchToQuestionDto(QuestionOptionsPatchDTO que
return;
}

boolean responseTypeChanging = false;

if (questionOptionsPatchDTO.getProfileField() != null) {
questionDto.setProfileField(questionOptionsPatchDTO.getProfileField());
}
Expand All @@ -119,29 +123,29 @@ default void updateOptionsQuestionPatchToQuestionDto(QuestionOptionsPatchDTO que
questionDto.setQuestionSuffix(questionOptionsPatchDTO.getQuestionSuffix());
}
if (questionOptionsPatchDTO.getResponseType() != null) {
responseTypeChanging = true;
questionDto.setResponseType(questionOptionsPatchDTO.getResponseType());
}
if (questionDto.getValidation() != null) {
Map<String, Object> map = questionOptionsPatchDTO.getValidation();

Map<String, Object> map = questionOptionsPatchDTO.getValidation();
if (questionDto.getValidation() != null && !responseTypeChanging) {
if (map != null) {
questionDto.getValidation().putAll(map);
}
}
else {
Map<String, Object> map = questionOptionsPatchDTO.getValidation();
if (map != null) {
questionDto.setValidation(map);
}
}
List<String> list = questionOptionsPatchDTO.getOptions();
if (questionDto.getOptions() != null) {
List<String> list = questionOptionsPatchDTO.getOptions();
if (list != null) {
questionDto.getOptions().clear();
questionDto.getOptions().addAll(list);
}
}
else {
List<String> list = questionOptionsPatchDTO.getOptions();
if (list != null) {
questionDto.setOptions(new ArrayList<>(list));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import gov.cabinetoffice.gap.adminbackend.entities.ApplicationFormEntity;
import gov.cabinetoffice.gap.adminbackend.entities.SchemeEntity;
import gov.cabinetoffice.gap.adminbackend.enums.ApplicationStatusEnum;
import gov.cabinetoffice.gap.adminbackend.enums.ResponseTypeEnum;
import gov.cabinetoffice.gap.adminbackend.exceptions.ApplicationFormException;
import gov.cabinetoffice.gap.adminbackend.exceptions.ConflictException;
import gov.cabinetoffice.gap.adminbackend.exceptions.FieldViolationException;
Expand Down Expand Up @@ -326,6 +327,72 @@ void patchQuestionValuesGenericHappyPathTest() {
this.utilMock.verify(() -> ApplicationFormUtils.updateAuditDetailsAfterFormChange(any(), eq(false)));
}

@Test
void patchQuestionsValuesChangeQuestionType_shortAnswerToDropdown_confirmValidationIsReplaced(){
ArgumentCaptor<ApplicationFormEntity> argument = ArgumentCaptor.forClass(ApplicationFormEntity.class);

ApplicationFormEntity existingForm = SAMPLE_SECOND_APPLICATION_FORM_ENTITY;

existingForm.getDefinition()
.getSectionById(SAMPLE_SECTION_ID)
.setQuestions(List.of(buildQuestion(ResponseTypeEnum.ShortAnswer, SAMPLE_QUESTION_ID, SAMPLE_QUESTION_FIELD_TITLE, null)));

when(applicationFormRepository.findById(SAMPLE_APPLICATION_ID))
.thenReturn(Optional.of(existingForm));

doReturn(SAMPLE_APPLICATION_FORM_ENTITY)
.when(applicationFormService).save(any());

ApplicationFormQuestionDTO updatedQuestionResponseType = buildQuestion(ResponseTypeEnum.Dropdown,
SAMPLE_QUESTION_ID, SAMPLE_QUESTION_FIELD_TITLE, SAMPLE_QUESTION_OPTIONS );

applicationFormService.patchQuestionValues(SAMPLE_APPLICATION_ID,
SAMPLE_SECTION_ID, SAMPLE_QUESTION_ID, updatedQuestionResponseType, new MockHttpSession());

verify(applicationFormService).save(argument.capture());

ApplicationFormEntity savedForm = argument.getValue();

ApplicationFormQuestionDTO updatedQuestion = savedForm.getDefinition().getSectionById(SAMPLE_SECTION_ID).getQuestionById(SAMPLE_QUESTION_ID);

assertThat(updatedQuestion.getResponseType()).isEqualTo(ResponseTypeEnum.Dropdown);
assertThat(updatedQuestion.getValidation()).isEqualTo(ResponseTypeEnum.Dropdown.getValidation());

}

@Test
void patchQuestionsValuesChangeQuestionType_dropdownToShortAnswer_confirmValidationIsAdded(){
ArgumentCaptor<ApplicationFormEntity> argument = ArgumentCaptor.forClass(ApplicationFormEntity.class);

ApplicationFormEntity existingForm = SAMPLE_SECOND_APPLICATION_FORM_ENTITY;

existingForm.getDefinition()
.getSectionById(SAMPLE_SECTION_ID)
.setQuestions(List.of(buildQuestion(ResponseTypeEnum.Dropdown, SAMPLE_QUESTION_ID, SAMPLE_QUESTION_FIELD_TITLE, null)));

when(applicationFormRepository.findById(SAMPLE_APPLICATION_ID))
.thenReturn(Optional.of(existingForm));

doReturn(SAMPLE_APPLICATION_FORM_ENTITY)
.when(applicationFormService).save(any());

ApplicationFormQuestionDTO updatedQuestionResponseType = buildQuestion(ResponseTypeEnum.ShortAnswer,
SAMPLE_QUESTION_ID, SAMPLE_QUESTION_FIELD_TITLE, null );

applicationFormService.patchQuestionValues(SAMPLE_APPLICATION_ID,
SAMPLE_SECTION_ID, SAMPLE_QUESTION_ID, updatedQuestionResponseType, new MockHttpSession());

verify(applicationFormService).save(argument.capture());

ApplicationFormEntity savedForm = argument.getValue();

ApplicationFormQuestionDTO updatedQuestion = savedForm.getDefinition().getSectionById(SAMPLE_SECTION_ID).getQuestionById(SAMPLE_QUESTION_ID);

assertThat(updatedQuestion.getResponseType()).isEqualTo(ResponseTypeEnum.ShortAnswer);
assertThat(updatedQuestion.getValidation()).isEqualTo(ResponseTypeEnum.ShortAnswer.getValidation());

}

@Test
void patchQuestionValuesOptionsHappyPathTest() {
ArgumentCaptor<ApplicationFormEntity> argument = ArgumentCaptor.forClass(ApplicationFormEntity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ public class ApplicationFormTestData {

public final static Map<String, Object> SAMPLE_VALIDATION = Collections.singletonMap("mandatory", true);

public static ApplicationFormQuestionDTO buildQuestion(ResponseTypeEnum responseType, String questionId, String questionTitle, List<String> options){
return new ApplicationFormQuestionDTO(questionId,
"ORG_TYPE", null, questionTitle, "Answer the question", null, null, null,
responseType, responseType.getValidation(), options, 1);
}

public final static ApplicationFormQuestionDTO SAMPLE_QUESTION = new ApplicationFormQuestionDTO(SAMPLE_QUESTION_ID,
"ORG_TYPE", null, SAMPLE_QUESTION_FIELD_TITLE, "Answer the question", null, null, null,
ResponseTypeEnum.YesNo, Collections.singletonMap("mandatory", true), null, 1);
ResponseTypeEnum.YesNo, ResponseTypeEnum.YesNo.getValidation(), null, 1);

public final static ApplicationFormQuestionDTO SAMPLE_QUESTION_WITH_OPTIONS = new ApplicationFormQuestionDTO(
SAMPLE_QUESTION_ID, "ORG_TYPE", null, "Select one of the folloiwng", "Answer the question", null, null,
null, ResponseTypeEnum.Dropdown, Collections.singletonMap("mandatory", true), SAMPLE_QUESTION_OPTIONS, 1);
SAMPLE_QUESTION_ID, "ORG_TYPE", null, "Select one of the following", "Answer the question", null, null,
null, ResponseTypeEnum.Dropdown, ResponseTypeEnum.Dropdown.getValidation(), SAMPLE_QUESTION_OPTIONS, 1);

public final static List<ApplicationFormQuestionDTO> SAMPLE_QUESTION_LIST = new LinkedList<>(
Collections.singletonList(SAMPLE_QUESTION));
Expand Down
Loading