Commits

Micha Kops committed 7d074a5

Tomcat security configuration added

Comments (0)

Files changed (6)

 	</repositories>
 	<build>
 		<finalName>rest-assured-example</finalName>
+		<plugins>
+			<plugin>
+				<groupId>org.codehaus.mojo</groupId>
+				<artifactId>tomcat-maven-plugin</artifactId>
+				<version>1.1</version>
+				<configuration>
+					<mode>both</mode>
+					<path>/ra</path>
+				</configuration>
+			</plugin>
+		</plugins>
 	</build>
 </project>

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

 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
 
-@Path("raexample")
+@Path("/service")
 public class RestAssuredSampleService {
 	@GET
 	@Produces(MediaType.APPLICATION_JSON)
 		users.add(person3);
 		return users;
 	}
+
+	@GET
+	@Produces(MediaType.APPLICATION_JSON)
+	@Path("/user/create")
+	public User createUser(@QueryParam("email") final String email,
+			@QueryParam("firstName") final String firstName,
+			@QueryParam("lastName") final String lastName) {
+		User user = new User();
+		user.setEmail(email);
+		user.setFirstName(firstName);
+		user.setLastName(lastName);
+		user.setId(1L);
+
+		return user;
+	}
+
+	@GET
+	@Produces(MediaType.TEXT_PLAIN)
+	@Path("/status/notfound")
+	public Response statusNotFound() {
+		return Response.status(404).build();
+	}
+
+	@GET
+	@Produces(MediaType.APPLICATION_JSON)
+	@Path("/secure/person")
+	public Person secureGetPerson() {
+		Person person = new Person();
+		person.setEmail("test@hascode.com");
+		person.setFirstName("Tim");
+		person.setLastName("Testerman");
+		person.setId(1L);
+		return person;
+	}
+
 }

src/main/tomcatconf/tomcat-users.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<tomcat-users>
+	<user name="admin" password="admin" roles="admin" />
+</tomcat-users>

src/main/webapp/META-INF/context.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<Context>
+	<Realm className="org.apache.catalina.realm.MemoryRealm"/>
+</Context>

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

     <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>

src/test/java/it/RestAssuredSampleServiceIT.java

 
 import static com.jayway.restassured.RestAssured.expect;
 import static com.jayway.restassured.RestAssured.get;
+import static com.jayway.restassured.RestAssured.given;
 import static com.jayway.restassured.matcher.RestAssuredMatchers.matchesXsd;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasXPath;
 		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");
+						equalTo("1")).when().get("/ra/service/single-user");
 	}
 
 	/**
 	@Ignore
 	@Test
 	public void testGetSingleUserProgrammatic() {
-		Response res = get("/rest-assured-examples-webapp/raexample/single-user");
+		Response res = get("/ra/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("/rest-assured-examples-webapp/raexample/single-user/xml");
+				.get("/ra/service/single-user/xml");
 	}
 
 	@Ignore
 				.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");
+				.when().get("/ra/service/persons/xml");
 	}
 
+	@Ignore
 	@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");
+				.get("/ra/service/single-user/xml");
+	}
+
+	@Ignore
+	@Test
+	public void testCreateuser() {
+		final String email = "test@hascode.com";
+		final String firstName = "Tim";
+		final String lastName = "Tester";
+
+		given().parameters("email", email, "firstName", firstName, "lastName",
+				lastName).expect().body("email", equalTo(email))
+				.body("firstName", equalTo(firstName))
+				.body("lastName", equalTo(lastName)).when()
+				.get("/ra/service/user/create");
+	}
+
+	@Ignore
+	@Test
+	public void testStatusNotFound() {
+		expect().statusCode(404).when().get("/ra/service/status/notfound");
 	}
 }