From ec2f72650fed404d3edf7f62c3f543df1849ca59 Mon Sep 17 00:00:00 2001 From: Tuna Aksoy Date: Wed, 5 Dec 2012 23:00:59 +0000 Subject: [PATCH] Tests for the REST API (for Email Mapping) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@44398 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../alfresco/rma/admin/emailmap.get.desc.xml | 54 ++--- .../alfresco/rma/admin/emailmap.post.desc.xml | 56 ++--- .../test/webscript/EmailMapScriptTest.java | 195 +++++++----------- 3 files changed, 123 insertions(+), 182 deletions(-) diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/admin/emailmap.get.desc.xml b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/admin/emailmap.get.desc.xml index 0347abaa75..1a8bd1a92e 100644 --- a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/admin/emailmap.get.desc.xml +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/admin/emailmap.get.desc.xml @@ -1,29 +1,29 @@ - Get the custom email property map - - fields are specified with "from" and "to". -
- Example data. -
-    "mappings":
-    [ 
-       {"from" : "messageTo", "to" : "imap:messageTo" } , 
-       {"from" : "Thread-Index", "to" : "imap:threadIndex" } , 
-       {"from" : "messageFrom", "to" : "imap:messageFrom" } , 
-       {"from" : "messageSubject", "to" : "cm:title" } , 
-       {"from" : "messageSubject", "to" : "imap:messageSubject" } , 
-       {"from" : "messageSubject", "to" : "cm:description" } , 
-       {"from" : "messageCc", "to" : "imap:messageCc" } , 
-       {"from" : "Message-ID", "to" : "imap:messageId" } 
-    ] 
-  
- ]]> -
- /api/rma/admin/emailmap - argument - user - required - internal + Get the custom email property map + + fields are specified with "from" and "to". +
+ Example data. +
+      "mappings":
+      [
+         {"from" : "messageTo", "to" : "imap:messageTo" } ,
+         {"from" : "Thread-Index", "to" : "imap:threadIndex" } ,
+         {"from" : "messageFrom", "to" : "imap:messageFrom" } ,
+         {"from" : "messageSubject", "to" : "cm:title" } ,
+         {"from" : "messageSubject", "to" : "imap:messageSubject" } ,
+         {"from" : "messageSubject", "to" : "cm:description" } ,
+         {"from" : "messageCc", "to" : "imap:messageCc" } ,
+         {"from" : "Message-ID", "to" : "imap:messageId" }
+      ]
+   
+ ]]> +
+ /api/rma/admin/emailmap + argument + user + required + internal
\ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/admin/emailmap.post.desc.xml b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/admin/emailmap.post.desc.xml index eec6580c60..cb9073d997 100644 --- a/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/admin/emailmap.post.desc.xml +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/rma/admin/emailmap.post.desc.xml @@ -1,40 +1,20 @@ - Update email property map - - Data is specified in JSON format as a JSONObject with two optional fields, "add" and "delete". -
- The contents of the add array are added. -
- The contents of the delete array are deleted. -
- Add mapping: -
-   {
-      "add":
-      [
-         {"to":"rmc:Wibble", "from":"whatever"},
-         {"to":"rmc:wobble", "from":"whatever"}
-      ]
-   }
-  
- Delete mapping: -
-   {
-      "delete":
-      [
-         {"to":"rmc:Wibble", "from":"whatever"},
-         {"to":"rmc:wobble", "from":"whatever"}
-      ]
-   }
-  
- Returns data in the same format as the get method - ]]> -
- /api/rma/admin/emailmap - argument - user - required - internal + Add email property map + + Data is specified in JSON format as a JSONObject +
+   {"to":"rmc:Wibble", "from":"whatever"}
+   
+
+ POST /api/rma/admin/emailmap adds the specified mapping + Returns data in the same format as the get method + ]]> +
+ /api/rma/admin/emailmap + argument + user + required + internal
\ 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 1187aebf9b..ef1028481e 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 @@ -18,133 +18,94 @@ */ 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.alfresco.repo.security.authentication.AuthenticationUtil; -import org.alfresco.service.cmr.security.AuthenticationService; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.Response; public class EmailMapScriptTest extends BaseRMWebScriptTestCase { - + /** URLs for the REST APIs */ public final static String URL_RM_EMAILMAP = "/api/rma/admin/emailmap"; - - AuthenticationService authenticationService; - - @Override - protected void setUp() throws Exception - { - this.authenticationService = (AuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService"); -// setCurrentUser(AuthenticationUtil.getAdminUserName()); - super.setUp(); - - // Set the current security context as admin - AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); - - } - - @Override - protected void tearDown() throws Exception - { - super.tearDown(); - - } - - public void testGetEmailMap() throws Exception - { - { - Response response = sendRequest(new GetRequest(URL_RM_EMAILMAP), Status.STATUS_OK); - - @SuppressWarnings("unused") - JSONObject top = new JSONObject(response.getContentAsString()); - System.out.println(response.getContentAsString()); - //JSONArray data = top.getJSONArray("data"); - } - } - - public void testUpdateEmailMap() throws Exception - { - /** - * Update the list by adding two values - */ - { - JSONObject obj = new JSONObject(); - - JSONArray add = new JSONArray(); - JSONObject val = new JSONObject(); - val.put("from", "whatever"); - val.put("to", "rmc:Wibble"); - add.put(val); - JSONObject val2 = new JSONObject(); - val2.put("from", "whatever"); - val2.put("to", "rmc:wobble"); - add.put(val2); - - obj.put("add", add); - - System.out.println(obj.toString()); - - /** - * Now do a post to add a couple of values - */ - Response response = sendRequest(new PostRequest(URL_RM_EMAILMAP, obj.toString(), "application/json"), Status.STATUS_OK); - System.out.println(response.getContentAsString()); - // Check the response - - - JSONArray delete = new JSONArray(); - delete.put(val2); - - } - - /** - * Update the list by deleting a value - * - * "whatever" has two mappings, delete one of them - */ - { - JSONObject obj = new JSONObject(); - JSONObject val2 = new JSONObject(); - JSONArray delete = new JSONArray(); - val2.put("from", "whatever"); - val2.put("to", "rmc:wobble"); - delete.put(val2); - obj.put("delete", delete); - - /** - * Now do a post to delete a couple of values - */ - Response response = sendRequest(new PostRequest(URL_RM_EMAILMAP, obj.toString(), "application/json"), Status.STATUS_OK); - System.out.println(response.getContentAsString()); + public final static String URL_RM_EMAILMAP_DELETE = "/api/rma/admin/emailmap/%s/%s"; - JSONObject top = new JSONObject(response.getContentAsString()); - JSONObject data = top.getJSONObject("data"); - JSONArray mappings = data.getJSONArray("mappings"); - - boolean wibbleFound = false; - for(int i = 0; i < mappings.length(); i++) - { - JSONObject mapping = mappings.getJSONObject(i); - - - if(mapping.get("from").equals("whatever")) - { - if(mapping.get("to").equals("rmc:Wibble")) - { - wibbleFound = true; - } - else - { - fail("custom mapping for field not deleted"); - } - } - } - assertTrue(wibbleFound); - } - } -} + /** Constant for the content type */ + private static final String APPLICATION_JSON = "application/json"; + /** + * Tests the REST APIs for a custom mapping + * + * @throws IOException + * @throws JSONException + */ + public void testEmailMap() throws IOException, JSONException + { + // Test Get + Response getResponse = sendRequest(new GetRequest(URL_RM_EMAILMAP), Status.STATUS_OK); + + JSONObject getResponseContent = new JSONObject(getResponse.getContentAsString()); + JSONObject getData = getResponseContent.getJSONObject("data"); + JSONArray getMappings = getData.getJSONArray("mappings"); + assertTrue(getMappings.length() == 20); + + // Test Post + JSONObject newMapping = new JSONObject(); + newMapping.put("from", "whatever"); + newMapping.put("to", "rmc:Wibble"); + + Response postResponse = sendRequest(new PostRequest(URL_RM_EMAILMAP, newMapping.toString(), APPLICATION_JSON), Status.STATUS_OK); + + JSONObject postResponseContent = new JSONObject(postResponse.getContentAsString()); + assertTrue(Boolean.parseBoolean(postResponseContent.getString("success"))); + + JSONObject postData = postResponseContent.getJSONObject("data"); + JSONArray postMappings = postData.getJSONArray("mappings"); + + assertTrue(postMappings.length() == 21); + assertTrue(existsMapping(postMappings)); + + postResponse = sendRequest(new PostRequest(URL_RM_EMAILMAP, newMapping.toString(), APPLICATION_JSON), Status.STATUS_OK); + postResponseContent = new JSONObject(postResponse.getContentAsString()); + assertFalse(Boolean.parseBoolean(postResponseContent.getString("success"))); + assertFalse(postResponseContent.getString("message").isEmpty()); + + // 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"); + JSONArray deleteMappings = deleteData.getJSONArray("mappings"); + + assertTrue(deleteMappings.length() == 20); + assertFalse(existsMapping(deleteMappings)); + } + + /** + * Helper method for checking if a custom mapping exists + * + * @param mappings The list of available mappings + * @return true if the custom mapping exists in the list of available mappings, false otherwise + * @throws JSONException + */ + private boolean existsMapping(JSONArray mappings) throws JSONException + { + boolean result = false; + for (int i = 0; i < mappings.length(); i++) + { + String from = mappings.getJSONObject(i).getString("from"); + String to = mappings.getJSONObject(i).getString("to"); + if (from.equalsIgnoreCase("whatever") && to.equalsIgnoreCase("rmc:Wibble")) + { + result = true; + break; + } + } + return result; + } +} \ No newline at end of file