From 88ab2c282b3ae965ee5fbb7998979fb4c17dedea Mon Sep 17 00:00:00 2001 From: David Webster Date: Thu, 7 May 2015 14:04:26 +0000 Subject: [PATCH] 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 --- .../UserSecurityClearancePutTest.java | 117 ++++++++++++++++++ .../test/AllUnitTestSuite.java | 4 + 2 files changed, 121 insertions(+) create mode 100644 rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/script/classification/UserSecurityClearancePutTest.java diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/script/classification/UserSecurityClearancePutTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/script/classification/UserSecurityClearancePutTest.java new file mode 100644 index 0000000000..589ee6920b --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/script/classification/UserSecurityClearancePutTest.java @@ -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 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 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); + } +} \ No newline at end of file diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/AllUnitTestSuite.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/AllUnitTestSuite.java index 138ac83e10..afd25c9dde 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/AllUnitTestSuite.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/AllUnitTestSuite.java @@ -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.ClassifyContentPost; 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.HoldPutUnitTest; import org.alfresco.module.org_alfresco_module_rm.script.hold.HoldsGetUnitTest; @@ -87,6 +89,8 @@ import org.junit.runners.Suite.SuiteClasses; ReasonsGetTest.class, ClassificationLevelsGetTest.class, ClassifyContentPost.class, + UserSecurityClearanceGetTest.class, + UserSecurityClearancePutTest.class, // action implementations FileReportActionUnitTest.class,