Skip to content

Commit

Permalink
Merge pull request #43 from companieshouse/feature/DSND-815-extract-i…
Browse files Browse the repository at this point in the history
…d-and-call-delete

Add delete functionality
  • Loading branch information
EmrysRoberts authored Jun 1, 2022
2 parents 279eb72 + ce3fd70 commit cd9e057
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<docker-maven-plugin.version>1.0.0</docker-maven-plugin.version>
<ch-kafka.version>1.4.2</ch-kafka.version>
<structured-logging.version>1.9.12</structured-logging.version>
<kafka-models.version>1.0.27</kafka-models.version>
<private-api-sdk-java.version>2.0.168</private-api-sdk-java.version>
<kafka-models.version>1.0.28</kafka-models.version>
<private-api-sdk-java.version>2.0.174</private-api-sdk-java.version>
<api-sdk-manager-java-library.version>1.0.4</api-sdk-manager-java-library.version>
<jib-maven-plugin.version>3.1.1</jib-maven-plugin.version>
<io-cucumber.version>7.2.3</io-cucumber.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/itest/resources/data/delete_disqualification.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"action": "DELETE",
"officer_id": "1234567890"
}
6 changes: 6 additions & 0 deletions src/itest/resources/features/Delete.feature
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public void receiveMainMessages(Message<ChsDelta> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -96,6 +98,27 @@ public void processDelta(Message<ChsDelta> chsDelta) {
}
}

/**
* Process CHS Delta delete message.
*/
public void processDelete(Message<ChsDelta> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ ApiResponse<Void> putDisqualification(
final String log,
final String officerId,
final InternalCorporateDisqualificationApi internalDisqualificationApi);

/**
* Delete disqualification.
*/
ApiResponse<Void> deleteDisqualification(final String log, final String officerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ public ApiResponse<Void> putDisqualification(final String log, final String offi
.upsert(uri, internalDisqualificationApi));
}

@Override
public ApiResponse<Void> deleteDisqualification(
final String log,
final String officerId) {
final String uri =
String.format("/disqualified-officers/delete/%s/internal", officerId);

Map<String,Object> 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<String, Object> createLogMap(String officerId, String method, String path) {
final Map<String, Object> logMap = new HashMap<>();
logMap.put("officer_id", officerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ChsDelta> mockChsDeltaMessage = testHelper.createChsDeltaMessage();
Message<ChsDelta> mockChsDeltaMessage = testHelper.createChsDeltaMessage(false);
DisqualificationDelta expectedDelta = testHelper.createDisqualificationDelta();
InternalNaturalDisqualificationApi apiObject = testHelper.createDisqualificationApi();
final ApiResponse<Void> response = new ApiResponse<>(HttpStatus.OK.value(), null, null);
Expand All @@ -74,9 +75,8 @@ void When_InvalidChsDeltaMessage_Expect_NonRetryableError() {

@Test
void When_ApiReturns500_Expect_RetryableError() throws IOException {
Message<ChsDelta> mockChsDeltaMessage = testHelper.createChsDeltaMessage();
Message<ChsDelta> mockChsDeltaMessage = testHelper.createChsDeltaMessage(false);
InternalNaturalDisqualificationApi apiObject = testHelper.createDisqualificationApi();
final ApiResponse<Void> 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));
Expand All @@ -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<ChsDelta> mockChsDeltaMessage = testHelper.createChsDeltaMessage(true);
final ApiResponse<Void> 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<ChsDelta> mockChsDeltaMessage = testHelper.createInvalidChsDeltaMessage();
assertThrows(NonRetryableErrorException.class, ()->deltaProcessor.processDelete(mockChsDeltaMessage));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,4 +81,23 @@ void putCorporateDisqualification() throws ApiErrorResponseException, URIValidat

}

@Test
void deleteDisqualification() {
final ApiResponse<Void> 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<Void> 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);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

public class TestHelper {

public Message<ChsDelta> createChsDeltaMessage() throws IOException {
public Message<ChsDelta> 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);
Expand Down Expand Up @@ -119,6 +120,14 @@ public Message<ChsDelta> createBrokenChsDeltaMessage() throws IOException {
return buildMessage(data);
}

public Message<ChsDelta> 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<ChsDelta> buildMessage(String data) {
ChsDelta mockChsDelta = ChsDelta.newBuilder()
.setData(data)
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/disqualified-officers-delete-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"action": "DELETE",
"officer_id": "1234567890"
}

0 comments on commit cd9e057

Please sign in to comment.