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
This commit is contained in:
Tuna Aksoy
2012-12-05 23:00:59 +00:00
parent 626f40a215
commit ec2f72650f
3 changed files with 123 additions and 182 deletions

View File

@@ -1,29 +1,29 @@
<webscript> <webscript>
<shortname>Get the custom email property map</shortname> <shortname>Get the custom email property map</shortname>
<description><![CDATA[ <description><![CDATA[
Get the custom email property map. Get the custom email property map.
<br /> <br />
fields are specified with "from" and "to". fields are specified with "from" and "to".
<br /> <br />
Example data. Example data.
<pre> <pre>
"mappings": "mappings":
[ [
{"from" : "messageTo", "to" : "imap:messageTo" } , {"from" : "messageTo", "to" : "imap:messageTo" } ,
{"from" : "Thread-Index", "to" : "imap:threadIndex" } , {"from" : "Thread-Index", "to" : "imap:threadIndex" } ,
{"from" : "messageFrom", "to" : "imap:messageFrom" } , {"from" : "messageFrom", "to" : "imap:messageFrom" } ,
{"from" : "messageSubject", "to" : "cm:title" } , {"from" : "messageSubject", "to" : "cm:title" } ,
{"from" : "messageSubject", "to" : "imap:messageSubject" } , {"from" : "messageSubject", "to" : "imap:messageSubject" } ,
{"from" : "messageSubject", "to" : "cm:description" } , {"from" : "messageSubject", "to" : "cm:description" } ,
{"from" : "messageCc", "to" : "imap:messageCc" } , {"from" : "messageCc", "to" : "imap:messageCc" } ,
{"from" : "Message-ID", "to" : "imap:messageId" } {"from" : "Message-ID", "to" : "imap:messageId" }
] ]
</pre> </pre>
]]> ]]>
</description> </description>
<url>/api/rma/admin/emailmap</url> <url>/api/rma/admin/emailmap</url>
<format default="json">argument</format> <format default="json">argument</format>
<authentication>user</authentication> <authentication>user</authentication>
<transaction allow="readonly">required</transaction> <transaction allow="readonly">required</transaction>
<lifecycle>internal</lifecycle> <lifecycle>internal</lifecycle>
</webscript> </webscript>

View File

@@ -1,40 +1,20 @@
<webscript> <webscript>
<shortname>Update email property map</shortname> <shortname>Add email property map</shortname>
<description><![CDATA[ <description><![CDATA[
Update the custom email property map Add the custom email property map
<br /> <br />
Data is specified in JSON format as a JSONObject with two optional fields, "add" and "delete". Data is specified in JSON format as a JSONObject
<br /> <pre>
The contents of the add array are added. {"to":"rmc:Wibble", "from":"whatever"}
<br /> </pre>
The contents of the delete array are deleted. <br />
<br /> POST /api/rma/admin/emailmap adds the specified mapping
Add mapping: Returns data in the same format as the get method
<pre> ]]>
{ </description>
"add": <url>/api/rma/admin/emailmap</url>
[ <format default="json">argument</format>
{"to":"rmc:Wibble", "from":"whatever"}, <authentication>user</authentication>
{"to":"rmc:wobble", "from":"whatever"} <transaction>required</transaction>
] <lifecycle>internal</lifecycle>
}
</pre>
Delete mapping:
<pre>
{
"delete":
[
{"to":"rmc:Wibble", "from":"whatever"},
{"to":"rmc:wobble", "from":"whatever"}
]
}
</pre>
Returns data in the same format as the get method
]]>
</description>
<url>/api/rma/admin/emailmap</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
<lifecycle>internal</lifecycle>
</webscript> </webscript>

View File

@@ -18,133 +18,94 @@
*/ */
package org.alfresco.module.org_alfresco_module_rm.test.webscript; 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.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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.extensions.webscripts.Status; 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.GetRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response; import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
public class EmailMapScriptTest extends BaseRMWebScriptTestCase public class EmailMapScriptTest extends BaseRMWebScriptTestCase
{ {
/** URLs for the REST APIs */
public final static String URL_RM_EMAILMAP = "/api/rma/admin/emailmap"; public final static String URL_RM_EMAILMAP = "/api/rma/admin/emailmap";
public final static String URL_RM_EMAILMAP_DELETE = "/api/rma/admin/emailmap/%s/%s";
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());
JSONObject top = new JSONObject(response.getContentAsString()); /** Constant for the content type */
JSONObject data = top.getJSONObject("data"); private static final String APPLICATION_JSON = "application/json";
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);
}
}
}
/**
* 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;
}
}