Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
aduursma committed Dec 26, 2017
1 parent e0a04fd commit 537e2d7
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 1 deletion.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
# commons-repository-test
# Commons Repository Test

![badge-version](https://img.shields.io/badge/Version-v1.0.0-green.svg)

## Introduction
This Maven module contains base classes for writing Spring Data REST integration testcases.

## Usage
Include the following in the _dependencies_ section of the Maven POM of the Maven module you want
to use the classes in:

<dependency>
<groupId>nl.agility.commons</groupId>
<artifactId>commons-repository-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>

47 changes: 47 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>nl.agility.maven</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<groupId>nl.agility.commons</groupId>
<artifactId>commons-repository-test</artifactId>
<version>1.0.0-${revision}</version>

<name>Agility IT Services - Commons Repository TEST</name>
<description>Commons Repository TEST contains base classes for writing Spring Data REST integration testcases</description>

<scm>
<url>https://github.com/aduursma/commons-repository-test</url>
<connection>scm:git:git://github.com/aduursma/commons-repository-test.git</connection>
<developerConnection>scm:git:[email protected]:aduursma/commons-repository-test.git</developerConnection>
</scm>

<properties>
<revision>SNAPSHOT</revision>
<rest-assured.version>3.0.6</rest-assured.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
16 changes: 16 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<document xmlns="http://maven.apache.org/changes/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/xsd/changes-1.0.0.xsd">

<properties>
<title>Release notes</title>
</properties>

<body>
<release version="1.0.0" description="Initial release">
<action type="add" dev="aduursma" date="26-12-2017">
Added initial project setup.
</action>
</release>
</body>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package nl.agility.commons.repository.test;

import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;
import static org.apache.http.HttpStatus.SC_METHOD_NOT_ALLOWED;
import static org.apache.http.HttpStatus.SC_NOT_FOUND;
import static org.apache.http.HttpStatus.SC_OK;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;

public abstract class BaseReadOnlyRepositoryIntegrationTest extends BaseRestIntegrationTest {

public static final String HAL_JSON = "application/hal+json;charset=UTF-8";

@Value("${spring.data.rest.default-page-size}")
protected Integer DEFAULT_PAGE_SIZE;

protected abstract String resourceUri();

protected abstract String existingResourceUri();

protected abstract String nonExistingResourceUri();

protected boolean isCollectionResource() {
return true;
}

@Test
public void verifyGetNonExistingUriIsHandled() {
String nonExistingUri = "/non-existing";

given()
.when()
.get(nonExistingUri)
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_NOT_FOUND)
.body("timestamp", notNullValue())
.body("status", equalTo(404))
.body("error", equalTo("Not Found"))
.body("exception", equalTo("org.springframework.web.servlet.NoHandlerFoundException"))
.body("message", equalTo("No handler found for GET " + nonExistingUri))
.body("path", equalTo(nonExistingUri))
;
}

@Test
public void verifyGetCollectionResourceIsOk() {
if (isCollectionResource()) {
given()
.when()
.get(resourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_OK)
.contentType(HAL_JSON)
;
}
}

@Test
public void verifyGetCollectionResourceIsLessThanOrEqualToConfiguredMaximum() {
if (isCollectionResource()) {
given()
.when()
.get(resourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_OK)
.contentType(HAL_JSON)
.body("page.size", lessThanOrEqualTo(DEFAULT_PAGE_SIZE))
;
}
}

@Test
public void verifyPostCollectionResourceIsNotAllowed() {
if (isCollectionResource()) {
given()
.contentType(JSON)
.body("{}")
.when()
.post(resourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_METHOD_NOT_ALLOWED)
.contentType(HAL_JSON)
.body("timestamp", notNullValue())
.body("status", equalTo(405))
.body("error", equalTo("Method Not Allowed"))
.body("exception", equalTo("org.springframework.web.HttpRequestMethodNotSupportedException"))
.body("message", equalTo("Request method 'POST' not supported"))
.body("path", equalTo(resourceUri()))
;
}
}

@Test
public void verifyGetExistingItemResourceIsOk() {
given()
.when()
.get(existingResourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_OK)
.contentType(HAL_JSON)
.body(notNullValue())
;
}

@Test
public void verifyGetNonExistingItemResourceIsNotFound() {
given()
.when()
.get(nonExistingResourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_NOT_FOUND)
.body(isEmptyString())
;
}

@Test
public void verifyPutItemResourceIsNotAllowed() {
given()
.contentType(JSON)
.body("{}")
.when()
.put(existingResourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_METHOD_NOT_ALLOWED)
.contentType(HAL_JSON)
.body("timestamp", notNullValue())
.body("status", equalTo(405))
.body("error", equalTo("Method Not Allowed"))
.body("exception", equalTo("org.springframework.web.HttpRequestMethodNotSupportedException"))
.body("message", equalTo("Request method 'PUT' not supported"))
.body("path", equalTo(existingResourceUri()))
;
}

@Test
public void verifyPatchItemResourceIsNotAllowed() {
given()
.contentType(JSON)
.body("{}")
.when()
.patch(existingResourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_METHOD_NOT_ALLOWED)
.contentType(HAL_JSON)
.body("timestamp", notNullValue())
.body("status", equalTo(405))
.body("error", equalTo("Method Not Allowed"))
.body("exception", equalTo("org.springframework.web.HttpRequestMethodNotSupportedException"))
.body("message", equalTo("Request method 'PATCH' not supported"))
.body("path", equalTo(existingResourceUri()))
;
}

@Test
public void verifyDeleteItemResourceIsNotAllowed() {
given()
.when()
.delete(existingResourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_METHOD_NOT_ALLOWED)
.contentType(HAL_JSON)
.body("timestamp", notNullValue())
.body("status", equalTo(405))
.body("error", equalTo("Method Not Allowed"))
.body("exception", equalTo("org.springframework.web.HttpRequestMethodNotSupportedException"))
.body("message", equalTo("Request method 'DELETE' not supported"))
.body("path", equalTo(existingResourceUri()))
;
}

@Test
public void verifyGetAssociationResourceIsOk() {
if (!isCollectionResource()) {
given()
.when()
.get(resourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_OK)
.contentType(HAL_JSON)
;
}
}

@Test
public void verifyPutAssociationResourceIsNotAllowed() {
if (!isCollectionResource()) {
given()
.contentType(JSON)
.body("{}")
.when()
.put(existingResourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_METHOD_NOT_ALLOWED)
.contentType(HAL_JSON)
.body("timestamp", notNullValue())
.body("status", equalTo(405))
.body("error", equalTo("Method Not Allowed"))
.body("exception", equalTo("org.springframework.web.HttpRequestMethodNotSupportedException"))
.body("message", equalTo("Request method 'PUT' not supported"))
.body("path", equalTo(existingResourceUri()))
;
}
}

@Test
public void verifyDeleteAssociationResourceIsNotAllowed() {
if (!isCollectionResource()) {
given()
.when()
.delete(resourceUri())
.then()
.headers(RESPONSE_HEADERS)
.statusCode(SC_METHOD_NOT_ALLOWED)
.contentType(JSON)
.body("timestamp", notNullValue())
.body("status", equalTo(405))
.body("error", equalTo("Method Not Allowed"))
.body("exception", equalTo("org.springframework.web.HttpRequestMethodNotSupportedException"))
.body("message", equalTo("Request method 'DELETE' not supported"))
.body("path", equalTo(resourceUri()))
;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package nl.agility.commons.repository.test;

import static org.hamcrest.Matchers.equalTo;

import java.util.HashMap;
import java.util.Map;

import org.hamcrest.Matcher;

public abstract class BaseRestIntegrationTest extends BaseTest {

public static final String CACHE_CONTROL = "Cache-Control";
public static final String EXPIRES = "Expires";
public static final String PRAGMA = "Pragma";
public static final String SERVER = "Server";
public static final String CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";
public static final String FRAME_OPTIONS = "X-Frame-Options";
public static final String XSS_PROTECTION = "X-XSS-Protection";

// public static final String CONTENT_TYPE = "Content-Type";

@SuppressWarnings("serial")
public static final Map<String, Matcher<?>> SECURITY_HEADERS = new HashMap<String, Matcher<?>>() {{
put(CACHE_CONTROL, equalTo("no-cache, no-store, max-age=0, must-revalidate"));
put(EXPIRES, equalTo("0"));
put(PRAGMA, equalTo("no-cache"));
put(SERVER, equalTo("Unknown"));
put(CONTENT_TYPE_OPTIONS, equalTo("nosniff"));
put(FRAME_OPTIONS, equalTo("SAMEORIGIN"));
put(XSS_PROTECTION, equalTo("1; mode=block"));

}};

@SuppressWarnings("serial")
public static final Map<String, Matcher<?>> RESPONSE_HEADERS = new HashMap<String, Matcher<?>>() {{
putAll(SECURITY_HEADERS);
// put(CONTENT_TYPE, equalTo("application/hal+json;charset=UTF-8"));
}};

}
27 changes: 27 additions & 0 deletions src/main/java/nl/agility/commons/repository/test/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nl.agility.commons.repository.test;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import io.restassured.RestAssured;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("integration-test")
public abstract class BaseTest {

@LocalServerPort
protected int serverPort;

@Before
public void setUpRestAssured() {
RestAssured.port = serverPort;
}

}

0 comments on commit 537e2d7

Please sign in to comment.