diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/professionalapi/ProfessionalInternalUserFunctionalTest.java b/src/functionalTest/java/uk/gov/hmcts/reform/professionalapi/ProfessionalInternalUserFunctionalTest.java index 82c1179593..1ac19f4da4 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/professionalapi/ProfessionalInternalUserFunctionalTest.java +++ b/src/functionalTest/java/uk/gov/hmcts/reform/professionalapi/ProfessionalInternalUserFunctionalTest.java @@ -13,6 +13,7 @@ import org.springframework.http.HttpStatus; import uk.gov.hmcts.reform.lib.util.serenity5.SerenityTest; import uk.gov.hmcts.reform.professionalapi.controller.constants.IdamStatus; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.MfaUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.NewUserCreationRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.OrganisationCreationRequest; @@ -30,11 +31,14 @@ import uk.gov.hmcts.reform.professionalapi.util.FeatureToggleConditionExtension; import uk.gov.hmcts.reform.professionalapi.util.ToggleEnable; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -54,6 +58,7 @@ import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; import static uk.gov.hmcts.reform.professionalapi.client.ProfessionalApiClient.createOrganisationRequest; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.PBA_STATUS_MESSAGE_ACCEPTED; +import static uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressUpdateRequest.dxAddressUpdateRequest; import static uk.gov.hmcts.reform.professionalapi.controller.request.OrganisationCreationRequest.anOrganisationCreationRequest; import static uk.gov.hmcts.reform.professionalapi.controller.request.UserCreationRequest.aUserCreationRequest; import static uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus.REVIEW; @@ -1231,4 +1236,339 @@ private static List> sortByValue(final List (String) map.get(key))) .collect(Collectors.toList()); } + + + @Test + void updateBothContactInformationAndDxAdressForOrgNeedUpdatingSuccessScenario() { + log.info("updateBothContactInformationAndDxAdressForOrgNeedUpdatingSuccessScenario :: STARTED"); + + String orgId1 = createActiveOrganisation(); + String orgId2 = createActiveOrganisation(); + + ContactInformationUpdateRequest contactInformationCreationRequest = + createContactInformationUpdateRequestWithDxAddress(orgId1,"addressLine1", + "addressLine3","uprn1",orgId2,"addLine1","addLine3","uprn2", + true,true,true,true,null,null); + + + Response result = professionalApiClient.updateContactInformationsToOrganisation( + contactInformationCreationRequest,OK); + + assertNotNull(result); + assertThat(result.statusCode()).isEqualTo(200); + + //retrieve saved contact info from organisation + JsonPath orgResponse = professionalApiClient.retrieveOrganisationDetails( + orgId1, hmctsAdmin,OK); + assertNotNull(orgResponse); + List savedContactInformationList = (List) orgResponse.get("contactInformation"); + assertThat(savedContactInformationList).isNotEmpty().hasSize(2); + //Assuming only one record found + HashMap savedContactInfo = savedContactInformationList.get(0); + + JsonPath orgResponse1 = professionalApiClient.retrieveOrganisationDetails( + orgId2, hmctsAdmin,OK); + assertNotNull(orgResponse); + List savedContactInformationList1 = (List) orgResponse1.get("contactInformation"); + assertThat(savedContactInformationList1).isNotEmpty().hasSize(2); + HashMap savedContactInfo1 = savedContactInformationList.get(0); + + ContactInformationUpdateRequest.ContactInformationUpdateData + contactInfoData = contactInformationCreationRequest.getContactInformationUpdateData().get(0); + ContactInformationUpdateRequest.ContactInformationUpdateData + contactInfoData1 = contactInformationCreationRequest.getContactInformationUpdateData().get(1); + assertThat(savedContactInfo.get("addressLine1")).isEqualTo(contactInfoData.getAddressLine1()); + assertThat(savedContactInfo.get("addressLine2")).isEqualTo(contactInfoData.getAddressLine2()); + assertThat(savedContactInfo.get("addressLine3")).isEqualTo(contactInfoData.getAddressLine3()); + assertThat(savedContactInfo.get("uprn")).isEqualTo(contactInfoData.getUprn()); + assertThat(savedContactInfo.get("county")).isEqualTo(contactInfoData.getCounty()); + assertThat(savedContactInfo.get("townCity")).isEqualTo(contactInfoData.getTownCity()); + assertThat(savedContactInfo.get("country")).isEqualTo(contactInfoData.getCountry()); + assertThat(savedContactInfo.get("postCode")).isEqualTo(contactInfoData.getPostCode()); + + assertThat(savedContactInfo1.get("addressLine1")).isEqualTo(contactInfoData1.getAddressLine1()); + assertThat(savedContactInfo1.get("addressLine2")).isEqualTo(contactInfoData1.getAddressLine2()); + assertThat(savedContactInfo1.get("addressLine3")).isEqualTo(contactInfoData1.getAddressLine3()); + assertThat(savedContactInfo1.get("uprn")).isEqualTo(contactInfoData1.getUprn()); + assertThat(savedContactInfo1.get("county")).isEqualTo(contactInfoData1.getCounty()); + assertThat(savedContactInfo1.get("townCity")).isEqualTo(contactInfoData1.getTownCity()); + assertThat(savedContactInfo1.get("country")).isEqualTo(contactInfoData1.getCountry()); + assertThat(savedContactInfo1.get("postCode")).isEqualTo(contactInfoData1.getPostCode()); + + //DxAddressCreationRequest assertion + List dxAddressesUpdated = (List)savedContactInfo.get("dxAddress"); + LinkedHashMap dxAdd = (LinkedHashMap)dxAddressesUpdated.get(1); + + assertThat(dxAdd.get("dxNumber").toString()).isEqualTo(contactInfoData + .getDxAddress().get(0).getDxNumber()); + assertThat(dxAdd.get("dxExchange").toString()).isEqualTo(contactInfoData + .getDxAddress().get(0).getDxExchange()); + + //DxAddressCreationRequest assertion + List dxAddressesUpdated1 = (List)savedContactInfo1.get("dxAddress"); + LinkedHashMap dxAdd1 = (LinkedHashMap)dxAddressesUpdated1.get(1); + + assertThat(dxAdd1.get("dxNumber").toString()).isEqualTo(contactInfoData1 + .getDxAddress().get(0).getDxNumber()); + assertThat(dxAdd1.get("dxExchange").toString()).isEqualTo(contactInfoData1 + .getDxAddress().get(0).getDxExchange()); + + log.info("updateBothContactInformationAndDxAdressForOrgNeedUpdatingSuccessScenario :: END"); + } + + /* @Test + void updateOnlyDxAddressDetailsForOrgShouldReturnSuccess() { + log.info("updateOnlyDxAddressDetailsForOrgShouldReturnSuccess :: STARTED"); + + Map response = professionalApiClient.createOrganisation(); + String organisationIdentifier = (String) response.get("organisationIdentifier"); + assertThat(organisationIdentifier).isNotEmpty(); + + OrganisationCreationRequest organisationCreationRequest = createOrganisationRequest().status("ACTIVE") + .build(); + //make organisation active + professionalApiClient.updateOrganisation(organisationCreationRequest, hmctsAdmin, organisationIdentifier,OK); + + JsonPath activeOrgResponse = professionalApiClient.retrieveOrganisationDetails( + organisationIdentifier, hmctsAdmin,OK); + assertNotNull(activeOrgResponse); + + List existingContactInformationList = activeOrgResponse.get("contactInformation"); + List contacts = activeOrgResponse.get("contactInformation"); + HashMap contact = (HashMap)contacts.get(0); + String addId = contact.get("addressId").toString(); + List dxAddresses = (List)contact.get("dxAddress"); + + //create request to update organisation + ContactInformationCreationRequest updatedContactInfoRequest = + professionalApiClient.createContactInformationWithDXAddUpdatedInfoRequests(); + assertThat(updatedContactInfoRequest).isNotNull(); + + Response result = professionalApiClient.updateContactInformationsToOrganisation( + updatedContactInfoRequest,OK); + + assertNotNull(result); + assertThat(result.statusCode()).isEqualTo(200); + + //retrieve saved contact info from organisation + JsonPath orgResponse = professionalApiClient.retrieveOrganisationDetails( + organisationIdentifier, hmctsAdmin,OK); + assertNotNull(orgResponse); + List savedContactInformationList = (List) orgResponse.get("contactInformation"); + + assertThat(savedContactInformationList).isNotEmpty().hasSize(1); + + log.info("updateOnlyDxAddressDetailsForOrgShouldReturnSuccess :: END"); + } + + @Test + void updateOnlyContactInformationForOrgShouldReturnSuccess() { + log.info("updateOnlyContactInformationForOrgShouldReturnSuccess :: STARTED"); + + Map response = professionalApiClient.createOrganisation(); + String organisationIdentifier = (String) response.get("organisationIdentifier"); + + assertThat(organisationIdentifier).isNotEmpty(); + JsonPath orgResponse = professionalApiClient.retrieveOrganisationDetails( + organisationIdentifier, hmctsAdmin,OK); + assertNotNull(orgResponse); + List existingContactInformationList = (List) orgResponse.get("contactInformation"); + + assertThat(existingContactInformationList).isNotEmpty() + .hasSizeGreaterThan(0); + + List contactInformationCreationRequestList = + professionalApiClient.createContactInformationCreationRequests(); + assertThat(contactInformationCreationRequestList).isNotEmpty() + .hasSizeGreaterThan(0); + ContactInformationCreationRequest contactInformationCreationRequest = + contactInformationCreationRequestList.get(0); + //Assuming only one record found + HashMap existingContactInfo = existingContactInformationList.get(0); + + assertThat(existingContactInfo.get("addressLine1")).isNotNull(); + assertThat(existingContactInfo.get("addressLine2")).isNotNull(); + assertThat(existingContactInfo.get("addressLine3")).isNotNull(); + assertThat(existingContactInfo.get("uprn")).isNotNull(); + assertThat(existingContactInfo.get("country")).isNotNull(); + assertThat(existingContactInfo.get("townCity")).isNotNull(); + assertThat(existingContactInfo.get("country")).isNotNull(); + assertThat(existingContactInfo.get("postCode")).isNotNull(); + + Response result = professionalApiClient.updateContactInformationsToOrganisation( + aContactInformationCreationRequest() + .uprn(contactInformationCreationRequest.getUprn()) + .addressLine1(contactInformationCreationRequest.getAddressLine1()) + .addressLine2(contactInformationCreationRequest.getAddressLine2()) + .addressLine3(contactInformationCreationRequest.getAddressLine3()) + .country(contactInformationCreationRequest.getCountry()) + .county(contactInformationCreationRequest.getCounty()) + .townCity(contactInformationCreationRequest.getTownCity()) + .postCode(contactInformationCreationRequest.getPostCode()) + .build(), + OK,organisationIdentifier,false,true, + existingContactInfo.get("addressId").toString()); + + assertNotNull(result); + assertThat(result.statusCode()).isEqualTo(200); + + log.info("updateOnlyContactInformationForOrgShouldReturnSuccess :: END"); + } + + @Test + void updateContactInformationAndDxAddressWithNoChangeInDataShouldReturnSuccess() { + log.info("updateContactInformationAndDxAddressWithNoChangeInDataShouldReturnSuccess :: STARTED"); + + Map response = professionalApiClient.createOrganisation(); + String organisationIdentifier = (String) response.get("organisationIdentifier"); + + assertThat(organisationIdentifier).isNotEmpty(); + JsonPath orgResponse = professionalApiClient.retrieveOrganisationDetails( + organisationIdentifier, hmctsAdmin,OK); + assertNotNull(orgResponse); + List existingContactInformationList = (List) orgResponse.get("contactInformation"); + + assertThat(existingContactInformationList).isNotEmpty() + .hasSizeGreaterThan(0); + + List contactInformationCreationRequestList = + professionalApiClient.createContactInformationCreationRequests(); + assertThat(contactInformationCreationRequestList).isNotEmpty() + .hasSizeGreaterThan(0); + ContactInformationCreationRequest contactInformationCreationRequest = + contactInformationCreationRequestList.get(0); + //Assuming only one record found + HashMap existingContactInfo = existingContactInformationList.get(0); + + assertThat(existingContactInfo.get("addressLine1")).isNotNull(); + assertThat(existingContactInfo.get("addressLine2")).isNotNull(); + assertThat(existingContactInfo.get("addressLine3")).isNotNull(); + assertThat(existingContactInfo.get("uprn")).isNotNull(); + assertThat(existingContactInfo.get("country")).isNotNull(); + assertThat(existingContactInfo.get("townCity")).isNotNull(); + assertThat(existingContactInfo.get("country")).isNotNull(); + assertThat(existingContactInfo.get("postCode")).isNotNull(); + + Response result = professionalApiClient.updateContactInformationsToOrganisation( + aContactInformationCreationRequest() + .uprn(contactInformationCreationRequest.getUprn()) + .addressLine1(contactInformationCreationRequest.getAddressLine1()) + .addressLine2(contactInformationCreationRequest.getAddressLine2()) + .addressLine3(contactInformationCreationRequest.getAddressLine3()) + .country(contactInformationCreationRequest.getCountry()) + .county(contactInformationCreationRequest.getCounty()) + .townCity(contactInformationCreationRequest.getTownCity()) + .postCode(contactInformationCreationRequest.getPostCode()) + .dxAddress(contactInformationCreationRequest.getDxAddress()) + .build(), + OK,organisationIdentifier,true,true, + existingContactInfo.get("addressId").toString()); + + assertNotNull(result); + assertThat(result.statusCode()).isEqualTo(200); + + log.info("updateContactInformationAndDxAddressWithNoChangeInDataShouldReturnSuccess :: END"); + } + + @Test + void contactInformationAndDxAddressBothFalseShouldReturnFailure() { + log.info("updateContactInformationAndDxAddressWithNoChangeInDataShouldReturnSuccess :: STARTED"); + + Map response = professionalApiClient.createOrganisation(); + String organisationIdentifier = (String) response.get("organisationIdentifier"); + + assertThat(organisationIdentifier).isNotEmpty(); + JsonPath orgResponse = professionalApiClient.retrieveOrganisationDetails( + organisationIdentifier, hmctsAdmin,OK); + assertNotNull(orgResponse); + List existingContactInformationList = (List) orgResponse.get("contactInformation"); + + assertThat(existingContactInformationList).isNotEmpty() + .hasSizeGreaterThan(0); + + List contactInformationCreationRequestList = + professionalApiClient.createContactInformationCreationRequests(); + assertThat(contactInformationCreationRequestList).isNotEmpty() + .hasSizeGreaterThan(0); + ContactInformationCreationRequest contactInformationCreationRequest = + contactInformationCreationRequestList.get(0); + //Assuming only one record found + HashMap existingContactInfo = existingContactInformationList.get(0); + + Response result = professionalApiClient.updateContactInformationsToOrganisation( + aContactInformationCreationRequest().build(),BAD_REQUEST,organisationIdentifier,false, + false,existingContactInfo.get("addressId").toString()); + + assertNotNull(result); + assertThat(result.getBody()).isNotNull(); + assertThat(result.statusCode()).isEqualTo(400); + + log.info("updateContactInformationAndDxAddressWithNoChangeInDataShouldReturnSuccess :: END"); + } +*/ + private String createActiveOrganisation() { + Map response = professionalApiClient.createOrganisation(); + String organisationIdentifier = (String) response.get("organisationIdentifier"); + assertThat(organisationIdentifier).isNotEmpty(); + OrganisationCreationRequest organisationCreationRequest = createOrganisationRequest().status("ACTIVE").build(); + professionalApiClient.updateOrganisation(organisationCreationRequest, hmctsAdmin, organisationIdentifier,OK); + return organisationIdentifier; + } + + public void deleteCreatedTestOrganisations(String orgId1, String orgId2) { + professionalApiClient.deleteOrganisation(orgId1, hmctsAdmin, NO_CONTENT);; + professionalApiClient.deleteOrganisation(orgId2, hmctsAdmin, NO_CONTENT); + } + + public ContactInformationUpdateRequest createContactInformationUpdateRequestWithDxAddress(String orgId1, + String addressLine1, + String addressLine3, + String uprn1, + String orgId2, + String addLine1, + String addLine3, + String uprn2, + Boolean dxAdd, + Boolean dxAdd1, + Boolean contactUpdate, + Boolean contactUpdate1, + String addId, + String addId1) { + ContactInformationUpdateRequest contactInformationUpdateRequest = new ContactInformationUpdateRequest(); + List contactInformationUpdateDataList + = new ArrayList<>(); + contactInformationUpdateDataList.add(new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId1, dxAdd, contactUpdate, addId,uprn1,addressLine1, + "addressLine2",addressLine3, "som1-town-city", + "some-county1","some-country1","som1-post-code", Arrays.asList( + dxAddressUpdateRequest().dxNumber("DX 1234567890").dxExchange("dxExchange-1").build()))); + contactInformationUpdateDataList.add(new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId2,dxAdd1, contactUpdate1, addId1,uprn2,addLine1, "addLine2",addLine3, + "som2-town-city","some-county2","some-country2","som2-post-code", + Arrays.asList(dxAddressUpdateRequest().dxAddressId("12345678").dxNumber("DX 2234567890").dxExchange( + "dxExchange-2").build()))); + contactInformationUpdateRequest.setContactInformationUpdateData(contactInformationUpdateDataList); + + return contactInformationUpdateRequest; + } + + public void verifyRetrievedOrg(String orgId,String sraId) { + + var orgResponse = professionalApiClient.retrieveOrganisationDetailsForV2(orgId, hmctsAdmin, OK); + assertThat(orgResponse).isNotNull(); + List organisationAttributes = (List)orgResponse.get("orgAttributes"); + assertThat(organisationAttributes).isNotNull(); + LinkedHashMap attr = (LinkedHashMap)organisationAttributes.get(0); + assertThat(attr).isNotNull(); + assertThat(attr.get("key")).isEqualTo("regulators-0"); + assertThat(attr.get("value").toString()).isEqualTo( + "{\"regulatorType\":\"Solicitor Regulation Authority " + + "(SRA)\",\"organisationRegistrationNumber\":\"" + sraId + "\"}"); + + final Object sraIdSaved = orgResponse.get("sraId"); + assertThat(sraIdSaved).isNotNull().isEqualTo(sraId); + LocalDateTime updatedDate = LocalDateTime.parse(orgResponse.get("lastUpdated").toString()); + assertThat(updatedDate.toLocalDate()).isEqualTo(LocalDate.now()); + } + } diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/professionalapi/client/ProfessionalApiClient.java b/src/functionalTest/java/uk/gov/hmcts/reform/professionalapi/client/ProfessionalApiClient.java index f9801c26d2..e1f4da8199 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/professionalapi/client/ProfessionalApiClient.java +++ b/src/functionalTest/java/uk/gov/hmcts/reform/professionalapi/client/ProfessionalApiClient.java @@ -14,6 +14,7 @@ import org.springframework.http.HttpStatus; import uk.gov.hmcts.reform.professionalapi.controller.advice.ErrorResponse; import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.DeleteMultipleAddressRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressCreationRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.MfaUpdateRequest; @@ -260,6 +261,77 @@ public static OrganisationOtherOrgsCreationRequest createOrganisationRequestForV return organisationOtherOrgsCreationRequest; } + public static List createContactInformationRequests() { + + List dx1 = new LinkedList<>(); + dx1.add(dxAddressCreationRequest() + .dxNumber("DX 1234567890") + .dxExchange("dxExchange").build()); + dx1.add(dxAddressCreationRequest() + .dxNumber("DX 123456777") + .dxExchange("dxExchange").build()); + List dx2 = new LinkedList<>(); + dx2.add(dxAddressCreationRequest() + .dxNumber("DX 123452222") + .dxExchange("dxExchange").build()); + dx2.add(dxAddressCreationRequest() + .dxNumber("DX 123456333") + .dxExchange("dxExchange").build()); + + List contactInfoList = new LinkedList<>(); + contactInfoList.add(aContactInformationCreationRequest() + .uprn("u1") + .addressLine1("address1") + .addressLine2("address2") + .addressLine3("address3") + .country("country") + .county("county") + .townCity("city") + .postCode("code") + .dxAddress(dx1) + .build()); + contactInfoList.add(aContactInformationCreationRequest() + .uprn("up2") + .addressLine1("add") + .addressLine2("add2") + .addressLine3("add3") + .country("country2") + .county("county2") + .townCity("city2") + .postCode("code2") + .dxAddress(dx2) + .build()); + + return contactInfoList; + } + + public ContactInformationCreationRequest createContactInformationWithDXAddUpdatedInfoRequests() { + + List dx1 = new LinkedList<>(); + dx1.add(dxAddressCreationRequest() + .dxNumber("DX1234567890") + .dxExchange("dxExchange").build()); + dx1.add(dxAddressCreationRequest() + .dxNumber("DX123456777") + .dxExchange("dxExchange1").build()); + + + ContactInformationCreationRequest updatedContactInfoRequest = + aContactInformationCreationRequest() + .uprn("uuprn") + .addressLine1("updatedaddressLine1") + .addressLine2("updatedaddressLine2") + .addressLine3("updatedaddressLine3") + .country("updatedcountry") + .county("updatedcounty") + .townCity("updatedtowncity") + .postCode("uppostcode") + .dxAddress(dx1) + .build(); + + return updatedContactInfoRequest; + } + public static List createContactInformationCreationRequests() { Set paymentAccounts = new HashSet<>(); paymentAccounts.add("PBA" + randomAlphabetic(7)); @@ -1752,4 +1824,26 @@ public void deleteMultipleAddressesOfOrganisation(List updateResponse = professionalReferenceDataClient + .updateOrgContactInformation(contactInformationCreationRequest, hmctsAdmin); + + assertThat(updateResponse).containsEntry("http_status", 200); + LinkedHashMap responses = (LinkedHashMap)updateResponse.get("response_body"); + assertThat(responses.get("status")).isEqualTo("success"); + assertThat(responses.get("message")).isEqualTo("All contactInformations updated successfully"); + + LinkedHashMap savedContactInformation1 = retrieveContacts(orgId1); + LinkedHashMap savedContactInformation2 = retrieveContacts(orgId2); + verifyContactInfo(savedContactInformation1, + contactInformationCreationRequest.getContactInformationUpdateData().get(0)); + verifyDxAddress(savedContactInformation1, + contactInformationCreationRequest.getContactInformationUpdateData().get(0)); + + verifyContactInfo(savedContactInformation2, + contactInformationCreationRequest.getContactInformationUpdateData().get(1)); + verifyDxAddress(savedContactInformation2, + contactInformationCreationRequest.getContactInformationUpdateData().get(1)); + + deleteCreatedTestOrganisations(orgId1, orgId2); + } + + + @Test + void shouldReturn400WhenContactInformationRequestIsNull() { + Map updateResponse = professionalReferenceDataClient + .updateOrgContactInformation(null,hmctsAdmin); + + assertThat(updateResponse).containsEntry("http_status", "400"); + assertThat(updateResponse.get("response_body").toString()).contains("Required request body is missing"); + } + + @Test + @Description("Multiple Requests - 1st req has missing add line 1 - 2nd req has invalid uprn") + void shouldReturn400WhenAddressLine1IsNullAndUprnInvalid() { + String orgId1 = createActiveOrganisationWithSingleContact(); + String orgId2 = createActiveOrganisationWithSingleContact(); + + ContactInformationUpdateRequest contactInformationCreationRequest = + createContactInformationUpdateRequestWithDxAddress(orgId1,"", + "addressLine3","uprn1",orgId2,null,"addLine3", + randomAlphabetic(17),true,true,true, + true,null,null); + + Map updateResponse = professionalReferenceDataClient + .updateOrgContactInformation(contactInformationCreationRequest, hmctsAdmin); + LinkedHashMap responses = (LinkedHashMap)updateResponse.get("response_body"); + assertThat(responses.get("status")).isEqualTo("failure"); + ArrayList responseList = (ArrayList)responses.get("responses"); + LinkedHashMap firstResult = (LinkedHashMap)responseList.get(0); + LinkedHashMap secondResult = (LinkedHashMap)responseList.get(1); + LinkedHashMap thirdResult = (LinkedHashMap)responseList.get(2); + assertThat(firstResult.get("organisationId")).isEqualTo(orgId1); + assertThat(firstResult.get("status")).isEqualTo("failure"); + assertThat(firstResult.get("statusCode")).isEqualTo(400); + assertThat(firstResult.get("message")).isEqualTo("AddressLine1 cannot be empty"); + + assertThat(secondResult.get("organisationId")).isEqualTo(orgId2); + assertThat(secondResult.get("status")).isEqualTo("failure"); + assertThat(secondResult.get("statusCode")).isEqualTo(400); + assertThat(secondResult.get("message")).isEqualTo( + "AddressLine1 cannot be empty"); + + assertThat(thirdResult.get("organisationId")).isEqualTo(orgId2); + assertThat(thirdResult.get("status")).isEqualTo("failure"); + assertThat(thirdResult.get("statusCode")).isEqualTo(400); + assertThat(thirdResult.get("message")).isEqualTo( + "Uprn must not be greater than 14 characters long"); + + deleteCreatedTestOrganisations(orgId1, orgId2); + } + + @Test + @Description("Multiple Requests - 1st req has missing orgid - 2nd req has nonexisting orgid") + void shouldReturn400ForNonExistingOrganisation() { + String orgId2 = "ABCDEF7"; + + ContactInformationUpdateRequest contactInformationCreationRequest = + createContactInformationUpdateRequestWithDxAddress(null,"", + "addressLine3","uprn1",orgId2,null,"addLine3", + randomAlphabetic(17),true,true,true, + true,null,null); + + Map updateResponse = professionalReferenceDataClient + .updateOrgContactInformation(contactInformationCreationRequest, hmctsAdmin); + LinkedHashMap responses = (LinkedHashMap)updateResponse.get("response_body"); + assertThat(responses.get("status")).isEqualTo("failure"); + + ArrayList responseList = (ArrayList)responses.get("responses"); + LinkedHashMap firstResult = (LinkedHashMap)responseList.get(0); + LinkedHashMap secondResult = (LinkedHashMap)responseList.get(1); + LinkedHashMap thirdResult = (LinkedHashMap)responseList.get(2); + LinkedHashMap fourthResult = (LinkedHashMap)responseList.get(3); + + assertThat(firstResult.get("status")).isEqualTo("failure"); + assertThat(firstResult.get("statusCode")).isEqualTo(400); + assertThat(firstResult.get("message")).isEqualTo("Organisation id is missing"); + + assertThat(secondResult.get("organisationId")).isEqualTo(orgId2); + assertThat(secondResult.get("status")).isEqualTo("failure"); + assertThat(secondResult.get("statusCode")).isEqualTo(400); + assertThat(secondResult.get("message")).isEqualTo( + "No Organisation was found with the given organisationIdentifier"); + + assertThat(thirdResult.get("organisationId")).isEqualTo(orgId2); + assertThat(thirdResult.get("status")).isEqualTo("failure"); + assertThat(thirdResult.get("statusCode")).isEqualTo(400); + assertThat(thirdResult.get("message")).isEqualTo( + "AddressLine1 cannot be empty"); + + assertThat(fourthResult.get("organisationId")).isEqualTo(orgId2); + assertThat(fourthResult.get("status")).isEqualTo("failure"); + assertThat(fourthResult.get("statusCode")).isEqualTo(400); + assertThat(fourthResult.get("message")).isEqualTo( + "Uprn must not be greater than 14 characters long"); + + } + + + @Test + @Description("Multiple Requests - Partial success -1st Dx add missing ,2nd DxAdd invalid , 3rd Success") + void shouldReturn200WhenUpdateMissing_InvalidAndSuccessfullySavedDxAddress() { + + String orgId1 = createActiveOrganisationWithSingleContact(); + String orgId2 = createActiveOrganisationWithSingleContact(); + String orgId3 = createActiveOrganisationWithSingleContact(); + String orgId4 = createActiveOrganisationWithMultipleContact(); + + + List contactInformationUpdateDataList + = new ArrayList<>(); + contactInformationUpdateDataList.add( + new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId1,true, false,"","uprn1", + "addressLine1","addressLine2","addressLine3", + "som1-town-city","some-county1","some-country1", + "som1-post-code",null)); + contactInformationUpdateDataList.add(new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId2,true, false, "addId","uprn2", + "addLine1","addLine2","addLine3", "som2-town-city", + "some-county2","some-country2","som2-post-code",Arrays.asList( + dxAddressUpdateRequest().dxAddressId("12345678").dxNumber(randomAlphabetic(17)) + .dxExchange(randomAlphabetic( + 17)).build()))); + + ContactInformationUpdateRequest contactInformationUpdateRequest = new ContactInformationUpdateRequest(); + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData = + new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId3,true, false, "addId",null,null, + null,null,null,null, + null,null,Arrays.asList(dxAddressUpdateRequest().dxAddressId("12345678") + .dxNumber("DX 1234567890").dxExchange("dxExchange").build())); + contactInformationUpdateDataList.add(contactInformationUpdateData); + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData1 = + new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId4,true, false, "",null,null, + null,null,null,null, + null,null,Arrays.asList(dxAddressUpdateRequest().dxAddressId("12345678") + .dxNumber("DX 2234567890").dxExchange("dxChanged").build())); + contactInformationUpdateDataList.add(contactInformationUpdateData1); + contactInformationUpdateRequest.setContactInformationUpdateData(contactInformationUpdateDataList); + + Map updateResponse = professionalReferenceDataClient + .updateOrgContactInformation(contactInformationUpdateRequest,hmctsAdmin); + + LinkedHashMap responses = (LinkedHashMap)updateResponse.get("response_body"); + assertThat(responses.get("status")).isEqualTo("partial_success"); + + ArrayList responseList = (ArrayList)responses.get("responses"); + LinkedHashMap firstResult = (LinkedHashMap)responseList.get(0); + assertThat(firstResult.get("status")).isEqualTo("failure"); + assertThat(firstResult.get("statusCode")).isEqualTo(400); + assertThat(firstResult.get("message")).isEqualTo("DX Number or DX Exchange cannot be empty"); + + LinkedHashMap secondResult = (LinkedHashMap)responseList.get(1); + assertThat(secondResult.get("organisationId")).isEqualTo(orgId2); + assertThat(secondResult.get("status")).isEqualTo("failure"); + assertThat(secondResult.get("statusCode")).isEqualTo(400); + assertThat(secondResult.get("message")).isEqualTo( + "DX Number (max=13) or DX Exchange (max=40) has invalid length"); + + LinkedHashMap thirdResult = (LinkedHashMap)responseList.get(2); + assertThat(thirdResult.get("organisationId")).isEqualTo(orgId3); + assertThat(thirdResult.get("status")).isEqualTo("success"); + assertThat(thirdResult.get("statusCode")).isEqualTo(200); + assertThat(thirdResult.get("message")).isEqualTo( + "dxAddress updated successfully"); + LinkedHashMap savedContactInformation = retrieveContacts(orgId3); + verifyDxAddress(savedContactInformation,contactInformationUpdateData); + + LinkedHashMap fourthResult = (LinkedHashMap)responseList.get(3); + assertThat(fourthResult.get("status")).isEqualTo("failure"); + assertThat(fourthResult.get("organisationId")).isEqualTo(orgId4); + assertThat(fourthResult.get("statusCode")).isEqualTo(400); + assertThat(fourthResult.get("message")).isEqualTo("Multiple addresses found for organisation . " + + "Please enter specific address id of the contact information to be updated"); + + deleteCreatedTestOrganisations(orgId1, orgId2); + deleteCreatedTestOrganisations(orgId3, orgId4); + } + + @Test + @Description("Multiple existing contacts for each req found but addressid is missing and dxadd and contact info" + + " are both false") + void shouldReturn400WhenMultipleContactsMissingAddId() { + + String orgId1 = createActiveOrganisationWithMultipleContact(); + String orgId2 = createActiveOrganisationWithSingleContact(); + + ContactInformationUpdateRequest contactInformationCreationRequest = + createContactInformationUpdateRequestWithDxAddress(orgId1,"addressLine1", + "addressLine3","uprn1",orgId2,null,"addLine3", + randomAlphabetic(17),false,false,false,false, + null,null); + + Map updateResponse = professionalReferenceDataClient + .updateOrgContactInformation(contactInformationCreationRequest, hmctsAdmin); + LinkedHashMap responses = (LinkedHashMap)updateResponse.get("response_body"); + assertThat(responses.get("status")).isEqualTo("failure"); + ArrayList responseList = (ArrayList)responses.get("responses"); + LinkedHashMap firstResult = (LinkedHashMap)responseList.get(0); + assertThat(firstResult.get("status")).isEqualTo("failure"); + assertThat(firstResult.get("organisationId")).isEqualTo(orgId1); + assertThat(firstResult.get("statusCode")).isEqualTo(400); + assertThat(firstResult.get("message")).isEqualTo("Multiple addresses found for organisation . " + + "Please enter specific address id of the contact information to be updated"); + LinkedHashMap secondResult = (LinkedHashMap)responseList.get(1); + assertThat(secondResult.get("status")).isEqualTo("failure"); + assertThat(secondResult.get("organisationId")).isEqualTo(orgId2); + assertThat(secondResult.get("statusCode")).isEqualTo(400); + assertThat(secondResult.get("message")).isEqualTo("dxAddressUpdate and " + + "contactInformationUpdate are both false . Cannot update contact information"); + + deleteCreatedTestOrganisations(orgId1, orgId2); + + } + + + @Test + @Description("Multiple Requests - 1st success only ContactInfo saved - 2nd fail AddLine1 missing") + void shouldReturn200PartialSuccessWhenUpdateSingleAddressWithoutDxAddressChangeAndInActiveOrgId() { + String orgId1 = createActiveOrganisationWithSingleContact(); + + ContactInformationUpdateRequest contactInformationCreationRequest = + createContactInformationUpdateRequestWithOutDxAddress(orgId1,"addressLine1", + "addressLine3","uprn1","KRGPTFT",null,"addLine3", + "uprn2",false,false,true,true, + null,null); + + Map updateResponse = professionalReferenceDataClient + .updateOrgContactInformation(contactInformationCreationRequest,hmctsAdmin); + + + LinkedHashMap responses = (LinkedHashMap)updateResponse.get("response_body"); + assertThat(responses.get("status")).isEqualTo("partial_success"); + + ArrayList responseList = (ArrayList)responses.get("responses"); + LinkedHashMap firstResult = (LinkedHashMap)responseList.get(0); + assertThat(firstResult.get("organisationId")).isEqualTo(orgId1); + assertThat(firstResult.get("status")).isEqualTo("success"); + assertThat(firstResult.get("statusCode")).isEqualTo(200); + assertThat(firstResult.get("message")).isEqualTo("contactInformation updated successfully"); + + LinkedHashMap savedContactInformation1 = retrieveContacts(orgId1); + + verifyContactInfo(savedContactInformation1, + contactInformationCreationRequest.getContactInformationUpdateData().get(0)); + + LinkedHashMap secondResult = (LinkedHashMap)responseList.get(1); + assertThat(secondResult.get("organisationId")).isEqualTo("KRGPTFT"); + assertThat(secondResult.get("status")).isEqualTo("failure"); + assertThat(secondResult.get("statusCode")).isEqualTo(400); + assertThat(secondResult.get("message")).isEqualTo("No Organisation was found with the given " + + "organisationIdentifier"); + + LinkedHashMap thirdResult = (LinkedHashMap)responseList.get(2); + assertThat(thirdResult.get("organisationId")).isEqualTo("KRGPTFT"); + assertThat(thirdResult.get("status")).isEqualTo("failure"); + assertThat(thirdResult.get("statusCode")).isEqualTo(400); + assertThat(thirdResult.get("message")).isEqualTo("AddressLine1 cannot be empty"); + + professionalReferenceDataClient.deleteOrganisation(hmctsAdmin,orgId1); + } + + + + + @Test + void shoudlReturn200ForOnlyDxNumberChangeOrOnlyDxExchange() { + String orgId1 = createActiveOrganisationWithSingleContact(); + String orgId2 = createActiveOrganisationWithSingleContact(); + + List dx1 = new LinkedList<>(); + dx1.add(dxAddressUpdateRequest().dxAddressId("12345678") + .dxNumber("DE1234567890") + .dxExchange("deExchange").build()); + dx1.add(dxAddressUpdateRequest().dxAddressId("123456788") + .dxNumber("DM123456777") + .dxExchange("dmExchange1").build()); + + List dx2 = new LinkedList<>(); + dx1.add(dxAddressUpdateRequest().dxAddressId("123456789") + .dxNumber("DP1234567890") + .dxExchange("dpExchange").build()); + dx1.add(dxAddressUpdateRequest().dxAddressId("1234567899") + .dxNumber("DL123456777") + .dxExchange("dlExchange1").build()); + + ContactInformationUpdateRequest contactInformationUpdateRequest = new ContactInformationUpdateRequest(); + List contactInformationUpdateDataList + = new ArrayList<>(); + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData = + new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId1,true, false, null,null,null, + null,null,null,null, + null,null,dx1); + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData1 = + new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId2, true, false, null,null,null, + null,null,null,null, + null,null,dx2); + contactInformationUpdateDataList.add(contactInformationUpdateData); + contactInformationUpdateDataList.add(contactInformationUpdateData1); + contactInformationUpdateRequest.setContactInformationUpdateData(contactInformationUpdateDataList); + + Map updateResponse = professionalReferenceDataClient + .updateOrgContactInformation(contactInformationUpdateRequest,hmctsAdmin + ); + + assertThat(updateResponse).containsEntry("http_status", 200); + LinkedHashMap responsesList = (LinkedHashMap)updateResponse.get("response_body"); + assertThat(responsesList.get("status")).isEqualTo("success"); + assertThat(responsesList.get("message")).isEqualTo("All contactInformations updated successfully"); + + LinkedHashMap savedContactInformation1 = retrieveContacts(orgId1); + LinkedHashMap savedContactInformation2 = retrieveContacts(orgId2); + + verifyDxAddress(savedContactInformation1,contactInformationUpdateData); + verifyDxAddress(savedContactInformation2,contactInformationUpdateData1); + + deleteCreatedTestOrganisations(orgId1, orgId2); + + } + + + private String createActiveOrganisationWithMultipleContact() { + OrganisationCreationRequest organisationCreationRequest2 = organisationRequestWithMultipleContactInformations() + .build(); + String organisationIdentifier = createAndActivateOrganisationWithGivenRequest(organisationCreationRequest2); + updateOrganisation(organisationIdentifier, hmctsAdmin, "ACTIVE"); + + return organisationIdentifier; + } + + private String createActiveOrganisationWithSingleContact() { + OrganisationCreationRequest organisationCreationRequest = someMinimalOrganisationRequest().build(); + String organisationIdentifier = createAndActivateOrganisationWithGivenRequest(organisationCreationRequest); + updateOrganisation(organisationIdentifier, hmctsAdmin, "ACTIVE"); + + return organisationIdentifier; + } + + + + public ContactInformationUpdateRequest createContactInformationUpdateRequestWithDxAddress(String orgId1, + String addressLine1, + String addressLine3, + String uprn1, + String orgId2, + String addLine1, + String addLine3, + String uprn2, + Boolean dxAdd, + Boolean dxAdd1, + Boolean contactUpdate, + Boolean contactUpdate1, + String addId, + String addId1) { + ContactInformationUpdateRequest contactInformationUpdateRequest = new ContactInformationUpdateRequest(); + List contactInformationUpdateDataList + = new ArrayList<>(); + contactInformationUpdateDataList.add(new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId1, dxAdd, contactUpdate, addId,uprn1,addressLine1, + "addressLine2",addressLine3, "som1-town-city", + "some-county1","some-country1","som1-post-code", Arrays.asList( + dxAddressUpdateRequest().dxAddressId("12345678").dxNumber("DX 1234567890") + .dxExchange("dxExchange-1").build()))); + contactInformationUpdateDataList.add(new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId2,dxAdd1, contactUpdate1, addId1,uprn2,addLine1, + "addLine2",addLine3, "som2-town-city", + "some-county2","some-country2","som2-post-code",Arrays.asList( + dxAddressUpdateRequest().dxAddressId("123456789").dxNumber("DX 2234567890") + .dxExchange("dxExchange-2").build()))); + contactInformationUpdateRequest.setContactInformationUpdateData(contactInformationUpdateDataList); + + return contactInformationUpdateRequest; + } + + public ContactInformationUpdateRequest createContactInformationUpdateRequestWithOutDxAddress(String orgId1, + String addressLine1, + String addressLine3, + String uprn1, + String orgId2, + String addLine1, + String addLine3, + String uprn2, + Boolean dxAdd, + Boolean dxAdd1, + Boolean contactUpdate, + Boolean + contactUpdate1, + String addId, + String addId1) { + ContactInformationUpdateRequest contactInformationUpdateRequest = new ContactInformationUpdateRequest(); + List contactInformationUpdateDataList + = new ArrayList<>(); + contactInformationUpdateDataList.add( + new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId1, dxAdd, contactUpdate, addId, uprn1,addressLine1, + "addressLine2",addressLine3, "som1-town-city", + "some-county1","some-country1","som1-post-code",null)); + contactInformationUpdateDataList.add( + new ContactInformationUpdateRequest.ContactInformationUpdateData( + orgId2, dxAdd1, contactUpdate1, addId1, uprn2,addLine1, + "addLine2",addLine3, "som2-town-city", + "some-county2","some-country2","som2-post-code",null)); + + contactInformationUpdateRequest.setContactInformationUpdateData(contactInformationUpdateDataList); + return contactInformationUpdateRequest; + } + + public LinkedHashMap retrieveContacts(String organisationIdentifier) { + java.util.Map retrieveOrganisationResponse = professionalReferenceDataClient + .retrieveSingleOrganisation(organisationIdentifier, hmctsAdmin); + + List savedContacts = (List)retrieveOrganisationResponse.get("contactInformation"); + LocalDateTime updatedDate = LocalDateTime.parse(retrieveOrganisationResponse.get("lastUpdated").toString()); + assertThat(updatedDate.toLocalDate()).isEqualTo(LocalDate.now()); + return (LinkedHashMap)savedContacts.get(0); + + } + + public void verifyContactInfo(LinkedHashMap savedContactInformation, + ContactInformationUpdateRequest.ContactInformationUpdateData + contactInformationUpdateData) { + assertThat(savedContactInformation.get("addressLine1").toString()) + .isEqualTo(contactInformationUpdateData.getAddressLine1()); + assertThat(savedContactInformation.get("addressLine2").toString()) + .isEqualTo(contactInformationUpdateData.getAddressLine2()); + assertThat(savedContactInformation.get("addressLine3").toString()) + .isEqualTo(contactInformationUpdateData.getAddressLine3()); + assertThat(savedContactInformation.get("country").toString()) + .isEqualTo(contactInformationUpdateData.getCountry()); + assertThat(savedContactInformation.get("postCode").toString()) + .isEqualTo(contactInformationUpdateData.getPostCode()); + + } + + public void verifyDxAddress(LinkedHashMap savedContactInformation,ContactInformationUpdateRequest + .ContactInformationUpdateData contactInformationUpdateData) { + List dxAdd = (List)savedContactInformation.get("dxAddress"); + LinkedHashMap dxAddress = (LinkedHashMap)dxAdd.get(0); + + assertThat((String)dxAddress.get("dxExchange")) + .isEqualTo(contactInformationUpdateData.getDxAddress().get(0).getDxExchange()); + assertThat((String)dxAddress.get("dxNumber")) + .isEqualTo(contactInformationUpdateData.getDxAddress().get(0).getDxNumber()); + + } + + public void deleteCreatedTestOrganisations(String orgId1, String orgId2) { + professionalReferenceDataClient.deleteOrganisation(hmctsAdmin,orgId1); + professionalReferenceDataClient.deleteOrganisation(hmctsAdmin,orgId2); + } + + +} diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/professionalapi/util/ProfessionalReferenceDataClient.java b/src/integrationTest/java/uk/gov/hmcts/reform/professionalapi/util/ProfessionalReferenceDataClient.java index cc701b95c8..2783fb5bce 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/professionalapi/util/ProfessionalReferenceDataClient.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/professionalapi/util/ProfessionalReferenceDataClient.java @@ -21,6 +21,7 @@ import uk.gov.hmcts.reform.professionalapi.controller.advice.ErrorResponse; import uk.gov.hmcts.reform.professionalapi.controller.request.BulkCustomerRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.DeleteMultipleAddressRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.MfaUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.NewUserCreationRequest; @@ -1044,4 +1045,29 @@ public Map retrieveUsersInOrganisationsByOrganisationIdentifiers String uriPath = sb.toString(); return postRequest(uriPath, request, null, null); } + + public Map updateOrgContactInformation( + ContactInformationUpdateRequest contactInformationUpdateRequest, + String role) { + + ResponseEntity responseEntity = null; + String urlPath = "http://localhost:" + prdApiPort + APP_INT_BASE_PATH + "/contactInformation"; + + try { + HttpEntity requestEntity = + new HttpEntity<>(contactInformationUpdateRequest, + getMultipleAuthHeaders(role)); + responseEntity = restTemplate.exchange(urlPath, HttpMethod.PUT, requestEntity, Map.class); + } catch (RestClientResponseException ex) { + HashMap statusAndBody = new HashMap<>(2); + statusAndBody.put("http_status", String.valueOf(ex.getRawStatusCode())); + statusAndBody.put("response_body", ex.getResponseBodyAsString()); + return statusAndBody; + } + + Map contactInformationResponse = new HashMap<>(); + contactInformationResponse.put("http_status", responseEntity.getStatusCodeValue()); + contactInformationResponse.put("response_body", responseEntity.getBody()); + return contactInformationResponse; + } } diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/constants/ProfessionalApiConstants.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/constants/ProfessionalApiConstants.java index 8ad52a839c..6f38cbe2b8 100644 --- a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/constants/ProfessionalApiConstants.java +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/constants/ProfessionalApiConstants.java @@ -10,6 +10,9 @@ private ProfessionalApiConstants() { public static final String EMAIL_REGEX = "^[A-Za-z0-9]+[\\w!#$%&'’.*+/=?`{|}~^-]+(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*@" + "[A-Za-z0-9]+(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; + public static final String NO_CONTACT_FOUND_FOR_GIVEN_ORG + = "No Contact Information was found with the given organisationIdentifier "; + public static final int LENGTH_OF_UUID = 36; public static final int LENGTH_OF_ORGANISATION_IDENTIFIER = 7; public static final String ORG_ID_VALIDATION_ERROR_MESSAGE @@ -19,9 +22,12 @@ private ProfessionalApiConstants() { public static final String ALPHA_NUMERIC_WITH_SPECIAL_CHAR_REGEX = "[^A-Za-z0-9-]"; public static final String NO_ORG_FOUND_FOR_GIVEN_ID - = "No Organisation was found with the given organisationIdentifier "; + = "No Organisation was found with the given organisationIdentifier"; public static final String ORG_NOT_ACTIVE_NO_USERS_RETURNED = "Organisation is not Active hence not returning any users"; + + public static final String DX_ADDRESS_NOT_FOUND + = "DX Number or DX Exchange cannot be empty"; public static final String ERROR_MESSAGE_403_FORBIDDEN = "403 Forbidden"; public static final int ZERO_INDEX = 0; public static final int ONE = 1; diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/internal/OrganisationInternalController.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/internal/OrganisationInternalController.java index 0eb4d8b934..cb69026d77 100644 --- a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/internal/OrganisationInternalController.java +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/internal/OrganisationInternalController.java @@ -12,6 +12,7 @@ import jakarta.validation.constraints.Pattern; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.EmptyResultDataAccessException; @@ -33,6 +34,7 @@ import uk.gov.hmcts.reform.professionalapi.configuration.resolver.UserId; import uk.gov.hmcts.reform.professionalapi.controller.SuperController; import uk.gov.hmcts.reform.professionalapi.controller.advice.ErrorResponse; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.MfaUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.NewUserCreationRequest; @@ -49,10 +51,14 @@ import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationsDetailResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationsWithPbaStatusResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateOrgResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.UpdatePbaStatusResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateResponseParent; import uk.gov.hmcts.reform.professionalapi.domain.Organisation; import uk.gov.hmcts.reform.professionalapi.domain.PbaResponse; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -745,4 +751,91 @@ public ResponseEntity retrieveOrganisationsByProfileIds( .status(HttpStatus.OK) .body(response); } + + + @Operation( + summary = "Updates contact informations(address details) to organisation", + description = "**IDAM Roles to access API** :
pui-organisation-manager", + security = { + @SecurityRequirement(name = "ServiceAuthorization"), + @SecurityRequirement(name = "Authorization") + } + + ) + + @ApiResponse( + responseCode = "200", + description = "Updated Contact Information", + content = @Content + ) + @ApiResponse( + responseCode = "400", + description = "An invalid request has been provided", + content = @Content + ) + @ApiResponse( + responseCode = "401", + description = "Unauthorized Error : " + + "The requested resource is restricted and requires authentication", + content = @Content + ) + + @ApiResponse( + responseCode = "403", + description = "Forbidden Error: Access denied", + content = @Content + ) + @ApiResponse( + responseCode = "500", + description = "Internal Server Error", + content = @Content + ) + + @PutMapping( + path = "/contactInformation", + consumes = APPLICATION_JSON_VALUE, + produces = APPLICATION_JSON_VALUE + ) + @ResponseStatus(value = HttpStatus.OK) + @Secured({"prd-admin"}) + public UpdateResponseParent updateContactInformationForOrganisation( + @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "contactInformationUpdateRequest") + @Valid @NotNull @RequestBody ContactInformationUpdateRequest contactInformationUpdateRequest + ) { + + //check if request list is empty + List contactInformationUpdateDataList = + contactInformationUpdateRequest.getContactInformationUpdateData(); + + if (contactInformationUpdateDataList.isEmpty()) { + throw new InvalidRequest("Request is empty"); + } + + final List updateOrgResponsesList = new ArrayList<>(); + //iterate over each address/orgid pair and update values + contactInformationUpdateDataList.forEach(contactInformationUpdateData -> { + String orgId = contactInformationUpdateData.getOrganisationId(); + if (StringUtils.isEmpty(orgId)) { + updateOrgResponsesList.add(new UpdateOrgResponse("","failure", + HttpStatus.BAD_REQUEST.value(),"Organisation id is missing")); + } else { + var existingOrganisation = organisationService.getOrganisationByOrgIdentifier(orgId); + organisationIdentifierValidatorImpl.validateOrganisationId(orgId, + updateOrgResponsesList, existingOrganisation); + organisationCreationRequestValidator.validateContactInformationAndDxAddress( + contactInformationUpdateData,updateOrgResponsesList); + if (!updateOrgResponsesList.stream().filter( + updateOrg -> orgId.equalsIgnoreCase(updateOrg.getOrganisationId())).findAny().isPresent()) { + organisationService.updateContactInformation(existingOrganisation, + contactInformationUpdateData.isDxAddressUpdate(),contactInformationUpdateData, + contactInformationUpdateData.isContactInformationUpdate(), + contactInformationUpdateData.getAddressid(),updateOrgResponsesList); + } + } + }); + + return organisationService.generateUpdateResponse(updateOrgResponsesList,"contactInformations"); + + } } + diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationCreationRequest.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationCreationRequest.java index 63183f97fd..690bf96763 100644 --- a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationCreationRequest.java +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationCreationRequest.java @@ -11,11 +11,15 @@ @Getter @Builder(builderMethodName = "aContactInformationCreationRequest") +@NotNull(message = "ContactInformationCreationRequest is required") public class ContactInformationCreationRequest { private final String uprn; + @NotEmpty + @NotNull(message = "AddressLine1 is required") @NotNull @NotEmpty + private final String addressLine1; private final String addressLine2; @@ -32,7 +36,6 @@ public class ContactInformationCreationRequest { private final List dxAddress; - @JsonCreator public ContactInformationCreationRequest( @JsonProperty("uprn") String uprn, diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationUpdateRequest.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationUpdateRequest.java new file mode 100644 index 0000000000..1be619b7dc --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationUpdateRequest.java @@ -0,0 +1,87 @@ +package uk.gov.hmcts.reform.professionalapi.controller.request; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@NotNull(message = "ContactInformationUpdateRequest is required") +public class ContactInformationUpdateRequest { + + @JsonProperty(value = "addresses") + private List contactInformationUpdateData; + + public ContactInformationUpdateRequest() { + + } + + @Getter + @Setter + public static class ContactInformationUpdateData { + + @Valid + @NotNull(message = " organisation Id is required.") + private String organisationId; + + boolean dxAddressUpdate; + boolean contactInformationUpdate; + String addressid; + + private final String uprn; + + @NotEmpty + @NotNull(message = "AddressLine1 is required") + private final String addressLine1; + + private final String addressLine2; + + private final String addressLine3; + + private final String townCity; + + private final String county; + + private final String country; + + private final String postCode; + + private final List dxAddress; + + @JsonCreator + public ContactInformationUpdateData( + @JsonProperty("organisationId") String organisationId, + @JsonProperty("dxAddressUpdate") boolean dxAddressUpdate, + @JsonProperty("contactInformationUpdate") boolean contactInformationUpdate, + @JsonProperty("addressid") String addressid, + @JsonProperty("uprn") String uprn, + @JsonProperty("addressLine1") String addressLine1, + @JsonProperty("addressLine2") String addressLine2, + @JsonProperty("addressLine3") String addressLine3, + @JsonProperty("townCity") String townCity, + @JsonProperty("county") String county, + @JsonProperty("country") String country, + @JsonProperty("postCode") String postCode, + @JsonProperty("dxAddress") List dxAddress) { + this.organisationId = organisationId; + this.dxAddressUpdate = dxAddressUpdate; + this.contactInformationUpdate = contactInformationUpdate; + this.addressid = addressid; + this.uprn = uprn; + this.addressLine1 = addressLine1; + this.addressLine2 = addressLine2; + this.addressLine3 = addressLine3; + this.townCity = townCity; + this.county = county; + this.country = country; + this.postCode = postCode; + this.dxAddress = dxAddress; + } + } +} \ No newline at end of file diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/DxAddressUpdateRequest.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/DxAddressUpdateRequest.java new file mode 100644 index 0000000000..6bd4c96415 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/DxAddressUpdateRequest.java @@ -0,0 +1,33 @@ +package uk.gov.hmcts.reform.professionalapi.controller.request; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder(builderMethodName = "dxAddressUpdateRequest") +public class DxAddressUpdateRequest { + + private final String dxAddressId; + + @NotNull + @Schema(name = "dxNumber", example = "string") + @Pattern(regexp = "^(?:DX|NI) \\d{10}+$") + private final String dxNumber; + @NotNull + private final String dxExchange; + + @JsonCreator + public DxAddressUpdateRequest(@JsonProperty("dxAddressId") String dxAddressId, + @JsonProperty("dxNumber") String dxNumber, + @JsonProperty("dxExchange") String dxExchange) { + + this.dxAddressId = dxAddressId; + this.dxNumber = dxNumber; + this.dxExchange = dxExchange; + } +} \ No newline at end of file diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/OrganisationCreationRequestValidator.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/OrganisationCreationRequestValidator.java index 3fa738cce7..7804e7a075 100644 --- a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/OrganisationCreationRequestValidator.java +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/OrganisationCreationRequestValidator.java @@ -4,11 +4,14 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import uk.gov.hmcts.reform.professionalapi.controller.advice.ResourceNotFoundException; import uk.gov.hmcts.reform.professionalapi.controller.request.BulkCustomerRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidContactInformations; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.NewUserCreationRequest; @@ -18,6 +21,7 @@ import uk.gov.hmcts.reform.professionalapi.controller.request.RequestValidator; import uk.gov.hmcts.reform.professionalapi.controller.request.UserCreationRequest; import uk.gov.hmcts.reform.professionalapi.controller.response.ContactInformationValidationResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateOrgResponse; import uk.gov.hmcts.reform.professionalapi.domain.Organisation; import uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus; @@ -31,6 +35,7 @@ import static java.util.Arrays.asList; import static org.apache.commons.lang.StringUtils.isBlank; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ALPHA_NUMERIC_WITH_SPECIAL_CHAR_REGEX; +import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.DX_ADDRESS_NOT_FOUND; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.EMAIL_REGEX; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ERROR_MESSAGE_EMPTY_CONTACT_INFORMATION; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ERROR_MESSAGE_INVALID_STATUS_PASSED; @@ -100,6 +105,51 @@ private List validateConstraintValidation( return contactInformationValidationResponses; } + public void validateContactInformationAndDxAddress( + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformation, + List updateOrgResponsesList) { + + if (contactInformation.isDxAddressUpdate()) { + List dxAddressList = contactInformation.getDxAddress(); + if (dxAddressList == null || dxAddressList.size() == 0) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformation.getOrganisationId(), + "failure", HttpStatus.BAD_REQUEST.value(), DX_ADDRESS_NOT_FOUND)); + } else if (dxAddressList != null && !dxAddressList.isEmpty()) { + dxAddressList.forEach(dxAddress -> { + if (StringUtils.isBlank(dxAddress.getDxNumber()) + || StringUtils.isBlank(dxAddress.getDxExchange())) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformation.getOrganisationId(), + "failure", HttpStatus.BAD_REQUEST.value(), + "DX Number or DX Exchange cannot be empty")); + } else if (dxAddress.getDxNumber().length() >= 14 || dxAddress.getDxExchange().length() >= 41) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformation.getOrganisationId(), + "failure",HttpStatus.BAD_REQUEST.value(), + "DX Number (max=13) or DX Exchange (max=40) has invalid length")); + } else if (!dxAddress.getDxNumber().matches("^[a-zA-Z0-9 ]*$")) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformation.getOrganisationId(), + "failure",HttpStatus.BAD_REQUEST.value(), + "Invalid Dx Number entered: " + dxAddress.getDxNumber() + ", it can only contain " + .concat("numbers, letters and spaces"))); + } + }); + } + } + if (contactInformation.isContactInformationUpdate()) { + if (StringUtils.isBlank(contactInformation.getAddressLine1())) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformation.getOrganisationId(), + "failure",HttpStatus.BAD_REQUEST.value(), + "AddressLine1 cannot be empty")); + } + if (null != contactInformation.getUprn() && contactInformation.getUprn().length() > 14) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformation.getOrganisationId(), + "failure",HttpStatus.BAD_REQUEST.value(), + "Uprn must not be greater than 14 characters long")); + } + } + + } + + public void validateContactInformations( List contactInformationCreationRequests) { diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/impl/OrganisationByProfileIdsRequestValidator.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/impl/OrganisationByProfileIdsRequestValidator.java index 13200f8d05..54c37f26d4 100644 --- a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/impl/OrganisationByProfileIdsRequestValidator.java +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/impl/OrganisationByProfileIdsRequestValidator.java @@ -10,4 +10,6 @@ public void validate(Integer pageSize) { throw new InvalidRequest("001 missing/invalid pageSize"); } } + + } diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/impl/OrganisationIdentifierValidatorImpl.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/impl/OrganisationIdentifierValidatorImpl.java index f46cb19133..f4f72d53df 100644 --- a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/impl/OrganisationIdentifierValidatorImpl.java +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/impl/OrganisationIdentifierValidatorImpl.java @@ -10,6 +10,7 @@ import uk.gov.hmcts.reform.professionalapi.controller.advice.ResourceNotFoundException; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.validator.OrganisationIdentifierValidator; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateOrgResponse; import uk.gov.hmcts.reform.professionalapi.domain.Organisation; import uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus; import uk.gov.hmcts.reform.professionalapi.service.OrganisationService; @@ -159,4 +160,22 @@ private boolean isSinceInValidFormat(String since) { return true; } + + public List validateOrganisationId(String orgId, List + updateOrgResponsesList, Organisation existingOrganisation) { + + if (!orgId.matches("^[A-Z0-9]{7}$")) { + updateOrgResponsesList.add(new UpdateOrgResponse(orgId, "failure", + HttpStatus.BAD_REQUEST.value(), + "The given organisationIdentifier must be 7 Alphanumeric Characters")); + } else if (Optional.ofNullable(existingOrganisation).isEmpty()) { + updateOrgResponsesList.add(new UpdateOrgResponse(orgId, "failure", + HttpStatus.BAD_REQUEST.value(), NO_ORG_FOUND_FOR_GIVEN_ID)); + } else if (OrganisationStatus.ACTIVE != existingOrganisation.getStatus()) { + updateOrgResponsesList.add(new UpdateOrgResponse(orgId, "failure", + HttpStatus.BAD_REQUEST.value(), ORG_NOT_ACTIVE)); + } + + return updateOrgResponsesList; + } } diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/response/UpdateOrgResponse.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/response/UpdateOrgResponse.java new file mode 100644 index 0000000000..5e0469e809 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/response/UpdateOrgResponse.java @@ -0,0 +1,20 @@ +package uk.gov.hmcts.reform.professionalapi.controller.response; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class UpdateOrgResponse { + + @JsonProperty + private String organisationId; + @JsonProperty + private String status; + @JsonProperty + private int statusCode; + @JsonProperty + private String message; + +} diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/response/UpdateResponseParent.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/response/UpdateResponseParent.java new file mode 100644 index 0000000000..daeb96ff2a --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/controller/response/UpdateResponseParent.java @@ -0,0 +1,30 @@ +package uk.gov.hmcts.reform.professionalapi.controller.response; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.List; + +import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; + +@Getter +@JsonPropertyOrder({"status"}) +@AllArgsConstructor +public class UpdateResponseParent { + + @JsonInclude(NON_NULL) + @JsonProperty("status") + private String status; + + @JsonInclude(NON_NULL) + @JsonProperty("message") + private String message; + + @JsonInclude(NON_NULL) + @JsonProperty(value = "responses") + private List updateOrgResponse; + +} diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/service/OrganisationService.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/service/OrganisationService.java index 93992861a1..2f8ae994ad 100644 --- a/src/main/java/uk/gov/hmcts/reform/professionalapi/service/OrganisationService.java +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/service/OrganisationService.java @@ -3,6 +3,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.OrgAttributeRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.OrganisationCreationRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.PbaRequest; @@ -14,6 +15,8 @@ import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationsDetailResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationsDetailResponseV2; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateOrgResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateResponseParent; import uk.gov.hmcts.reform.professionalapi.domain.Organisation; import uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus; import uk.gov.hmcts.reform.professionalapi.domain.PaymentAccount; @@ -32,6 +35,12 @@ public interface OrganisationService { BulkCustomerOrganisationsDetailResponse retrieveOrganisationDetailsForBulkCustomer(String bulkCustId, String idamId); + List updateContactInformation(Organisation existingOrganisation,Boolean dxAddressUpdate, + ContactInformationUpdateRequest.ContactInformationUpdateData + contactInformationUpdateData,Boolean + contactInformationUpdate, String addressid, + List updateOrgResponsesList); + OrganisationsDetailResponseV2 retrieveAllOrganisationsForV2Api(LocalDateTime formattedSince, Pageable pageable); OrganisationEntityResponseV2 retrieveOrganisationForV2Api(String organisationIdentifier, @@ -81,4 +90,8 @@ void addContactInformationsToOrganisation( ResponseEntity retrieveOrganisationByUserId(String userId); + UpdateResponseParent generateUpdateResponse(List updateOrgNameResponse,String field); + + + } diff --git a/src/main/java/uk/gov/hmcts/reform/professionalapi/service/impl/OrganisationServiceImpl.java b/src/main/java/uk/gov/hmcts/reform/professionalapi/service/impl/OrganisationServiceImpl.java index 3e56cb744f..73f3e81725 100644 --- a/src/main/java/uk/gov/hmcts/reform/professionalapi/service/impl/OrganisationServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/reform/professionalapi/service/impl/OrganisationServiceImpl.java @@ -21,8 +21,10 @@ import uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants; import uk.gov.hmcts.reform.professionalapi.controller.feign.UserProfileFeignClient; import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.DeleteUserProfilesRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.OrgAttributeRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.OrganisationCreationRequest; @@ -42,6 +44,8 @@ import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationsDetailResponseV2; import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationsWithPbaStatusResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.SuperUserResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateOrgResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateResponseParent; import uk.gov.hmcts.reform.professionalapi.domain.AddPbaResponse; import uk.gov.hmcts.reform.professionalapi.domain.BulkCustomerDetails; import uk.gov.hmcts.reform.professionalapi.domain.ContactInformation; @@ -87,10 +91,13 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import java.util.stream.Stream; import static java.lang.Boolean.TRUE; +import static java.util.stream.Collectors.toList; import static org.apache.commons.lang3.ObjectUtils.isNotEmpty; import static org.springframework.util.CollectionUtils.isEmpty; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ERROR_MSG_PARTIAL_SUCCESS; @@ -285,8 +292,8 @@ public void addSuperUserToOrganisation( } public void addContactInformationToOrganisation( - List contactInformationCreationRequest, - Organisation organisation) { + List contactInformationCreationRequest, + Organisation organisation) { if (contactInformationCreationRequest != null) { contactInformationCreationRequest.forEach(contactInfo -> { @@ -321,8 +328,8 @@ public ContactInformation setNewContactInformationFromRequest(ContactInformation } - private void addDxAddressToContactInformation(List dxAddressCreationRequest, - ContactInformation contactInformation) { + public void addDxAddressToContactInformation(List dxAddressCreationRequest, + ContactInformation contactInformation) { if (dxAddressCreationRequest != null) { List dxAddresses = new ArrayList<>(); dxAddressCreationRequest.forEach(dxAdd -> { @@ -330,12 +337,42 @@ private void addDxAddressToContactInformation(List dxA RefDataUtil.removeEmptySpaces(dxAdd.getDxNumber()), RefDataUtil.removeEmptySpaces(dxAdd.getDxExchange()), contactInformation); + dxAddress.setLastUpdated(LocalDateTime.now()); dxAddresses.add(dxAddress); }); dxAddressRepository.saveAll(dxAddresses); } } + public void updateDxAddressInformation(List dxAddressUpdateRequests, + List existingContactInformationList) { + + if (dxAddressUpdateRequests != null) { + List dxAddresses = new ArrayList<>(); + dxAddressUpdateRequests.forEach(dxAdd -> { + existingContactInformationList.forEach(existing -> { + List existingDxArrdessesList = existing.getDxAddresses(); + existingDxArrdessesList.forEach(existingDxAdd -> { + if (existingDxAdd.getId().equals(dxAdd.getDxAddressId())) { + existingDxAdd.setDxNumber(dxAdd.getDxNumber()); + existingDxAdd.setDxExchange(dxAdd.getDxExchange()); + existingDxAdd.setLastUpdated(LocalDateTime.now()); + dxAddresses.add(existingDxAdd); + } else { + DxAddress dxAddress = new DxAddress( + RefDataUtil.removeEmptySpaces(dxAdd.getDxNumber()), + RefDataUtil.removeEmptySpaces(dxAdd.getDxExchange()), + existing); + dxAddress.setLastUpdated(LocalDateTime.now()); + dxAddresses.add(dxAddress); + } + }); + }); + }); + dxAddressRepository.saveAll(dxAddresses); + } + } + public List retrieveActiveOrganisationDetails(List activeOrganisations) { List updatedOrganisationDetails = new ArrayList<>(); @@ -1056,5 +1093,138 @@ private boolean getMoreAvailable(Page pageableOrganisations) { return !pageableOrganisations.isLast(); } + @Transactional + public void updateContacts(Boolean contactInformationUpdate, Boolean dxAddressUpdate, + ContactInformationUpdateRequest.ContactInformationUpdateData + contactInformationUpdateData,List existingContactInformationList, + Organisation organisation,List updateOrgResponsesList) { + // if both are false no update takes place + if (!dxAddressUpdate && !contactInformationUpdate) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformationUpdateData.getOrganisationId(), + "failure", HttpStatus.BAD_REQUEST.value(), "dxAddressUpdate and " + + "contactInformationUpdate are both false . Cannot update contact information")); + } else { + if (dxAddressUpdate) { + // if only dxaddress needs updating + updateDxAddressInformation(contactInformationUpdateData.getDxAddress(), + existingContactInformationList); + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformationUpdateData.getOrganisationId(), + "success", HttpStatus.OK.value(), "dxAddress updated successfully")); + } + if (contactInformationUpdate) { + //If single address is present and contact info needs updating then update details + updateContactInformation(existingContactInformationList.get(0), + contactInformationUpdateData,organisation); + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformationUpdateData.getOrganisationId(), + "success", HttpStatus.OK.value(), "contactInformation updated successfully")); + } + } + + } + + + private void updateContactInformation(ContactInformation existingContactInformation, + ContactInformationUpdateRequest.ContactInformationUpdateData + contactInformationUpdateData, + Organisation organisation + ) { + if (StringUtils.isNotEmpty(contactInformationUpdateData.getAddressLine1())) { + existingContactInformation.setAddressLine1( + RefDataUtil.removeEmptySpaces(contactInformationUpdateData.getAddressLine1())); + } + if (StringUtils.isNotEmpty(contactInformationUpdateData.getAddressLine2())) { + existingContactInformation.setAddressLine2( + RefDataUtil.removeEmptySpaces(contactInformationUpdateData.getAddressLine2())); + } + if (StringUtils.isNotEmpty(contactInformationUpdateData.getAddressLine3())) { + existingContactInformation.setAddressLine3( + RefDataUtil.removeEmptySpaces(contactInformationUpdateData.getAddressLine3())); + } + if (StringUtils.isNotEmpty(contactInformationUpdateData.getTownCity())) { + existingContactInformation.setTownCity(RefDataUtil.removeEmptySpaces(contactInformationUpdateData + .getTownCity())); + } + if (StringUtils.isNotEmpty(contactInformationUpdateData.getCounty())) { + existingContactInformation.setCounty(RefDataUtil.removeEmptySpaces(contactInformationUpdateData + .getCounty())); + } + if (StringUtils.isNotEmpty(contactInformationUpdateData.getCountry())) { + existingContactInformation.setCountry(RefDataUtil.removeEmptySpaces(contactInformationUpdateData + .getCountry())); + } + if (StringUtils.isNotEmpty(contactInformationUpdateData.getPostCode())) { + existingContactInformation.setPostCode(RefDataUtil.removeEmptySpaces(contactInformationUpdateData + .getPostCode())); + } + existingContactInformation.setOrganisation(organisation); + existingContactInformation.setLastUpdated(LocalDateTime.now()); + contactInformationRepository.save(existingContactInformation); + } + + + @Override + @Transactional + public List updateContactInformation(Organisation existingOrganisation,Boolean dxAddressUpdate, + ContactInformationUpdateRequest.ContactInformationUpdateData + contactInformationUpdateData,Boolean + contactInformationUpdate, String addressid, + List updateOrgResponsesList) { + + List existingContactInformationList = existingOrganisation.getContactInformation(); + + if (!existingContactInformationList.isEmpty()) { + //if single contact information record found for organisation then update the same + if (existingContactInformationList.size() == 1) { + updateContacts(contactInformationUpdate,dxAddressUpdate,contactInformationUpdateData, + existingContactInformationList,existingOrganisation,updateOrgResponsesList); + } else { + //if multiple records found then update the one for which the id is provided + if (StringUtils.isEmpty(addressid)) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformationUpdateData + .getOrganisationId(),"failure", HttpStatus.BAD_REQUEST.value(), + "Multiple addresses found for organisation . Please enter specific address " + + "id of the contact information to be updated")); + } else { + AtomicBoolean idFound = new AtomicBoolean(false); + existingContactInformationList.forEach(existingInfo -> { + if (existingInfo.getId().toString().equalsIgnoreCase(addressid)) { + idFound.set(true); + updateContacts(contactInformationUpdate,dxAddressUpdate,contactInformationUpdateData, + existingContactInformationList,existingOrganisation,updateOrgResponsesList); + } + }); + if (!idFound.get()) { + updateOrgResponsesList.add(new UpdateOrgResponse(contactInformationUpdateData + .getOrganisationId(),"failure", HttpStatus.BAD_REQUEST.value(), + "Could not find address to update for the id provided please check and " + + "try again")); + } + } + } + } else { + updateOrgResponsesList.add(new UpdateOrgResponse(existingOrganisation.getOrganisationIdentifier(), + "failure", HttpStatus.BAD_REQUEST.value(), "No contact information existing for given organisation")); + } + return updateOrgResponsesList; + } + + @Override + public UpdateResponseParent generateUpdateResponse(List updateOrgResponses,String field) { + AtomicReference message = null; + boolean result = updateOrgResponses.stream().anyMatch(updateOrgNameResponse -> + updateOrgNameResponse.getStatusCode() != 200); + List responseList = updateOrgResponses.stream().filter(updateOrgNameResponse -> + updateOrgNameResponse.getStatusCode() == 400).collect(toList()); + + if (updateOrgResponses.size() == responseList.size()) { + return new UpdateResponseParent("failure",null,updateOrgResponses); + } else if (result) { + return new UpdateResponseParent("partial_success",null,updateOrgResponses); + } else { + return new UpdateResponseParent("success","All " + field + " updated successfully", + null); + } + } + } diff --git a/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/internal/OrganisationInternalControllerTest.java b/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/internal/OrganisationInternalControllerTest.java index 958e2af2a9..09d31e7ee8 100644 --- a/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/internal/OrganisationInternalControllerTest.java +++ b/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/internal/OrganisationInternalControllerTest.java @@ -21,6 +21,8 @@ import org.springframework.web.context.request.ServletRequestAttributes; import uk.gov.hmcts.reform.professionalapi.controller.advice.ResourceNotFoundException; import uk.gov.hmcts.reform.professionalapi.controller.feign.UserProfileFeignClient; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.MfaUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.NewUserCreationRequest; @@ -38,7 +40,10 @@ import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationEntityResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationsDetailResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateOrgResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateResponseParent; import uk.gov.hmcts.reform.professionalapi.controller.response.UserProfileCreationResponse; +import uk.gov.hmcts.reform.professionalapi.domain.ContactInformation; import uk.gov.hmcts.reform.professionalapi.domain.MFAStatus; import uk.gov.hmcts.reform.professionalapi.domain.Organisation; import uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus; @@ -57,7 +62,9 @@ import uk.gov.hmcts.reform.professionalapi.service.impl.PrdEnumServiceImpl; import java.nio.charset.Charset; +import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -76,6 +83,8 @@ import static org.mockito.Mockito.when; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ORG_NAME; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ORG_STATUS; +import static uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressCreationRequest.dxAddressCreationRequest; +import static uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressUpdateRequest.dxAddressUpdateRequest; @ExtendWith(MockitoExtension.class) @SuppressWarnings("checkstyle:AbbreviationAsWordInName") @@ -87,6 +96,9 @@ class OrganisationInternalControllerTest { private PaymentAccountService paymentAccountServiceMock; private Organisation organisation; private OrganisationCreationRequest organisationCreationRequest; + + private ContactInformationCreationRequest contactInformationCreationRequest; + private OrganisationOtherOrgsCreationRequest organisationOtherOrgsCreationRequest; private OrganisationCreationRequestValidator organisationCreationRequestValidatorMock; private PaymentAccountValidator paymentAccountValidatorMock; @@ -161,7 +173,10 @@ void setUp() throws Exception { organisationOtherOrgsCreationRequest = new OrganisationOtherOrgsCreationRequest("test", "PENDING", null, "sra-id", "false", "number02", "company-url", userCreationRequest, null, null,"Doctor",null); - + contactInformationCreationRequest = new ContactInformationCreationRequest("uprn1","addressLine1", + "addressLine2","addressLine3", "some-country1","some-county1","som1-town-city", + "som1-post-code",Arrays.asList(dxAddressCreationRequest() + .dxNumber("DX 1234567890").dxExchange("dxExchange").build())); organisation.setOrganisationIdentifier("AK57L4T"); organisationResponse = new OrganisationResponse(organisation); @@ -572,4 +587,74 @@ void testRetrieveOrgByPbaStatus() { .getOrganisationsByPbaStatus(pbaStatus.toString()); } + @Test + void testUpdateOrgContactInformation() { + Organisation organisation1 = new Organisation("Org-Name-1", OrganisationStatus.ACTIVE, "sra-id", + "companyN", false, "www.org.com"); + Organisation organisation2 = new Organisation("Org-Name-2", OrganisationStatus.ACTIVE, "sra-id-0", + "companyN", false, "www.org.com"); + + ContactInformationUpdateRequest contactInformationUpdateRequest = new ContactInformationUpdateRequest(); + List contactInformationUpdateDataList + = new ArrayList<>(); + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData1 = + new ContactInformationUpdateRequest.ContactInformationUpdateData( + organisation1.getOrganisationIdentifier(),true,true,"", + "uprn1","addressLine1", + "addressLine2","addressLine3", "som1-town-city", + "some-county1","some-country1","som1-post-code",Arrays.asList( + dxAddressUpdateRequest().dxAddressId("12345678").dxNumber("DX 1234567890") + .dxExchange("dxExchange-1").build())); + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData2 = + new ContactInformationUpdateRequest.ContactInformationUpdateData( + organisation2.getOrganisationIdentifier(),true,true,"", + "uprn2","addressLine2", + "addressLine2-2","addressLine3-3", "som2-town-city", + "some-county2","some-country2","som2-post-code",Arrays.asList( + dxAddressUpdateRequest().dxAddressId("12345678").dxNumber("DX 2234567890") + .dxExchange("dxExchange-2").build())); + contactInformationUpdateDataList.add(contactInformationUpdateData1); + contactInformationUpdateDataList.add(contactInformationUpdateData2); + contactInformationUpdateRequest.setContactInformationUpdateData(contactInformationUpdateDataList); + + final List updateContactInformationResponsesList = new ArrayList<>(); + var contactInformation = new ContactInformation(); + contactInformation.setCountry("TestCountry"); + contactInformation.setCreated(LocalDateTime.now()); + contactInformation.setId(UUID.randomUUID()); + + var contactInformation1 = new ContactInformation(); + contactInformation1.setCountry("TestAnotherCountry"); + contactInformation1.setCreated(LocalDateTime.now()); + contactInformation1.setId(UUID.randomUUID()); + ArrayList existingContactInformationList = new ArrayList<>(); + existingContactInformationList.addAll(List.of(contactInformation1, contactInformation)); + + organisation.setContactInformations(existingContactInformationList); + when(organisationServiceMock.getOrganisationByOrgIdentifier(any())) + .thenReturn(organisation); + when(orgIdValidatorMock.validateOrganisationId(any(), + any(),any())).thenReturn(updateContactInformationResponsesList); + + when(organisationServiceMock.updateContactInformation(any(),any(),any(), + any(),any(),any())).thenReturn(updateContactInformationResponsesList); + + UpdateResponseParent updateResponse = new UpdateResponseParent("success", + "All contactInformations updated successfully",null); + when(organisationServiceMock. generateUpdateResponse(any(),any())).thenReturn(updateResponse); + + UpdateResponseParent response = organisationInternalController + .updateContactInformationForOrganisation(contactInformationUpdateRequest); + + assertThat(response).isNotNull(); + assertThat(response.getStatus()).isEqualTo("success"); + assertThat(response.getMessage()).isEqualTo("All contactInformations updated successfully"); + + verify(organisationServiceMock, times(2)).getOrganisationByOrgIdentifier(any()); + verify(organisationServiceMock, times(2)) + .updateContactInformation(any(),any(),any(),any(),any(),any()); + + } + + } diff --git a/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationCreationRequestTest.java b/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationCreationRequestTest.java index 31216fdfa8..08fbedbb04 100644 --- a/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationCreationRequestTest.java +++ b/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/request/ContactInformationCreationRequestTest.java @@ -30,7 +30,7 @@ void test_has_mandatory_fields_specified_not_null() { Set> violations = validator .validate(contactInformationCreationRequest); - assertThat(violations).hasSize(2); + assertThat(violations).hasSize(3); } @Test diff --git a/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/OrganisationCreationRequestValidatorTest.java b/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/OrganisationCreationRequestValidatorTest.java index 120915331c..1bef4b3dca 100644 --- a/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/OrganisationCreationRequestValidatorTest.java +++ b/src/test/java/uk/gov/hmcts/reform/professionalapi/controller/request/validator/OrganisationCreationRequestValidatorTest.java @@ -14,6 +14,7 @@ import uk.gov.hmcts.reform.professionalapi.controller.advice.ResourceNotFoundException; import uk.gov.hmcts.reform.professionalapi.controller.request.BulkCustomerRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressCreationRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidContactInformations; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidRequest; @@ -23,6 +24,7 @@ import uk.gov.hmcts.reform.professionalapi.controller.request.OrganisationOtherOrgsCreationRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.RequestValidator; import uk.gov.hmcts.reform.professionalapi.controller.request.UserCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateOrgResponse; import uk.gov.hmcts.reform.professionalapi.domain.Organisation; import uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus; @@ -45,6 +47,7 @@ import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ERROR_MESSAGE_INVALID_STATUS_PASSED; import static uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest.aContactInformationCreationRequest; import static uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressCreationRequest.dxAddressCreationRequest; +import static uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressUpdateRequest.dxAddressUpdateRequest; import static uk.gov.hmcts.reform.professionalapi.controller.request.validator.OrganisationCreationRequestValidator.isInputOrganisationStatusValid; import static uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus.ACTIVE; import static uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus.PENDING; @@ -462,6 +465,27 @@ void test_add_contacts_informations_to_orgs_validation_address_line_is_missing() } + @Test + void test_validate_contact_information() { + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData = + new ContactInformationUpdateRequest.ContactInformationUpdateData( + "orgId",true,true,"addId","uprn1", + "addressLine1","addressLine2","addressLine3", + "som1-town-city","some-county1","some-country1", + "som1-post-code",Arrays.asList( + dxAddressUpdateRequest().dxAddressId("12345678").dxNumber("DX 1234567890") + .dxExchange("dxExchange-1").build())); + + final List updateContactInformationResponsesList = new ArrayList<>(); + + organisationCreationRequestValidator + .validateContactInformationAndDxAddress( + contactInformationUpdateData, + updateContactInformationResponsesList); + assertTrue(true); + } + + @Test void test_add_contacts_informations_to_orgs_valid_dx_address() { List contactInformationCreationRequests = diff --git a/src/test/java/uk/gov/hmcts/reform/professionalapi/helper/OrganisationFixtures.java b/src/test/java/uk/gov/hmcts/reform/professionalapi/helper/OrganisationFixtures.java index 86e98801c4..28fd42c253 100644 --- a/src/test/java/uk/gov/hmcts/reform/professionalapi/helper/OrganisationFixtures.java +++ b/src/test/java/uk/gov/hmcts/reform/professionalapi/helper/OrganisationFixtures.java @@ -29,6 +29,14 @@ private OrganisationFixtures() { public static OrganisationCreationRequest.OrganisationCreationRequestBuilder someMinimalOrganisationRequest() { + List dx1 = new LinkedList<>(); + dx1.add(dxAddressCreationRequest() + .dxNumber("DX1234567890") + .dxExchange("dxExchange").build()); + dx1.add(dxAddressCreationRequest() + .dxNumber("DX123456777") + .dxExchange("dxExchange1").build()); + return anOrganisationCreationRequest() .name("some-org-name") .status("ACTIVE") @@ -40,6 +48,7 @@ public static OrganisationCreationRequest.OrganisationCreationRequestBuilder som .contactInformation(Arrays.asList(aContactInformationCreationRequest() .addressLine1("addressLine1") .uprn("uprn") + .dxAddress(dx1) .build())); } @@ -93,6 +102,28 @@ public static OrganisationCreationRequest.OrganisationCreationRequestBuilder org .build())); } + public static OrganisationCreationRequest.OrganisationCreationRequestBuilder + organisationRequestWithMultipleContactInformations() { + Set paymentAccounts = new HashSet<>(); + paymentAccounts.add("PBA1234567"); + + return anOrganisationCreationRequest() + .name("some-org-name") + .status("PENDING") + .sraId("sra-id") + .sraRegulated("false") + .companyUrl("company -url") + .companyNumber(randomAlphabetic(8)) + .paymentAccount(paymentAccounts) + .superUser(aUserCreationRequest() + .firstName("some-fname") + .lastName("some-lname") + .email("someone@somewhere.com") + .build()) + .contactInformation(getContactInformationList()); + } + + public static OrganisationOtherOrgsCreationRequest otherOrganisationRequestWithAllFields() { Set paymentAccounts = new HashSet<>(); @@ -264,6 +295,20 @@ public static List getContactInformationList( } + public static ContactInformationCreationRequest getContactInformationWithoutDxAddress() { + + return aContactInformationCreationRequest() + .uprn("uprn1") + .addressLine1("Updated-addressLine1") + .addressLine2("Updated-addressLine2") + .addressLine3("Updated-addressLine3") + .country("Updated-country") + .county("Updated-county") + .townCity("Updated-town-city") + .postCode("Uppost-code") + .build(); + } + public static List createContactInformationCreationRequests() { Set paymentAccounts = new HashSet<>(); paymentAccounts.add("PBA" + RandomStringUtils.randomAlphabetic(7)); @@ -315,6 +360,90 @@ public static List createContactInformationCr return contactInfoList; } + public static ContactInformationCreationRequest + .ContactInformationCreationRequestBuilder createContactInformationRequest() { + + return aContactInformationCreationRequest() + .addressLine1("up-addLine1") + .addressLine2("up-addLine2") + .addressLine3("up-addLine3") + .country("up-country") + .county("up-county") + .townCity("up-town-city") + .uprn("up-uprn1") + .postCode("up-post-code") + .dxAddress(Arrays.asList(dxAddressCreationRequest() + .dxNumber("DX 1234567890") + .dxExchange("up-dxExchange").build())); + + } + + public static ContactInformationCreationRequest + .ContactInformationCreationRequestBuilder createContactInformationRequestWithOnlyDxAddress() { + + return aContactInformationCreationRequest() + .addressLine1("Updated-addressLine1") + .dxAddress(Arrays.asList(dxAddressCreationRequest() + .dxNumber("DX 0000000000") + .dxExchange("Updated-dxExchange").build())); + + } + + public static ContactInformationCreationRequest + .ContactInformationCreationRequestBuilder contactInformationWithOnlyDxNumberChanged() { + + return aContactInformationCreationRequest() + .addressLine1("addressLine1") + .dxAddress(Arrays.asList(dxAddressCreationRequest() + .dxNumber("DX 0000000000") + .dxExchange("dxExchange").build())); + + } + + public static ContactInformationCreationRequest + .ContactInformationCreationRequestBuilder contactInformationWithOnlyDxExchangeChanged() { + + return aContactInformationCreationRequest() + .addressLine1("addressLine1") + .dxAddress(Arrays.asList(dxAddressCreationRequest() + .dxNumber("DX 1234567890") + .dxExchange("Up-dxExchange").build())); + + } + + public static ContactInformationCreationRequest + .ContactInformationCreationRequestBuilder contactInformationWithOnlyAddressLine3Changed() { + + return aContactInformationCreationRequest() + .addressLine1("addressLine1") + .addressLine2("addressLine2") + .addressLine3("updated-addLine3") + .country("country") + .county("county") + .townCity("town-city") + .uprn("uprn1") + .postCode("some-post-code") + .dxAddress(Arrays.asList(dxAddressCreationRequest() + .dxNumber("DX 1234567890") + .dxExchange("dxExchange").build())); + + } + + public static ContactInformationCreationRequest + .ContactInformationCreationRequestBuilder createContactInformationRequestWithoutDxAddress() { + + return aContactInformationCreationRequest() + .addressLine1("upaddressLine1") + .addressLine2("upaddressLine2") + .addressLine3("upaddressLine3") + .country("upcountry") + .county("upcounty") + .townCity("uptown-city") + .uprn("uprn1") + .postCode("uppost-code"); + + } + public static OrganisationCreationRequest.OrganisationCreationRequestBuilder organisationRequestWithMultipleAddressAllFields() { Set paymentAccounts = new HashSet<>(); diff --git a/src/test/java/uk/gov/hmcts/reform/professionalapi/service/impl/OrganisationServiceImplTest.java b/src/test/java/uk/gov/hmcts/reform/professionalapi/service/impl/OrganisationServiceImplTest.java index 4d0a951e6a..e2a81e9fcf 100644 --- a/src/test/java/uk/gov/hmcts/reform/professionalapi/service/impl/OrganisationServiceImplTest.java +++ b/src/test/java/uk/gov/hmcts/reform/professionalapi/service/impl/OrganisationServiceImplTest.java @@ -28,6 +28,7 @@ import uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants; import uk.gov.hmcts.reform.professionalapi.controller.feign.UserProfileFeignClient; import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationCreationRequest; +import uk.gov.hmcts.reform.professionalapi.controller.request.ContactInformationUpdateRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressCreationRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.InvalidRequest; import uk.gov.hmcts.reform.professionalapi.controller.request.OrgAttributeRequest; @@ -49,6 +50,7 @@ import uk.gov.hmcts.reform.professionalapi.controller.response.OrganisationsWithPbaStatusResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.ProfessionalUsersEntityResponse; import uk.gov.hmcts.reform.professionalapi.controller.response.ProfessionalUsersResponse; +import uk.gov.hmcts.reform.professionalapi.controller.response.UpdateOrgResponse; import uk.gov.hmcts.reform.professionalapi.domain.AddPbaResponse; import uk.gov.hmcts.reform.professionalapi.domain.BulkCustomerDetails; import uk.gov.hmcts.reform.professionalapi.domain.ContactInformation; @@ -119,6 +121,7 @@ import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ORG_STATUS; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.PBA_STATUS_MESSAGE_AUTO_ACCEPTED; import static uk.gov.hmcts.reform.professionalapi.controller.constants.ProfessionalApiConstants.ZERO_INDEX; +import static uk.gov.hmcts.reform.professionalapi.controller.request.DxAddressUpdateRequest.dxAddressUpdateRequest; import static uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus.ACTIVE; import static uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus.BLOCKED; import static uk.gov.hmcts.reform.professionalapi.domain.OrganisationStatus.REVIEW; @@ -136,6 +139,10 @@ class OrganisationServiceImplTest { private final OrganisationRepository organisationRepository = mock(OrganisationRepository.class); private final OrgAttributeRepository orgAttributeRepository = mock(OrgAttributeRepository.class); + private final ContactInformationRepository contactInformationRepository = mock(ContactInformationRepository.class); + + private final DxAddressRepository dxAddressRepository = mock(DxAddressRepository.class); + private final ProfessionalUserRepository professionalUserRepositoryMock = mock(ProfessionalUserRepository.class); private final PaymentAccountRepository paymentAccountRepositoryMock = mock(PaymentAccountRepository.class); private final UserAccountMapRepository userAccountMapRepositoryMock = mock(UserAccountMapRepository.class); @@ -206,6 +213,7 @@ class OrganisationServiceImplTest { private final ProfessionalUserService professionalUserServiceMock = mock(ProfessionalUserService.class); + @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); @@ -2718,4 +2726,113 @@ void shouldRetrieveOrganisationsWithEmptyProfileIdList() { assertThat(result).isNotNull(); assertThat(result.getOrganisationInfo()).isNullOrEmpty(); } + + @Test + @SuppressWarnings("unchecked") + void test_updateOrganisationContactInformation() { + final List updateOrgNameResponsesList = new ArrayList<>(); + organisation.setContactInformations(List.of(contactInformation)); + when(organisationRepository.findByOrganisationIdentifier(any(String.class))).thenReturn(organisation); + verify(organisationRepository, times(0)).findByOrganisationIdentifier(any(String.class)); + when(dxAddressRepositoryMock.save(any(DxAddress.class))).thenReturn(dxAddress); + assertNotNull(contactInformationCreationRequest); + ContactInformation existingContactInformation = new ContactInformation(); + existingContactInformation.setAddressLine1(contactInformationCreationRequest.getAddressLine1()); + existingContactInformation.setTownCity(contactInformationCreationRequest.getTownCity()); + + when(contactInformationRepository.save(existingContactInformation)).thenReturn(existingContactInformation); + assertNotNull(existingContactInformation); + assertThat(existingContactInformation.getAddressLine1()) + .isEqualTo(contactInformationCreationRequest.getAddressLine1()); + + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData = + getContactInformationData(); + List updateContactInformationResponse = + sut.updateContactInformation(organisation,true,contactInformationUpdateData,true, + "",updateOrgNameResponsesList); + + assertThat(updateContactInformationResponse).isNotNull(); + assertThat(updateContactInformationResponse.get(0).getStatus()).isEqualTo("success"); + assertThat(updateContactInformationResponse.get(0).getStatusCode()).isEqualTo(200); + assertThat(updateContactInformationResponse.get(0).getMessage()).isEqualToIgnoringCase( + "dxAddress updated successfully"); + assertThat(updateContactInformationResponse.get(1).getStatus()).isEqualTo("success"); + assertThat(updateContactInformationResponse.get(1).getStatusCode()).isEqualTo(200); + assertThat(updateContactInformationResponse.get(1).getMessage()).isEqualToIgnoringCase( + "contactInformation updated successfully"); + + } + + @Test + void testUpdateOrgContactInformationEmptyAddressId() { + + var contactInformation = new ContactInformation(); + contactInformation.setCountry("TestCountry"); + contactInformation.setCreated(LocalDateTime.now()); + + var contactInformation1 = new ContactInformation(); + contactInformation1.setCountry("TestAnotherCountry"); + contactInformation1.setCreated(LocalDateTime.now()); + ArrayList existingContactInformationList = new ArrayList<>(); + existingContactInformationList.addAll(List.of(contactInformation1, contactInformation)); + + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData = + getContactInformationData(); + + organisation.setContactInformations(existingContactInformationList); + final List updateOrgNameResponsesList = new ArrayList<>(); + assertNotNull(existingContactInformationList); + List updateContactInformationResponse = + sut.updateContactInformation(organisation,true,contactInformationUpdateData,true, + null,updateOrgNameResponsesList); + assertThat(updateContactInformationResponse).isNotNull(); + assertThat(updateContactInformationResponse.size()).isEqualTo(1); + assertThat(updateContactInformationResponse.get(0).getMessage()).isEqualToIgnoringCase("Multiple" + + " addresses found for organisation . Please enter specific address " + + "id of the contact information to be updated"); + } + + @Test + void testUpdateOrgContactInformationWithDxAddressUpdateAndContactInformationUpdateFalse() { + + var contactInformation = new ContactInformation(); + contactInformation.setCountry("TestCountry"); + contactInformation.setCreated(LocalDateTime.now()); + contactInformation.setId(UUID.randomUUID()); + + var contactInformation1 = new ContactInformation(); + contactInformation1.setCountry("TestAnotherCountry"); + contactInformation1.setCreated(LocalDateTime.now()); + contactInformation1.setId(UUID.randomUUID()); + ArrayList existingContactInformationList = new ArrayList<>(); + existingContactInformationList.addAll(List.of(contactInformation1, contactInformation)); + + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData = + getContactInformationData(); + + organisation.setContactInformations(existingContactInformationList); + final List updateOrgNameResponsesList = new ArrayList<>(); + assertNotNull(existingContactInformationList); + List updateContactInformationResponse = + sut.updateContactInformation(organisation,true,contactInformationUpdateData,true, + "12345",updateOrgNameResponsesList); + assertThat(updateContactInformationResponse).isNotNull(); + assertThat(updateContactInformationResponse.size()).isEqualTo(1); + assertThat(updateContactInformationResponse.get(0).getMessage()).isEqualToIgnoringCase("Could " + + "not find address to update for the id provided please check and try again"); + } + + + public ContactInformationUpdateRequest.ContactInformationUpdateData getContactInformationData() { + ContactInformationUpdateRequest.ContactInformationUpdateData contactInformationUpdateData = + new ContactInformationUpdateRequest.ContactInformationUpdateData( + organisation.getOrganisationIdentifier(),true,true,"", + "uprn1","addressLine1","addressLine2","addressLine3", + "som1-town-city","some-county1","some-country1","som1-post-code", + Arrays.asList(dxAddressUpdateRequest().dxAddressId("12345678").dxNumber("DX 1234567890") + .dxExchange("dxExchange-1").build())); + + return contactInformationUpdateData; + } + }