Author: scottbw
Date: Mon Aug 8 14:23:30 2011
New Revision: 1154970
URL: http://svn.apache.org/viewvc?rev=1154970&view=rev
Log:
Improved code comments and consistency in yet more test classes.
Modified:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetAccessRequestPolicyControllerTest.java
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetServicesControllerTest.java
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetsControllerTest.java
Modified:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetAccessRequestPolicyControllerTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetAccessRequestPolicyControllerTest.java?rev=1154970&r1=1154969&r2=1154970&view=diff
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetAccessRequestPolicyControllerTest.java
(original)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetAccessRequestPolicyControllerTest.java
Mon Aug 8 14:23:30 2011
@@ -18,10 +18,12 @@ import static org.junit.Assert.assertFal
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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;
@@ -33,204 +35,253 @@ import org.jdom.input.SAXBuilder;
import org.junit.BeforeClass;
import org.junit.Test;
-public class WidgetAccessRequestPolicyControllerTest extends
AbstractControllerTest {
-
- protected static final String TEST_WARP_SERVICE_URL_VALID =
TEST_SERVER_LOCATION+"warp";
-
- private static String id; // policy id
-
- @BeforeClass
- public static void setup(){
- try {
- HttpClient client = new HttpClient();
- setAuthenticationCredentials(client);
- PostMethod post = new
PostMethod(TEST_WARP_SERVICE_URL_VALID);
- post.addParameter("widgetId", "1");
- post.addParameter("subdomains", "true");
- post.addParameter("origin", "http://www.9128.org");
- post.setDoAuthentication(true);
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("post failed");
- }
- // Now lets GET it to make sure it was added OK
- Element[] policies = getPolicies();
- for (Element policy:policies){
- if
(policy.getAttribute("origin").getValue().equals("http://www.9128.org")){
- id = policy.getAttributeValue("id");
- }
- }
- }
-
- @Test
- public void addPolicy(){
- // To test adding policies works, we just need to check that
the pre-test created one OK
- assertTrue(id != null);
- }
-
- @Test
- public void grantPolicy(){
- try {
- HttpClient client = new HttpClient();
- setAuthenticationCredentials(client);
- PutMethod put = new
PutMethod(TEST_WARP_SERVICE_URL_VALID+"/"+id+"?granted=true");
- put.setDoAuthentication(true);
- client.executeMethod(put);
- int code = put.getStatusCode();
- assertEquals(200,code);
- put.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("put failed");
- }
- // Now lets GET it to make sure it was modded OK
- Element[] policies = getPolicies();
- for (Element policy:policies){
- if
(policy.getAttribute("origin").getValue().equals("http://www.9128.org")){
- try {
-
assertTrue(policy.getAttribute("granted").getBooleanValue());
- } catch (DataConversionException e) {
- e.printStackTrace();
- fail("bad return value for granted");
- }
- }
- }
- }
-
- @Test
- public void revokePolicy(){
- try {
- HttpClient client = new HttpClient();
- setAuthenticationCredentials(client);
- PutMethod put = new
PutMethod(TEST_WARP_SERVICE_URL_VALID+"/"+id+"?granted=false");
- put.setDoAuthentication(true);
- client.executeMethod(put);
- int code = put.getStatusCode();
- assertEquals(200,code);
- put.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("put failed");
- }
- // Now lets GET it to make sure it was modded OK
- Element[] policies = getPolicies();
- for (Element policy:policies){
- if
(policy.getAttribute("origin").getValue().equals("http://www.9128.org")){
- try {
-
assertFalse(policy.getAttribute("granted").getBooleanValue());
- } catch (DataConversionException e) {
- e.printStackTrace();
- fail("bad return value for granted");
- }
- }
- }
- }
-
- @Test
- public void deletePolicy(){
- try {
- HttpClient client = new HttpClient();
- setAuthenticationCredentials(client);
- DeleteMethod del = new
DeleteMethod(TEST_WARP_SERVICE_URL_VALID+"/"+id);
- del.setDoAuthentication(true);
- client.executeMethod(del);
- int code = del.getStatusCode();
- assertEquals(200,code);
- del.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("put failed");
- }
- // Now lets make sure it was deleted OK
- Element[] policies = getPolicies();
- for (Element policy:policies){
- if (policy.getAttribute("id").getValue().equals(id)){
- fail("Policy was not deleted");
- }
- }
- }
-
- @Test
- public void testGrantNonExistingPolicy(){
- try {
- HttpClient client = new HttpClient();
- setAuthenticationCredentials(client);
- PutMethod put = new
PutMethod(TEST_WARP_SERVICE_URL_VALID+"/9999?granted=true");
- put.setDoAuthentication(true);
- client.executeMethod(put);
- int code = put.getStatusCode();
- assertEquals(404,code);
- put.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("put failed");
- }
- }
-
- @Test
- public void testGetWithoutAuthentication(){
- try {
- HttpClient client = new HttpClient();
- GetMethod get = new
GetMethod(TEST_WARP_SERVICE_URL_VALID);
- get.addRequestHeader("content-type", "text/xml");
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(401,code);
- get.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("get failed");
- }
- }
-
- //// Helpers
-
- private static Element[] getPolicies(){
- try {
- HttpClient client = new HttpClient();
- setAuthenticationCredentials(client);
- GetMethod get = new
GetMethod(TEST_WARP_SERVICE_URL_VALID);
- get.setDoAuthentication(true);
- get.addRequestHeader("content-type", "text/xml");
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200,code);
- InputStream stream = get.getResponseBodyAsStream();
- Element[] response = getPolicies(stream);
- get.releaseConnection();
- return response;
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("get failed");
- return null;
- }
- }
-
- @SuppressWarnings("unchecked")
- private static Element[] getPolicies(InputStream response){
- try {
- SAXBuilder builder = new SAXBuilder();
- Document doc = builder.build(response);
- List policies =
doc.getRootElement().getChildren("policy");
- if (policies != null){
- return (Element[])policies.toArray(new
Element[policies.size()]);
- }
- } catch (Exception e) {
- e.printStackTrace();
- fail("Bad XML returned by server");
- }
- return null;
- }
+/**
+ * Test cases for the WARP REST API
+ */
+public class WidgetAccessRequestPolicyControllerTest extends
+ AbstractControllerTest {
+
+ protected static final String TEST_WARP_SERVICE_URL_VALID =
TEST_SERVER_LOCATION
+ + "warp";
+
+ private static String id; // policy id
+
+ /**
+ * Setup some test policies
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ @BeforeClass
+ public static void setup() throws HttpException, IOException {
+
+ //
+ // POST a policy for testing
+ //
+ HttpClient client = new HttpClient();
+ setAuthenticationCredentials(client);
+ PostMethod post = new PostMethod(TEST_WARP_SERVICE_URL_VALID);
+ post.addParameter("widgetId", "1");
+ post.addParameter("subdomains", "true");
+ post.addParameter("origin", "http://www.9128.org");
+ post.setDoAuthentication(true);
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(201, code);
+ post.releaseConnection();
+
+ //
+ // Now lets GET it to make sure it was added OK
+ //
+ Element[] policies = getPolicies();
+ for (Element policy : policies) {
+ if (policy.getAttribute("origin").getValue()
+ .equals("http://www.9128.org")) {
+ id = policy.getAttributeValue("id");
+ }
+ }
+ }
+
+ /**
+ * Test that the test policy was added OK
+ */
+ @Test
+ public void addPolicy() {
+ // To test adding policies works, we just need to check that the pre-test
+ // created one OK
+ assertTrue(id != null);
+ }
+
+ /**
+ * Test granting a policy
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ @Test
+ public void grantPolicy() throws HttpException, IOException {
+
+ //
+ // PUT the granted status to true
+ //
+ HttpClient client = new HttpClient();
+ setAuthenticationCredentials(client);
+ PutMethod put = new PutMethod(TEST_WARP_SERVICE_URL_VALID + "/" + id
+ + "?granted=true");
+ put.setDoAuthentication(true);
+ client.executeMethod(put);
+ int code = put.getStatusCode();
+ assertEquals(200, code);
+ put.releaseConnection();
+
+ //
+ // Now lets GET it to make sure it was modded OK
+ //
+ Element[] policies = getPolicies();
+ for (Element policy : policies) {
+ if (policy.getAttribute("origin").getValue()
+ .equals("http://www.9128.org")) {
+ try {
+ assertTrue(policy.getAttribute("granted").getBooleanValue());
+ } catch (DataConversionException e) {
+ e.printStackTrace();
+ fail("bad return value for granted");
+ }
+ }
+ }
+ }
+
+ /**
+ * Test we can revoke a policy
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void revokePolicy() throws HttpException, IOException {
+ //
+ // PUT the value of granted = false
+ //
+ HttpClient client = new HttpClient();
+ setAuthenticationCredentials(client);
+ PutMethod put = new PutMethod(TEST_WARP_SERVICE_URL_VALID + "/" + id
+ + "?granted=false");
+ put.setDoAuthentication(true);
+ client.executeMethod(put);
+ int code = put.getStatusCode();
+ assertEquals(200, code);
+ put.releaseConnection();
+
+ //
+ // Now lets GET it to make sure it was modded OK
+ //
+ Element[] policies = getPolicies();
+ for (Element policy : policies) {
+ if (policy.getAttribute("origin").getValue()
+ .equals("http://www.9128.org")) {
+ try {
+ assertFalse(policy.getAttribute("granted").getBooleanValue());
+ } catch (DataConversionException e) {
+ e.printStackTrace();
+ fail("bad return value for granted");
+ }
+ }
+ }
+ }
+
+ /**
+ * Test we can delete a policy
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ @Test
+ public void deletePolicy() throws HttpException, IOException {
+
+ //
+ // DELETE the policy
+ //
+ HttpClient client = new HttpClient();
+ setAuthenticationCredentials(client);
+ DeleteMethod del = new DeleteMethod(TEST_WARP_SERVICE_URL_VALID + "/" +
id);
+ del.setDoAuthentication(true);
+ client.executeMethod(del);
+ int code = del.getStatusCode();
+ assertEquals(200, code);
+ del.releaseConnection();
+
+ //
+ // GET to make sure it was deleted OK
+ //
+ Element[] policies = getPolicies();
+ for (Element policy : policies) {
+ if (policy.getAttribute("id").getValue().equals(id)) {
+ fail("Policy was not deleted");
+ }
+ }
+ }
+
+ /**
+ * Test we can't grant a policy that doesn't exist
+ *
+ * @throws IOException
+ * @throws HttpException
+ *
+ */
+ @Test
+ public void testGrantNonExistingPolicy() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+ setAuthenticationCredentials(client);
+ PutMethod put = new PutMethod(TEST_WARP_SERVICE_URL_VALID
+ + "/9999?granted=true");
+ put.setDoAuthentication(true);
+ client.executeMethod(put);
+ int code = put.getStatusCode();
+ assertEquals(404, code);
+ put.releaseConnection();
+ }
+
+ /**
+ * Test that we can't get access to WARP without admin credentials
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ @Test
+ public void testGetWithoutAuthentication() throws HttpException, IOException
{
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(TEST_WARP_SERVICE_URL_VALID);
+ get.addRequestHeader("content-type", "text/xml");
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(401, code);
+ get.releaseConnection();
+ }
+
+ // // Helpers
+
+ /**
+ * Helper method for fetching the list of policies
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ private static Element[] getPolicies() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+ setAuthenticationCredentials(client);
+ GetMethod get = new GetMethod(TEST_WARP_SERVICE_URL_VALID);
+ get.setDoAuthentication(true);
+ get.addRequestHeader("content-type", "text/xml");
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200, code);
+ InputStream stream = get.getResponseBodyAsStream();
+ Element[] response = getPolicies(stream);
+ get.releaseConnection();
+ return response;
+ }
+
+ /**
+ * Helper method to get list of policies from server response
+ *
+ * @param response
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ private static Element[] getPolicies(InputStream response) {
+ try {
+ SAXBuilder builder = new SAXBuilder();
+ Document doc = builder.build(response);
+ @SuppressWarnings("rawtypes")
+ List policies = doc.getRootElement().getChildren("policy");
+ if (policies != null) {
+ return (Element[]) policies.toArray(new Element[policies.size()]);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("Bad XML returned by server");
+ }
+ return null;
+ }
}
Modified:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java?rev=1154970&r1=1154969&r2=1154970&view=diff
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java
(original)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetInstancesControllerTest.java
Mon Aug 8 14:23:30 2011
@@ -26,301 +26,345 @@ import org.apache.commons.httpclient.met
import org.junit.Ignore;
import org.junit.Test;
-
/**
- * Controller tests. Run this after the Ant script completes (give it a sec
for Tomcat to restart)
- * @author scott
- *
+ * Test cases for the Widget Instances REST API
*/
public class WidgetInstancesControllerTest extends AbstractControllerTest {
-
- private static final String LOCALIZED_WIDGET =
"http://www.getwookie.org/widgets/localetest";
- private static String test_id_key = "";
-
- @Test
- public void getLocalizedInstance(){
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=localetest&shareddatakey=localetest&locale=fr");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,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();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("post failed");
- }
- }
-
- @Test
- public void getExistingInstanceByInstanceParams(){
- try {
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
get.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&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"));
- } catch (Exception e){
- e.printStackTrace();
- fail("get failed");
- }
- }
-
- @Test
- public void getExistingInstanceByIdKey(){
- try {
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
get.setQueryString("api_key="+API_KEY_VALID+"&id_key="+test_id_key);
- client.executeMethod(get);
- int code = get.getStatusCode();
- assertEquals(200, code);
-
assertTrue(get.getResponseBodyAsString().contains("locales/en/index.htm"));
- assertTrue(get.getResponseBodyAsString().contains("locale test"));
- } catch (Exception e){
- e.printStackTrace();
- fail("get failed");
- }
- }
-
- @Test
- public void getExistingInstanceByIdResource(){
- try {
- 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/en/index.htm"));
- assertTrue(get.getResponseBodyAsString().contains("locale test"));
- } catch (Exception e){
- e.printStackTrace();
- fail("get failed");
- }
- }
-
- @Test
- // Tests if specifying "early modern french" locale returns standard FR
start file
- public void getGracefulLocalizedInstance(){
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=localetest1b&shareddatakey=localetest1b&locale=fr-1694acad");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,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();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("post failed");
- }
- }
-
- @Test
- public void getNonLocalizedInstance(){
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=localetest2&shareddatakey=localetest2&locale=bu");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,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();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("post failed");
- }
- }
-
- @Test
- public void getDefaultLocalizedInstance(){
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+LOCALIZED_WIDGET+"&userid=localetest3&shareddatakey=localetest3");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,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();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("post failed");
- }
- }
-
- @Test
- public void getInstanceById(){
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=test");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("post failed");
- }
- }
-
- @Test
- public void getInstanceById_AlreadyExists(){
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=test");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200,code);
- post.releaseConnection();
- }
- catch (Exception e) {
- fail("post failed");
- }
- }
-
- @Test
- public void getInstance_InvalidAPIkey(){
- try {
- 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(401,code);
- post.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("post failed");
- }
- }
-
- @Test
- public void getInstanceById_InvalidWidget(){
- try {
- 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();
- }
- catch (Exception e) {
- fail("post failed");
- }
- }
-
- @Test @Ignore
- public void stop(){
- fail("test not written");
- }
-
- @Test @Ignore
- public void resume(){
- fail("test not written");
- }
-
- @Test
- public void cloneSharedData(){
- // Create an instance
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=clonetestsrc");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
- }
- catch (Exception e) {
- fail("create instance failed");
- }
-
- // Set some shared data
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&is_public=true&shareddatakey=clonetestsrc&propertyname=cat&propertyvalue=garfield");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
- }
- catch (Exception e) {
- fail("set shared data failed");
- }
-
- // Clone it
- try {
- HttpClient client = new HttpClient();
- PutMethod post = new
PutMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=clonetestsrc&requestid=clone&cloneshareddatakey=clonetestsync");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- post.releaseConnection();
- }
- catch (Exception e) {
- fail("clone shared data failed");
- }
-
- // Check it
- // Create an instance for the clone
- try {
- HttpClient client = new HttpClient();
- PostMethod post = new
PostMethod(TEST_INSTANCES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=clonetestsync");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(201,code);
- post.releaseConnection();
- }
- catch (Exception e) {
- fail("create instance failed");
- }
- // Get the data for the clone
- try {
- HttpClient client = new HttpClient();
- GetMethod post = new
GetMethod(TEST_PROPERTIES_SERVICE_URL_VALID);
-
post.setQueryString("api_key="+API_KEY_VALID+"&widgetid="+WIDGET_ID_VALID+"&userid=test&shareddatakey=clonetestsync&propertyname=cat");
- client.executeMethod(post);
- int code = post.getStatusCode();
- assertEquals(200, code);
- String resp = post.getResponseBodyAsString();
- assertEquals("garfield",resp);
- post.releaseConnection();
- }
- catch (Exception e) {
- fail("create instance failed");
- }
- }
+
+ private static final String LOCALIZED_WIDGET =
"http://www.getwookie.org/widgets/localetest";
+ private static String test_id_key = "";
+
+ /**
+ * 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="
+ + LOCALIZED_WIDGET
+ + "&userid=localetest&shareddatakey=localetest&locale=fr");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(201, 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="
+ + LOCALIZED_WIDGET
+ + "&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 + "&id_key=" + test_id_key);
+ client.executeMethod(get);
+ int code = get.getStatusCode();
+ assertEquals(200, code);
+ assertTrue(get.getResponseBodyAsString().contains("locales/en/index.htm"));
+ assertTrue(get.getResponseBodyAsString().contains("locale test"));
+ }
+
+ /**
+ * 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/en/index.htm"));
+ assertTrue(get.getResponseBodyAsString().contains("locale test"));
+ }
+
+ /**
+ * 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="
+ + LOCALIZED_WIDGET
+ +
"&userid=localetest1b&shareddatakey=localetest1b&locale=fr-1694acad");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(201, 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="
+ + LOCALIZED_WIDGET
+ + "&userid=localetest2&shareddatakey=localetest2&locale=bu");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(201, 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="
+ + LOCALIZED_WIDGET + "&userid=localetest3&shareddatakey=localetest3");
+ client.executeMethod(post);
+ int code = post.getStatusCode();
+ assertEquals(201, 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(201, 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(401, 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(201, 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(201, 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:
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetServicesControllerTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetServicesControllerTest.java?rev=1154970&r1=1154969&r2=1154970&view=diff
==============================================================================
---
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetServicesControllerTest.java
(original)
+++
incubator/wookie/trunk/src-tests/org/apache/wookie/tests/functional/WidgetServicesControllerTest.java
Mon Aug 8 14:23:30 2011
@@ -24,6 +24,11 @@ import org.apache.commons.httpclient.met
import org.apache.commons.httpclient.methods.PutMethod;
import org.junit.Test;
+/**
+ * Test cases for the Widget Services REST API
+ *
+ * Note that this API is deprecated and may be removed in a future release.
+ */
public class WidgetServicesControllerTest extends AbstractControllerTest {
@Test
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=1154970&r1=1154969&r2=1154970&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
Mon Aug 8 14:23:30 2011
@@ -16,19 +16,25 @@ package org.apache.wookie.tests.function
import static org.junit.Assert.*;
+import java.io.IOException;
+
import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.Test;
/**
- * @author scott
- *
+ * Test cases for the Widget REST API
*/
public class WidgetsControllerTest extends AbstractControllerTest {
+ /**
+ * Test GET all widgets
+ * @throws IOException
+ * @throws HttpException
+ */
@Test
- public void getAllWidgets(){
- try {
+ public void getAllWidgets() throws HttpException, IOException{
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(TEST_WIDGETS_SERVICE_URL_VALID);
get.setQueryString("all=true");
@@ -38,16 +44,15 @@ public class WidgetsControllerTest exten
String response = get.getResponseBodyAsString();
assertTrue(response.contains("<widget id=\"1\"
identifier=\"http://notsupported\""));
get.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("get failed");
- }
}
+ /**
+ * Test we can GET a widget using its internal ID as a resource path
+ * @throws IOException
+ * @throws HttpException
+ */
@Test
- public void getSpecificWidget(){
- try {
+ public void getSpecificWidget() throws HttpException, IOException{
HttpClient client = new HttpClient();
GetMethod get = new
GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/1");
client.executeMethod(get);
@@ -56,32 +61,31 @@ public class WidgetsControllerTest exten
String response = get.getResponseBodyAsString();
assertTrue(response.contains("<widget id=\"1\"
identifier=\"http://notsupported\""));
get.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("get failed");
- }
}
+ /**
+ * Test that a request for a non-existing widget ID gets a 404
+ * @throws IOException
+ * @throws HttpException
+ */
@Test
- public void getSpecificWidget_nonexisting(){
- try {
+ 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();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("get failed");
- }
}
+ /**
+ * Tests we get GET widgets by service type
+ * NOTE that the Services functionality is deprecated and may be
removed in a future release
+ * @throws IOException
+ * @throws HttpException
+ */
@Test
- public void getWidgetType(){
- try {
+ public void getWidgetType() throws HttpException, IOException{
HttpClient client = new HttpClient();
GetMethod get = new
GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/unsupported");
client.executeMethod(get);
@@ -90,17 +94,16 @@ public class WidgetsControllerTest exten
String response = get.getResponseBodyAsString();
assertTrue(response.contains("<widget id=\"1\"
identifier=\"http://notsupported\""));
get.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("get failed");
- }
}
- // We expect a valid response, just no actual widgets
+ /**
+ * Tests we get GET widgets by service type when it is empty; we expect a
valid response, just no actual widgets
+ * NOTE that the Services functionality is deprecated and may be removed in
a future release
+ * @throws IOException
+ * @throws HttpException
+ */
@Test
- public void getWidgetType_empty(){
- try {
+ public void getWidgetType_empty() throws HttpException, IOException{
HttpClient client = new HttpClient();
GetMethod get = new
GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/games");
client.executeMethod(get);
@@ -109,28 +112,21 @@ public class WidgetsControllerTest exten
String response = get.getResponseBodyAsString();
assertFalse(response.contains("<widget "));
get.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("get failed");
- }
}
- // resource doesn't match either a widget.id or a
widgetservice.servicename
+ /**
+ * Tests a GET which matches neither an existing widget id or existing
service type gives us a 404
+ * @throws IOException
+ * @throws HttpException
+ */
@Test
- public void getWidgetType_noneexistant(){
- try {
+ public void getWidgetType_noneexistant() throws HttpException,
IOException{
HttpClient client = new HttpClient();
GetMethod get = new
GetMethod(TEST_WIDGETS_SERVICE_URL_VALID+"/nosuchtype");
client.executeMethod(get);
int code = get.getStatusCode();
assertEquals(404,code);
get.releaseConnection();
- }
- catch (Exception e) {
- e.printStackTrace();
- fail("get failed");
- }
}
}