Skip to content

Commit

Permalink
Merge pull request #25 from companieshouse/feature/add_integration_tests
Browse files Browse the repository at this point in the history
Add integration tests
  • Loading branch information
amartin7663 authored Apr 25, 2022
2 parents 153e0d4 + 39126a2 commit 8283b91
Show file tree
Hide file tree
Showing 18 changed files with 792 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/

.idea
*.iml
53 changes: 52 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
<map-struct.version>1.4.2.Final</map-struct.version>
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<maven-build-helper-plugin.version>3.2.0</maven-build-helper-plugin.version>
<maven-failsafe-plugin.version>3.0.0-M5</maven-failsafe-plugin.version>
<maven-failsafe-plugin.version>2.12.4</maven-failsafe-plugin.version>
<docker-maven-plugin.version>1.0.0</docker-maven-plugin.version>
<io-cucumber.version>7.2.3</io-cucumber.version>
<ch-kafka.version>1.4.2</ch-kafka.version>
<structured-logging.version>1.9.12</structured-logging.version>
<kafka-models.version>1.0.25</kafka-models.version>
<private-api-sdk-java.version>2.0.146</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>
<wiremock.standalone.version>2.32.0</wiremock.standalone.version>
<skip.integration.tests>false</skip.integration.tests>
<skip.unit.tests>false</skip.unit.tests>

