Skip to content

Commit 1217a5a

Browse files
authored
Fix: [AEA-4017] - handle full request including headers (#84)
## Summary - Routine Change ### Details - handle headers and body passed to lambda - log correlation id headers - tidy up dev container to allow java tests to be run and debugged
1 parent fdcb64d commit 1217a5a

8 files changed

Lines changed: 147 additions & 7 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ RUN apt-get update \
66
&& apt-get -y install --no-install-recommends htop vim curl git build-essential \
77
libffi-dev libssl-dev libxml2-dev libxslt1-dev libjpeg8-dev libbz2-dev \
88
zlib1g-dev unixodbc unixodbc-dev libsecret-1-0 libsecret-1-dev libsqlite3-dev \
9-
openjdk-8-jdk jq apt-transport-https ca-certificates gnupg-agent \
9+
jq apt-transport-https ca-certificates gnupg-agent \
1010
software-properties-common bash-completion python3-pip make libbz2-dev \
1111
libreadline-dev libsqlite3-dev wget llvm libncurses5-dev libncursesw5-dev \
12-
xz-utils tk-dev liblzma-dev netcat ruby-full build-essential zlib1g-dev
12+
xz-utils tk-dev liblzma-dev netcat ruby-full build-essential zlib1g-dev \
13+
&& apt remove -y openjdk-8-jdk-headless openjdk-8-jre-headless openjdk-8-jre
1314

1415
# install aws stuff
1516
RUN wget -O /tmp/awscliv2.zip "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" && \

.devcontainer/devcontainer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"dbaeumer.vscode-eslint",
3939
"lfm.vscode-makefile-term",
4040
"GrapeCity.gc-excelviewer",
41-
"redhat.vscode-xml",
4241
"streetsidesoftware.code-spell-checker",
4342
"timonwong.shellcheck",
4443
"github.vscode-github-actions",

licenses/licenses.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@
4747
<names>
4848
<name>The MIT License</name>
4949
<name>MIT License</name>
50+
<name>MIT</name>
5051
</names>
5152
<urls>
5253
<url>http://code.google.com/p/mockito/wiki/License</url>
5354
<url>http://www.opensource.org/licenses/mit-license</url>
55+
<url>https://opensource.org/licenses/MIT</url>
5456
</urls>
5557
</license>
5658
<license>

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@
151151
<artifactId>junit-jupiter-api</artifactId>
152152
<scope>test</scope>
153153
</dependency>
154+
<dependency>
155+
<groupId>org.mockito</groupId>
156+
<artifactId>mockito-core</artifactId>
157+
<version>5.11.0</version>
158+
<scope>test</scope>
159+
</dependency>
154160
</dependencies>
155161

156162
<build>

src/main/java/software/nhs/fhirvalidator/handler/HandlerStream.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88

99
import com.amazonaws.services.lambda.runtime.Context;
1010
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
11+
import com.google.gson.JsonObject;
12+
import com.google.gson.JsonParser;
1113

1214
import org.apache.logging.log4j.LogManager;
1315
import org.apache.logging.log4j.Logger;
1416

1517
import software.amazon.lambda.powertools.logging.Logging;
18+
import software.amazon.lambda.powertools.logging.LoggingUtils;
1619
import software.nhs.fhirvalidator.controller.ValidateController;
1720
import software.nhs.fhirvalidator.util.ResourceUtils;
1821

@@ -25,7 +28,7 @@ public HandlerStream() {
2528
log.info("Creating the Validator instance for the first time...");
2629
String manifest_file = System.getenv("PROFILE_MANIFEST_FILE");
2730
if (manifest_file == null) {
28-
manifest_file = "nhs_digital.manifest.json";
31+
manifest_file = "uk_core.manifest.json";
2932
}
3033
log.info(String.format("Using manifest file : %s", manifest_file));
3134

@@ -49,8 +52,22 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
4952
}
5053
String rawInput = result.toString();
5154
log.info(rawInput);
52-
53-
String validatorResult = validateController.validate(rawInput);
55+
JsonObject jsonPayload = JsonParser.parseString(rawInput).getAsJsonObject();
56+
JsonObject headers = jsonPayload.get("headers").getAsJsonObject();
57+
String xRequestID = headers.get("x-request-id") == null ? "" : headers.get("x-request-id").getAsString();
58+
LoggingUtils.appendKey("x-request-id", xRequestID);
59+
String nhsdCorrelationID = headers.get("nhsd-correlation-id") == null ? "" : headers.get("nhsd-correlation-id").getAsString();
60+
LoggingUtils.appendKey("nhsd-correlation-id", nhsdCorrelationID);
61+
String nhsdRequestID = headers.get("nhsd-request-id") == null ? "" : headers.get("nhsd-request-id").getAsString();
62+
LoggingUtils.appendKey("nhsd-request-id", nhsdRequestID);
63+
String xCorrelationID = headers.get("x-correlation-id") == null ? "" : headers.get("x-correlation-id").getAsString();
64+
LoggingUtils.appendKey("x-correlation-id", xCorrelationID);
65+
String apigwRequestID = headers.get("apigw-request-id") == null ? "" : headers.get("apigw-request-id").getAsString();
66+
LoggingUtils.appendKey("apigw-request-id", apigwRequestID);
67+
68+
log.info("Calling validate function");
69+
String validatorResult = validateController.validate(jsonPayload.get("body").toString());
70+
log.info(validatorResult);
5471

5572
try (PrintWriter writer = new PrintWriter(outputStream)) {
5673
writer.print(validatorResult);
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
package software.nhs.fhirvalidator;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertTrue;
45

6+
import java.io.ByteArrayInputStream;
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
11+
512
import nl.altindag.log.LogCaptor;
613
import software.nhs.fhirvalidator.handler.HandlerStream;
14+
import software.nhs.fhirvalidator.util.ResourceUtils;
15+
16+
import com.amazonaws.services.lambda.runtime.Context;
17+
import com.google.gson.JsonObject;
18+
import com.google.gson.JsonParser;
719

820
import org.junit.jupiter.api.Test;
21+
import static org.mockito.Mockito.mock;
922

1023
class HandlerTest {
1124

1225
@Test
13-
void logInfoAndWarnMessages() {
26+
void logStartupMessage() {
1427
LogCaptor logCaptor = LogCaptor.forClass(HandlerStream.class);
1528

1629
new HandlerStream();
1730

1831
assertTrue(logCaptor.getInfoLogs().contains("Validator is ready"));
32+
}
33+
34+
@Test
35+
void handlerCanProcessEvent() throws IOException {
36+
String stepFunctionEvent = ResourceUtils.getResourceContent("examples/stepFunctionEvent.json");
37+
HandlerStream handlerStream = new HandlerStream();
38+
39+
InputStream inputStream = new ByteArrayInputStream(stepFunctionEvent.getBytes());
40+
OutputStream outputStream = new ByteArrayOutputStream();
41+
42+
handlerStream.handleRequest(inputStream, outputStream, mock(Context.class));
43+
44+
JsonObject actualJsonResult = JsonParser.parseString(outputStream.toString()).getAsJsonObject();
45+
46+
String expectedResult = ResourceUtils.getResourceContent("results/stepFunctionResult.json");
47+
JsonObject expectedJsonResult = JsonParser.parseString(expectedResult).getAsJsonObject();
1948

49+
assertEquals(expectedJsonResult, actualJsonResult);
2050
}
2151
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"body": {
3+
"resourceType": "Bundle",
4+
"type": "transaction",
5+
"entry": [
6+
{
7+
"fullUrl": "urn:uuid:4d70678c-81e4-4ff4-8c67-17596fd0aa46",
8+
"resource": {
9+
"resourceType": "Task",
10+
"id": "4d70678c-81e4-4ff4-8c67-17596fd0aa46",
11+
"basedOn": [
12+
{
13+
"identifier": {
14+
"system": "https://fhir.nhs.uk/Id/prescription-order-number",
15+
"value": "16B2E0-A83008-81C13H"
16+
}
17+
}
18+
],
19+
"status": "completed",
20+
"businessStatus": {
21+
"coding": [
22+
{
23+
"system": "https://fhir.nhs.uk/CodeSystem/task-businessStatus-nppt",
24+
"code": "dispatched"
25+
}
26+
]
27+
},
28+
"intent": "order",
29+
"focus": {
30+
"identifier": {
31+
"system": "https://fhir.nhs.uk/Id/prescription-order-item-number",
32+
"value": "6989b7bd-8db6-428c-a593-4022e3044c00"
33+
}
34+
},
35+
"for": {
36+
"identifier": {
37+
"system": "https://fhir.nhs.uk/Id/nhs-number",
38+
"value": "9449304130"
39+
}
40+
},
41+
"lastModified": "2023-10-11T10:11:12Z",
42+
"owner": {
43+
"identifier": {
44+
"system": "https://fhir.nhs.uk/Id/ods-organization-code",
45+
"value": "C9Z1O"
46+
}
47+
}
48+
},
49+
"request": {
50+
"method": "POST",
51+
"url": "Task"
52+
}
53+
}
54+
]
55+
},
56+
"headers": {
57+
"Accept": "*/*",
58+
"Accept-Encoding": "gzip, deflate, br",
59+
"apigw-request-id": "5600dff8-6553-42e3-a0ff-c2c9d04d3de6",
60+
"Authorization": "Bearer nSoPA0PNA2JG8phqPwtCn9ys1G6z",
61+
"Content-Type": "application/json",
62+
"Host": "psu-pr-150.dev.eps.national.nhs.uk",
63+
"nhsd-nhslogin-user": "P9:9912003071",
64+
"Postman-Token": "8ca7a240-0b9e-4c1f-90d8-a526faa533e2",
65+
"User-Agent": "PostmanRuntime/7.37.3",
66+
"X-Amzn-Trace-Id": "Root=1-66290323-47ec5ff50d05147878b956d3",
67+
"x-correlation-id": "ead03d9a-d14e-414d-9e01-217319210ddd",
68+
"X-Forwarded-For": "86.5.218.71",
69+
"X-Forwarded-Port": "443",
70+
"X-Forwarded-Proto": "https",
71+
"x-request-id": "c39f1caa-cd63-438d-bc2b-fafd3f02ed41"
72+
},
73+
"querystring": {},
74+
"path": {}
75+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"resourceType": "OperationOutcome",
3+
"issue": [
4+
{
5+
"severity": "information",
6+
"code": "informational",
7+
"diagnostics": "No issues detected during validation"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)