[jboss-cvs] JBossAS SVN: r108788 - branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Oct 22 07:57:21 EDT 2010


Author: mlinhard
Date: 2010-10-22 07:57:21 -0400 (Fri, 22 Oct 2010)
New Revision: 108788

Modified:
   branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test/RESTClientTestCase.java
Log:
added EDG REST client tests

Modified: branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test/RESTClientTestCase.java
===================================================================
--- branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test/RESTClientTestCase.java	2010-10-22 11:51:19 UTC (rev 108787)
+++ branches/JBPAPP_5_1_datagrid/testsuite/src/main/org/jboss/test/cluster/datagrid/test/RESTClientTestCase.java	2010-10-22 11:57:21 UTC (rev 108788)
@@ -21,13 +21,33 @@
  */
 package org.jboss.test.cluster.datagrid.test;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.DeleteMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
 import org.jboss.test.JBossClusteredTestCase;
 
 /**
- * Tests for the REST client..
+ * Tests for the REST client.
  * 
  * @author <a href="mailto:mlinhard at redhat.com">Michal Linhard</a>
- * @version $Revision: $
+ * @version $Revision$
  */
 public class RESTClientTestCase extends JBossClusteredTestCase {
 
@@ -35,7 +55,416 @@
 		super(name);
 	}
 
