diff --git a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml index 2d7ae00b0a..9636dafab8 100644 --- a/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml +++ b/rm-server/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml @@ -514,7 +514,7 @@ - + @@ -522,7 +522,7 @@ class="org.alfresco.repo.web.scripts.rule.RmActionConditionDefinitionsGet" parent="webscript"> - + @@ -539,4 +539,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.delete.desc.xml b/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.delete.desc.xml new file mode 100644 index 0000000000..2aaf104c0f --- /dev/null +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.delete.desc.xml @@ -0,0 +1,12 @@ + + Remove a group or a user from a role + + + /api/rm/role/{roleId}/children/{authorityName} + /api/rm/{store_type}/{store_id}/{id}/role/{roleId}/children/{authorityName} + argument + user + required + \ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.delete.json.ftl b/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.delete.json.ftl new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.delete.json.ftl @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.post.desc.xml b/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.post.desc.xml new file mode 100644 index 0000000000..7ddf219242 --- /dev/null +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.post.desc.xml @@ -0,0 +1,12 @@ + + Add a group or a user to a role + + + /api/rm/role/{roleId}/children/{authorityName} + /api/rm/{store_type}/{store_id}/{id}/role/{roleId}/children/{authorityName} + argument + user + required + \ No newline at end of file diff --git a/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.post.json.ftl b/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.post.json.ftl new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/rm-server/config/alfresco/templates/webscripts/org/alfresco/repository/groups/rm-children.post.json.ftl @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenAbstract.java b/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenAbstract.java new file mode 100644 index 0000000000..f1b594aae0 --- /dev/null +++ b/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenAbstract.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2005-2013 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.web.scripts.groups; + +import java.util.Map; + +import org.alfresco.module.org_alfresco_module_rm.script.admin.RoleDeclarativeWebScript; +import org.alfresco.service.cmr.repository.NodeRef; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptException; +import org.springframework.extensions.webscripts.WebScriptRequest; +import org.tuckey.web.filters.urlrewrite.utils.StringUtils; + +/** + * Abstract class for adding/removing a user/group to/from a role + * This class contains the common methods needed in the sub classes. + * + * @author Tuna Aksoy + * @since 2.1 + */ +public class RmChildrenAbstract extends RoleDeclarativeWebScript +{ + /** Constants for the url parameters */ + private static final String ROLE_ID = "roleId"; + private static final String AUTHORITY_NAME = "authorityName"; + + /** + * Util method for getting the nodeRef from the request + * + * @param req The webscript request + * @return The nodeRef passed in the request + */ + protected NodeRef getFilePlan(WebScriptRequest req) + { + NodeRef filePlan = super.getFilePlan(req); + if (filePlan == null) + { + throw new WebScriptException(Status.STATUS_NOT_FOUND, "No filePlan was provided on the URL."); + } + return filePlan; + } + + /** + * Util method for getting the roleId from the request + * + * @param req The webscript request + * @return The role id passed in the request + */ + protected String getRoleId(WebScriptRequest req) + { + return getParamValue(req, ROLE_ID); + } + + /** + * Util method for getting the authorityName from the request + * + * @param req The webscript request + * @return The authorityName passed in the request + */ + protected String getAuthorityName(WebScriptRequest req) + { + return getParamValue(req, AUTHORITY_NAME); + } + + /** + * Helper method to get the value of parameter from the request + * + * @param req The webscript request + * @param param The name of the parameter for which the value is requested + * @return The value for the requested parameter + */ + private String getParamValue(WebScriptRequest req, String param) + { + Map templateVars = req.getServiceMatch().getTemplateVars(); + + String authorityName = templateVars.get(param); + if (StringUtils.isBlank(authorityName)) + { + throw new WebScriptException(Status.STATUS_NOT_FOUND, "No '" + param + "' was provided on the URL."); + } + return authorityName; + } +} diff --git a/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenDelete.java b/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenDelete.java new file mode 100644 index 0000000000..597520e7e5 --- /dev/null +++ b/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenDelete.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2005-2013 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.web.scripts.groups; + +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.service.cmr.repository.NodeRef; +import org.springframework.extensions.webscripts.Cache; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptRequest; + +/** + * Webscript for removing a user or a group from a role + * + * @author Tuna Aksoy + * @since 2.1 + */ +public class RmChildrenDelete extends RmChildrenAbstract +{ + /** + * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, + * org.springframework.extensions.webscripts.Status, + * org.springframework.extensions.webscripts.Cache) + */ + @Override + protected Map executeImpl(WebScriptRequest req, Status status, Cache cache) + { + NodeRef filePlan = getFilePlan(req); + String roleId = getRoleId(req); + String authorityName = getAuthorityName(req); + + filePlanRoleService.unassignRoleFromAuthority(filePlan, roleId, authorityName); + + return new HashMap(); + } +} diff --git a/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenPost.java b/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenPost.java new file mode 100644 index 0000000000..ca20dd7c1a --- /dev/null +++ b/rm-server/source/java/org/alfresco/repo/web/scripts/groups/RmChildrenPost.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2005-2013 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.web.scripts.groups; + +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.service.cmr.repository.NodeRef; +import org.springframework.extensions.webscripts.Cache; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptRequest; + +/** + * Webscript for adding a user or a group to a role + * + * @author Tuna Aksoy + * @since 2.1 + */ +public class RmChildrenPost extends RmChildrenAbstract +{ + /** + * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, + * org.springframework.extensions.webscripts.Status, + * org.springframework.extensions.webscripts.Cache) + */ + @Override + protected Map executeImpl(WebScriptRequest req, Status status, Cache cache) + { + NodeRef filePlan = getFilePlan(req); + String roleId = getRoleId(req); + String authorityName = getAuthorityName(req); + + filePlanRoleService.assignRoleToAuthority(filePlan, roleId, authorityName); + + return new HashMap(); + } +} 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 04f9ccbdc6..77d6c58674 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 @@ -27,6 +27,7 @@ import org.alfresco.module.org_alfresco_module_rm.test.webscript.EmailMapScriptT import org.alfresco.module.org_alfresco_module_rm.test.webscript.EventRestApiTest; import org.alfresco.module.org_alfresco_module_rm.test.webscript.RMCaveatConfigScriptTest; import org.alfresco.module.org_alfresco_module_rm.test.webscript.RMConstraintScriptTest; +import org.alfresco.module.org_alfresco_module_rm.test.webscript.RmChildrenRestApiTest; import org.alfresco.module.org_alfresco_module_rm.test.webscript.RoleRestApiTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -51,10 +52,10 @@ import org.junit.runners.Suite.SuiteClasses; EmailMapScriptTest.class, EmailMapKeysRestApiTest.class, CapabilitiesRestApiTest.class, - ActionDefinitionsRestApiTest.class + ActionDefinitionsRestApiTest.class, //RmClassesRestApiTest.class, - //RmPropertiesRestApiTest.class - + //RmPropertiesRestApiTest.class, + RmChildrenRestApiTest.class }) public class WebScriptTestSuite { diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/BaseRMWebScriptTestCase.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/BaseRMWebScriptTestCase.java index 7b793f04d5..d89fb83cc0 100644 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/BaseRMWebScriptTestCase.java +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/BaseRMWebScriptTestCase.java @@ -51,6 +51,7 @@ import org.alfresco.service.cmr.repository.Period; import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.search.SearchService; import org.alfresco.service.cmr.security.AuthorityService; +import org.alfresco.service.cmr.security.AuthorityType; import org.alfresco.service.cmr.security.MutableAuthenticationService; import org.alfresco.service.cmr.security.PersonService; import org.alfresco.service.cmr.site.SiteInfo; @@ -60,6 +61,7 @@ import org.alfresco.service.cmr.tagging.TaggingService; import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.QName; import org.alfresco.service.transaction.TransactionService; +import org.alfresco.util.PropertyMap; import org.springframework.context.ApplicationContext; /** @@ -228,12 +230,12 @@ public class BaseRMWebScriptTestCase extends BaseWebScriptTest @Override public Object execute() throws Throwable { - AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName()); + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); setupTestDataImpl(); return null; } }); - + retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback() { @Override @@ -330,4 +332,40 @@ public class BaseRMWebScriptTestCase extends BaseWebScriptTest assertNotNull("Collaboration site document library component was not successfully created.", documentLibrary); } + + protected void createUser(String userName) + { + if (authenticationService.authenticationExists(userName) == false) + { + authenticationService.createAuthentication(userName, "PWD".toCharArray()); + + PropertyMap ppOne = new PropertyMap(4); + ppOne.put(ContentModel.PROP_USERNAME, userName); + ppOne.put(ContentModel.PROP_AUTHORITY_DISPLAY_NAME, "title" + userName); + ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName"); + ppOne.put(ContentModel.PROP_LASTNAME, "lastName"); + ppOne.put(ContentModel.PROP_EMAIL, "email@email.com"); + ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle"); + + personService.createPerson(ppOne); + } + } + + protected void deleteUser(String userName) + { + personService.deletePerson(userName); + } + + protected void createGroup(String groupName) + { + if (authorityService.authorityExists(groupName) == false) + { + authorityService.createAuthority(AuthorityType.GROUP, groupName); + } + } + + protected void deleteGroup(String groupName) + { + authorityService.deleteAuthority(groupName, true); + } } diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RMCaveatConfigScriptTest.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RMCaveatConfigScriptTest.java index ab295efaec..838de9a444 100644 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RMCaveatConfigScriptTest.java +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RMCaveatConfigScriptTest.java @@ -38,73 +38,49 @@ import org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.Response; /** - * + * * * @author Mark Rogers */ @SuppressWarnings("unused") public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase { - private MutableAuthenticationService authenticationService; private RMCaveatConfigService caveatConfigService; - private PersonService personService; - - + protected final static String RM_LIST = "rmc:smListTest"; protected final static String RM_LIST_URI_ELEM = "rmc_smListTest"; - + private static final String URL_RM_CONSTRAINTS = "/api/rma/admin/rmconstraints"; - + @Override - protected void initServices() + protected void initServices() { super.initServices(); - + this.caveatConfigService = (RMCaveatConfigService)getServer().getApplicationContext().getBean("CaveatConfigService"); - this.authenticationService = (MutableAuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService"); - this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService"); } - - private void createUser(String userName) - { - if (this.authenticationService.authenticationExists(userName) == false) - { - this.authenticationService.createAuthentication(userName, "PWD".toCharArray()); - - PropertyMap ppOne = new PropertyMap(4); - ppOne.put(ContentModel.PROP_USERNAME, userName); - ppOne.put(ContentModel.PROP_AUTHORITY_DISPLAY_NAME, "title" + userName); - ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName"); - ppOne.put(ContentModel.PROP_LASTNAME, "lastName"); - ppOne.put(ContentModel.PROP_EMAIL, "email@email.com"); - ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle"); - - this.personService.createPerson(ppOne); - } - } - - + public void testGetRMConstraints() throws Exception { { - Response response = sendRequest(new GetRequest(URL_RM_CONSTRAINTS), Status.STATUS_OK); - + Response response = sendRequest(new GetRequest(URL_RM_CONSTRAINTS), Status.STATUS_OK); + JSONObject top = new JSONObject(response.getContentAsString()); System.out.println(response.getContentAsString()); assertNotNull(top.getJSONArray("data")); } - + /** * Add a list, then get it back via the list rest script */ caveatConfigService.addRMConstraint(RM_LIST, "my title", new String[0]); - + { - Response response = sendRequest(new GetRequest(URL_RM_CONSTRAINTS), Status.STATUS_OK); + Response response = sendRequest(new GetRequest(URL_RM_CONSTRAINTS), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); System.out.println(response.getContentAsString()); JSONArray data = top.getJSONArray("data"); - + boolean found = false; assertTrue("no data returned", data.length() > 0); for(int i = 0; i < data.length(); i++) @@ -118,17 +94,17 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase { found = true; } - + /** * vallidate the detail URL returned */ - sendRequest(new GetRequest(url), Status.STATUS_OK); - } - } + sendRequest(new GetRequest(url), Status.STATUS_OK); + } + } } - + /** - * + * * @throws Exception */ public void testGetRMConstraint() throws Exception @@ -141,75 +117,75 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase caveatConfigService.deleteRMConstraint(RM_LIST); } caveatConfigService.addRMConstraint(RM_LIST, "my title", new String[0]); - - + + createUser("fbloggs"); createUser("jrogers"); createUser("jdoe"); - - + + List values = new ArrayList(); values.add("NOFORN"); values.add("FGI"); caveatConfigService.updateRMConstraintListAuthority(RM_LIST, "fbloggs", values); caveatConfigService.updateRMConstraintListAuthority(RM_LIST, "jrogers", values); caveatConfigService.updateRMConstraintListAuthority(RM_LIST, "jdoe", values); - + /** - * Positive test Get the constraint + * Positive test Get the constraint */ { String url = URL_RM_CONSTRAINTS + "/" + RM_LIST_URI_ELEM; Response response = sendRequest(new GetRequest(url), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); - + String constraintName = data.getString("constraintName"); assertNotNull("constraintName is null", constraintName); // JSONArray allowedValues = data.getJSONArray("allowedValues"); - + // assertTrue("values not correct", compare(array, allowedValues)); - + // JSONArray constraintDetails = data.getJSONArray("constraintDetails"); -// +// // assertTrue("details array does not contain 3 elements", constraintDetails.length() == 3); // for(int i =0; i < constraintDetails.length(); i++) // { // JSONObject detail = constraintDetails.getJSONObject(i); // } } - + /** - * + * * @throws Exception */ - + /** * Negative test - Attempt to get a constraint that does exist */ { String url = URL_RM_CONSTRAINTS + "/" + "rmc_wibble"; - sendRequest(new GetRequest(url), Status.STATUS_NOT_FOUND); + sendRequest(new GetRequest(url), Status.STATUS_NOT_FOUND); } - - personService.deletePerson("fbloggs"); - personService.deletePerson("jrogers"); - personService.deletePerson("jdoe"); - - - - + + deleteUser("fbloggs"); + deleteUser("jrogers"); + deleteUser("jdoe"); + + + + } - + /** * Create an RM Constraint * @throws Exception */ public void testUpdateRMConstraint() throws Exception { - + String constraintName = null; /* * Create a new list @@ -220,64 +196,64 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase array.put("LEMON"); array.put("BANANA"); array.put("PEACH"); - + JSONObject obj = new JSONObject(); obj.put("allowedValues", array); obj.put("constraintTitle", title); /** * Now do a post to create a new list - */ - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); + */ + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - - JSONObject data = top.getJSONObject("data"); - constraintName = data.getString("constraintName"); + + JSONObject data = top.getJSONObject("data"); + constraintName = data.getString("constraintName"); JSONArray allowedValues = data.getJSONArray("allowedValues"); assertTrue("values not correct", compare(array, allowedValues)); - + } - + /** * Now update both values and title - remove BANANA, PEACH, Add APPLE. */ - + { String newTitle = "this is the new title"; JSONArray array = new JSONArray(); array.put("LEMON"); array.put("APPLE"); - + JSONObject obj = new JSONObject(); obj.put("allowedValues", array); obj.put("constraintName", constraintName); obj.put("constraintTitle", newTitle); - + System.out.println(obj.toString()); - + /** * Now do a post to update list */ - Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); + Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); // Check the response JSONObject top = new JSONObject(response.getContentAsString()); JSONObject data = top.getJSONObject("data"); - + System.out.println(response.getContentAsString()); - + String url = data.getString("url"); String constraintName2 = data.getString("constraintName"); String constraintTitle = data.getString("constraintTitle"); JSONArray allowedValues = data.getJSONArray("allowedValues"); - - assertTrue(allowedValues.length() == 2); + + assertTrue(allowedValues.length() == 2); assertTrue("values not correct", compare(array, allowedValues)); assertNotNull(url); assertEquals(constraintName2, constraintName); assertNotNull(constraintTitle); assertEquals("title not as expected", constraintTitle, newTitle); - + // Check that data has been persisted. - Response resp2 = sendRequest(new GetRequest(url), Status.STATUS_OK); + Response resp2 = sendRequest(new GetRequest(url), Status.STATUS_OK); JSONObject top2 = new JSONObject(resp2.getContentAsString()); System.out.println("Problem here"); System.out.println(resp2.getContentAsString()); @@ -285,48 +261,48 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase String constraintTitle2 = data2.getString("constraintTitle"); JSONArray allowedValues2 = data2.getJSONArray("allowedValues"); assertTrue("values not correct", compare(array, allowedValues2)); - assertTrue("allowedValues is not 2", allowedValues2.length() == 2); + assertTrue("allowedValues is not 2", allowedValues2.length() == 2); assertEquals(constraintName2, constraintName); assertNotNull(constraintTitle2); assertEquals("title not as expected", constraintTitle2, newTitle); - + } - + /** * Now put without allowed values */ { String newTitle = "update with no values"; - + JSONObject obj = new JSONObject(); obj.put("constraintName", RM_LIST); obj.put("constraintTitle", newTitle); - + /** * Now do a put to update a new list */ - - Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); + + Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); // Check the response JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); - + String url = data.getString("url"); String constraintName2 = data.getString("constraintName"); String constraintTitle = data.getString("constraintTitle"); JSONArray allowedValues = data.getJSONArray("allowedValues"); - - assertTrue(allowedValues.length() == 2); - + + assertTrue(allowedValues.length() == 2); + assertNotNull(url); assertEquals(constraintName2, constraintName); assertNotNull(constraintTitle); assertEquals("title not as expected", constraintTitle, newTitle); } - + /** * Now post without constraint Title */ @@ -334,24 +310,24 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase JSONArray array = new JSONArray(); array.put("LEMON"); array.put("APPLE"); - + JSONObject obj = new JSONObject(); obj.put("allowedValues", array); - + System.out.println(obj.toString()); - + /** * Now do a Put to update the list - title should remain */ - - Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); + + Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); // Check the response JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); } - + /** * Add a new value (PEAR) to the list */ @@ -360,20 +336,20 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase array.put("PEAR"); array.put("LEMON"); array.put("APPLE"); - + JSONObject obj = new JSONObject(); obj.put("allowedValues", array); - + System.out.println(obj.toString()); - - Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); + + Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); // Check the response JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); } - + /** * Remove a value (PEAR) from the list */ @@ -381,23 +357,23 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase JSONArray array = new JSONArray(); array.put("APPLE"); array.put("LEMON"); - + JSONObject obj = new JSONObject(); obj.put("allowedValues", array); - + System.out.println(obj.toString()); - - Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); + + Response response = sendRequest(new PutRequest(URL_RM_CONSTRAINTS + "/" + constraintName, obj.toString(), "application/json"), Status.STATUS_OK); // Check the response JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); } - + } - + /** * Create an RM Constraint * @throws Exception @@ -410,51 +386,51 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase //caveatConfigService.deleteRMConstraint(RM_LIST); /** - * create a new list + * create a new list */ { JSONArray array = new JSONArray(); array.put("NOFORN"); array.put("FGI"); - + JSONObject obj = new JSONObject(); obj.put("allowedValues", array); obj.put("constraintName", RM_LIST); obj.put("constraintTitle", GUID.generate()); - + System.out.println(obj.toString()); - + /** * Now do a post to create a new list */ - - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); - // Check the response + + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); + // Check the response } - + /** - * Now go and get the constraint + * Now go and get the constraint */ { String url = URL_RM_CONSTRAINTS + "/" + RM_LIST_URI_ELEM; Response response = sendRequest(new GetRequest(url), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); - + String constraintName = data.getString("constraintName"); assertNotNull("constraintName is null", constraintName); - + // JSONArray constraintDetails = data.getJSONArray("constraintDetails"); -// +// // assertTrue("details array does not contain 3 elements", constraintDetails.length() == 3); // for(int i =0; i < constraintDetails.length(); i++) // { // JSONObject detail = constraintDetails.getJSONObject(i); // } } - + /** * Now a constraint with a generated name */ @@ -464,40 +440,40 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase array.put("Red"); array.put("Blue"); array.put("Green"); - + JSONObject obj = new JSONObject(); obj.put("allowedValues", array); obj.put("constraintTitle", title); - + System.out.println(obj.toString()); - + /** * Now do a post to create a new list - */ - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); + */ + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); - - // Check the response - + + // Check the response + String url = data.getString("url"); String constraintName = data.getString("constraintName"); String constraintTitle = data.getString("constraintTitle"); JSONArray allowedValues = data.getJSONArray("allowedValues"); - - assertTrue(allowedValues.length() == 3); + + assertTrue(allowedValues.length() == 3); assertNotNull(url); assertNotNull(constraintName); assertNotNull(constraintTitle); assertEquals("title not as expected", constraintTitle, title); - sendRequest(new GetRequest(url), Status.STATUS_OK); - - + sendRequest(new GetRequest(url), Status.STATUS_OK); + + } - + /** * Now a constraint with an empty list of values. */ @@ -508,23 +484,23 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase obj.put("allowedValues", array); obj.put("constraintName", "rmc_whazoo"); obj.put("constraintTitle", GUID.generate()); - + System.out.println(obj.toString()); - + /** * Now do a post to create a new list */ - - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); + + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); - // Check the response + // Check the response } - + // /** // * Negative tests - duplicate list // */ @@ -532,36 +508,36 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase // JSONArray array = new JSONArray(); // array.put("NOFORN"); // array.put("FGI"); -// +// // JSONObject obj = new JSONObject(); // obj.put("allowedValues", array); // obj.put("constraintName", RM_LIST); // obj.put("constraintTitle", "this is the title"); -// +// // System.out.println(obj.toString()); -// +// // /** // * Now do a post to create a new list // */ -// Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_CREATED); +// Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_CREATED); // JSONObject top = new JSONObject(response.getContentAsString()); -// +// // JSONObject data = top.getJSONObject("data"); // System.out.println(response.getContentAsString()); // -// // Check the response +// // Check the response // } - + } - - + + public void testGetRMConstraintValues() throws Exception { createUser("fbloggs"); createUser("jrogers"); createUser("jdoe"); - + /** * Delete the list to remove any junk then recreate it. */ @@ -571,7 +547,7 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase caveatConfigService.deleteRMConstraint(RM_LIST); } caveatConfigService.addRMConstraint(RM_LIST, "my title", new String[0]); - + List values = new ArrayList(); values.add("NOFORN"); values.add("FGI"); @@ -579,57 +555,57 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase caveatConfigService.updateRMConstraintListAuthority(RM_LIST, "jrogers", values); caveatConfigService.updateRMConstraintListAuthority(RM_LIST, "jdoe", values); } - + /** - * Positive test Get the constraint + * Positive test Get the constraint */ { String url = URL_RM_CONSTRAINTS + "/" + RM_LIST_URI_ELEM + "/values"; Response response = sendRequest(new GetRequest(url), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); - + String constraintName = data.getString("constraintName"); assertNotNull("constraintName is null", constraintName); String constraintTitle = data.getString("constraintTitle"); assertNotNull("constraintTitle is null", constraintTitle); - + JSONArray values = data.getJSONArray("values"); - + assertTrue("details array does not contain 2 elements", values.length() == 2); boolean fgiFound = false; boolean nofornFound = false; - + for(int i =0; i < values.length(); i++) { JSONObject value = values.getJSONObject(i); - + if(value.getString("valueName").equalsIgnoreCase("FGI")) { fgiFound = true; - + } - + if(value.getString("valueName").equalsIgnoreCase("NOFORN")) { nofornFound = true; } - - + + } assertTrue("fgi not found", fgiFound); assertTrue("noforn not found", nofornFound); } - - personService.deletePerson("fbloggs"); - personService.deletePerson("jrogers"); - personService.deletePerson("jdoe"); + + deleteUser("fbloggs"); + deleteUser("jrogers"); + deleteUser("jdoe"); } - - + + /** * Update a value in a constraint * @throws Exception @@ -641,74 +617,74 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase caveatConfigService.deleteRMConstraint(RM_LIST); } caveatConfigService.addRMConstraint(RM_LIST, "my title", new String[0]); - + /** * Add some data to an empty list */ - { + { JSONArray values = new JSONArray(); - + JSONArray authorities = new JSONArray(); authorities.put("fbloggs"); authorities.put("jdoe"); - + JSONObject valueA = new JSONObject(); valueA.put("value", "NOFORN"); - valueA.put("authorities", authorities); - + valueA.put("authorities", authorities); + values.put(valueA); - + JSONObject valueB = new JSONObject(); valueB.put("value", "FGI"); - valueB.put("authorities", authorities); - + valueB.put("authorities", authorities); + values.put(valueB); - + JSONObject obj = new JSONObject(); obj.put("values", values); - - + + /** * Do the first update - should get back * NOFORN - fbloggs, jdoe * FGI - fbloggs, jdoe */ - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST + "/values" , obj.toString(), "application/json"), Status.STATUS_OK); + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST + "/values" , obj.toString(), "application/json"), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); assertNotNull("data is null", data); - + JSONArray myValues = data.getJSONArray("values"); - assertTrue("two values not found", myValues.length() == 2); + assertTrue("two values not found", myValues.length() == 2); for(int i = 0; i < myValues.length(); i++) { JSONObject myObj = myValues.getJSONObject(i); - } - } - + } + } + /** * Add to a new value, NOCON, fbloggs, jrogers */ - { + { JSONArray values = new JSONArray(); - + JSONArray authorities = new JSONArray(); authorities.put("fbloggs"); authorities.put("jrogers"); - + JSONObject valueA = new JSONObject(); valueA.put("value", "NOCON"); - valueA.put("authorities", authorities); - + valueA.put("authorities", authorities); + values.put(valueA); - - + + JSONObject obj = new JSONObject(); obj.put("values", values); - - + + /** * Add a new value - should get back * NOFORN - fbloggs, jdoe @@ -716,21 +692,21 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase * NOCON - fbloggs, jrogers */ System.out.println(obj.toString()); - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST + "/values" , obj.toString(), "application/json"), Status.STATUS_OK); + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST + "/values" , obj.toString(), "application/json"), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); assertNotNull("data is null", data); - + JSONArray myValues = data.getJSONArray("values"); - assertTrue("three values not found", myValues.length() == 3); + assertTrue("three values not found", myValues.length() == 3); for(int i = 0; i < myValues.length(); i++) { JSONObject myObj = myValues.getJSONObject(i); } } - + /** * Add to an existing value (NOFORN, jrogers) * should get back @@ -740,81 +716,81 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase */ { JSONArray values = new JSONArray(); - + JSONArray authorities = new JSONArray(); authorities.put("fbloggs"); authorities.put("jrogers"); authorities.put("jdoe"); - + JSONObject valueA = new JSONObject(); valueA.put("value", "NOFORN"); - valueA.put("authorities", authorities); - + valueA.put("authorities", authorities); + values.put(valueA); - - + + JSONObject obj = new JSONObject(); obj.put("values", values); - - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST + "/values" , obj.toString(), "application/json"), Status.STATUS_OK); + + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST + "/values" , obj.toString(), "application/json"), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); assertNotNull("data is null", data); - + JSONArray myValues = data.getJSONArray("values"); - assertTrue("three values not found", myValues.length() == 3); + assertTrue("three values not found", myValues.length() == 3); for(int i = 0; i < myValues.length(); i++) { JSONObject myObj = myValues.getJSONObject(i); } } - + /** * Remove from existing value (NOCON, fbloggs) */ { JSONArray values = new JSONArray(); - + JSONArray authorities = new JSONArray(); authorities.put("jrogers"); - + JSONObject valueA = new JSONObject(); valueA.put("value", "NOCON"); - valueA.put("authorities", authorities); - + valueA.put("authorities", authorities); + values.put(valueA); - - + + JSONObject obj = new JSONObject(); obj.put("values", values); - - + + /** * should get back * NOFORN - fbloggs, jdoe * FGI - fbloggs, jdoe * NOCON - jrogers */ - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST + "/values" , obj.toString(), "application/json"), Status.STATUS_OK); + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST + "/values" , obj.toString(), "application/json"), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); assertNotNull("data is null", data); - + JSONArray myValues = data.getJSONArray("values"); - assertTrue("three values not found", myValues.length() == 3); + assertTrue("three values not found", myValues.length() == 3); boolean foundNOCON = false; boolean foundNOFORN = false; boolean foundFGI = false; - + for(int i = 0; i < myValues.length(); i++) { JSONObject myObj = myValues.getJSONObject(i); - + if(myObj.getString("valueName").equalsIgnoreCase("NOCON")) { foundNOCON = true; @@ -828,17 +804,17 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase foundFGI = true; } } - + assertTrue("not found NOCON", foundNOCON); assertTrue("not found NOFORN", foundNOFORN); assertTrue("not found FGI", foundFGI); } } - - + + /** * Delete the entire constraint - * + * * @throws Exception */ public void testDeleteRMConstraint() throws Exception @@ -851,20 +827,20 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase caveatConfigService.deleteRMConstraint(RM_LIST); } caveatConfigService.addRMConstraint(RM_LIST, "my title", new String[0]); - + /** * Now do a delete */ - Response response = sendRequest(new DeleteRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST), Status.STATUS_OK); - + Response response = sendRequest(new DeleteRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST), Status.STATUS_OK); + /** * Now delete the list that should have been deleted */ // TODO NEED TO THINK ABOUT THIS BEHAVIOUR //{ - // sendRequest(new DeleteRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST), Status.STATUS_NOT_FOUND); + // sendRequest(new DeleteRequest(URL_RM_CONSTRAINTS + "/" + RM_LIST), Status.STATUS_NOT_FOUND); //} - + /** * Negative test - delete list that does not exist */ @@ -872,29 +848,29 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase sendRequest(new DeleteRequest(URL_RM_CONSTRAINTS + "/" + "rmc_wibble"), Status.STATUS_NOT_FOUND); } } - + private boolean compare(JSONArray from, JSONArray to) throws Exception { List ret = new ArrayList(); - + if(from.length() != to.length()) { - fail("arrays are different lengths" + from.length() +", " + to.length()); + fail("arrays are different lengths" + from.length() +", " + to.length()); return false; } - + for(int i = 0 ; i < to.length(); i++) { ret.add(to.getString(i)); } - + for(int i = 0 ; i < from.length(); i++) { String val = from.getString(i); - + if(ret.contains(val)) { - + } else { @@ -902,20 +878,20 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase return false; } } - + return true; } - - + + /** * Create an RM Constraint value * @throws Exception */ public void testGetRMConstraintValue() throws Exception { - + String constraintName = null; - + /* * Create a new list */ @@ -925,22 +901,22 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase array.put("POTATO"); array.put("CARROT"); array.put("TURNIP"); - + JSONObject obj = new JSONObject(); obj.put("allowedValues", array); obj.put("constraintTitle", title); /** * Now do a post to create a new list - */ - Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); + */ + Response response = sendRequest(new PostRequest(URL_RM_CONSTRAINTS, obj.toString(), "application/json"), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - - JSONObject data = top.getJSONObject("data"); - constraintName = data.getString("constraintName"); + + JSONObject data = top.getJSONObject("data"); + constraintName = data.getString("constraintName"); JSONArray allowedValues = data.getJSONArray("allowedValues"); assertTrue("values not correct", compare(array, allowedValues)); } - + /** * Get the CARROT value */ @@ -949,7 +925,7 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase Response response = sendRequest(new GetRequest(url), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); } - + { String url = URL_RM_CONSTRAINTS + "/" + constraintName + "/values/" + "ONION"; sendRequest(new GetRequest(url), Status.STATUS_NOT_FOUND); diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RMConstraintScriptTest.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RMConstraintScriptTest.java index 6116258954..9d7054795f 100644 --- a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RMConstraintScriptTest.java +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RMConstraintScriptTest.java @@ -21,13 +21,9 @@ package org.alfresco.module.org_alfresco_module_rm.test.webscript; import java.util.ArrayList; import java.util.List; -import org.alfresco.model.ContentModel; import org.alfresco.module.org_alfresco_module_rm.caveat.RMCaveatConfigService; import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMWebScriptTestCase; import org.alfresco.repo.security.authentication.AuthenticationUtil; -import org.alfresco.service.cmr.security.MutableAuthenticationService; -import org.alfresco.service.cmr.security.PersonService; -import org.alfresco.util.PropertyMap; import org.json.JSONObject; import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; @@ -40,27 +36,23 @@ import org.springframework.extensions.webscripts.TestWebScriptServer.Response; */ public class RMConstraintScriptTest extends BaseRMWebScriptTestCase { - private MutableAuthenticationService authenticationService; private RMCaveatConfigService caveatConfigService; - private PersonService personService; - + protected final static String RM_LIST = "rmc:smListTest"; protected final static String RM_LIST_URI_ELEM = "rmc_smListTest"; - + private static final String URL_RM_CONSTRAINTS = "/api/rma/rmconstraints"; - + @Override - protected void initServices() + protected void initServices() { super.initServices(); - + this.caveatConfigService = (RMCaveatConfigService)getServer().getApplicationContext().getBean("CaveatConfigService"); - this.authenticationService = (MutableAuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService"); - this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService"); } - + /** - * + * * @throws Exception */ public void testGetRMConstraint() throws Exception @@ -76,62 +68,44 @@ public class RMConstraintScriptTest extends BaseRMWebScriptTestCase caveatConfigService.deleteRMConstraint(RM_LIST); } caveatConfigService.addRMConstraint(RM_LIST, "my title", new String[0]); - - + + createUser("fbloggs"); createUser("jrogers"); createUser("jdoe"); - - + + List values = new ArrayList(); values.add("NOFORN"); values.add("FGI"); caveatConfigService.updateRMConstraintListAuthority(RM_LIST, "fbloggs", values); caveatConfigService.updateRMConstraintListAuthority(RM_LIST, "jrogers", values); caveatConfigService.updateRMConstraintListAuthority(RM_LIST, "jdoe", values); - + AuthenticationUtil.setFullyAuthenticatedUser("jdoe"); /** - * Positive test Get the constraint + * Positive test Get the constraint */ { String url = URL_RM_CONSTRAINTS + "/" + RM_LIST_URI_ELEM; Response response = sendRequest(new GetRequest(url), Status.STATUS_OK); JSONObject top = new JSONObject(response.getContentAsString()); - + JSONObject data = top.getJSONObject("data"); System.out.println(response.getContentAsString()); - + data.getJSONArray("allowedValuesForCurrentUser"); } - + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); - personService.deletePerson("fbloggs"); - personService.deletePerson("jrogers"); - personService.deletePerson("jdoe"); - - } - - private void createUser(String userName) - { - if (this.authenticationService.authenticationExists(userName) == false) - { - this.authenticationService.createAuthentication(userName, "PWD".toCharArray()); - - PropertyMap ppOne = new PropertyMap(4); - ppOne.put(ContentModel.PROP_USERNAME, userName); - ppOne.put(ContentModel.PROP_AUTHORITY_DISPLAY_NAME, "title" + userName); - ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName"); - ppOne.put(ContentModel.PROP_LASTNAME, "lastName"); - ppOne.put(ContentModel.PROP_EMAIL, "email@email.com"); - ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle"); - - this.personService.createPerson(ppOne); - } + deleteUser("fbloggs"); + deleteUser("jrogers"); + deleteUser("jdoe"); + } + } - - - \ No newline at end of file + + diff --git a/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RmChildrenRestApiTest.java b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RmChildrenRestApiTest.java new file mode 100644 index 0000000000..db9033f4e9 --- /dev/null +++ b/rm-server/test/java/org/alfresco/module/org_alfresco_module_rm/test/webscript/RmChildrenRestApiTest.java @@ -0,0 +1,215 @@ +/* + * Copyright (C) 2005-2013 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.module.org_alfresco_module_rm.test.webscript; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Set; + +import org.alfresco.module.org_alfresco_module_rm.role.FilePlanRoleService; +import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMWebScriptTestCase; +import org.alfresco.service.cmr.repository.StoreRef; +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.PostRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.Response; + +/** + * REST API Tests for adding/removing users/groups to/from a role + * + * @author Tuna Aksoy + * @since 2.1 + */ +public class RmChildrenRestApiTest extends BaseRMWebScriptTestCase +{ + /** URL for the REST APIs */ + private static final String RM_CHILDREN_URL = "/api/rm/%s/role/%s/children/%s"; + + /** Constant for the content type */ + private static final String APPLICATION_JSON = "application/json"; + + /** + * Test the REST API to add/remove a user to/from a role + * + * @throws IOException + * @throws JSONException + */ + public void testRmAddRemoveUser() throws IOException, JSONException + { + // Create a test user + String userName = "geshjsjuasftg"; + createUser(userName); + + // Check if the user is already assigned to the role + assertFalse(getUsersAssignedToRole().contains(userName)); + + // Format url and send request + String url = getFormattedUrlString(userName); + Response response = postRequest(url); + + // Check the content from the response + checkContent(response); + + // The user should be added to the role + assertTrue(getUsersAssignedToRole().contains(userName)); + + // Remove the user from the role + response = deleteRequest(url); + + // Check the content from the response + checkContent(response); + + // The user should be removed from the role + assertFalse(getUsersAssignedToRole().contains(userName)); + + // Delete the user + deleteUser(userName); + } + + /** + * Test the REST API to add/remove a group to/from a role + * + * @throws IOException + * @throws JSONException + */ + public void testRmAddRemoveGroup() throws IOException, JSONException + { + // Create a group + String groupName = "arhweurawy"; + createGroup(groupName); + + // Check if the group is already assigned to the role + assertFalse(getGroupsAssignedToRole().contains(groupName)); + + // Format url and send request + String url = getFormattedUrlString(groupName); + Response response = postRequest(url); + + // Check the content from the response + checkContent(response); + + // The group should be added to the role + assertTrue(getGroupsAssignedToRole().contains(groupName)); + + // Remove the group from the role + response = deleteRequest(url); + + // Check the content from the response + checkContent(response); + + // The user should be removed from the role + assertFalse(getGroupsAssignedToRole().contains(groupName)); + + // Delete the group + deleteGroup(groupName); + } + + /** + * Util method to get a set of groups assigned to a role + * + * @return Returns a set of groups assigned to a role + */ + private Set getGroupsAssignedToRole() + { + return filePlanRoleService.getGroupsAssignedToRole(filePlan, FilePlanRoleService.ROLE_SECURITY_OFFICER); + } + + /** + * Util method to get a set of users assigned to a role + * + * @return Returns a set of users assigned to a role + */ + private Set getUsersAssignedToRole() + { + return filePlanRoleService.getUsersAssignedToRole(filePlan, FilePlanRoleService.ROLE_SECURITY_OFFICER); + } + + /** + * Util method to get a formatted nodeRef string + * + * @return Returns a formatted nodeRef string + */ + private String getFormattedFilePlanString() + { + StoreRef storeRef = filePlan.getStoreRef(); + String storeType = storeRef.getProtocol(); + String storeId = storeRef.getIdentifier(); + String id = filePlan.getId(); + + StringBuffer sb = new StringBuffer(); + sb.append(storeType); + sb.append("/"); + sb.append(storeId); + sb.append("/"); + sb.append(id); + + return sb.toString(); + } + + /** + * Util method to get a formatted url string + * + * @param authorityName The name of the authority which should be added/removed to/from a role + * @return Returns a formatted url string + */ + private String getFormattedUrlString(String authorityName) + { + return String.format(RM_CHILDREN_URL, getFormattedFilePlanString(), FilePlanRoleService.ROLE_SECURITY_OFFICER, authorityName); + } + + /** + * Util method to send a post request + * + * @param url The url which should be used to make the post request + * @return Returns the response from the server + * @throws UnsupportedEncodingException + * @throws IOException + */ + private Response postRequest(String url) throws UnsupportedEncodingException, IOException + { + return sendRequest(new PostRequest(url, new JSONObject().toString(), APPLICATION_JSON), Status.STATUS_OK); + } + + /** + * Util method to send a delete request + * + * @param url The url which should be used to make the delete request + * @return Returns the response from the server + * @throws IOException + */ + private Response deleteRequest(String url) throws IOException + { + return sendRequest(new DeleteRequest(url), Status.STATUS_OK); + } + + /** + * Util method to check the server response + * + * @param response The server response + * @throws UnsupportedEncodingException + */ + private void checkContent(Response response) throws UnsupportedEncodingException + { + String contentAsString = response.getContentAsString(); + assertNotNull(contentAsString); + assertTrue(contentAsString.equals("{}")); + } +}