diff --git a/pom.xml b/pom.xml index ade67a1..2c62ce4 100644 --- a/pom.xml +++ b/pom.xml @@ -23,8 +23,8 @@ 1.0.0 1.4.2 1.9.12 - 1.0.27 - 2.0.168 + 1.0.28 + 2.0.174 1.0.4 3.1.1 7.2.3 diff --git a/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/consumer/DisqualifiedOfficersDeltaConsumerITest.java b/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/consumer/DisqualifiedOfficersDeltaConsumerITest.java index 40b795e..818a7f2 100644 --- a/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/consumer/DisqualifiedOfficersDeltaConsumerITest.java +++ b/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/consumer/DisqualifiedOfficersDeltaConsumerITest.java @@ -17,7 +17,7 @@ public class DisqualifiedOfficersDeltaConsumerITest extends AbstractIntegrationT @Test public void testSendingKafkaMessage() { - ChsDelta chsDelta = new ChsDelta("{ \"key\": \"value\" }", 1, "some_id"); + ChsDelta chsDelta = new ChsDelta("{ \"key\": \"value\" }", 1, "some_id", false); kafkaTemplate.send(mainTopic, chsDelta); } diff --git a/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/data/TestData.java b/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/data/TestData.java index 11da52b..ec07588 100644 --- a/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/data/TestData.java +++ b/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/data/TestData.java @@ -21,6 +21,11 @@ public static String getOutputData(String officerType, String disqType) { return readFile(path).replaceAll("\n", ""); } + public static String getDeleteData() { + String path = "src/itest/resources/data/delete_disqualification.json"; + return readFile(path).replaceAll("\n", ""); + } + private static String readFile(String path) { String data; try { diff --git a/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/steps/CommonSteps.java b/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/steps/CommonSteps.java index 7bd950a..1becf2d 100644 --- a/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/steps/CommonSteps.java +++ b/src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/steps/CommonSteps.java @@ -21,13 +21,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.StreamSupport; -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; -import static com.github.tomakehurst.wiremock.client.WireMock.put; -import static com.github.tomakehurst.wiremock.client.WireMock.requestMadeFor; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; -import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static com.github.tomakehurst.wiremock.client.WireMock.*; import static org.assertj.core.api.Assertions.assertThat; public class CommonSteps { @@ -62,7 +56,7 @@ public void theConsumerReceivesDisqualificationOfType(String officerType, String this.type = officerType; this.output = TestData.getOutputData(officerType, disqType); - ChsDelta delta = new ChsDelta(TestData.getInputData(officerType, disqType), 1, "1"); + ChsDelta delta = new ChsDelta(TestData.getInputData(officerType, disqType), 1, "1", false); kafkaTemplate.send(mainTopic, delta); countDown(); @@ -77,7 +71,7 @@ public void invalidAvroMessageIsSent() throws Exception { @When("a message with invalid data is sent") public void messageWithInvalidDataIsSent() throws Exception { - ChsDelta delta = new ChsDelta("InvalidData", 1, "1"); + ChsDelta delta = new ChsDelta("InvalidData", 1, "1", false); kafkaTemplate.send(mainTopic, delta); countDown(); @@ -89,7 +83,7 @@ public void theConsumerReceivesMessageButDataApiReturns(int responseCode) throws stubPutDisqualification("natural", responseCode); ChsDelta delta = new ChsDelta( - TestData.getInputData("natural", "undertaking"), 1, "1"); + TestData.getInputData("natural", "undertaking"), 1, "1", false); kafkaTemplate.send(mainTopic, delta); countDown(); @@ -98,7 +92,7 @@ public void theConsumerReceivesMessageButDataApiReturns(int responseCode) throws @When("the consumer receives a message that causes an error") public void theConsumerReceivesMessageThatCausesAnError() throws Exception { ChsDelta delta = new ChsDelta( - TestData.getInputData("natural", "error"), 1, "1"); + TestData.getInputData("natural", "error"), 1, "1", false); kafkaTemplate.send(mainTopic, delta); countDown(); @@ -129,6 +123,22 @@ public void theMessageShouldRetryAndError(int retries) { assertThat(errors).isEqualTo(1); } + @When("the consumer receives a delete payload") + public void theConsumerReceivesDelete() throws Exception { + configureWiremock(); + stubDeleteDisqualification(200); + ChsDelta delta = new ChsDelta(TestData.getDeleteData(), 1, "1", true); + kafkaTemplate.send(mainTopic, delta); + + countDown(); + } + + @Then("a DELETE request is sent to the disqualifications api with the encoded Id") + public void deleteRequestIsSent() { + verify(1, deleteRequestedFor(urlMatching( + "/disqualified-officers/delete/1kETe9SJWIp9OlvZgO1xmjyt5_s/internal"))); + } + @After public void shutdownWiremock(){ if (wireMockServer != null) @@ -150,6 +160,11 @@ private void stubPutDisqualification(String type, int responseCode) { .willReturn(aResponse().withStatus(responseCode))); } + private void stubDeleteDisqualification(int responseCode) { + stubFor(delete(urlEqualTo("/disqualified-officers/delete/1kETe9SJWIp9OlvZgO1xmjyt5_s/internal")) + .willReturn(aResponse().withStatus(responseCode))); + } + private void countDown() throws Exception { CountDownLatch countDownLatch = new CountDownLatch(1); countDownLatch.await(5, TimeUnit.SECONDS); diff --git a/src/itest/resources/data/delete_disqualification.json b/src/itest/resources/data/delete_disqualification.json new file mode 100644 index 0000000..812db05 --- /dev/null +++ b/src/itest/resources/data/delete_disqualification.json @@ -0,0 +1,4 @@ +{ + "action": "DELETE", + "officer_id": "1234567890" +} \ No newline at end of file diff --git a/src/itest/resources/features/Delete.feature b/src/itest/resources/features/Delete.feature new file mode 100644 index 0000000..7997435 --- /dev/null +++ b/src/itest/resources/features/Delete.feature @@ -0,0 +1,6 @@ +Feature: Delete + + Scenario: send DELETE request to data Api + Given the application is running + When the consumer receives a delete payload + Then a DELETE request is sent to the disqualifications api with the encoded Id \ No newline at end of file diff --git a/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/consumer/DisqualifiedOfficersDeltaConsumer.java b/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/consumer/DisqualifiedOfficersDeltaConsumer.java index 39a97be..82f2ada 100644 --- a/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/consumer/DisqualifiedOfficersDeltaConsumer.java +++ b/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/consumer/DisqualifiedOfficersDeltaConsumer.java @@ -53,7 +53,11 @@ public void receiveMainMessages(Message chsDeltaMessage, logger.info("A new message read from " + topic + " topic with payload: " + chsDeltaMessage.getPayload()); try { - deltaProcessor.processDelta(chsDeltaMessage); + if (Boolean.TRUE.equals(chsDeltaMessage.getPayload().getIsDelete())) { + deltaProcessor.processDelete(chsDeltaMessage); + } else { + deltaProcessor.processDelta(chsDeltaMessage); + } } catch (Exception exception) { logger.error(String.format("Exception occurred while processing the topic: %s " + "with message: %s", topic, chsDeltaMessage), exception); diff --git a/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/processor/DisqualifiedOfficersDeltaProcessor.java b/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/processor/DisqualifiedOfficersDeltaProcessor.java index 27602d0..729aaab 100644 --- a/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/processor/DisqualifiedOfficersDeltaProcessor.java +++ b/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/processor/DisqualifiedOfficersDeltaProcessor.java @@ -11,6 +11,7 @@ import org.springframework.messaging.MessageHeaders; import org.springframework.stereotype.Component; +import uk.gov.companieshouse.api.delta.DisqualificationDeleteDelta; import uk.gov.companieshouse.api.delta.DisqualificationDelta; import uk.gov.companieshouse.api.delta.DisqualificationOfficer; import uk.gov.companieshouse.api.disqualification.InternalCorporateDisqualificationApi; @@ -19,6 +20,7 @@ import uk.gov.companieshouse.delta.ChsDelta; import uk.gov.companieshouse.disqualifiedofficers.delta.exception.NonRetryableErrorException; import uk.gov.companieshouse.disqualifiedofficers.delta.exception.RetryableErrorException; +import uk.gov.companieshouse.disqualifiedofficers.delta.mapper.MapperUtils; import uk.gov.companieshouse.disqualifiedofficers.delta.service.api.ApiClientService; import uk.gov.companieshouse.disqualifiedofficers.delta.transformer.DisqualifiedOfficersApiTransformer; import uk.gov.companieshouse.logging.Logger; @@ -96,6 +98,27 @@ public void processDelta(Message chsDelta) { } } + /** + * Process CHS Delta delete message. + */ + public void processDelete(Message chsDelta) { + final ChsDelta payload = chsDelta.getPayload(); + final String logContext = payload.getContextId(); + final String officerId; + + ObjectMapper mapper = new ObjectMapper(); + DisqualificationDeleteDelta disqualifiedOfficersDelete; + try { + disqualifiedOfficersDelete = mapper.readValue(payload.getData(), + DisqualificationDeleteDelta.class); + } catch (Exception ex) { + throw new NonRetryableErrorException( + "Error when extracting disqualified-officers delete delta", ex); + } + officerId = MapperUtils.encode(disqualifiedOfficersDelete.getOfficerId()); + apiClientService.deleteDisqualification(logContext, officerId); + } + /** * Invoke Disqualifications Data API. */ diff --git a/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientService.java b/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientService.java index ce2f3e7..05858ff 100644 --- a/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientService.java +++ b/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientService.java @@ -26,4 +26,9 @@ ApiResponse putDisqualification( final String log, final String officerId, final InternalCorporateDisqualificationApi internalDisqualificationApi); + + /** + * Delete disqualification. + */ + ApiResponse deleteDisqualification(final String log, final String officerId); } \ No newline at end of file diff --git a/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientServiceImpl.java b/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientServiceImpl.java index 5b71df5..945a250 100644 --- a/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientServiceImpl.java +++ b/src/main/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientServiceImpl.java @@ -84,6 +84,21 @@ public ApiResponse putDisqualification(final String log, final String offi .upsert(uri, internalDisqualificationApi)); } + @Override + public ApiResponse deleteDisqualification( + final String log, + final String officerId) { + final String uri = + String.format("/disqualified-officers/delete/%s/internal", officerId); + + Map logMap = createLogMap(officerId,"DELETE", uri); + logger.infoContext(log, String.format("DELETE %s", uri), logMap); + + return executeOp(log, "deleteDisqualification", uri, + getApiClient(log).privateDisqualificationResourceHandler() + .deleteDisqualification(uri)); + } + private Map createLogMap(String officerId, String method, String path) { final Map logMap = new HashMap<>(); logMap.put("officer_id", officerId); diff --git a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/processor/DisqualifiedOfficersProcessorTest.java b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/processor/DisqualifiedOfficersProcessorTest.java index b4ab25a..c3eee4f 100644 --- a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/processor/DisqualifiedOfficersProcessorTest.java +++ b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/processor/DisqualifiedOfficersProcessorTest.java @@ -14,6 +14,7 @@ import uk.gov.companieshouse.delta.ChsDelta; import uk.gov.companieshouse.disqualifiedofficers.delta.exception.NonRetryableErrorException; import uk.gov.companieshouse.disqualifiedofficers.delta.exception.RetryableErrorException; +import uk.gov.companieshouse.disqualifiedofficers.delta.mapper.MapperUtils; import uk.gov.companieshouse.disqualifiedofficers.delta.service.api.ApiClientService; import uk.gov.companieshouse.disqualifiedofficers.delta.transformer.DisqualifiedOfficersApiTransformer; import uk.gov.companieshouse.disqualifiedofficers.delta.utils.TestHelper; @@ -53,7 +54,7 @@ void setUp() { @Test @DisplayName("Transforms a kafka message containing a ChsDelta payload into a DisqualificationDelta") void When_ValidChsDeltaMessage_Expect_ValidDisqualificationDeltaMapping() throws IOException { - Message mockChsDeltaMessage = testHelper.createChsDeltaMessage(); + Message mockChsDeltaMessage = testHelper.createChsDeltaMessage(false); DisqualificationDelta expectedDelta = testHelper.createDisqualificationDelta(); InternalNaturalDisqualificationApi apiObject = testHelper.createDisqualificationApi(); final ApiResponse response = new ApiResponse<>(HttpStatus.OK.value(), null, null); @@ -74,9 +75,8 @@ void When_InvalidChsDeltaMessage_Expect_NonRetryableError() { @Test void When_ApiReturns500_Expect_RetryableError() throws IOException { - Message mockChsDeltaMessage = testHelper.createChsDeltaMessage(); + Message mockChsDeltaMessage = testHelper.createChsDeltaMessage(false); InternalNaturalDisqualificationApi apiObject = testHelper.createDisqualificationApi(); - final ApiResponse response = new ApiResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), null, null); when(apiClientService.putDisqualification(any(),any(), eq(apiObject))).thenThrow(new RetryableErrorException("")); when(transformer.transformNaturalDisqualification(any())).thenReturn(apiObject); assertThrows(RetryableErrorException.class, ()->deltaProcessor.processDelta(mockChsDeltaMessage)); @@ -88,4 +88,23 @@ void When_Transformer_Fails_Expect_RetryableError() throws IOException { when(transformer.transformNaturalDisqualification(any())).thenCallRealMethod(); assertThrows(RetryableErrorException.class, ()->deltaProcessor.processDelta(mockChsDeltaMessage)); } + + @Test + void When_Valid_Delete_Message_Received_Delete_Endpoint_is_Called() throws IOException { + Message mockChsDeltaMessage = testHelper.createChsDeltaMessage(true); + final ApiResponse response = new ApiResponse<>(HttpStatus.OK.value(), null, null); + String encoded_id = MapperUtils.encode("1234567890"); + + when(apiClientService.deleteDisqualification("context_id", encoded_id)).thenReturn(response); + + deltaProcessor.processDelete(mockChsDeltaMessage); + + verify(apiClientService).deleteDisqualification("context_id", encoded_id); + } + + @Test + void When_InvalidDeleteMessage_Expect_NonRetryableError() { + Message mockChsDeltaMessage = testHelper.createInvalidChsDeltaMessage(); + assertThrows(NonRetryableErrorException.class, ()->deltaProcessor.processDelete(mockChsDeltaMessage)); + } } \ No newline at end of file diff --git a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/serialization/ChsDeltaDeserializerTest.java b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/serialization/ChsDeltaDeserializerTest.java index 46d674a..4e90898 100644 --- a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/serialization/ChsDeltaDeserializerTest.java +++ b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/serialization/ChsDeltaDeserializerTest.java @@ -20,7 +20,7 @@ public class ChsDeltaDeserializerTest { @Test void When_deserialize_Expect_ValidChsDeltaObject() { - ChsDelta chsDelta = new ChsDelta("data", 1, "context_id"); + ChsDelta chsDelta = new ChsDelta("data", 1, "context_id",false); byte[] data = encodedData(chsDelta); ChsDelta deserializedObject = deserializer.deserialize("", data); diff --git a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/serialization/ChsDeltaSerializerTest.java b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/serialization/ChsDeltaSerializerTest.java index dc258b4..49aa326 100644 --- a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/serialization/ChsDeltaSerializerTest.java +++ b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/serialization/ChsDeltaSerializerTest.java @@ -26,8 +26,8 @@ public void init() { @Test void When_serialize_Expect_ValidByteArray() { - ChsDelta chsDelta = new ChsDelta("data", 1, "context_id"); - byte[] data = { 0x08, 0x64, 0x61, 0x74, 0x61, 0x02, 0x14, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x78, 0x74, 0x5F, 0x69, 0x64 }; + ChsDelta chsDelta = new ChsDelta("data", 1, "context_id",false); + byte[] data = { 0x08, 0x64, 0x61, 0x74, 0x61, 0x02, 0x14, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x78, 0x74, 0x5F, 0x69, 0x64, 0x0 }; byte[] serializedObject = serializer.serialize("", chsDelta); diff --git a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientServiceImplTest.java b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientServiceImplTest.java index 9500b77..df72362 100644 --- a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientServiceImplTest.java +++ b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/service/api/ApiClientServiceImplTest.java @@ -11,6 +11,7 @@ import uk.gov.companieshouse.api.disqualification.InternalNaturalDisqualificationApi; import uk.gov.companieshouse.api.disqualification.InternalCorporateDisqualificationApi; import uk.gov.companieshouse.api.error.ApiErrorResponseException; +import uk.gov.companieshouse.api.handler.delta.disqualification.request.PrivateDisqualificationDelete; import uk.gov.companieshouse.api.handler.delta.disqualification.request.PrivateNaturalDisqualificationUpsert; import uk.gov.companieshouse.api.handler.delta.disqualification.request.PrivateCorporateDisqualificationUpsert; import uk.gov.companieshouse.api.handler.exception.URIValidationException; @@ -80,4 +81,23 @@ void putCorporateDisqualification() throws ApiErrorResponseException, URIValidat } + @Test + void deleteDisqualification() { + final ApiResponse expectedResponse = new ApiResponse<>(HttpStatus.OK.value(), null, null); + ApiClientServiceImpl apiClientServiceSpy = Mockito.spy(apiClientService); + doReturn(expectedResponse).when(apiClientServiceSpy).executeOp(anyString(), anyString(), + anyString(), + any(PrivateDisqualificationDelete.class)); + + ApiResponse response = apiClientServiceSpy.deleteDisqualification("context_id", + "ZTgzYWQwODAzMGY1ZDNkNGZiOTAxOWQ1YzJkYzc5MWViMTE3ZjQxZA=="); + verify(apiClientServiceSpy).executeOp(anyString(), eq("deleteDisqualification"), + eq("/disqualified-officers/delete/" + + "ZTgzYWQwODAzMGY1ZDNkNGZiOTAxOWQ1YzJkYzc5MWViMTE3ZjQxZA==/internal"), + any(PrivateDisqualificationDelete.class)); + + assertThat(response).isEqualTo(expectedResponse); + + } + } diff --git a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/utils/TestHelper.java b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/utils/TestHelper.java index d4fe20d..8b7f3a0 100644 --- a/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/utils/TestHelper.java +++ b/src/test/java/uk/gov/companieshouse/disqualifiedofficers/delta/utils/TestHelper.java @@ -21,9 +21,10 @@ public class TestHelper { - public Message createChsDeltaMessage() throws IOException { + public Message createChsDeltaMessage(boolean isDelete) throws IOException { + String filename = isDelete ? "disqualified-officers-delete-example.json":"disqualified-officers-delta-example.json"; InputStreamReader exampleJsonPayload = new InputStreamReader( - ClassLoader.getSystemClassLoader().getResourceAsStream("disqualified-officers-delta-example.json")); + ClassLoader.getSystemClassLoader().getResourceAsStream(filename)); String data = FileCopyUtils.copyToString(exampleJsonPayload); return buildMessage(data); @@ -119,6 +120,14 @@ public Message createBrokenChsDeltaMessage() throws IOException { return buildMessage(data); } + public Message createDeleteMessage() throws IOException { + InputStreamReader exampleJsonPayload = new InputStreamReader( + ClassLoader.getSystemClassLoader().getResourceAsStream("disqualified-officers-delete-example.json")); + String data = FileCopyUtils.copyToString(exampleJsonPayload); + + return buildMessage(data); + } + private Message buildMessage(String data) { ChsDelta mockChsDelta = ChsDelta.newBuilder() .setData(data) diff --git a/src/test/resources/disqualified-officers-delete-example.json b/src/test/resources/disqualified-officers-delete-example.json new file mode 100644 index 0000000..812db05 --- /dev/null +++ b/src/test/resources/disqualified-officers-delete-example.json @@ -0,0 +1,4 @@ +{ + "action": "DELETE", + "officer_id": "1234567890" +} \ No newline at end of file