From 1e2b842b564f6cde8b79ec7eba001ade0ba1088e Mon Sep 17 00:00:00 2001 From: Tuna Aksoy Date: Thu, 6 Dec 2012 16:43:07 +0000 Subject: [PATCH] Changed REST API tests for DataSets git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@44430 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../test/WebScriptTestSuite.java | 12 +-- .../test/webscript/DataSetPostTest.java | 83 ---------------- .../test/webscript/DataSetRestApiTest.java | 95 +++++++++++++++++++ .../test/webscript/DataSetsGetTest.java | 53 ----------- .../test/webscript/EmailMapScriptTest.java | 6 +- 5 files changed, 103 insertions(+), 146 deletions(-) delete mode 100644 rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetPostTest.java create mode 100644 rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetRestApiTest.java delete mode 100644 rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetsGetTest.java diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/WebScriptTestSuite.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/WebScriptTestSuite.java index 0679b820c5..50453987db 100644 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/WebScriptTestSuite.java +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/WebScriptTestSuite.java @@ -22,8 +22,7 @@ import junit.framework.Test; import junit.framework.TestSuite; import org.alfresco.module.org_alfresco_module_rm.test.webscript.BootstraptestDataRestApiTest; -import org.alfresco.module.org_alfresco_module_rm.test.webscript.DataSetPostTest; -import org.alfresco.module.org_alfresco_module_rm.test.webscript.DataSetsGetTest; +import org.alfresco.module.org_alfresco_module_rm.test.webscript.DataSetRestApiTest; import org.alfresco.module.org_alfresco_module_rm.test.webscript.DispositionRestApiTest; import org.alfresco.module.org_alfresco_module_rm.test.webscript.EventRestApiTest; import org.alfresco.module.org_alfresco_module_rm.test.webscript.RMCaveatConfigScriptTest; @@ -34,17 +33,17 @@ import org.alfresco.module.org_alfresco_module_rm.test.webscript.RoleRestApiTest /** * RM WebScript test suite - * + * * @author Roy Wetherall */ public class WebScriptTestSuite extends TestSuite { /** * Creates the test suite - * + * * @return the test suite */ - public static Test suite() + public static Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(BootstraptestDataRestApiTest.class); @@ -54,8 +53,7 @@ public class WebScriptTestSuite extends TestSuite suite.addTestSuite(RMConstraintScriptTest.class); suite.addTestSuite(RmRestApiTest.class); suite.addTestSuite(RoleRestApiTest.class); - suite.addTestSuite(DataSetsGetTest.class); - suite.addTestSuite(DataSetPostTest.class); + suite.addTestSuite(DataSetRestApiTest.class); return suite; } } diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetPostTest.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetPostTest.java deleted file mode 100644 index ff33550979..0000000000 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetPostTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package org.alfresco.module.org_alfresco_module_rm.test.webscript; - -import java.io.IOException; - -import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMWebScriptTestCase; -import org.apache.commons.lang.StringUtils; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.springframework.extensions.webscripts.Status; -import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; -import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; -import org.springframework.extensions.webscripts.TestWebScriptServer.Response; - -public class DataSetPostTest extends BaseRMWebScriptTestCase -{ - /** URL for the REST APIs */ - private static final String GET_DATASETS_URL = "/api/rma/datasets?site=%s"; - private static final String POST_DATASET_URL = "/api/rma/datasets/%s?site=%s"; - - /** Constant for the content type */ - private static final String APPLICATION_JSON = "application/json"; - - /** - * Test the REST API to import a RM data set into a file plan - * - * @throws IOException - * @throws JSONException - */ - public void testPostDataSetAction() throws IOException, JSONException - { - String dataSetId = getDataSetId(); - if (StringUtils.isNotBlank(dataSetId)) - { - // Format url and send request - String url = String.format(POST_DATASET_URL, dataSetId, SITE_ID); - Response response = sendRequest(new PostRequest(url, new JSONObject().toString(), APPLICATION_JSON), Status.STATUS_OK); - - // Check the content from the response - String contentAsString = response.getContentAsString(); - assertNotNull(contentAsString); - - // Convert the response to json and check the result - JSONObject contentAsJson = new JSONObject(contentAsString); - String success = contentAsJson.getString("success"); - assertNotNull(success); - assertTrue(success.equals("true")); - - // It is not possible to import the same data set into the same file plan - response = sendRequest(new PostRequest(url, new JSONObject().toString(), APPLICATION_JSON), Status.STATUS_INTERNAL_SERVER_ERROR); - } - } - - /** - * Helper method for getting a data set id - * - * @return A data set id from the list of available data sets or null if there is no data set - * @throws IOException - * @throws JSONException - */ - private String getDataSetId() throws IOException, JSONException - { - // Format url and send request - String url = String.format(GET_DATASETS_URL, SITE_ID); - Response response = sendRequest(new GetRequest(url), Status.STATUS_OK); - - // Get the response as string, convert to json and retrieve the data sets - String contentAsString = response.getContentAsString(); - JSONObject contentAsJson = new JSONObject(contentAsString); - JSONObject data = contentAsJson.getJSONObject("data"); - JSONArray dataSets = data.getJSONArray("datasets"); - - // Check if there are some test data sets - String dataSetId = null; - if (dataSets.length() > 0) - { - JSONObject dataSet = (JSONObject) dataSets.get(0); - dataSetId = (String) dataSet.get("id"); - } - - return dataSetId; - } -} diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetRestApiTest.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetRestApiTest.java new file mode 100644 index 0000000000..5bd576c9e8 --- /dev/null +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetRestApiTest.java @@ -0,0 +1,95 @@ +package org.alfresco.module.org_alfresco_module_rm.test.webscript; + +import java.io.IOException; + +import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMWebScriptTestCase; +import org.apache.commons.lang.StringUtils; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.Response; + +public class DataSetRestApiTest extends BaseRMWebScriptTestCase +{ + /** URL for the REST APIs */ + private static final String GET_DATASETS_URL = "/api/rma/datasets?site=%s"; + private static final String POST_DATASET_URL = "/api/rma/datasets/%s?site=%s"; + + /** Constant for the content type */ + private static final String APPLICATION_JSON = "application/json"; + + /** + * Test the REST API to retrieve details of available RM data sets + * and to import an RM data set into a file plan + * + * @throws IOException + * @throws JSONException + */ + public void testGetPostDataSetAction() throws IOException, JSONException + { + /** Test GET */ + + // Format url and send request + String getUrl = String.format(GET_DATASETS_URL, SITE_ID); + Response getResponse = sendRequest(new GetRequest(getUrl), Status.STATUS_OK); + + // Check the content from the response + String getContentAsString = getResponse.getContentAsString(); + assertNotNull(getContentAsString); + + // Convert the response to json and check the data + JSONObject getContentAsJson = new JSONObject(getContentAsString); + JSONObject getData = getContentAsJson.getJSONObject("data"); + assertNotNull(getData); + + // Get the data sets and check them + JSONArray getDataSets = getData.getJSONArray("datasets"); + assertNotNull(getDataSets); + + // Check the label and the id of the data sets + for (int i = 0; i < getDataSets.length(); i++) + { + JSONObject dataSet = (JSONObject) getDataSets.get(i); + assertTrue(dataSet.length() == 3); + assertNotNull(dataSet.get("label")); + assertNotNull(dataSet.get("id")); + assertNotNull(dataSet.get("isLoaded")); + } + + /** Test POST */ + String dataSetId = getDataSets.getJSONObject(0).getString("id"); + if (StringUtils.isNotBlank(dataSetId)) + { + // Format url and send request + String url = String.format(POST_DATASET_URL, dataSetId, SITE_ID); + Response response = sendRequest(new PostRequest(url, new JSONObject().toString(), APPLICATION_JSON), Status.STATUS_OK); + + // Check the content from the response + String contentAsString = response.getContentAsString(); + assertNotNull(contentAsString); + + // Convert the response to json and check the result + JSONObject contentAsJson = new JSONObject(contentAsString); + String success = contentAsJson.getString("success"); + assertNotNull(success); + assertTrue(success.equals("true")); + + // It is not possible to import the same data set into the same file plan + response = sendRequest(new PostRequest(url, new JSONObject().toString(), APPLICATION_JSON), Status.STATUS_OK); + + // Check the content from the response + contentAsString = response.getContentAsString(); + assertNotNull(contentAsString); + + // Convert the response to json and check the result + contentAsJson = new JSONObject(contentAsString); + success = contentAsJson.getString("success"); + assertNotNull(success); + assertTrue(success.equals("false")); + assertNotNull(contentAsJson.getString("message")); + } + } +} \ No newline at end of file diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetsGetTest.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetsGetTest.java deleted file mode 100644 index 0159ac3a08..0000000000 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/DataSetsGetTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.alfresco.module.org_alfresco_module_rm.test.webscript; - -import java.io.IOException; - -import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMWebScriptTestCase; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.springframework.extensions.webscripts.Status; -import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; -import org.springframework.extensions.webscripts.TestWebScriptServer.Response; - -public class DataSetsGetTest extends BaseRMWebScriptTestCase -{ - /** URL for the REST API */ - private static final String GET_DATASETS_URL = "/api/rma/datasets?site=%s"; - - /** - * Test the REST API to retrieve details of available RM data sets - * - * @throws IOException - * @throws JSONException - */ - public void testGetDataSetsAction() throws IOException, JSONException - { - // Format url and send request - String url = String.format(GET_DATASETS_URL, SITE_ID); - Response response = sendRequest(new GetRequest(url), Status.STATUS_OK); - - // Check the content from the response - String contentAsString = response.getContentAsString(); - assertNotNull(contentAsString); - - // Convert the response to json and check the data - JSONObject contentAsJson = new JSONObject(contentAsString); - JSONObject data = contentAsJson.getJSONObject("data"); - assertNotNull(data); - - // Get the data sets and check them - JSONArray dataSets = data.getJSONArray("datasets"); - assertNotNull(dataSets); - - // Check the label and the id of the data sets - for (int i = 0; i < dataSets.length(); i++) - { - JSONObject dataSet = (JSONObject) dataSets.get(i); - assertTrue(dataSet.length() == 3); - assertNotNull(dataSet.get("label")); - assertNotNull(dataSet.get("id")); - assertNotNull(dataSet.get("isLoaded")); - } - } -} \ No newline at end of file diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/EmailMapScriptTest.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/EmailMapScriptTest.java index ef1028481e..d071c5e1ef 100644 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/EmailMapScriptTest.java +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/EmailMapScriptTest.java @@ -47,7 +47,7 @@ public class EmailMapScriptTest extends BaseRMWebScriptTestCase */ public void testEmailMap() throws IOException, JSONException { - // Test Get + /** Test GET */ Response getResponse = sendRequest(new GetRequest(URL_RM_EMAILMAP), Status.STATUS_OK); JSONObject getResponseContent = new JSONObject(getResponse.getContentAsString()); @@ -55,7 +55,7 @@ public class EmailMapScriptTest extends BaseRMWebScriptTestCase JSONArray getMappings = getData.getJSONArray("mappings"); assertTrue(getMappings.length() == 20); - // Test Post + /** Test POST */ JSONObject newMapping = new JSONObject(); newMapping.put("from", "whatever"); newMapping.put("to", "rmc:Wibble"); @@ -76,7 +76,7 @@ public class EmailMapScriptTest extends BaseRMWebScriptTestCase assertFalse(Boolean.parseBoolean(postResponseContent.getString("success"))); assertFalse(postResponseContent.getString("message").isEmpty()); - // Test Delete + /** Test DELETE */ Response deleteResponse = sendRequest(new DeleteRequest(String.format(URL_RM_EMAILMAP_DELETE, "whatever", "rmc:Wibble")), Status.STATUS_OK); JSONObject deleteResponseContent = new JSONObject(deleteResponse.getContentAsString()); JSONObject deleteData = deleteResponseContent.getJSONObject("data");