Skip to content

Commit aeb3bff

Browse files
authored
Merge pull request #16 from companieshouse/feature/Set_up_Integration_Tests_
Feature/set up integration tests
2 parents 3bb3fe9 + 8de1f98 commit aeb3bff

File tree

9 files changed

+306
-1
lines changed

9 files changed

+306
-1
lines changed

pom.xml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,39 @@
121121
<artifactId>junit-jupiter</artifactId>
122122
<scope>test</scope>
123123
</dependency>
124-
125124
<dependency>
126125
<groupId>uk.gov.companieshouse</groupId>
127126
<artifactId>api-security-java</artifactId>
128127
<version>${api-security-java.version}</version>
129128
</dependency>
129+
<dependency>
130+
<groupId>io.cucumber</groupId>
131+
<artifactId>cucumber-core</artifactId>
132+
<version>${io-cucumber.version}</version>
133+
<scope>test</scope>
134+
</dependency>
135+
<dependency>
136+
<groupId>io.cucumber</groupId>
137+
<artifactId>cucumber-java</artifactId>
138+
<version>${io-cucumber.version}</version>
139+
<scope>test</scope>
140+
</dependency>
141+
<dependency>
142+
<groupId>io.cucumber</groupId>
143+
<artifactId>cucumber-junit</artifactId>
144+
<version>${io-cucumber.version}</version>
145+
<scope>test</scope>
146+
</dependency>
147+
<dependency>
148+
<groupId>io.cucumber</groupId>
149+
<artifactId>cucumber-spring</artifactId>
150+
<version>${io-cucumber.version}</version>
151+
</dependency>
152+
<dependency>
153+
<groupId>org.testcontainers</groupId>
154+
<artifactId>mongodb</artifactId>
155+
<scope>test</scope>
156+
</dependency>
130157
</dependencies>
131158

