incubator-wookie-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scot...@apache.org
Subject svn commit: r1154962 [1/2] - /incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/
Date Mon, 08 Aug 2011 13:42:57 GMT
Author: scottbw
Date: Mon Aug  8 13:42:56 2011
New Revision: 1154962

URL: http://svn.apache.org/viewvc?rev=1154962&view=rev
Log:
Improved the code comments in more test classes. Also got rid of trapping of exceptions in test cases - instead just let them be thrown so we can debug it in the console.

Modified:
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ParticipantsControllerTest.java
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/UpdatesControllerTest.java
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WhiteListControllerTest.java

Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java?rev=1154962&r1=1154961&r2=1154962&view=diff
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java (original)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/FolderLocalizationTest.java Mon Aug  8 13:42:56 2011
@@ -15,10 +15,12 @@ package org.apache.wookie.tests.function
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
 
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.cookie.CookiePolicy;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
@@ -27,197 +29,267 @@ import org.jdom.Document;
 import org.jdom.input.SAXBuilder;
 import org.junit.BeforeClass;
 import org.junit.Test;
+
 /**
  * Tests folder-based localization using the localetest widget
- * @author scott
- *
  */
 public class FolderLocalizationTest extends AbstractControllerTest {
-	
-	private static final String LOCALIZED_WIDGET = "http://www.getwookie.org/widgets/localetest";
-	private static String WIDGET_START_URL_ROOT;
-	
-	private static HttpClient client;
-	private static GetMethod get;
-	
-	@BeforeClass
-	public static void setup(){
-		// Set up the client and enable cookies
-        client = new HttpClient();
-        client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
-        get = new GetMethod();
-        
-        // Create a widget instance localized to Yorkshire English
-        try {
-	        PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=foldertest1&shareddatakey=foldertest1&locale=en-gb-yorks");
-	        client.executeMethod(post);
-	      
-	        WIDGET_START_URL_ROOT = getStartFile(post.getResponseBodyAsString());
-	        // We have to load the start file in order to start the session
-	        getResource(WIDGET_START_URL_ROOT);
-	        // take off the resource bit
-	        String path = WIDGET_START_URL_ROOT.substring(WIDGET_START_URL_ROOT.indexOf("locales"));
-	        WIDGET_START_URL_ROOT= StringUtils.remove(WIDGET_START_URL_ROOT, path);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    }			
-	}
-	
-	/**
-	 * Gets the start file url for an instance
-	 * @param response
-	 * @return
-	 */
-	private static String getStartFile(String response){
-		SAXBuilder builder = new SAXBuilder();
-		Reader in = new StringReader(response);
-		Document doc;
-		try {
-			doc = builder.build(in);
-		} catch (Exception e) {
-			return null;
-		} 		
-		return doc.getRootElement().getChild("url").getText();
-	}
-	
-	/**
-	 * Gets a resource and returns the final URL after redirection
-	 * @param url
-	 * @return
-	 */
-	private static String getResource(String url){
-		get = new GetMethod(url);
-		get.setFollowRedirects(true);
-		try {
-			client.executeMethod(get);
-			return TEST_SERVER_ORIGIN + get.getPath();
-		} catch (Exception e) {
-			e.printStackTrace();
-		} 
-		return null;
-	}
-	
-	@Test
-	// The root resource is overridden by the EN-GB-YORKS localized resource
-	public void getLocalizedResourceFromRootPath(){
-		String resource = "index.htm";
-		String url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+"locales/en-gb-yorks/"+resource, getResource(url));
-	}
-	
-	@Test
-	// The root resource is correct, so should not be redirected
-	public void getRootResourceFromRootResource(){
-		String resource = "Images/test.png";
-		String url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+resource, getResource(url));	
-	}
-	
-	@Test
-	// The FR resource is replaced by the Root resource as there is no EN resource
-	public void getRootResourceFromIncorrectLocalizedPath(){
-		String resource = "locales/fr/Images/test.png";
-		String url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+"Images/test.png", getResource(url));
-	}
-	
-	@Test
-	// The EN resource is replaced by the Root resource as there is no EN resource
-	public void getRootResourceFromLocalizedPath(){
-		String resource = "locales/en/Images/test.png";
-		String url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+"Images/test.png", getResource(url));
-	}
-	
-	@Test
-	// The FR resource is replaced by the EN-GB-YORKS resource
-	public void getLocalizedResourceFromIncorrectLocalizedPath(){
-		String resource = "locales/fr/index.htm";
-		String url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+"locales/en-gb-yorks/index.htm", getResource(url));
-	}
-	
-	@Test
-	// The EN-GB-YORKS resource is not available and is replaced by the EN resource
-	public void getLocalizedResourceGracefulDegradation(){
-		String resource = "locales/en-gb-yorks/test.txt";
-		String url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+"locales/en/test.txt", getResource(url));
-	}
-	
-	@Test
-	// Request the instance with different locales, and check that the correct
-	// resources are returned in each case (i.e. that the locale of the instance has changed)
-	public void updateLocalizedResources(){
-		
-        // Update the widget instance localized to French 
-        try {
-	        PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=foldertest1&shareddatakey=foldertest1&locale=fr");
-	        client.executeMethod(post);
-	      
-	        WIDGET_START_URL_ROOT = getStartFile(post.getResponseBodyAsString());
-	        // We have to load the start file in order to start the session
-	        getResource(WIDGET_START_URL_ROOT);
-	        // take off the resource bit
-	        String path = WIDGET_START_URL_ROOT.substring(WIDGET_START_URL_ROOT.indexOf("locales"));
-	        WIDGET_START_URL_ROOT= StringUtils.remove(WIDGET_START_URL_ROOT, path);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    }	
-		
-		String resource = "index.htm";
-		String url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+"locales/fr/index.htm", getResource(url));
-		
-        // Update the widget instance localized to English 
-        try {
-	        PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=foldertest1&shareddatakey=foldertest1&locale=en");
-	        client.executeMethod(post);
-	      
-	        WIDGET_START_URL_ROOT = getStartFile(post.getResponseBodyAsString());
-	        // We have to load the start file in order to start the session
-	        getResource(WIDGET_START_URL_ROOT);
-	        // take off the resource bit
-	        String path = WIDGET_START_URL_ROOT.substring(WIDGET_START_URL_ROOT.indexOf("locales"));
-	        WIDGET_START_URL_ROOT= StringUtils.remove(WIDGET_START_URL_ROOT, path);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    }	
-		
-		resource = "index.htm";
-		url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+"locales/en/index.htm", getResource(url));
-		
-	     // Update the widget instance unlocalized (expecting the default locale here to be "en")
-        try {
-	        PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=foldertest1&shareddatakey=foldertest1");
-	        client.executeMethod(post);
-	      
-	        WIDGET_START_URL_ROOT = getStartFile(post.getResponseBodyAsString());
-	        // We have to load the start file in order to start the session
-	        getResource(WIDGET_START_URL_ROOT);
-	        // take off the resource bit
-	        String path = WIDGET_START_URL_ROOT.substring(WIDGET_START_URL_ROOT.indexOf("locales"));
-	        WIDGET_START_URL_ROOT= StringUtils.remove(WIDGET_START_URL_ROOT, path);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    }	
-		
-		resource = "index.htm";
-		url = WIDGET_START_URL_ROOT + resource;
-		assertEquals(WIDGET_START_URL_ROOT+"locales/en/index.htm", getResource(url));
-	}
+
+  private static final String LOCALIZED_WIDGET = "http://www.getwookie.org/widgets/localetest";
+  private static String WIDGET_START_URL_ROOT;
+
+  private static HttpClient client;
+  private static GetMethod get;
+
+  /**
+   * Setup a widget instance to test with, and create a shared HTTP client with
+   * cookies enabled.
+   * 
+   * @throws HttpException
+   * @throws IOException
+   */
+  @BeforeClass
+  public static void setup() throws HttpException, IOException {
+    
+    //
+    // Set up the client and enable cookies
+    //
+    client = new HttpClient();
+    client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
+    get = new GetMethod();
+
+    //
+    // Create a widget instance localized to Yorkshire English
+    //
+    PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + LOCALIZED_WIDGET
+        + "&userid=foldertest1&shareddatakey=foldertest1&locale=en-gb-yorks");
+    client.executeMethod(post);
+
+    //
+    // Get the root URL
+    //
+    WIDGET_START_URL_ROOT = getStartFile(post.getResponseBodyAsString());
+
+    //
+    // We have to load the start file in order to start the session
+    //
+    getResource(WIDGET_START_URL_ROOT);
+
+    //
+    // take off the resource bit
+    //
+    WIDGET_START_URL_ROOT = getRootUrl(WIDGET_START_URL_ROOT);
+    
+    post.releaseConnection();
+  }
+
+  /**
+   * Gets the start file url for an instance
+   * 
+   * @param response
+   * @return
+   */
+  private static String getStartFile(String response) {
+    SAXBuilder builder = new SAXBuilder();
+    Reader in = new StringReader(response);
+    Document doc;
+    try {
+      doc = builder.build(in);
+    } catch (Exception e) {
+      return null;
+    }
+    return doc.getRootElement().getChild("url").getText();
+  }
+
+  /**
+   * Gets a resource and returns the final URL after redirection
+   * 
+   * @param url
+   * @return
+   */
+  private static String getResource(String url) {
+    get = new GetMethod(url);
+    get.setFollowRedirects(true);
+    try {
+      client.executeMethod(get);
+      return TEST_SERVER_ORIGIN + get.getPath();
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+    return null;
+  }
+
+  /**
+   * Test that the root resource is overridden by the EN-GB-YORKS localized
+   * resource
+   */
+  @Test
+  public void getLocalizedResourceFromRootPath() {
+    String resource = "index.htm";
+    String url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + "locales/en-gb-yorks/" + resource,
+        getResource(url));
+  }
+
+  /**
+   * The root resource is correct, so should not be redirected
+   */
+  @Test
+  public void getRootResourceFromRootResource() {
+    String resource = "Images/test.png";
+    String url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + resource, getResource(url));
+  }
+
+  /**
+   * The FR resource is replaced by the Root resource as there is no EN resource
+   */
+  @Test
+  public void getRootResourceFromIncorrectLocalizedPath() {
+    String resource = "locales/fr/Images/test.png";
+    String url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + "Images/test.png", getResource(url));
+  }
+
+  /**
+   * The EN resource is replaced by the Root resource as there is no EN resource
+   */
+  @Test
+  public void getRootResourceFromLocalizedPath() {
+    String resource = "locales/en/Images/test.png";
+    String url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + "Images/test.png", getResource(url));
+  }
+
+  /**
+   * The FR resource is replaced by the EN-GB-YORKS resource
+   */
+  @Test
+  public void getLocalizedResourceFromIncorrectLocalizedPath() {
+    String resource = "locales/fr/index.htm";
+    String url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + "locales/en-gb-yorks/index.htm",
+        getResource(url));
+  }
+
+  /**
+   * The EN-GB-YORKS resource is not available and is replaced by the EN
+   * resource
+   */
+  @Test
+  public void getLocalizedResourceGracefulDegradation() {
+    String resource = "locales/en-gb-yorks/test.txt";
+    String url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + "locales/en/test.txt",
+        getResource(url));
+  }
+
+  /**
+   * Request the instance with different locales, and check that the correct
+   * resources are returned in each case (i.e. that the locale of the instance
+   * has changed)
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void updateLocalizedResources() throws HttpException, IOException {
+    //
+    // Update the widget instance localized to French
+    //
+    PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + LOCALIZED_WIDGET
+        + "&userid=foldertest1&shareddatakey=foldertest1&locale=fr");
+    client.executeMethod(post);
+
+    WIDGET_START_URL_ROOT = getStartFile(post.getResponseBodyAsString());
+    
+    //
+    // We have to load the start file in order to start the session
+    //
+    getResource(WIDGET_START_URL_ROOT);
+
+    //
+    // take off the resource bit
+    //
+    WIDGET_START_URL_ROOT = getRootUrl(WIDGET_START_URL_ROOT);
+    
+    post.releaseConnection();
+
+    String resource = "index.htm";
+    String url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + "locales/fr/index.htm",getResource(url));
+
+    //
+    // Update the widget instance localized to English
+    //
+
+    post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + LOCALIZED_WIDGET
+        + "&userid=foldertest1&shareddatakey=foldertest1&locale=en");
+    client.executeMethod(post);
+
+    WIDGET_START_URL_ROOT = getStartFile(post.getResponseBodyAsString());
+    
+    //
+    // We have to load the start file in order to start the session
+    //
+    getResource(WIDGET_START_URL_ROOT);
+    
+    //
+    // take off the resource bit
+    //
+    WIDGET_START_URL_ROOT = getRootUrl(WIDGET_START_URL_ROOT);
+    
+    post.releaseConnection();
+
+    resource = "index.htm";
+    url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + "locales/en/index.htm",
+        getResource(url));
+
+    //
+    // Update the widget instance unlocalized (expecting the default locale here
+    // to be "en")
+    //
+    post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + LOCALIZED_WIDGET + "&userid=foldertest1&shareddatakey=foldertest1");
+    client.executeMethod(post);
+
+    WIDGET_START_URL_ROOT = getStartFile(post.getResponseBodyAsString());
+    
+    //
+    // We have to load the start file in order to start the session
+    //
+    getResource(WIDGET_START_URL_ROOT);
+    
+    //
+    // take off the resource bit
+    //
+    WIDGET_START_URL_ROOT = getRootUrl(WIDGET_START_URL_ROOT);
+   
+    post.releaseConnection();
+
+    resource = "index.htm";
+    url = WIDGET_START_URL_ROOT + resource;
+    assertEquals(WIDGET_START_URL_ROOT + "locales/en/index.htm",
+        getResource(url));
+  }
+  
+  /**
+   * Removes the resource path and query components of a given URL
+   * @param url
+   * @return the "root" url
+   */
+  private static String getRootUrl(String url){
+    String path = url.substring(url.indexOf("locales"));
+    url = StringUtils.remove(url, path);
+    return url;
+  }
 
 }

Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ParticipantsControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ParticipantsControllerTest.java?rev=1154962&r1=1154961&r2=1154962&view=diff
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ParticipantsControllerTest.java (original)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ParticipantsControllerTest.java Mon Aug  8 13:42:56 2011
@@ -16,243 +16,283 @@ package org.apache.wookie.tests.function
 
 import static org.junit.Assert.*;
 
