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

Dfr 3175 CT controller postcode field not mandatory amend application details #1775

Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a6e65f7
Create controller
hamzahtahirhmcts Jul 9, 2024
dc4227c
extend basecontroller
hamzahtahirhmcts Jul 9, 2024
88fd07b
add postmap for /amend-application-app-sol
hamzahtahirhmcts Jul 9, 2024
1f2531c
add postmap for /amend-application-app
hamzahtahirhmcts Jul 9, 2024
ebf3902
add postmap for /amend-application-res-sol
hamzahtahirhmcts Jul 9, 2024
2890432
check applicant postcode and solicitor postcode methods
hamzahtahirhmcts Jul 9, 2024
baab5f3
check respondent postcode and respondent solicitor postcode method
hamzahtahirhmcts Jul 9, 2024
09b60be
Call validation methods
hamzahtahirhmcts Jul 9, 2024
16f3622
Add descriptions
hamzahtahirhmcts Jul 9, 2024
f7a8c13
rename methods
hamzahtahirhmcts Jul 9, 2024
0150e90
Add test data
hamzahtahirhmcts Jul 11, 2024
29d0d9f
Add unit tests to cover errors for each page
hamzahtahirhmcts Jul 11, 2024
e80f13c
Add unit tests to cover valid postcodes
hamzahtahirhmcts Jul 11, 2024
08a09a3
Use constants instead
hamzahtahirhmcts Jul 11, 2024
4b2ed03
Merge branch 'master' into DFR-3175-new-controller-Postcode-field-not…
hamzahtahirhmcts Jul 11, 2024
8984129
Add new line
hamzahtahirhmcts Jul 11, 2024
2e125d6
Add validation for postalService
hamzahtahirhmcts Jul 11, 2024
26f67a8
Remove the initializer
hamzahtahirhmcts Jul 11, 2024
d91e97a
rename to amend-application-validate-applicant-solicitor-address
hamzahtahirhmcts Jul 11, 2024
5457097
rename to amend-application-validate-applicant-address
hamzahtahirhmcts Jul 11, 2024
1f1f8f5
rename to amend-application-validate-respondent-solicitor
hamzahtahirhmcts Jul 11, 2024
59c9e61
use requireNonNull
hamzahtahirhmcts Jul 11, 2024
6d9722d
Merge remote-tracking branch 'origin/DFR-3175-new-controller-Postcode…
hamzahtahirhmcts Jul 11, 2024
0a0c58b
Mock postalService and finremCaseDetailsMapper
hamzahtahirhmcts Jul 12, 2024
f50e175
Merge branch 'master' into DFR-3175-new-controller-Postcode-field-not…
hamzahtahirhmcts Jul 12, 2024
caf242c
Add missing test case
hamzahtahirhmcts Jul 12, 2024
510f05b
Fix SonarCloud issues
hamzahtahirhmcts Jul 12, 2024
113ff3c
Fix SonarCloud issues
hamzahtahirhmcts Jul 12, 2024
04b7715
Merge remote-tracking branch 'origin/DFR-3175-new-controller-Postcode…
hamzahtahirhmcts Jul 12, 2024
bdfe056
Merge branch 'master' into DFR-3175-new-controller-Postcode-field-not…
hamzahtahirhmcts Jul 16, 2024
48d3c3c
Merge branch 'master' into DFR-3175-new-controller-Postcode-field-not…
hamzahtahirhmcts Jul 22, 2024
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
@@ -0,0 +1,138 @@
package uk.gov.hmcts.reform.finrem.caseorchestration.controllers;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.reform.ccd.client.model.AboutToStartOrSubmitCallbackResponse;
import uk.gov.hmcts.reform.ccd.client.model.CallbackRequest;
import uk.gov.hmcts.reform.ccd.client.model.CaseDetails;
import uk.gov.hmcts.reform.finrem.caseorchestration.mapper.FinremCaseDetailsMapper;
import uk.gov.hmcts.reform.finrem.caseorchestration.model.ccd.FinremCaseDetails;
import uk.gov.hmcts.reform.finrem.caseorchestration.service.InternationalPostalService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static uk.gov.hmcts.reform.finrem.caseorchestration.OrchestrationConstants.AUTHORIZATION_HEADER;
import static uk.gov.hmcts.reform.finrem.caseorchestration.OrchestrationConstants.YES_VALUE;
import static uk.gov.hmcts.reform.finrem.caseorchestration.model.ccd.CCDConfigConstant.APPLICANT_ADDRESS;
import static uk.gov.hmcts.reform.finrem.caseorchestration.model.ccd.CCDConfigConstant.APPLICANT_REPRESENTED;
import static uk.gov.hmcts.reform.finrem.caseorchestration.model.ccd.CCDConfigConstant.CONTESTED_RESPONDENT_REPRESENTED;
import static uk.gov.hmcts.reform.finrem.caseorchestration.model.ccd.CCDConfigConstant.CONTESTED_SOLICITOR_ADDRESS;
import static uk.gov.hmcts.reform.finrem.caseorchestration.model.ccd.CCDConfigConstant.RESPONDENT_ADDRESS;
import static uk.gov.hmcts.reform.finrem.caseorchestration.model.ccd.CCDConfigConstant.RESP_SOLICITOR_ADDRESS;

