You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2011/03/28 15:38:38 UTC

svn commit: r1086227 - in /incubator/wookie/trunk: src-tests/org/apache/wookie/tests/functional/WhiteListControllerTest.java src/org/apache/wookie/controller/WhiteListController.java

Author: scottbw
Date: Mon Mar 28 13:38:38 2011
New Revision: 1086227

URL: http://svn.apache.org/viewvc?rev=1086227&view=rev
Log:
Added catch points for bad parameters in the Whitelist controller, and added a set of functional tests to check we have correct behaviour

Added:
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WhiteListControllerTest.java
Modified:
    incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java

Added: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WhiteListControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WhiteListControllerTest.java?rev=1086227&view=auto
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WhiteListControllerTest.java (added)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WhiteListControllerTest.java Mon Mar 28 13:38:38 2011
@@ -0,0 +1,216 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.tests.functional;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+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.PostMethod;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.junit.Test;
+
+public class WhiteListControllerTest extends AbstractControllerTest {
+	
+	private static final String WHITELIST_SERVICE_URL_VALID = TEST_SERVER_LOCATION+"whitelist";
+
+	@Test
+	public void getEntriesUnauthorized(){
+		try {
+	        HttpClient client = new HttpClient();
+	        GetMethod get = new GetMethod(WHITELIST_SERVICE_URL_VALID);
+	        client.executeMethod(get);
+	        int code = get.getStatusCode();
+	        assertEquals(401, code);
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("get failed");
+	    }
+	}
+	
+	@Test
+	public void getEntries(){
+		try {
+	        HttpClient client = new HttpClient();
+	        GetMethod get = new GetMethod(WHITELIST_SERVICE_URL_VALID);
+			setAuthenticationCredentials(client);
+	        client.executeMethod(get);
+	        int code = get.getStatusCode();
+	        assertEquals(200, code);
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("get failed");
+	    }
+	}
+	
+	@Test
+	public void addEntry(){
+		try {
+	        HttpClient client = new HttpClient();
+	        PostMethod post = new PostMethod(WHITELIST_SERVICE_URL_VALID);
+			setAuthenticationCredentials(client);
+	        post.setParameter("url", "http://test.apache.org");
+	        client.executeMethod(post);
+	        int code = post.getStatusCode();
+	        assertEquals(201, code);
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("post failed");
+	    }	
+		try {
+	        HttpClient client = new HttpClient();
+	        GetMethod get = new GetMethod(WHITELIST_SERVICE_URL_VALID);
+			setAuthenticationCredentials(client);
+	        client.executeMethod(get);
+	        int code = get.getStatusCode();
+	        assertEquals(200, code);
+	        assertTrue(get.getResponseBodyAsString().contains("http://test.apache.org"));
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("get failed");
+	    }
+	}
+	
+	@Test
+	public void removeEntry(){
+		String id = null;
+		try {
+	        HttpClient client = new HttpClient();
+	        GetMethod get = new GetMethod(WHITELIST_SERVICE_URL_VALID);
+			setAuthenticationCredentials(client);
+	        get.setRequestHeader("Content-type", "application/json");
+	        client.executeMethod(get);
+	        int code = get.getStatusCode();
+	        assertEquals(200, code);
+	        assertTrue(get.getResponseBodyAsString().contains("http://test.apache.org"));
+	        // Now find the ID and delete it
+	        JSONObject obj = new JSONObject(get.getResponseBodyAsString());
+	        JSONArray entries = obj.getJSONArray("entries");
+	        for (int i=0;i<entries.length();i++){
+	        	JSONObject entry = entries.getJSONObject(i);
+	        	if (entry.getString("url").equals("http://test.apache.org")) id = entry.getString("id");
+	        }
+	        }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("get failed");
+	    }
+		try {
+	        HttpClient client = new HttpClient();
+	        DeleteMethod del = new DeleteMethod(WHITELIST_SERVICE_URL_VALID+"/"+id);
+			setAuthenticationCredentials(client);
+	        client.executeMethod(del);
+	        int code = del.getStatusCode();
+	        assertEquals(200, code);
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("delete failed");
+	    }
+		try {
+	        HttpClient client = new HttpClient();
+	        GetMethod get = new GetMethod(WHITELIST_SERVICE_URL_VALID);
+	        get.setRequestHeader("Content-type", "application/json");
+			setAuthenticationCredentials(client);
+	        client.executeMethod(get);
+	        int code = get.getStatusCode();
+	        assertEquals(200, code);
+	        System.out.println(get.getResponseBodyAsString());
+	        assertFalse(get.getResponseBodyAsString().contains("http://test.apache.org"));
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("get failed");
+	    }
+	}
+	
+	@Test
+	public void removeNonExistantEntry(){
+		try {
+	        HttpClient client = new HttpClient();
+	        DeleteMethod del = new DeleteMethod(WHITELIST_SERVICE_URL_VALID+"/99999999");
+			setAuthenticationCredentials(client);
+	        client.executeMethod(del);
+	        int code = del.getStatusCode();
+	        assertEquals(404, code);
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("delete failed");
+	    }	
+	}
+	
+	@Test
+	public void addEntryNoUrl(){
+		try {
+	        HttpClient client = new HttpClient();
+	        PostMethod post = new PostMethod(WHITELIST_SERVICE_URL_VALID);
+			setAuthenticationCredentials(client);
+	        client.executeMethod(post);
+	        int code = post.getStatusCode();
+	        assertEquals(400, code);
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("post failed");
+	    }	
+	}
+	
+	@Test
+	public void addEntryInvalidUrl(){
+		try {
+	        HttpClient client = new HttpClient();
+	        PostMethod post = new PostMethod(WHITELIST_SERVICE_URL_VALID);
+	        post.setParameter("url", "I AM NOT A URL");
+			setAuthenticationCredentials(client);
+	        client.executeMethod(post);
+	        int code = post.getStatusCode();
+	        assertEquals(400, code);
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("post failed");
+	    }	
+	}
+	
+	@Test
+	public void addDuplicateEntry(){
+		try {
+	        HttpClient client = new HttpClient();
+	        PostMethod post = new PostMethod(WHITELIST_SERVICE_URL_VALID);
+			setAuthenticationCredentials(client);
+	        post.setParameter("url", "http://test.apache.org");
+	        client.executeMethod(post);
+	        int code = post.getStatusCode();
+	        assertEquals(201, code);
+	        client.executeMethod(post);
+	        code = post.getStatusCode();
+	        assertEquals(409, code);
+	    }
+	    catch (Exception e) {
+	    	e.printStackTrace();
+	    	fail("post failed");
+	    }
+	}
+
+}

Modified: incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java?rev=1086227&r1=1086226&r2=1086227&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/controller/WhiteListController.java Mon Mar 28 13:38:38 2011
@@ -26,6 +26,7 @@ import org.apache.wookie.exceptions.Reso
 import org.apache.wookie.exceptions.ResourceNotFoundException;
 import org.apache.wookie.exceptions.UnauthorizedAccessException;
 import org.apache.wookie.helpers.WhitelistHelper;
+import org.apache.wookie.w3c.util.IRIValidator;
 
 /**
  * Controller for Whitelist entries
@@ -56,7 +57,11 @@ public class WhiteListController extends
 		
 		String url = request.getParameter("url");
 		if (url == null || url.trim().length() == 0) throw new InvalidParametersException();
+		if (!IRIValidator.isValidIRI(url)) throw new InvalidParametersException();
 		IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
+		// Check for duplicates
+		IWhitelist[] matches = persistenceManager.findByValue(IWhitelist.class, "fUrl", url);
+		if (matches.length != 0) throw new ResourceDuplicationException();
 		IWhitelist entry = persistenceManager.newInstance(IWhitelist.class);
 		entry.setfUrl(url);
 		return persistenceManager.save(entry);