mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
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:
@@ -1,34 +1,14 @@
|
|||||||
<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 />
|
|
||||||
The contents of the add array are added.
|
|
||||||
<br />
|
|
||||||
The contents of the delete array are deleted.
|
|
||||||
<br />
|
|
||||||
Add mapping:
|
|
||||||
<pre>
|
<pre>
|
||||||
{
|
{"to":"rmc:Wibble", "from":"whatever"}
|
||||||
"add":
|
|
||||||
[
|
|
||||||
{"to":"rmc:Wibble", "from":"whatever"},
|
|
||||||
{"to":"rmc:wobble", "from":"whatever"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
</pre>
|
|
||||||
Delete mapping:
|
|
||||||
<pre>
|
|
||||||
{
|
|
||||||
"delete":
|
|
||||||
[
|
|
||||||
{"to":"rmc:Wibble", "from":"whatever"},
|
|
||||||
{"to":"rmc:wobble", "from":"whatever"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
</pre>
|
</pre>
|
||||||
|
<br />
|
||||||
|
POST /api/rma/admin/emailmap adds the specified mapping
|
||||||
Returns data in the same format as the get method
|
Returns data in the same format as the get method
|
||||||
]]>
|
]]>
|
||||||
</description>
|
</description>
|
||||||
|
@@ -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;
|
/** Constant for the content type */
|
||||||
|
private static final String APPLICATION_JSON = "application/json";
|
||||||
@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
|
* Tests the REST APIs for a custom mapping
|
||||||
*/
|
|
||||||
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
|
* @throws IOException
|
||||||
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
|
public void testEmailMap() throws IOException, JSONException
|
||||||
{
|
{
|
||||||
JSONObject obj = new JSONObject();
|
// Test Get
|
||||||
JSONObject val2 = new JSONObject();
|
Response getResponse = sendRequest(new GetRequest(URL_RM_EMAILMAP), Status.STATUS_OK);
|
||||||
JSONArray delete = new JSONArray();
|
|
||||||
val2.put("from", "whatever");
|
JSONObject getResponseContent = new JSONObject(getResponse.getContentAsString());
|
||||||
val2.put("to", "rmc:wobble");
|
JSONObject getData = getResponseContent.getJSONObject("data");
|
||||||
delete.put(val2);
|
JSONArray getMappings = getData.getJSONArray("mappings");
|
||||||
obj.put("delete", delete);
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Now do a post to delete a couple of values
|
* 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
|
||||||
*/
|
*/
|
||||||
Response response = sendRequest(new PostRequest(URL_RM_EMAILMAP, obj.toString(), "application/json"), Status.STATUS_OK);
|
private boolean existsMapping(JSONArray mappings) throws JSONException
|
||||||
System.out.println(response.getContentAsString());
|
{
|
||||||
|
boolean result = false;
|
||||||
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++)
|
for (int i = 0; i < mappings.length(); i++)
|
||||||
{
|
{
|
||||||
JSONObject mapping = mappings.getJSONObject(i);
|
String from = mappings.getJSONObject(i).getString("from");
|
||||||
|
String to = mappings.getJSONObject(i).getString("to");
|
||||||
|
if (from.equalsIgnoreCase("whatever") && to.equalsIgnoreCase("rmc:Wibble"))
|
||||||
if(mapping.get("from").equals("whatever"))
|
|
||||||
{
|
{
|
||||||
if(mapping.get("to").equals("rmc:Wibble"))
|
result = true;
|
||||||
{
|
break;
|
||||||
wibbleFound = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fail("custom mapping for field not deleted");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return result;
|
||||||
assertTrue(wibbleFound);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user