Expand Down Expand Up @@ -124,6 +126,55 @@
<version>${test-containers.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>${io-cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${io-cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${io-cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${io-cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>${wiremock.standalone.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.conscrypt</groupId>
<artifactId>conscrypt-openjdk-uber</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars-helpers</artifactId>
</exclusion>
<exclusion>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<pluginManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.companieshouse.disqualifiedofficers.delta;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.spring.CucumberContextConfiguration;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/itest/resources/features",
plugin = {"pretty", "json:target/cucumber-report.json"})
@CucumberContextConfiguration
public class CucumberFeaturesRunnerITest extends AbstractIntegrationTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Map<String, Object> consumerConfigs(KafkaContainer kafkaContainer) {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "insolvency-delta-consumer");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "disqualified-officer-delta-consumer");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ChsDeltaDeserializer.class);
return props;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.companieshouse.disqualifiedofficers.delta.data;

import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestData {

public static String getInputData(String officerType, String disqType) {
disqType = disqType.replace(' ', '_');
String path = "src/itest/resources/data/" + officerType + '_' + disqType + "_in.json";
return readFile(path);
}

public static String getOutputData(String officerType, String disqType) {
disqType = disqType.replace(' ', '_');
String path = "src/itest/resources/data/" + officerType + '_' + disqType + "_out.json";
return readFile(path).replaceAll("\n", "");
}

private static String readFile(String path) {
String data;
try {
data = FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(new File(path))));
} catch (IOException e) {
data = null;
}
return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package uk.gov.companieshouse.disqualifiedofficers.delta.matcher;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.github.tomakehurst.wiremock.matching.MatchResult;
import com.github.tomakehurst.wiremock.matching.ValueMatcher;
import uk.gov.companieshouse.logging.Logger;

/**
* Custom matcher class used to match requests made by the consumer to the
* data api. The url, request type and request body are compared.
*/
public class DisqualificationRequestMatcher implements ValueMatcher<Request> {

private String expectedOutput;
private String type;
private Logger logger;

public DisqualificationRequestMatcher(Logger logger, String type, String output) {
this.type = type;
this.expectedOutput = output;
this.logger = logger;
}

@Override
public MatchResult match(Request value) {

return MatchResult.aggregate(matchUrl(value.getUrl()), matchMethod(value.getMethod()),
matchBody(value.getBodyAsString()));
}

private MatchResult matchUrl(String actualUrl) {
String expectedUrl = "/disqualified-officers/" + type + "/1234567890/internal";

MatchResult urlResult = MatchResult.of(expectedUrl.equals(actualUrl));

if (! urlResult.isExactMatch()) {
logger.error("url does not match expected: <" + expectedUrl + "> actual: <" + actualUrl + ">");
}

return urlResult;
}

private MatchResult matchMethod(RequestMethod actualMethod) {
RequestMethod expectedMethod = RequestMethod.PUT;

MatchResult typeResult = MatchResult.of(expectedMethod.equals(actualMethod));

if (! typeResult.isExactMatch()) {
logger.error("Method does not match expected: <" + expectedMethod + "> actual: <" + actualMethod + ">");
}

return typeResult;
}

private MatchResult matchBody(String actualBody) {

ObjectMapper mapper = new ObjectMapper();

MatchResult bodyResult;
JsonNode expectedBody;
try {
expectedBody = mapper.readTree(expectedOutput);
} catch (JsonProcessingException e) {
logger.error("Could not process expectedBody JSON: " + e);
return MatchResult.of(false);
}

JsonNode actual;
try {
actual = mapper.readTree(actualBody);
} catch (JsonProcessingException e) {
logger.error("Could not process actualBody JSON: " + e);
return MatchResult.of(false);
}

bodyResult = MatchResult.of(expectedBody.equals(actual));

if (! bodyResult.isExactMatch()) {
logger.error("Body does not match expected: <" + expectedBody + "> actual: <" + actualBody + ">");
}

return bodyResult;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package uk.gov.companieshouse.disqualifiedofficers.delta.steps;

import io.cucumber.java.After;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import uk.gov.companieshouse.delta.ChsDelta;
import uk.gov.companieshouse.disqualifiedofficers.delta.data.TestData;
import uk.gov.companieshouse.disqualifiedofficers.delta.matcher.DisqualificationRequestMatcher;
import uk.gov.companieshouse.logging.Logger;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

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 org.assertj.core.api.Assertions.assertThat;


public class CommonSteps {

@Value("${disqualified-officers.delta.topic.main}")
private String mainTopic;

@Value("${wiremock.server.port}")
private String port;

private static WireMockServer wireMockServer;

@Autowired
private KafkaTemplate<String, ChsDelta> kafkaTemplate;
@Autowired
private Logger logger;

private String type;
private String output;

@Given("the application is running")
public void theApplicationRunning() {
assertThat(kafkaTemplate).isNotNull();
}

@When("^the consumer receives a (natural|corporate) disqualification of (undertaking|court order)$")
public void theConsumerReceivesDisqualificationOfType(String officerType, String disqType) throws Exception {
configureWiremock();
stubPutDisqualification(officerType);
this.type = officerType;
this.output = TestData.getOutputData(officerType, disqType);

ChsDelta delta = new ChsDelta(TestData.getInputData(officerType, disqType), 1, "1");
kafkaTemplate.send(mainTopic, delta);

CountDownLatch countDownLatch = new CountDownLatch(1);
countDownLatch.await(5, TimeUnit.SECONDS);
}

@Then("a PUT request is sent to the disqualifications api with the transformed data")
public void putRequestIsSentToTheDisqualificationsApi() {
verify(1, requestMadeFor(new DisqualificationRequestMatcher(logger, type, output)));
}

@After
public void shutdownWiremock(){
wireMockServer.stop();
}

private void configureWiremock() {
wireMockServer = new WireMockServer(Integer.parseInt(port));
wireMockServer.start();
configureFor("localhost", Integer.parseInt(port));
}

private void stubPutDisqualification(String type) {
stubFor(put(urlEqualTo("/disqualified-officers/" + type + "/1234567890/internal"))
.willReturn(aResponse().withStatus(200)));
}
}

57 changes: 57 additions & 0 deletions src/itest/resources/data/corporate_court_order_in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"disqualified_officer": [
{"corporate_ind" : "1",
"officer_disq_id": "3000034602",
"external_number": "166284060001",
"officer_id": "1234567890",
"officer_detail_id": "3002560732",
"title": "",
"forename": "",
"middle_name": "",
"surname": "BABYLON INCORPORATION LIMITED",
"honours": "",
"nationality": "",
"registered_number": "00053723",
"registered_location": "SCOTLAND",
"disqualifications": [
{
"disq_eff_date": "20150218",
"disq_end_date": "20250217",
"disq_type": "ORDER",
"hearing_date": "20180214",
"section_of_the_act": "CDDO 1986 A11A",
"court_ref": "INV3975227",
"court_name": "Insolvency Service",
"address": {
"premise": "3000",
"address_line_1": "SYLVESTERucy AVENUE",
"address_line_2": "Skewen",
"locality": "Neath",
"region": "",
"country": "Wales",
"postal_code": "SA10 6RR"
},
"variation_court": "SWINDLERS",
"variation_court_ref_no": "1",
"var_instrument_start_date": "20210217",
"company_names": [
"CONSORTIUM TECHNOLOGY LIMITED"
]
}
],
"exemptions": [{
"court_name": "rinder",
"granted_on": "20140203",
"expires_on": "20160412",
"purpose": "ALPHABET",
"company_names": [
"123 LTD",
"321 LTD"

]
}]
}
],
"CreatedTime": "16-FEB-22 14.40.57.000000",
"delta_at": "20211008152823383176"
}
Loading

0 comments on commit 8283b91

Please sign in to comment.