Commits

Micha Kops committed 50ab48b

Service documentation added, Jersey configuration modified to deliver html files.

  • Participants
  • Parent commits f29dbda

Comments (0)

Files changed (5)

 				<version>1.1</version>
 				<configuration>
 					<mode>both</mode>
-					<path>/ra</path>
+					<path>/</path>
 				</configuration>
 			</plugin>
 		</plugins>

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

 	@GET
 	@Produces(MediaType.TEXT_PLAIN)
 	@Path("/secure/person")
-	public String secureGetPerson() {
-		return "Ok";
+	public Response secureGetPerson() {
+		return Response.ok().build();
 	}
 
 	@GET
 	}
 
 	@GET
-	@Produces(MediaType.APPLICATION_XML)
-	@Consumes(MediaType.APPLICATION_XML)
-	@Path("/contentype/accept")
-	public Response restrictToSingleContentType() {
-		return Response.noContent().build();
-	}
-
-	@GET
 	@Produces(MediaType.TEXT_PLAIN)
 	@Path("/access/cookie-token-secured")
 	public Response accessSecuredByCookie(
 		}
 		return "";
 	}
-	// TODO:
-	// Setting the Content Type
-	// Verifying the Content Type
-
-	// Setting default values
-	// Specification reuse
-
 }