@RestController
@RequestMapping(value = "/case-orchestration")
@Slf4j
@RequiredArgsConstructor
public class AmendApplicationContestedController extends BaseController {

private static final String postCode = "PostCode";
hamzahtahirhmcts marked this conversation as resolved.
Show resolved Hide resolved

@Autowired
private FinremCaseDetailsMapper finremCaseDetailsMapper;

private final InternationalPostalService postalService;


hamzahtahirhmcts marked this conversation as resolved.
Show resolved Hide resolved
@PostMapping(path = "/amend-application-validate-applicant-solicitor-address", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(description = "Validate postcode on applicant solicitor page")
public ResponseEntity<AboutToStartOrSubmitCallbackResponse> amendApplicationAppSolicitor(
@RequestBody CallbackRequest callbackRequest,
@RequestHeader(value = AUTHORIZATION_HEADER, required = false) final String authToken
) {
Map<String, Object> caseData = callbackRequest.getCaseDetails().getData();

List<String> errors = new ArrayList<>();
validateApplicantSolicitorPostcodeDetails(caseData, errors);

return ResponseEntity.ok(AboutToStartOrSubmitCallbackResponse.builder().data(caseData).errors(errors).build());
}

@PostMapping(path = "/amend-application-validate-applicant-address", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(description = "Validate postcode on applicant details page")
public ResponseEntity<AboutToStartOrSubmitCallbackResponse> amendApplicationApp(
@RequestBody CallbackRequest callbackRequest,
@RequestHeader(value = AUTHORIZATION_HEADER, required = false) final String authToken
) {
CaseDetails caseDetails = callbackRequest.getCaseDetails();
Map<String, Object> caseData = callbackRequest.getCaseDetails().getData();
List<String> errors = new ArrayList<>();
validateApplicantPostcodeDetails(caseData, errors);

FinremCaseDetails finremCaseDetails = finremCaseDetailsMapper.mapToFinremCaseDetails(caseDetails);
errors.addAll(postalService.validate(finremCaseDetails.getData()));

return ResponseEntity.ok(AboutToStartOrSubmitCallbackResponse.builder().data(caseData).errors(errors).build());
}

@PostMapping(path = "/amend-application-validate-respondent-solicitor", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(description = "Validate postcode on respondent solicitor page")
public ResponseEntity<AboutToStartOrSubmitCallbackResponse> amendApplicationRespondentSolicitor(
@RequestBody CallbackRequest callbackRequest,
@RequestHeader(value = AUTHORIZATION_HEADER, required = false) final String authToken
) {
Map<String, Object> caseData = callbackRequest.getCaseDetails().getData();
List<String> errors = new ArrayList<>();
validateRespondentPostcodeDetails(caseData, errors);

return ResponseEntity.ok(AboutToStartOrSubmitCallbackResponse.builder().data(caseData).errors(errors).build());
}

private void validateApplicantSolicitorPostcodeDetails(Map<String, Object> caseData, List<String> errors) {

if (YES_VALUE.equals(caseData.get(APPLICANT_REPRESENTED))) {
Map<String, Object> applicantSolicitorAddress = (Map<String, Object>) caseData.get(CONTESTED_SOLICITOR_ADDRESS);
String applicantSolicitorPostCode = (String) applicantSolicitorAddress.get(postCode);

if (StringUtils.isBlank(applicantSolicitorPostCode)) {
errors.add("Postcode field is required for applicant solicitor address.");
}
}

}

private void validateApplicantPostcodeDetails(Map<String, Object> caseData, List<String> errors) {

Map<String, Object> applicantAddress = (Map<String, Object>) caseData.get(APPLICANT_ADDRESS);
String applicantPostCode = (String) applicantAddress.get(postCode);

if (StringUtils.isBlank(applicantPostCode)) {
errors.add("Postcode field is required for applicant address.");
}
}

private void validateRespondentPostcodeDetails(Map<String, Object> caseData, List<String> errors) {

if (YES_VALUE.equals(caseData.get(CONTESTED_RESPONDENT_REPRESENTED))) {
Map<String, Object> respondentSolicitorAddress = (Map<String, Object>) caseData.get(RESP_SOLICITOR_ADDRESS);
String respondentPostCode = (String) respondentSolicitorAddress.get(postCode);

if (StringUtils.isBlank(respondentPostCode)) {
errors.add("Postcode field is required for respondent solicitor address.");
}
} else {
Map<String, Object> respondentAddress = (Map<String, Object>) caseData.get(RESPONDENT_ADDRESS);
String respondentPostCode = (String) respondentAddress.get(postCode);

if (StringUtils.isBlank(respondentPostCode)) {
so99y marked this conversation as resolved.
Show resolved Hide resolved
errors.add("Postcode field is required for respondent address.");
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package uk.gov.hmcts.reform.finrem.caseorchestration.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;

import java.io.File;
import java.util.Objects;

import static com.jayway.jsonassert.impl.matcher.IsCollectionWithSize.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static uk.gov.hmcts.reform.finrem.caseorchestration.OrchestrationConstants.AUTHORIZATION_HEADER;

@WebMvcTest(AmendApplicationContestedController.class)
public class AmendApplicationContestedControllerTest extends BaseControllerTest {

private static final String AMEND_APPLICATION_APP_SOL_URL = "/case-orchestration/amend-application-validate-applicant-solicitor-address";
private static final String AMEND_APPLICATION_APP_URL = "/case-orchestration/amend-application-validate-applicant-address";
private static final String AMEND_APPLICATION_RES_SOL_URL = "/case-orchestration/amend-application-validate-respondent-solicitor";
public static final String AUTH_TOKEN = "tokien:)";
hamzahtahirhmcts marked this conversation as resolved.
Show resolved Hide resolved

private final ObjectMapper objectMapper = new ObjectMapper();
hamzahtahirhmcts marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void givenInvalidApplicantSolicitorPostcode_whenAmendApplication_thenReturnValidationError() throws Exception {
requestContent = objectMapper.readTree(new File(Objects.requireNonNull(getClass()
.getResource("/fixtures/contested/amend-applicant-solicitor-details-postcode-validation.json")).toURI()));

mvc.perform(post(AMEND_APPLICATION_APP_SOL_URL)
.content(requestContent.toString())
.header(AUTHORIZATION_HEADER, AUTH_TOKEN)
.contentType(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors", hasSize(1)))
.andExpect(jsonPath("$.errors[0]", is("Postcode field is required for applicant solicitor address.")));
}

@Test
public void givenInvalidApplicantPostcode_whenAmendApplication_thenReturnValidationError() throws Exception {
requestContent = objectMapper.readTree(new File(Objects.requireNonNull(getClass()
.getResource("/fixtures/contested/amend-applicant-solicitor-details-postcode-validation.json")).toURI()));

mvc.perform(post(AMEND_APPLICATION_APP_URL)
.content(requestContent.toString())
.header(AUTHORIZATION_HEADER, AUTH_TOKEN)
.contentType(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors", hasSize(1)))
.andExpect(jsonPath("$.errors[0]", is("Postcode field is required for applicant address.")));
}

@Test
public void givenInvalidRespondentSolicitorPostcode_whenAmendApplication_thenReturnValidationError() throws Exception {
requestContent = objectMapper.readTree(new File(Objects.requireNonNull(getClass()
.getResource("/fixtures/contested/amend-applicant-solicitor-details-postcode-validation.json")).toURI()));

mvc.perform(post(AMEND_APPLICATION_RES_SOL_URL)
.content(requestContent.toString())
.header(AUTHORIZATION_HEADER, AUTH_TOKEN)
.contentType(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors", hasSize(1)))
.andExpect(jsonPath("$.errors[0]", is("Postcode field is required for respondent solicitor address.")));
}

@Test
public void givenInvalidRespondentPostcode_whenAmendApplication_thenReturnValidationError() throws Exception {
requestContent = objectMapper.readTree(new File(Objects.requireNonNull(getClass()
.getResource("/fixtures/contested/amend-applicant-solicitor-details-postcode-validation.json")).toURI()));

((ObjectNode) requestContent.get("case_details").get("case_data")).put("respondentRepresented", "No");


mvc.perform(post(AMEND_APPLICATION_RES_SOL_URL)
.content(requestContent.toString())
.header(AUTHORIZATION_HEADER, AUTH_TOKEN)
.contentType(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors", hasSize(1)))
.andExpect(jsonPath("$.errors[0]", is("Postcode field is required for respondent address.")));
}

@Test
public void givenValidApplicantSolicitorPostcode_whenAmendApplication_thenReturnSuccess() throws Exception {
requestContent = objectMapper.readTree(new File(Objects.requireNonNull(getClass()
.getResource("/fixtures/contested/amend-applicant-solicitor-details-postcode-validation.json")).toURI()));

((ObjectNode) requestContent.get("case_details").get("case_data").get("applicantSolicitorAddress")).put("PostCode", "AB12 3CD");

mvc.perform(post(AMEND_APPLICATION_APP_SOL_URL)
.content(requestContent.toString())
.header(AUTHORIZATION_HEADER, AUTH_TOKEN)
.contentType(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors", hasSize(0)));
}

@Test
public void givenValidApplicantPostcode_whenAmendApplication_thenReturnSuccess() throws Exception {
requestContent = objectMapper.readTree(new File(Objects.requireNonNull(getClass()
.getResource("/fixtures/contested/amend-applicant-solicitor-details-postcode-validation.json")).toURI()));

((ObjectNode) requestContent.get("case_details").get("case_data").get("applicantAddress")).put("PostCode", "AB12 3CD");

mvc.perform(post(AMEND_APPLICATION_APP_URL)
.content(requestContent.toString())
.header(AUTHORIZATION_HEADER, AUTH_TOKEN)
.contentType(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors", hasSize(0)));
}

@Test
public void givenValidRespondentSolicitorPostcode_whenAmendApplication_thenReturnSuccess() throws Exception {
requestContent = objectMapper.readTree(new File(Objects.requireNonNull(getClass()
.getResource("/fixtures/contested/amend-applicant-solicitor-details-postcode-validation.json")).toURI()));

((ObjectNode) requestContent.get("case_details").get("case_data").get("rSolicitorAddress")).put("PostCode", "AB12 3CD");

mvc.perform(post(AMEND_APPLICATION_RES_SOL_URL)
.content(requestContent.toString())
.header(AUTHORIZATION_HEADER, AUTH_TOKEN)
.contentType(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors", hasSize(0)));
}

@Test
public void givenValidRespondentPostcode_whenAmendApplication_thenReturnSuccess() throws Exception {
requestContent = objectMapper.readTree(new File(Objects.requireNonNull(getClass()
.getResource("/fixtures/contested/amend-applicant-solicitor-details-postcode-validation.json")).toURI()));

((ObjectNode) requestContent.get("case_details").get("case_data")).put("respondentRepresented", "No");
((ObjectNode) requestContent.get("case_details").get("case_data").get("respondentAddress")).put("PostCode", "AB12 3CD");

mvc.perform(post(AMEND_APPLICATION_RES_SOL_URL)
.content(requestContent.toString())
.header(AUTHORIZATION_HEADER, AUTH_TOKEN)
.contentType(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.errors", hasSize(0)));
}
}
hamzahtahirhmcts marked this conversation as resolved.
Show resolved Hide resolved
Loading