From b358169b9d43efe29f71c07aff1b95de764120d6 Mon Sep 17 00:00:00 2001 From: ChrisS1512 <87066931+ChrisS1512@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:28:50 +0000 Subject: [PATCH 1/2] PUB-2573 - Updated functional tests --- .../MediaApplicationCreationTest.java | 231 +++++++++++++++++- .../management/utils/FunctionalTestBase.java | 22 +- 2 files changed, 244 insertions(+), 9 deletions(-) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java index 64dd8ec3..3b5cad09 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java +++ b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java @@ -1,26 +1,35 @@ package uk.gov.hmcts.reform.pip.account.management.controllers; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpHeaders; import io.restassured.response.Response; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.io.ClassPathResource; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringExtension; +import uk.gov.hmcts.reform.pip.account.management.model.MediaApplication; +import uk.gov.hmcts.reform.pip.account.management.model.MediaApplicationStatus; import uk.gov.hmcts.reform.pip.account.management.utils.FunctionalTestBase; import uk.gov.hmcts.reform.pip.account.management.utils.OAuthClient; +import java.io.IOException; +import java.util.List; import java.util.Map; import java.util.concurrent.ThreadLocalRandom; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.springframework.http.HttpHeaders.AUTHORIZATION; +import static org.springframework.http.HttpStatus.NOT_FOUND; +import static org.springframework.http.HttpStatus.NO_CONTENT; import static org.springframework.http.HttpStatus.OK; @ExtendWith(SpringExtension.class) @ActiveProfiles(profiles = "functional") @SpringBootTest(classes = {OAuthClient.class}) +@SuppressWarnings("PMD.TooManyMethods") class MediaApplicationCreationTest extends FunctionalTestBase { private static final String TEST_NAME = "E2E Account Management Test Name"; @@ -33,20 +42,228 @@ class MediaApplicationCreationTest extends FunctionalTestBase { private static final String TESTING_SUPPORT_APPLICATION_URL = "/testing-support/application/"; private static final String MEDIA_APPLICATION_URL = "/application"; + private static final String GET_IMAGE_BY_ID = "/application/image/%s"; + private static final String GET_MEDIA_APPLICATION_URL = "/application/%s"; + private static final String APPROVE_APPLICATION = "/application/%s/APPROVED"; + private static final String REJECT_APPLICATION = "/application/%s/REJECTED"; + private static final String REJECT_APPLICATION_WITH_REASONS = "/application/%s/REJECTED/reasons"; + private static final String GET_APPLICATIONS_BY_STATUS = "/application/status/PENDING"; + private static final String REPORTING = "/application/reporting"; + private static final String GET_ALL_APPLICATIONS = "/application"; private static final String BEARER = "Bearer "; private static final String MOCK_FILE = "files/test-image.png"; + Map> reasons = + Map.of("Reason 1", List.of("Reason 1", "Reason 2"), "Reason 2", List.of("Reason 3", "Reason 4")); + + private Map bearer; + + @BeforeAll + public void startUp() { + bearer = Map.of(HttpHeaders.AUTHORIZATION, BEARER + accessToken); + } + @AfterAll public void teardown() { - doDeleteRequest(TESTING_SUPPORT_APPLICATION_URL + TEST_EMAIL, Map.of(AUTHORIZATION, BEARER + accessToken), ""); + doDeleteRequest(TESTING_SUPPORT_APPLICATION_URL + TEST_EMAIL, bearer); } - @Test - void shouldBeAbleToCreateAMediaApplication() throws Exception { - final Response response = - doPostMultipartForApplication(MEDIA_APPLICATION_URL, Map.of(AUTHORIZATION, BEARER + accessToken), - new ClassPathResource(MOCK_FILE).getFile(), TEST_NAME, TEST_EMAIL, TEST_EMPLOYER, STATUS); + private MediaApplication createApplication() throws IOException { + Response response = doPostMultipartForApplication(MEDIA_APPLICATION_URL, bearer, + new ClassPathResource(MOCK_FILE).getFile(), + TEST_NAME, TEST_EMAIL, TEST_EMPLOYER, STATUS); assertThat(response.getStatusCode()).isEqualTo(OK.value()); + + return response.getBody().as(MediaApplication.class); + } + + @Test + void shouldBeAbleToCreateAndGetAMediaApplication() throws Exception { + MediaApplication mediaApplication = createApplication(); + + final Response getResponse = doGetRequest(String.format(GET_MEDIA_APPLICATION_URL, mediaApplication.getId()), + bearer); + + assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); + MediaApplication retrievedMediaApplication = getResponse.getBody().as(MediaApplication.class); + + assertThat(retrievedMediaApplication.getId()).isEqualTo(mediaApplication.getId()); + assertThat(retrievedMediaApplication.getStatus()).isEqualTo(MediaApplicationStatus.PENDING); + } + + @Test + void shouldBeAbleToGetTheImageForAnApplication() throws Exception { + MediaApplication mediaApplication = createApplication(); + + final Response getResponse = doGetRequest(String.format(GET_IMAGE_BY_ID, mediaApplication.getImage()), + bearer); + + assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); + byte[] retrievedImage = getResponse.getBody().asByteArray(); + + assertThat(retrievedImage).isEqualTo(new ClassPathResource(MOCK_FILE).getContentAsByteArray()); + } + + @Test + void shouldBeAbleToGetApplicationsByStatus() throws Exception { + MediaApplication mediaApplication = createApplication(); + MediaApplication approvedMediaApplication = createApplication(); + + approvedMediaApplication = doPutRequest(String.format(APPROVE_APPLICATION, approvedMediaApplication.getId()), + bearer).getBody().as(MediaApplication.class); + + final Response getResponse = doGetRequest(GET_APPLICATIONS_BY_STATUS, + bearer); + + assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); + MediaApplication[] retrievedApplciations = getResponse.getBody().as(MediaApplication[].class); + + assertThat(retrievedApplciations).contains(mediaApplication); + assertThat(retrievedApplciations).doesNotContain(approvedMediaApplication); + } + + @Test + void shouldBeAbleToGetAllMediaApplications() throws Exception { + MediaApplication mediaApplication = createApplication(); + MediaApplication approvedMediaApplication = createApplication(); + + Response approvedResponse = doPutRequest(String.format(APPROVE_APPLICATION, approvedMediaApplication.getId()), + bearer); + + approvedMediaApplication = approvedResponse.getBody().as(MediaApplication.class); + + final Response getResponse = doGetRequest(GET_ALL_APPLICATIONS, + bearer); + + assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); + MediaApplication[] retrievedApplciations = getResponse.getBody().as(MediaApplication[].class); + + assertThat(retrievedApplciations).contains(mediaApplication); + assertThat(retrievedApplciations).contains(approvedMediaApplication); + } + + @Test + void statusShouldUpdateWhenApplicationIsUpdated() throws Exception { + MediaApplication mediaApplication = createApplication(); + + Response approvedMediaApplication = doPutRequest(String.format(APPROVE_APPLICATION, mediaApplication.getId()), + bearer); + + assertThat(approvedMediaApplication.getStatusCode()).isEqualTo(OK.value()); + assertThat(approvedMediaApplication.getBody().as(MediaApplication.class).getStatus()) + .isEqualTo(MediaApplicationStatus.APPROVED); + + MediaApplication getApprovedApplication = doGetRequest(String.format(GET_MEDIA_APPLICATION_URL, + mediaApplication.getId()), bearer) + .getBody().as(MediaApplication.class); + + assertThat(getApprovedApplication.getStatus()).isEqualTo(MediaApplicationStatus.APPROVED); + } + + @Test + void shouldNoLongerBeAbleToGetImageWhenApplicationIsApproved() throws Exception { + MediaApplication mediaApplication = createApplication(); + + doPutRequest(String.format(APPROVE_APPLICATION, mediaApplication.getId()), + bearer); + + final Response getResponse = doGetRequest(String.format(GET_IMAGE_BY_ID, mediaApplication.getImage()), + bearer); + + assertThat(getResponse.getStatusCode()).isEqualTo(NOT_FOUND.value()); + } + + @Test + void shouldNoLongerBeAbleToGetImageWhenApplicationIsRejected() throws Exception { + MediaApplication mediaApplication = createApplication(); + + doPutRequest(String.format(REJECT_APPLICATION, mediaApplication.getId()), + bearer); + + final Response getResponse = doGetRequest(String.format(GET_IMAGE_BY_ID, mediaApplication.getImage()), + bearer); + + assertThat(getResponse.getStatusCode()).isEqualTo(NOT_FOUND.value()); + } + + @Test + void shouldBeAbleToDeleteAnApplication() throws Exception { + MediaApplication mediaApplication = createApplication(); + + Response deleteResponse = doDeleteRequest(String.format(GET_MEDIA_APPLICATION_URL, mediaApplication.getId()), + bearer); + + assertThat(deleteResponse.getStatusCode()).isEqualTo(OK.value()); + + final Response getImageResponse = doGetRequest(String.format(GET_IMAGE_BY_ID, mediaApplication.getImage()), + bearer); + + assertThat(getImageResponse.getStatusCode()).isEqualTo(NOT_FOUND.value()); + + final Response getApplicationResponse = doGetRequest(String.format(GET_MEDIA_APPLICATION_URL, + mediaApplication.getId()), bearer); + + assertThat(getApplicationResponse.getStatusCode()).isEqualTo(NOT_FOUND.value()); + } + + @Test + void shouldBeAbleToUpdateAnApplicationWithRejectionReasons() throws Exception { + MediaApplication mediaApplication = createApplication(); + + Response rejectedApplicationResponse = + doPutRequestWithJsonBody(String.format(REJECT_APPLICATION_WITH_REASONS, mediaApplication.getId()), + bearer, new ObjectMapper().writeValueAsString(reasons)); + + assertThat(rejectedApplicationResponse.getStatusCode()).isEqualTo(OK.value()); + assertThat(rejectedApplicationResponse.getBody().as(MediaApplication.class).getStatus()) + .isEqualTo(MediaApplicationStatus.REJECTED); + + MediaApplication getRejectedApplication = doGetRequest(String.format(GET_MEDIA_APPLICATION_URL, + mediaApplication.getId()), bearer) + .getBody().as(MediaApplication.class); + + assertThat(getRejectedApplication.getStatus()).isEqualTo(MediaApplicationStatus.REJECTED); + } + + @Test + void shouldNoLongerBeAbleToGetImageWhenApplicationIsRejectedWithReasons() throws Exception { + MediaApplication mediaApplication = createApplication(); + + doPutRequestWithJsonBody(String.format(REJECT_APPLICATION_WITH_REASONS, mediaApplication.getId()), + bearer, + new ObjectMapper().writeValueAsString(reasons)); + + final Response getImageResponse = doGetRequest(String.format(GET_IMAGE_BY_ID, mediaApplication.getImage()), + bearer); + + assertThat(getImageResponse.getStatusCode()).isEqualTo(NOT_FOUND.value()); + } + + @Test + void ensureMediaApplicationsAreDeletedWhenReportingIsCalled() throws Exception { + MediaApplication rejectedApplication = createApplication(); + + rejectedApplication = doPutRequest(String.format(REJECT_APPLICATION, rejectedApplication.getId()), + bearer).getBody().as(MediaApplication.class); + + MediaApplication approvedApplication = createApplication(); + + approvedApplication = doPutRequest(String.format(APPROVE_APPLICATION, approvedApplication.getId()), + bearer).getBody().as(MediaApplication.class); + + MediaApplication pendingApplication = createApplication(); + + Response reportingResponse = doPostRequest(REPORTING, bearer, ""); + assertThat(reportingResponse.getStatusCode()).isEqualTo(NO_CONTENT.value()); + + final Response getResponse = doGetRequest(GET_ALL_APPLICATIONS, + bearer); + + MediaApplication[] retrievedApplications = getResponse.getBody().as(MediaApplication[].class); + + assertThat(retrievedApplications).contains(pendingApplication); + assertThat(retrievedApplications).doesNotContain(rejectedApplication); + assertThat(retrievedApplications).doesNotContain(approvedApplication); } } diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/FunctionalTestBase.java b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/FunctionalTestBase.java index ae512b35..4595cdfa 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/FunctionalTestBase.java +++ b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/utils/FunctionalTestBase.java @@ -73,13 +73,31 @@ protected Response doPostMultipartForApplication(final String path, final Map additionalHeaders, - final String body) { + protected Response doPutRequest(final String path, final Map additionalHeaders) { + return given() + .relaxedHTTPSValidation() + .headers(getRequestHeaders(additionalHeaders)) + .when() + .put(path) + .thenReturn(); + } + + protected Response doPutRequestWithJsonBody(final String path, final Map additionalHeaders, + String body) { return given() .relaxedHTTPSValidation() .headers(getRequestHeaders(additionalHeaders)) .body(body) .when() + .put(path) + .thenReturn(); + } + + protected Response doDeleteRequest(final String path, final Map additionalHeaders) { + return given() + .relaxedHTTPSValidation() + .headers(getRequestHeaders(additionalHeaders)) + .when() .delete(path) .thenReturn(); } From 9b446751be242422604db941187d9a72187909d4 Mon Sep 17 00:00:00 2001 From: ChrisS1512 <87066931+ChrisS1512@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:51:56 +0000 Subject: [PATCH 2/2] PUB-2573 - Updated functional tests --- .../MediaApplicationCreationTest.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java index 3b5cad09..9583034a 100644 --- a/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java +++ b/src/functionalTest/java/uk/gov/hmcts/reform/pip/account/management/controllers/MediaApplicationCreationTest.java @@ -110,8 +110,8 @@ void shouldBeAbleToGetApplicationsByStatus() throws Exception { MediaApplication mediaApplication = createApplication(); MediaApplication approvedMediaApplication = createApplication(); - approvedMediaApplication = doPutRequest(String.format(APPROVE_APPLICATION, approvedMediaApplication.getId()), - bearer).getBody().as(MediaApplication.class); + doPutRequest(String.format(APPROVE_APPLICATION, approvedMediaApplication.getId()), + bearer); final Response getResponse = doGetRequest(GET_APPLICATIONS_BY_STATUS, bearer); @@ -119,8 +119,8 @@ void shouldBeAbleToGetApplicationsByStatus() throws Exception { assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); MediaApplication[] retrievedApplciations = getResponse.getBody().as(MediaApplication[].class); - assertThat(retrievedApplciations).contains(mediaApplication); - assertThat(retrievedApplciations).doesNotContain(approvedMediaApplication); + assertThat(retrievedApplciations).anyMatch(app -> app.getId().equals(mediaApplication.getId())); + assertThat(retrievedApplciations).noneMatch(app -> app.getId().equals(approvedMediaApplication.getId())); } @Test @@ -128,19 +128,17 @@ void shouldBeAbleToGetAllMediaApplications() throws Exception { MediaApplication mediaApplication = createApplication(); MediaApplication approvedMediaApplication = createApplication(); - Response approvedResponse = doPutRequest(String.format(APPROVE_APPLICATION, approvedMediaApplication.getId()), + doPutRequest(String.format(APPROVE_APPLICATION, approvedMediaApplication.getId()), bearer); - approvedMediaApplication = approvedResponse.getBody().as(MediaApplication.class); - final Response getResponse = doGetRequest(GET_ALL_APPLICATIONS, bearer); assertThat(getResponse.getStatusCode()).isEqualTo(OK.value()); - MediaApplication[] retrievedApplciations = getResponse.getBody().as(MediaApplication[].class); + MediaApplication[] retrievedApplications = getResponse.getBody().as(MediaApplication[].class); - assertThat(retrievedApplciations).contains(mediaApplication); - assertThat(retrievedApplciations).contains(approvedMediaApplication); + assertThat(retrievedApplications).anyMatch(app -> app.getId().equals(mediaApplication.getId())); + assertThat(retrievedApplications).anyMatch(app -> app.getId().equals(approvedMediaApplication.getId())); } @Test @@ -244,13 +242,13 @@ void shouldNoLongerBeAbleToGetImageWhenApplicationIsRejectedWithReasons() throws void ensureMediaApplicationsAreDeletedWhenReportingIsCalled() throws Exception { MediaApplication rejectedApplication = createApplication(); - rejectedApplication = doPutRequest(String.format(REJECT_APPLICATION, rejectedApplication.getId()), - bearer).getBody().as(MediaApplication.class); + doPutRequest(String.format(REJECT_APPLICATION, rejectedApplication.getId()), + bearer); MediaApplication approvedApplication = createApplication(); - approvedApplication = doPutRequest(String.format(APPROVE_APPLICATION, approvedApplication.getId()), - bearer).getBody().as(MediaApplication.class); + doPutRequest(String.format(APPROVE_APPLICATION, approvedApplication.getId()), + bearer); MediaApplication pendingApplication = createApplication(); @@ -262,8 +260,8 @@ void ensureMediaApplicationsAreDeletedWhenReportingIsCalled() throws Exception { MediaApplication[] retrievedApplications = getResponse.getBody().as(MediaApplication[].class); - assertThat(retrievedApplications).contains(pendingApplication); - assertThat(retrievedApplications).doesNotContain(rejectedApplication); - assertThat(retrievedApplications).doesNotContain(approvedApplication); + assertThat(retrievedApplications).anyMatch(app -> app.getId().equals(pendingApplication.getId())); + assertThat(retrievedApplications).noneMatch(app -> app.getId().equals(rejectedApplication.getId())); + assertThat(retrievedApplications).noneMatch(app -> app.getId().equals(approvedApplication.getId())); } }