Commits

Micha Kops committed bbd7de8

REST Services Testing Tutorial Sources added

  • Participants
  • Parent commits 640cbd3

Comments (0)

Files changed (6)

rest-test-tutorial/pom.xml

+<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/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>rest-test-tutorial</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<packaging>war</packaging>
+
+	<dependencies>
+		<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>com.jayway.restassured</groupId>
+			<artifactId>rest-assured</artifactId>
+			<version>1.2.3</version>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.9</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>com.sun.jersey</groupId>
+			<artifactId>jersey-server</artifactId>
+			<version>1.9</version>
+		</dependency>
+		<dependency>
+			<groupId>com.sun.jersey.jersey-test-framework</groupId>
+			<artifactId>jersey-test-framework-core</artifactId>
+			<version>1.9</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>com.sun.jersey.jersey-test-framework</groupId>
+			<artifactId>jersey-test-framework-external</artifactId>
+			<version>1.9</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>glassfish-repository</id>
+			<name>Java.net Repository for Glassfish</name>
+			<url>http://download.java.net/maven/glassfish</url>
+		</repository>
+	</repositories>
+</project>

rest-test-tutorial/src/main/java/com/hascode/tutorial/rest/User.java

+package com.hascode.tutorial.rest;
+
+import java.util.Date;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+public class User {
+	private Long id;
+
+	private String firstName;
+
+	private String lastName;
+
+	private Date birthday;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(final Long id) {
+		this.id = id;
+	}
+
+	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 Date getBirthday() {
+		return birthday;
+	}
+
+	public void setBirthday(final Date birthday) {
+		this.birthday = birthday;
+	}
+}

rest-test-tutorial/src/main/java/com/hascode/tutorial/rest/UserService.java

+package com.hascode.tutorial.rest;
+
+import java.util.Date;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Path("/user")
+public class UserService {
+	@GET
+	@Produces(MediaType.APPLICATION_JSON)
+	@Path("/id/{id}")
+	public User findById(@PathParam("id") final Long id) {
+		if (id.equals(666l)) {
+			return null;
+		}
+		final User user = new User();
+		user.setId(id);
+		user.setFirstName("Tim");
+		user.setLastName("Tester");
+		user.setBirthday(new Date(1321009871));
+		return user;
+	}
+}

rest-test-tutorial/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.tutorial.rest</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>
+</web-app>

rest-test-tutorial/src/test/java/com/hascode/tutorial/rest/UserServiceTestUsingJerseyTestFramework.java

+package com.hascode.tutorial.rest;
+
+import static org.junit.Assert.assertEquals;
+
+import java.net.URISyntaxException;
+
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.junit.Test;
+
+import com.sun.jersey.api.client.UniformInterfaceException;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.test.framework.AppDescriptor;
+import com.sun.jersey.test.framework.JerseyTest;
+import com.sun.jersey.test.framework.WebAppDescriptor;
+
+public class UserServiceTestUsingJerseyTestFramework extends JerseyTest {
+	@Override
+	protected AppDescriptor configure() {
+		return new WebAppDescriptor.Builder().build();
+	}
+
+	@Test
+	public void testUserFetchesSuccess() throws JSONException,
+			URISyntaxException {
+		WebResource webResource = client().resource("http://localhost:8080/");
+		JSONObject json = webResource.path("/rest-test-tutorial/user/id/12")
+				.get(JSONObject.class);
+		assertEquals("12", json.get("id"));
+		assertEquals("Tim", json.get("firstName"));
+		assertEquals("Tester", json.get("lastName"));
+		assertEquals("1970-01-16T07:56:49.871+01:00", json.get("birthday"));
+	}
+
+	@Test(expected = UniformInterfaceException.class)
+	public void testUserNotFound() {
+		WebResource webResource = client().resource("http://localhost:8080/");
+		JSONObject json = webResource.path("/rest-test-tutorial/user/id/666")
+				.get(JSONObject.class);
+	}
+}

rest-test-tutorial/src/test/java/com/hascode/tutorial/rest/UserServiceTestUsingRestAssured.java

+package com.hascode.tutorial.rest;
+
+import static com.jayway.restassured.RestAssured.expect;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.nullValue;
+
+import org.junit.Test;
+
+public class UserServiceTestUsingRestAssured {
+	@Test
+	public void testUserFetchesSuccess() {
+		expect().body("id", equalTo("12")).body("firstName", equalTo("Tim"))
+				.body("lastName", equalTo("Tester"))
+				.body("birthday", equalTo("1970-01-16T07:56:49.871+01:00"))
+				.when().get("/rest-test-tutorial/user/id/12");
+	}
+
+	@Test
+	public void testUserNotFound() {
+		expect().body(nullValue()).when()
+				.get("/rest-test-tutorial/user/id/666");
+	}
+}