Micha Kops avatar Micha Kops committed 81e7a5c

initial import

Comments (0)

Files changed (9)

+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>rest-assured-examples-webapp</artifactId>
+	<packaging>war</packaging>
+	<version>0.0.1-SNAPSHOT</version>
+	<name>rest-assured-examples-webapp Maven Webapp</name>
+	<url>http://maven.apache.org</url>
+	<dependencies>
+		<dependency>
+			<groupId>com.sun.jersey</groupId>
+			<artifactId>jersey-server</artifactId>
+			<version>1.9</version>
+		</dependency>
+		<dependency>
+			<groupId>javax.xml.bind</groupId>
+			<artifactId>jaxb-api</artifactId>
+			<version>2.2.4</version>
+		</dependency>
+		<dependency>
+			<groupId>com.sun.xml.bind</groupId>
+			<artifactId>jaxb-impl</artifactId>
+			<version>2.2.4</version>
+		</dependency>
+		<dependency>
+			<groupId>com.sun.jersey</groupId>
+			<artifactId>jersey-core</artifactId>
+			<version>1.9</version>
+		</dependency>
+		<dependency>
+			<groupId>com.sun.jersey</groupId>
+			<artifactId>jersey-json</artifactId>
+			<version>1.9</version>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.10</version>
+		</dependency>
+		<dependency>
+			<groupId>com.jayway.restassured</groupId>
+			<artifactId>rest-assured</artifactId>
+			<version>1.2.3</version>
+		</dependency>
+	</dependencies>
+	<repositories>
+		<repository>
+			<id>maven2-repository.dev.java.net</id>
+			<name>Java.net Repository for Maven</name>
+			<url>http://download.java.net/maven/2/</url>
+			<layout>default</layout>
+		</repository>
+		<repository>
+			<id>maven-repository.dev.java.net</id>
+			<name>Java.net Maven 1 Repository (legacy)</name>
+			<url>http://download.java.net/maven/1</url>
+			<layout>legacy</layout>
+		</repository>
+	</repositories>
+	<build>
+		<finalName>rest-assured-example</finalName>
+	</build>
+</project>

src/main/java/com/hascode/ra_samples/Person.java

+package com.hascode.ra_samples;
+
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "person")
+public class Person {
+
+	private String firstName;
+	private String lastName;
+	private Long id;
+	private String email;
+
+	public String getFirstName() {
+		return firstName;
+	}
+
+	public void setFirstName(final String firstName) {
+		this.firstName = firstName;
+	}
+
+	public String getLastName() {
+		return lastName;
+	}
+
+	public void setLastName(final String lastName) {
+		this.lastName = lastName;
+	}
+
+	@XmlAttribute(name = "id")
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(final Long id) {
+		this.id = id;
+	}
+
+	public String getEmail() {
+		return email;
+	}
+
+	public void setEmail(final String email) {
+		this.email = email;
+	}
+}

src/main/java/com/hascode/ra_samples/RestAssuredSampleService.java

+package com.hascode.ra_samples;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Path("raexample")
+public class RestAssuredSampleService {
+	@GET
+	@Produces(MediaType.APPLICATION_JSON)
+	@Path("/single-user")
+	public User getSingleUser() {
+		User user = new User();
+		user.setEmail("test@hascode.com");
+		user.setFirstName("Tim");
+		user.setLastName("Testerman");
+		user.setId(1L);
+		return user;
+	}
+
+	@GET
+	@Produces(MediaType.APPLICATION_XML)
+	@Path("/single-user/xml")
+	public User getSingleUserAsXml() {
+		User user = new User();
+		user.setEmail("test@hascode.com");
+		user.setFirstName("Tim");
+		user.setLastName("Testerman");
+		user.setId(1L);
+		return user;
+	}
+
+	@GET
+	@Produces(MediaType.APPLICATION_XML)
+	@Path("/persons/xml")
+	public List<Person> allUsers() {
+		List<Person> users = new ArrayList<Person>();
+		Person person1 = new Person();
+		person1.setEmail("test@hascode.com");
+		person1.setFirstName("Tim");
+		person1.setLastName("Testerman");
+		person1.setId(1L);
+		users.add(person1);
+
+		Person person2 = new Person();
+		person2.setEmail("dev@hascode.com");
+		person2.setFirstName("Sara");
+		person2.setLastName("Stevens");
+		person2.setId(20L);
+		users.add(person2);
+
+		Person person3 = new Person();
+		person3.setEmail("devnull@hascode.com");
+		person3.setFirstName("Mark");
+		person3.setLastName("Mustache");
+		person3.setId(11L);
+		users.add(person3);
+		return users;
+	}
+}

