Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CCD-5367 Task Completion - CCD Send Header from callback response to EXUI #297

Merged
merged 14 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class StubResponseController {

private static final Logger LOG = LoggerFactory.getLogger(StubResponseController.class);
static final String WIREMOCK_STUB_MAPPINGS_ENDPOINT = "/__admin/mappings";
static final List<String> CUSTOM_HEADERS = List.of("Client-Context");


@Value("${wiremock.server.host}")
private String mockHttpServerHost;
Expand Down Expand Up @@ -150,7 +152,7 @@ public ResponseEntity<Object> forwardGetRequests(HttpServletRequest request) thr
HttpRequest httpRequest = HttpRequest.newBuilder(uri)
.build();
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
return new ResponseEntity<Object>(httpResponse.body().toString(),
return new ResponseEntity<>(httpResponse.body().toString(),
HttpStatus.valueOf(httpResponse.statusCode()));
} catch (IOException e) {
LOG.error("Error occurred", e);
Expand All @@ -160,8 +162,6 @@ public ResponseEntity<Object> forwardGetRequests(HttpServletRequest request) thr
}
}



/**
* Forward POST requests to Wiremock Server and return POST responses to Test Stub Client.
*/
Expand All @@ -174,8 +174,16 @@ public ResponseEntity<Object> forwardPostRequests(HttpServletRequest request) th
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
return new ResponseEntity<Object>(httpResponse.body().toString(),
HttpStatus.valueOf(httpResponse.statusCode()));
HttpHeaders customHeaders = getCustomHeaders(httpResponse.headers());

if (customHeaders.size() > 0) {
return new ResponseEntity<>(httpResponse.body().toString(),
customHeaders,
HttpStatus.valueOf(httpResponse.statusCode()));
} else {
return new ResponseEntity<>(httpResponse.body().toString(),
HttpStatus.valueOf(httpResponse.statusCode()));
}
} catch (IOException e) {
LOG.error("Error occurred", e);
return ResponseEntity
Expand All @@ -196,7 +204,7 @@ public ResponseEntity<Object> forwardPutRequests(HttpServletRequest request) thr
.PUT(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
return new ResponseEntity<Object>(httpResponse.body().toString(),
return new ResponseEntity<>(httpResponse.body().toString(),
HttpStatus.valueOf(httpResponse.statusCode()));
} catch (IOException e) {
LOG.error("Error occurred", e);
Expand All @@ -217,7 +225,7 @@ public ResponseEntity<Object> forwardDeleteRequests(HttpServletRequest request)
.DELETE()
.build();
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
return new ResponseEntity<Object>(httpResponse.body().toString(),
return new ResponseEntity<>(httpResponse.body().toString(),
HttpStatus.valueOf(httpResponse.statusCode()));
} catch (IOException e) {
LOG.error("Error occurred", e);
Expand Down Expand Up @@ -246,7 +254,7 @@ public ResponseEntity<String> configureUser(@RequestBody IdamUserInfo userInfo)
.POST(HttpRequest.BodyPublishers.ofString(request))
.build();
HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
ResponseEntity<String> stringResponseEntity = new ResponseEntity<String>(httpResponse.body().toString(),
ResponseEntity<String> stringResponseEntity = new ResponseEntity<>(httpResponse.body().toString(),
HttpStatus.valueOf(httpResponse.statusCode()));

stringResponseEntity.getStatusCodeValue();
Expand All @@ -260,6 +268,18 @@ public ResponseEntity<String> configureUser(@RequestBody IdamUserInfo userInfo)
}
}

private HttpHeaders getCustomHeaders(java.net.http.HttpHeaders originalHeaders) {

HttpHeaders springHeaders = new HttpHeaders();
CUSTOM_HEADERS.forEach(context -> {
if (null != originalHeaders && originalHeaders.map().containsKey(context.toLowerCase())) {
springHeaders.put(context, originalHeaders.map().get(context.toLowerCase()));
}
});

return springHeaders;
}

private String asJson(Object object) throws JsonProcessingException {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import uk.gov.hmcts.reform.ccd.test.stubs.service.mock.server.MockHttpServer;
import uk.gov.hmcts.reform.ccd.test.stubs.service.util.HeadersProvider;

import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.util.Collections;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

Expand Down Expand Up @@ -115,6 +117,27 @@ void shouldReturnStatusOK_ForwardPostRequests() throws IOException, InterruptedE
assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK));
}

/**
* Forward POST requests unit test with status OK.
*/
@Test
@DisplayName("Test for forwardPostRequests() status OK - empty custom headers")
void shouldReturnStatusOK_ForwardPostRequestsForEmptyCustomHeaders() throws IOException, InterruptedException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
HttpResponse mockResponse = mock(HttpResponse.class);
Mockito.doReturn(mockResponse).when(mockHttpClient).send(any(), any());
when(mockResponse.body()).thenReturn("MOCK BODY");

HeadersProvider mockHeadersProvider = mock(HeadersProvider.class);
when(mockHeadersProvider.getHeaders()).thenReturn(Collections.emptyMap());

when(mockResponse.statusCode()).thenReturn(200);

ResponseEntity<Object> responseEntityReturned = stubResponseController.forwardPostRequests(mockRequest);
assertNotNull(responseEntityReturned);
assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK));
}

