Author: scottbw
Date: Fri Mar 2 09:38:16 2012
New Revision: 1296101
URL: http://svn.apache.org/viewvc?rev=1296101&view=rev
Log:
Added PUT method to /widgets API and added test cases (see WOOKIE-319)
Modified:
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/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=1296101&r1=1296100&r2=1296101&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
Fri Mar 2 09:38:16 2012
@@ -25,6 +25,7 @@ import org.apache.commons.httpclient.Htt
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.PutMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
@@ -268,6 +269,101 @@ public class WidgetsControllerTest exten
assertEquals(404, get.getStatusCode());
}
+
+ @Test
+ public void updateWidget() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ //
+ // Use admin credentials
+ //
+ setAuthenticationCredentials(client);
+
+ PutMethod post = new
PutMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/http%3A%2F%2Fuploadtest");
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src-tests/testdata/upload-test.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 200 (Updated)
+ //
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(200,code);
+ post.releaseConnection();
+
+ }
+
+ @Test
+ public void updateWidgetUnauthorized() throws HttpException,
IOException{
+ HttpClient client = new HttpClient();
+
+ PutMethod post = new
PutMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/http%3A%2F%2Fuploadtest");
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src-tests/testdata/upload-test.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 200 (Updated)
+ //
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(401,code);
+ post.releaseConnection();
+ }
+
+ @Test
+ public void updateWidgetNotFound() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ //
+ // Use admin credentials
+ //
+ setAuthenticationCredentials(client);
+
+ PutMethod post = new
PutMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/http%3A%2F%2Fnosuchwidget");
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src-tests/testdata/upload-test.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 200 (Updated)
+ //
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(404,code);
+ post.releaseConnection();
+
+ }
+
/**
* Check that when we update a widget, we don't duplicate access
policies. See WOOKIE-273.
* @throws HttpException
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=1296101&r1=1296100&r2=1296101&view=diff
==============================================================================
---
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java
(original)
+++
incubator/wookie/trunk/src/org/apache/wookie/controller/WidgetsController.java
Fri Mar 2 09:38:16 2012
@@ -61,15 +61,34 @@ import org.apache.wookie.w3c.exceptions.
*/
public class WidgetsController extends Controller{
- private static final long serialVersionUID = 8759704878105474902L;
-
- @Override
- protected void doPut(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
- }
+ private static final long serialVersionUID = 8759704878105474902L;
// Implementation
+
+ /* (non-Javadoc)
+ * @see org.apache.wookie.controller.Controller#update(java.lang.String,
javax.servlet.http.HttpServletRequest)
+ */
+ @Override
+ protected void update(String resourceId, HttpServletRequest request)
+ throws ResourceNotFoundException, InvalidParametersException,
+ UnauthorizedAccessException {
+
+ 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);
+ }
+ // return widget result
+ if (widget == null) throw new ResourceNotFoundException();
+
+ try {
+ create(resourceId, request);
+ } catch (ResourceDuplicationException e) {
+ e.printStackTrace();
+ }
+ }
/* (non-Javadoc)
* @see org.apache.wookie.controller.Controller#show(java.lang.String,
javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)