-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from companieshouse/feature/add_integration_tests
Add integration tests
- Loading branch information
Showing
18 changed files
with
792 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...st/java/uk/gov/companieshouse/disqualifiedofficers/delta/CucumberFeaturesRunnerITest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/data/TestData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...gov/companieshouse/disqualifiedofficers/delta/matcher/DisqualificationRequestMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
87 changes: 87 additions & 0 deletions
87
src/itest/java/uk/gov/companieshouse/disqualifiedofficers/delta/steps/CommonSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.