/**
* Forward POST requests unit test with exception thrown.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.gov.hmcts.reform.ccd.test.stubs.service.util;

import java.util.List;
import java.util.Map;

public interface HeadersProvider {
Map<String, List<String>> getHeaders();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.gov.hmcts.reform.ccd.test.stubs.service.util;

import java.net.http.HttpHeaders;
import java.util.List;
import java.util.Map;

public class HttpHeadersProvider implements HeadersProvider {
private final HttpHeaders httpHeaders;

public HttpHeadersProvider(HttpHeaders httpHeaders) {
this.httpHeaders = httpHeaders;
}

@Override
public Map<String, List<String>> getHeaders() {
return httpHeaders.map();
}
}
3 changes: 2 additions & 1 deletion wiremock/mappings/ccd/callback_about_to_submit.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
"Content-Type": "application/json",
"Client-Context": "ewogICJ1c2VyX3Rhc2siOiB7CiAgICAidGFza19kYXRhIjogewogICAgICAgInRhc2sxIjogIkluaXRpYWwgY2FsbCIsCiAgICAgICAidGFzazIiOiAiQWJvdXQgdG8gc3VibWl0IC0gY29tcGxldGVkIiwKICAgICAgICJ0YXNrMyI6ICJTdWJtaXR0ZWQgLSBwZW5kaW5nIgogICAgfQogIH0sCiAgImZpZWxkMSI6ICJTdHJpbmciCn0="
},
"jsonBody": {
"data": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
"Content-Type": "application/json",
"Client-Context": "ewogICJ1c2VyX3Rhc2siOiB7CiAgICAidGFza19kYXRhIjogewogICAgICAgInRhc2sxIjogIkluaXRpYWwgY2FsbCIsCiAgICAgICAidGFzazIiOiAiQWJvdXQgdG8gc3VibWl0IC0gY29tcGxldGVkIiwKICAgICAgICJ0YXNrMyI6ICJTdWJtaXR0ZWQgLSBwZW5kaW5nIgogICAgfQogIH0sCiAgImZpZWxkMSI6ICJTdHJpbmciCn0="
},
"jsonBody": {
"data": {
Expand Down
1 change: 1 addition & 0 deletions wiremock/mappings/ccd/callback_submitted.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"response": {
"status": 200,
"headers": {
"Client-Context": "ewogICJ1c2VyX3Rhc2siOiB7CiAgICAgICAgInRhc2tfZGF0YSI6IHsKCSAgICAgICAgInRhc2sxIjogIkluaXRpYWwgY2FsbCIsCgkgICAgICAgICJ0YXNrMiI6ICJBYm91dCB0byBzdWJtaXQgLSBjb21wbGV0ZWQiLAogICAgICAgICAgICAidGFzazMiOiAiU3VibWl0dGVkIC0gY29tcGxldGVkIgogICAgICAgIH0KICB9LAogICJmaWVsZDIiOiAiU3RyaW5nIiwKICAiY29tcGxldGVfdGFzayI6IHRydWUKfQ==",
"Content-Type": "application/json"
},
"jsonBody": {
Expand Down