File 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>
-    
-    <security-role>
-        <role-name>admin</role-name>
-    </security-role>
-    
-    <security-constraint>
-        <display-name>MySecurityConstraint</display-name>
-        <web-resource-collection>
-            <web-resource-name>SecuredRestService</web-resource-name>
-            <url-pattern>/service/secure/*</url-pattern>
-        </web-resource-collection>
-        <auth-constraint>
-            <role-name>admin</role-name>
-        </auth-constraint>
-    </security-constraint>
-    
-    <login-config>
-        <auth-method>BASIC</auth-method>
-        <realm-name>hasCode.com Secured REST Service</realm-name>
-    </login-config>
+<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">
+	<filter>
+		<filter-name>jersey</filter-name>
+		<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
+		<init-param>
+			<param-name>com.sun.jersey.config.property.packages</param-name>
+			<param-value>com.hascode.ra_samples</param-value>
+		</init-param>
+		<init-param>
+			<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
+			<param-value>/index.html</param-value>
+		</init-param>
+	</filter>
+	<filter-mapping>
+		<filter-name>jersey</filter-name>
+		<url-pattern>/*</url-pattern>
+	</filter-mapping>
+	
+	<welcome-file-list>
+		<welcome-file>index.html</welcome-file>
+	</welcome-file-list>
+
+	<security-role>
+		<role-name>admin</role-name>
+	</security-role>
+
+	<security-constraint>
+		<display-name>MySecurityConstraint</display-name>
+		<web-resource-collection>
+			<web-resource-name>SecuredRestService</web-resource-name>
+			<url-pattern>/service/secure/*</url-pattern>
+		</web-resource-collection>
+		<auth-constraint>
+			<role-name>admin</role-name>
+		</auth-constraint>
+	</security-constraint>
+
+	<login-config>
+		<auth-method>BASIC</auth-method>
+		<realm-name>hasCode.com Secured REST Service</realm-name>
+	</login-config>
 </web-app>

File src/main/webapp/index.html

 <html>
 	<head>
-		<title>hasCode.com - REST-assured Examples Webapp</title>
+		<title>hasCode.com - REST-assured Examples</title>
+		<style type="text/css">
+			body {
+				font-family: Arial, sans-serif;
+			}
+			h1 {
+				font-size:20px;
+				border-bottom:5px solid #000;
+				margin-top:20px;
+			}
+				
+			table, td {
+				border:1px solid #000;
+				padding: 10px;
+				border-collapse:collapse;
+				font-size:12px;
+			}	
+			th {
+				background-color:#000;
+				color:#fff;
+				font-size:13px;
+				height:20px;
+				padding:5px 10px;
+			}
+			blockquote {
+				text-align:left;
+			}
+			em {
+				font-weight:bold;
+				font-style:normal;
+				font-size:13px;
+			}
+			.type {
+				background-color:#666;
+			}
+			.header {
+				background-color:#ddd;
+			}
+		</style>
 	</head>
 	<body>
-		<h1>hasCode.com REST-assured xxamples</h1>
+		<h1>hasCode.com REST-assured Examples</h1>
 		<table>
-			<caption>Examples Overview</caption>
-			<thead>
+			<caption>Services Overview</caption>
+			<tbody>
+				<!-- Example #1 -->
 				<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>
+					<th colspan="5"><em>Validate returned JSON via GET</em></th>
 				</tr>
 				<tr>
-					<td><a href="/rest-assured-examples-webapp/raexample/single-user">/rest-assured-examples-webapp/raexample/single-user</a></td>
+					<td class="type" rowspan="2">Request</td>
+					<td class="header">URL</td>
+					<td class="header">Method</td>
+					<td class="header">Content-Type</td>
+					<td class="header">Extras</td>
+				</tr>
+				<tr>
+					<td><a href="/service/single-user">/service/single-user</a></td>
 					<td>GET</td>
+					<td>-</td>
+					<td>-</td>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Response</td>
+					<td class="header">Status</td>
+					<td class="header">Content-Type</td>
+					<td colspan="2" class="header">Body/Content</td>
+				</tr>
+				<tr>
+					<td>200</td>
 					<td>application/json</td>
-					<td>200</td>
-					<td><blockquote><pre escaped="true" lang="json">{
+					<td colspan="2"><blockquote><pre escaped="true" lang="json">{
   "email":"test@hascode.com",
   "firstName":"Tim",
   "id":"1",
   "lastName":"Testerman"
 }</pre></blockquote></td>
 				</tr>
+				
+				<!-- Example #2 -->
 				<tr>
-					<td colspan="4" align="center"><em>XML Example</em></td>
+					<th colspan="5"><em>Validate returned XML via GET</em></th>
 				</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 class="type" rowspan="2">Request</td>
+					<td class="header">URL</td>
+					<td class="header">Method</td>
+					<td class="header">Content-Type</td>
+					<td class="header">Extras</td>
+				</tr>
+				<tr>
+					<td><a href="/service/single-user">/service/single-user/xml</a></td>
 					<td>GET</td>
+					<td>-</td>
+					<td>-</td>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Response</td>
+					<td class="header">Status</td>
+					<td class="header">Content-Type</td>
+					<td colspan="2" class="header">Body/Content</td>
+				</tr>
+				<tr>
+					<td>200</td>
 					<td>application/xml</td>
-					<td>200</td>
-					<td><blockquote><pre escaped="true" lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+					<td colspan="2"><blockquote><pre escaped="true" lang="xml">&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
 &lt;user&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;/user&gt;</pre></blockquote></td>
 				</tr>
-							<tr>
-					<td colspan="4" align="center"><em>XML using XPath</em></td>
+				
+				<!-- Example #3 -->
+				<tr>
+					<th colspan="5"><em>XPath Validation</em></th>
 				</tr>
 				<tr>
-					<td><a href="/rest-assured-examples-webapp/raexample/persons/xml">/rest-assured-examples-webapp/raexample/persons/xml</a></td>
+					<td class="type" rowspan="2">Request</td>
+					<td class="header">URL</td>
+					<td class="header">Method</td>
+					<td class="header">Content-Type</td>
+					<td class="header">Extras</td>
+				</tr>	
+				<tr>
+					<td><a href="/service/single-user">/service/persons/xml</a></td>
 					<td>GET</td>
+					<td>-</td>
+					<td>-</td>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Response</td>
+					<td class="header">Status</td>
+					<td class="header">Content-Type</td>
+					<td colspan="2" class="header">Body/Content</td>
+				</tr>
+				<tr>
+					<td>200</td>
 					<td>application/xml</td>
-					<td>200</td>
-					<td><blockquote><pre escaped="true" lang="xml"><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+					<td colspan="2"><blockquote><pre escaped="true" lang="xml">&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
 &lt;people&gt;
   &lt;person id="1"&gt;
     &lt;email&gt;test@hascode.com&lt;/email&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>	
+				
+				<!-- Example #4 -->
+				<tr>
+					<th colspan="5"><em>Passing Parameters</em></th>
 				</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>
+					<td class="type" rowspan="2">Request</td>
+					<td class="header">URL</td>
+					<td class="header">Method</td>
+					<td class="header">Content-Type</td>
+					<td class="header">Extras</td>
 				</tr>
+				<tr>
+					<td><a href="/service/user/create">/service/user/create</a></td>
+					<td>GET</td>
+					<td>-</td>
+					<td>Params: email, firstName, lastName</td>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Response</td>
+					<td class="header">Status</td>
+					<td class="header">Content-Type</td>
+					<td colspan="2" class="header">Body/Content</td>
+				</tr>
+				<tr>
+					<td>200</td>
+					<td>application/json</td>
+					<td colspan="2"><blockquote><pre escaped="true" lang="xml">&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
+&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>	
+
+				<!-- Example #5 -->
+				<tr>
+					<th colspan="5"><em>404 - Page Not Found</em></th>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Request</td>
+					<td class="header">URL</td>
+					<td class="header">Method</td>
+					<td class="header">Content-Type</td>
+					<td class="header">Extras</td>
+				</tr>
+				<tr>
+					<td><a href="/service/status/notfound">/service/status/notfound</a></td>
+					<td>GET</td>
+					<td>-</td>
+					<td>-</td>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Response</td>
+					<td class="header">Status</td>
+					<td class="header">Content-Type</td>
+					<td colspan="2" class="header">Body/Content</td>
+				</tr>
+				<tr>
+					<td>404</td>
+					<td>text/plain</td>
+					<td colspan="2">-</td>
+				</tr>					
+
+				<!-- Example #6 -->
+				<tr>
+					<th colspan="5"><em>Basic Authentication</em></th>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Request</td>
+					<td class="header">URL</td>
+					<td class="header">Method</td>
+					<td class="header">Content-Type</td>
+					<td class="header">Extras</td>
+				</tr>
+				<tr>
+					<td><a href="/service//secure/person">/service//secure/person</a></td>
+					<td>GET</td>
+					<td>-</td>
+					<td>Basic Auth with username=admin and password=admin</td>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Response</td>
+					<td class="header">Status</td>
+					<td class="header">Content-Type</td>
+					<td colspan="2" class="header">Body/Content</td>
+				</tr>
+				<tr>
+					<td>401 / 200</td>
+					<td>text/plain</td>
+					<td colspan="2">-</td>
+				</tr>	
+				
+																						<!-- Example #6 -->
+				<tr>
+					<th colspan="5"><em>HTTP Headers</em></th>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Request</td>
+					<td class="header">URL</td>
+					<td class="header">Method</td>
+					<td class="header">Content-Type</td>
+					<td class="header">Extras</td>
+				</tr>
+				<tr>
+					<td><a href="/service/header/print">/service/header/print</a></td>
+					<td>GET</td>
+					<td>-</td>
+					<td>Header "myparam"</td>
+				</tr>
+				<tr>
+					<td class="type" rowspan="2">Response</td>
+					<td class="header">Status</td>
+					<td class="header">Content-Type</td>
+					<td colspan="2" class="header">Body/Content</td>
+				</tr>
+				<tr>
+					<td>200</td>
+					<td>text/plain</td>
+					<td colspan="2">String value of given header param "myparam"</td>
+				</tr>				
 			</tbody>
 		</table>
 	</body>

File src/test/java/it/RestAssuredSampleServiceIT.java

 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import groovyx.net.http.ContentType;
 
 import java.io.File;
 import java.io.InputStream;
 
-import org.junit.Ignore;
 import org.junit.Test;
 
 import com.jayway.restassured.RestAssured;
+import com.jayway.restassured.builder.ResponseSpecBuilder;
 import com.jayway.restassured.parsing.Parser;
 import com.jayway.restassured.path.json.JsonPath;
 import com.jayway.restassured.response.Response;
+import com.jayway.restassured.specification.ResponseSpecification;
 
+/**
+ * hasCode.com - REST-assured Examples by Micha Kops
+ * 
+ * visit www.hascode.com for the full tutorial
+ */
 public class RestAssuredSampleServiceIT {
 
-	/**
-	 * Tutorial note: The JSON returned is this
-	 * <code>{"email":"test@hascode.com","firstName":"Tim","id":"1","lastName":"Testerman"}</code>
-	 */
 	@Test
 	public void testGetSingleUser() {
 		expect().statusCode(200)
 				.body("email", equalTo("test@hascode.com"), "firstName",
 						equalTo("Tim"), "lastName", equalTo("Testerman"), "id",
-						equalTo("1")).when().get("/ra/service/single-user");
+						equalTo("1")).when().get("/service/single-user");
 	}
 
