Skip to content

Commit

Permalink
Integration with REST-assured
Browse files Browse the repository at this point in the history
  • Loading branch information
aguibert committed Jan 16, 2020
1 parent 4e192d9 commit cfb9678
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ subprojects {

repositories {
mavenCentral()
mavenLocal()
}

javadoc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -44,15 +46,16 @@
*/
class MicroShedTestExtension implements BeforeAllCallback {

static final Logger LOGGER = LoggerFactory.getLogger(MicroShedTestExtension.class);
static final Logger LOG = LoggerFactory.getLogger(MicroShedTestExtension.class);

@Override
public void beforeAll(ExtensionContext context) throws Exception {
Class<?> testClass = context.getRequiredTestClass();
ApplicationEnvironment config = ApplicationEnvironment.load();
LOGGER.info("Using ApplicationEnvironment class: " + config.getClass().getCanonicalName());
LOG.info("Using ApplicationEnvironment class: " + config.getClass().getCanonicalName());
config.applyConfiguration(testClass);
config.start();
configureRestAssured(config);
injectRestClients(testClass);
}

Expand All @@ -79,7 +82,7 @@ private static void injectRestClients(Class<?> clazz) {
Object restClient = rcBuilder.build(restClientField.getType());
try {
restClientField.set(null, restClient);
LOGGER.debug("Injected rest client for " + restClientField);
LOG.debug("Injected rest client for " + restClientField);
} catch (Exception e) {
throw new ExtensionConfigurationException("Unable to set field " + restClientField, e);
}
Expand All @@ -99,6 +102,37 @@ private static String createJwtIfNeeded(Field restClientField) {
return null;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void configureRestAssured(ApplicationEnvironment config) {
Class<?> RestAssured = null;
try {
RestAssured = Class.forName("io.restassured.RestAssured", false, MicroShedTestExtension.class.getClassLoader());
} catch (ClassNotFoundException | LinkageError e) {
return;
}

try {
URL appURL = new URL(config.getApplicationURL());
String baseURI = appURL.getProtocol() + "://" + appURL.getHost();
int port = appURL.getPort();
String basePath = appURL.getPath();
LOG.info("Configuring RestAssured with baseURI=" + baseURI + " port=" + port + " basePath=" + basePath);

RestAssured.getField("baseURI").set(null, baseURI);
RestAssured.getField("basePath").set(null, basePath);
RestAssured.getField("port").set(null, port);

// Configure JSONB as the JSON object mapper by invoking
// RestAssured.objectMapper(ObjectMapperType.JSONB);
Class<Enum> ObjectMapperType = (Class<Enum>) Class.forName("io.restassured.mapper.ObjectMapperType", false, MicroShedTestExtension.class.getClassLoader());
Object JSONB = Enum.valueOf(ObjectMapperType, "JSONB");
Method objectMapper = RestAssured.getMethod("objectMapper", ObjectMapperType);
objectMapper.invoke(null, JSONB);
} catch (Exception e) {
LOG.warn("Unable to configure RestAssured because of: " + e.getMessage(), e);
}
}

@SuppressWarnings("unchecked")
private static Optional<Class<? extends Annotation>> getMpRestClient() {
try {
Expand Down
6 changes: 6 additions & 0 deletions docs/features/14_RestAssured.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
layout: post
title: "RestAssured"
---

TODO: Write doc for integration with RESTAssured
1 change: 1 addition & 0 deletions sample-apps/everything-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
testCompile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.29'
testCompile 'org.testcontainers:mockserver:1.12.3'
testCompile 'org.mock-server:mockserver-client-java:5.5.4'
testCompile 'io.rest-assured:rest-assured:4.1.3-SNAPSHOT' // 4.1.2'
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
*
*/
package org.example.app;

import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
import org.microshed.testing.SharedContainerConfig;
import org.microshed.testing.jupiter.MicroShedTest;

@MicroShedTest
@SharedContainerConfig(AppContainerConfig.class)
public class RestAssuredTest {

@Test
public void testCreatePerson() {
given()
.queryParam("name", "Hank")
.queryParam("age", 45)
.contentType(JSON)
.when()
.post("/")
.then()
.statusCode(200)
.contentType(JSON);
}

@Test
public void testMinSizeName() {
long minSizeNameId = given()
.queryParam("name", "Bob")
.queryParam("age", 42)
.contentType(JSON)
.when()
.post("/")
.then()
.statusCode(200)
.contentType(JSON)
.extract()
.as(long.class);

Person p = given()
.pathParam("personId", minSizeNameId)
.when()
.get("/{personId}")
.then()
.statusCode(200)
.contentType(JSON)
.extract()
.as(Person.class);
assertEquals("Bob", p.name);
assertEquals(42, p.age);
assertEquals(minSizeNameId, p.id);
}

@Test
public void testMinAge() {
long minAgeId = given()
.queryParam("name", "Newborn")
.queryParam("age", 0)
.contentType(JSON)
.when()
.post("/")
.then()
.statusCode(200)
.contentType(JSON)
.extract()
.as(long.class);

Person p = given()
.pathParam("personId", minAgeId)
.when()
.get("/{personId}")
.then()
.statusCode(200)
.contentType(JSON)
.extract()
.as(Person.class);
assertEquals("Newborn", p.name);
assertEquals(0, p.age);
assertEquals(minAgeId, p.id);
}

@Test
public void testGetPerson() {
long bobId = given()
.queryParam("name", "Bob")
.queryParam("age", 24)
.contentType(JSON)
.when()
.post("/")
.then()
.statusCode(200)
.contentType(JSON)
.extract()
.as(long.class);

Person bob = given()
.pathParam("personId", bobId)
.when()
.get("/{personId}")
.then()
.statusCode(200)
.contentType(JSON)
.extract()
.as(Person.class);
assertEquals("Bob", bob.name);
assertEquals(24, bob.age);
assertNotNull(bob.id);
}

@Test
public void testGetUnknownPerson() {
given()
.pathParam("personId", -1L)
.when()
.get("/{personId}")
.then()
.statusCode(404);
}

@Test
public void testCreateBadPersonNullName() {
given()
.queryParam("name", (Object[]) null)
.queryParam("age", 5)
.contentType(JSON)
.when()
.post("/")
.then()
.statusCode(400);
}

@Test
public void testCreateBadPersonNegativeAge() {
given()
.queryParam("name", "NegativeAgePersoN")
.queryParam("age", -1)
.contentType(JSON)
.when()
.post("/")
.then()
.statusCode(400);
}

@Test
public void testCreateBadPersonNameTooLong() {
given()
.queryParam("name", "NameTooLongPersonNameTooLongPersonNameTooLongPerson")
.queryParam("age", 5)
.contentType(JSON)
.when()
.post("/")
.then()
.statusCode(400);
}

}

0 comments on commit cfb9678

Please sign in to comment.