diff --git a/src/main/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiService.java b/src/main/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiService.java index 2592ca62..626088c7 100644 --- a/src/main/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiService.java +++ b/src/main/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiService.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.time.OffsetDateTime; import java.util.HashMap; + import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import uk.gov.companieshouse.api.InternalApiClient; @@ -15,7 +16,8 @@ import uk.gov.companieshouse.logging.Logger; import uk.gov.companieshouse.pscdataapi.exceptions.SerDesException; import uk.gov.companieshouse.pscdataapi.exceptions.ServiceUnavailableException; -import uk.gov.companieshouse.pscdataapi.models.PscData; +import uk.gov.companieshouse.pscdataapi.models.PscDocument; +import uk.gov.companieshouse.pscdataapi.transform.CompanyPscTransformer; import uk.gov.companieshouse.pscdataapi.util.PscTransformationHelper; @Service @@ -26,6 +28,7 @@ public class ChsKafkaApiService { private static final String CHANGED_EVENT_TYPE = "changed"; private static final String DELETE_EVENT_TYPE = "deleted"; + private final CompanyPscTransformer companyPscTransformer; private final InternalApiClient internalApiClient; private final Logger logger; private final ObjectMapper objectMapper; @@ -35,10 +38,12 @@ public class ChsKafkaApiService { @Value("${chs.api.kafka.resource-changed.uri}") private String resourceChangedUri; - public ChsKafkaApiService(InternalApiClient internalApiClient, Logger logger, ObjectMapper objectMapper) { + public ChsKafkaApiService(InternalApiClient internalApiClient, Logger logger, + ObjectMapper objectMapper, CompanyPscTransformer companyPscTransformer) { this.internalApiClient = internalApiClient; this.logger = logger; this.objectMapper = objectMapper; + this.companyPscTransformer = companyPscTransformer; } /** @@ -73,34 +78,51 @@ public ApiResponse invokeChsKafkaApi(String contextId, String companyNumbe public ApiResponse invokeChsKafkaApiWithDeleteEvent(String contextId, String companyNumber, String notificationId, - String kind, PscData pscData) { + String kind, PscDocument pscDocument) { internalApiClient.setBasePath(chsKafkaApiUrl); PrivateChangedResourcePost changedResourcePost = internalApiClient.privateChangedResourceHandler() .postChangedResource(resourceChangedUri, mapChangedResource(contextId, companyNumber, - notificationId, kind, true, pscData)); + notificationId, kind, true, pscDocument)); return handleApiCall(changedResourcePost); } private ChangedResource mapChangedResource(String contextId, String companyNumber, String notificationId, - String kind, boolean isDelete, PscData pscData) { + String kind, boolean isDelete, PscDocument pscDocument) { ChangedResourceEvent event = new ChangedResourceEvent(); ChangedResource changedResource = new ChangedResource(); event.setPublishedAt(String.valueOf(OffsetDateTime.now())); if (isDelete) { event.setType(DELETE_EVENT_TYPE); - try { - // This write value/read value is necessary to remove null fields during the jackson conversion - Object pscDataAsObject = objectMapper.readValue( - objectMapper.writeValueAsString(pscData), - Object.class); - changedResource.setDeletedData(pscDataAsObject); - } catch (JsonProcessingException ex) { - throw new SerDesException("Failed to serialise/deserialise psc data", ex); + if (pscDocument != null) { + // This write-value/read-value is necessary to remove null fields during the jackson conversion + try { + Object pscObject = switch (pscDocument.getData().getKind()) { + case "individual-person-with-significant-control" -> + companyPscTransformer.transformPscDocToIndividual(pscDocument, false); + case "individual-beneficial-owner" -> + companyPscTransformer.transformPscDocToIndividualBeneficialOwner(pscDocument, false); + case "corporate-entity-person-with-significant-control" -> + companyPscTransformer.transformPscDocToCorporateEntity(pscDocument); + case "corporate-entity-beneficial-owner" -> + companyPscTransformer.transformPscDocToCorporateEntityBeneficialOwner(pscDocument); + case "legal-person-person-with-significant-control" -> + companyPscTransformer.transformPscDocToLegalPerson(pscDocument); + case "legal-person-beneficial-owner" -> + companyPscTransformer.transformPscDocToLegalPersonBeneficialOwner(pscDocument); + case "super-secure-person-with-significant-control" -> + companyPscTransformer.transformPscDocToSuperSecure(pscDocument); + case "super-secure-beneficial-owner" -> + companyPscTransformer.transformPscDocToSuperSecureBeneficialOwner(pscDocument); + default -> null; + }; + changedResource.setDeletedData(deserializedData(pscObject)); + } catch (JsonProcessingException e) { + throw new SerDesException("Failed to serialise/deserialise psc data", e); + } } - } else { event.setType(CHANGED_EVENT_TYPE); } @@ -137,4 +159,8 @@ private ApiResponse handleApiCall(PrivateChangedResourcePost changedResour throw exception; } } + + private Object deserializedData(Object pscDocument) throws JsonProcessingException { + return objectMapper.readValue(objectMapper.writeValueAsString(pscDocument), Object.class); + } } 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 4cfe3b2e..7cc452b0 100644 --- a/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscService.java +++ b/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyPscService.java @@ -173,7 +173,7 @@ public void deletePsc(String companyNumber, String notificationId, String contex repository.delete(pscDocument); try { chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent(contextId, - companyNumber, notificationId, kind, pscDocument.getData()); + companyNumber, notificationId, kind, pscDocument); } catch (Exception exception) { throw new ServiceUnavailableException(exception.getMessage()); } 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 a00e41be..919ccf8f 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiServiceTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/api/ChsKafkaApiServiceTest.java @@ -6,6 +6,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpResponseException; @@ -24,13 +25,19 @@ import uk.gov.companieshouse.api.handler.chskafka.PrivateChangedResourceHandler; import uk.gov.companieshouse.api.handler.chskafka.request.PrivateChangedResourcePost; import uk.gov.companieshouse.api.model.ApiResponse; +import uk.gov.companieshouse.api.psc.*; import uk.gov.companieshouse.logging.Logger; import uk.gov.companieshouse.pscdataapi.exceptions.ServiceUnavailableException; +import uk.gov.companieshouse.pscdataapi.models.PscDocument; +import uk.gov.companieshouse.pscdataapi.transform.CompanyPscTransformer; import uk.gov.companieshouse.pscdataapi.util.TestHelper; @ExtendWith(MockitoExtension.class) class ChsKafkaApiServiceTest { + private static final String EVENT_TYPE_CHANGED = "changed"; + private static final String EVENT_TYPE_DELETED = "deleted"; + @Mock private Logger logger; @Mock @@ -43,6 +50,8 @@ class ChsKafkaApiServiceTest { private ApiResponse response; @Mock private ObjectMapper objectMapper; + @Mock + CompanyPscTransformer companyPscTransformer; @InjectMocks private ChsKafkaApiService chsKafkaApiService; @Captor @@ -55,31 +64,257 @@ void invokeChsKafkaEndpoint() throws ApiErrorResponseException { when(privateChangedResourcePost.execute()).thenReturn(response); ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApi( - TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, "individual-person-with-significant-control"); + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.INDIVIDUAL_KIND); Assertions.assertThat(apiResponse).isNotNull(); verify(internalApiClient, times(1)).privateChangedResourceHandler(); verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); verify(privateChangedResourcePost, times(1)).execute(); - Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo("changed"); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_CHANGED); Assertions.assertThat(changedResourceCaptor.getValue().getResourceUri()).isEqualTo("/company/companyNumber/persons-with-significant-control/individual/notificationId"); } @Test - void invokeChsKafkaEndpointDelete() throws ApiErrorResponseException { + void invokeChsKafkaEndpointWithDeleteForIndividual() throws ApiErrorResponseException, JsonProcessingException { + // given + when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); + when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(privateChangedResourcePost); + when(privateChangedResourcePost.execute()).thenReturn(response); + + PscDocument document = TestHelper.buildPscDocument(TestHelper.INDIVIDUAL_KIND); + Individual individual = new Individual(); + individual.setKind(Individual.KindEnum.INDIVIDUAL_PERSON_WITH_SIGNIFICANT_CONTROL); + individual.setName("Individual"); + when(companyPscTransformer.transformPscDocToIndividual(document, false)).thenReturn(individual); + + when(objectMapper.writeValueAsString(individual)).thenReturn(individual.toString()); + when(objectMapper.readValue(individual.toString(), Object.class)).thenReturn(individual); + + // when + ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent( + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.INDIVIDUAL_KIND, + document); + Assertions.assertThat(apiResponse).isNotNull(); + + //then + verify(internalApiClient, times(1)).privateChangedResourceHandler(); + verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); + verify(privateChangedResourcePost, times(1)).execute(); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); + Assertions.assertThat(changedResourceCaptor.getValue().getDeletedData()).isInstanceOf(Individual.class); + } + + @Test + void invokeChsKafkaEndpointWithDeleteForIndividualBeneficialOwner() throws ApiErrorResponseException, JsonProcessingException { + // given when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(privateChangedResourcePost); when(privateChangedResourcePost.execute()).thenReturn(response); + PscDocument document = TestHelper.buildPscDocument(TestHelper.INDIVIDUAL_BO_KIND); + IndividualBeneficialOwner individualBO = new IndividualBeneficialOwner(); + individualBO.setKind(IndividualBeneficialOwner.KindEnum.INDIVIDUAL_BENEFICIAL_OWNER); + individualBO.setName("Individual-Beneficial-Owner"); + when(companyPscTransformer.transformPscDocToIndividualBeneficialOwner(document, false)).thenReturn(individualBO); + + when(objectMapper.writeValueAsString(individualBO)).thenReturn(individualBO.toString()); + when(objectMapper.readValue(individualBO.toString(), Object.class)).thenReturn(individualBO); + + // when + ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent( + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.INDIVIDUAL_KIND, + document); + Assertions.assertThat(apiResponse).isNotNull(); + + //then + verify(internalApiClient, times(1)).privateChangedResourceHandler(); + verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); + verify(privateChangedResourcePost, times(1)).execute(); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); + Assertions.assertThat(changedResourceCaptor.getValue().getDeletedData()).isInstanceOf(IndividualBeneficialOwner.class); + } + + @Test + void invokeChsKafkaEndpointWithDeleteForLegalPerson() throws ApiErrorResponseException, JsonProcessingException { + // given + when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); + when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(privateChangedResourcePost); + when(privateChangedResourcePost.execute()).thenReturn(response); + + PscDocument document = TestHelper.buildPscDocument(TestHelper.LEGAL_KIND); + LegalPerson legalPerson = new LegalPerson(); + legalPerson.setKind(LegalPerson.KindEnum.LEGAL_PERSON_PERSON_WITH_SIGNIFICANT_CONTROL); + legalPerson.setName("Legal-Person"); + when(companyPscTransformer.transformPscDocToLegalPerson(document)).thenReturn(legalPerson); + + when(objectMapper.writeValueAsString(legalPerson)).thenReturn(legalPerson.toString()); + when(objectMapper.readValue(legalPerson.toString(), Object.class)).thenReturn(legalPerson); + + // when + ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent( + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.LEGAL_KIND, + document); + Assertions.assertThat(apiResponse).isNotNull(); + + //then + verify(internalApiClient, times(1)).privateChangedResourceHandler(); + verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); + verify(privateChangedResourcePost, times(1)).execute(); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); + Assertions.assertThat(changedResourceCaptor.getValue().getDeletedData()).isInstanceOf(LegalPerson.class); + } + + @Test + void invokeChsKafkaEndpointWithDeleteForLegalPersonBeneficialOwner() throws ApiErrorResponseException, JsonProcessingException { + // given + when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); + when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(privateChangedResourcePost); + when(privateChangedResourcePost.execute()).thenReturn(response); + + PscDocument document = TestHelper.buildPscDocument(TestHelper.LEGAL_BO_KIND); + LegalPersonBeneficialOwner legalPersonBeneficialOwner = new LegalPersonBeneficialOwner(); + legalPersonBeneficialOwner.setKind(LegalPersonBeneficialOwner.KindEnum.LEGAL_PERSON_BENEFICIAL_OWNER); + legalPersonBeneficialOwner.setName("Legal-Person-Beneficial-Owner"); + when(companyPscTransformer.transformPscDocToLegalPersonBeneficialOwner( + document)).thenReturn(legalPersonBeneficialOwner); + + when(objectMapper.writeValueAsString(legalPersonBeneficialOwner)) + .thenReturn(legalPersonBeneficialOwner.toString()); + when(objectMapper.readValue(legalPersonBeneficialOwner.toString(), Object.class)) + .thenReturn(legalPersonBeneficialOwner); + + // when + ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent( + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.LEGAL_BO_KIND, + document); + Assertions.assertThat(apiResponse).isNotNull(); + + //then + verify(internalApiClient, times(1)).privateChangedResourceHandler(); + verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); + verify(privateChangedResourcePost, times(1)).execute(); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); + Assertions.assertThat(changedResourceCaptor.getValue().getDeletedData()).isInstanceOf(LegalPersonBeneficialOwner.class); + } + + @Test + void invokeChsKafkaEndpointWithDeleteForSuperSecure() throws ApiErrorResponseException, JsonProcessingException { + // given + when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); + when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(privateChangedResourcePost); + when(privateChangedResourcePost.execute()).thenReturn(response); + + PscDocument document = TestHelper.buildPscDocument(TestHelper.SECURE_KIND); + SuperSecure superSecure = new SuperSecure(); + superSecure.setKind(SuperSecure.KindEnum.SUPER_SECURE_PERSON_WITH_SIGNIFICANT_CONTROL); + when(companyPscTransformer.transformPscDocToSuperSecure( + document)).thenReturn(superSecure); + + when(objectMapper.writeValueAsString(superSecure)).thenReturn(superSecure.toString()); + when(objectMapper.readValue(superSecure.toString(), Object.class)).thenReturn(superSecure); + + // when + ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent( + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.SECURE_KIND, + document); + Assertions.assertThat(apiResponse).isNotNull(); + + //then + verify(internalApiClient, times(1)).privateChangedResourceHandler(); + verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); + verify(privateChangedResourcePost, times(1)).execute(); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); + Assertions.assertThat(changedResourceCaptor.getValue().getDeletedData()).isInstanceOf(SuperSecure.class); + } + + @Test + void invokeChsKafkaEndpointWithDeleteForSuperSecureBO() throws ApiErrorResponseException, JsonProcessingException { + // given + when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); + when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(privateChangedResourcePost); + when(privateChangedResourcePost.execute()).thenReturn(response); + + PscDocument document = TestHelper.buildPscDocument(TestHelper.SECURE_BO_KIND); + SuperSecureBeneficialOwner superSecureBO = new SuperSecureBeneficialOwner(); + superSecureBO.setKind(SuperSecureBeneficialOwner.KindEnum.SUPER_SECURE_BENEFICIAL_OWNER); + when(companyPscTransformer.transformPscDocToSuperSecureBeneficialOwner( + document)).thenReturn(superSecureBO); + + when(objectMapper.writeValueAsString(superSecureBO)).thenReturn(superSecureBO.toString()); + when(objectMapper.readValue(superSecureBO.toString(), Object.class)).thenReturn(superSecureBO); + + // when + ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent( + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.SECURE_BO_KIND, + document); + Assertions.assertThat(apiResponse).isNotNull(); + + //then + verify(internalApiClient, times(1)).privateChangedResourceHandler(); + verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); + verify(privateChangedResourcePost, times(1)).execute(); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); + Assertions.assertThat(changedResourceCaptor.getValue().getDeletedData()).isInstanceOf(SuperSecureBeneficialOwner.class); + } + + @Test + void invokeChsKafkaEndpointWithDeleteForCorporateEntity() throws ApiErrorResponseException, JsonProcessingException { + // given + when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); + when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(privateChangedResourcePost); + when(privateChangedResourcePost.execute()).thenReturn(response); + + PscDocument document = TestHelper.buildPscDocument(TestHelper.CORPORATE_KIND); + CorporateEntity corporateEntity = new CorporateEntity(); + corporateEntity.setKind(CorporateEntity.KindEnum.CORPORATE_ENTITY_PERSON_WITH_SIGNIFICANT_CONTROL); + corporateEntity.setName("Corporate-Entity-Person-With-Significant-Control"); + when(companyPscTransformer.transformPscDocToCorporateEntity(document)).thenReturn(corporateEntity); + + when(objectMapper.writeValueAsString(corporateEntity)).thenReturn(corporateEntity.toString()); + when(objectMapper.readValue(corporateEntity.toString(), Object.class)).thenReturn(corporateEntity); + + // when + ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent( + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.CORPORATE_KIND, + document); + Assertions.assertThat(apiResponse).isNotNull(); + + //then + verify(internalApiClient, times(1)).privateChangedResourceHandler(); + verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); + verify(privateChangedResourcePost, times(1)).execute(); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); + Assertions.assertThat(changedResourceCaptor.getValue().getDeletedData()).isInstanceOf(CorporateEntity.class); + } + + @Test + void invokeChsKafkaEndpointWithDeleteForCorporateEntityBO() throws ApiErrorResponseException, JsonProcessingException { + // given + when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler); + when(privateChangedResourceHandler.postChangedResource(any(), any())).thenReturn(privateChangedResourcePost); + when(privateChangedResourcePost.execute()).thenReturn(response); + + PscDocument document = TestHelper.buildPscDocument(TestHelper.CORPORATE_BO_KIND); + CorporateEntityBeneficialOwner corporateEntityBO = new CorporateEntityBeneficialOwner(); + corporateEntityBO.setKind(CorporateEntityBeneficialOwner.KindEnum.CORPORATE_ENTITY_BENEFICIAL_OWNER); + corporateEntityBO.setName("Corporate-Entity-Beneficial-Owner"); + when(companyPscTransformer.transformPscDocToCorporateEntityBeneficialOwner(document)).thenReturn(corporateEntityBO); + + when(objectMapper.writeValueAsString(corporateEntityBO)).thenReturn(corporateEntityBO.toString()); + when(objectMapper.readValue(corporateEntityBO.toString(), Object.class)).thenReturn(corporateEntityBO); + + // when ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent( - TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, "kind", - TestHelper.buildPscDocument("individual-persons-with-significant-control").getData()); + TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, TestHelper.CORPORATE_BO_KIND, + document); Assertions.assertThat(apiResponse).isNotNull(); + //then verify(internalApiClient, times(1)).privateChangedResourceHandler(); verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); verify(privateChangedResourcePost, times(1)).execute(); - Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo("deleted"); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); + Assertions.assertThat(changedResourceCaptor.getValue().getDeletedData()).isInstanceOf(CorporateEntityBeneficialOwner.class); } @Test @@ -96,7 +331,7 @@ void invokeChsKafkaEndpointThrowsApiErrorException() throws ApiErrorResponseExce verify(internalApiClient, times(1)).privateChangedResourceHandler(); verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); verify(privateChangedResourcePost, times(1)).execute(); - Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo("changed"); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_CHANGED); } @Test @@ -113,7 +348,7 @@ void invokeChsKafkaEndpointWithDeleteThrowsApiErrorException() throws ApiErrorRe verify(internalApiClient, times(1)).privateChangedResourceHandler(); verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); verify(privateChangedResourcePost, times(1)).execute(); - Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo("deleted"); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); } @Test @@ -130,7 +365,7 @@ void invokeChsKafkaEndpointThrowsRuntimeException() throws ApiErrorResponseExcep verify(internalApiClient, times(1)).privateChangedResourceHandler(); verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); verify(privateChangedResourcePost, times(1)).execute(); - Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo("changed"); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_CHANGED); } @Test @@ -147,6 +382,6 @@ void invokeChsKafkaEndpointWithDeleteThrowsRuntimeException() throws ApiErrorRes verify(internalApiClient, times(1)).privateChangedResourceHandler(); verify(privateChangedResourceHandler, times(1)).postChangedResource(any(), changedResourceCaptor.capture()); verify(privateChangedResourcePost, times(1)).execute(); - Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo("deleted"); + Assertions.assertThat(changedResourceCaptor.getValue().getEvent().getType()).isEqualTo(EVENT_TYPE_DELETED); } } diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagDisabledITest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagDisabledITest.java index 89ef83db..9c9ff49f 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagDisabledITest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagDisabledITest.java @@ -93,7 +93,7 @@ void testThatKafkaApiShouldBeCalledOnDeleteWhenFeatureFlagDisabled() when(changedResourcePost.execute()).thenReturn(response); ApiResponse apiResponse = chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent(TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, "kind", - TestHelper.buildPscDocument("individual-persons-with-significant-control").getData()); + TestHelper.buildPscDocument("individual-persons-with-significant-control")); Assertions.assertThat(apiResponse).isNotNull(); diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagEnabledITest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagEnabledITest.java index f134eab0..172a1517 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagEnabledITest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/api/ResourceChangedApiServiceAspectFeatureFlagEnabledITest.java @@ -76,7 +76,7 @@ void testThatAspectShouldNotProceedOnDeleteWhenFeatureFlagEnabled() throws Servi when(changedResourcePost.execute()).thenReturn(response); chsKafkaApiService.invokeChsKafkaApiWithDeleteEvent(TestHelper.X_REQUEST_ID, TestHelper.COMPANY_NUMBER, TestHelper.NOTIFICATION_ID, "kind", - TestHelper.buildPscDocument("individual-persons-with-significant-control").getData()); + TestHelper.buildPscDocument("individual-persons-with-significant-control")); verifyNoInteractions(apiClientService); verifyNoInteractions(internalApiClient); 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 3bcddbc6..936e5f4a 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformerTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/transform/CompanyPscTransformerTest.java @@ -33,16 +33,6 @@ class CompanyPscTransformerTest { private Logger logger; private static final String NOTIFICATION_ID = "notificationId"; - - private static final String INDIVIDUAL_KIND = "individual-person-with-significant-control"; - private static final String CORPORATE_KIND = "corporate-entity-person-with-significant-control"; - public static final String LEGAL_KIND = "legal-person-person-with-significant-control"; - private static final String SECURE_KIND = "super-secure-person-with-significant-control"; - public static final String INDIVIDUAL_BO_KIND = "individual-beneficial-owner"; - public static final String CORPORATE_BO_KIND = "corporate-entity-beneficial-owner"; - 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 SHOW_FULL_DOB_TRUE = true; private static final boolean SHOW_FULL_DOB_FALSE = false; @@ -60,8 +50,8 @@ void testInsertEmptyPscTransform(){ @Test void testInsertIndividualPscWithDateOfBirthIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_KIND, SHOW_FULL_DOB_TRUE, true); - PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_KIND, SHOW_FULL_DOB_TRUE, true); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.INDIVIDUAL_KIND, SHOW_FULL_DOB_TRUE, true); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.INDIVIDUAL_KIND, SHOW_FULL_DOB_TRUE, true); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -82,8 +72,8 @@ void testInsertIndividualPscWithDateOfBirthIsTransformedSuccessfully() throws Fa @Test void testInsertIndividualPscNoDateOfBirthIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_KIND, SHOW_FULL_DOB_FALSE, true); - PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_KIND, SHOW_FULL_DOB_FALSE, true); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.INDIVIDUAL_KIND, SHOW_FULL_DOB_FALSE, true); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.INDIVIDUAL_KIND, SHOW_FULL_DOB_FALSE, true); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -104,8 +94,8 @@ void testInsertIndividualPscNoDateOfBirthIsTransformedSuccessfully() throws Fail @Test void testInsertCorporateEntityPscIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(CORPORATE_KIND); - PscDocument expectedDocument = TestHelper.buildPscDocument(CORPORATE_KIND); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.CORPORATE_KIND); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.CORPORATE_KIND); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -122,8 +112,8 @@ void testInsertCorporateEntityPscIsTransformedSuccessfully() throws FailedToTran @Test void testInsertLegalPersonPscIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(LEGAL_KIND); - PscDocument expectedDocument = TestHelper.buildPscDocument(LEGAL_KIND); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.LEGAL_KIND); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.LEGAL_KIND); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -140,8 +130,8 @@ void testInsertLegalPersonPscIsTransformedSuccessfully() throws FailedToTransfor @Test void testInsertSuperSecurePscIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(SECURE_KIND); - PscDocument expectedDocument = TestHelper.buildPscDocument(SECURE_KIND); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.SECURE_KIND); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.SECURE_KIND); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -157,8 +147,8 @@ void testInsertSuperSecurePscIsTransformedSuccessfully() throws FailedToTransfor @Test void testInsertIndividualBeneficialOwnerPscIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE, true); - PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE, true); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE, true); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE, true); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -181,8 +171,8 @@ void testInsertIndividualBeneficialOwnerPscIsTransformedSuccessfully() throws Fa @Test void testInsertIndividualBeneficialOwnerPscIsTransformedSuccessfullyNoPscStatements() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE, false); - PscDocument expectedDocument = TestHelper.buildPscDocument(INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE, false); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE, false); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.INDIVIDUAL_BO_KIND, SHOW_FULL_DOB_TRUE, false); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -206,8 +196,8 @@ void testInsertIndividualBeneficialOwnerPscIsTransformedSuccessfullyNoPscStateme @Test void testInsertCorporateEntityBeneficialOwnerPscIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(CORPORATE_BO_KIND); - PscDocument expectedDocument = TestHelper.buildPscDocument(CORPORATE_BO_KIND); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.CORPORATE_BO_KIND); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.CORPORATE_BO_KIND); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -226,8 +216,8 @@ void testInsertCorporateEntityBeneficialOwnerPscIsTransformedSuccessfully() thro @Test void testInsertLegalPersonBeneficialOwnerPscIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(LEGAL_BO_KIND); - PscDocument expectedDocument = TestHelper.buildPscDocument(LEGAL_BO_KIND); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.LEGAL_BO_KIND); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.LEGAL_BO_KIND); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -246,8 +236,8 @@ void testInsertLegalPersonBeneficialOwnerPscIsTransformedSuccessfully() throws F @Test void testInsertSuperSecureBeneficialOwnerPscIsTransformedSuccessfully() throws FailedToTransformException { // given - fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(SECURE_BO_KIND); - PscDocument expectedDocument = TestHelper.buildPscDocument(SECURE_BO_KIND); + fullRecordCompanyPSCApi = TestHelper.buildFullRecordPsc(TestHelper.SECURE_BO_KIND); + PscDocument expectedDocument = TestHelper.buildPscDocument(TestHelper.SECURE_BO_KIND); // when PscDocument result = pscTransformer.transformPscOnInsert(NOTIFICATION_ID, fullRecordCompanyPSCApi); // then @@ -367,7 +357,7 @@ void testBasicListSummaryWithNullDataTransform(){ @Test void testListSummaryTransform(){ - PscDocument pscDocument = TestHelper.buildPscDocument(CORPORATE_BO_KIND); + PscDocument pscDocument = TestHelper.buildPscDocument(TestHelper.CORPORATE_BO_KIND); ListSummary listSummary = pscTransformer .transformPscDocToListSummary(pscDocument, true); 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 50ac6478..e8dfc1d6 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/util/TestHelper.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/util/TestHelper.java @@ -15,15 +15,23 @@ import uk.gov.companieshouse.api.metrics.CountsApi; import uk.gov.companieshouse.api.metrics.MetricsApi; import uk.gov.companieshouse.api.metrics.PscApi; +import uk.gov.companieshouse.api.psc.CorporateEntity; +import uk.gov.companieshouse.api.psc.CorporateEntityBeneficialOwner; import uk.gov.companieshouse.api.psc.Data; import uk.gov.companieshouse.api.psc.ExternalData; import uk.gov.companieshouse.api.psc.FullRecordCompanyPSCApi; import uk.gov.companieshouse.api.psc.Identification; +import uk.gov.companieshouse.api.psc.Individual; +import uk.gov.companieshouse.api.psc.IndividualBeneficialOwner; import uk.gov.companieshouse.api.psc.InternalData; import uk.gov.companieshouse.api.psc.ItemLinkTypes; import uk.gov.companieshouse.api.psc.ListSummary; +import uk.gov.companieshouse.api.psc.LegalPerson; +import uk.gov.companieshouse.api.psc.LegalPersonBeneficialOwner; import uk.gov.companieshouse.api.psc.PscList; import uk.gov.companieshouse.api.psc.SensitiveData; +import uk.gov.companieshouse.api.psc.SuperSecure; +import uk.gov.companieshouse.api.psc.SuperSecureBeneficialOwner; import uk.gov.companieshouse.api.psc.UsualResidentialAddress; import uk.gov.companieshouse.pscdataapi.models.Address; import uk.gov.companieshouse.pscdataapi.models.DateOfBirth; @@ -36,11 +44,16 @@ import uk.gov.companieshouse.pscdataapi.models.Updated; public class TestHelper { - public static final String INDIVIDUAL_KIND = "individual-person-with-significant-control"; - public static final String CORPORATE_KIND = "corporate-entity-person-with-significant-control"; - public static final String LEGAL_KIND = "legal-person-person-with-significant-control"; - public static final String SECURE_KIND = "super-secure-person-with-significant-control"; - public static final String INDIVIDUAL_BO_KIND = "individual-beneficial-owner"; + + public static final String INDIVIDUAL_KIND = Individual.KindEnum.INDIVIDUAL_PERSON_WITH_SIGNIFICANT_CONTROL.toString(); + public static final String CORPORATE_KIND = CorporateEntity.KindEnum.CORPORATE_ENTITY_PERSON_WITH_SIGNIFICANT_CONTROL.toString(); + public static final String LEGAL_KIND = LegalPerson.KindEnum.LEGAL_PERSON_PERSON_WITH_SIGNIFICANT_CONTROL.toString(); + public static final String SECURE_KIND = SuperSecure.KindEnum.SUPER_SECURE_PERSON_WITH_SIGNIFICANT_CONTROL.toString(); + public static final String INDIVIDUAL_BO_KIND = IndividualBeneficialOwner.KindEnum.INDIVIDUAL_BENEFICIAL_OWNER.toString(); + public static final String CORPORATE_BO_KIND = CorporateEntityBeneficialOwner.KindEnum.CORPORATE_ENTITY_BENEFICIAL_OWNER.toString(); + public static final String LEGAL_BO_KIND = LegalPersonBeneficialOwner.KindEnum.LEGAL_PERSON_BENEFICIAL_OWNER.toString(); + public static final String SECURE_BO_KIND = SuperSecureBeneficialOwner.KindEnum.SUPER_SECURE_BENEFICIAL_OWNER.toString(); + public static final String COMPANY_NUMBER = "companyNumber"; public static final String NOTIFICATION_ID = "notificationId"; public static final String PSC_ID = "pscId";