-	/**
-	 * Tutorial note: The JSON returned is this
-	 * <code>{"email":"test@hascode.com","firstName":"Tim","id":"1","lastName":"Testerman"}</code>
-	 */
 	@Test
 	public void testGetSingleUserProgrammatic() {
-		Response res = get("/ra/service/single-user");
+		Response res = get("/service/single-user");
 		assertEquals(200, res.getStatusCode());
 		String json = res.asString();
 		JsonPath jp = new JsonPath(json);
 				.body("user.email", equalTo("test@hascode.com"),
 						"user.firstName", equalTo("Tim"), "user.lastName",
 						equalTo("Testerman"), "user.id", equalTo("1")).when()
-				.get("/ra/service/single-user/xml");
+				.get("/service/single-user/xml");
 	}
 
 	@Test
 				.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("/ra/service/persons/xml");
+				.when().get("/service/persons/xml");
 	}
 
 	@Test
 		InputStream xsd = getClass().getResourceAsStream("/user.xsd");
 		assertNotNull(xsd);
 		expect().statusCode(200).body(matchesXsd(xsd)).when()
-				.get("/ra/service/single-user/xml");
+				.get("/service/single-user/xml");
 	}
 
 	@Test
 				lastName).expect().body("email", equalTo(email))
 				.body("firstName", equalTo(firstName))
 				.body("lastName", equalTo(lastName)).when()
