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 2012/03/01 12:25:18 UTC

svn commit: r1295518 - in /incubator/wookie/trunk: WebContent/WEB-INF/web.xml src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java src-tests/testdata/delete-test.wgt src/org/apache/wookie/controller/WidgetsController.java

Author: scottbw
Date: Thu Mar  1 11:25:17 2012
New Revision: 1295518

URL: http://svn.apache.org/viewvc?rev=1295518&view=rev
Log:
Added DELETE /widget/{id_or_uri} to REST API and added test cases. See WOOKIE-319.

Added:
    incubator/wookie/trunk/src-tests/testdata/delete-test.wgt   (with props)
Modified:
    incubator/wookie/trunk/WebContent/WEB-INF/web.xml
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java
    incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java

Modified: incubator/wookie/trunk/WebContent/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/WebContent/WEB-INF/web.xml?rev=1295518&r1=1295517&r2=1295518&view=diff
==============================================================================
--- incubator/wookie/trunk/WebContent/WEB-INF/web.xml (original)
+++ incubator/wookie/trunk/WebContent/WEB-INF/web.xml Thu Mar  1 11:25:17 2012
@@ -383,6 +383,8 @@
 				<web-resource-name>WidgetServlet</web-resource-name>
 				<url-pattern>/widgets/*</url-pattern>
 				<http-method>POST</http-method>
+				<http-method>DELETE</http-method>
+				<http-method>PUT</http-method>
 			</web-resource-collection>		
 			<auth-constraint>
 				<role-name>widgetadmin</role-name>

Modified: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java?rev=1295518&r1=1295517&r2=1295518&view=diff
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java (original)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java Thu Mar  1 11:25:17 2012
@@ -24,6 +24,7 @@ import org.apache.commons.httpclient.Htt
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.DeleteMethod;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.multipart.FilePart;
@@ -197,6 +198,77 @@ public class WidgetsControllerTest exten
 	  post.releaseConnection();     
 	}
 	
+	@Test
+	public void deleteWidgetUnauthorized() throws HttpException, IOException{
+    HttpClient client = new HttpClient();
+    DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/1");
+	  client.executeMethod(delete);
+	  assertEquals(401, delete.getStatusCode());
+	  
+    //
+    // Check it wasn't deleted
+    //
+    GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/1");
+    client.executeMethod(get);
+    assertEquals(200, get.getStatusCode());
+	}
+	
+	@Test
+	public void deleteWidgetNonexisting() throws HttpException, IOException{
+    HttpClient client = new HttpClient();
+    //
+    // Use admin credentials
+    //
+    client.getState().setCredentials(
+        new AuthScope("localhost", 8080, "wookie"),
+        new UsernamePasswordCredentials("java", "java")
+        );
+    DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/9999");
+    client.executeMethod(delete);
+    assertEquals(404, delete.getStatusCode());
+	}
+	
+	@Test
+	public void deleteWidget() throws HttpException, IOException{
+    HttpClient client = new HttpClient();
+    //
+    // Use admin credentials
+    //
+    client.getState().setCredentials(
+        new AuthScope("localhost", 8080, "wookie"),
+        new UsernamePasswordCredentials("java", "java")
+        );
+    
+    
+    //
+    // Upload widget we'll test deleting next
+    //
+    File file = new File("src-tests/testdata/delete-test.wgt");
+    assertTrue(file.exists());
+    PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
+    Part[] parts = { new FilePart(file.getName(), file) };
+    post.setRequestEntity(new MultipartRequestEntity(parts, post
+        .getParams()));
+    client.executeMethod(post);   
+    int code = post.getStatusCode();
+    assertEquals(201,code);
+    post.releaseConnection();   
+    
+    //
+    // Delete the widget
+    //
+    DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/http%3A%2F%2Fdeletetest");
+    client.executeMethod(delete);
+    assertEquals(200, delete.getStatusCode());
+    
+    //
+    // Check it was deleted
+    //
+    GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/http%3A%2F%2Fdeletetest");
+    client.executeMethod(get);
+    assertEquals(404, get.getStatusCode());
+	}
+	
 	/**
 	 * Check that when we update a widget, we don't duplicate access policies. See WOOKIE-273.
 	 * @throws HttpException

Added: incubator/wookie/trunk/src-tests/testdata/delete-test.wgt
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/testdata/delete-test.wgt?rev=1295518&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/wookie/trunk/src-tests/testdata/delete-test.wgt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java?rev=1295518&r1=1295517&r2=1295518&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java Thu Mar  1 11:25:17 2012
@@ -62,12 +62,6 @@ import org.apache.wookie.w3c.exceptions.
 public class WidgetsController extends Controller{
 
 	private static final long serialVersionUID = 8759704878105474902L;
-	
-	@Override
-	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
-			throws ServletException, IOException {
-		resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
-	}
 
 	@Override
 	protected void doPut(HttpServletRequest req, HttpServletResponse resp)
@@ -88,7 +82,6 @@ public class WidgetsController extends C
 			return;
 		}
 		
-		System.out.println("ResourceID:"+resourceId);
 		IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
 		IWidget widget = persistenceManager.findWidgetByGuid(resourceId);
 		// attempt to get specific widget by id
@@ -125,6 +118,34 @@ public class WidgetsController extends C
     IWidget[] widgets = persistenceManager.findAll(IWidget.class);
 		returnXml(WidgetHelper.createXMLWidgetsDocument(widgets, getLocalPath(request), getLocales(request)),response);
 	}
+	
+  /* (non-Javadoc)
+   * @see org.apache.wookie.controller.Controller#remove(java.lang.String, javax.servlet.http.HttpServletRequest)
+   */
+  @Override
+  protected boolean remove(String resourceId, HttpServletRequest request)
+      throws ResourceNotFoundException, UnauthorizedAccessException,
+      InvalidParametersException {
+
+    
+    //
+    // Identify the widget to delete
+    //
+    IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
+    IWidget widget = persistenceManager.findWidgetByGuid(resourceId);
+    // attempt to get specific widget by id
+    if (widget == null) {
+      persistenceManager = PersistenceManagerFactory.getPersistenceManager();
+      widget = persistenceManager.findById(IWidget.class, resourceId);
+    }
+    
+    if (widget == null) throw new ResourceNotFoundException();
+    
+    //
+    // Delete the widget
+    //
+    return WidgetFactory.destroy(widget);
+  }
 
 	/**
 	 * Install a new Widget by uploading and installing it