diff --git a/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscService.java b/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscService.java index 78cbe1af..7299db1e 100644 --- a/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscService.java +++ b/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscService.java @@ -1,5 +1,6 @@ package uk.gov.companieshouse.pscdataapi.service; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; @@ -176,8 +177,12 @@ public Individual getIndividualPsc( .filter(document -> document.getData().getKind() .equals("individual-person-with-significant-control")); if (pscDocument.isPresent()) { + boolean showFullDateOfBirth = determineShowFullDob( + companyNumber, registerView, pscDocument.get()); + Individual individual = transformer - .transformPscDocToIndividual(pscDocument.get(), registerView); + .transformPscDocToIndividual(pscDocument.get(), showFullDateOfBirth); + if (individual == null) { throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, "Failed to transform PSCDocument to Individual"); @@ -187,6 +192,8 @@ public Individual getIndividualPsc( throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, "Individual PSC document not found in Mongo with id " + notificationId); } + } catch (ResourceNotFoundException rnfe) { + throw rnfe; } catch (Exception ex) { logger.error(ex.getMessage(), DataMapHolder.getLogMap()); throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, @@ -208,8 +215,13 @@ public IndividualBeneficialOwner getIndividualBeneficialOwnerPsc( .equals("individual-beneficial-owner") && document.getCompanyNumber().equals(companyNumber)); if (pscDocument.isPresent()) { + boolean showFullDateOfBirth = determineShowFullDob( + companyNumber, registerView, pscDocument.get()); + IndividualBeneficialOwner individualBeneficialOwner = transformer - .transformPscDocToIndividualBeneficialOwner(pscDocument.get(),registerView); + .transformPscDocToIndividualBeneficialOwner(pscDocument.get(), + showFullDateOfBirth); + if (individualBeneficialOwner == null) { throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, "Failed to transform PSCDocument to IndividualBeneficialOwner"); @@ -220,6 +232,8 @@ public IndividualBeneficialOwner getIndividualBeneficialOwnerPsc( "Individual Beneficial Owner PSC document not found in Mongo with id" + notificationId); } + } catch (ResourceNotFoundException rnfe) { + throw rnfe; } catch (Exception ex) { logger.error(ex.getMessage(), DataMapHolder.getLogMap()); throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, @@ -227,6 +241,48 @@ public IndividualBeneficialOwner getIndividualBeneficialOwnerPsc( } } + private boolean determineShowFullDob(String companyNumber, Boolean registerView, + PscDocument pscDocument) throws ResourceNotFoundException { + if (!registerView) { + return false; + } else { + Optional companyMetrics = companyMetricsApiService + .getCompanyMetrics(companyNumber); + + if (companyMetrics.isPresent()) { + try { + String registerMovedTo = companyMetrics.get().getRegisters() + .getPersonsWithSignificantControl().getRegisterMovedTo(); + if (registerMovedTo.equals("public-register")) { + Boolean isCeased = pscDocument.getData().getCeased(); + boolean ceased = isCeased != null && isCeased; + LocalDate ceasedOn = pscDocument.getData().getCeasedOn(); + LocalDate movedToPublicRegister = companyMetrics.get().getRegisters() + .getPersonsWithSignificantControl().getMovedOn().toLocalDate(); + + if (!ceased || movedToPublicRegister.isBefore(ceasedOn)) { + return true; + } else { + throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, + "not-on-public-register"); + } + } else { + throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, + "not-on-public-register"); + } + } catch (ResourceNotFoundException rnfe) { + throw rnfe; + } catch (Exception ignored) { + throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, + "not-on-public-register"); + } + } else { + throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, String.format( + "No company metrics data found for company number: %s", companyNumber)); + } + } + } + /** Get PSC record and transform it into a Corporate Entity PSC. * @param companyNumber Company number. * @param notificationId Mongo Id. diff --git a/src/main/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformer.java b/src/main/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformer.java index 8e9ca002..d9f83553 100644 --- a/src/main/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformer.java +++ b/src/main/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformer.java @@ -50,7 +50,7 @@ public class CompanyPscTransformer { * @return PSC mongo Document. */ public Individual transformPscDocToIndividual( - PscDocument pscDocument, Boolean registerView) { + PscDocument pscDocument, boolean showFullDateOfBirth) { logger.info("Attempting to transform pscDocument to Individual", DataMapHolder.getLogMap()); Individual individual = new Individual(); @@ -67,7 +67,7 @@ public Individual transformPscDocToIndividual( } if (pscDocument.getSensitiveData() != null) { individual.setDateOfBirth(mapDateOfBirth( - pscDocument.getSensitiveData().getDateOfBirth(), registerView)); + pscDocument.getSensitiveData().getDateOfBirth(), showFullDateOfBirth)); } if (pscDocument.getDeltaAt() != null) { individual.setNotifiedOn(LocalDate.parse(pscDocument.getDeltaAt(), dateTimeFormatter)); @@ -81,7 +81,7 @@ public Individual transformPscDocToIndividual( * @return PSC mongo Document. */ public IndividualBeneficialOwner transformPscDocToIndividualBeneficialOwner( - PscDocument pscDocument, Boolean registerView) { + PscDocument pscDocument, boolean showFullDateOfBirth) { logger.info("Attempting to transform pscDocument to IndividualBeneficialOwner", DataMapHolder.getLogMap()); IndividualBeneficialOwner individualBo = new IndividualBeneficialOwner(); @@ -99,7 +99,7 @@ public IndividualBeneficialOwner transformPscDocToIndividualBeneficialOwner( } if (pscDocument.getSensitiveData() != null) { individualBo.setDateOfBirth(mapDateOfBirth( - pscDocument.getSensitiveData().getDateOfBirth(), registerView)); + pscDocument.getSensitiveData().getDateOfBirth(), showFullDateOfBirth)); } if (pscDocument.getDeltaAt() != null) { individualBo.setNotifiedOn(LocalDate.parse(pscDocument.getDeltaAt(), @@ -413,11 +413,11 @@ private void handleSecureFields(Data data, PscData pscData) { } private uk.gov.companieshouse.api.psc.DateOfBirth mapDateOfBirth( - DateOfBirth inputDateOfBirth, Boolean registerView) { + DateOfBirth inputDateOfBirth, boolean showFullDateOfBirth) { if (inputDateOfBirth != null) { uk.gov.companieshouse.api.psc.DateOfBirth dateOfBirth = new uk.gov.companieshouse.api.psc.DateOfBirth(); - if (registerView) { + if (showFullDateOfBirth) { dateOfBirth.setDay(inputDateOfBirth.getDay()); } else { dateOfBirth.setDay(null); diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiServiceTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiServiceTest.java index b728ecda..22864d41 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiServiceTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiServiceTest.java @@ -4,7 +4,6 @@ import com.google.api.client.http.HttpResponseException; import org.assertj.core.api.Assertions; import org.junit.Assert; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -40,19 +39,11 @@ public class ChsKafkaApiServiceTest { private PrivateChangedResourcePost privateChangedResourcePost; @Mock private ApiResponse response; - - private TestHelper testHelper; @InjectMocks private ChsKafkaApiService chsKafkaApiService; - @Captor ArgumentCaptor changedResourceCaptor; - @BeforeEach - void setUp() { - testHelper = new TestHelper(); - } - @Test void invokeChsKafkaEndpoint() throws ApiErrorResponseException { when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/controller/CompanyPscControllerTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/controller/CompanyPscControllerTest.java index f55505cd..7eb4639b 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/controller/CompanyPscControllerTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/controller/CompanyPscControllerTest.java @@ -1,6 +1,5 @@ package uk.gov.companieshouse.pscdataapi.controller; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -13,22 +12,10 @@ import uk.gov.companieshouse.api.exception.ResourceNotFoundException; import uk.gov.companieshouse.api.exception.ServiceUnavailableException; import uk.gov.companieshouse.api.psc.*; -import uk.gov.companieshouse.pscdataapi.models.Links; -import uk.gov.companieshouse.pscdataapi.models.PscDocument; -import uk.gov.companieshouse.pscdataapi.models.Updated; import uk.gov.companieshouse.pscdataapi.service.CompanyPscService; import uk.gov.companieshouse.pscdataapi.util.TestHelper; -import java.time.LocalDate; -import java.time.OffsetDateTime; -import java.time.format.DateTimeFormatter; - -import java.util.Collections; -import java.util.Optional; - import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.*; import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; @@ -39,36 +26,9 @@ @SpringBootTest @AutoConfigureMockMvc class CompanyPscControllerTest { - private static final String PUT_URL = - "/company/123456789/persons-with-significant-control/123456789/full_record"; - - private static final String GET_List_Summary_URL = - "/company/123456789/persons-with-significant-control/"; - private static final String GET_URL = - "/company/123456789/persons-with-significant-control/individual/123456789"; - - private static final String GET_IndividualBeneficialOwner_URL = - "/company/123456789/persons-with-significant-control/individual-beneficial-owner/123456789"; - - private static final String GET_SuperSecure_URL = - "/company/123456789/persons-with-significant-control/super-secure/123456789"; - - private static final String GET_CorporateEntity_URL = - "/company/123456789/persons-with-significant-control/corporate-entity/123456789"; - - private static final String GET_CorporateEntityBeneficialOwner_URL = - "/company/123456789/persons-with-significant-control/corporate-entity-beneficial-owner/123456789"; - - private static final String GET_Legal_Person_URL = - "/company/123456789/persons-with-significant-control/legal-person/123456789"; - private static final String GET_Legal_Person_Beneficial_Owner_URL = - "/company/123456789/persons-with-significant-control/legal-person-beneficial-owner/123456789"; - - private static final String GET_SuperSecureBeneficialOwner_URL = - "/company/123456789/persons-with-significant-control/super-secure-beneficial-owner/123456789"; private static final String X_REQUEST_ID = "123456"; - private static final String MOCK_COMPANY_NUMBER = "123456789"; + private static final String MOCK_COMPANY_NUMBER = "1234567"; private static final String MOCK_NOTIFICATION_ID = "123456789"; private static final Boolean MOCK_REGISTER_VIEW_TRUE = true; private static final Boolean MOCK_REGISTER_VIEW_FALSE = false; @@ -78,25 +38,28 @@ class CompanyPscControllerTest { private static final String ERIC_PRIVILEGES = "*"; private static final String ERIC_AUTH = "internal-app"; - private static final String DELETE_URL = String.format("/company/%s/persons-with-significant-control/%s/full_record", MOCK_COMPANY_NUMBER, MOCK_COMPANY_NUMBER); - - private FullRecordCompanyPSCApi request; - private PscDocument document; - - private SuperSecure superSecure; - private SuperSecureBeneficialOwner superSecureBeneficialOwner; - private Individual individual; - private IndividualBeneficialOwner individualBeneficialOwner; - private CorporateEntity corporateEntity; - private CorporateEntityBeneficialOwner corporateEntityBeneficialOwner; - private LegalPerson legalPerson; - private LegalPersonBeneficialOwner legalPersonBeneficialOwner; - - private ListSummary listSummary; - - private TestHelper testHelper; - - private String dateString; + private static final String PUT_URL = String.format( + "/company/%s/persons-with-significant-control/%s/full_record", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_Individual_URL = String.format( + "/company/%s/persons-with-significant-control/individual/%s", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_IndividualBeneficialOwner_URL = String.format( + "/company/%s/persons-with-significant-control/individual-beneficial-owner/%s", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_CorporateEntity_URL = String.format( + "/company/%s/persons-with-significant-control/corporate-entity/%s", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_CorporateEntityBeneficialOwner_URL = String.format( + "/company/%s/persons-with-significant-control/corporate-entity-beneficial-owner/%s", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_Legal_Person_URL = String.format( + "/company/%s/persons-with-significant-control/legal-person/%s", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_Legal_Person_Beneficial_Owner_URL = String.format( + "/company/%s/persons-with-significant-control/legal-person-beneficial-owner/%s", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_SuperSecure_URL = String.format( + "/company/%s/persons-with-significant-control/super-secure/%s", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_SuperSecureBeneficialOwner_URL = String.format( + "/company/%s/persons-with-significant-control/super-secure-beneficial-owner/%s", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); + private static final String GET_List_Summary_URL = String.format( + "/company/%s/persons-with-significant-control/", MOCK_COMPANY_NUMBER); + private static final String DELETE_URL = String.format( + "/company/%s/persons-with-significant-control/%s/full_record", MOCK_COMPANY_NUMBER, MOCK_NOTIFICATION_ID); @MockBean private CompanyPscService companyPscService; @@ -105,37 +68,11 @@ class CompanyPscControllerTest { @Autowired private CompanyPscController companyPscController; - @BeforeEach - public void setUp() { - testHelper = new TestHelper(); - OffsetDateTime date = OffsetDateTime.now(); - request = new FullRecordCompanyPSCApi(); - InternalData internal = new InternalData(); - ExternalData external = new ExternalData(); - Data data = new Data(); - external.setNotificationId(MOCK_NOTIFICATION_ID); - external.setData(data); - data.setKind("kind"); - internal.setDeltaAt(date); - request.setInternalData(internal); - request.setExternalData(external); - document = new PscDocument(); - document.setCompanyNumber(MOCK_COMPANY_NUMBER); - document.setNotificationId(MOCK_NOTIFICATION_ID); - document.setUpdated(new Updated().setAt(LocalDate.now())); - final DateTimeFormatter dateTimeFormatter = - DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSS"); - dateString = date.format(dateTimeFormatter); - - - } - @Test void contextLoads() { assertThat(companyPscController).isNotNull(); } - @Test void callPutRequest() throws Exception { doNothing() @@ -178,7 +115,7 @@ void getSuperSecurePSCWhenNoApiKeyPresent() throws Exception { @DisplayName( "GET request returns a 200 response when Super Secure PSC found") void getSuperSecurePSCFound() throws Exception { - when(companyPscService.getSuperSecurePsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)).thenReturn(superSecure); + when(companyPscService.getSuperSecurePsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)).thenReturn(new SuperSecure()); mockMvc.perform(get(GET_SuperSecure_URL) .header("ERIC-Identity", ERIC_IDENTITY) @@ -244,7 +181,7 @@ void getSuperSecureBeneficialOwnerPSCWhenNoApiKeyPresent() throws Exception { @DisplayName( "GET request returns a 200 response when Super Secure Beneficial Owner PSC found") void getSuperSecureBeneficialOwnerPSCFound() throws Exception { - when(companyPscService.getSuperSecureBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)).thenReturn(superSecureBeneficialOwner); + when(companyPscService.getSuperSecureBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)).thenReturn(new SuperSecureBeneficialOwner()); mockMvc.perform(get(GET_SuperSecureBeneficialOwner_URL) .header("ERIC-Identity", ERIC_IDENTITY) @@ -310,7 +247,7 @@ void getCorporateEntityPSCWhenNoApiKeyPresent() throws Exception { @DisplayName( "GET request returns a 200 response when Corporate Entity PSC found") void getCorporateEntityPSCFound() throws Exception { - when(companyPscService.getCorporateEntityPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)).thenReturn(corporateEntity); + when(companyPscService.getCorporateEntityPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)).thenReturn(new CorporateEntity()); mockMvc.perform(get(GET_CorporateEntity_URL) .header("ERIC-Identity", ERIC_IDENTITY) @@ -329,7 +266,7 @@ void getCorporateEntityPSCFound() throws Exception { @DisplayName( "GET request returns a 200 response when Corporate Entity PSC found") void getCorporateEntityPSCWithoutPrivileges() throws Exception { - when(companyPscService.getCorporateEntityPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)).thenReturn(corporateEntity); + when(companyPscService.getCorporateEntityPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)).thenReturn(new CorporateEntity()); mockMvc.perform(get(GET_CorporateEntity_URL) .header("ERIC-Identity", ERIC_IDENTITY) @@ -448,7 +385,7 @@ void callPscDeleteRequestWhenServiceUnavailableAndReturn503() throws Exception { @Test @DisplayName("Return 401 when no api key is present") void getPSCWhenNoApiKeyPresent() throws Exception { - mockMvc.perform(get(GET_URL)).andExpect(status().isUnauthorized()); + mockMvc.perform(get(GET_Individual_URL)).andExpect(status().isUnauthorized()); verify(companyPscService ,times(0)) .getIndividualPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID, MOCK_REGISTER_VIEW_TRUE); @@ -458,9 +395,9 @@ void getPSCWhenNoApiKeyPresent() throws Exception { @DisplayName( "GET request returns a 200 response when Individual PSC found") void getIndividualPSCFound() throws Exception { - when(companyPscService.getIndividualPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID, MOCK_REGISTER_VIEW_FALSE)).thenReturn(individual); + when(companyPscService.getIndividualPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID, MOCK_REGISTER_VIEW_FALSE)).thenReturn(new Individual()); - mockMvc.perform(get(GET_URL) + mockMvc.perform(get(GET_Individual_URL) .header("ERIC-Identity", ERIC_IDENTITY) .header("ERIC-Identity-Type", ERIC_IDENTITY_TYPE) .contentType(APPLICATION_JSON) @@ -479,7 +416,7 @@ void getIndividualPSCFound() throws Exception { void getIndividualPSCDocumentWhenServiceIsDown() throws Exception { when(companyPscService.getIndividualPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID, MOCK_REGISTER_VIEW_TRUE)).thenThrow(ServiceUnavailableException.class); - mockMvc.perform(get(GET_URL) + mockMvc.perform(get(GET_Individual_URL) .header("ERIC-Identity", ERIC_IDENTITY) .header("ERIC-Identity-Type", ERIC_IDENTITY_TYPE) .param("register_view", "true") @@ -499,7 +436,7 @@ void getIndividualPSCDocumentWhenServiceIsDown() throws Exception { void getIndividualPSCDocumentWhenResourceNotFound() throws Exception { when(companyPscService.getIndividualPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID, MOCK_REGISTER_VIEW_TRUE)).thenThrow(ResourceNotFoundException.class); - mockMvc.perform(get(GET_URL) + mockMvc.perform(get(GET_Individual_URL) .header("ERIC-Identity", ERIC_IDENTITY) .header("ERIC-Identity-Type", ERIC_IDENTITY_TYPE) .param("register_view", "true") @@ -530,7 +467,7 @@ void getIndividualBeneficialOwnerPSCWhenNoApiKeyPresent() throws Exception { void getIndividualBeneficialOwnerPSCFound() throws Exception { when(companyPscService .getIndividualBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID,MOCK_REGISTER_VIEW_FALSE)) - .thenReturn(individualBeneficialOwner); + .thenReturn(new IndividualBeneficialOwner()); mockMvc.perform(get(GET_IndividualBeneficialOwner_URL) .header("ERIC-Identity", ERIC_IDENTITY) @@ -594,7 +531,7 @@ void getIndividualBeneficialOwnerPSCDocumentWhenResourceNotFound() throws Except void getCorporateBeneficialOwnerPSCFound() throws Exception { when(companyPscService .getCorporateEntityBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)) - .thenReturn(corporateEntityBeneficialOwner); + .thenReturn(new CorporateEntityBeneficialOwner()); mockMvc.perform(get(GET_CorporateEntityBeneficialOwner_URL) .header("ERIC-Identity", ERIC_IDENTITY) @@ -657,7 +594,7 @@ void getCorporateEntityBeneficialOwnerPSCDocumentWhenResourceNotFound() throws E void getLegalPSCFound() throws Exception { when(companyPscService .getLegalPersonPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)) - .thenReturn(legalPerson); + .thenReturn(new LegalPerson()); mockMvc.perform(get(GET_Legal_Person_URL) .header("ERIC-Identity", ERIC_IDENTITY) @@ -719,7 +656,7 @@ void getLegalPersonPSCDocumentWhenResourceNotFound() throws Exception { void getLegalPersonBeneficialOwnerPSCFound() throws Exception { when(companyPscService .getLegalPersonBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,MOCK_NOTIFICATION_ID)) - .thenReturn(legalPersonBeneficialOwner); + .thenReturn(new LegalPersonBeneficialOwner()); mockMvc.perform(get(GET_Legal_Person_Beneficial_Owner_URL) .header("ERIC-Identity", ERIC_IDENTITY) @@ -779,7 +716,7 @@ void getLegalPersonBeneficialOwnerPSCDocumentWhenResourceNotFound() throws Excep @Test void callPscListGetRequestWithParams() throws Exception { when(companyPscService.retrievePscListSummaryFromDb(MOCK_COMPANY_NUMBER, 2, false, 5)) - .thenReturn(testHelper.createPscList()); + .thenReturn(new PscList()); mockMvc.perform(MockMvcRequestBuilders .get(GET_List_Summary_URL) @@ -798,7 +735,7 @@ void callPscListGetRequestWithParams() throws Exception { @Test void callPscListGetRequestWithRegisterView() throws Exception { when(companyPscService.retrievePscListSummaryFromDb(MOCK_COMPANY_NUMBER, 2, true, 5)) - .thenReturn(testHelper.createPscList()); + .thenReturn(new PscList()); mockMvc.perform(MockMvcRequestBuilders .get(GET_List_Summary_URL) @@ -817,7 +754,7 @@ void callPscListGetRequestWithRegisterView() throws Exception { @Test void callPscListGetRequestNoParams() throws Exception { when(companyPscService.retrievePscListSummaryFromDb(MOCK_COMPANY_NUMBER, 0, false, 25)) - .thenReturn(testHelper.createPscList()); + .thenReturn(new PscList()); mockMvc.perform(MockMvcRequestBuilders .get(GET_List_Summary_URL) diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverterTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverterTest.java index 51dc315a..3d4a2aaa 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverterTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverterTest.java @@ -2,18 +2,17 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.bson.Document; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import uk.gov.companieshouse.api.psc.FullRecordCompanyPSCApi; import uk.gov.companieshouse.pscdataapi.exceptions.FailedToConvertException; +import uk.gov.companieshouse.pscdataapi.util.TestHelper; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; class CompanyPscReadConverterTest { - private static final String NOTIFICATION_ID = "123456"; - private CompanyPscReadConverter converter; @BeforeEach @@ -23,11 +22,12 @@ void setUp() { @Test void canConvertDocument() { - Document notificationIdNode = new Document("notification_id", NOTIFICATION_ID); + Document notificationIdNode = new Document("notification_id", TestHelper.NOTIFICATION_ID); Document document = new Document("external_data", notificationIdNode); FullRecordCompanyPSCApi pscApi = converter.convert(document); - assertEquals(NOTIFICATION_ID, pscApi.getExternalData().getNotificationId()); + Assertions.assertNotNull(pscApi); + Assertions.assertEquals(TestHelper.NOTIFICATION_ID, pscApi.getExternalData().getNotificationId()); } @Test diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverterTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverterTest.java index 05ac4d5c..4ebf238d 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverterTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverterTest.java @@ -2,18 +2,18 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.mongodb.BasicDBObject; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import uk.gov.companieshouse.pscdataapi.models.PscDocument; import uk.gov.companieshouse.pscdataapi.exceptions.FailedToConvertException; +import uk.gov.companieshouse.pscdataapi.util.TestHelper; + import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; class CompanyPscWriteConverterTest { - private static final String PSC_ID = "pscId"; - private CompanyPscWriteConverter converter; @BeforeEach @@ -24,12 +24,13 @@ void setUp() { @Test void canConvertDocument() { PscDocument document = new PscDocument(); - document.setNotificationId(PSC_ID); + document.setNotificationId(TestHelper.PSC_ID); BasicDBObject object = converter.convert(document); + Assertions.assertNotNull(object); String json = object.toJson(); - assertTrue(json.contains(PSC_ID)); + Assertions.assertTrue(json.contains(TestHelper.PSC_ID)); } @Test diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscServiceTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscServiceTest.java index 39f66eca..156eed9f 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscServiceTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscServiceTest.java @@ -1,6 +1,5 @@ package uk.gov.companieshouse.pscdataapi.service; -import java.time.LocalDate; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; @@ -8,6 +7,7 @@ import java.util.Collections; import java.util.List; import java.util.Optional; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -21,6 +21,7 @@ import uk.gov.companieshouse.api.api.CompanyMetricsApiService; import uk.gov.companieshouse.api.exception.ResourceNotFoundException; import uk.gov.companieshouse.api.metrics.MetricsApi; +import uk.gov.companieshouse.api.metrics.RegisterApi; import uk.gov.companieshouse.api.metrics.RegistersApi; import uk.gov.companieshouse.api.psc.*; import uk.gov.companieshouse.logging.Logger; @@ -32,7 +33,6 @@ import uk.gov.companieshouse.pscdataapi.util.TestHelper; import static com.mongodb.internal.connection.tlschannel.util.Util.assertTrue; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; @@ -43,76 +43,43 @@ @ExtendWith(MockitoExtension.class) class CompanyPscServiceTest { - private static final String NOTIFICATION_ID = "pscId"; - - private static final String MOCK_COMPANY_NUMBER = "1234567"; - - private static final Boolean MOCK_REGISTER_TRUE = true; - - private static final Boolean MOCK_REGISTER_FALSE = false; + private static final String NOTIFICATION_ID = TestHelper.NOTIFICATION_ID; + private static final String COMPANY_NUMBER = TestHelper.COMPANY_NUMBER; + private static final Boolean REGISTER_VIEW_TRUE = true; + private static final Boolean REGISTER_VIEW_FALSE = false; + private static final boolean SHOW_FULL_DOB_TRUE = true; + private static final boolean SHOW_FULL_DOB_FALSE = false; @Mock private Logger logger; - @Mock private CompanyPscRepository repository; - @Mock private CompanyPscTransformer transformer; - @Mock private ChsKafkaApiService chsKafkaApiService; - @Captor private ArgumentCaptor dateCaptor; - @Spy @InjectMocks private CompanyPscService service; - @Mock CompanyMetricsApiService companyMetricsApiService; private FullRecordCompanyPSCApi request; private PscDocument document; private String dateString; - - private TestHelper testHelper; + private OffsetDateTime date; + private OffsetDateTime laterDate; @BeforeEach public void setUp() { - testHelper = new TestHelper(); - OffsetDateTime date = OffsetDateTime.now(); - request = new FullRecordCompanyPSCApi(); - InternalData internal = new InternalData(); - ExternalData external = new ExternalData(); - Data data = new Data(); - external.setNotificationId(NOTIFICATION_ID); - external.setData(data); - data.setKind("individual-person-with-significant-control"); - internal.setDeltaAt(date); - request.setInternalData(internal); - request.setExternalData(external); - document = new PscDocument(); - document.setUpdated(new Updated().setAt(LocalDate.now())); - final DateTimeFormatter dateTimeFormatter = - DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSS"); - dateString = date.format(dateTimeFormatter); - document.setCompanyNumber(MOCK_COMPANY_NUMBER); - document.setPscId("1234"); - PscData pscData = new PscData(); - pscData.setKind("individual-person-with-significant-control"); - document.setNotificationId(MOCK_COMPANY_NUMBER); - document.setData(pscData); - Identification identification = new Identification(); - identification.setCountryRegistered("x"); - identification.setLegalForm("x"); - identification.setPlaceRegistered("x"); - identification.setLegalAuthority("x"); - identification.setRegistrationNumber("x"); - document.setIdentification(new PscIdentification(identification)); - + date = TestHelper.createOffsetDateTime(); + dateString = date.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSS")); + laterDate = TestHelper.createLaterOffsetDateTime(); + request = TestHelper.buildBasicFullRecordPsc(); + document = TestHelper.buildBasicDocument(); } @Test @@ -124,7 +91,7 @@ void insertBrandNewPscRecordSavesPsc() { service.insertPscRecord("", request); verify(repository).save(document); - assertEquals(dateString, dateCaptor.getValue()); + Assertions.assertEquals(dateString, dateCaptor.getValue()); assertNotNull(document.getCreated().getAt()); } @@ -140,9 +107,9 @@ void insertUpdatePscRecordSavesPsc() { service.insertPscRecord("", request); verify(repository).save(document); - assertEquals(dateString, dateCaptor.getValue()); + Assertions.assertEquals(dateString, dateCaptor.getValue()); assertNotNull(document.getCreated().getAt()); - assertEquals(date, document.getCreated().getAt()); + Assertions.assertEquals(date, document.getCreated().getAt()); } @Test @@ -155,7 +122,7 @@ void insertPscRecordDoesNotSavePscWhenUpdateAlreadyMade() { service.insertPscRecord("", request); verify(repository, times(0)).save(document); - assertEquals(dateString, dateCaptor.getValue()); + Assertions.assertEquals(dateString, dateCaptor.getValue()); } @Test @@ -183,10 +150,10 @@ void insertNewCreatedWhenCreatedCallToMongoFails() { @Test @DisplayName("When company number & notification id is provided, delete PSC") public void testDeletePSC() { - when(repository.getPscByCompanyNumberAndId("1234567",NOTIFICATION_ID)).thenReturn(Optional.ofNullable(document)); - service.deletePsc("1234567",NOTIFICATION_ID); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.ofNullable(document)); + service.deletePsc(COMPANY_NUMBER,NOTIFICATION_ID); - verify(repository, times(1)).getPscByCompanyNumberAndId("1234567",NOTIFICATION_ID); + verify(repository, times(1)).getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID); verify(repository, times(1)).delete(document); } @@ -213,64 +180,254 @@ public void testDeletePSCThrowsResourceNotFoundExceptionWhenCompanyNumberAndNoti } @Test - public void GetIndividualPscReturn200WhenRegisterViewIsTrue() { - Individual individual = new Individual(); - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)) + public void GetIndividualPscReturns404WhenRegisterViewIsTrueAndNoMetrics() { + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) .thenReturn(Optional.of(document)); - when(transformer.transformPscDocToIndividual(document, MOCK_REGISTER_TRUE)).thenReturn(individual); - Individual result = service.getIndividualPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID, MOCK_REGISTER_TRUE); + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> + service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE)); + String expectedErrorMessage = "404 NOT_FOUND \"No company metrics data found for company number: " + COMPANY_NUMBER +"\""; + Assertions.assertEquals(expectedErrorMessage, exception.getMessage()); + } - assertEquals(individual,result); + @Test + public void GetIndividualPscReturns404WhenRegisterViewIsTrueAndEmptyMetrics() { + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(new MetricsApi())); + + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> + service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE)); + String expectedErrorMessage = "404 NOT_FOUND \"not-on-public-register\""; + Assertions.assertEquals(expectedErrorMessage, exception.getMessage()); } @Test - public void GetIndividualPscReturn200WhenRegisterViewIsFalse() { + public void GetIndividualPscReturns404WhenRegisterViewIsTrueAndWrongRegisterMovedTo() { + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + MetricsApi metrics = new MetricsApi(); + RegistersApi registers = new RegistersApi(); + registers.setPersonsWithSignificantControl(new RegisterApi().registerMovedTo("wrong")); + metrics.setRegisters(registers); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(metrics)); + + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> + service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE)); + String expectedErrorMessage = "404 NOT_FOUND \"not-on-public-register\""; + Assertions.assertEquals(expectedErrorMessage, exception.getMessage()); + } + + @Test + public void GetIndividualPscReturns404WhenRegisterViewIsTrueAndOnPublicRegisterAndNoMovedToDate() { + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + MetricsApi metrics = new MetricsApi(); + RegistersApi registers = new RegistersApi(); + registers.setPersonsWithSignificantControl(new RegisterApi().registerMovedTo("public-register")); + metrics.setRegisters(registers); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(metrics)); + + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> + service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE)); + String expectedErrorMessage = "404 NOT_FOUND \"not-on-public-register\""; + Assertions.assertEquals(expectedErrorMessage, exception.getMessage()); + } + + @Test + public void GetIndividualPscReturns404WhenRegisterViewIsTrueAndOnPublicRegisterAndMovedToDateAndNoCeasedOn() { + PscData pscData = document.getData(); + pscData.setCeased(true); + document.setData(pscData); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + + MetricsApi metrics = new MetricsApi(); + RegistersApi registers = new RegistersApi(); + RegisterApi pscRegisters = new RegisterApi(); + pscRegisters.registerMovedTo("public-register"); + pscRegisters.setMovedOn(date); + registers.setPersonsWithSignificantControl(pscRegisters); + metrics.setRegisters(registers); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(metrics)); + + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> + service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE)); + String expectedErrorMessage = "404 NOT_FOUND \"not-on-public-register\""; + Assertions.assertEquals(expectedErrorMessage, exception.getMessage()); + } + + @Test + public void GetIndividualPscReturns404WhenRegisterViewIsTrueAndOnPublicRegisterAndMovedToDateEqualsCeasedOn() { + PscData pscData = document.getData(); + pscData.setCeased(true); + pscData.setCeasedOn(date.toLocalDate()); + document.setData(pscData); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + + MetricsApi metrics = new MetricsApi(); + RegistersApi registers = new RegistersApi(); + RegisterApi pscRegisters = new RegisterApi(); + pscRegisters.registerMovedTo("public-register"); + pscRegisters.setMovedOn(date); + registers.setPersonsWithSignificantControl(pscRegisters); + metrics.setRegisters(registers); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(metrics)); + + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> + service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE)); + String expectedErrorMessage = "404 NOT_FOUND \"not-on-public-register\""; + Assertions.assertEquals(expectedErrorMessage, exception.getMessage()); + } + + @Test + public void GetIndividualPscReturns404WhenRegisterViewIsTrueAndOnPublicRegisterAndMovedToDateAfterCeasedOn() { + PscData pscData = document.getData(); + pscData.setCeased(true); + pscData.setCeasedOn(date.toLocalDate()); + document.setData(pscData); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + + MetricsApi metrics = new MetricsApi(); + RegistersApi registers = new RegistersApi(); + RegisterApi pscRegisters = new RegisterApi(); + pscRegisters.registerMovedTo("public-register"); + pscRegisters.setMovedOn(laterDate); + registers.setPersonsWithSignificantControl(pscRegisters); + metrics.setRegisters(registers); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(metrics)); + + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> + service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE)); + String expectedErrorMessage = "404 NOT_FOUND \"not-on-public-register\""; + Assertions.assertEquals(expectedErrorMessage, exception.getMessage()); + } + + @Test + public void GetIndividualPscReturns200WhenRegisterViewIsTrueAndOnPublicRegisterAndMovedToDateBeforeCeasedOn() { + PscData pscData = document.getData(); + pscData.setCeased(true); + pscData.setCeasedOn(laterDate.toLocalDate()); + document.setData(pscData); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + + MetricsApi metrics = new MetricsApi(); + RegistersApi registers = new RegistersApi(); + RegisterApi pscRegisters = new RegisterApi(); + pscRegisters.registerMovedTo("public-register"); + pscRegisters.setMovedOn(date); + registers.setPersonsWithSignificantControl(pscRegisters); + metrics.setRegisters(registers); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(metrics)); + Individual individual = new Individual(); - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)) + when(transformer.transformPscDocToIndividual(document, SHOW_FULL_DOB_TRUE)).thenReturn(individual); + + Individual result = service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE); + Assertions.assertEquals(individual, result); + } + + @Test + public void GetIndividualPscReturn200WhenRegisterViewIsFalse() { + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) .thenReturn(Optional.of(document)); - when(transformer.transformPscDocToIndividual(document, MOCK_REGISTER_FALSE)).thenReturn(individual); - Individual result = service.getIndividualPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID, MOCK_REGISTER_FALSE); + Individual individual = new Individual(); + when(transformer.transformPscDocToIndividual(document, SHOW_FULL_DOB_FALSE)).thenReturn(individual); - assertEquals(individual,result); + Individual result = service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_FALSE); + Assertions.assertEquals(individual, result); } @Test public void GetIndividualPscReturn404() { - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)) + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getIndividualPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID,MOCK_REGISTER_FALSE)); + assertThrows(ResourceNotFoundException.class, () -> service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_FALSE)); } @Test public void GetWrongTypePscReturn404() { - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)) + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getIndividualPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID,MOCK_REGISTER_FALSE)); + assertThrows(ResourceNotFoundException.class, () -> service.getIndividualPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_FALSE)); + } + + @Test + public void GetIndividualBeneficialOwnerPscReturns400WhenRegisterViewIsTrueAndOnPublicRegisterAndMovedToDateEqualsCeasedOn() { + PscData pscData = document.getData(); + pscData.setCeased(true); + pscData.setCeasedOn(date.toLocalDate()); + pscData.setKind(TestHelper.INDIVIDUAL_BO_KIND); + document.setData(pscData); + when(repository.findById(NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + + MetricsApi metrics = new MetricsApi(); + RegistersApi registers = new RegistersApi(); + RegisterApi pscRegisters = new RegisterApi(); + pscRegisters.registerMovedTo("public-register"); + pscRegisters.setMovedOn(date); + registers.setPersonsWithSignificantControl(pscRegisters); + metrics.setRegisters(registers); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(metrics)); + + ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> + service.getIndividualBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE)); + String expectedErrorMessage = "404 NOT_FOUND \"not-on-public-register\""; + Assertions.assertEquals(expectedErrorMessage, exception.getMessage()); + } + + @Test + public void GetIndividualBeneficialOwnerPscReturns200WhenRegisterViewIsTrueAndOnPublicRegisterAndMovedToDateBeforeCeasedOn() { + PscData pscData = document.getData(); + pscData.setCeased(true); + pscData.setCeasedOn(laterDate.toLocalDate()); + pscData.setKind(TestHelper.INDIVIDUAL_BO_KIND); + document.setData(pscData); + when(repository.findById(NOTIFICATION_ID)) + .thenReturn(Optional.of(document)); + + MetricsApi metrics = new MetricsApi(); + RegistersApi registers = new RegistersApi(); + RegisterApi pscRegisters = new RegisterApi(); + pscRegisters.registerMovedTo("public-register"); + pscRegisters.setMovedOn(date); + registers.setPersonsWithSignificantControl(pscRegisters); + metrics.setRegisters(registers); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)).thenReturn(Optional.of(metrics)); + + IndividualBeneficialOwner individualBo = new IndividualBeneficialOwner(); + when(transformer.transformPscDocToIndividualBeneficialOwner(document, SHOW_FULL_DOB_TRUE)).thenReturn(individualBo); + + IndividualBeneficialOwner result = service.getIndividualBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_TRUE); + Assertions.assertEquals(individualBo, result); } @Test - public void GetIndividualBeneficialOwnerPscReturn200() { - document.getData().setKind("individual-beneficial-owner"); + public void GetIndividualBeneficialOwnerPscReturn200WhenRegisterViewIsFalse() { + document.getData().setKind(TestHelper.INDIVIDUAL_BO_KIND); IndividualBeneficialOwner individualBeneficialOwner = new IndividualBeneficialOwner(); when(repository.findById(NOTIFICATION_ID)).thenReturn(Optional.of(document)); - when(transformer.transformPscDocToIndividualBeneficialOwner(document,MOCK_REGISTER_FALSE)) + when(transformer.transformPscDocToIndividualBeneficialOwner(document, SHOW_FULL_DOB_FALSE)) .thenReturn(individualBeneficialOwner); IndividualBeneficialOwner result = service - .getIndividualBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID,MOCK_REGISTER_FALSE); + .getIndividualBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_FALSE); - assertEquals(individualBeneficialOwner,result); + Assertions.assertEquals(individualBeneficialOwner, result); } @Test public void GetIndividualBeneficialOwnerPscReturn404() { when(repository.findById(NOTIFICATION_ID)).thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getIndividualBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID,MOCK_REGISTER_FALSE)); + assertThrows(ResourceNotFoundException.class, () -> service.getIndividualBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID, REGISTER_VIEW_FALSE)); } @Test @@ -280,7 +437,7 @@ public void GetWrongTypeIndividualBeneficialOwnerPscReturn404() { .equals("WRONG KIND"))) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getIndividualBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID,MOCK_REGISTER_FALSE)); + assertThrows(ResourceNotFoundException.class, () -> service.getIndividualBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID,REGISTER_VIEW_FALSE)); } @Test public void GetCorporateEntityBeneficialOwnerPscReturn200() { @@ -292,16 +449,16 @@ public void GetCorporateEntityBeneficialOwnerPscReturn200() { .thenReturn(corporateEntityBeneficialOwner); CorporateEntityBeneficialOwner result = service - .getCorporateEntityBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID); + .getCorporateEntityBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID); - assertEquals(corporateEntityBeneficialOwner,result); + Assertions.assertEquals(corporateEntityBeneficialOwner, result); } @Test public void GetCorporateEntityBeneficialOwnerPscReturn404() { when(repository.findById(NOTIFICATION_ID)).thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getCorporateEntityBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getCorporateEntityBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID)); } @Test @@ -311,7 +468,7 @@ public void GetWrongTypeCorporateBeneficialOwnerPscReturn404() { .equals("WRONG KIND"))) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getCorporateEntityBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getCorporateEntityBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID)); } @Test @@ -324,98 +481,98 @@ public void GetLegalPersonPscReturn200() { .thenReturn(legalPerson); LegalPerson result = service - .getLegalPersonPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID); + .getLegalPersonPsc(COMPANY_NUMBER,NOTIFICATION_ID); - assertEquals(legalPerson,result); + Assertions.assertEquals(legalPerson, result); } @Test public void GetLegalPersonPscReturn404() { when(repository.findById(NOTIFICATION_ID)).thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getLegalPersonPsc(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getLegalPersonPsc(COMPANY_NUMBER, NOTIFICATION_ID)); } @Test public void GetSuperSecurePscReturn200() { document.getData().setKind("super-secure-person-with-significant-control"); SuperSecure superSecure = new SuperSecure(); - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.of(document)); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.of(document)); when(transformer.transformPscDocToSuperSecure(document)) .thenReturn(superSecure); SuperSecure result = service - .getSuperSecurePsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID); + .getSuperSecurePsc(COMPANY_NUMBER,NOTIFICATION_ID); - assertEquals(superSecure,result); + Assertions.assertEquals(superSecure, result); } @Test public void GetSuperSecurePscReturn404() { - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.empty()); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getSuperSecurePsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getSuperSecurePsc(COMPANY_NUMBER,NOTIFICATION_ID)); } @Test public void GetWrongTypeSuperSecurePscReturn404() { - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER,NOTIFICATION_ID) + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID) .filter(document -> document.getData().getKind() .equals("WRONG KIND"))) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getSuperSecurePsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getSuperSecurePsc(COMPANY_NUMBER,NOTIFICATION_ID)); } @Test public void GetSuperSecureBeneficialOwnerPscReturn200() { document.getData().setKind("super-secure-beneficial-owner"); SuperSecureBeneficialOwner superSecureBeneficialOwner = new SuperSecureBeneficialOwner(); - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.of(document)); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.of(document)); when(transformer.transformPscDocToSuperSecureBeneficialOwner(document)) .thenReturn(superSecureBeneficialOwner); SuperSecureBeneficialOwner result = service - .getSuperSecureBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID); + .getSuperSecureBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID); - assertEquals(superSecureBeneficialOwner,result); + Assertions.assertEquals(superSecureBeneficialOwner, result); } @Test public void GetSuperSecureBeneficialOwnerPscReturn404() { - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.empty()); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getSuperSecureBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getSuperSecureBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID)); } @Test public void GetWrongTypeSuperSecureBeneficialOwnerPscReturn404() { - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER,NOTIFICATION_ID) + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID) .filter(document -> document.getData().getKind() .equals("WRONG KIND"))) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getSuperSecureBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getSuperSecureBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID)); } @Test public void GetCorporateEntityPscReturn200() { document.getData().setKind("corporate-entity-person-with-significant-control"); CorporateEntity corporateEntity = new CorporateEntity(); - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.of(document)); + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER,NOTIFICATION_ID)).thenReturn(Optional.of(document)); when(transformer.transformPscDocToCorporateEntity(document)) .thenReturn(corporateEntity); - CorporateEntity result = service.getCorporateEntityPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID); + CorporateEntity result = service.getCorporateEntityPsc(COMPANY_NUMBER,NOTIFICATION_ID); - assertEquals(corporateEntity,result); + Assertions.assertEquals(corporateEntity, result); } @Test public void GetCorporateEntityPscReturn404() { - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)) + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getCorporateEntityPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getCorporateEntityPsc(COMPANY_NUMBER,NOTIFICATION_ID)); } @Test @@ -425,15 +582,15 @@ public void GetWrongTypeLegalPersonPscReturn404() { .equals("WRONG KIND"))) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getLegalPersonPsc(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getLegalPersonPsc(COMPANY_NUMBER, NOTIFICATION_ID)); } @Test public void GetWrongTypeCorporateEntityPscReturn404() { - when(repository.getPscByCompanyNumberAndId(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)) + when(repository.getPscByCompanyNumberAndId(COMPANY_NUMBER, NOTIFICATION_ID)) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getCorporateEntityPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getCorporateEntityPsc(COMPANY_NUMBER,NOTIFICATION_ID)); } @Test @@ -446,15 +603,15 @@ public void GetLegalPersonBeneficialOwnerPscReturn200() { .thenReturn(legalPersonBeneficialOwner); LegalPersonBeneficialOwner result = service - .getLegalPersonBeneficialOwnerPsc(MOCK_COMPANY_NUMBER,NOTIFICATION_ID); + .getLegalPersonBeneficialOwnerPsc(COMPANY_NUMBER,NOTIFICATION_ID); - assertEquals(legalPersonBeneficialOwner,result); + Assertions.assertEquals(legalPersonBeneficialOwner, result); } @Test public void GetLegalPersonBeneficialOwnerPscReturn404() { when(repository.findById(NOTIFICATION_ID)).thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getLegalPersonBeneficialOwnerPsc(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getLegalPersonBeneficialOwnerPsc(COMPANY_NUMBER, NOTIFICATION_ID)); } @Test @@ -464,18 +621,17 @@ public void GetWrongTypeLegalPersonBeneficialOwnerPscReturn404() { .equals("WRONG KIND"))) .thenReturn(Optional.empty()); - assertThrows(ResourceNotFoundException.class, () -> service.getLegalPersonBeneficialOwnerPsc(MOCK_COMPANY_NUMBER, NOTIFICATION_ID)); + assertThrows(ResourceNotFoundException.class, () -> service.getLegalPersonBeneficialOwnerPsc(COMPANY_NUMBER, NOTIFICATION_ID)); } @Test void whenNoPSCExistGetPSCListShouldThrow() { - assertThrows(ResourceNotFoundException.class, ()-> service.retrievePscListSummaryFromDb( MOCK_COMPANY_NUMBER, 0, false,25)); + assertThrows(ResourceNotFoundException.class, ()-> service.retrievePscListSummaryFromDb( COMPANY_NUMBER, 0, false,25)); } @Test void pscListReturnedByCompanyNumberFromRepository() throws ResourceNotFoundException { - TestHelper testHelper = new TestHelper(); - PscList expectedPscList = testHelper.createPscList(); + PscList expectedPscList = TestHelper.createPscList(); PscData pscData = new PscData(); document.setData(pscData); ListSummary listSummary = new ListSummary(); @@ -487,24 +643,24 @@ void pscListReturnedByCompanyNumberFromRepository() throws ResourceNotFoundExcep identification.setLegalForm("x"); listSummary.setIdentification(identification); - when(companyMetricsApiService.getCompanyMetrics(MOCK_COMPANY_NUMBER)) - .thenReturn(Optional.ofNullable(testHelper.createMetrics())); + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)) + .thenReturn(Optional.of(TestHelper.createMetrics())); when(repository.getPscDocumentList(anyString(), anyInt(), anyInt())).thenReturn(Optional.of(Collections.singletonList(document))); when(transformer.transformPscDocToListSummary(document, false)) .thenReturn(listSummary); - PscList PscDocumentList = service.retrievePscListSummaryFromDb(MOCK_COMPANY_NUMBER,0, false,25); + PscList PscDocumentList = service.retrievePscListSummaryFromDb(COMPANY_NUMBER,0, false,25); - assertEquals(expectedPscList, PscDocumentList); - verify(repository, times(1)).getPscDocumentList(MOCK_COMPANY_NUMBER, 0, 25); + Assertions.assertEquals(expectedPscList, PscDocumentList); + verify(repository, times(1)).getPscDocumentList(COMPANY_NUMBER, 0, 25); } @Test void pscListWithNoMetricsReturnedByCompanyNumberFromRepository() throws ResourceNotFoundException { - PscList expectedPscList = testHelper.createPscListWithNoMetrics(); + PscList expectedPscList = TestHelper.createPscListWithNoMetrics(); PscData pscData = new PscData(); document.setData(pscData); - document.setId("1234"); + document.setId(TestHelper.PSC_ID); ListSummary listSummary = new ListSummary(); Identification identification = new Identification(); @@ -520,43 +676,43 @@ void pscListWithNoMetricsReturnedByCompanyNumberFromRepository() throws Resource when(transformer.transformPscDocToListSummary(document, false)) .thenReturn(listSummary); - PscList PscDocumentList = service.retrievePscListSummaryFromDb(MOCK_COMPANY_NUMBER,0, false,25); + PscList PscDocumentList = service.retrievePscListSummaryFromDb(COMPANY_NUMBER,0, false,25); - assertEquals(expectedPscList, PscDocumentList); - verify(repository, times(1)).getPscDocumentList(MOCK_COMPANY_NUMBER, 0, 25); + Assertions.assertEquals(expectedPscList, PscDocumentList); + verify(repository, times(1)).getPscDocumentList(COMPANY_NUMBER, 0, 25); } @Test void whenNoMetricsDataFoundForCompanyInRegisterViewShouldThrow() throws ResourceNotFoundException { - when(companyMetricsApiService.getCompanyMetrics(MOCK_COMPANY_NUMBER)) + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)) .thenReturn(Optional.empty()); - Exception ex = assertThrows(ResourceNotFoundException.class, () -> service.retrievePscListSummaryFromDb(MOCK_COMPANY_NUMBER,0, true,25)); + Exception ex = assertThrows(ResourceNotFoundException.class, () -> service.retrievePscListSummaryFromDb(COMPANY_NUMBER,0, true,25)); - String expectedMessage = "No company metrics data found for company number: 1234567"; + String expectedMessage = "No company metrics data found for company number: " + COMPANY_NUMBER; String actualMessage = ex.getMessage(); assertNotNull(actualMessage); assertTrue(actualMessage.contains(expectedMessage)); - verify(repository, times(0)).getListSummaryRegisterView(MOCK_COMPANY_NUMBER, 0, OffsetDateTime.parse("2020-12-20T06:00Z"), 25); + verify(repository, times(0)).getListSummaryRegisterView(COMPANY_NUMBER, 0, OffsetDateTime.parse("2020-12-20T06:00Z"), 25); } @Test void whenCompanyNotInPublicRegisterGetPSCListShouldThrow() throws ResourceNotFoundException { - MetricsApi metricsApi = testHelper.createMetrics(); + MetricsApi metricsApi = TestHelper.createMetrics(); RegistersApi registersApi = new RegistersApi(); metricsApi.setRegisters(registersApi); - when(companyMetricsApiService.getCompanyMetrics(MOCK_COMPANY_NUMBER)) + when(companyMetricsApiService.getCompanyMetrics(COMPANY_NUMBER)) .thenReturn(Optional.of(metricsApi)); - Exception ex = assertThrows(ResourceNotFoundException.class, () -> service.retrievePscListSummaryFromDb(MOCK_COMPANY_NUMBER,0, true,25)); + Exception ex = assertThrows(ResourceNotFoundException.class, () -> service.retrievePscListSummaryFromDb(COMPANY_NUMBER,0, true,25)); - String expectedMessage = "company 1234567 not on public register"; + String expectedMessage = "company " + COMPANY_NUMBER + " not on public register"; String actualMessage = ex.getMessage(); assertNotNull(actualMessage); assertTrue(actualMessage.contains(expectedMessage)); - verify(service, times(1)).retrievePscListSummaryFromDb(MOCK_COMPANY_NUMBER,0, true, 25); - verify(repository, times(0)).getListSummaryRegisterView(MOCK_COMPANY_NUMBER, 0, OffsetDateTime.parse("2020-12-20T06:00Z"), 25); + verify(service, times(1)).retrievePscListSummaryFromDb(COMPANY_NUMBER,0, true, 25); + verify(repository, times(0)).getListSummaryRegisterView(COMPANY_NUMBER, 0, OffsetDateTime.parse("2020-12-20T06:00Z"), 25); } } \ No newline at end of file diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformerTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformerTest.java index 95a8903c..a23bb7cd 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformerTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformerTest.java @@ -1,7 +1,6 @@ package uk.gov.companieshouse.pscdataapi.transform; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -35,17 +34,13 @@ class CompanyPscTransformerTest { public static final String LEGAL_BO_KIND = "legal-person-beneficial-owner"; public static final String SECURE_BO_KIND = "super-secure-beneficial-owner"; - private static final Boolean REGISTER_VIEW_TRUE = true; - private static final Boolean REGISTER_VIEW_FALSE = false; + private static final boolean SHOW_FULL_DOB_TRUE = true; + private static final boolean SHOW_FULL_DOB_FALSE = false; @InjectMocks private CompanyPscTransformer pscTransformer; private FullRecordCompanyPSCApi fullRecordCompanyPSCApi; - @BeforeEach - public void setUp() { - } - @Test void testInsertEmptyPscTransform(){ PscDocument pscDocument = pscTransformer @@ -54,10 +49,10 @@ void testInsertEmptyPscTransform(){ } @Test - void testInsertIndividualPscIsTransformedSuccessfullyWhenRegisterViewTrue() throws FailedToTransformException { + void testInsertIndividualPscWithDateOfBirthIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_KIND, REGISTER_VIEW_TRUE); - PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_KIND, REGISTER_VIEW_TRUE); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_KIND, SHOW_FULL_DOB_TRUE); + PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_KIND, SHOW_FULL_DOB_TRUE); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -76,10 +71,10 @@ void testInsertIndividualPscIsTransformedSuccessfullyWhenRegisterViewTrue() thro } @Test - void testInsertIndividualPscIsTransformedSuccessfullyWhenRegisterViewFalse() throws FailedToTransformException { + void testInsertIndividualPscNoDateOfBirthIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_KIND, REGISTER_VIEW_FALSE); - PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_KIND, REGISTER_VIEW_FALSE); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_KIND, SHOW_FULL_DOB_FALSE); + PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_KIND, SHOW_FULL_DOB_FALSE); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -151,8 +146,8 @@ void testInsertSuperSecurePscIsTransformedSuccessfully() throws FailedToTransfor @Test void testInsertIndividualBeneficialOwnerPscIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_BO_KIND, REGISTER_VIEW_TRUE); - PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_BO_KIND, REGISTER_VIEW_TRUE); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE); + PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -247,7 +242,7 @@ void testBasicPscDocumentWithNullDataTransform(){ pscDocument.setDeltaAt("20230102030405000000"); pscDocument.setSensitiveData(new PscSensitiveData()); - Individual individual = pscTransformer.transformPscDocToIndividual(pscDocument, true); + Individual individual = pscTransformer.transformPscDocToIndividual(pscDocument, SHOW_FULL_DOB_TRUE); Assertions.assertNotNull(individual); Assertions.assertEquals("2023-01-02", individual.getNotifiedOn().toString()); @@ -256,14 +251,14 @@ void testBasicPscDocumentWithNullDataTransform(){ @Test void testEmptyPscIndividualTransform(){ Individual individual = pscTransformer - .transformPscDocToIndividual(new PscDocument(), true); + .transformPscDocToIndividual(new PscDocument(), SHOW_FULL_DOB_TRUE); Assertions.assertNotNull(individual); } @Test void testEmptyPscIndividualBeneficialOwnerTransform(){ IndividualBeneficialOwner individualBeneficialOwner = pscTransformer - .transformPscDocToIndividualBeneficialOwner(new PscDocument(), true); + .transformPscDocToIndividualBeneficialOwner(new PscDocument(), SHOW_FULL_DOB_TRUE); Assertions.assertNotNull(individualBeneficialOwner); } diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/util/TestHelper.java b/src/test/java/uk/gov/companieshouse/pscdataapi/util/TestHelper.java index 15046cdc..b8932ffe 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/util/TestHelper.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/util/TestHelper.java @@ -1,9 +1,14 @@ package uk.gov.companieshouse.pscdataapi.util; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.List; import org.springframework.util.FileCopyUtils; @@ -23,21 +28,23 @@ public class TestHelper { public static final String INDIVIDUAL_BO_KIND = "individual-beneficial-owner"; public static final String COMPANY_NUMBER = "companyNumber"; public static final String NOTIFICATION_ID = "notificationId"; + public static final String PSC_ID = "pscId"; public static final String X_REQUEST_ID = "654321"; + private TestHelper(){} public static FullRecordCompanyPSCApi buildFullRecordPsc(String kind) { return buildFullRecordPsc(kind, false); } - public static FullRecordCompanyPSCApi buildFullRecordPsc(String kind, boolean registerView) { + public static FullRecordCompanyPSCApi buildFullRecordPsc(String kind, boolean showFullDateOfBirth) { FullRecordCompanyPSCApi output = new FullRecordCompanyPSCApi(); ExternalData externalData = new ExternalData(); Data data = new Data(); - externalData.setPscId("pscId"); + externalData.setPscId(PSC_ID); // Not setting the notificationId as that is passed to the Transformer - externalData.setCompanyNumber("1234567"); + externalData.setCompanyNumber(COMPANY_NUMBER); InternalData internalData = new InternalData(); internalData.setDeltaAt(OffsetDateTime.parse("2022-01-12T00:00:00Z")); @@ -76,7 +83,7 @@ public static FullRecordCompanyPSCApi buildFullRecordPsc(String kind, boolean re if(kind.contains("individual")) { SensitiveData sensitiveData = new SensitiveData(); uk.gov.companieshouse.api.psc.DateOfBirth dateOfBirth = new uk.gov.companieshouse.api.psc.DateOfBirth(); - if(registerView){ + if(showFullDateOfBirth){ dateOfBirth.setDay(21); } else{ dateOfBirth.setDay(null); @@ -131,18 +138,32 @@ public static FullRecordCompanyPSCApi buildFullRecordPsc(String kind, boolean re return output; } + public static FullRecordCompanyPSCApi buildBasicFullRecordPsc(){ + FullRecordCompanyPSCApi result = new FullRecordCompanyPSCApi(); + InternalData internal = new InternalData(); + ExternalData external = new ExternalData(); + Data data = new Data(); + external.setNotificationId(NOTIFICATION_ID); + data.setKind(INDIVIDUAL_KIND); + external.setData(data); + internal.setDeltaAt(createOffsetDateTime()); + result.setExternalData(external); + result.setInternalData(internal); + return result; + } + public static PscDocument buildPscDocument(String kind) { return buildPscDocument(kind, false); } - public static PscDocument buildPscDocument(String kind, boolean registerView) { + public static PscDocument buildPscDocument(String kind, boolean showFullDateOfBirth) { PscDocument output = new PscDocument(); PscData data = new PscData(); - output.setId("notificationId"); - output.setNotificationId("notificationId"); - output.setPscId("pscId"); - output.setCompanyNumber("1234567"); + output.setId(NOTIFICATION_ID); + output.setNotificationId(NOTIFICATION_ID); + output.setPscId(PSC_ID); + output.setCompanyNumber(COMPANY_NUMBER); output.setDeltaAt("20220112000000000000"); output.setUpdated(new Updated().setAt(LocalDate.now())); output.setUpdatedBy("user"); @@ -179,7 +200,7 @@ public static PscDocument buildPscDocument(String kind, boolean registerView) { if(kind.contains("individual")) { PscSensitiveData sensitiveData = new PscSensitiveData(); DateOfBirth dateOfBirth = new DateOfBirth(); - if(registerView){ + if(showFullDateOfBirth){ dateOfBirth.setDay(21); } else { dateOfBirth.setDay(null); @@ -223,14 +244,36 @@ public static PscDocument buildPscDocument(String kind, boolean registerView) { return output; } + public static PscDocument buildBasicDocument(){ + PscDocument document = new PscDocument(); + document.setUpdated(new Updated().setAt(LocalDate.now())); + document.setCompanyNumber(COMPANY_NUMBER); + document.setPscId(PSC_ID); + document.setNotificationId(COMPANY_NUMBER); + PscData pscData = new PscData(); + pscData.setKind(INDIVIDUAL_KIND); + document.setData(pscData); + PscIdentification identification = new PscIdentification(); + identification.setCountryRegistered("x"); + identification.setLegalForm("x"); + identification.setPlaceRegistered("x"); + identification.setLegalAuthority("x"); + identification.setRegistrationNumber("x"); + document.setIdentification(identification); + return document; + } + public static String createJsonPayload() throws IOException { - InputStreamReader exampleJsonPayload = new InputStreamReader( - ClassLoader.getSystemClassLoader().getResourceAsStream("psc_payload.json")); + InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("psc_payload.json"); + if (inputStream == null){ + throw new IOException("Failed to load Json payload input stream"); + } + InputStreamReader exampleJsonPayload = new InputStreamReader(inputStream); return FileCopyUtils.copyToString(exampleJsonPayload); } - public PscList createPscList() { + public static PscList createPscList() { ListSummary listSummary = new ListSummary(); Identification identification = new Identification(); identification.setPlaceRegistered("x"); @@ -250,44 +293,13 @@ public PscList createPscList() { return pscList; } - private ListSummary createListSummary() { - ListSummary listSummary = new ListSummary(); - listSummary.setEtag("string"); - listSummary.setName("string"); - NameElements nameElements = new NameElements(); - nameElements.setTitle("Mr"); - nameElements.setForename("Forname"); - nameElements.setMiddleName("Middle"); - nameElements.setSurname("Surname"); - listSummary.setNameElements(nameElements); - uk.gov.companieshouse.api.psc.Address address = new uk.gov.companieshouse.api.psc.Address(); - address.setAddressLine1("1 street"); - address.setAddressLine2("2 street"); - address.setCountry("uk"); - address.setRegion("south"); - address.setPremises("prem"); - address.setPoBox("po"); - address.setLocality("Local"); - address.setCareOf("care"); - address.setPostalCode("post"); - listSummary.setAddress(address); - listSummary.setCeasedOn(LocalDate.now()); - listSummary.setIsSanctioned(true); - listSummary.setNationality("British"); - listSummary.setCountryOfResidence("Uk"); - listSummary.setDescription(ListSummary.DescriptionEnum - .SUPER_SECURE_PERSONS_WITH_SIGNIFICANT_CONTROL); - listSummary.setPrincipalOfficeAddress(address); - return listSummary; - } - - private Links createLinks() { + private static Links createLinks() { Links links = new Links(); - links.setSelf(String.format("/company/%s/persons-with-significant-control", "1234567")); + links.setSelf(String.format("/company/%s/persons-with-significant-control", COMPANY_NUMBER)); return links; } - public MetricsApi createMetrics() { + public static MetricsApi createMetrics() { MetricsApi metrics = new MetricsApi(); CountsApi counts = new CountsApi(); PscApi pscs = new PscApi(); @@ -299,7 +311,7 @@ public MetricsApi createMetrics() { return metrics; } - public PscList createPscListWithNoMetrics() { + public static PscList createPscListWithNoMetrics() { ListSummary listSummary = new ListSummary(); Identification identification = new Identification(); identification.setPlaceRegistered("x"); @@ -316,44 +328,16 @@ public PscList createPscListWithNoMetrics() { return pscList; } - public PscList createPscListRegisterView() { - ListSummary listSummary = new ListSummary(); - Identification identification = new Identification(); - identification.setPlaceRegistered("x"); - identification.setCountryRegistered("x"); - identification.setRegistrationNumber("x"); - identification.setLegalAuthority("x"); - identification.setLegalForm("x"); - listSummary.setIdentification(identification); - PscList pscList = new PscList(); - pscList.setItems(Collections.singletonList(listSummary)); - pscList.setActiveCount(1); - pscList.setCeasedCount(0); - pscList.setTotalResults(1); - pscList.setStartIndex(0); - pscList.setItemsPerPage(25); - pscList.setLinks(createLinks()); - return pscList; + static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"); + public static OffsetDateTime createOffsetDateTime() { + LocalDateTime localDate = LocalDateTime.parse("2023-01-02T13:04:05.678", formatter); + ZoneOffset offset = ZoneId.systemDefault().getRules().getOffset(localDate); + return OffsetDateTime.of(localDate, offset); } - - public MetricsApi createMetricsRegisterView() { - MetricsApi metrics = new MetricsApi(); - CountsApi counts = new CountsApi(); - PscApi pscs = new PscApi(); - pscs.setActiveStatementsCount(1); - pscs.setWithdrawnStatementsCount(0); - pscs.setStatementsCount(1); - counts.setPersonsWithSignificantControl(pscs); - metrics.setCounts(counts); - - RegistersApi registers = new RegistersApi(); - RegisterApi pscStatements = new RegisterApi(); - pscStatements.setRegisterMovedTo("public-register"); - String date = "2020-12-20T06:00:00Z"; - OffsetDateTime dt = OffsetDateTime.parse(date); - pscStatements.setMovedOn(dt); - registers.setPersonsWithSignificantControl(pscStatements); - metrics.setRegisters(registers); - return metrics; + public static OffsetDateTime createLaterOffsetDateTime() { + LocalDateTime laterLocalDate = LocalDateTime.parse("2023-01-03T13:04:05.678", formatter); + ZoneOffset offset = ZoneId.systemDefault().getRules().getOffset(laterLocalDate); + return OffsetDateTime.of(laterLocalDate, offset); } + }