-				.get("/ra/service/user/create");
+				.get("/service/user/create");
 	}
 
 	@Test
 	public void testStatusNotFound() {
-		expect().statusCode(404).when().get("/ra/service/status/notfound");
+		expect().statusCode(404).when().get("/service/status/notfound");
 	}
 
 	@Test
 	public void testAuthenticationWorking() {
 		// we're not authenticated, service returns "401 Unauthorized"
-		expect().statusCode(401).when().get("/ra/service/secure/person");
+		expect().statusCode(401).when().get("/service/secure/person");
 
 		// with authentication it is working
-		expect().statusCode(200).body(equalTo("Ok")).when().with()
-				.authentication().basic("admin", "admin")
-				.get("/ra/service/secure/person");
+		expect().statusCode(200).when().with().authentication()
+				.basic("admin", "admin").get("/service/secure/person");
 	}
 
 	@Test
 	public void testSetRequestHeaders() {
 		expect().body(equalTo("TEST")).when().with().header("myparam", "TEST")
-				.get("/ra/service/header/print");
+				.get("/service/header/print");
 		expect().body(equalTo("foo")).when().with().header("myparam", "foo")
-				.get("/ra/service/header/print");
+				.get("/service/header/print");
 	}
 
 	@Test
 	public void testReturnedHeaders() {
 		expect().headers("customHeader1", "foo", "anotherHeader", "bar").when()
-				.get("/ra/service/header/multiple");
-	}
-
-	@Ignore("tbd .. @Consumes is set but status 200 returned")
-	@Test
-	public void testRestrictToSingleContentType() {
-		expect().contentType(ContentType.XML).statusCode(415).when().with()
-				.contentType(ContentType.JSON)
-				.get("/ra/service/contentype/accept");
+				.get("/service/header/multiple");
 	}
 
 	@Test
 	public void testAccessSecuredByCookie() {
 		expect().statusCode(403).when()
-				.get("/ra/service/access/cookie-token-secured");
+				.get("/service/access/cookie-token-secured");
 		given().cookie("authtoken", "abcdef").expect().statusCode(200).when()
-				.get("/ra/service/access/cookie-token-secured");
+				.get("/service/access/cookie-token-secured");
 	}
 
 	@Test
 	public void testModifyCookie() {
 		expect().cookie("userName", equalTo("Ted")).when().with()
-				.param("name", "Ted").get("/ra/service/cookie/modify");
+				.param("name", "Ted").get("/service/cookie/modify");
 		expect().cookie("userName", equalTo("Bill")).when().with()
-				.param("name", "Bill").get("/ra/service/cookie/modify");
+				.param("name", "Bill").get("/service/cookie/modify");
 	}
 
 	@Test
 		assertTrue(file.canRead());
 		given().multiPart(file).expect()
 				.body(equalTo("This is an uploaded test file.")).when()
-				.post("/ra/service/file/upload");
+				.post("/service/file/upload");
 	}
 
 	@Test
 	public void testRegisterParserForUnknownContentType() {
 		RestAssured.registerParser("text/json", Parser.JSON);
-		expect().body("test", equalTo(true)).when()
-				.get("/ra/service/detail/json");
+		expect().body("test", equalTo(true)).when().get("/service/detail/json");
+	}
+
+	@Test
+	public void testSpecReuse() {
+		ResponseSpecBuilder builder = new ResponseSpecBuilder();
+		builder.expectStatusCode(200);
+		builder.expectBody("email", equalTo("test@hascode.com"));
+		builder.expectBody("firstName", equalTo("Tim"));
+		builder.expectBody("lastName", equalTo("Testerman"));
+		builder.expectBody("id", equalTo("1"));
+		ResponseSpecification responseSpec = builder.build();
+
+		// now we're able to use this specification for this test
+		expect().spec(responseSpec).when().get("/service/single-user");
+
+		// now re-use for another test that returns similar data .. you may
+		// extend the specification with further tests as you wish
+		final String email = "test@hascode.com";
+		final String firstName = "Tim";
+		final String lastName = "Testerman";
+
+		expect().spec(responseSpec)
+				.when()
+				.with()
+				.parameters("email", email, "firstName", firstName, "lastName",
+						lastName).get("/service/user/create");
 	}
 }