RM-2120: Add unit test for usersecurityclearance.put webscript & add it (and get) to test suite

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@103829 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Webster
2015-05-07 14:04:26 +00:00
parent 1c5e3a8eca
commit 88ab2c282b
2 changed files with 121 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
package org.alfresco.module.org_alfresco_module_rm.script.classification;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException;
import org.alfresco.module.org_alfresco_module_rm.classification.SecurityClearance;
import org.alfresco.module.org_alfresco_module_rm.classification.SecurityClearanceService;
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest;
import org.alfresco.service.cmr.security.PersonService;
import org.json.JSONObject;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.WebScriptException;
import java.util.Map;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
public class UserSecurityClearancePutTest extends BaseWebScriptUnitTest
{
/**
* Classpath location of ftl template for web script
*/
private static final String WEBSCRIPT_TEMPLATE = WEBSCRIPT_ROOT_RM + "classification/usersecurityclearance.put.json.ftl";
private static final String USERNAME = "username";
private static final String CLEARANCE_ID = "clearanceId";
/**
* User security clearance webscript instance
*/
private @Spy @InjectMocks
UserSecurityClearancePut webscript;
/**
* Mock Security Clearance Service
*/
private @Mock
SecurityClearanceService mockSecurityClearanceService;
/**
* {@inheritDoc}
*/
@Override
protected DeclarativeWebScript getWebScript()
{
return webscript;
}
/**
* {@inheritDoc}
*/
@Override
protected String getWebScriptTemplate()
{
return WEBSCRIPT_TEMPLATE;
}
/**
* Test the Security Clearance webscript
*
* @throws Exception
*/
@Test
public void testExecuteImpl() throws Exception
{
String username = "user1";
String clearanceId = "Top Secret";
String clearanceDisplay = "Don't tell anyone";
String firstName = "Firstname";
String lastName = "Lastname";
PersonService.PersonInfo personInfo = new PersonService.PersonInfo(generateNodeRef(), username, firstName, lastName);
ClassificationLevel clearanceLevel = new ClassificationLevel(clearanceId, clearanceDisplay);
SecurityClearance securityClearance = new SecurityClearance(personInfo, clearanceLevel);
// Setup web script parameters
Map<String, String> parameters = buildParameters(USERNAME, username, CLEARANCE_ID, clearanceId);
when(mockSecurityClearanceService.setUserSecurityClearance(username, clearanceId)).thenReturn(securityClearance);
// Execute web script
JSONObject json = executeJSONWebScript(parameters);
assertNotNull(json);
// check the JSON result using Jackson to allow easy equality testing.
ObjectMapper mapper = new ObjectMapper();
String expectedJSONString = "{\"data\":{\"firstName\":\"Firstname\",\"lastName\":\"Lastname\",\"completeName\":\"Firstname Lastname (user1)\",\"fullName\":\"Firstname Lastname\",\"classificationLabel\":\"Don't tell anyone\",\"userName\":\"user1\",\"classificationId\":\"Top Secret\"}}";
JsonNode expected = mapper.readTree(expectedJSONString);
assertEquals(expected, mapper.readTree(json.toString()));
}
/**
* Test the Security Clearance webscript can't be called by a user with insufficient clearance
*
* @throws Exception
*/
@Test (expected = WebScriptException.class)
public void testNonClearedUser() throws Exception
{
String username = "user1";
String clearanceId = "Top Secret";
// Setup web script parameters
Map<String, String> parameters = buildParameters(USERNAME, username, CLEARANCE_ID, clearanceId);
when(mockSecurityClearanceService.setUserSecurityClearance(username, clearanceId))
.thenThrow(new ClassificationServiceException.LevelIdNotFound(clearanceId));
// Execute web script - this should throw the expected exception.
executeJSONWebScript(parameters);
}
}

View File

@@ -41,6 +41,8 @@ import org.alfresco.module.org_alfresco_module_rm.recorded.version.config.Record
import org.alfresco.module.org_alfresco_module_rm.script.classification.ClassificationLevelsGetTest; import org.alfresco.module.org_alfresco_module_rm.script.classification.ClassificationLevelsGetTest;
import org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentPost; import org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentPost;
import org.alfresco.module.org_alfresco_module_rm.script.classification.ReasonsGetTest; import org.alfresco.module.org_alfresco_module_rm.script.classification.ReasonsGetTest;
import org.alfresco.module.org_alfresco_module_rm.script.classification.UserSecurityClearanceGetTest;
import org.alfresco.module.org_alfresco_module_rm.script.classification.UserSecurityClearancePutTest;
import org.alfresco.module.org_alfresco_module_rm.script.hold.HoldPostUnitTest; import org.alfresco.module.org_alfresco_module_rm.script.hold.HoldPostUnitTest;
import org.alfresco.module.org_alfresco_module_rm.script.hold.HoldPutUnitTest; import org.alfresco.module.org_alfresco_module_rm.script.hold.HoldPutUnitTest;
import org.alfresco.module.org_alfresco_module_rm.script.hold.HoldsGetUnitTest; import org.alfresco.module.org_alfresco_module_rm.script.hold.HoldsGetUnitTest;
@@ -87,6 +89,8 @@ import org.junit.runners.Suite.SuiteClasses;
ReasonsGetTest.class, ReasonsGetTest.class,
ClassificationLevelsGetTest.class, ClassificationLevelsGetTest.class,
ClassifyContentPost.class, ClassifyContentPost.class,
UserSecurityClearanceGetTest.class,
UserSecurityClearancePutTest.class,
// action implementations // action implementations
FileReportActionUnitTest.class, FileReportActionUnitTest.class,