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 2014/02/19 11:49:02 UTC
svn commit: r1569676 [3/3] -
/wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/
Modified: wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java
URL: http://svn.apache.org/viewvc/wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java?rev=1569676&r1=1569675&r2=1569676&view=diff
==============================================================================
--- wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java (original)
+++ wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java Wed Feb 19 10:49:01 2014
@@ -24,13 +24,13 @@ 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;
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;
+import org.apache.wookie.tests.helpers.Request;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
@@ -41,409 +41,391 @@ import org.junit.Test;
*/
public class WidgetInstancesControllerTest extends AbstractControllerTest {
- private static String TEST_ID_KEY = "";
+ private static String TEST_ID_KEY = "";
- @BeforeClass
- public static void setup() throws HttpException, IOException {
- HttpClient client = new HttpClient();
-
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- //
- // Setup POST method
- //
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
-
- //
- // Get the locale test widget
- //
- File file = new File("src/test/resources/localetest.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 201 (Created)
- //
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
- }
-
- @AfterClass
- public static void tearDown() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- DeleteMethod delete;
-
- // now set auth as admin
- setAuthenticationCredentials(client);
-
- delete = new DeleteMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- delete.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
- + WIDGET_ID_VALID + "&userid=test&shareddatakey=test");
- client.executeMethod(delete);
- delete.releaseConnection();
-
- delete = new DeleteMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- delete.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
- + WIDGET_ID_VALID + "&userid=test&shareddatakey=clonetestsrc");
- client.executeMethod(delete);
- delete.releaseConnection();
-
- delete = new DeleteMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- delete.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
- + WIDGET_ID_VALID + "&userid=test&shareddatakey=clonetestsync");
- client.executeMethod(delete);
- delete.releaseConnection();
-
- delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_LOCALIZED);
- client.executeMethod(delete);
- delete.releaseConnection();
- }
-
- /**
- * Test we can get an instance localized using the locale parameter
- *
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getLocalizedInstance() 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_LOCALIZED
- + "&userid=localetest&shareddatakey=localetest&locale=fr");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- assertTrue(post.getResponseBodyAsString().contains("locales/fr/index.htm"));
- assertTrue(post.getResponseBodyAsString().contains(
- "tester les paramètres régionaux"));
- TEST_ID_KEY = post.getResponseBodyAsString().substring(
- post.getResponseBodyAsString().indexOf("<identifier>") + 12,
- post.getResponseBodyAsString().indexOf("</identifier>"));
- post.releaseConnection();
- }
-
- /**
- * Test we can get an existing instance using instance parameters - widgetid,
- * apikey, userid, shareddatakey, locale
- *
- * @throws HttpException
- * @throws IOException
- */
- @Test
- public void getExistingInstanceByInstanceParams() throws HttpException,
- IOException {
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
- + WIDGET_ID_LOCALIZED
- + "&userid=localetest&shareddatakey=localetest&locale=fr");
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200, code);
- assertTrue(get.getResponseBodyAsString().contains("locales/fr/index.htm"));
- assertTrue(get.getResponseBodyAsString().contains(
- "tester les paramètres régionaux"));
- }
-
- /**
- * Test we can get an existing instance using just the id_key parameter
- *
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getExistingInstanceByIdKey() throws HttpException, IOException {
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- get.setQueryString("api_key=" + API_KEY_VALID + "&idkey=" + TEST_ID_KEY);
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200, code);
- System.out.println(get.getResponseBodyAsString());
- assertTrue(get.getResponseBodyAsString().contains("locales/fr/index.htm"));
- assertTrue(get.getResponseBodyAsString().contains(
- "tester les paramètres régionaux"));
- }
-
- /**
- * Test we can get an existing instance using the id_key as a resource path
- *
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getExistingInstanceByIdResource() throws HttpException,
- IOException {
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_INSTANCES_SERVICE_URL_VALID + "/"
- + TEST_ID_KEY);
- get.setQueryString("api_key=" + API_KEY_VALID);
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200, code);
- assertTrue(get.getResponseBodyAsString().contains("locales/fr/index.htm"));
- assertTrue(get.getResponseBodyAsString().contains(
- "tester les paramètres régionaux"));
- }
-
- /**
- * Test that instance localization includes support for fallback locales -
- * e.g. specifying "early modern french" locale returns standard FR start file
- *
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getGracefulLocalizedInstance() 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_LOCALIZED
- + "&userid=localetest1b&shareddatakey=localetest1b&locale=fr-1694acad");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- assertTrue(post.getResponseBodyAsString().contains("locales/fr/index.htm"));
- assertFalse(post.getResponseBodyAsString().contains("locale test"));
- assertTrue(post.getResponseBodyAsString().contains(
- "tester les paramètres régionaux"));
- post.releaseConnection();
- }
-
- /**
- * Tests that requesting an instance for an unsupported locale returns a
- * non-localized instance
- *
- * @throws IOException
- */
- @Test
- public void getNonLocalizedInstance() throws IOException {
- HttpClient client = new HttpClient();
- PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
- + WIDGET_ID_LOCALIZED
- + "&userid=localetest2&shareddatakey=localetest2&locale=bu");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- assertFalse(post.getResponseBodyAsString().contains("locales/fr/index.htm"));
- assertFalse(post.getResponseBodyAsString().contains("locales/en/index.htm"));
- assertTrue(post.getResponseBodyAsString().contains("index.htm"));
- assertTrue(post.getResponseBodyAsString().contains("locale test"));
- assertFalse(post.getResponseBodyAsString().contains(
- "tester les paramètres régionaux"));
- post.releaseConnection();
- }
-
- /**
- * Test that requesting an instance with no locale property returns the
- * instance for the default locale
- *
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getDefaultLocalizedInstance() 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_LOCALIZED + "&userid=localetest3&shareddatakey=localetest3");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- assertTrue(post.getResponseBodyAsString().contains("locales/en/index.htm"));
- assertTrue(post.getResponseBodyAsString().contains("locale test"));
- assertFalse(post.getResponseBodyAsString().contains(
- "tester les paramètres régionaux"));
- post.releaseConnection();
- }
-
- /**
- * Tests we can create an instance using instance params and widget id and get
- * a 201 response
- *
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getInstanceById() 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=test");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- post.releaseConnection();
- }
-
- /**
- * Tests that getting an existing instance returns 200 rather than 201
- *
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getInstanceById_AlreadyExists() 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=test");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- post.releaseConnection();
- }
-
- /**
- * Tests that a request for an instance with an invalid API key is rejected
- *
- * @throws HttpException
- * @throws IOException
- */
- @Test
- public void getInstance_InvalidAPIkey() throws HttpException, IOException {
- HttpClient client = new HttpClient();
- PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- post.setQueryString("api_key=" + API_KEY_INVALID + "&widgetid="
- + WIDGET_ID_VALID + "&userid=test&shareddatakey=test");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(403, code);
- post.releaseConnection();
- }
-
- /**
- * Tests that we get a 404 when requesting an instance for a non-installed
- * widget, even though we still get the representation of the
- * "unsupported widget widget"
- *
- * @throws HttpException
- * @throws IOException
- */
- @Test
- public void getInstanceById_InvalidWidget() 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_INVALID + "&userid=test&shareddatakey=test");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(404, code); // but must return the "default widget"
- assertTrue(post.getResponseBodyAsString().contains(
- "Unsupported widget widget"));
- post.releaseConnection();
- }
-
- /**
- * Test for stop() extension feature. The feature itself may be removed in
- * future releases.
- */
- @Test
- @Ignore
- public void stop() {
- fail("test not written");
- }
-
- /**
- * Test for resume() extension feature. The feature itself may be removed in
- * future releases.
- */
- @Test
- @Ignore
- public void resume() {
- fail("test not written");
- }
-
- /**
- * Tests that we can clone an instance
- *
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void cloneSharedData() throws HttpException, IOException {
- //
- // Create an instance using POST
- //
- 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=clonetestsrc");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- post.releaseConnection();
-
- //
- // Set some shared data
- //
- client = new HttpClient();
- 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=clonetestsrc&propertyname=cat&propertyvalue=garfield");
- client.executeMethod(post);
- code = post.getStatusCode();
- assertEquals(201, code);
- post.releaseConnection();
-
- //
- // Clone it using PUT
- //
- client = new HttpClient();
- PutMethod put = new PutMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- put.setQueryString("api_key="
- + API_KEY_VALID
- + "&widgetid="
- + WIDGET_ID_VALID
- + "&userid=test&shareddatakey=clonetestsrc&requestid=clone&cloneshareddatakey=clonetestsync");
- client.executeMethod(put);
- code = put.getStatusCode();
- assertEquals(200, code);
- put.releaseConnection();
-
- //
- // Create an instance for the clone
- //
- client = new HttpClient();
- post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
- post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
- + WIDGET_ID_VALID + "&userid=test&shareddatakey=clonetestsync");
- client.executeMethod(post);
- code = post.getStatusCode();
- assertEquals(200, code);
- post.releaseConnection();
-
- //
- // Get the data for the clone and check it is the same set for the original
- //
- 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=clonetestsync&propertyname=cat");
- client.executeMethod(get);
- code = get.getStatusCode();
- assertEquals(200, code);
- String resp = get.getResponseBodyAsString();
- assertEquals("garfield", resp);
- post.releaseConnection();
- }
+ @BeforeClass
+ public static void setup() throws HttpException, IOException {
+
+ //
+ // Setup POST method
+ //
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+
+ //
+ // Get the locale test widget
+ //
+ File file = new File("src/test/resources/localetest.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 201 (Created)
+ //
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(201,code);
+ }
+
+ @AfterClass
+ public static void tearDown() throws HttpException, IOException{
+ Request delete;
+
+ delete = new Request("DELETE", TEST_INSTANCES_SERVICE_URL_VALID + "?api_key=" + API_KEY_VALID + "&widgetid="
+ + WIDGET_ID_VALID + "&userid=test&shareddatakey=test");
+ delete.execute(true, false);
+
+ delete = new Request("DELETE", TEST_INSTANCES_SERVICE_URL_VALID + "?api_key=" + API_KEY_VALID + "&widgetid="
+ + WIDGET_ID_VALID + "&userid=test&shareddatakey=clonetestsrc");
+ delete.execute(true, false);
+
+ delete = new Request("DELETE", TEST_INSTANCES_SERVICE_URL_VALID + "?api_key=" + API_KEY_VALID + "&widgetid="
+ + WIDGET_ID_VALID + "&userid=test&shareddatakey=clonetestsync");
+ delete.execute(true, false);
+
+ delete = new Request("DELETE", TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_LOCALIZED);
+ delete.execute(true, false);
+ }
+
+ /**
+ * Test we can get an instance localized using the locale parameter
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getLocalizedInstance() 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_LOCALIZED
+ + "&userid=localetest&shareddatakey=localetest&locale=fr");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(200, code);
+ assertTrue(post.getResponseBodyAsString().contains("locales/fr/index.htm"));
+ assertTrue(post.getResponseBodyAsString().contains(
+ "tester les paramètres régionaux"));
+ TEST_ID_KEY = post.getResponseBodyAsString().substring(
+ post.getResponseBodyAsString().indexOf("<identifier>") + 12,
+ post.getResponseBodyAsString().indexOf("</identifier>"));
+ post.releaseConnection();
+ }
+
+ /**
+ * Test we can get an existing instance using instance parameters - widgetid,
+ * apikey, userid, shareddatakey, locale
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ @Test
+ public void getExistingInstanceByInstanceParams() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+ get.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+ + WIDGET_ID_LOCALIZED
+ + "&userid=localetest&shareddatakey=localetest&locale=fr");
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200, code);
+ assertTrue(get.getResponseBodyAsString().contains("locales/fr/index.htm"));
+ assertTrue(get.getResponseBodyAsString().contains(
+ "tester les paramètres régionaux"));
+ }
+
+ /**
+ * Test we can get an existing instance using just the id_key parameter
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getExistingInstanceByIdKey() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+ get.setQueryString("api_key=" + API_KEY_VALID + "&idkey=" + TEST_ID_KEY);
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200, code);
+ System.out.println(get.getResponseBodyAsString());
+ assertTrue(get.getResponseBodyAsString().contains("locales/fr/index.htm"));
+ assertTrue(get.getResponseBodyAsString().contains(
+ "tester les paramètres régionaux"));
+ }
+
+ /**
+ * Test we can get an existing instance using the id_key as a resource path
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getExistingInstanceByIdResource() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_INSTANCES_SERVICE_URL_VALID + "/"
+ + TEST_ID_KEY);
+ get.setQueryString("api_key=" + API_KEY_VALID);
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200, code);
+ assertTrue(get.getResponseBodyAsString().contains("locales/fr/index.htm"));
+ assertTrue(get.getResponseBodyAsString().contains(
+ "tester les paramètres régionaux"));
+ }
+
+ /**
+ * Test that instance localization includes support for fallback locales -
+ * e.g. specifying "early modern french" locale returns standard FR start file
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getGracefulLocalizedInstance() 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_LOCALIZED
+ + "&userid=localetest1b&shareddatakey=localetest1b&locale=fr-1694acad");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(200, code);
+ assertTrue(post.getResponseBodyAsString().contains("locales/fr/index.htm"));
+ assertFalse(post.getResponseBodyAsString().contains("locale test"));
+ assertTrue(post.getResponseBodyAsString().contains(
+ "tester les paramètres régionaux"));
+ post.releaseConnection();
+ }
+
+ /**
+ * Tests that requesting an instance for an unsupported locale returns a
+ * non-localized instance
+ *
+ * @throws IOException
+ */
+ @Test
+ public void getNonLocalizedInstance() throws IOException {
+ HttpClient client = new HttpClient();
+ PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+ post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+ + WIDGET_ID_LOCALIZED
+ + "&userid=localetest2&shareddatakey=localetest2&locale=bu");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(200, code);
+ assertFalse(post.getResponseBodyAsString().contains("locales/fr/index.htm"));
+ assertFalse(post.getResponseBodyAsString().contains("locales/en/index.htm"));
+ assertTrue(post.getResponseBodyAsString().contains("index.htm"));
+ assertTrue(post.getResponseBodyAsString().contains("locale test"));
+ assertFalse(post.getResponseBodyAsString().contains(
+ "tester les paramètres régionaux"));
+ post.releaseConnection();
+ }
+
+ /**
+ * Test that requesting an instance with no locale property returns the
+ * instance for the default locale
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getDefaultLocalizedInstance() 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_LOCALIZED + "&userid=localetest3&shareddatakey=localetest3");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(200, code);
+ assertTrue(post.getResponseBodyAsString().contains("locales/en/index.htm"));
+ assertTrue(post.getResponseBodyAsString().contains("locale test"));
+ assertFalse(post.getResponseBodyAsString().contains(
+ "tester les paramètres régionaux"));
+ post.releaseConnection();
+ }
+
+ /**
+ * Tests we can create an instance using instance params and widget id and get
+ * a 201 response
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getInstanceById() 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=test");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(200, code);
+ post.releaseConnection();
+ }
+
+ /**
+ * Tests that getting an existing instance returns 200 rather than 201
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getInstanceById_AlreadyExists() 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=test");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(200, code);
+ post.releaseConnection();
+ }
+
+ /**
+ * Tests that a request for an instance with an invalid API key is rejected
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ @Test
+ public void getInstance_InvalidAPIkey() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+ PostMethod post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+ post.setQueryString("api_key=" + API_KEY_INVALID + "&widgetid="
+ + WIDGET_ID_VALID + "&userid=test&shareddatakey=test");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(403, code);
+ post.releaseConnection();
+ }
+
+ /**
+ * Tests that we get a 404 when requesting an instance for a non-installed
+ * widget, even though we still get the representation of the
+ * "unsupported widget widget"
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ @Test
+ public void getInstanceById_InvalidWidget() 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_INVALID + "&userid=test&shareddatakey=test");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(404, code); // but must return the "default widget"
+ assertTrue(post.getResponseBodyAsString().contains(
+ "Unsupported widget widget"));
+ post.releaseConnection();
+ }
+
+ /**
+ * Test for stop() extension feature. The feature itself may be removed in
+ * future releases.
+ */
+ @Test
+ @Ignore
+ public void stop() {
+ fail("test not written");
+ }
+
+ /**
+ * Test for resume() extension feature. The feature itself may be removed in
+ * future releases.
+ */
+ @Test
+ @Ignore
+ public void resume() {
+ fail("test not written");
+ }
+
+ /**
+ * Tests that we can clone an instance
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void cloneSharedData() throws HttpException, IOException {
+ //
+ // Create an instance using POST
+ //
+ 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=clonetestsrc");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(200, code);
+ post.releaseConnection();
+
+ //
+ // Set some shared data
+ //
+ client = new HttpClient();
+ 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=clonetestsrc&propertyname=cat&propertyvalue=garfield");
+ client.executeMethod(post);
+ code = post.getStatusCode();
+ assertEquals(201, code);
+ post.releaseConnection();
+
+ //
+ // Clone it using PUT
+ //
+ client = new HttpClient();
+ PutMethod put = new PutMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+ put.setQueryString("api_key="
+ + API_KEY_VALID
+ + "&widgetid="
+ + WIDGET_ID_VALID
+ + "&userid=test&shareddatakey=clonetestsrc&requestid=clone&cloneshareddatakey=clonetestsync");
+ client.executeMethod(put);
+ code = put.getStatusCode();
+ assertEquals(200, code);
+ put.releaseConnection();
+
+ //
+ // Create an instance for the clone
+ //
+ client = new HttpClient();
+ post = new PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
+ post.setQueryString("api_key=" + API_KEY_VALID + "&widgetid="
+ + WIDGET_ID_VALID + "&userid=test&shareddatakey=clonetestsync");
+ client.executeMethod(post);
+ code = post.getStatusCode();
+ assertEquals(200, code);
+ post.releaseConnection();
+
+ //
+ // Get the data for the clone and check it is the same set for the original
+ //
+ 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=clonetestsync&propertyname=cat");
+ client.executeMethod(get);
+ code = get.getStatusCode();
+ assertEquals(200, code);
+ String resp = get.getResponseBodyAsString();
+ assertEquals("garfield", resp);
+ post.releaseConnection();
+ }
}
Modified: wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/WidgetsControllerTest.java
URL: http://svn.apache.org/viewvc/wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/WidgetsControllerTest.java?rev=1569676&r1=1569675&r2=1569676&view=diff
==============================================================================
--- wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/WidgetsControllerTest.java (original)
+++ wookie/trunk/wookie-server/src/test/java/org/apache/wookie/tests/functional/WidgetsControllerTest.java Wed Feb 19 10:49:01 2014
@@ -29,11 +29,11 @@ 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;
import org.apache.commons.io.FileUtils;
+import org.apache.wookie.tests.helpers.Request;
import org.apache.wookie.w3c.W3CWidgetFactory;
import org.apache.wookie.w3c.exceptions.BadManifestException;
import org.apache.wookie.w3c.exceptions.BadWidgetZipFileException;
@@ -50,33 +50,33 @@ import org.junit.Test;
*/
public class WidgetsControllerTest extends AbstractControllerTest {
- private static String WIDGET_ID_ACCESS_TEST = "http://wookie.apache.org/widgets/access-test";
- private static String WIDGET_ID_DELETE_TEST = "http://deletetest";
- private static String WIDGET_ID_NOT_SUPPORTED = "http://notsupported";
- private static String WIDGET_ID_UPLOAD_TEST = "http://uploadtest";
- private static String WIDGET_ID_UPLOAD_POLICIES_TEST = "http://uploadtest/policies";
- private static String WIDGET_ID_UPLOAD_TEST_2 = "http://uploadtest_2";
- private static String WIDGET_ID_WEATHER = "http://www.getwookie.org/widgets/weather";
-
- @AfterClass
- public static void tearDown() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- setAuthenticationCredentials(client);
- DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_ACCESS_TEST));
- client.executeMethod(delete);
- delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
- client.executeMethod(delete);
- delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST_2));
- client.executeMethod(delete);
- delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_POLICIES_TEST));
- client.executeMethod(delete);
- }
-
- /*
- * Utility method for creating a temp directory
- * @return a new temp directory
- * @throws IOException
- */
+ private static String WIDGET_ID_ACCESS_TEST = "http://wookie.apache.org/widgets/access-test";
+ private static String WIDGET_ID_DELETE_TEST = "http://deletetest";
+ private static String WIDGET_ID_NOT_SUPPORTED = "http://notsupported";
+ private static String WIDGET_ID_UPLOAD_TEST = "http://uploadtest";
+ private static String WIDGET_ID_UPLOAD_POLICIES_TEST = "http://uploadtest/policies";
+ private static String WIDGET_ID_UPLOAD_TEST_2 = "http://uploadtest_2";
+ private static String WIDGET_ID_WEATHER = "http://www.getwookie.org/widgets/weather";
+
+ @AfterClass
+ public static void tearDown() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ setAuthenticationCredentials(client);
+ DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_ACCESS_TEST));
+ client.executeMethod(delete);
+ delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
+ client.executeMethod(delete);
+ delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST_2));
+ client.executeMethod(delete);
+ delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_POLICIES_TEST));
+ client.executeMethod(delete);
+ }
+
+ /*
+ * Utility method for creating a temp directory
+ * @return a new temp directory
+ * @throws IOException
+ */
public static File createTempDirectory() throws IOException {
final File temp;
@@ -95,792 +95,707 @@ public class WidgetsControllerTest exten
return (temp);
}
- /**
- * Test GET all widgets
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getAllWidgets() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID);
- get.setQueryString("all=true");
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200,code);
- String response = get.getResponseBodyAsString();
- assertTrue(response.contains("<widget id=\"http://notsupported\""));
- get.releaseConnection();
- }
-
- /**
- * Test we can GET a widget using its URI as a resource path
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getSpecificWidgetByUri() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_NOT_SUPPORTED));
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200,code);
- String response = get.getResponseBodyAsString();
- assertTrue(response.contains("<widget id=\"http://notsupported\""));
- get.releaseConnection();
- }
- /**
- * Test we can GET a widget using its URI as a resource path
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getSpecificWidgetByUri2() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_NOT_SUPPORTED);
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200,code);
- String response = get.getResponseBodyAsString();
- assertTrue(response.contains("<widget id=\"http://notsupported\""));
- get.releaseConnection();
- }
-
- /**
- * Test that a request for a non-existing widget ID gets a 404
- * @throws IOException
- * @throws HttpException
- */
- @Test
- public void getSpecificWidget_nonexisting() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/9999");
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(404,code);
- get.releaseConnection();
- }
-
- @Test
- public void importWidget_unauthorized() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(401,code);
- post.releaseConnection();
- }
-
- @Test
- public void importWidget() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
-
- //
- // Use upload test widget
- //
- File file = new File("src/test/resources/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 201 (Created)
- //
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
-
- //
- // Cleanup
- //
- DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
- client.executeMethod(delete);
- }
-
- /**
- * Test that we can import widgets using POST that contain
- * access requests. See WOOKIE-379.
- *
- * @throws HttpException
- * @throws IOException
- * @throws JDOMException
- */
- @Test
- public void importWidgetWithAccessPolicies() throws HttpException, IOException, JDOMException{
-
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
-
- //
- // Use upload test widget
- //
- File file = new File("src/test/resources/upload-policies-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 201 (Created)
- //
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
-
- //
- // Check the policy was created
- //
- GetMethod get = new GetMethod(TEST_POLICIES_SERVICE_URL_VALID + "/" + WIDGET_ID_UPLOAD_POLICIES_TEST);
- get.setRequestHeader("Accept", "text/xml");
- client.executeMethod(get);
-
- SAXBuilder builder = new SAXBuilder();
- Document doc = builder.build(get.getResponseBodyAsStream());
- Element policies = doc.getRootElement();
- assertEquals(1, policies.getChildren("policy").size());
- Element policy = policies.getChild("policy");
- assertEquals(WIDGET_ID_UPLOAD_POLICIES_TEST, policy.getAttributeValue("scope"));
- assertEquals("*", policy.getAttributeValue("origin"));
- assertEquals("ALLOW", policy.getAttributeValue("directive"));
-
- //
- // Cleanup
- //
- DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_POLICIES_TEST));
- client.executeMethod(delete);
- }
-
-
- @Test
- public void importWidgetWithDefaultIcon() throws HttpException, IOException, JDOMException{
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
-
- //
- // Use upload test widget
- //
- File file = new File("src/test/resources/upload-test-2.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 201 (Created)
- //
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- //
- // Lets take a look at the metadata...
- //
- SAXBuilder builder = new SAXBuilder();
- Document doc = builder.build(post.getResponseBodyAsStream());
- Element iconElement = doc.getRootElement().getChild("icon", doc.getRootElement().getNamespace());
- assertNotNull(iconElement);
- assertEquals("http://localhost:8080/wookie/deploy/uploadtest_2/icon.png", iconElement.getAttributeValue("src"));
- post.releaseConnection();
- }
-
-
- @Test
- public void downloadWidgetPackage() throws BadWidgetZipFileException, BadManifestException, Exception{
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_NOT_SUPPORTED);
- get.setRequestHeader("accept", "application/widget");
- get.setFollowRedirects(true);
- client.executeMethod(get);
-
- assertEquals(200, get.getStatusCode());
-
- File file = File.createTempFile("wookie", ".wgt");
- FileUtils.writeByteArrayToFile(file, get.getResponseBody());
-
- System.out.println(get.getStatusCode());
-
-
- File outputFolder = File.createTempFile("temp", Long.toString(System.nanoTime()));
- outputFolder.delete();
- outputFolder.mkdir();
-
- System.out.println(outputFolder.getPath());
-
- W3CWidgetFactory fac = new W3CWidgetFactory();
- fac.setOutputDirectory(outputFolder.getPath());
- W3CWidget widget = fac.parse(file);
- assertEquals("Unsupported widget widget", widget.getLocalName("en"));
- }
-
- @Test
- public void importWrongFileType() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
-
- //
- // We'll use a copy of the unsupported widget widget for testing
- //
- File file = new File("src/test/resources/not_a_widget.zip");
- 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 a 400
- //
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(400,code);
- post.releaseConnection();
- }
-
- /**
- * Test to ensure that, given two widgets with identical filenames, the second uploaded file
- * does not overwrite the first when it comes to downloading the widget. See WOOKIE-402
- *
- * @throws BadWidgetZipFileException
- * @throws BadManifestException
- * @throws Exception
- */
- @Test
- public void importWidgetSameFilename() throws BadWidgetZipFileException, BadManifestException, Exception{
-
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
-
- //
- // Use upload test widget
- //
- File uploadFile = new File("src/test/resources/upload-test.wgt");
- assertTrue(uploadFile.exists());
-
- //
- // Add test wgt file to POST
- //
- Part[] parts = { new FilePart(uploadFile.getName(), uploadFile) };
- post.setRequestEntity(new MultipartRequestEntity(parts, post
- .getParams()));
-
- //
- // POST the file to /widgets and check we get 201 (Created)
- //
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
-
- //
- // Now, upload a new widget, with the same filename
- // We'll use the WARP widget
- //
- uploadFile = new File("src/test/resources/upload-policies-test.wgt");
- assertTrue(uploadFile.exists());
- File tempFolder = createTempDirectory();
- File newFile = new File(tempFolder+File.separator+"upload-test.wgt");
- FileUtils.copyFile(uploadFile, newFile);
- assertTrue(newFile.exists());
-
- System.out.println(newFile.getPath());
-
- //
- // Add test wgt file to POST
- //
- post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
- Part[] newParts = { new FilePart(newFile.getName(), newFile) };
- post.setRequestEntity(new MultipartRequestEntity(newParts, post
- .getParams()));
-
- //
- // POST the file to /widgets and check we get 201 (Created)
- //
- client.executeMethod(post);
- code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
-
- //
- // Now lets try to download the first test widget; this should not
- // have been overwritten by the second one we uploaded, even though we
- // gave it the same file name
- //
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/"+"http://uploadtest");
- get.setRequestHeader("Accept","application/widget");
- client.executeMethod(get);
- code = get.getStatusCode();
- assertEquals(200,code);
-
- InputStream inputStream = get.getResponseBodyAsStream();
- tempFolder = createTempDirectory();
- File downloadedFile = new File(tempFolder + File.separator + "downloadedTestWidget.zip");
- OutputStream out = new FileOutputStream(downloadedFile);
-
- int read = 0;
- byte[] bytes = new byte[1024];
-
- while ((read = inputStream.read(bytes)) != -1) {
- out.write(bytes, 0, read);
- }
- inputStream.close();
- out.flush();
- out.close();
- get.releaseConnection();
-
- //
- // check the downloaded file
- //
- W3CWidgetFactory fac = new W3CWidgetFactory();
- fac.setStartPageProcessor(null);
- File outputDir = new File("src/test" + File.separatorChar + "resources" + File.separatorChar + "widgets");
- outputDir.mkdirs();
- fac.setOutputDirectory("src/test" + File.separatorChar + "resources" + File.separatorChar + "widgets");
- W3CWidget widget = fac.parse(downloadedFile);
-
- //
- // Cleanup
- //
- FileUtils.deleteQuietly(downloadedFile);
- FileUtils.deleteQuietly(tempFolder);
- FileUtils.deleteQuietly(newFile);
- DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
- client.executeMethod(delete);
- delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_POLICIES_TEST));
- client.executeMethod(delete);
-
- assertEquals("http://uploadtest", widget.getIdentifier());
-
-
- }
-
- @Test
- public void deleteWidgetUnauthorized() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_WEATHER);
- client.executeMethod(delete);
- assertEquals(401, delete.getStatusCode());
-
- //
- // Check it wasn't deleted
- //
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_WEATHER);
- client.executeMethod(get);
- assertEquals(200, get.getStatusCode());
- }
-
- @Test
- public void deleteWidgetNonexisting() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
- 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
- //
- setAuthenticationCredentials(client);
-
- //
- // Upload widget we'll test deleting next
- //
- File file = new File("src/test/resources/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 + encodeString("/" + WIDGET_ID_DELETE_TEST));
- client.executeMethod(delete);
- assertEquals(200, delete.getStatusCode());
-
- //
- // Check it was deleted
- //
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_DELETE_TEST));
- client.executeMethod(get);
- assertEquals(404, get.getStatusCode());
- }
-
- /**
- * We allow updates to existing widgets via POST as well as PUT
- * (to allow browsers to update using forms)
- * @throws HttpException
- * @throws IOException
- */
- @Test
- public void updateWidgetByPost() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- //
- // Use upload test widget
- //
- File file = new File("src/test/resources/upload-test.wgt");
- assertTrue(file.exists());
-
- //
- // Add test wgt file to POST
- //
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
- Part[] parts = { new FilePart(file.getName(), file) };
- post.setRequestEntity(new MultipartRequestEntity(parts, post
- .getParams()));
-
- //
- // POST the file to /widgets and check we get 201 (Created)
- //
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
-
- //
- // Now lets try updating
- //
- post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
-
- //
- // Use upload test widget
- //
- file = new File("src/test/resources/upload-test.wgt");
- assertTrue(file.exists());
-
- //
- // Add test wgt file to POST
- //
- Part[] newParts = { new FilePart(file.getName(), file) };
- post.setRequestEntity(new MultipartRequestEntity(newParts, post
- .getParams()));
-
- //
- // POST the file to /widgets and check we get 200 (Updated)
- //
- client.executeMethod(post);
- code = post.getStatusCode();
- assertEquals(200,code);
- post.releaseConnection();
-
- }
-
- @Test
- public void updateWidgetByPut() throws HttpException, IOException{
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- PutMethod put = new PutMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
-
- //
- // Use upload test widget
- //
- File file = new File("src/test/resources/upload-test.wgt");
- assertTrue(file.exists());
-
- //
- // Add test wgt file to PUT
- //
- Part[] parts = { new FilePart(file.getName(), file) };
- put.setRequestEntity(new MultipartRequestEntity(parts, put
- .getParams()));
-
- //
- // PUT the file to /widgets and check we get 200 (Updated)
- //
- client.executeMethod(put);
- int code = put.getStatusCode();
- assertEquals(200,code);
- put.releaseConnection();
-
- }
-
- @Test
- public void updateWidgetUnauthorized() throws HttpException, IOException{
- HttpClient client = new HttpClient();
-
- PutMethod post = new PutMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
-
- //
- // Use upload test widget
- //
- File file = new File("src/test/resources/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 + encodeString("/" + WIDGET_ID_INVALID));
-
- //
- // Use upload test widget
- //
- File file = new File("src/test/resources/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
- * @throws IOException
- * @throws InterruptedException
- * @throws JDOMException
- */
- @Test
- public void checkForDuplicateAccessRequests() throws HttpException, IOException, InterruptedException, JDOMException{
-
- //
- // Add the test widget, and update it a few times
- //
- for (int i=0;i<4;i++){
-
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
-
- //
- // Add the access test widget. This just has a single access request
- // for the origin "http://accesstest.incubator.apache.org"
- //
- File file = new File("src/test/resources/access-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
- //
- client.executeMethod(post);
- post.releaseConnection();
- }
-
- //
- // Check that we only have one copy of the access request, not two
- //
- HttpClient client = new HttpClient();
- setAuthenticationCredentials(client);
- GetMethod get = new GetMethod(TEST_POLICIES_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_ACCESS_TEST));
- //this needs to be Accept rather than accepts which fails on tomcat
- get.setRequestHeader("Accept","text/xml");
- client.executeMethod(get);
- assertEquals(1,PoliciesControllerTest.processPolicies(get.getResponseBodyAsStream()).getChildren("policy").size());
-
- }
-
- @Test
- public void workingWithUnidentifiedWidgets() throws HttpException, IOException, JDOMException{
- HttpClient client = new HttpClient();
- //
- // Use admin credentials
- //
- setAuthenticationCredentials(client);
-
- PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
-
- //
- // Add the access test widget. This just has a single access request
- // for the origin "http://accesstest.incubator.apache.org"
- //
- File file = new File("src/test/resources/upload-test-noid.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
- //
- client.executeMethod(post);
- SAXBuilder builder = new SAXBuilder();
- Document doc = builder.build(post.getResponseBodyAsStream());
- String id = doc.getRootElement().getAttributeValue("id");
- post.releaseConnection();
-
- //
- // Now we'll update it
- //
- PutMethod put = new PutMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + id));
-
- //
- // Add test wgt file to PUT
- //
- Part[] updateparts = { new FilePart(file.getName(), file) };
- put.setRequestEntity(new MultipartRequestEntity(updateparts, put
- .getParams()));
-
- //
- // PUT the file to /widgets and check we get 200 (Updated)
- //
- client.executeMethod(put);
- int code = put.getStatusCode();
- assertEquals(200,code);
- put.releaseConnection();
-
- //
- // DELETE the widget
- //
- DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + id));
- client.executeMethod(delete);
- }
-
- /**
- * Download a widget using the Accept type of ("Accept","application/widget")
- * Once downloaded make sure that the widget package is correct - run it through the parser
- * @throws Exception
- */
- @Test
- public void getActualWidget() throws Exception{
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/"+WIDGET_ID_WEATHER);
- get.setRequestHeader("Accept","application/widget");
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200,code);
-
- InputStream inputStream = get.getResponseBodyAsStream();
- File downloadedFile = new File("src"+ File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar + "downloadedWeatherWidget.zip");
- OutputStream out = new FileOutputStream(downloadedFile);
-
- int read = 0;
- byte[] bytes = new byte[1024];
-
- while ((read = inputStream.read(bytes)) != -1) {
- out.write(bytes, 0, read);
- }
- inputStream.close();
- out.flush();
- out.close();
- get.releaseConnection();
-
- // check the downloaded file
- W3CWidgetFactory fac = new W3CWidgetFactory();
- fac.setStartPageProcessor(null);
- File outputDir = new File("src"+File.separatorChar+"test" + File.separatorChar + "resources" + File.separatorChar + "widgets");
- outputDir.mkdirs();
- fac.setOutputDirectory("src" + File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar + "widgets");
- W3CWidget widget = fac.parse(downloadedFile);
- File file = fac.getUnzippedWidgetDirectory();
- assertEquals("src" + File.separatorChar + "test" + File.separatorChar + "resources"
- + File.separatorChar + "widgets" + File.separatorChar
- + "www.getwookie.org" + File.separatorChar + "widgets"
- + File.separatorChar + "weather", file.getPath());
- assertTrue(file.isDirectory());
- assertEquals(WIDGET_ID_WEATHER, widget.getIdentifier());
-
- //clean up
- FileUtils.deleteDirectory(outputDir);
- FileUtils.deleteQuietly(downloadedFile);
- }
+ /**
+ * Test GET all widgets
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getAllWidgets() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID);
+ get.setQueryString("all=true");
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200,code);
+ String response = get.getResponseBodyAsString();
+ assertTrue(response.contains("<widget id=\"http://notsupported\""));
+ get.releaseConnection();
+ }
+
+ /**
+ * Test we can GET a widget using its URI as a resource path
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getSpecificWidgetByUri() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_NOT_SUPPORTED));
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200,code);
+ String response = get.getResponseBodyAsString();
+ assertTrue(response.contains("<widget id=\"http://notsupported\""));
+ get.releaseConnection();
+ }
+ /**
+ * Test we can GET a widget using its URI as a resource path
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getSpecificWidgetByUri2() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_NOT_SUPPORTED);
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200,code);
+ String response = get.getResponseBodyAsString();
+ assertTrue(response.contains("<widget id=\"http://notsupported\""));
+ get.releaseConnection();
+ }
+
+ /**
+ * Test that a request for a non-existing widget ID gets a 404
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void getSpecificWidget_nonexisting() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/9999");
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(404,code);
+ get.releaseConnection();
+ }
+
+ @Test
+ public void importWidget_unauthorized() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ PostMethod post = new PostMethod(TEST_WIDGETS_SERVICE_URL_VALID);
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(403,code);
+ post.releaseConnection();
+ }
+
+ @Test
+ public void importWidget() throws HttpException, IOException{
+
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src/test/resources/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.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 201 (Created)
+ //
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(201,code);
+
+ //
+ // Cleanup
+ //
+ Request delete = new Request("DELETE", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
+ delete.execute(true, false);
+ }
+
+ /**
+ * Test that we can import widgets using POST that contain
+ * access requests. See WOOKIE-379.
+ *
+ * @throws HttpException
+ * @throws IOException
+ * @throws JDOMException
+ */
+ @Test
+ public void importWidgetWithAccessPolicies() throws HttpException, IOException, JDOMException{
+
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src/test/resources/upload-policies-test.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 201 (Created)
+ //
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(201,code);
+
+ //
+ // Check the policy was created
+ //
+ Request get = new Request("GET", TEST_POLICIES_SERVICE_URL_VALID + "/" + WIDGET_ID_UPLOAD_POLICIES_TEST);
+ get.setAccepts("text/xml");
+ get.execute(true, false);
+
+ SAXBuilder builder = new SAXBuilder();
+ Document doc = builder.build(get.getResponseBodyAsStream());
+ Element policies = doc.getRootElement();
+ assertEquals(1, policies.getChildren("policy").size());
+ Element policy = policies.getChild("policy");
+ assertEquals(WIDGET_ID_UPLOAD_POLICIES_TEST, policy.getAttributeValue("scope"));
+ assertEquals("*", policy.getAttributeValue("origin"));
+ assertEquals("ALLOW", policy.getAttributeValue("directive"));
+
+ //
+ // Cleanup
+ //
+ Request delete = new Request("DELETE", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_POLICIES_TEST));
+ delete.execute(true, false);
+ }
+
+
+ @Test
+ public void importWidgetWithDefaultIcon() throws HttpException, IOException, JDOMException{
+
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src/test/resources/upload-test-2.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 201 (Created)
+ //
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(201,code);
+ //
+ // Lets take a look at the metadata...
+ //
+ SAXBuilder builder = new SAXBuilder();
+ Document doc = builder.build(post.getResponseBodyAsStream());
+ Element iconElement = doc.getRootElement().getChild("icon", doc.getRootElement().getNamespace());
+ assertNotNull(iconElement);
+ assertEquals("http://localhost:8080/wookie/deploy/uploadtest_2/icon.png", iconElement.getAttributeValue("src"));
+ }
+
+
+ @Test
+ public void downloadWidgetPackage() throws BadWidgetZipFileException, BadManifestException, Exception{
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_NOT_SUPPORTED);
+ get.setRequestHeader("accept", "application/widget");
+ get.setFollowRedirects(true);
+ client.executeMethod(get);
+
+ assertEquals(200, get.getStatusCode());
+
+ File file = File.createTempFile("wookie", ".wgt");
+ FileUtils.writeByteArrayToFile(file, get.getResponseBody());
+
+ System.out.println(get.getStatusCode());
+
+
+ File outputFolder = File.createTempFile("temp", Long.toString(System.nanoTime()));
+ outputFolder.delete();
+ outputFolder.mkdir();
+
+ System.out.println(outputFolder.getPath());
+
+ W3CWidgetFactory fac = new W3CWidgetFactory();
+ fac.setOutputDirectory(outputFolder.getPath());
+ W3CWidget widget = fac.parse(file);
+ assertEquals("Unsupported widget widget", widget.getLocalName("en"));
+ }
+
+ @Test
+ public void importWrongFileType() throws HttpException, IOException{
+
+ Request post = new Request("POST",TEST_WIDGETS_SERVICE_URL_VALID);
+
+ //
+ // We'll use a copy of the unsupported widget widget for testing
+ //
+ File file = new File("src/test/resources/not_a_widget.zip");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get a 400
+ //
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(400,code);
+ }
+
+ /**
+ * Test to ensure that, given two widgets with identical filenames, the second uploaded file
+ * does not overwrite the first when it comes to downloading the widget. See WOOKIE-402
+ *
+ * @throws BadWidgetZipFileException
+ * @throws BadManifestException
+ * @throws Exception
+ */
+ @Test
+ public void importWidgetSameFilename() throws BadWidgetZipFileException, BadManifestException, Exception{
+
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+
+ //
+ // Use upload test widget
+ //
+ File uploadFile = new File("src/test/resources/upload-test.wgt");
+ assertTrue(uploadFile.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(uploadFile.getName(), uploadFile) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 201 (Created)
+ //
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(201,code);
+
+ //
+ // Now, upload a new widget, with the same filename
+ // We'll use the WARP widget
+ //
+ uploadFile = new File("src/test/resources/upload-policies-test.wgt");
+ assertTrue(uploadFile.exists());
+ File tempFolder = createTempDirectory();
+ File newFile = new File(tempFolder+File.separator+"upload-test.wgt");
+ FileUtils.copyFile(uploadFile, newFile);
+ assertTrue(newFile.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+ Part[] newParts = { new FilePart(newFile.getName(), newFile) };
+ post.setRequestEntity(new MultipartRequestEntity(newParts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 201 (Created)
+ //
+ post.execute(true, false);
+ code = post.getStatusCode();
+ assertEquals(201,code);
+
+ //
+ // Now lets try to download the first test widget; this should not
+ // have been overwritten by the second one we uploaded, even though we
+ // gave it the same file name
+ //
+ Request get = new Request("GET", TEST_WIDGETS_SERVICE_URL_VALID+"/"+"http://uploadtest");
+ get.setAccepts("application/widget");
+ get.execute(false, false);
+ code = get.getStatusCode();
+ assertEquals(200,code);
+
+ InputStream inputStream = get.getResponseBodyAsStream();
+ tempFolder = createTempDirectory();
+ File downloadedFile = new File(tempFolder + File.separator + "downloadedTestWidget.zip");
+ OutputStream out = new FileOutputStream(downloadedFile);
+
+ int read = 0;
+ byte[] bytes = new byte[1024];
+
+ while ((read = inputStream.read(bytes)) != -1) {
+ out.write(bytes, 0, read);
+ }
+ inputStream.close();
+ out.flush();
+ out.close();
+
+ //
+ // check the downloaded file
+ //
+ W3CWidgetFactory fac = new W3CWidgetFactory();
+ fac.setStartPageProcessor(null);
+ File outputDir = new File("src/test" + File.separatorChar + "resources" + File.separatorChar + "widgets");
+ outputDir.mkdirs();
+ fac.setOutputDirectory("src/test" + File.separatorChar + "resources" + File.separatorChar + "widgets");
+ W3CWidget widget = fac.parse(downloadedFile);
+
+ //
+ // Cleanup
+ //
+ FileUtils.deleteQuietly(downloadedFile);
+ FileUtils.deleteQuietly(tempFolder);
+ FileUtils.deleteQuietly(newFile);
+ Request delete = new Request("DELETE", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
+ delete.execute(true, false);
+ delete = new Request("DELETE", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_POLICIES_TEST));
+ delete.execute(true, false);
+
+ assertEquals("http://uploadtest", widget.getIdentifier());
+
+
+ }
+
+ @Test
+ public void deleteWidgetUnauthorized() throws HttpException, IOException{
+ HttpClient client = new HttpClient();
+ DeleteMethod delete = new DeleteMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_WEATHER);
+ client.executeMethod(delete);
+ assertEquals(403, delete.getStatusCode());
+
+ //
+ // Check it wasn't deleted
+ //
+ GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID + "/" + WIDGET_ID_WEATHER);
+ client.executeMethod(get);
+ assertEquals(200, get.getStatusCode());
+ }
+
+ @Test
+ public void deleteWidgetNonexisting() throws HttpException, IOException{
+ Request delete = new Request("DELETE", TEST_WIDGETS_SERVICE_URL_VALID + "/9999");
+ delete.execute(true, false);
+ assertEquals(404, delete.getStatusCode());
+ }
+
+ @Test
+ public void deleteWidget() throws HttpException, IOException{
+
+ //
+ // Upload widget we'll test deleting next
+ //
+ File file = new File("src/test/resources/delete-test.wgt");
+ assertTrue(file.exists());
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(201,code);
+
+ //
+ // Delete the widget
+ //
+ Request delete = new Request("DELETE", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_DELETE_TEST));
+ delete.execute(true, false);
+ assertEquals(200, delete.getStatusCode());
+
+ //
+ // Check it was deleted
+ //
+ Request get = new Request("GET", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_DELETE_TEST));
+ get.execute(false, false);
+ assertEquals(404, get.getStatusCode());
+ }
+
+ /**
+ * We allow updates to existing widgets via POST as well as PUT
+ * (to allow browsers to update using forms)
+ * @throws HttpException
+ * @throws IOException
+ */
+ @Test
+ public void updateWidgetByPost() throws HttpException, IOException{
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src/test/resources/upload-test.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 201 (Created)
+ //
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(201,code);
+
+ //
+ // Now lets try updating
+ //
+ post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
+
+ //
+ // Use upload test widget
+ //
+ file = new File("src/test/resources/upload-test.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] newParts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(newParts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 200 (Updated)
+ //
+ post.execute(true, false);
+ code = post.getStatusCode();
+ assertEquals(200,code);
+
+ }
+
+ @Test
+ public void updateWidgetByPut() throws HttpException, IOException{
+
+ Request put = new Request("PUT", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src/test/resources/upload-test.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to PUT
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ put.setRequestEntity(new MultipartRequestEntity(parts, put.getClient()
+ .getParams()));
+
+ //
+ // PUT the file to /widgets and check we get 200 (Updated)
+ //
+ put.execute(true, false);
+ int code = put.getStatusCode();
+ assertEquals(200,code);
+
+ }
+
+ @Test
+ public void updateWidgetUnauthorized() throws HttpException, IOException{
+
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_UPLOAD_TEST));
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src/test/resources/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.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 200 (Updated)
+ //
+ post.execute(false, false);
+ int code = post.getStatusCode();
+ assertEquals(403,code);
+ }
+
+ @Test
+ public void updateWidgetNotFound() throws HttpException, IOException{
+
+ Request post = new Request("PUT", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_INVALID));
+
+ //
+ // Use upload test widget
+ //
+ File file = new File("src/test/resources/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.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets and check we get 200 (Updated)
+ //
+ post.execute(true, false);
+ int code = post.getStatusCode();
+ assertEquals(404,code);
+ }
+
+ /**
+ * Check that when we update a widget, we don't duplicate access policies. See WOOKIE-273.
+ * @throws HttpException
+ * @throws IOException
+ * @throws InterruptedException
+ * @throws JDOMException
+ */
+ @Test
+ public void checkForDuplicateAccessRequests() throws HttpException, IOException, InterruptedException, JDOMException{
+
+ //
+ // Add the test widget, and update it a few times
+ //
+ for (int i=0;i<4;i++){
+
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+
+ //
+ // Add the access test widget. This just has a single access request
+ // for the origin "http://accesstest.incubator.apache.org"
+ //
+ File file = new File("src/test/resources/access-test.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets
+ //
+ post.execute(true, false);
+ }
+
+ //
+ // Check that we only have one copy of the access request, not two
+ //
+ Request get = new Request("GET", TEST_POLICIES_SERVICE_URL_VALID + encodeString("/" + WIDGET_ID_ACCESS_TEST));
+ get.setAccepts("text/xml");
+ get.execute(true, false);
+ assertEquals(1,PoliciesControllerTest.processPolicies(get.getResponseBodyAsStream()).getChildren("policy").size());
+
+ }
+
+ @Test
+ public void workingWithUnidentifiedWidgets() throws HttpException, IOException, JDOMException{
+
+ Request post = new Request("POST", TEST_WIDGETS_SERVICE_URL_VALID);
+
+ //
+ // Add the access test widget. This just has a single access request
+ // for the origin "http://accesstest.incubator.apache.org"
+ //
+ File file = new File("src/test/resources/upload-test-noid.wgt");
+ assertTrue(file.exists());
+
+ //
+ // Add test wgt file to POST
+ //
+ Part[] parts = { new FilePart(file.getName(), file) };
+ post.setRequestEntity(new MultipartRequestEntity(parts, post.getClient()
+ .getParams()));
+
+ //
+ // POST the file to /widgets
+ //
+ post.execute(true, false);
+ SAXBuilder builder = new SAXBuilder();
+ Document doc = builder.build(post.getResponseBodyAsStream());
+ String id = doc.getRootElement().getAttributeValue("id");
+
+ //
+ // Now we'll update it
+ //
+ Request put = new Request("PUT", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + id));
+
+ //
+ // Add test wgt file to PUT
+ //
+ Part[] updateparts = { new FilePart(file.getName(), file) };
+ put.setRequestEntity(new MultipartRequestEntity(updateparts, put.getClient()
+ .getParams()));
+
+ //
+ // PUT the file to /widgets and check we get 200 (Updated)
+ //
+ put.execute(true, false);
+ int code = put.getStatusCode();
+ assertEquals(200,code);
+
+ //
+ // DELETE the widget
+ //
+ Request delete = new Request("DELETE", TEST_WIDGETS_SERVICE_URL_VALID + encodeString("/" + id));
+ delete.execute(true, false);
+ }
+
+ /**
+ * Download a widget using the Accept type of ("Accept","application/widget")
+ * Once downloaded make sure that the widget package is correct - run it through the parser
+ * @throws Exception
+ */
+ @Test
+ public void getActualWidget() throws Exception{
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/"+WIDGET_ID_WEATHER);
+ get.setRequestHeader("Accept","application/widget");
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200,code);
+
+ InputStream inputStream = get.getResponseBodyAsStream();
+ File downloadedFile = new File("src"+ File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar + "downloadedWeatherWidget.zip");
+ OutputStream out = new FileOutputStream(downloadedFile);
+
+ int read = 0;
+ byte[] bytes = new byte[1024];
+
+ while ((read = inputStream.read(bytes)) != -1) {
+ out.write(bytes, 0, read);
+ }
+ inputStream.close();
+ out.flush();
+ out.close();
+ get.releaseConnection();
+
+ // check the downloaded file
+ W3CWidgetFactory fac = new W3CWidgetFactory();
+ fac.setStartPageProcessor(null);
+ File outputDir = new File("src"+File.separatorChar+"test" + File.separatorChar + "resources" + File.separatorChar + "widgets");
+ outputDir.mkdirs();
+ fac.setOutputDirectory("src" + File.separatorChar + "test" + File.separatorChar + "resources" + File.separatorChar + "widgets");
+ W3CWidget widget = fac.parse(downloadedFile);
+ File file = fac.getUnzippedWidgetDirectory();
+ assertEquals("src" + File.separatorChar + "test" + File.separatorChar + "resources"
+ + File.separatorChar + "widgets" + File.separatorChar
+ + "www.getwookie.org" + File.separatorChar + "widgets"
+ + File.separatorChar + "weather", file.getPath());
+ assertTrue(file.isDirectory());
+ assertEquals(WIDGET_ID_WEATHER, widget.getIdentifier());
+
+ //clean up
+ FileUtils.deleteDirectory(outputDir);
+ FileUtils.deleteQuietly(downloadedFile);
+ }
}