Skip to content
Open
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
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ dependencies {
implementation 'javax.validation:validation-api:2.0.1.Final'
compileOnly 'javax.servlet:servlet-api:2.5'
implementation 'org.openapitools:jackson-databind-nullable:0.2.6'
implementation 'org.zalando:problem-spring-web-starter:0.29.1'

implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.6.0'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '4.1.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ void postFail() {
"func-CASE1001"
],
"text": "System : Start Recording : Record: Case Code:0008, New Case"
}""")
}
""")
.when()
.baseUri(getUri(ENDPOINT_URL))
.redirects().follow(false)
Expand Down Expand Up @@ -133,9 +134,14 @@ void getFail() {
.extract().response();

assertEquals(400, response.statusCode());
assertThat(response.asPrettyString()).contains(
"Required request parameter 'start_date_time' for method parameter type OffsetDateTime is not present");

assertThat(response.asPrettyString()).contains("""
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Required parameter 'start_date_time' is not present.",
"instance": "/courtlogs"
}""");
}

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package uk.gov.hmcts.darts.audio.controller;

import ch.qos.logback.classic.Level;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand All @@ -17,6 +16,7 @@
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.unit.DataSize;
Expand Down Expand Up @@ -376,7 +376,7 @@ void addAudioBeyondAudioFileSizeThresholdExceeded() throws Exception {
Assertions.fail();
} catch (RestClientException restClientException) {
String expectedJson = """
400 : "{"title":"Bad Request","status":400,"detail":"Maximum upload size exceeded"}"
400 : "{"type":"about:blank","title":"Bad Request","status":400,"detail":"Maximum upload size exceeded","instance":"/audios"}"
""";

assertEquals(expectedJson.trim(), restClientException.getMessage());
Expand Down Expand Up @@ -404,18 +404,15 @@ void addAudioMetadataNonExistingCourthouse() throws Exception {
objectMapper.writeValueAsString(addAudioMetadataRequest).getBytes()
);

MvcResult mvcResult = mockMvc.perform(
mockMvc.perform(
multipart(ENDPOINT)
.file(audioFile)
.file(metadataJson))
.andExpect(status().isBadRequest())
.andReturn();

String actualJson = mvcResult.getResponse().getContentAsString();
String expectedJson = """
{"type":"COMMON_100","title":"Provided courthouse does not exist","status":400,"detail":"Courthouse 'TEST' not found."}""";

JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.NON_EXTENSIBLE);
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.is("COMMON_100")))
.andExpect(MockMvcResultMatchers.jsonPath("$.status", Matchers.is(400)))
.andExpect(MockMvcResultMatchers.jsonPath("$.detail", Matchers.is("Courthouse 'TEST' not found.")))
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.is("Provided courthouse does not exist")));
}

@Test
Expand Down Expand Up @@ -734,19 +731,14 @@ void addAudioReturnForbiddenError() throws Exception {
objectMapper.writeValueAsString(addAudioMetadataRequest).getBytes()
);

MvcResult mvcResult = mockMvc.perform(
mockMvc.perform(
multipart(ENDPOINT)
.file(audioFile)
.file(metadataJson))
.andExpect(status().isForbidden())
.andReturn();

String actualResponse = mvcResult.getResponse().getContentAsString();

String expectedResponse = """
{"type":"AUTHORISATION_109","title":"User is not authorised for this endpoint","status":403}
""";
JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.is("AUTHORISATION_109")))
.andExpect(MockMvcResultMatchers.jsonPath("$.status", Matchers.is(403)))
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.is("User is not authorised for this endpoint")));
}

private AddAudioMetadataRequest createAddAudioRequest(OffsetDateTime startedAt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package uk.gov.hmcts.darts.audio.controller;

import org.hamcrest.Matchers;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import uk.gov.hmcts.darts.common.entity.HearingEntity;
import uk.gov.hmcts.darts.common.entity.MediaEntity;
import uk.gov.hmcts.darts.common.entity.ObjectAdminActionEntity;
Expand Down Expand Up @@ -131,19 +129,11 @@ void shouldReturn404WhenMediaRecordDoesNotExist(SecurityRoleEnum role) throws Ex
given.anAuthenticatedUserWithGlobalAccessAndRole(role);

// When
MvcResult mvcResult = mockMvc.perform(get(ENDPOINT.resolve("123456789")))
mockMvc.perform(get(ENDPOINT.resolve("123456789")))
.andExpect(status().isNotFound())
.andReturn();

// Then
var jsonString = mvcResult.getResponse().getContentAsString();
JSONAssert.assertEquals("""
{
"type": "AUDIO_102",
"title": "The requested media cannot be found",
"status": 404
}
""", jsonString, JSONCompareMode.STRICT);
.andExpect(jsonPath("$.type", Matchers.is("AUDIO_102")))
.andExpect(jsonPath("$.status", Matchers.is(404)))
.andExpect(jsonPath("$.title", Matchers.is("The requested media cannot be found")));
}

@ParameterizedTest
Expand All @@ -153,19 +143,12 @@ void shouldDenyAccess(SecurityRoleEnum role) throws Exception {
given.anAuthenticatedUserWithGlobalAccessAndRole(role);

// When
MvcResult mvcResult = mockMvc.perform(get(ENDPOINT.resolve("123456789")))
mockMvc.perform(get(ENDPOINT.resolve("123456789")))
.andExpect(status().isForbidden())
.andReturn();
.andExpect(jsonPath("$.type", Matchers.is("AUTHORISATION_109")))
.andExpect(jsonPath("$.status", Matchers.is(403)))
.andExpect(jsonPath("$.title", Matchers.is("User is not authorised for this endpoint")));

// Then
var jsonString = mvcResult.getResponse().getContentAsString();
JSONAssert.assertEquals("""
{
"type": "AUTHORISATION_109",
"title": "User is not authorised for this endpoint",
"status": 403
}
""", jsonString, JSONCompareMode.STRICT);
}

