From cfb9678f5eb99de28883f33593446471e24ec8d2 Mon Sep 17 00:00:00 2001 From: Andrew Guibert Date: Thu, 16 Jan 2020 15:58:30 -0600 Subject: [PATCH] Integration with REST-assured --- build.gradle | 1 + .../jupiter/MicroShedTestExtension.java | 40 ++++- docs/features/14_RestAssured.md | 6 + sample-apps/everything-app/build.gradle | 1 + .../java/org/example/app/RestAssuredTest.java | 162 ++++++++++++++++++ 5 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 docs/features/14_RestAssured.md create mode 100644 sample-apps/everything-app/src/test/java/org/example/app/RestAssuredTest.java diff --git a/build.gradle b/build.gradle index a2752f40..dd6d823f 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ subprojects { repositories { mavenCentral() + mavenLocal() } javadoc { diff --git a/core/src/main/java/org/microshed/testing/jupiter/MicroShedTestExtension.java b/core/src/main/java/org/microshed/testing/jupiter/MicroShedTestExtension.java index 333a9351..e566d686 100644 --- a/core/src/main/java/org/microshed/testing/jupiter/MicroShedTestExtension.java +++ b/core/src/main/java/org/microshed/testing/jupiter/MicroShedTestExtension.java @@ -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; @@ -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); } @@ -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); } @@ -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 ObjectMapperType = (Class) 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> getMpRestClient() { try { diff --git a/docs/features/14_RestAssured.md b/docs/features/14_RestAssured.md new file mode 100644 index 00000000..6264fc0d --- /dev/null +++ b/docs/features/14_RestAssured.md @@ -0,0 +1,6 @@ +--- +layout: post +title: "RestAssured" +--- + +TODO: Write doc for integration with RESTAssured \ No newline at end of file diff --git a/sample-apps/everything-app/build.gradle b/sample-apps/everything-app/build.gradle index db6cacd1..93c91837 100644 --- a/sample-apps/everything-app/build.gradle +++ b/sample-apps/everything-app/build.gradle @@ -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' } diff --git a/sample-apps/everything-app/src/test/java/org/example/app/RestAssuredTest.java b/sample-apps/everything-app/src/test/java/org/example/app/RestAssuredTest.java new file mode 100644 index 00000000..27c0c234 --- /dev/null +++ b/sample-apps/everything-app/src/test/java/org/example/app/RestAssuredTest.java @@ -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); + } + +}