src/main/java/com/hascode/ra_samples/User.java

+package com.hascode.ra_samples;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+public class User {
+	private String firstName;
+	private String lastName;
+	private Long id;
+	private String email;
+
+	public String getFirstName() {
+		return firstName;
+	}
+
+	public void setFirstName(final String firstName) {
+		this.firstName = firstName;
+	}
+
+	public String getLastName() {
+		return lastName;
+	}
+
+	public void setLastName(final String lastName) {
+		this.lastName = lastName;
+	}
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(final Long id) {
+		this.id = id;
+	}
+
+	public String getEmail() {
+		return email;
+	}
+
+	public void setEmail(final String email) {
+		this.email = email;
+	}
+}

src/main/resources/user.xml

+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<user xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="user.xsd">
+	<email>test@hascode.com</email>
+	<firstName>Tim</firstName>
+	<id>1</id>
+	<lastName>Testerman</lastName>
+</user>
+

src/main/resources/user.xsd

+<?xml version="1.0" encoding="UTF-8"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema">
+
+	<element name="user">
+		<complexType>
+			<sequence>
+				<element name="email">
+					<simpleType>
+						<restriction base="string">
+							<pattern value=".+@.+"></pattern>
+						</restriction>
+					</simpleType>
+				</element>
+				<element name="firstName" type="string"></element>
+				<element name="id" type="int"></element>
+				<element name="lastName" type="string"></element>
+			</sequence>
+		</complexType>
+	</element>
+</schema>

src/main/webapp/WEB-INF/web.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+    <servlet>
+        <servlet-name>Jersey REST Servlet</servlet-name>
+        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
+        <init-param>
+            <param-name>com.sun.jersey.config.property.packages</param-name>
+            <param-value>com.hascode.ra_samples</param-value>
+        </init-param>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>Jersey REST Servlet</servlet-name>
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+    
+    <welcome-file-list>
+    	<welcome-file>/index.html</welcome-file>
+    </welcome-file-list>
+</web-app>

src/main/webapp/index.html

+<html>
+	<head>
+		<title>hasCode.com - REST-assured Examples Webapp</title>
+	</head>
+	<body>
+		<h1>hasCode.com REST-assured xxamples</h1>
+		<table>
+			<caption>Examples Overview</caption>
+			<thead>
+				<tr>
+					<th>URL</th>
+					<th>Method</th>
+					<th>Content-Type</th>
+					<th>HTTP-Status</th>
+					<th>Body</th>
+				</tr>
+			</thead>
+			<tbody>
+				<tr>
+					<td colspan="4" align="center"><em>Validate returned JSON via Get</em></td>
+				</tr>
+				<tr>
+					<td><a href="/rest-assured-examples-webapp/raexample/single-user">/rest-assured-examples-webapp/raexample/single-user</a></td>
+					<td>GET</td>
+					<td>application/json</td>
+					<td>200</td>
+					<td><blockquote><pre escaped="true" lang="json">{
+  "email":"test@hascode.com",
+  "firstName":"Tim",
+  "id":"1",
+  "lastName":"Testerman"
+}</pre></blockquote></td>
+				</tr>
+				<tr>
+					<td colspan="4" align="center"><em>XML Example</em></td>
+				</tr>
+				<tr>
+					<td><a href="/rest-assured-examples-webapp/raexample/user-with-attrs/xml">/rest-assured-examples-webapp/raexample/user-with-attrs/xml</a></td>
+					<td>GET</td>
+					<td>application/xml</td>
+					<td>200</td>
+					<td><blockquote><pre escaped="true" lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+&lt;user&gt;
+  &lt;email&gt;test@hascode.com&lt;/email&gt;
+  &lt;firstName&gt;Tim&lt;/firstName&gt;
+  &lt;id&gt;1&lt;/id&gt;
+  &lt;lastName&gt;Testerman&lt;/lastName&gt;
+&lt;/user&gt;</pre></blockquote></td>
+				</tr>
+							<tr>
+					<td colspan="4" align="center"><em>XML using XPath</em></td>
+				</tr>
+				<tr>
+					<td><a href="/rest-assured-examples-webapp/raexample/persons/xml">/rest-assured-examples-webapp/raexample/persons/xml</a></td>
+					<td>GET</td>
+					<td>application/xml</td>
+					<td>200</td>
+					<td><blockquote><pre escaped="true" lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+&lt;people&gt;
+  &lt;person id="1"&gt;
+    &lt;email&gt;test@hascode.com&lt;/email&gt;
+    &lt;firstName&gt;Tim&lt;/firstName&gt;
+    &lt;lastName&gt;Testerman&lt;/lastName&gt;
+  &lt;/person&gt;
+  &lt;person id="20"&gt;
+    &lt;email&gt;dev@hascode.com&lt;/email&gt;
+    &lt;firstName&gt;Sara&lt;/firstName&gt;
+    &lt;lastName&gt;Stevens&lt;/lastName&gt;
+  &lt;/person&gt;
+  &lt;person id="11"&gt;
+    &lt;email&gt;devnull@hascode.com&lt;/email&gt;
+    &lt;firstName&gt;Mark&lt;/firstName&gt;
+    &lt;lastName&gt;Mustache&lt;/lastName&gt;
+  &lt;/person&gt;
+&lt;/people&gt;</pre></blockquote></td>
+				</tr>
+								<tr>
+					<td colspan="4" align="center"><em></em></td>
+				</tr>
+				<tr>
+					<td><a href="/rest-assured-examples-webapp/raexample/">/rest-assured-examples-webapp/raexample/</a></td>
+					<td></td>
+					<td></td>
+					<td></td>
+					<td></td>
+				</tr>
+			</tbody>
+		</table>
+	</body>
+</html>

