From a0c9b6dc73e61681d8ce8d3368d2f480b0ca0411 Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Wed, 6 May 2015 07:33:24 +0000 Subject: [PATCH] Initial unit tests for RM-2113. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@103745 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../classification/ClassificationSuite.java | 3 +- .../SecurityClearanceServiceImplUnitTest.java | 95 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceServiceImplUnitTest.java diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSuite.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSuite.java index 12ff51e206..d25a46d67d 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSuite.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationSuite.java @@ -32,7 +32,8 @@ import org.junit.runners.Suite; ClassificationLevelManagerUnitTest.class, ClassificationReasonManagerUnitTest.class, ClassificationServiceDAOUnitTest.class, - ClassificationServiceImplUnitTest.class + ClassificationServiceImplUnitTest.class, + SecurityClearanceServiceImplUnitTest.class }) public class ClassificationSuite { diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceServiceImplUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceServiceImplUnitTest.java new file mode 100644 index 0000000000..1fe38cf153 --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/SecurityClearanceServiceImplUnitTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2005-2015 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.classification; + +import static org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel.ASPECT_SECURITY_CLEARANCE; +import static org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel.PROP_CLEARANCE_LEVEL; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; + +import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper; +import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; +import org.alfresco.service.cmr.dictionary.DictionaryService; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.StoreRef; +import org.alfresco.service.cmr.security.PersonService; +import org.alfresco.service.cmr.security.PersonService.PersonInfo; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** + * Unit tests for {@link SecurityClearanceServiceImpl}. + * + * @author Neil Mc Erlean + * @since 3.0 + */ +public class SecurityClearanceServiceImplUnitTest +{ + @InjectMocks private SecurityClearanceServiceImpl securityClearanceServiceImpl; + + @Mock private AuthenticationUtil mockedAuthenticationUtil; + @Mock private ClassificationService mockClassificationService; + @Mock private DictionaryService mockDictionaryService; + @Mock private NodeService mockNodeService; + @Mock private PersonService mockPersonService; + + @Before public void setUp() + { + MockitoAnnotations.initMocks(this); + } + + private PersonInfo createMockPerson(String userName, String firstName, String lastName, String clearanceLevel) + { + final NodeRef userNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, userName); + final PersonInfo info = new PersonInfo(userNode, userName, firstName, lastName); + + when(mockPersonService.getPerson(eq(userName), anyBoolean())).thenReturn(userNode); + when(mockPersonService.getPerson(eq(userNode))).thenReturn(info); + + when(mockNodeService.hasAspect(eq(userNode), eq(ASPECT_SECURITY_CLEARANCE))).thenReturn(clearanceLevel != null); + when(mockNodeService.getProperty(eq(userNode), eq(PROP_CLEARANCE_LEVEL))).thenReturn(clearanceLevel); + + if (clearanceLevel != null) + { + final ClassificationLevel dummyValue = new ClassificationLevel(clearanceLevel, clearanceLevel); + when(mockClassificationService.getClassificationLevelById(eq(clearanceLevel))).thenReturn(dummyValue); + } + + return info; + } + + @Test public void userWithNoClearanceGetsDefaultClearance() + { + final PersonInfo user1 = createMockPerson("user1", "User", "One", null); + MockAuthenticationUtilHelper.setup(mockedAuthenticationUtil, user1.getUserName()); + + when(mockClassificationService.getDefaultClassificationLevel()) + .thenReturn(new ClassificationLevel("default", "default")); + + final SecurityClearance clearance = securityClearanceServiceImpl.getUserSecurityClearance(); + + assertEquals("default", clearance.getClassificationLevel().getId()); + } +}