private MediaEntity createAndSaveMediaEntity(HearingEntity hearingEntity, UserAccountEntity userAccountEntity) {
Expand Down Expand Up @@ -216,4 +199,4 @@ private ObjectAdminActionEntity createAndSaveAdminActionEntity(MediaEntity media
.saveAndFlush(objectAdminActionEntity);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.fasterxml.jackson.core.JsonProcessingException;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
Expand All @@ -10,6 +11,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import uk.gov.hmcts.darts.audio.enums.AudioRequestOutputFormat;
import uk.gov.hmcts.darts.audio.exception.AudioApiError;
import uk.gov.hmcts.darts.audio.model.GetAdminMediaResponseCase;
Expand Down Expand Up @@ -324,17 +326,12 @@ void wrongPermissions() throws Exception {
userAccountStub.givenUserIsAuthorisedJudge(userIdentity);

// when
MvcResult mvcResult = mockMvc.perform(get(ENDPOINT_URL)
mockMvc.perform(get(ENDPOINT_URL)
.queryParam("transformed_media_id", "1"))
.andExpect(status().isForbidden())
.andReturn();

// then
String actualJson = mvcResult.getResponse().getContentAsString();
String expectedJson = """
{"type":"AUTHORISATION_109","title":"User is not authorised for this endpoint","status":403}
""";
JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.NON_EXTENSIBLE);
.andExpect(MockMvcResultMatchers.jsonPath("$.type", Matchers.is("AUTHORISATION_109")))
.andExpect(MockMvcResultMatchers.jsonPath("$.status", Matchers.is(403)))
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.is("User is not authorised for this endpoint")));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package uk.gov.hmcts.darts.audio.controller;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import uk.gov.hmcts.darts.audio.entity.MediaRequestEntity;
import uk.gov.hmcts.darts.audiorequests.model.AudioRequestType;
Expand All @@ -21,8 +21,6 @@
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand Down Expand Up @@ -102,19 +100,11 @@ void updateTransformedMediaLastAccessedTimestampShouldReturnForbiddenErrorWhenRe

MockHttpServletRequestBuilder requestBuilder = patch(ENDPOINT_URL, transformedMediaId);

MvcResult mvcResult = mockMvc.perform(requestBuilder)
mockMvc.perform(requestBuilder)
.andExpect(status().isForbidden())
.andReturn();

String actualJson = mvcResult.getResponse().getContentAsString();
String expectedJson = """
{
"type":"AUDIO_REQUESTS_101",
"title":"The audio request is not valid for this user",
"status":403
}""";

assertEquals(expectedJson, actualJson, NON_EXTENSIBLE);
.andExpect(jsonPath("$.type", Matchers.is("AUDIO_REQUESTS_101")))
.andExpect(jsonPath("$.status", Matchers.is(403)))
.andExpect(jsonPath("$.title", Matchers.is("The audio request is not valid for this user")));

verify(mockAuthorisation).authoriseByTransformedMediaId(
transformedMediaEntity.getId(),
Expand All @@ -123,4 +113,4 @@ void updateTransformedMediaLastAccessedTimestampShouldReturnForbiddenErrorWhenRe
verify(mockAuthorisation).authoriseTransformedMediaAgainstUser(transformedMediaId);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.test.web.servlet.MvcResult;
import uk.gov.hmcts.darts.audio.model.PostAdminMediasSearchRequest;
import uk.gov.hmcts.darts.audio.model.PostAdminMediasSearchResponseItem;
import uk.gov.hmcts.darts.audio.model.Problem;
import uk.gov.hmcts.darts.authorisation.component.UserIdentity;
import uk.gov.hmcts.darts.common.entity.CourthouseEntity;
import uk.gov.hmcts.darts.common.entity.MediaEntity;
Expand All @@ -36,6 +37,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -386,16 +388,10 @@ void tooManyResults() throws Exception {
.andExpect(status().is4xxClientError())
.andReturn();

String actualResponse = TestUtils.removeTags(TAGS_TO_IGNORE, mvcResult.getResponse().getContentAsString());
String expectedResponse = """
{
"type": "AUDIO_116",
"title": "Too many results",
"status": 400
}""";

JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);

Problem problem = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), Problem.class);
assertEquals("AUDIO_116", problem.getType().toString());
assertEquals("Too many results", problem.getTitle());
assertEquals(400, problem.getStatus());
}

@Test
Expand All @@ -410,16 +406,10 @@ void noPermissions() throws Exception {
.andExpect(status().isForbidden())
.andReturn();

String actualResponse = TestUtils.removeTags(TAGS_TO_IGNORE, mvcResult.getResponse().getContentAsString());
String expectedResponse = """
{
"type": "AUTHORISATION_109",
"title": "User is not authorised for this endpoint",
"status": 403
}""";

JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);

Problem problem = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), Problem.class);
assertEquals("AUTHORISATION_109", problem.getType().toString());
assertEquals("User is not authorised for this endpoint", problem.getTitle());
assertEquals(403, problem.getStatus());
}

}
Loading