132159
<build>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package uk.gov.companieshouse.pscdataapi;
2+
3+
import io.cucumber.junit.Cucumber;
4+
import io.cucumber.junit.CucumberOptions;
5+
import io.cucumber.spring.CucumberContextConfiguration;
6+
import org.junit.runner.RunWith;
7+
8+
import uk.gov.companieshouse.pscdataapi.config.AbstractIntegrationTest;
9+
10+
@RunWith(Cucumber.class)
11+
@CucumberOptions(features = "src/itest/resources/features",
12+
plugin = {"pretty", "json:target/cucumber-report.json"})
13+
@CucumberContextConfiguration
14+
public class CucumberFeaturesRunnerITest extends AbstractIntegrationTest {
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package uk.gov.companieshouse.pscdataapi.config;
2+
3+
import org.springframework.boot.test.context.SpringBootTest;
4+
import org.springframework.test.annotation.DirtiesContext;
5+
import org.springframework.test.context.ActiveProfiles;
6+
7+
/**
8+
* Loads the application context.
9+
* Best place to mock your downstream calls.
10+
*/
11+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
12+
@DirtiesContext
13+
@ActiveProfiles({"test"})
14+
public abstract class AbstractIntegrationTest extends AbstractMongoConfig {
15+
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package uk.gov.companieshouse.pscdataapi.config;
2+
3+
import org.springframework.test.context.DynamicPropertyRegistry;
4+
import org.springframework.test.context.DynamicPropertySource;
5+
import org.testcontainers.containers.MongoDBContainer;
6+
import org.testcontainers.utility.DockerImageName;
7+
8+
/**
9+
* Mongodb configuration runs on test container.
10+
*/
11+
public class AbstractMongoConfig {
12+
13+
public static final MongoDBContainer mongoDBContainer = new MongoDBContainer(
14+
DockerImageName.parse("mongo:4"));
15+
16+
@DynamicPropertySource
17+
public static void setProperties(DynamicPropertyRegistry registry) {
18+
registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);
19+
mongoDBContainer.start();
20+
}
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package uk.gov.companieshouse.pscdataapi.config;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import org.springframework.http.ResponseEntity;
6+
7+
/**
8+
* Context to store the state.
9+
*/
10+
public enum CucumberContext {
11+
12+
CONTEXT;
13+
14+
private static final String RESPONSE = "RESPONSE";
15+
16+
private final ThreadLocal<Map<String, Object>> testContexts = ThreadLocal.withInitial(HashMap::new);
17+
18+
public ResponseEntity<?> getResponse() {
19+
return get(RESPONSE);
20+
}
21+
22+
public ResponseEntity<?> setResponse(ResponseEntity<?> response) {
23+
return set(RESPONSE, response);
24+
}
25+
26+
public <T> T get(String name) {
27+
return (T) testContexts.get()
28+
.get(name);
29+
}
30+
31+
public <T> T set(String name, T object) {
32+
testContexts.get()
33+
.put(name, object);
34+
return object;
35+
}
36+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package uk.gov.companieshouse.pscdataapi.steps;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import io.cucumber.java.After;
5+
import io.cucumber.java.Before;
6+
import io.cucumber.java.en.Given;
7+
import io.cucumber.java.en.Then;
8+
import io.cucumber.java.en.When;
9+
import org.assertj.core.api.Assertions;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.data.mongodb.core.MongoTemplate;
13+
import org.springframework.http.HttpEntity;
14+
import org.springframework.http.HttpHeaders;
15+
import org.springframework.http.HttpMethod;
16+
import org.springframework.http.MediaType;
17+
import org.springframework.http.ResponseEntity;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static uk.gov.companieshouse.pscdataapi.config.AbstractMongoConfig.mongoDBContainer;
21+
22+
import uk.gov.companieshouse.pscdataapi.config.CucumberContext;
23+
import uk.gov.companieshouse.pscdataapi.util.FileReaderUtil;
24+
import uk.gov.companieshouse.pscdataapi.repository.CompanyPscRepository;
25+
26+
import java.util.Collections;
27+
28+
public class PscDataSteps {
29+
private String contextId;
30+
31+
@Autowired
32+
private ObjectMapper objectMapper;
33+
34+
@Autowired
35+
private TestRestTemplate restTemplate;
36+
37+
@Autowired
38+
private MongoTemplate mongoTemplate;
39+
40+
@Autowired
41+
private CompanyPscRepository companyPscRepository;
42+
43+
private final String COMPANY_NUMBER = "34777772";
44+
45+
@Before
46+
public void dbCleanUp(){
47+
if (!mongoDBContainer.isRunning()) {
48+
mongoDBContainer.start();
49+
}
50+
companyPscRepository.deleteAll();
51+
}
52+
53+
@Given("Psc data api service is running")
54+
public void theApplicationRunning() {
55+
assertThat(restTemplate).isNotNull();
56+
}
57+
58+
@When("I send a PUT request with payload {string} file with notification id {string}")
59+
public void i_send_psc_statement_put_request_with_payload(String dataFile, String notificationId) {
60+
String data = FileReaderUtil.readFile("src/itest/resources/json/input/" + dataFile + ".json");
61+
62+
HttpHeaders headers = new HttpHeaders();
63+
headers.setContentType(MediaType.APPLICATION_JSON);
64+
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
65+
66+
this.contextId = "5234234234";
67+
CucumberContext.CONTEXT.set("contextId", this.contextId);
68+
headers.set("x-request-id", this.contextId);
69+
headers.set("ERIC-Identity", "TEST-IDENTITY");
70+
headers.set("ERIC-Identity-Type", "key");
71+
headers.set("ERIC-Authorised-Key-Roles", "*");
72+
73+
HttpEntity request = new HttpEntity(data, headers);
74+
String uri = "/company/{company_number}/persons-with-significant-control/{notfication_id}/full_record";
75+
ResponseEntity<Void> response = restTemplate.exchange(uri, HttpMethod.PUT, request, Void.class, COMPANY_NUMBER, notificationId);
76+
77+
CucumberContext.CONTEXT.set("statusCode", response.getStatusCodeValue());
78+
}
79+
80+
@Then("a record exists with id {string}")
81+
public void statement_exists(String statementId) {
82+
Assertions.assertThat(companyPscRepository.existsById(statementId)).isTrue();
83+
}
84+
85+
@Then("I should receive {int} status code")
86+
public void i_should_receive_status_code(Integer statusCode) {
87+
int expectedStatusCode = CucumberContext.CONTEXT.get("statusCode");
88+
Assertions.assertThat(expectedStatusCode).isEqualTo(statusCode);
89+
}
90+
91+
@After
92+
public void dbStop(){
93+
mongoDBContainer.stop();
94+
}
95+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package uk.gov.companieshouse.pscdataapi.util;
2+
3+
import org.springframework.util.FileCopyUtils;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
10+
public class FileReaderUtil {
11+
12+
public static String readFile(String path) {
13+
String data;
14+
try {
15+
data = FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(new File(path))));
16+
} catch (IOException e) {
17+
data = null;
18+
}
19+
return data;
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Feature: Process Psc Data Requests
2+
3+
Scenario Outline: Processing psc data information successfully
4+
5+
Given Psc data api service is running
6+
When I send a PUT request with payload "<data>" file with notification id "<notificationId>"
7+
Then I should receive 201 status code
8+
And a record exists with id "<notificationId>"
9+
10+
Examples:
11+
| notificationId | data |
12+
| ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ | psc_data_api |
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"_id": "YfTs9WeeqpXTqf6dc6FZ4C0H0ZZ",
3+
"internal_data": {
4+
"created_at" : "2021-11-02T08:47:45",
5+
"updated_at" :"2021-09-14T10:30:16.000Z",
6+
"updated_by" :"CHIPS",
7+
"delta_at" : "2023-11-20T08:47:45.378Z"
8+
},
9+
"external_data": {
10+
"notification_id": "ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ",
11+
"psc_id" : "ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ",
12+
"sensitive_data": {
13+
"usual_residential_address" : {
14+
"address_line_1" : "ura_line1",
15+
"address_line_2" : "ura_line2",
16+
"care_of" : "ura_care_of",
17+
"country" : "United Kingdom",
18+
"locality" : "Cardiff",
19+
"po_box" : "ura_po",
20+
"postal_code" : "CF2 1B6",
21+
"premises" : "URA",
22+
"region" : "ura_region"
23+
},
24+
"residential_address_same_as_service_address": true,
25+
"date_of_birth": {
26+
"day": 21,
27+
"year": 1995,
28+
"month": 10
29+
}
30+
},
31+
"data" : {
32+
"service_address" : {
33+
"address_line_1" : "sa_line1",
34+
"address_line_2" : "sa_line2",
35+
"care_of" : "sa_care_of",
36+
"country" : "United Kingdom",
37+
"locality" : "Cardiff",
38+
"po_box" : "sa_po",
39+
"postal_code" : "CF2 1B6",
40+
"premises" : "SA",
41+
"region" : "sa_region"
42+
},
43+
"natures_of_control": [
44+
"part-right-to-share-surplus-assets-75-to-100-percent"
45+
],
46+
"country_of_residence" : "United Kingdom",
47+
"links" : [{
48+
"self" : "/company/34777772/persons-with-significant-control/ZfTs9WeeqpXTqf6dc6FZ4C0H0ZZ"
49+
}],
50+
"nationality" : "British",
51+
"kind" : "individual-person-with-significant-control",
52+
"company_number": "34777772",
53+
"name_elements": {
54+
"title":"Mr.",
55+
"surname" : "JONES",
56+
"forename" : "PHIL",
57+
"middlename" : "tom"
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)