Skip to content

Commit b549a04

Browse files
Sample REST API Tests
1 parent 14dc714 commit b549a04

File tree

15 files changed

+278
-34
lines changed

15 files changed

+278
-34
lines changed

Diff for: model-api/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
<artifactId>openpojo</artifactId>
2929
<scope>test</scope>
3030
</dependency>
31+
<!-- Model BDD -->
32+
<dependency>
33+
<groupId>com.github.bordertech.sample.app</groupId>
34+
<artifactId>model-bdd</artifactId>
35+
<version>${project.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
3139
</dependencies>
3240

3341
</project>

Diff for: model-api/src/test/java/com/sample/app/model/client/AddressDetail_Test.java renamed to model-api/src/test/java/com/sample/app/model/client/AddressDetailTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.sample.app.model.client;
22

3+
import com.sample.app.test.Unit;
34
import org.junit.Assert;
45
import org.junit.Test;
6+
import org.junit.experimental.categories.Category;
57

68
/**
79
* Example test.
810
*/
9-
public class AddressDetail_Test {
11+
@Category(Unit.class)
12+
public class AddressDetailTest {
1013

1114
@Test
1215
public void testCountryCodeAccessors() {

Diff for: model-api/src/test/java/com/sample/app/model/client/ModelPojoTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
import com.openpojo.validation.test.impl.GetterTester;
99
import com.openpojo.validation.test.impl.SerializableTester;
1010
import com.openpojo.validation.test.impl.SetterTester;
11+
import com.sample.app.test.Unit;
1112
import org.junit.Test;
13+
import org.junit.experimental.categories.Category;
1214

1315
/**
1416
* Unit tests for model POJOs.
1517
*/
18+
@Category(Unit.class)
1619
public class ModelPojoTest {
1720

1821
// The package to test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.sample.app.test;
2+
3+
/**
4+
* Unit test category.
5+
*/
6+
public interface Integration {
7+
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.sample.app.test;
2+
3+
/**
4+
* Smoke test category.
5+
*/
6+
public interface Smoke {
7+
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.sample.app.test;
2+
3+
/**
4+
* Unit test category.
5+
*/
6+
public interface Unit {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Unit test categories.
3+
*/
4+
package com.sample.app.test;

Diff for: model-mock-impl/src/test/java/com/sample/app/model/bdd/runner/RunCucumberTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.sample.app.model.bdd.runner;
22

33
import com.sample.app.model.impl.MockDataUtil;
4+
import com.sample.app.test.Unit;
45
import cucumber.api.CucumberOptions;
56
import net.serenitybdd.cucumber.CucumberWithSerenity;
67
import org.junit.AfterClass;
78
import org.junit.BeforeClass;
9+
import org.junit.experimental.categories.Category;
810
import org.junit.runner.RunWith;
911

1012
/**
@@ -16,6 +18,7 @@
1618
features = "classpath:api/bdd/features",
1719
glue = {"com.sample.app.model.bdd.steps"}
1820
)
21+
@Category(Unit.class)
1922
public class RunCucumberTest {
2023

2124
@BeforeClass

Diff for: pom.xml

+36
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@
3939
<enforcer.skip>true</enforcer.skip>
4040
</properties>
4141
</profile>
42+
<profile>
43+
<id>smoke</id>
44+
<properties>
45+
<bt.qa.skip>true</bt.qa.skip>
46+
<dependency-check.skip>true</dependency-check.skip>
47+
<enforcer.skip>true</enforcer.skip>
48+
</properties>
49+
<build>
50+
<plugins>
51+
<!-- Run Smoke Test Category -->
52+
<!-- mvn test -Psmoke -->
53+
<!-- Default properties -Dserver.smoke.port=8082 -Dserver.smoke.host=http://localhost -Dserver.smoke.base=/lde/api/v1/ -->
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-surefire-plugin</artifactId>
57+
<version>2.18</version>
58+
<configuration>
59+
<groups>com.sample.app.test.Smoke</groups>
60+
</configuration>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
</profile>
4265
</profiles>
4366

4467
<build>
@@ -59,6 +82,19 @@
5982
</plugin>
6083
</plugins>
6184
</pluginManagement>
85+
86+
<plugins>
87+
<!-- Run Unit Test Category -->
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-surefire-plugin</artifactId>
91+
<!-- After 2.18 Cucumber runner does not work with Categories -->
92+
<version>2.18</version>
93+
<configuration>
94+
<groups>com.sample.app.test.Unit</groups>
95+
</configuration>
96+
</plugin>
97+
</plugins>
6298
</build>
6399

64100
<dependencyManagement>

Diff for: rest-server-impl/pom.xml

+42
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<artifactId>rest-server-impl</artifactId>
1414
<name>rest-server-impl</name>
1515

16+
<properties>
17+
<enforcer.fail>false</enforcer.fail>
18+
<!-- Needed for tomcat jar lookups -->
19+
<surefire.useSystemClassLoader>false</surefire.useSystemClassLoader>
20+
</properties>
21+
1622
<dependencies>
1723

1824
<!-- API -->
@@ -41,6 +47,42 @@
4147
<scope>provided</scope>
4248
</dependency>
4349

50+
<!-- JUNIT -->
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
57+
<!-- Rest Assured (Uses GSON for JSON) -->
58+
<dependency>
59+
<groupId>io.rest-assured</groupId>
60+
<artifactId>rest-assured</artifactId>
61+
<version>4.2.0</version>
62+
<scope>test</scope>
63+
</dependency>
64+
65+
<!-- MOCK Services -->
66+
<dependency>
67+
<groupId>com.github.bordertech.sample.app</groupId>
68+
<artifactId>model-mock-impl</artifactId>
69+
<version>${project.version}</version>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>com.github.bordertech.sample.app</groupId>
74+
<artifactId>model-bdd</artifactId>
75+
<version>${project.version}</version>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<!-- Tomcat LDE -->
80+
<dependency>
81+
<groupId>com.github.bordertech.lde</groupId>
82+
<artifactId>lde-tomcat</artifactId>
83+
<scope>test</scope>
84+
</dependency>
85+
4486
</dependencies>
4587

4688
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.restfriends.sample.restassured.smoke;
2+
3+
import com.sample.app.test.Smoke;
4+
import io.restassured.RestAssured;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
import org.junit.experimental.categories.Category;
8+
9+
/**
10+
* Sample API Smoke tests.
11+
*/
12+
@Category(Smoke.class)
13+
public class SampleApiSmokeTest {
14+
15+
@BeforeClass
16+
public static void setupRest() {
17+
18+
// Port
19+
String port = System.getProperty("server.smoke.port");
20+
RestAssured.port = port == null ? 8082 : Integer.valueOf(port);
21+
22+
// Base
23+
String basePath = System.getProperty("server.smoke.base");
24+
RestAssured.basePath = basePath == null ? "/lde/api/v1/" : basePath;
25+
26+
// Host
27+
String baseHost = System.getProperty("server.smoke.host");
28+
RestAssured.baseURI = baseHost == null ? "http://localhost" : baseHost;
29+
30+
// Logging
31+
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
32+
}
33+
34+
@Test
35+
public void basicPingTest() {
36+
RestAssured.given().when().get("/clients").then().statusCode(200);
37+
}
38+
39+
@Test
40+
public void basicSearchSystemError() {
41+
RestAssured.given().queryParam("search", "error").when().get("/clients").then().statusCode(400);
42+
}
43+
44+
@Test
45+
public void basicRetrieveClient() {
46+
RestAssured.given().pathParam("id", "ORG1").when().get("/clients/{id}").then().statusCode(200);
47+
}
48+
49+
@Test
50+
public void basicRetrieveClientError() {
51+
RestAssured.given().pathParam("id", "error").when().get("/clients/{id}").then().statusCode(400);
52+
}
53+
54+
@Test
55+
public void basicRetrieveClientNotFound() {
56+
RestAssured.given().pathParam("id", "notfound").when().get("/clients/{id}").then().statusCode(400);
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

Diff for: rest-server-impl/src/test/resources/api-tests/AttachmentViewer.postman_collection.json

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## TOMCAT Settings
2+
bordertech.lde.port.default=8082
3+
4+
## MOCK Environment
5+
bordertech.config.environment=MOCK
6+
7+
## TODO Fix CONFIG to dump parameters
8+
bordertech.config.parameters.dump.console=true

0 commit comments

Comments
 (0)