Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -0,0 +1,39 @@
package uk.gov.hmcts.cp.subscription.config;

import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import java.security.SecureRandom;
import java.security.cert.X509Certificate;

@Configuration
public class SSLTrustingRestTemplateConfig {

private static final X509TrustManager TRUST_ALL = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};

@PostConstruct
public void trustAllSsl() throws Exception {
var ssl = SSLContext.getInstance("TLS");
ssl.init(null, new TrustManager[]{TRUST_ALL}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;

import uk.gov.hmcts.cp.openapi.model.ClientSubscriptionRequest;
import uk.gov.hmcts.cp.openapi.model.NotificationEndpoint;
import uk.gov.hmcts.cp.subscription.integration.config.TestContainersInitialise;
Expand All @@ -23,6 +24,13 @@
import java.util.List;
import java.util.UUID;

import com.github.tomakehurst.wiremock.WireMockServer;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static uk.gov.hmcts.cp.openapi.model.EventType.CUSTODIAL_RESULT;
import static uk.gov.hmcts.cp.openapi.model.EventType.PRISON_COURT_REGISTER_GENERATED;

Expand All @@ -32,6 +40,15 @@
@Slf4j
public abstract class IntegrationTestBase {

protected static final String MATERIAL_METADATA_RESPONSE_PATH = "wiremock/material-client/__files/material-response.json";
protected static final String MATERIAL_CONTENT_RESPONSE_PATH = "wiremock/material-client/__files/material-with-contenturl.json";
protected static final String MATERIAL_PDF_PATH = "wiremock/material-client/__files/material-content.pdf";
protected static final String CALLBACK_RESPONSE_PATH = "wiremock/callback-client/__files/callback-accepted.json";
protected static final String MATERIAL_URI = "/material-query-api/query/api/rest/material/material/";
public static final String METADATA = "/metadata";
public static final String APPLICATION_JSON = "application/json";
public static final String CONTENT_TYPE = "Content-Type";

@Resource
protected MockMvc mockMvc;

Expand Down Expand Up @@ -91,4 +108,51 @@ protected DocumentMappingEntity insertDocument(UUID materialId, EntityEventType
protected String loadPayload(String path) throws IOException {
return new ClassPathResource(path).getContentAsString(StandardCharsets.UTF_8);
}

protected void stubMaterialMetadata(UUID materialId) throws IOException {
String materialPath = MATERIAL_URI + materialId;
String metadataBody = new ClassPathResource(MATERIAL_METADATA_RESPONSE_PATH).getContentAsString(StandardCharsets.UTF_8);
stubFor(get(urlPathMatching(".*" + materialPath + METADATA))
.willReturn(aResponse()
.withStatus(200)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)
.withBody(metadataBody)));
}

protected void stubMaterialContent(UUID materialId) throws IOException {
String materialPath = MATERIAL_URI + materialId;
String contentBody = new ClassPathResource(MATERIAL_CONTENT_RESPONSE_PATH).getContentAsString(StandardCharsets.UTF_8);
stubFor(get(urlPathMatching(".*" + materialPath + "/content"))
.willReturn(aResponse()
.withStatus(200)
.withHeader(CONTENT_TYPE, "application/vnd.material.query.material+json")
.withBody(contentBody)
.withTransformers("response-template")));
}

protected void stubMaterialBinary(UUID materialId) throws IOException {
String materialPath = MATERIAL_URI + materialId;
byte[] pdfBody = new ClassPathResource(MATERIAL_PDF_PATH).getContentAsByteArray();
stubFor(get(urlPathMatching(".*" + materialPath + "/binary"))
.willReturn(aResponse()
.withStatus(200)
.withHeader(CONTENT_TYPE, "application/pdf")
.withHeader("Content-Disposition", "inline; filename=\"material-content.pdf\"")
.withBody(pdfBody)));
}

protected void stubMaterialMetadataNoContent(UUID materialId) {
String materialPath = MATERIAL_URI + materialId;
stubFor(get(urlPathMatching(".*" + materialPath + METADATA))
.willReturn(aResponse().withStatus(204)));
}

protected void stubCallbackEndpoint(WireMockServer server, String callbackUri) throws IOException {
String body = new ClassPathResource(CALLBACK_RESPONSE_PATH).getContentAsString(StandardCharsets.UTF_8);
server.stubFor(post(urlPathEqualTo(callbackUri))
.willReturn(aResponse()
.withStatus(200)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)
.withBody(body)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;
Expand Down Expand Up @@ -31,11 +31,8 @@
import uk.gov.hmcts.cp.subscription.services.CallbackDeliveryService;


@EnableWireMock({@ConfigureWireMock(name = "material-client", baseUrlProperties = "material-client.url", port = 18081)})
@TestPropertySource(properties = {
"material-client.retry.timeoutMilliSecs=500",
"material-client.retry.intervalMilliSecs=100"
})
@EnableWireMock({@ConfigureWireMock(name = "material-client", baseUrlProperties = "material-client.url", port = 0)})
@ActiveProfiles("test")
class NotificationControllerIntegrationTest extends IntegrationTestBase {

private static final String NOTIFICATION_PCR_URI = "/notifications/pcr";
Expand All @@ -54,7 +51,7 @@ void setUp() {

@Test
void prison_court_register_generated_should_return_success() throws Exception {
String pcrPayload = loadPayload("stubs/requests/pcr-request-prison-court-register.json");
String pcrPayload = loadPayload("stubs/requests/progression-pcr/pcr-request-prison-court-register.json");

mockMvc.perform(post(NOTIFICATION_PCR_URI)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -68,7 +65,7 @@ void prison_court_register_generated_should_return_success() throws Exception {

@Test
void custodial_result_should_return_unsupported() throws Exception {
String pcrPayload = loadPayload("stubs/requests/pcr-request-custodial-result.json");
String pcrPayload = loadPayload("stubs/requests/progression-pcr/pcr-request-custodial-result.json");

doThrow(new UnsupportedOperationException("CUSTODIAL_RESULT not implemented"))
.when(callbackDeliveryService).processPcrEvent(any(PcrEventPayload.class), any(UUID.class));
Expand All @@ -86,7 +83,7 @@ void custodial_result_should_return_unsupported() throws Exception {

@Test
void material_metadata_not_found_should_return_404() throws Exception {
String pcrPayload = loadPayload("stubs/requests/pcr-request-material-not-found.json");
String pcrPayload = loadPayload("stubs/requests/progression-pcr/pcr-request-material-not-found.json");

mockMvc.perform(post(NOTIFICATION_PCR_URI)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -98,7 +95,8 @@ void material_metadata_not_found_should_return_404() throws Exception {

@Test
void material_metadata_timeout_should_return_504_via_global_exception_handler() throws Exception {
String pcrPayload = loadPayload("stubs/requests/pcr-request-material-timeout.json");
String pcrPayload = loadPayload("stubs/requests/progression-pcr/pcr-request-material-timeout.json");


mockMvc.perform(post(NOTIFICATION_PCR_URI)
.contentType(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
class NotificationControllerValidationTest extends IntegrationTestBase {

private static final String NOTIFICATION_PCR_URI = "/notifications/pcr";
private static final String PCR_REQUEST_VALID = "stubs/requests/pcr-request-valid.json";
private static final String PCR_REQUEST_MISSING_MATERIAL = "stubs/requests/pcr-request-missing-material.json";
private static final String PCR_REQUEST_MISSING_EVENT = "stubs/requests/pcr-request-missing-event.json";
private static final String PCR_REQUEST_VALID = "stubs/requests/progression-pcr/pcr-request-valid.json";
private static final String PCR_REQUEST_MISSING_MATERIAL = "stubs/requests/progression-pcr/pcr-request-missing-material.json";
private static final String PCR_REQUEST_MISSING_EVENT = "stubs/requests/progression-pcr/pcr-request-missing-event.json";
private static final UUID SUBSCRIPTION_ID = randomUUID();
private static final UUID DOCUMENT_ID = randomUUID();
private static final String SUBSCRIPTION_DOCUMENT_URI = "/client-subscriptions/{clientSubscriptionId}/documents/{documentId}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
class SubscriptionControllerValidationTest extends IntegrationTestBase {

private static final String CLIENT_SUBSCRIPTIONS = "/client-subscriptions";
private static final String SUBSCRIPTION_REQUEST_BAD_EVENT = "stubs/requests/subscription-request-bad-event-type.json";
private static final String SUBSCRIPTION_REQUEST_INVALID_CALLBACK = "stubs/requests/subscription-request-invalid-callback-url.json";
private static final String SUBSCRIPTION_REQUEST_BAD_EVENT = "stubs/requests/subscription/subscription-request-bad-event-type.json";
private static final String SUBSCRIPTION_REQUEST_INVALID_CALLBACK = "stubs/requests/subscription/subscription-request-invalid-callback-url.json";

@Test
void bad_event_type_should_return_400() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class SubscriptionSaveControllerIntegrationTest extends IntegrationTestBase {

private static final String SUBSCRIPTION_REQUEST_VALID = "stubs/requests/subscription-request-valid.json";
private static final String SUBSCRIPTION_REQUEST_VALID = "stubs/requests/subscription/subscription-request-valid.json";

@BeforeEach
void beforeEach() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Slf4j
class SubscriptionUpdateControllerIntegrationTest extends IntegrationTestBase {

private static final String SUBSCRIPTION_REQUEST_VALID = "stubs/requests/subscription-request-valid.json";
private static final String SUBSCRIPTION_REQUEST_VALID = "stubs/requests/subscription/subscription-request-valid.json";

@BeforeEach
void beforeEach() {
Expand Down
Loading
Loading