Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
@WithTags({@WithTag("testType:Functional")})
public abstract class BaseTest {

@Autowired
protected TestUtil testUtil;
protected final TestUtil testUtil;

@Value("${test.url}")
protected String testUrl;
Expand All @@ -35,6 +34,11 @@ public abstract class BaseTest {
protected RequestSpecification request;
protected RequestSpecification unAuthenticatedRequest;

@Autowired
protected BaseTest(TestUtil testUtil) {
this.testUtil = testUtil;
}

@BeforeEach
public void setupRequestSpecification() {
request = testUtil
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@

import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import uk.gov.hmcts.reform.em.annotation.testutil.TestUtil;

import java.util.UUID;

import static org.hamcrest.CoreMatchers.equalTo;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.API_ANNOTATION_SETS;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.API_FILTER;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.FIELD_DOCUMENT_ID;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.FIELD_ID;

class FilterAnnotationSetScenariosTest extends BaseTest {

@Autowired
public FilterAnnotationSetScenariosTest(TestUtil testUtil) {
super(testUtil);
}

@Test
void shouldReturn404WhenFilterAnnotationSetWithNonExistentDocumentId() {
final UUID documentId = UUID.randomUUID();

request
.param("documentId", documentId)
.get("/api/annotation-sets/filter")
.param(FIELD_DOCUMENT_ID, documentId)
.get(API_FILTER)
.then()
.assertThat()
.statusCode(204)
Expand All @@ -29,13 +40,13 @@ void shouldReturn200WhenFilterAnnotationSetWithDocumentId() {
createAnnotationSet(annotationSetId, documentId);

request
.param("documentId", documentId)
.get("/api/annotation-sets/filter")
.param(FIELD_DOCUMENT_ID, documentId)
.get(API_FILTER)
.then()
.assertThat()
.statusCode(200)
.body("id", equalTo(annotationSetId.toString()))
.body("documentId", equalTo(documentId.toString()))
.body(FIELD_ID, equalTo(annotationSetId.toString()))
.body(FIELD_DOCUMENT_ID, equalTo(documentId.toString()))
.log().all();
}

Expand All @@ -46,8 +57,8 @@ void shouldReturn401WhenUnAuthenticatedUserFilterAnnotationSetWithDocumentId() {
createAnnotationSet(annotationSetId, documentId);

unAuthenticatedRequest
.param("documentId", documentId)
.get("/api/annotation-sets/filter")
.param(FIELD_DOCUMENT_ID, documentId)
.get(API_FILTER)
.then()
.assertThat()
.statusCode(401)
Expand All @@ -58,21 +69,20 @@ private void createAnnotationSet(final UUID annotationSetId, final UUID document
final JSONObject annotationSet = createAnnotationSetPayload(annotationSetId, documentId);
request
.body(annotationSet.toString())
.post("/api/annotation-sets")
.post(API_ANNOTATION_SETS)
.then()
.assertThat()
.statusCode(201)
.body("id", equalTo(annotationSetId.toString()))
.body("documentId", equalTo(documentId.toString()))
.header("Location", equalTo("/api/annotation-sets/" + annotationSetId))
.body(FIELD_ID, equalTo(annotationSetId.toString()))
.body(FIELD_DOCUMENT_ID, equalTo(documentId.toString()))
.header("Location", equalTo(API_ANNOTATION_SETS + "/" + annotationSetId))
.log().all();
}

private JSONObject createAnnotationSetPayload(final UUID annotationSetId, final UUID documentId) {
final JSONObject annotationSet = new JSONObject();
annotationSet.put("documentId", documentId);
annotationSet.put("id", annotationSetId.toString());

annotationSet.put(FIELD_DOCUMENT_ID, documentId);
annotationSet.put(FIELD_ID, annotationSetId.toString());
return annotationSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,144 +7,155 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import uk.gov.hmcts.reform.em.annotation.testutil.TestUtil;
import uk.gov.hmcts.reform.em.annotation.testutil.ToggleProperties;

import java.util.UUID;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.API_METADATA;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.DEFAULT_ROTATION_ANGLE;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.FIELD;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.FIELD_DOCUMENT_ID;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.FIELD_ERRORS;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.FIELD_ROTATION_ANGLE;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.HEADER_LOCATION;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.MESSAGE;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.STATUS_BAD_REQUEST;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.STATUS_CREATED;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.STATUS_NO_CONTENT;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.STATUS_OK;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.STATUS_UNAUTHORIZED;
import static uk.gov.hmcts.reform.em.annotation.functional.TestConsts.VALIDATION_NOT_NULL;

@EnableConfigurationProperties(ToggleProperties.class)
class MetadataScenariosTest extends BaseTest {

private final ToggleProperties toggleProperties;

@Autowired
ToggleProperties toggleProperties;
public MetadataScenariosTest(TestUtil testUtil, ToggleProperties toggleProperties) {
super(testUtil);
this.toggleProperties = toggleProperties;
}

@Test
void shouldReturn201WhenCreateNewMetadata() {
// If the Endpoint Toggles are enabled, continue, if not skip and ignore
assumeTrue(toggleProperties.isEnableMetadataEndpoint());

final String documentId = UUID.randomUUID().toString();
final ValidatableResponse response = createMetadata(documentId);

response
.statusCode(201)
.body("rotationAngle", equalTo(90))
.body("documentId", equalTo(documentId))
.header("Location", equalTo("/api/metadata/" + documentId))
.statusCode(STATUS_CREATED)
.body(FIELD_ROTATION_ANGLE, equalTo(DEFAULT_ROTATION_ANGLE))
.body(FIELD_DOCUMENT_ID, equalTo(documentId))
.header(HEADER_LOCATION, equalTo(API_METADATA + documentId))
.log().all();
}

@Test
void shouldReturn400WhenCreateNewMetadataWithoutDocumentId() {
// If the Endpoint Toggles are enabled, continue, if not skip and ignore
assumeTrue(toggleProperties.isEnableMetadataEndpoint());

final String documentId = UUID.randomUUID().toString();
final JSONObject metadataPayload = createMetadataPayload(documentId);

metadataPayload.remove("documentId");
metadataPayload.remove(FIELD_DOCUMENT_ID);

request
.body(metadataPayload.toString())
.post("/api/metadata/")
.post(API_METADATA)
.then()
.statusCode(400)
.body("fieldErrors", Matchers.hasSize(1))
.body("fieldErrors[0].field", equalTo("documentId"))
.body("fieldErrors[0].message", equalTo("NotNull"))
.statusCode(STATUS_BAD_REQUEST)
.body(FIELD_ERRORS, Matchers.hasSize(1))
.body(FIELD_ERRORS + "[0]." + FIELD, equalTo(FIELD_DOCUMENT_ID))
.body(FIELD_ERRORS + "[0]." + MESSAGE, equalTo(VALIDATION_NOT_NULL))
.log().all();
}

@Test
void shouldReturn400WhenCreateNewMetadataWithoutRotationAngle() {
// If the Endpoint Toggles are enabled, continue, if not skip and ignore
assumeTrue(toggleProperties.isEnableMetadataEndpoint());

final String documentId = UUID.randomUUID().toString();
final JSONObject metadataPayload = createMetadataPayload(documentId);

metadataPayload.remove("rotationAngle");
metadataPayload.remove(FIELD_ROTATION_ANGLE);

request
.body(metadataPayload.toString())
.post("/api/metadata/")
.post(API_METADATA)
.then()
.statusCode(400)
.body("fieldErrors", Matchers.hasSize(1))
.body("fieldErrors[0].field", equalTo("rotationAngle"))
.body("fieldErrors[0].message", equalTo("NotNull"))
.statusCode(STATUS_BAD_REQUEST)
.body(FIELD_ERRORS, Matchers.hasSize(1))
.body(FIELD_ERRORS + "[0]." + FIELD, equalTo(FIELD_ROTATION_ANGLE))
.body(FIELD_ERRORS + "[0]." + MESSAGE, equalTo(VALIDATION_NOT_NULL))
.log().all();
}

@Test
void shouldReturn401WhenUnAuthenticatedUserCreateNewMetadata() {
// If the Endpoint Toggles are enabled, continue, if not skip and ignore
assumeTrue(toggleProperties.isEnableMetadataEndpoint());

final String documentId = UUID.randomUUID().toString();
final JSONObject metadataPayload = createMetadataPayload(documentId);

unAuthenticatedRequest
.body(metadataPayload)
.post("/api/metadata/")
.post(API_METADATA)
.then()
.statusCode(401)
.statusCode(STATUS_UNAUTHORIZED)
.log().all();
}

@Test
void shouldReturn200WhenGetMetadataByDocumentId() {
// If the Endpoint Toggles are enabled, continue, if not skip and ignore
assumeTrue(toggleProperties.isEnableMetadataEndpoint());

final String documentId = UUID.randomUUID().toString();
createMetadata(documentId);

request
.get("/api/metadata/" + documentId)
.get(API_METADATA + documentId)
.then()
.statusCode(200)
.body("rotationAngle", equalTo(90))
.body("documentId", equalTo(documentId))
.statusCode(STATUS_OK)
.body(FIELD_ROTATION_ANGLE, equalTo(DEFAULT_ROTATION_ANGLE))
.body(FIELD_DOCUMENT_ID, equalTo(documentId))
.log().all();
}

@Test
void shouldReturn204WhenGetMetadataByNonExistentDocumentId() {
// If the Endpoint Toggles are enabled, continue, if not skip and ignore
assumeTrue(toggleProperties.isEnableMetadataEndpoint());

final String documentId = UUID.randomUUID().toString();

request
.get("/api/metadata/" + documentId)
.get(API_METADATA + documentId)
.then()
.statusCode(204)
.statusCode(STATUS_NO_CONTENT)
.log().all();
}

@Test
void shouldReturn401WhenUnAuthenticatedUserGetMetadataByDocumentId() {
// If the Endpoint Toggles are enabled, continue, if not skip and ignore
assumeTrue(toggleProperties.isEnableMetadataEndpoint());

final String documentId = UUID.randomUUID().toString();
createMetadata(documentId);

unAuthenticatedRequest
.get("/api/metadata/" + documentId)
.get(API_METADATA + documentId)
.then()
.statusCode(401)
.statusCode(STATUS_UNAUTHORIZED)
.log().all();
}

public JSONObject createMetadataPayload(final String documentId) {
@NotNull
private JSONObject createMetadataPayload(final String documentId) {
final JSONObject jsonObject = new JSONObject();
jsonObject.put("rotationAngle", 90);
jsonObject.put("documentId", documentId);

jsonObject.put(FIELD_ROTATION_ANGLE, DEFAULT_ROTATION_ANGLE);
jsonObject.put(FIELD_DOCUMENT_ID, documentId);
return jsonObject;
}

Expand All @@ -154,9 +165,9 @@ private ValidatableResponse createMetadata(final String documentId) {

return request
.body(metadata.toString())
.post("/api/metadata/")
.post(API_METADATA)
.then()
.statusCode(201)
.statusCode(STATUS_CREATED)
.log().all();
}
}
Loading