-	public void testRESTClient() throws Exception {
-		getLog().debug("testRESTClient");
+	private HttpClient client;
+	private static final String INFINISPAN_REST_PREFIX = "http://localhost:8080/infinispan-server-rest/rest";
+	private static final String FULL_PATH = INFINISPAN_REST_PREFIX + "/___defaultcache";
+	private static final String DATE_PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
+
+	private static class TestSerializable implements Serializable {
+		private String content;
+
+		public TestSerializable(String content) {
+			super();
+			this.content = content;
+		}
+
+		public String getContent() {
+			return content;
+		}
 	}
+
+	@Override
+	protected void setUp() throws Exception {
+		super.setUp();
+		client = new HttpClient();
+	}
+	
+	@Override
+	protected void tearDown() throws Exception {
+		super.tearDown();
+		delete(FULL_PATH + "/testBasicOperation");
+		delete(FULL_PATH + "/testGet");
+		delete(FULL_PATH + "/testHead");
+		delete(FULL_PATH + "/testPostDuplicate");
+		delete(FULL_PATH + "/testPutDataWithTimeToLive");
+		delete(FULL_PATH + "/testRemoveEntry");
+		delete(FULL_PATH + "/testWipeCacheBucket");
+		delete(FULL_PATH + "/testWipeCacheBucket2");
+		delete(FULL_PATH + "/testAsyncAddRemove");
+		delete(FULL_PATH + "/testPutUnknownClass");
+		delete(FULL_PATH + "/testETagChanges");
+		delete(FULL_PATH + "/testIfModifiedSince");
+		delete(FULL_PATH + "/testIfUnmodifiedSince");
+		delete(FULL_PATH + "/testIfNoneMatch");
+		delete(FULL_PATH + "/testIfMatch");
+	}
+
+	public void testCacheIsCleanForTesting() throws Exception {
+		head(FULL_PATH + "/testBasicOperation", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testGet", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testHead", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testPostDuplicate", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testPutDataWithTimeToLive", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testRemoveEntry", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testWipeCacheBucket", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testWipeCacheBucket2", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testAsyncAddRemove", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testPutUnknownClass", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testETagChanges", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testIfModifiedSince", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testIfUnmodifiedSince", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testIfNoneMatch", HttpServletResponse.SC_NOT_FOUND);
+		head(FULL_PATH + "/testIfMatch", HttpServletResponse.SC_NOT_FOUND);
+	}
+	
+	public void testBasicOperation() throws Exception {
+		String fullPathKey = FULL_PATH + "/testBasicOperation";
+		String initialXML = "<hey>ho</hey>";
+		PutMethod insert = put(fullPathKey, initialXML, "application/octet-stream");
+		assertEquals("", insert.getResponseBodyAsString().trim());
+
+		GetMethod get = get(fullPathKey);
+		assertEquals(initialXML, get.getResponseBodyAsString());
+		assertEquals("application/octet-stream", get.getResponseHeader("Content-Type").getValue());
+
+		DeleteMethod remove = new DeleteMethod(fullPathKey);
+		client.executeMethod(remove);
+		client.executeMethod(get);
+
+		assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode());
+
+		client.executeMethod(insert);
+		client.executeMethod(get);
+		assertEquals(initialXML, get.getResponseBodyAsString());
+
+		DeleteMethod removeAll = new DeleteMethod(FULL_PATH);
+		client.executeMethod(removeAll);
+
+		client.executeMethod(get);
+		assertEquals(HttpServletResponse.SC_NOT_FOUND, get.getStatusCode());
+
+		ByteArrayOutputStream bout = new ByteArrayOutputStream();
+		ObjectOutputStream oo = new ObjectOutputStream(bout);
+		oo.writeObject(new TestSerializable("CONTENT"));
+		oo.flush();
+
+		byte[] byteData = bout.toByteArray();
+		put(fullPathKey, byteData, "application/octet-stream");
+
+		byte[] bytesBack = get(fullPathKey).getResponseBody();
+		assertEquals(byteData.length, bytesBack.length);
+
+		ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(
+				bytesBack));
+		TestSerializable ts = (TestSerializable) oin.readObject();
+		assertEquals("CONTENT", ts.getContent());
+	}
+
+	public void testEmptyGet() throws Exception {
+		get(FULL_PATH + "/nodata", HttpServletResponse.SC_NOT_FOUND);
+	}
+
+	public void testGet() throws Exception {
+		String fullPathKey = FULL_PATH + "/testGet";
+		post(fullPathKey, "data", "application/text");
+		GetMethod get = get(fullPathKey);
+		assertNotNull(get.getResponseHeader("ETag").getValue());
+		assertNotNull(get.getResponseHeader("Last-Modified").getValue());
+		assertEquals("application/text", get.getResponseHeader("Content-Type").getValue());
+		assertEquals("data", get.getResponseBodyAsString());
+	}
+
+	public void testHead() throws Exception {
+		String fullPathKey = FULL_PATH + "/testHead";
+		post(fullPathKey, "data", "application/text");
+		HeadMethod head = head(fullPathKey);
+		assertNotNull(head.getResponseHeader("ETag").getValue());
+		assertNotNull(head.getResponseHeader("Last-Modified").getValue());
+		assertEquals("application/text", head.getResponseHeader("Content-Type").getValue());
+		assertNull(head.getResponseBodyAsString());
+	}
+
+	public void testPostDuplicate() throws Exception {
+		String fullPathKey = FULL_PATH + "/testPostDuplicate";
+		
+		post(fullPathKey, "data", "application/text");
+		// second post, returns 409
+		post(fullPathKey, "data", "application/text", HttpServletResponse.SC_CONFLICT);
+		// Should be all ok as its a put
+		put(fullPathKey, "data", "application/text");
+	}
+
+	public void testPutDataWithTimeToLive() throws Exception {
+		String fullPathKey = FULL_PATH + "/testPutDataWithTimeToLive";
+		
+		post(fullPathKey, "data", "application/text", HttpServletResponse.SC_OK, 
+			// headers
+			"Content-Type", "application/text",	
+			"timeToLiveSeconds", "2",	
+			"maxIdleTimeSeconds", "3"	
+		);
+		
+		assertEquals("data", get(fullPathKey).getResponseBodyAsString());
+		Thread.sleep(3000);
+		// should be evicted
+		get(fullPathKey, HttpServletResponse.SC_NOT_FOUND);
+	}
+
+	public void testRemoveEntry() throws Exception {
+		String fullPathKey = FULL_PATH + "/testRemoveEntry";
+		post(fullPathKey, "data", "application/text");
+		head(fullPathKey);
+		client.executeMethod(new DeleteMethod(fullPathKey));
+		head(fullPathKey, HttpServletResponse.SC_NOT_FOUND);
+	}
+
+	public void testWipeCacheBucket() throws Exception {
+		String fullPathKey = FULL_PATH + "/testWipeCacheBucket";
+		post(fullPathKey, "data", "application/text");
+		post(fullPathKey + "2", "data", "application/text");
+		head(fullPathKey);
+		head(fullPathKey + "2");
+		client.executeMethod(new DeleteMethod(FULL_PATH));
+		head(fullPathKey, HttpServletResponse.SC_NOT_FOUND);
+		head(fullPathKey + "2", HttpServletResponse.SC_NOT_FOUND);
+	}
+
+	public void testAsyncAddRemove() throws Exception {
+		String fullPathKey = FULL_PATH + "/testAsyncAddRemove";
+		post(fullPathKey, "data", "application/text", HttpServletResponse.SC_OK, 
+				// headers
+				"performAsync", "true" );
+
+		Thread.sleep(50);
+		head(fullPathKey);
+
+		DeleteMethod del = new DeleteMethod(fullPathKey);
+		del.setRequestHeader("performAsync", "true");
+		client.executeMethod(del);
+		Thread.sleep(50);
+		head(fullPathKey, HttpServletResponse.SC_NOT_FOUND);
+	}
+	
+	// https://jira.jboss.org/browse/JBPAPP-5256
+	public void testPutUnknownClass() throws Exception {
+		String fullPathKey = FULL_PATH + "/testPutUnknownClass";
+
+		ByteArrayOutputStream bout = new ByteArrayOutputStream();
+		ObjectOutputStream oo = new ObjectOutputStream(bout);
+		oo.writeObject(new TestSerializable("CONTENT"));
+		oo.flush();
+		byte[] byteData = bout.toByteArray();
+		put(fullPathKey, byteData, "application/x-java-serialized-object");
+		byte[] bytesBack = get(fullPathKey).getResponseBody();
+		assertEquals(byteData.length, bytesBack.length);
+		ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(
+				bytesBack));
+		TestSerializable ts = (TestSerializable) oin.readObject();
+		assertEquals("CONTENT", ts.getContent());
+	}
+	
+	public void testETagChanges() throws Exception {
+		String fullPathKey = FULL_PATH + "/testETagChanges";
+		put(fullPathKey, "data", "application/text");
+		String eTagFirst = get(fullPathKey).getResponseHeader("ETag").getValue();
+		// second get should get the same ETag
+		assertEquals(eTagFirst, get(fullPathKey).getResponseHeader("ETag").getValue());
+		// do second PUT
+		put(fullPathKey, "data", "application/text");
+		// get ETag again
+		assertNotSame(eTagFirst, get(fullPathKey).getResponseHeader("ETag").getValue());
+	}
+	
+	public void testIfModifiedSince() throws Exception {
+		String fullPathKey = FULL_PATH + "/testIfModifiedSince";
+		put(fullPathKey, "data", "application/text");
+		String dateLast = get(fullPathKey).getResponseHeader("Last-Modified").getValue();
+		String dateMinus = addDay(dateLast, -1);
+		String datePlus = addDay(dateLast, 1);
+		
+		GetMethod get = get(fullPathKey, HttpServletResponse.SC_OK, 
+			// resource has been modified since
+			"If-Modified-Since", dateMinus
+		);
+		assertNotNull(get.getResponseBodyAsString());
+		
+		get = get(fullPathKey, HttpServletResponse.SC_OK, 
+			// exact same date as stored one
+			"If-Modified-Since", dateLast
+		);
+		assertNotNull(get.getResponseBodyAsString());
+		
+		get = get(fullPathKey, HttpServletResponse.SC_NOT_MODIFIED, 
+			// resource hasn't been modified since
+			"If-Modified-Since", datePlus
+		);
+		assertNull(get.getResponseBodyAsString());
+	}
+
+	
+	public void testIfUnmodifiedSince() throws Exception {
+		String fullPathKey = FULL_PATH + "/testIfUnmodifiedSince";
+		put(fullPathKey, "data", "application/text");
+		
+		String dateLast = get(fullPathKey).getResponseHeader("Last-Modified").getValue();
+		String dateMinus = addDay(dateLast, -1);
+		String datePlus = addDay(dateLast, 1);
+		
+		// JBPAPP-5262
+		GetMethod get = get(fullPathKey, HttpServletResponse.SC_OK, "If-Unmodified-Since", dateLast);
+		assertNotNull(get.getResponseBodyAsString());
+
+		get = get(fullPathKey, HttpServletResponse.SC_OK, "If-Unmodified-Since", datePlus);
+		assertNotNull(get.getResponseBodyAsString());
+		
+		get(fullPathKey, HttpServletResponse.SC_PRECONDITION_FAILED, "If-Unmodified-Since", dateMinus);
+	}
+
+	public void testIfNoneMatch() throws Exception {
+		String fullPathKey = FULL_PATH + "/testIfNoneMatch";
+		put(fullPathKey, "data", "application/text");
+		String eTag = get(fullPathKey).getResponseHeader("ETag").getValue();
+		
+		GetMethod get = get(fullPathKey, HttpServletResponse.SC_NOT_MODIFIED, 
+				"If-None-Match", eTag);
+		assertNull(get.getResponseBodyAsString());
+
+		get = get(fullPathKey, HttpServletResponse.SC_OK, 
+				"If-None-Match", eTag+"garbage");
+		assertNotNull(get.getResponseBodyAsString());
+	}
+	
+	public void testIfMatch() throws Exception {
+		String fullPathKey = FULL_PATH + "/testIfMatch";
+		put(fullPathKey, "data", "application/text");
+		GetMethod get = get(fullPathKey);
+		
+		String eTag = get.getResponseHeader("ETag").getValue();
+		
+		// test GET with If-Match behaviour
+		get.setRequestHeader("If-Match", eTag);
+		assertEquals(HttpServletResponse.SC_OK, client.executeMethod(get));
+		assertNotNull(get.getResponseBodyAsString());
+
+		get.setRequestHeader("If-Match", eTag+"garbage");
+		assertEquals(HttpServletResponse.SC_PRECONDITION_FAILED, client.executeMethod(get));
+		
+		// test HEAD with If-Match behaviour
+		HeadMethod head = new HeadMethod(fullPathKey);
+		head.setRequestHeader("If-Match", eTag);
+		assertEquals(HttpServletResponse.SC_OK, client.executeMethod(head));
+		
+		head.setRequestHeader("If-Match", eTag+"garbage");
+		assertEquals(HttpServletResponse.SC_PRECONDITION_FAILED, client.executeMethod(head));
+	}
+	
+	// https://jira.jboss.org/browse/JBPAPP-5266
+	public void testNonExistentCache() throws Exception {
+		head(INFINISPAN_REST_PREFIX + "/nonexistentcache/nodata", HttpServletResponse.SC_NOT_FOUND);
+		get(INFINISPAN_REST_PREFIX + "/nonexistentcache/nodata", HttpServletResponse.SC_NOT_FOUND);
+	}
+	
+	//----------------------------------------- helper methods -----------------------------------------
+	
+	private String addDay(String aDate, int days) throws Exception {
+		SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN_RFC1123, Locale.US);
+		Date date = format.parse(aDate);
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(date);
+		cal.add(Calendar.DATE, days);
+		return format.format(cal.getTime());
+	}	
+
+	protected HeadMethod head(String uri) throws Exception {
+		return head(uri, HttpServletResponse.SC_OK);
+	}
+	
+	protected HeadMethod head(String uri, int expectedCode) throws Exception {
+		return head(uri, expectedCode, new String[0][0]);
+	}
+	
+	protected HeadMethod head(String uri, int expectedCode, String[][] headers) throws Exception {
+		HeadMethod head = new HeadMethod(uri);
+		for (String[] eachHeader : headers) {
+			head.setRequestHeader(eachHeader[0], eachHeader[1]);
+		}
+		assertEquals(expectedCode, client.executeMethod(head));
+		return head;
+	}
+	
+	protected GetMethod get(String uri) throws Exception {
+		return get(uri, HttpServletResponse.SC_OK);
+	}
+	
+	protected GetMethod get(String uri, int expectedCode) throws Exception {
+		return get(uri, expectedCode, new Object[0]);
+	}
+	
+	protected GetMethod get(String uri, int expectedCode, Object... headers) throws Exception {
+		GetMethod get = new GetMethod(uri);
+		if (headers.length % 2 != 0) throw new IllegalArgumentException("bad headers argument");
+		for (int i = 0; i < headers.length; i += 2) {
+			get.setRequestHeader((String) headers[i], (String) headers[i+1]);
+		}
+		assertEquals(expectedCode, client.executeMethod(get));
+		return get;
+	}
+	
+	protected PutMethod put(String uri, Object data, String contentType) throws Exception {
+		return put(uri, data, contentType, HttpServletResponse.SC_OK);
+	}
+	
+	protected PutMethod put(String uri, Object data, String contentType, int expectedCode) throws Exception {
+		return put(uri, data, contentType, expectedCode, new Object[0]);
+	}
+	
+	protected PutMethod put(String uri, Object data, String contentType, int expectedCode, Object... headers) throws Exception {
+		PutMethod put = new PutMethod(uri);
+		if (data instanceof String) {
+			put.setRequestEntity(new StringRequestEntity((String) data, contentType, "UTF-8"));
+		} else if (data instanceof byte[]) {
+			put.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream((byte[]) data)));
+		} else {
+			throw new IllegalArgumentException("Unknown data type for PUT method");
+		}
+		put.setRequestHeader("Content-Type", contentType);
+		if (headers.length % 2 != 0) throw new IllegalArgumentException("bad headers argument");
+		for (int i = 0; i < headers.length; i += 2) {
+			put.setRequestHeader((String) headers[i], (String) headers[i+1]);
+		}
+		assertEquals(expectedCode, client.executeMethod(put));
+		return put;
+	}
+	
+	protected PostMethod post(String uri, Object data, String contentType) throws Exception {
+		return post(uri, data, contentType, HttpServletResponse.SC_OK);
+	}
+	
+	protected PostMethod post(String uri, Object data, String contentType, int expectedCode) throws Exception {
+		return post(uri, data, contentType, expectedCode, new Object[0]);
+	}
+	
+	protected PostMethod post(String uri, Object data, String contentType, int expectedCode, Object... headers) throws Exception {
+		PostMethod post = new PostMethod(uri);
+		if (data instanceof String) {
+			post.setRequestEntity(new StringRequestEntity((String) data, contentType, "UTF-8"));
+		} else if (data instanceof byte[]) {
+			post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream((byte[]) data)));
+		} else {
+			throw new IllegalArgumentException("Unknown data type for POST method");
+		}
+		post.setRequestHeader("Content-Type", contentType);
+		if (headers.length % 2 != 0) throw new IllegalArgumentException("bad headers argument");
+		for (int i = 0; i < headers.length; i += 2) {
+			post.setRequestHeader((String) headers[i], (String) headers[i+1]);
+		}
+		assertEquals(expectedCode, client.executeMethod(post));
+		return post;
+	}
+	
+	protected DeleteMethod delete(String uri) throws Exception {
+		DeleteMethod delete = new DeleteMethod(uri);
+		client.executeMethod(delete);
+		return delete;
+	}
 }



More information about the jboss-cvs-commits mailing list