|
| 1 | +package com.restfriends.sample.restassured.test; |
| 2 | + |
| 3 | +import com.github.bordertech.lde.api.LdeLauncher; |
| 4 | +import com.sample.app.model.impl.MockDataUtil; |
| 5 | +import com.sample.app.test.Unit; |
| 6 | +import io.restassured.RestAssured; |
| 7 | +import io.restassured.response.ValidatableResponse; |
| 8 | +import org.hamcrest.Matchers; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.AfterClass; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.experimental.categories.Category; |
| 15 | + |
| 16 | +/** |
| 17 | + * Sample API Unit tests. |
| 18 | + */ |
| 19 | +@Category(Unit.class) |
| 20 | +public class SampleApiTest { |
| 21 | + |
| 22 | + @BeforeClass |
| 23 | + public static void startTomcat() { |
| 24 | + LdeLauncher.launchServer(false); |
| 25 | + } |
| 26 | + |
| 27 | + @AfterClass |
| 28 | + public static void closeTomcat() { |
| 29 | + LdeLauncher.stopServer(); |
| 30 | + } |
| 31 | + |
| 32 | + @BeforeClass |
| 33 | + public static void setupRest() { |
| 34 | + // Port |
| 35 | + String port = System.getProperty("server.port"); |
| 36 | + RestAssured.port = port == null ? 8082 : Integer.valueOf(port); |
| 37 | + |
| 38 | + // Base |
| 39 | + String basePath = System.getProperty("server.base"); |
| 40 | + RestAssured.basePath = basePath == null ? "/lde/api/v1/" : basePath; |
| 41 | + |
| 42 | + // Host |
| 43 | + String baseHost = System.getProperty("server.host"); |
| 44 | + RestAssured.baseURI = baseHost == null ? "http://localhost" : baseHost; |
| 45 | + |
| 46 | + // Logging |
| 47 | + RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); |
| 48 | + } |
| 49 | + |
| 50 | + @Before |
| 51 | + @After |
| 52 | + public void cleanData() { |
| 53 | + MockDataUtil.resetData(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void basicPingTest() { |
| 58 | + ValidatableResponse resp = RestAssured.given().when().get("/clients").then(); |
| 59 | + resp.statusCode(200).body("data.size()", Matchers.is(9)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void basicSearchSystemError() { |
| 64 | + ValidatableResponse resp = RestAssured.given().queryParam("search", "error").when().get("/clients").then(); |
| 65 | + resp.statusCode(400).body("error.status", Matchers.is(400)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void basicRetrieveClient() { |
| 70 | + ValidatableResponse resp = RestAssured.given().pathParam("id", "ORG1").when().get("/clients/{id}").then(); |
| 71 | + resp.statusCode(200).body("data.clientId", Matchers.is("ORG1")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void basicRetrieveClientError() { |
| 76 | + ValidatableResponse resp = RestAssured.given().pathParam("id", "error").when().get("/clients/{id}").then(); |
| 77 | + resp.statusCode(400).body("error.status", Matchers.is(400)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void basicRetrieveClientNotFound() { |
| 82 | + ValidatableResponse resp = RestAssured.given().pathParam("id", "notfound").when().get("/clients/{id}").then(); |
| 83 | + resp.statusCode(400).body("error.status", Matchers.is(400)); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments