Skip to content

Commit 86a37c7

Browse files
committed
added maven project for rest assured tutorial
1 parent 06dd18a commit 86a37c7

12 files changed

Lines changed: 446 additions & 0 deletions

File tree

‎rest-assured-tutorial/.classpath‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
31+
<attributes>
32+
<attribute name="owner.project.facets" value="java"/>
33+
</attributes>
34+
</classpathentry>
35+
<classpathentry kind="output" path="target/classes"/>
36+
</classpath>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
3+
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_BUILDER_ENABLED" value="false"/>
4+
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_DISABLED_BUILDER" value="org.eclipse.wst.jsdt.core.javascriptValidator"/>
5+
<mapAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS"/>
6+
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
7+
</launchConfiguration>

‎rest-assured-tutorial/.gitignore‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

‎rest-assured-tutorial/.project‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>rest-assured-tutorial</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.validation.validationbuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.m2e.core.maven2Builder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
</buildSpec>
29+
<natures>
30+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
31+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
32+
<nature>org.eclipse.jdt.core.javanature</nature>
33+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
34+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
35+
</natures>
36+
</projectDescription>

‎rest-assured-tutorial/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=========
2+
3+
## A Guide To REST-Assured
4+
5+
6+
### Relevant Articles:
7+

‎rest-assured-tutorial/pom.xml‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>rest-assured</artifactId>
6+
<version>0.1.0-SNAPSHOT</version>
7+
8+
<name>rest-assured</name>
9+
10+
<dependencies>
11+
12+
<!-- utils -->
13+
<dependency>
14+
<groupId>io.rest-assured</groupId>
15+
<artifactId>rest-assured</artifactId>
16+
<version>3.0.0</version>
17+
<scope>test</scope>
18+
</dependency>
19+
<dependency>
20+
<groupId>io.rest-assured</groupId>
21+
<artifactId>json-schema-validator</artifactId>
22+
<version>3.0.0</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.github.fge</groupId>
26+
<artifactId>json-schema-validator</artifactId>
27+
<version>2.2.6</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.github.fge</groupId>
31+
<artifactId>json-schema-core</artifactId>
32+
<version>1.2.5</version>
33+
</dependency>
34+
35+
<!-- web -->
36+
37+
<!-- test scoped -->
38+
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>4.3</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.hamcrest</groupId>
48+
<artifactId>hamcrest-all</artifactId>
49+
<version>1.3</version>
50+
</dependency>
51+
52+
</dependencies>
53+
54+
<build>
55+
<finalName>rest-assured</finalName>
56+
<resources>
57+
<resource>
58+
<directory>src/main/resources</directory>
59+
<filtering>true</filtering>
60+
</resource>
61+
</resources>
62+
63+
<plugins>
64+
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<version>${maven-compiler-plugin.version}</version>
69+
<configuration>
70+
<source>1.8</source>
71+
<target>1.8</target>
72+
</configuration>
73+
</plugin>
74+
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-surefire-plugin</artifactId>
78+
<version>${maven-surefire-plugin.version}</version>
79+
</plugin>
80+
81+
</plugins>
82+
83+
</build>
84+
85+
86+
</project>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.baeldung.restassured;
2+
3+
import static io.restassured.RestAssured.get;
4+
import static io.restassured.RestAssured.post;
5+
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
6+
import static io.restassured.module.jsv.JsonSchemaValidatorSettings.settings;
7+
import static org.hamcrest.Matchers.equalTo;
8+
import static org.hamcrest.Matchers.hasItems;
9+
import static org.hamcrest.Matchers.containsString;
10+
import io.restassured.module.jsv.JsonSchemaValidator;
11+
import static org.hamcrest.xml.HasXPath.hasXPath;
12+
13+
import org.junit.Test;
14+
15+
import com.github.fge.jsonschema.SchemaVersion;
16+
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
17+
import com.github.fge.jsonschema.main.JsonSchemaFactory;
18+
19+
public class RestAssuredTest {
20+
21+
@Test
22+
public void givenJsonResponse_whenKeyValuePairMatches_thenCorrect() {
23+
JsonSchemaFactory factory = JsonSchemaFactory
24+
.newBuilder()
25+
.setValidationConfiguration(
26+
ValidationConfiguration.newBuilder()
27+
.setDefaultVersion(SchemaVersion.DRAFTV3)
28+
.freeze()).freeze();
29+
JsonSchemaValidator.settings = settings().with()
30+
.jsonSchemaFactory(factory).and().with()
31+
.checkedValidation(false);
32+
}
33+
34+
@Test
35+
public void givenJsonArrayOfSimilarObjects_whenHasGivenValuesForGivenKey_thenCorrect() {
36+
37+
}
38+
39+
@Test
40+
public void givenUrl_whenSuccessOnGetsResponse_andJsonHasRequiredKV_thenCorrect() {
41+
42+
get("/events?id=390").then().statusCode(200).assertThat()
43+
.body("data.id", equalTo(390));
44+
45+
}
46+
47+
@Test
48+
public void givenUrl_whenJsonResponseHasArrayWithGivenValuesUnderKey_thenCorrect() {
49+
50+
get("/events?id=390").then().assertThat()
51+
.body("odds.price", hasItems("1.30", "5.25"));
52+
53+
}
54+
55+
@Test
56+
public void givenUrl_whenJsonResponseConformsToSchema_thenCorrect() {
57+
get("/events?id=390").then().assertThat()
58+
.body(matchesJsonSchemaInClasspath("event_0.json"));
59+
}
60+
61+
@Test
62+
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
63+
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory
64+
.newBuilder()
65+
.setValidationConfiguration(
66+
ValidationConfiguration.newBuilder()
67+
.setDefaultVersion(SchemaVersion.DRAFTV4)
68+
.freeze()).freeze();
69+
70+
get("/events?id=390")
71+
.then()
72+
.assertThat()
73+
.body(matchesJsonSchemaInClasspath("event_0.json").using(
74+
jsonSchemaFactory));
75+
76+
}
77+
78+
@Test
79+
public void givenUrl_whenValidatesResponseWithStaticSettings_thenCorrect() {
80+
81+
get("/events?id=390")
82+
.then()
83+
.assertThat()
84+
.body(matchesJsonSchemaInClasspath("event_0.json").using(
85+
settings().with().checkedValidation(false)));
86+
87+
}
88+
89+
@Test
90+
public void givenUrl_whenCheckingFloatValuePasses_thenCorrect() {
91+
get("/odd").then().assertThat().body("odd.ck", equalTo(12.2f));
92+
}
93+
94+
@Test
95+
public void givenUrl_whenXmlResponseValueTestsEqual_thenCorrect() {
96+
post("/employees").then().assertThat()
97+
.body("employees.employee.first-name", equalTo("Jane"));
98+
}
99+
100+
@Test
101+
public void givenUrl_whenMultipleXmlValuesTestEqual_thenCorrect() {
102+
post("/employees").then().assertThat()
103+
.body("employees.employee.first-name", equalTo("Jane"))
104+
.body("employees.employee.last-name", equalTo("Daisy"))
105+
.body("employees.employee.sex", equalTo("f"));
106+
}
107+
108+
@Test
109+
public void givenUrl_whenMultipleXmlValuesTestEqualInShortHand_thenCorrect() {
110+
post("/employees")
111+
.then()
112+
.assertThat()
113+
.body("employees.employee.first-name", equalTo("Jane"),
114+
"employees.employee.last-name", equalTo("Daisy"),
115+
"employees.employee.sex", equalTo("f"));
116+
}
117+
118+
@Test
119+
public void givenUrl_whenValidatesXmlUsingXpath_thenCorrect() {
120+
post("/employees")
121+
.then()
122+
.assertThat()
123+
.body(hasXPath("/employees/employee/first-name",
124+
containsString("Ja")));
125+
126+
}
127+
128+
@Test
129+
public void givenUrl_whenValidatesXmlUsingXpath2_thenCorrect() {
130+
post("/employees")
131+
.then()
132+
.assertThat()
133+
.body(hasXPath("/employees/employee/first-name[text()='Jane']"));
134+
135+
}
136+
137+
@Test
138+
public void givenUrl_whenVerifiesScienceTeacherFromXml_thenCorrect() {
139+
get("/teachers")
140+
.then()
141+
.body("teachers.teacher.find { it.@department == 'science' }.subject",
142+
hasItems("math", "physics"));
143+
}
144+
145+
@Test
146+
public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
147+
get("/odds").then().body("odds.findAll { it.status > 0 }.price",
148+
hasItems(1.30f, 1.20f));
149+
}
150+
151+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<employees>
2+
<employee category="skilled">
3+
<first-name>Jane</first-name>
4+
<last-name>Daisy</last-name>
5+
<sex>f</sex>
6+
</employee>
7+
<employee category="unskilled">
8+
<first-name>John</first-name>
9+
<last-name>Doe</last-name>
10+
<sex>m</sex>
11+
</employee>
12+
<employee category="skilled">
13+
<first-name>Billy</first-name>
14+
<last-name>Getty</last-name>
15+
<sex>m</sex>
16+
</employee>
17+
<employee category="skilled">
18+
<first-name>Hill</first-name>
19+
<last-name>Clinton</last-name>
20+
<sex>f</sex>
21+
</employee>
22+
</employees>

0 commit comments

Comments
 (0)