+import java.io.IOException;
+
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.DeleteMethod;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-
 /**
- * Controller tests. Run this after the Ant script completes (give it a sec for Tomcat to restart)
- * @author scott
- *
+ * Test cases for the Participants REST API
  */
-public class ParticipantsControllerTest extends AbstractControllerTest {	
-	
-	private static String instance_id_key;
-	
-	@BeforeClass
-	public static void setUp() throws Exception {
-
-		// Create an instance
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest");
-	        client.executeMethod(post);
-	        String response = post.getResponseBodyAsString();
-	        instance_id_key = response.substring(response.indexOf("<identifier>")+12, response.indexOf("</identifier>"));
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    }
-	}
-
-	@Test
-	public void addParticipant(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest&participant_id=1&participant_display_name=bob&participant_thumbnail_url=http://www.test.org");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(201,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }
-	    // Now lets GET it to make sure it was added OK
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(200,code);
-	        String response = get.getResponseBodyAsString();
-	        assertTrue(response.contains("<participant id=\"1\" display_name=\"bob\" thumbnail_url=\"http://www.test.org\" />"));
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("get failed");
-	    }
-	}
-	
-	@Test
-	public void getParticipant_usingIdKey(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_VALID+"&id_key="+instance_id_key);
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(200,code);
-	        String response = get.getResponseBodyAsString();
-	        assertTrue(response.contains("<participant id=\"1\" display_name=\"bob\" thumbnail_url=\"http://www.test.org\" />"));
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("get failed");
-	    }
-	}
-	
-	@Test
-	public void addParticipant_AlreadyExists(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest&participant_id=1&participant_display_name=bob&participants_thumbnail_url=http://www.test.org");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(200,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }
-	}
-	
-	@Test
-	public void addParticipant_InvalidAPIkey(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_INVALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(401,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }		
-	}
-	
-	@Test
-	public void addParticipant_InvalidParticipant(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest&participant_id=&participant_display_name=bob&participants_thumbnail_url=http://www.test.org");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(400,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }
-	}
-	
-	@Test
-	public void addParticipant_InvalidWidget(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_INVALID+"&userid=test&shareddatakey=participantstest&participant_id=1&participant_display_name=bob&participants_thumbnail_url=http://www.test.org");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(400,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }
-	}
-	
-	@Test
-	public void deleteParticipant(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        DeleteMethod post = new DeleteMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest&participant_id=1");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(200,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("delete failed");
-	    }
-	    // Now lets GET it to make sure it was deleted
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(200,code);
-	        String response = get.getResponseBodyAsString();
-	        assertFalse(response.contains("<participant id=\"1\" display_name=\"bob\" thumbnail_url=\"http://www.test.org\" />"));
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("get failed");
-	    }
-	}
-	
-	@Test
-	public void deleteParticipant_InvalidAPIKey(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        DeleteMethod post = new DeleteMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_INVALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(401,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }	
-	}
-	
-	@Test
-	public void deleteParticipant_InvalidParticipant(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        DeleteMethod post = new DeleteMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstest&participant_id=99");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(404,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }	
-	}
-	
-	@Test
-	public void deleteParticipant_InvalidInstance(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        DeleteMethod post = new DeleteMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=participantstestinvalidkey&participant_id=1");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(400,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }	
-	}
+public class ParticipantsControllerTest extends AbstractControllerTest {
+
+  private static String instance_id_key;
+
+  /**
+   * Create a widget instance to test
+   * 
+   * @throws Exception
+   */
+  @BeforeClass
+  public static void setUp() throws Exception {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID + "&userid=test&shareddatakey=participantstest");
+    client.executeMethod(post);
+    String response = post.getResponseBodyAsString();
+    instance_id_key = response.substring(response.indexOf("<identifier>") + 12,
+        response.indexOf("</identifier>"));
+    post.releaseConnection();
+  }
+
+  /**
+   * Tests adding and then getting a participant
+   * 
+   * @throws HttpException
+   * @throws IOException
+   */
+  @Test
+  public void addParticipant() throws HttpException, IOException {
+
+    //
+    // Create a new participant
+    //
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=participantstest&participant_id=1&participant_display_name=bob&participant_thumbnail_url=http://www.test.org");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(201, code);
+    post.releaseConnection();
+
+    //
+    // Now lets GET it to make sure it was added OK
+    //
+    client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID + "&userid=test&shareddatakey=participantstest");
+    client.executeMethod(get);
+    code = get.getStatusCode();
+    assertEquals(200, code);
+    String response = get.getResponseBodyAsString();
+    assertTrue(response
+        .contains("<participant id=\"1\" display_name=\"bob\" thumbnail_url=\"http://www.test.org\" />"));
+    get.releaseConnection();
+
+  }
+
+  /**
+   * Tests getting a participant using the widget instance ID key
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void getParticipant_usingIdKey() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_VALID + "&id_key="
+        + instance_id_key);
+    client.executeMethod(get);
+    int code = get.getStatusCode();
+    assertEquals(200, code);
+    String response = get.getResponseBodyAsString();
+    assertTrue(response
+        .contains("<participant id=\"1\" display_name=\"bob\" thumbnail_url=\"http://www.test.org\" />"));
+    get.releaseConnection();
+  }
+
+  /**
+   * Tests trying to add a duplicate participant
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void addParticipant_AlreadyExists() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=participantstest&participant_id=1&participant_display_name=bob&participants_thumbnail_url=http://www.test.org");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(200, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Try to add a participant with an invalid API key
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void addParticipant_InvalidAPIkey() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_INVALID + "&widgetid="
+        + WIDGET_ID_VALID + "&userid=test&shareddatakey=participantstest");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(401, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Try to add a participant with no participant ID
+   * 
+   * @throws HttpException
+   * @throws IOException
+   */
+  @Test
+  public void addParticipant_InvalidParticipant() throws HttpException,
+      IOException {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=participantstest&participant_id=&participant_display_name=bob&participants_thumbnail_url=http://www.test.org");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(400, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Try to add a participant using a non-existing widget
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void addParticipant_InvalidWidget() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_INVALID
+        + "&userid=test&shareddatakey=participantstest&participant_id=1&participant_display_name=bob&participants_thumbnail_url=http://www.test.org");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(400, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Delete a participant and check it was deleted
+   * 
+   * @throws HttpException
+   * @throws IOException
+   */
+  @Test
+  public void deleteParticipant() throws HttpException, IOException {
+    //
+    // Delete the participant
+    //
+    HttpClient client = new HttpClient();
+    DeleteMethod post = new DeleteMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=participantstest&participant_id=1");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(200, code);
+    post.releaseConnection();
+
+    //
+    // Now lets GET it to make sure it was deleted
+    //
+
+    client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID + "&userid=test&shareddatakey=participantstest");
+    client.executeMethod(get);
+    code = get.getStatusCode();
+    assertEquals(200, code);
+    String response = get.getResponseBodyAsString();
+    assertFalse(response
+        .contains("<participant id=\"1\" display_name=\"bob\" thumbnail_url=\"http://www.test.org\" />"));
+    get.releaseConnection();
+
+  }
+
+  /**
+   * Try to delete a participant with an invalid API key
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void deleteParticipant_InvalidAPIKey() throws HttpException,
+      IOException {
+    HttpClient client = new HttpClient();
+    DeleteMethod post = new DeleteMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_INVALID + "&widgetid="
+        + WIDGET_ID_VALID + "&userid=test&shareddatakey=participantstest");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(401, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Try to delete a non-existing participant
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void deleteParticipant_InvalidParticipant() throws HttpException,
+      IOException {
+    HttpClient client = new HttpClient();
+    DeleteMethod post = new DeleteMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=participantstest&participant_id=99");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(404, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Try to delete a participant for a non-existing widget instance
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void deleteParticipant_InvalidInstance() throws HttpException,
+      IOException {
+    HttpClient client = new HttpClient();
+    DeleteMethod post = new DeleteMethod(TEST_PARTICIPANTS_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=participantstestinvalidkey&participant_id=1");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(400, code);
+    post.releaseConnection();
+  }
 
 }

Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java?rev=1154962&r1=1154961&r2=1154962&view=diff
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java (original)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/PropertiesControllerTest.java Mon Aug  8 13:42:56 2011
@@ -14,9 +14,11 @@
 package org.apache.wookie.tests.functional;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+
+import java.io.IOException;
 
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.DeleteMethod;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
@@ -24,269 +26,337 @@ import org.apache.commons.httpclient.met
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+/**
+ * Test cases for the Properties REST API
+ */
 public class PropertiesControllerTest extends AbstractControllerTest {
-	
-	@BeforeClass
-	public static void setup(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=propstest");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(201,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("post failed");
-	    }
-	}
-	
-	@Test
-	public void setPreferenceUsingPostParameters() throws Exception{
-	    try {
-	    	String url = TEST_PROPERTIES_SERVICE_URL_VALID;
-	    	//String url = TEST_PROPERTIES_SERVICE_URL_VALID;
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(url);
-	        post.addParameter("api_key", API_KEY_VALID);
-	        post.addParameter("widgetid", WIDGET_ID_VALID);
-	        post.addParameter("userid", "test");
-	        post.addParameter("is_public", "false");
-	        post.addParameter("shareddatakey","propstest");
-	        post.addParameter("propertyname", "testpost");
-	        post.addParameter("propertyvalue","pass");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(201,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("POST failed");
-	    }	 
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=propstest&propertyname=testpost");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(200, code);
-	        String resp = get.getResponseBodyAsString();
-	        assertEquals("pass",resp);
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("POST  failed to set info correctly");
-	    }
-	}
-	
-	@Test
-	public void setPreference(){
-		// Set some shared data
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=false&shareddatakey=propstest&propertyname=pass&propertyvalue=pass");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(201,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("set shared data failed");
-	    }	 
-	    
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=propstest&propertyname=pass");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(200, code);
-	        String resp = get.getResponseBodyAsString();
-	        assertEquals("pass",resp);
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("get preference failed");
-	    }		
-	}
-	
-	@Test
-	public void setSharedData(){
-		// Set some shared data
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=true&shareddatakey=propstest&propertyname=cat&propertyvalue=garfield");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(201,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("set shared data failed");
-	    }	 
-	    
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=propstest&propertyname=cat");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(200, code);
-	        String resp = get.getResponseBodyAsString();
-	        assertEquals("garfield",resp);
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("get property failed");
-	    }	
-	}
-	
-	@Test
-	public void updateProperty(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PutMethod put = new PutMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        put.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=true&shareddatakey=propstest&propertyname=cat&propertyvalue=felix");
-	        client.executeMethod(put);
-	        int code = put.getStatusCode();
-	        assertEquals(200,code);
-	        put.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("set shared data failed");
-	    }	 
-	    
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=propstest&propertyname=cat");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(200, code);
-	        String resp = get.getResponseBodyAsString();
-	        assertEquals("felix",resp);
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("get property failed");
-	    }	
-		
-	}
-	
-	@Test
-	public void removeProperty(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        DeleteMethod delete = new DeleteMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        delete.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=true&shareddatakey=propstest&propertyname=cat");
-	        client.executeMethod(delete);
-	        int code = delete.getStatusCode();
-	        assertEquals(200,code);
-	        delete.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("delete failed");
-	    }			
-	}
-	
-	@Test
-	public void removePropertyNonExisting(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        DeleteMethod delete = new DeleteMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        delete.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=true&shareddatakey=propstest&propertyname=bogus");
-	        client.executeMethod(delete);
-	        int code = delete.getStatusCode();
-	        assertEquals(404,code);
-	        delete.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("delete failed");
-	    }			
-	}
-	
-	@Test
-	public void setPropertyNoName(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=true&shareddatakey=propstest&propertyvalue=garfield");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(400,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("set shared data failed");
-	    }	
-	}
-	
-	@Test
-	public void setPropertyEmptyName(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=true&shareddatakey=propstest&propertyname=&propertyvalue=garfield");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(400,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("set shared data failed");
-	    }	
-	}
-	
-	@Test
-	public void setPropertyWhitespaceName(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=true&shareddatakey=propstest&propertyname=%20%20&propertyvalue=garfield");
-	        client.executeMethod(post);
-	        int code = post.getStatusCode();
-	        assertEquals(400,code);
-	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("set shared data failed");
-	    }	
-	}
-	
-	@Test
-	public void getPropertyInvalidAPIKey(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_INVALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=propstest&propertyname=cat");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(401, code);
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("get property failed");
-	    }	
-	}
-	
-	@Test
-	public void getPropertyInvalidName(){
-	    try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=propstest&propertyname=madeupname");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(404, code);
-	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("get property failed");
-	    }	
-	}
+
+  /**
+   * Create a new instance for use in testing
+   * 
+   * @throws HttpException
+   * @throws IOException
+   */
+  @BeforeClass
+  public static void setup() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+    post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID + "&userid=test&shareddatakey=propstest");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(201, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Test that we can set preferences using post parameters
+   * 
+   * @throws Exception
+   */
+  @Test
+  public void setPreferenceUsingPostParameters() throws Exception {
+    //
+    // Set a property ("testpost=pass") using POST
+    //
+    String url = TEST_PROPERTIES_SERVICE_URL_VALID;
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(url);
+    post.addParameter("api_key", API_KEY_VALID);
+    post.addParameter("widgetid", WIDGET_ID_VALID);
+    post.addParameter("userid", "test");
+    post.addParameter("is_public", "false");
+    post.addParameter("shareddatakey", "propstest");
+    post.addParameter("propertyname", "testpost");
+    post.addParameter("propertyvalue", "pass");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(201, code);
+    post.releaseConnection();
+
+    //
+    // Read back property using GET
+    //
+    client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=propstest&propertyname=testpost");
+    client.executeMethod(get);
+    code = get.getStatusCode();
+    assertEquals(200, code);
+    String resp = get.getResponseBodyAsString();
+    assertEquals("pass", resp);
+    get.releaseConnection();
+
+  }
+
+  /**
+   * Tests that we can set a property using querystring parameters on a POST
+   * method
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void setPreference() throws HttpException, IOException {
+    //
+    // Set a preference using POST with querystring parameters
+    //
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&is_public=false&shareddatakey=propstest&propertyname=pass&propertyvalue=pass");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(201, code);
+    post.releaseConnection();
+
+    //
+    // Read it back using GET
+    //
+    client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=propstest&propertyname=pass");
+    client.executeMethod(get);
+    code = get.getStatusCode();
+    assertEquals(200, code);
+    String resp = get.getResponseBodyAsString();
+    assertEquals("pass", resp);
+    get.releaseConnection();
+
+  }
+
+  /**
+   * Test we can set shared data values using querystring parameters
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void setSharedData() throws HttpException, IOException {
+
+    //
+    // Set some shared data with a POST and querystring
+    //
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&is_public=true&shareddatakey=propstest&propertyname=cat&propertyvalue=garfield");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(201, code);
+    post.releaseConnection();
+
+    //
+    // Read back the value using GET
+    //
+    client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=propstest&propertyname=cat");
+    client.executeMethod(get);
+    code = get.getStatusCode();
+    assertEquals(200, code);
+    String resp = get.getResponseBodyAsString();
+    assertEquals("garfield", resp);
+    get.releaseConnection();
+
+  }
+
+  /**
+   * Test we can update an existing property
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void updateProperty() throws HttpException, IOException {
+
+    //
+    // Set propstest=cat using POST
+    //
+    HttpClient client = new HttpClient();
+    PutMethod put = new PutMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    put.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&is_public=true&shareddatakey=propstest&propertyname=cat&propertyvalue=felix");
+    client.executeMethod(put);
+    int code = put.getStatusCode();
+    assertEquals(200, code);
+    put.releaseConnection();
+
+    //
+    // Read back the value using GET
+    //
+    client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=propstest&propertyname=cat");
+    client.executeMethod(get);
+    code = get.getStatusCode();
+    assertEquals(200, code);
+    String resp = get.getResponseBodyAsString();
+    assertEquals("felix", resp);
+    get.releaseConnection();
+
+  }
+
+  /**
+   * Test removing a property
+   * 
+   * @throws HttpException
+   * @throws IOException
+   */
+  @Test
+  public void removeProperty() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    DeleteMethod delete = new DeleteMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    delete
+        .setQueryString("api_key="
+            + API_KEY_VALID
+            + "&widgetid="
+            + WIDGET_ID_VALID
+            + "&userid=test&is_public=true&shareddatakey=propstest&propertyname=cat");
+    client.executeMethod(delete);
+    int code = delete.getStatusCode();
+    assertEquals(200, code);
+    delete.releaseConnection();
+  }
+
+  /**
+   * Test removing a non-existing property.
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void removePropertyNonExisting() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    DeleteMethod delete = new DeleteMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    delete
+        .setQueryString("api_key="
+            + API_KEY_VALID
+            + "&widgetid="
+            + WIDGET_ID_VALID
+            + "&userid=test&is_public=true&shareddatakey=propstest&propertyname=bogus");
+    client.executeMethod(delete);
+    int code = delete.getStatusCode();
+    assertEquals(404, code);
+    delete.releaseConnection();
+  }
+
+  /**
+   * Try to set a property without specifying the property name
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void setPropertyNoName() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&is_public=true&shareddatakey=propstest&propertyvalue=garfield");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(400, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Try to set a property with an empty (zero length) property name
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void setPropertyEmptyName() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&is_public=true&shareddatakey=propstest&propertyname=&propertyvalue=garfield");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(400, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Try to set a property with a whitespace-only name
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void setPropertyWhitespaceName() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    PostMethod post = new PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    post.setQueryString("api_key="
+        + API_KEY_VALID
+        + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&is_public=true&shareddatakey=propstest&propertyname=%20%20&propertyvalue=garfield");
+    client.executeMethod(post);
+    int code = post.getStatusCode();
+    assertEquals(400, code);
+    post.releaseConnection();
+  }
+
+  /**
+   * Try to set a property without a valid API key
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void getPropertyInvalidAPIKey() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_INVALID + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=propstest&propertyname=cat");
+    client.executeMethod(get);
+    int code = get.getStatusCode();
+    assertEquals(401, code);
+    get.releaseConnection();
+  }
+
+  /**
+   * Try to get the value of a non-existant property
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void getPropertyInvalidName() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
+    get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+        + WIDGET_ID_VALID
+        + "&userid=test&shareddatakey=propstest&propertyname=madeupname");
+    client.executeMethod(get);
+    int code = get.getStatusCode();
+    assertEquals(404, code);
+    get.releaseConnection();
+  }
 
 }

Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java?rev=1154962&r1=1154961&r2=1154962&view=diff
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java (original)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/ProxyTest.java Mon Aug  8 13:42:56 2011
@@ -17,10 +17,12 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthPolicy;
@@ -32,26 +34,50 @@ import org.junit.Ignore;
 import org.junit.Test;
 
 
+/**
+ * Test cases for the server-side web proxy
+ */
 public class ProxyTest extends AbstractControllerTest {
 
 	private static String instance_id_key;
 	private static String other_instance_id_key;
 	private static final String OTHER_WIDGET_ID = "http://www.getwookie.org/widgets/natter";
 	private static final String PROXY_URL = TEST_SERVER_LOCATION+"proxy";
+	
+	/**
+	 * The valid site URL is our Apache incubator page. This is added by default to the global site whitelist
+	 */
 	private static final String VALID_SITE_URL = "http://incubator.apache.org/wookie/";
+	
+	/**
+	 * Returns XML for checking we get the correct content-type
+	 */
 	private static final String VALID_SITE_XML_URL = TEST_SERVER_LOCATION+"widgets?all=true";
+	
+	/**
+	 * An invalid URL
+	 */
 	private static final String INVALID_SITE_URL = "DFASFAFEQ3FQ32145235123452";
+	
+	/**
+	 * A site not allowed in any policies
+	 */
 	private static final String BLOCKED_SITE_URL = "http://very.bad.place";
+	
+	/**
+	 * The policy allowed site URL is one that the Weather widget is granted acccess to using WARP in the default install
+	 */
 	private static final String POLICY_ALLOWED_SITE_URL = "http://newsrss.bbc.co.uk/weather/forecast/9/Next3DaysRSS.xml";
 	private static final String POLICY_DISALLOWED_SITE_URL = "http://news.bbc.co.uk";
 	private static final String PROTECTED_SITE_URL = TEST_SERVER_LOCATION+"admin/";
 
 	/**
-	 * Create an instance of a widget
+	 * Create two instances of a widget for testing
+	 * @throws IOException 
+	 * @throws HttpException 
 	 */
 	@BeforeClass
-	public static void setup(){
-		try {
+	public static void setup() throws HttpException, IOException{
 			HttpClient client = new HttpClient();
 			PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
 			post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=proxytest");
@@ -59,26 +85,20 @@ public class ProxyTest extends AbstractC
 			String response = post.getResponseBodyAsString();
 			instance_id_key = response.substring(response.indexOf("<identifier>")+12, response.indexOf("</identifier>"));
 			post.releaseConnection();
-		}
-		catch (Exception e) {
-			e.printStackTrace();
-			fail("post failed");
-		}
-		try {
-			HttpClient client = new HttpClient();
-			PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+		
+			 client = new HttpClient();
+			 post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
 			post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+OTHER_WIDGET_ID+"&userid=test&shareddatakey=proxytest");
 			client.executeMethod(post);
-			String response = post.getResponseBodyAsString();
+			 response = post.getResponseBodyAsString();
 			other_instance_id_key = response.substring(response.indexOf("<identifier>")+12, response.indexOf("</identifier>"));
 			post.releaseConnection();
-		}
-		catch (Exception e) {
-			e.printStackTrace();
-			fail("post failed");
-		}
+		
 	}
-
+	
+/**
+ * Check we can access a site allowed under the global whitelist using our widget instance's id key
+ */
 	@Test
 	public void getValidSite(){
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+VALID_SITE_URL;
@@ -86,12 +106,18 @@ public class ProxyTest extends AbstractC
 		assertEquals(500,send(url,"POST")); // This URL doesn't support POST
 	}
 	
+	/**
+	 * Check we can access a site allowed under a widget-specific policy using an instance's id key
+	 */
 	@Test
 	public void getAllowedSite(){
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+POLICY_ALLOWED_SITE_URL;
 		assertEquals(200,send(url,"GET"));
 	}
 	
+	/**
+	 * Check that we can't access a site which is not in the whitelist or WARP
+	 */
 	@Test
 	public void getDisallowedSite(){
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+POLICY_DISALLOWED_SITE_URL;
@@ -99,7 +125,9 @@ public class ProxyTest extends AbstractC
 		assertEquals(403,send(url,"POST")); // This URL doesn't support POST
 	}
 	
-	// Test trying to get to a site allowed for a different widget, but not this one
+	/**
+	 * Test trying to get to a site allowed for a different widget, but not this one
+	 */
 	@Test
 	public void getAllowedSiteWrongInstance(){
 		String url = PROXY_URL+"?instanceid_key="+other_instance_id_key+"&url="+POLICY_ALLOWED_SITE_URL;
@@ -107,6 +135,10 @@ public class ProxyTest extends AbstractC
 		assertEquals(403,send(url,"POST"));
 	}
 	
+	/**
+	 * Test we can get the content from a site with the correct content-type (in this case XML)
+	 * @throws Exception
+	 */
 	@Test
 	public void getValidSiteAndValidXMLContentType() throws Exception{
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+VALID_SITE_XML_URL;
@@ -118,6 +150,11 @@ public class ProxyTest extends AbstractC
 		assertEquals(200,code);
 		assertTrue(req.getResponseHeader("Content-Type").toString().contains("text/xml"));
 	}
+	
+	/**
+	 * Test we can get the content from a site with the correct content-type (in this case HTML)
+	 * @throws Exception
+	 */
 	@Test
 	public void getValidSiteAndValidHTMLContentType() throws Exception{
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+VALID_SITE_URL;
@@ -129,7 +166,11 @@ public class ProxyTest extends AbstractC
 		assertEquals(200,code);
 		assertTrue(req.getResponseHeader("Content-Type").toString().contains("text/html"));
 	}
-
+	
+	
+/**
+ * Test that we can't access a site with an invalid instance ID key
+ */
 	@Test
 	public void getValidSiteInvalidIdKey(){
 		String url = PROXY_URL+"?instanceid_key=TEST&url="+VALID_SITE_URL;
@@ -137,6 +178,9 @@ public class ProxyTest extends AbstractC
 		assertEquals(403,send(url,"POST"));
 	}
 
+	/**
+	 * Test that we can't access a site no instance id key
+	 */
 	@Test
 	public void getValidSiteNoIdKey(){
 		String url = PROXY_URL+"?url="+VALID_SITE_URL;
@@ -144,6 +188,9 @@ public class ProxyTest extends AbstractC
 		assertEquals(403,send(url,"POST"));
 	}
 
+	/**
+	 * Test we can't get access to a site using an invalid URL
+	 */
 	@Test
 	public void getInvalidSite(){
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+INVALID_SITE_URL;
@@ -151,12 +198,19 @@ public class ProxyTest extends AbstractC
 		assertEquals(400,send(url,"POST"));
 	}
 
+	 /**
+   * Test we can't get access to a site that isn't in either the global whitelist of WARP
+   */
 	@Test
 	public void getBlockedSite(){
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+BLOCKED_SITE_URL;
 		assertEquals(403,send(url,"GET"));
 	}
 
+	/**
+	 * Tests that we can get valid returns when using querystring parameters in a POST request
+	 * @throws Exception
+	 */
 	@Test
 	public void postWithOnlyQueryStringParameters() throws Exception{
 		HttpClient client = new HttpClient();
@@ -164,7 +218,9 @@ public class ProxyTest extends AbstractC
 		authPrefs.add(AuthPolicy.DIGEST );
 		authPrefs.add(AuthPolicy.BASIC);
 		client.getParams().setParameter (AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
+		//
 		// send the basic authentication response even before the server gives an unauthorized response
+		//
 		client.getParams().setAuthenticationPreemptive(true);
 		client.getState().setCredentials(
 				new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
@@ -177,6 +233,9 @@ public class ProxyTest extends AbstractC
 		assertEquals(200,code);
 	}
 	
+	/**
+	 * Ignored - this was a case for handling double-encoding, but which is no longer valid
+	 */
 	@Ignore // Not a valid use case
 	@Test
 	public void getWithEncodedParameters(){
@@ -184,12 +243,19 @@ public class ProxyTest extends AbstractC
 		assertEquals(200,send(url,"GET"));
 	}
 	
+	/**
+	 * Test the proxy can handle a site URL that contains unencoded parameters including a second "?" character
+	 */
 	@Test
 	public void getWithUnencodedParameters(){
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+VALID_SITE_URL+"?x=1&y=2";
 		assertEquals(200,send(url,"GET"));
 	}
 	
+	 /**
+   * Test the proxy can handle a site URL that contains unencoded parameters appended in the usual form generated
+   * by JQuery
+   */
 	@Test
 	public void getWithJQueryEncodedParameters(){
 		String url = PROXY_URL+"?instanceid_key="+instance_id_key+"&url="+VALID_SITE_URL+"&x=y";
@@ -197,6 +263,10 @@ public class ProxyTest extends AbstractC
 		assertEquals(500,send(url,"POST")); // This URL doesn't support POST
 	}
 	
+	/**
+	 * Test that the proxy behaves as expected when a POST request contains a mix of querystring parameters and post parameters
+	 * @throws Exception
+	 */
 	@Test
 	public void postWithMixedQueryAndParameters() throws Exception{
 		HttpClient client = new HttpClient();
@@ -204,7 +274,9 @@ public class ProxyTest extends AbstractC
 		authPrefs.add(AuthPolicy.DIGEST );
 		authPrefs.add(AuthPolicy.BASIC);
 		client.getParams().setParameter (AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
+		//
 		// send the basic authentication response even before the server gives an unauthorized response
+		//
 		client.getParams().setAuthenticationPreemptive(true);
 		client.getState().setCredentials(
 				new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
@@ -224,9 +296,7 @@ public class ProxyTest extends AbstractC
 	 */
 	@Test
 	public void postWithPassingParameters() throws Exception{
-	    try {
 	    	String url = PROXY_URL;
-	    	//String url = TEST_PROPERTIES_SERVICE_URL_VALID;
 	        HttpClient client = new HttpClient();
 	        PostMethod post = new PostMethod(url);
 	        post.addParameter("instanceid_key", instance_id_key);
@@ -242,26 +312,22 @@ public class ProxyTest extends AbstractC
 	        int code = post.getStatusCode();
 	        assertEquals(200,code);
 	        post.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("POST via proxy failed");
-	    }	 
-	    try {
-	        HttpClient client = new HttpClient();
+
+	         client = new HttpClient();
 	        GetMethod get = new GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
 	        get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=proxytest&propertyname=pass");
 	        client.executeMethod(get);
-	        int code = get.getStatusCode();
+	         code = get.getStatusCode();
 	        assertEquals(200, code);
 	        String resp = get.getResponseBodyAsString();
 	        assertEquals("pass",resp);
 	        get.releaseConnection();
-	    }
-	    catch (Exception e) {
-	    	fail("POST via proxy failed to set info correctly");
-	    }
 	}
 
+	/**
+	 * Tests that the proxy behaves correctly when using POST parameters only
+	 * @throws Exception
+	 */
 	@Test
 	public void postWithOnlyParameters() throws Exception{
 		HttpClient client = new HttpClient();
@@ -269,7 +335,9 @@ public class ProxyTest extends AbstractC
 		authPrefs.add(AuthPolicy.DIGEST );
 		authPrefs.add(AuthPolicy.BASIC);
 		client.getParams().setParameter (AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
+		//
 		// send the basic authentication response even before the server gives an unauthorized response
+		//
 		client.getParams().setAuthenticationPreemptive(true);
 		client.getState().setCredentials(
 				new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
@@ -284,6 +352,11 @@ public class ProxyTest extends AbstractC
 		assertEquals(200,code);
 	}
 
+	/**
+	 * Test that the proxy passes through the correct status code when trying to access 
+	 * an allowed resource without authn credentials required at the final target site
+	 * @throws Exception
+	 */
 	@Test
 	public void getProtectedSiteWithoutAuth() throws Exception{
 		HttpClient client = new HttpClient();
@@ -295,6 +368,10 @@ public class ProxyTest extends AbstractC
 		assertEquals(401,code);
 	}
 	
+	/**
+	 * Test that the proxy passes along credentials to the proxied site and is granted access
+	 * @throws Exception
+	 */
 	@Test
 	public void getProtectedSiteWithBasicAuth() throws Exception{
 		HttpClient client = new HttpClient();
@@ -302,7 +379,9 @@ public class ProxyTest extends AbstractC
 		authPrefs.add(AuthPolicy.DIGEST );
 		authPrefs.add(AuthPolicy.BASIC);
 		client.getParams().setParameter (AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
+		//
 		// send the basic authentication response even before the server gives an unauthorized response
+		//
 		client.getParams().setAuthenticationPreemptive(true);
 		client.getState().setCredentials(
 				new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
@@ -315,6 +394,12 @@ public class ProxyTest extends AbstractC
 		assertEquals(200,code);
 	}
 
+	/**
+	 * Utility method for sending requests
+	 * @param url
+	 * @param method
+	 * @return
+	 */
 	private int send(String url, String method){
 		try {
 			HttpClient client = new HttpClient();

Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/UpdatesControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/UpdatesControllerTest.java?rev=1154962&r1=1154961&r2=1154962&view=diff
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/UpdatesControllerTest.java (original)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/UpdatesControllerTest.java Mon Aug  8 13:42:56 2011
@@ -14,76 +14,92 @@
 package org.apache.wookie.tests.functional;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.wookie.tests.helpers.WidgetUploader;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
 /**
- * Test cases for Widget Updates controller
+ * Test cases for Widget Updates controller.
+ * 
+ * NOTE:
+ * 
+ * These are simple test cases for the controller. Actually triggering the
+ * updates is tested using the conformance tests in
+ * org.apache.wookie.test.conformance.WidgetUpdates
  */
-public class UpdatesControllerTest extends AbstractControllerTest{
-	
-	protected static final String TEST_UPDATES_URL_VALID = TEST_SERVER_LOCATION+"updates";
-	
-	@BeforeClass
-	public static void setup(){
-		
-		// Install all the test widgets
-		try {
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-acquisition11/001/ta-ac-001.wgt");//ac11
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-acquisition13/001/ta-ac-001.wgt");//ac13
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/003/ta-pr-003.wgt");//pr203
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/008/ta-pr-008.wgt");//pr208
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/009/ta-pr-009.wgt");//pr209
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/010/ta-pr-010.wgt");//pr210
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/011/ta-pr-011.wgt");//pr211
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/012/ta-pr-012.wgt");//pr212
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/013/ta-pr-013.wgt");//pr213
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/015/ta-pr-015.wgt");//pr215
-			WidgetUploader.uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/016/ta-pr-016.wgt");//pr216
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	}
-
-	@Test
-	public void getUpdatesUnauthorized(){
-		try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_UPDATES_URL_VALID);
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(401, code);
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("get failed");
-	    }
-	}
-	
-	@Test
-	public void getUpdates(){
-		try {
-	        HttpClient client = new HttpClient();
-	        GetMethod get = new GetMethod(TEST_UPDATES_URL_VALID);
-			setAuthenticationCredentials(client);
-	        get.setRequestHeader("Content-type", "text/xml");
-	        client.executeMethod(get);
-	        int code = get.getStatusCode();
-	        assertEquals(200, code);
-	    }
-	    catch (Exception e) {
-	    	e.printStackTrace();
-	    	fail("get failed");
-	    }
-	}
-	
-	// NOTE: Actually triggering the updates is tested using the conformance tests.
+public class UpdatesControllerTest extends AbstractControllerTest {
+
+  protected static final String TEST_UPDATES_URL_VALID = TEST_SERVER_LOCATION
+      + "updates";
+
+  /**
+   * Install widgets for testing
+   * 
+   * @throws IOException
+   */
+  @BeforeClass
+  public static void setup() throws IOException {
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-acquisition11/001/ta-ac-001.wgt");// ac11
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-acquisition13/001/ta-ac-001.wgt");// ac13
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/003/ta-pr-003.wgt");// pr203
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/008/ta-pr-008.wgt");// pr208
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/009/ta-pr-009.wgt");// pr209
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/010/ta-pr-010.wgt");// pr210
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/011/ta-pr-011.wgt");// pr211
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/012/ta-pr-012.wgt");// pr212
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/013/ta-pr-013.wgt");// pr213
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/015/ta-pr-015.wgt");// pr215
+    WidgetUploader
+        .uploadWidget("http://dev.w3.org/2006/waf/widgets-updates/test-suite/test-cases/ta-processing2/016/ta-pr-016.wgt");// pr216
+  }
+
+  /**
+   * Tests that a request for updates is refused without admin credentials
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void getUpdatesUnauthorized() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_UPDATES_URL_VALID);
+    client.executeMethod(get);
+    int code = get.getStatusCode();
+    assertEquals(401, code);
+  }
+
+  /**
+   * Tests tha a request for updates with admin credentials returns a valid
+   * response
+   * 
+   * @throws IOException
+   * @throws HttpException
+   */
+  @Test
+  public void getUpdates() throws HttpException, IOException {
+    HttpClient client = new HttpClient();
+    GetMethod get = new GetMethod(TEST_UPDATES_URL_VALID);
+    setAuthenticationCredentials(client);
+    get.setRequestHeader("Content-type", "text/xml");
+    client.executeMethod(get);
+    int code = get.getStatusCode();
+    assertEquals(200, code);
+  }
 
 }



Mime
View raw message