src/test/java/it/RestAssuredSampleServiceIT.java

+package it;
+
+import static com.jayway.restassured.RestAssured.expect;
+import static com.jayway.restassured.RestAssured.get;
+import static com.jayway.restassured.matcher.RestAssuredMatchers.matchesXsd;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasXPath;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.InputStream;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+import com.jayway.restassured.path.json.JsonPath;
+import com.jayway.restassured.response.Response;
+
+public class RestAssuredSampleServiceIT {
+
+	/**
+	 * Tutorial note: The JSON returned is this
+	 * <code>{"email":"test@hascode.com","firstName":"Tim","id":"1","lastName":"Testerman"}</code>
+	 */
+	@Ignore
+	@Test
+	public void testGetSingleUser() {
+		expect().statusCode(equalTo(200))
+				.body("email", equalTo("test@hascode.com"), "firstName",
+						equalTo("Tim"), "lastName", equalTo("Testerman"), "id",
+						equalTo("1")).when()
+				.get("/rest-assured-examples-webapp/raexample/single-user");
+	}
+
+	/**
+	 * Tutorial note: The JSON returned is this
+	 * <code>{"email":"test@hascode.com","firstName":"Tim","id":"1","lastName":"Testerman"}</code>
+	 */
+	@Ignore
+	@Test
+	public void testGetSingleUserProgrammatic() {
+		Response res = get("/rest-assured-examples-webapp/raexample/single-user");
+		assertEquals(200, res.getStatusCode());
+		String json = res.asString();
+		JsonPath jp = new JsonPath(json);
+		assertEquals("test@hascode.com", jp.get("email"));
+		assertEquals("Tim", jp.get("firstName"));
+		assertEquals("Testerman", jp.get("lastName"));
+		assertEquals("1", jp.get("id"));
+	}
+
+	@Ignore
+	@Test
+	public void testGetSingleUserAsXml() {
+		expect().statusCode(equalTo(200))
+				.body("user.email", equalTo("test@hascode.com"),
+						"user.firstName", equalTo("Tim"), "user.lastName",
+						equalTo("Testerman"), "user.id", equalTo("1")).when()
+				.get("/rest-assured-examples-webapp/raexample/single-user/xml");
+	}
+
+	@Ignore
+	@Test
+	public void testGetPersons() {
+		expect().statusCode(equalTo(200))
+				.body(hasXPath("//person[@id='1']/email[.='test@hascode.com'] and firstName='Tim' and lastName='Testerman'"))
+				.body(hasXPath("//person[@id='20']/email[.='dev@hascode.com'] and firstName='Sara' and lastName='Stevens'"))
+				.body(hasXPath("//person[@id='1']/email[.='devnull@hascode.com'] and firstName='Mark' and lastName='Mustache'"))
+				.when()
+				.get("/rest-assured-examples-webapp/raexample/persons/xml");
+	}
+
+	@Test
+	public void testGetSingleUserAgainstSchema() {
+		InputStream xsd = getClass().getResourceAsStream("/user.xsd");
+		assertNotNull(xsd);
+		expect().statusCode(equalTo(200)).body(matchesXsd(xsd)).when()
+				.get("/rest-assured-examples-webapp/raexample/single-user/xml");
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.