mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-21 18:09:20 +00:00
RM-2798 (Move content classification support into RM Enterprise AMPs) - Repo code
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@118596 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -47,7 +47,6 @@ public class BootstrapImporterModuleComponentUnitTest extends BaseUnitTest
|
||||
@Mock(name="importer") private ImporterBootstrap mockedImporter;
|
||||
@Mock(name="modulePatchExecuter") private ModulePatchExecuter mockedModulePatchExecuter;
|
||||
@Mock(name="recordContributorsGroupBootstrapComponent") private RecordContributorsGroupBootstrapComponent mockedRecordContributorsGroupBootstrapComponent;
|
||||
@Mock(name="clearanceForAdminBootstrapComponent") private ClearanceForAdminBootstrapComponent mockedClearanceForAdminBootstrapComponent;
|
||||
|
||||
/** importer */
|
||||
@InjectMocks
|
||||
|
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.bootstrap;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevelManager;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceBootstrap;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.security.PersonService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ClearanceForAdminBootstrapComponent}.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ClearanceForAdminBootstrapComponentUnitTest implements ClassifiedContentModel
|
||||
{
|
||||
@InjectMocks ClearanceForAdminBootstrapComponent clearanceForAdminBootstrapComponent;
|
||||
@Mock AuthenticationUtil mockAuthenticationUtil;
|
||||
@Mock PersonService mockPersonService;
|
||||
@Mock NodeService mockNodeService;
|
||||
@Mock ClassificationServiceBootstrap mockClassificationServiceBootstrap;
|
||||
|
||||
@Before public void setUp()
|
||||
{
|
||||
initMocks(this);
|
||||
}
|
||||
|
||||
/** Check that the admin user gets assigned the provided clearance. */
|
||||
@Test public void testCreateClearanceForAdmin()
|
||||
{
|
||||
// Allow the classification level id to be found.
|
||||
ClassificationLevel level = new ClassificationLevel("id", "displayLabelKey");
|
||||
ClassificationLevelManager mockClassificationLevelManager = mock(ClassificationLevelManager.class);
|
||||
when(mockClassificationLevelManager.getMostSecureLevel()).thenReturn(level);
|
||||
when(mockClassificationServiceBootstrap.getClassificationLevelManager()).thenReturn(mockClassificationLevelManager);
|
||||
|
||||
// Set up the admin user.
|
||||
when(mockAuthenticationUtil.getAdminUserName()).thenReturn("admin");
|
||||
NodeRef admin = new NodeRef("admin://node/");
|
||||
when(mockPersonService.getPerson("admin", false)).thenReturn(admin);
|
||||
|
||||
// Call the method under test.
|
||||
clearanceForAdminBootstrapComponent.createClearanceForAdmin();
|
||||
|
||||
verify(mockNodeService).setProperty(admin, PROP_CLEARANCE_LEVEL, "id");
|
||||
// Check that the classification levels were loaded.
|
||||
verify(mockClassificationServiceBootstrap).onBootstrap(null);
|
||||
}
|
||||
}
|
@@ -1,122 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.module.org_alfresco_module_rm.caveat.dao;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.MalformedConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link CaveatDAOFromJSON}.
|
||||
*
|
||||
* @author Tom Page
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class CaveatDAOFromJSONUnitTest
|
||||
{
|
||||
/** The class under test. */
|
||||
CaveatDAOFromJSON caveatDAOFromJSON = new CaveatDAOFromJSON();
|
||||
|
||||
@Before public void initClassUnderTest()
|
||||
{
|
||||
NamespaceService namespaceService = mock(NamespaceService.class);
|
||||
when(namespaceService.getNamespaceURI(anyString())).thenReturn("{mockedNamespace}");
|
||||
|
||||
DictionaryService dictionaryService = mock(DictionaryService.class);
|
||||
PropertyDefinition mockProperty = mock(PropertyDefinition.class);
|
||||
when(dictionaryService.getProperty(any(QName.class))).thenReturn(mockProperty);
|
||||
|
||||
caveatDAOFromJSON.setDictionaryService(dictionaryService);
|
||||
caveatDAOFromJSON.setNamespaceService(namespaceService);
|
||||
}
|
||||
|
||||
/** Test that loading the default caveat configuration file doesn't throw any exceptions. */
|
||||
@Test
|
||||
public void testGetCaveatGroups_defaultConfiguration()
|
||||
{
|
||||
caveatDAOFromJSON.setConfigLocation("/alfresco/module/org_alfresco_module_rm/caveat/rm-caveats.json");
|
||||
Map<String, CaveatGroup> caveatGroups = caveatDAOFromJSON.getCaveatGroups();
|
||||
assertNotNull(caveatGroups);
|
||||
}
|
||||
|
||||
/** Test that if the caveat configuration file is missing then an empty set of caveat groups is created. */
|
||||
@Test
|
||||
public void testGetCaveatGroups_missingConfiguration()
|
||||
{
|
||||
caveatDAOFromJSON.setConfigLocation("/does/not/exist.json");
|
||||
Map<String, CaveatGroup> caveatGroups = caveatDAOFromJSON.getCaveatGroups();
|
||||
assertTrue("A missing configuration file should result in no caveat groups", caveatGroups.keySet().isEmpty());
|
||||
}
|
||||
|
||||
/** Test that malformed JSON causes an exception. */
|
||||
@Test(expected = MalformedConfiguration.class)
|
||||
public void testGetCaveatGroups_malformedJSON()
|
||||
{
|
||||
caveatDAOFromJSON.setConfigLocation("/alfresco/caveat/rm-caveats-malformedJSON.json");
|
||||
caveatDAOFromJSON.getCaveatGroups();
|
||||
}
|
||||
|
||||
/** Test that a missing id causes an exception. */
|
||||
@Test(expected = MalformedConfiguration.class)
|
||||
public void testGetCaveatGroups_missingId()
|
||||
{
|
||||
caveatDAOFromJSON.setConfigLocation("/alfresco/caveat/rm-caveats-missingMarkId.json");
|
||||
caveatDAOFromJSON.getCaveatGroups();
|
||||
}
|
||||
|
||||
/** Test that a duplicate group id causes an exception. */
|
||||
@Test(expected = MalformedConfiguration.class)
|
||||
public void testGetCaveatGroups_duplicateGroupId()
|
||||
{
|
||||
caveatDAOFromJSON.setConfigLocation("/alfresco/caveat/rm-caveats-duplicateGroupId.json");
|
||||
caveatDAOFromJSON.getCaveatGroups();
|
||||
}
|
||||
|
||||
/** Test that a duplicate mark id (within a group) causes an exception. */
|
||||
@Test(expected = MalformedConfiguration.class)
|
||||
public void testGetCaveatGroups_duplicateMarkId()
|
||||
{
|
||||
caveatDAOFromJSON.setConfigLocation("/alfresco/caveat/rm-caveats-duplicateMarkId.json");
|
||||
caveatDAOFromJSON.getCaveatGroups();
|
||||
}
|
||||
|
||||
/** Test that a duplicate mark id (in different groups) doesn't cause an exception. */
|
||||
@Test
|
||||
public void testGetCaveatGroups_duplicateMarkIdInDifferentGroups()
|
||||
{
|
||||
caveatDAOFromJSON.setConfigLocation("/alfresco/caveat/rm-caveats-duplicateMarkIdInDifferentGroups.json");
|
||||
Map<String, CaveatGroup> caveatGroups = caveatDAOFromJSON.getCaveatGroups();
|
||||
assertNotNull(caveatGroups);
|
||||
}
|
||||
}
|
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.service.cmr.dictionary.ConstraintException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClassificationLevelConstraint}.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ClassificationLevelConstraintUnitTest
|
||||
{
|
||||
private static final ClassificationLevel LEVEL_ONE = new ClassificationLevel("id1", "DisplayKey1");
|
||||
private static final ClassificationLevel LEVEL_TWO = new ClassificationLevel("id2", "DisplayKey2");
|
||||
private static final List<ClassificationLevel> DEFAULT_LEVELS = Arrays.asList(LEVEL_ONE, LEVEL_TWO);
|
||||
|
||||
@InjectMocks ClassificationLevelConstraint classificationLevelConstraint;
|
||||
@Mock ClassificationSchemeService mockClassificationSchemeService;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
// Currently this list of levels suffices for all the tests.
|
||||
doReturn(DEFAULT_LEVELS).when(mockClassificationSchemeService).getClassificationLevels();
|
||||
}
|
||||
|
||||
/** Check that evaluateSingleValue throws no exceptions when an id is found. */
|
||||
@Test
|
||||
public void evaluateSingleValue_valid()
|
||||
{
|
||||
classificationLevelConstraint.evaluateSingleValue("id1");
|
||||
}
|
||||
|
||||
/** Check that evaluateSingleValue throws an exception when an id is not found. */
|
||||
@Test(expected = ConstraintException.class)
|
||||
public void evaluateSingleValue_stringNotFound()
|
||||
{
|
||||
classificationLevelConstraint.evaluateSingleValue("non-existant id");
|
||||
}
|
||||
|
||||
/** Check that evaluateSingleValue throws an exception when supplied with something that isn't a String. */
|
||||
@Test(expected = ConstraintException.class)
|
||||
public void evaluateSingleValue_notString()
|
||||
{
|
||||
classificationLevelConstraint.evaluateSingleValue(Integer.valueOf(123));
|
||||
}
|
||||
}
|
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.LevelIdNotFound;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClassificationLevelManager}.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ClassificationLevelManagerUnitTest
|
||||
{
|
||||
private static final ClassificationLevel LEVEL_1 = new ClassificationLevel("id1", "displayLabelKey1");
|
||||
private static final ClassificationLevel LEVEL_2 = new ClassificationLevel("id2", "displayLabelKey2");
|
||||
private static final ClassificationLevel LEVEL_3 = new ClassificationLevel("id3", "displayLabelKey3");
|
||||
private static final List<ClassificationLevel> LEVELS = Arrays.asList(LEVEL_1, LEVEL_2, LEVEL_3);
|
||||
|
||||
private ClassificationLevelManager classificationLevelManager;
|
||||
|
||||
@Before public void setup()
|
||||
{
|
||||
classificationLevelManager = new ClassificationLevelManager();
|
||||
classificationLevelManager.setClassificationLevels(LEVELS);
|
||||
}
|
||||
|
||||
@Test public void findClassificationById_found()
|
||||
{
|
||||
ClassificationLevel actual = classificationLevelManager.findLevelById("id2");
|
||||
assertEquals(LEVEL_2, actual);
|
||||
}
|
||||
|
||||
@Test(expected=LevelIdNotFound.class) public void findClassificationById_notFound()
|
||||
{
|
||||
classificationLevelManager.findLevelById("id_unknown");
|
||||
}
|
||||
|
||||
@Test public void getMostSecureLevel()
|
||||
{
|
||||
ClassificationLevel actual = classificationLevelManager.getMostSecureLevel();
|
||||
assertEquals(LEVEL_1, actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that I have created the classification level manager with a list of classification levels
|
||||
* Then the unclassified level is available
|
||||
*/
|
||||
@Test public void getUnclassifiedLevel()
|
||||
{
|
||||
assertEquals(LEVELS.size() + 1, classificationLevelManager.getClassificationLevels().size());
|
||||
assertEquals(ClassificationLevelManager.UNCLASSIFIED, classificationLevelManager.findLevelById(ClassificationLevelManager.UNCLASSIFIED_ID));
|
||||
}
|
||||
}
|
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.ReasonIdNotFound;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClassificationReasonManager}.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ClassificationReasonManagerUnitTest
|
||||
{
|
||||
private static final ClassificationReason REASON_1 = new ClassificationReason("id1", "displayLabelKey1");
|
||||
private static final ClassificationReason REASON_2 = new ClassificationReason("id2", "displayLabelKey2");
|
||||
private static final ClassificationReason REASON_3 = new ClassificationReason("id3", "displayLabelKey3");
|
||||
private static final List<ClassificationReason> REASONS = Arrays.asList(REASON_1, REASON_2, REASON_3);
|
||||
|
||||
private ClassificationReasonManager classificationReasonManager;
|
||||
|
||||
@Before public void setup()
|
||||
{
|
||||
classificationReasonManager = new ClassificationReasonManager();
|
||||
classificationReasonManager.setClassificationReasons(REASONS);
|
||||
}
|
||||
|
||||
@Test public void findClassificationById_found()
|
||||
{
|
||||
ClassificationReason actual = classificationReasonManager.findReasonById("id2");
|
||||
assertEquals(REASON_2, actual);
|
||||
}
|
||||
|
||||
@Test(expected = ReasonIdNotFound.class) public void findClassificationById_notFound()
|
||||
{
|
||||
classificationReasonManager.findReasonById("id_unknown");
|
||||
}
|
||||
}
|
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClassificationSchemeEntityFactory}.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClassificationSchemeEntityFactoryUnitTest
|
||||
{
|
||||
ClassificationSchemeEntityFactory classificationSchemeEntityFactory = new ClassificationSchemeEntityFactory();
|
||||
@Mock JSONObject mockJsonObject;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
initMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateClassificationLevel() throws JSONException
|
||||
{
|
||||
when(mockJsonObject.getString("name")).thenReturn("name1");
|
||||
when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2");
|
||||
|
||||
ClassificationLevel classificationLevel = classificationSchemeEntityFactory.create(ClassificationLevel.class, mockJsonObject);
|
||||
|
||||
assertEquals("name1", classificationLevel.getId());
|
||||
assertEquals("displayLabelKey2", classificationLevel.getDisplayLabelKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateClassificationReason() throws JSONException
|
||||
{
|
||||
when(mockJsonObject.getString("id")).thenReturn("id1");
|
||||
when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2");
|
||||
|
||||
ClassificationReason classificationReason = classificationSchemeEntityFactory.create(ClassificationReason.class, mockJsonObject);
|
||||
|
||||
assertEquals("id1", classificationReason.getId());
|
||||
assertEquals("displayLabelKey2", classificationReason.getDisplayLabelKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateExemptionCategory() throws JSONException
|
||||
{
|
||||
when(mockJsonObject.getString("id")).thenReturn("id1");
|
||||
when(mockJsonObject.getString("displayLabel")).thenReturn("displayLabelKey2");
|
||||
|
||||
ExemptionCategory exemptionCategory = classificationSchemeEntityFactory.create(ExemptionCategory.class, mockJsonObject);
|
||||
|
||||
assertEquals("id1", exemptionCategory.getId());
|
||||
assertEquals("displayLabelKey2", exemptionCategory.getDisplayLabelKey());
|
||||
}
|
||||
}
|
@@ -1,178 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.ExemptionCategoryIdNotFound;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.LevelIdNotFound;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.ReasonIdNotFound;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ClassificationSchemeServiceImpl}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClassificationSchemeServiceImplUnitTest
|
||||
{
|
||||
private static final List<ClassificationLevel> DEFAULT_CLASSIFICATION_LEVELS = asLevelList("Top Secret", "rm.caveat.classification.mark.ts.label",
|
||||
"Secret", "rm.caveat.classification.mark.s.label",
|
||||
"Confidential", "rm.caveat.classification.mark.c.label",
|
||||
"No Clearance", "rm.classification.noClearance");
|
||||
|
||||
/**
|
||||
* A convenience method for turning lists of level id Strings into lists
|
||||
* of {@code ClassificationLevel} objects.
|
||||
*
|
||||
* @param idsAndLabels A varargs/array of Strings like so: [ id0, label0, id1, label1... ]
|
||||
* @throws IllegalArgumentException if {@code idsAndLabels} has a non-even length.
|
||||
*/
|
||||
public static List<ClassificationLevel> asLevelList(String ... idsAndLabels)
|
||||
{
|
||||
if (idsAndLabels.length % 2 != 0)
|
||||
{
|
||||
throw new IllegalArgumentException(String.format("Cannot create %s objects with %d args.",
|
||||
ClassificationLevel.class.getSimpleName(), idsAndLabels.length));
|
||||
}
|
||||
|
||||
final List<ClassificationLevel> levels = new ArrayList<>(idsAndLabels.length / 2);
|
||||
for (int i = 0; i < idsAndLabels.length; i += 2)
|
||||
{
|
||||
levels.add(new ClassificationLevel(idsAndLabels[i], idsAndLabels[i+1]));
|
||||
}
|
||||
|
||||
return levels;
|
||||
}
|
||||
|
||||
@InjectMocks private ClassificationSchemeServiceImpl classificationSchemeServiceImpl;
|
||||
|
||||
@Mock private NodeService mockNodeService;
|
||||
@Mock private DictionaryService mockDictionaryService;
|
||||
@Mock private ClassificationLevelManager mockLevelManager;
|
||||
@Mock private ClassificationReasonManager mockReasonManager;
|
||||
@Mock private ExemptionCategoryManager mockExemptionCategoryManager;
|
||||
@Captor private ArgumentCaptor<Map<QName, Serializable>> propertiesCaptor;
|
||||
|
||||
@Before public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that restrictList returns the three lower security levels when supplied with "secret" (i.e. that it doesn't
|
||||
* return "top secret").
|
||||
*/
|
||||
@Test public void restrictList_filter()
|
||||
{
|
||||
ClassificationLevel targetLevel = new ClassificationLevel("Secret", "rm.caveat.classification.mark.s.label");
|
||||
|
||||
List<ClassificationLevel> actual = classificationSchemeServiceImpl.restrictList(DEFAULT_CLASSIFICATION_LEVELS, targetLevel);
|
||||
|
||||
List<ClassificationLevel> expected = asLevelList("Secret", "rm.caveat.classification.mark.s.label",
|
||||
"Confidential", "rm.caveat.classification.mark.c.label",
|
||||
"No Clearance", "rm.classification.noClearance");
|
||||
assertEquals(expected, actual);
|
||||
// Check that the returned list can't be modified.
|
||||
ExceptionUtils.expectedException(UnsupportedOperationException.class, () -> actual.remove(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that restrictList returns an empty list when the target is not contained in the list.
|
||||
*/
|
||||
@Test public void restrictList_targetNotFound()
|
||||
{
|
||||
ClassificationLevel targetLevel = new ClassificationLevel("UnrecognisedLevel", "rm.classification.IMadeThisUp");
|
||||
|
||||
List<ClassificationLevel> actual = classificationSchemeServiceImpl.restrictList(DEFAULT_CLASSIFICATION_LEVELS, targetLevel);
|
||||
|
||||
assertEquals("Expected an empty list when the target level is not found.", 0, actual.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getClassificationLevelById()
|
||||
{
|
||||
String levelId = "classificationLevelId1";
|
||||
ClassificationLevel classificationLevel = new ClassificationLevel(levelId, "displayLabelKey");
|
||||
when(mockLevelManager.findLevelById(levelId)).thenReturn(classificationLevel);
|
||||
ClassificationLevel classificationLevelById = classificationSchemeServiceImpl.getClassificationLevelById(levelId);
|
||||
assertEquals(classificationLevel, classificationLevelById);
|
||||
}
|
||||
|
||||
@Test(expected = LevelIdNotFound.class)
|
||||
public void getClassificationLevelById_nonExisting()
|
||||
{
|
||||
String classificationLevelId = "aRandomId";
|
||||
doThrow(new LevelIdNotFound("Id not found!")).when(mockLevelManager).findLevelById(classificationLevelId);
|
||||
classificationSchemeServiceImpl.getClassificationLevelById(classificationLevelId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getClassificationReasonById()
|
||||
{
|
||||
String reasonId = "classificationReasonId1";
|
||||
ClassificationReason classificationReason = new ClassificationReason(reasonId, "displayLabelKey");
|
||||
when(mockReasonManager.findReasonById(reasonId)).thenReturn(classificationReason);
|
||||
ClassificationReason classificationReasonById = classificationSchemeServiceImpl.getClassificationReasonById(reasonId);
|
||||
assertEquals(classificationReason, classificationReasonById);
|
||||
}
|
||||
|
||||
@Test(expected = ReasonIdNotFound.class)
|
||||
public void getClassificationReasonById_nonExisting()
|
||||
{
|
||||
String classificationReasonId = "aRandomId";
|
||||
doThrow(new ReasonIdNotFound("Id not found!")).when(mockReasonManager).findReasonById(classificationReasonId);
|
||||
classificationSchemeServiceImpl.getClassificationReasonById(classificationReasonId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getExemptionCategoryById()
|
||||
{
|
||||
String exemptionCategoryId = "exemptionCategoryId";
|
||||
ExemptionCategory exemptionCategory = new ExemptionCategory(exemptionCategoryId, "displayLabelKey");
|
||||
when(mockExemptionCategoryManager.findCategoryById(exemptionCategoryId)).thenReturn(exemptionCategory);
|
||||
classificationSchemeServiceImpl.getExemptionCategoryById(exemptionCategoryId);
|
||||
}
|
||||
|
||||
@Test(expected = ExemptionCategoryIdNotFound.class)
|
||||
public void getExemptionCategoryById_nonExisting()
|
||||
{
|
||||
String exemptionCategoryId = "aRandomId";
|
||||
doThrow(new ExemptionCategoryIdNotFound("Id not found!")).when(mockExemptionCategoryManager).findCategoryById(exemptionCategoryId);
|
||||
classificationSchemeServiceImpl.getExemptionCategoryById(exemptionCategoryId);
|
||||
}
|
||||
}
|
@@ -1,344 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.apache.commons.collections.ListUtils.union;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationLevelFieldsValidator;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationReasonFieldsValidator;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ClassificationSchemeEntityValidator;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.validation.ExemptionCategoryFieldsValidator;
|
||||
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.attributes.AttributeService;
|
||||
import org.apache.log4j.Appender;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClassificationServiceBootstrap}.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ClassificationServiceBootstrapUnitTest implements ClassifiedContentModel
|
||||
{
|
||||
private static final List<ClassificationLevel> DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS =
|
||||
asLevelList("TS", "rm.caveat.classification.mark.ts.label",
|
||||
"S", "rm.caveat.classification.mark.s.label",
|
||||
"C", "rm.caveat.classification.mark.c.label");
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final List<ClassificationLevel> DEFAULT_CLASSIFICATION_LEVELS =
|
||||
union(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS,
|
||||
asLevelList("U", "rm.classification.unclassified"));
|
||||
private static final List<ClassificationLevel> ALT_CLASSIFICATION_LEVELS = asLevelList("B", "Board",
|
||||
"EM", "ExecutiveManagement",
|
||||
"E", "Employee",
|
||||
"P", "Public");
|
||||
private static final List<ClassificationReason> PLACEHOLDER_CLASSIFICATION_REASONS = asList(new ClassificationReason("id1", "label1"),
|
||||
new ClassificationReason("id2", "label2"));
|
||||
private static final List<ClassificationReason> ALTERNATIVE_CLASSIFICATION_REASONS = asList(new ClassificationReason("id8", "label8"),
|
||||
new ClassificationReason("id9", "label9"));
|
||||
private static final List<ExemptionCategory> EXEMPTION_CATEGORIES = asList(new ExemptionCategory("id1", "label1"),
|
||||
new ExemptionCategory("id2", "label2"));
|
||||
private static final List<ExemptionCategory> CHANGED_EXEMPTION_CATEGORIES = asList(new ExemptionCategory("id3", "label3"));
|
||||
|
||||
/**
|
||||
* A convenience method for turning lists of level id Strings into lists
|
||||
* of {@code ClassificationLevel} objects.
|
||||
*
|
||||
* @param idsAndLabels A varargs/array of Strings like so: [ id0, label0, id1, label1... ]
|
||||
* @throws IllegalArgumentException if {@code idsAndLabels} has a non-even length.
|
||||
*/
|
||||
public static List<ClassificationLevel> asLevelList(String ... idsAndLabels)
|
||||
{
|
||||
if (idsAndLabels.length % 2 != 0)
|
||||
{
|
||||
throw new IllegalArgumentException(String.format("Cannot create %s objects with %d args.",
|
||||
ClassificationLevel.class.getSimpleName(), idsAndLabels.length));
|
||||
}
|
||||
|
||||
final List<ClassificationLevel> levels = new ArrayList<>(idsAndLabels.length / 2);
|
||||
|
||||
for (int i = 0; i < idsAndLabels.length; i += 2)
|
||||
{
|
||||
levels.add(new ClassificationLevel(idsAndLabels[i], idsAndLabels[i+1]));
|
||||
}
|
||||
|
||||
return levels;
|
||||
}
|
||||
|
||||
@InjectMocks ClassificationServiceBootstrap classificationServiceBootstrap;
|
||||
@Mock ClassificationServiceDAO mockClassificationServiceDAO;
|
||||
@Mock AttributeService mockAttributeService;
|
||||
@Mock AuthenticationUtil mockAuthenticationUtil;
|
||||
@Mock ExemptionCategoryManager mockExemptionCategoryManager;
|
||||
@Mock ClearanceLevelManager mockClearanceLevelManager;
|
||||
/** Using a mock appender in the class logger so that we can verify some of the logging requirements. */
|
||||
@Mock Appender mockAppender;
|
||||
@Captor ArgumentCaptor<List<ClearanceLevel>> clearanceLevelCaptor;
|
||||
|
||||
private ClassificationLevelFieldsValidator classificationLevelFieldsValidator = new ClassificationLevelFieldsValidator();
|
||||
private ClassificationSchemeEntityValidator<ClassificationLevel> classificationLevelValidator = new ClassificationSchemeEntityValidator<>(classificationLevelFieldsValidator);
|
||||
private ClassificationReasonFieldsValidator classificationReasonFieldsValidator = new ClassificationReasonFieldsValidator();
|
||||
private ClassificationSchemeEntityValidator<ClassificationReason> classificationReasonValidator = new ClassificationSchemeEntityValidator<>(classificationReasonFieldsValidator);
|
||||
private ExemptionCategoryFieldsValidator exemptionCategoryFieldsValidator = new ExemptionCategoryFieldsValidator();
|
||||
private ClassificationSchemeEntityValidator<ExemptionCategory> exemptionCategoryValidator = new ClassificationSchemeEntityValidator<>(exemptionCategoryFieldsValidator);
|
||||
|
||||
@Before public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
MockAuthenticationUtilHelper.setup(mockAuthenticationUtil);
|
||||
// This seems to be necessary because the POJO managers aren't constructor arguments.
|
||||
classificationServiceBootstrap.setExemptionCategoryManager(mockExemptionCategoryManager);
|
||||
classificationServiceBootstrap.setClearanceLevelManager(mockClearanceLevelManager);
|
||||
}
|
||||
|
||||
@Test public void defaultLevelsConfigurationVanillaSystem()
|
||||
{
|
||||
when(mockClassificationServiceDAO.<ClassificationLevel>getConfiguredValues(ClassificationLevel.class)).thenReturn(DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS);
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
|
||||
|
||||
classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
|
||||
|
||||
verify(mockAttributeService).setAttribute(eq((Serializable) DEFAULT_CONFIGURED_CLASSIFICATION_LEVELS),
|
||||
anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test public void levelsConfigurationChanged()
|
||||
{
|
||||
when(mockClassificationServiceDAO.<ClassificationLevel>getConfiguredValues(ClassificationLevel.class)).thenReturn(ALT_CLASSIFICATION_LEVELS);
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
|
||||
.thenReturn((Serializable) DEFAULT_CLASSIFICATION_LEVELS);
|
||||
|
||||
List<ClassificationLevel> entities = classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
|
||||
|
||||
assertEquals(ALT_CLASSIFICATION_LEVELS, entities);
|
||||
}
|
||||
|
||||
@Test (expected=MissingConfiguration.class)
|
||||
public void missingLevelsConfigurationVanillaSystemShouldFail() throws Exception
|
||||
{
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
|
||||
|
||||
classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that we can add a classification entity after the system has been bootstrapped for the first time.
|
||||
* <p>
|
||||
* <a href="https://issues.alfresco.com/jira/browse/RM-2484">RM-2484</a><pre>
|
||||
* Given a classification scheme has been installed
|
||||
* When I add new entities into the classification scheme configuration files
|
||||
* And I restart the server
|
||||
* Then the new entities are installed.
|
||||
* </pre>
|
||||
*/
|
||||
@Test public void addClassificationSchemeEntity()
|
||||
{
|
||||
List<ClassificationLevel> initialList = asLevelList("A", "Admin", "B", "Board Member");
|
||||
List<ClassificationLevel> correctedList = asLevelList("A", "Admin", "B", "Board Member", "C", "Common Employee");
|
||||
|
||||
when(mockClassificationServiceDAO.<ClassificationLevel>getConfiguredValues(ClassificationLevel.class)).thenReturn(correctedList);
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable) initialList);
|
||||
|
||||
List<ClassificationLevel> entities = classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
|
||||
|
||||
assertEquals(correctedList, entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that we can remove a classification entity after the system has been bootstrapped for the first time.
|
||||
* <p>
|
||||
* <a href="https://issues.alfresco.com/jira/browse/RM-2486">RM-2486</a><pre>
|
||||
* Given a classification scheme has been installed
|
||||
* And no content is classified using a given entity
|
||||
* When I delete the entity from the classification scheme configuration
|
||||
* And I restart the server
|
||||
* Then the entity is no longer available in the system.
|
||||
* </pre>
|
||||
*/
|
||||
@Test public void deleteUnusedClassificationSchemeEntity()
|
||||
{
|
||||
List<ClassificationLevel> initialList = asLevelList("A", "Admin", "B", "Board Member", "T", "Typo - Oops!", "C", "Common Employee");
|
||||
List<ClassificationLevel> correctedList = asLevelList("A", "Admin", "B", "Board Member", "C", "Common Employee");
|
||||
|
||||
when(mockClassificationServiceDAO.<ClassificationLevel>getConfiguredValues(ClassificationLevel.class)).thenReturn(correctedList);
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable) initialList);
|
||||
|
||||
List<ClassificationLevel> entities = classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationLevel.class, LEVELS_KEY, classificationLevelValidator);
|
||||
|
||||
assertEquals(correctedList, entities);
|
||||
}
|
||||
|
||||
@Test public void pristineSystemShouldBootstrapReasonsConfiguration()
|
||||
{
|
||||
// There are no classification reasons stored in the AttributeService.
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(null);
|
||||
|
||||
// We'll use a small set of placeholder classification reasons.
|
||||
when(mockClassificationServiceDAO.<ClassificationReason>getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
|
||||
|
||||
classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
|
||||
|
||||
verify(mockAttributeService).setAttribute(eq((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS),
|
||||
anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test public void checkAttributesNotTouchedIfConfiguredReasonsHaveNotChanged()
|
||||
{
|
||||
// The classification reasons stored are the same values that are found on the classpath.
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn((Serializable)PLACEHOLDER_CLASSIFICATION_REASONS);
|
||||
when(mockClassificationServiceDAO.<ClassificationReason>getConfiguredValues(ClassificationReason.class)).thenReturn(PLACEHOLDER_CLASSIFICATION_REASONS);
|
||||
|
||||
classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
|
||||
|
||||
verify(mockAttributeService, never()).setAttribute(any(Serializable.class),
|
||||
anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
/** Check that if the reasons supplied on the classpath differ from those already persisted then they are changed. */
|
||||
@Test
|
||||
public void changeConfiguredReasons()
|
||||
{
|
||||
// The classification reasons stored are different from those found on the classpath.
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString())).thenReturn(
|
||||
(Serializable) PLACEHOLDER_CLASSIFICATION_REASONS);
|
||||
when(mockClassificationServiceDAO.<ClassificationReason>getConfiguredValues(ClassificationReason.class)).thenReturn(ALTERNATIVE_CLASSIFICATION_REASONS);
|
||||
|
||||
// Call the method under test.
|
||||
List<ClassificationReason> actual = classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
|
||||
|
||||
assertEquals(ALTERNATIVE_CLASSIFICATION_REASONS, actual);
|
||||
// Check the persisted values were changed.
|
||||
verify(mockAttributeService).setAttribute((Serializable) ALTERNATIVE_CLASSIFICATION_REASONS, REASONS_KEY);
|
||||
}
|
||||
|
||||
@Test(expected = MissingConfiguration.class)
|
||||
public void noReasonsFoundCausesException()
|
||||
{
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
|
||||
.thenReturn((Serializable) null);
|
||||
when(mockClassificationServiceDAO.getConfiguredValues(ClassificationReason.class)).thenReturn(null);
|
||||
|
||||
classificationServiceBootstrap.getConfiguredSchemeEntities(ClassificationReason.class, REASONS_KEY, classificationReasonValidator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitConfiguredExemptionCategories_firstLoad()
|
||||
{
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
|
||||
.thenReturn((Serializable) null);
|
||||
when(mockClassificationServiceDAO.<ExemptionCategory>getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES);
|
||||
|
||||
List<ExemptionCategory> entities = classificationServiceBootstrap.getConfiguredSchemeEntities(ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
|
||||
|
||||
assertEquals(EXEMPTION_CATEGORIES, entities);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitConfiguredExemptionCategories_secondLoad()
|
||||
{
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
|
||||
.thenReturn((Serializable) EXEMPTION_CATEGORIES);
|
||||
when(mockClassificationServiceDAO.<ExemptionCategory>getConfiguredValues(ExemptionCategory.class)).thenReturn(EXEMPTION_CATEGORIES);
|
||||
|
||||
List<ExemptionCategory> entities = classificationServiceBootstrap.getConfiguredSchemeEntities(ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
|
||||
|
||||
assertEquals(EXEMPTION_CATEGORIES, entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that if the exemption categories supplied on the classpath differ from those already persisted then the new
|
||||
* values are persisted.
|
||||
* <p>
|
||||
* <a href="https://issues.alfresco.com/jira/browse/RM-2337">RM-2337</a><pre>
|
||||
* Given that I have already started the system once
|
||||
* When I stop the system
|
||||
* And modify the exemption categories
|
||||
* And restart the system
|
||||
* Then the new exemption categories are loaded.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
public void testInitConfiguredExemptionCategories_changedCategories()
|
||||
{
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
|
||||
.thenReturn((Serializable) EXEMPTION_CATEGORIES);
|
||||
when(mockClassificationServiceDAO.<ExemptionCategory>getConfiguredValues(ExemptionCategory.class)).thenReturn(CHANGED_EXEMPTION_CATEGORIES);
|
||||
|
||||
// Call the method under test.
|
||||
List<ExemptionCategory> actual = classificationServiceBootstrap.getConfiguredSchemeEntities(ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
|
||||
|
||||
assertEquals(CHANGED_EXEMPTION_CATEGORIES, actual);
|
||||
// Check the persisted values were changed.
|
||||
verify(mockAttributeService).setAttribute((Serializable) CHANGED_EXEMPTION_CATEGORIES, EXEMPTION_CATEGORIES_KEY);
|
||||
}
|
||||
|
||||
@Test(expected = MissingConfiguration.class)
|
||||
public void testInitConfiguredExemptionCategories_noCategoriesFound()
|
||||
{
|
||||
when(mockAttributeService.getAttribute(anyString(), anyString(), anyString()))
|
||||
.thenReturn((Serializable) null);
|
||||
when(mockClassificationServiceDAO.getConfiguredValues(ExemptionCategory.class)).thenReturn(null);
|
||||
|
||||
classificationServiceBootstrap.getConfiguredSchemeEntities(ExemptionCategory.class, EXEMPTION_CATEGORIES_KEY, exemptionCategoryValidator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the initialise method creates a clearance level corresponding to each classification level and that
|
||||
* the display label for the lowest clearance level is "No Clearance" (rather than "Unclassified").
|
||||
*/
|
||||
@Test public void initConfiguredClearanceLevels()
|
||||
{
|
||||
ClassificationLevel topSecret = new ClassificationLevel("1", "TopSecret");
|
||||
ClassificationLevel secret = new ClassificationLevel("2", "Secret");
|
||||
ImmutableList<ClassificationLevel> classificationLevels = ImmutableList.of(topSecret, secret, ClassificationLevelManager.UNCLASSIFIED);
|
||||
doNothing().when(mockClearanceLevelManager).setClearanceLevels(clearanceLevelCaptor.capture());
|
||||
|
||||
// Call the method under test.
|
||||
classificationServiceBootstrap.initConfiguredClearanceLevels(classificationLevels);
|
||||
|
||||
List<ClearanceLevel> clearanceLevels = clearanceLevelCaptor.getValue();
|
||||
assertEquals("There should be one clearance level for each classification level.", classificationLevels.size(), clearanceLevels.size());
|
||||
assertEquals("TopSecret", clearanceLevels.get(0).getDisplayLabelKey());
|
||||
assertEquals("Secret", clearanceLevels.get(1).getDisplayLabelKey());
|
||||
assertEquals("rm.classification.noClearance", clearanceLevels.get(2).getDisplayLabelKey());
|
||||
}
|
||||
}
|
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MalformedConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ClassificationServiceDAO}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClassificationServiceDAOUnitTest
|
||||
{
|
||||
@Test public void getConfiguredReasons_readingDefaultConfigurationShouldWork()
|
||||
{
|
||||
ClassificationServiceDAO c = new ClassificationServiceDAO();
|
||||
c.setReasonConfigLocation("/alfresco/module/org_alfresco_module_rm/classification/rm-classification-reasons.json");
|
||||
List<ClassificationReason> config = c.getConfiguredValues(ClassificationReason.class);
|
||||
assertFalse(config.isEmpty());
|
||||
}
|
||||
|
||||
@Test public void getConfiguredReasons_readingMissingConfigurationShouldProduceEmptyConfig() throws Exception
|
||||
{
|
||||
ClassificationServiceDAO c = new ClassificationServiceDAO();
|
||||
c.setReasonConfigLocation("/no/such/resource");
|
||||
assertTrue(c.getConfiguredValues(ClassificationReason.class).isEmpty());
|
||||
}
|
||||
|
||||
@Test (expected = MalformedConfiguration.class)
|
||||
public void getConfiguredReasons_readingMalformedConfigurationShouldFail()
|
||||
{
|
||||
ClassificationServiceDAO c = new ClassificationServiceDAO();
|
||||
c.setReasonConfigLocation("/alfresco/classification/rm-classification-reasons-malformed.json");
|
||||
c.getConfiguredValues(ClassificationReason.class);
|
||||
}
|
||||
}
|
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.LevelIdNotFound;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClearanceLevelManager}.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ClearanceLevelManagerUnitTest
|
||||
{
|
||||
static final ClassificationLevel TOP_SECRET = new ClassificationLevel("TS", "Top Secret Classification");
|
||||
static final ClassificationLevel SECRET = new ClassificationLevel("S", "Secret Classification");
|
||||
|
||||
static final ClearanceLevel TOP_SECRET_CLEARANCE = new ClearanceLevel(TOP_SECRET , "Top Secret Clearance");
|
||||
static final ClearanceLevel SECRET_CLEARANCE = new ClearanceLevel(SECRET, "Secret Clearance");
|
||||
|
||||
/** The class under test. */
|
||||
ClearanceLevelManager clearanceLevelManager;
|
||||
|
||||
/** Reset the {@code ClearanceLevelManager} with the three clearance levels. */
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
List<ClearanceLevel> clearanceLevels = ImmutableList.of(TOP_SECRET_CLEARANCE,
|
||||
SECRET_CLEARANCE,
|
||||
ClearanceLevelManager.NO_CLEARANCE);
|
||||
clearanceLevelManager = new ClearanceLevelManager();
|
||||
clearanceLevelManager.setClearanceLevels(clearanceLevels);
|
||||
}
|
||||
|
||||
/** Check that the secret clearance can be found from the classification level id "S". */
|
||||
@Test
|
||||
public void findLevelByClassificationLevelId_found()
|
||||
{
|
||||
ClearanceLevel actual = clearanceLevelManager.findLevelByClassificationLevelId("S");
|
||||
assertEquals(SECRET_CLEARANCE, actual);
|
||||
}
|
||||
|
||||
/** Check that an exception is thrown when the classification level id is not found. */
|
||||
@Test(expected = LevelIdNotFound.class)
|
||||
public void findLevelByClassificationLevelId_notFound()
|
||||
{
|
||||
clearanceLevelManager.findLevelByClassificationLevelId("UNKNOWN ID");
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that I have created a clearance level manager from a list of clearance levels (including the no clearance level)
|
||||
* Then the no clearance level is available
|
||||
*
|
||||
* ...(and not added in the same way as for {@link ClassificationLevelManager#setClassificationLevels(List)}).
|
||||
*/
|
||||
@Test public void noClearanceLevel()
|
||||
{
|
||||
assertEquals("The three clearance levels should be available.", 3, clearanceLevelManager.getClearanceLevels().size());
|
||||
ClearanceLevel noClearance = clearanceLevelManager.findLevelByClassificationLevelId(ClassificationLevelManager.UNCLASSIFIED_ID);
|
||||
assertEquals("'No clearance' could not be found.", ClearanceLevelManager.NO_CLEARANCE, noClearance);
|
||||
}
|
||||
}
|
@@ -1,382 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.AlfMock.generateNodeRef;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.AlfMock.generateText;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.model.QuickShareModel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.InvalidNode;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.LevelIdNotFound;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.freeze.FreezeService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper;
|
||||
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
|
||||
import org.alfresco.repo.security.permissions.AccessDeniedException;
|
||||
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.security.PersonService.PersonInfo;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ContentClassificationServiceImpl}.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ContentClassificationServiceImplUnitTest implements ClassifiedContentModel
|
||||
{
|
||||
private static final String CLASSIFICATION_LEVEL_ID = "classificationLevelId";
|
||||
private static final ClassificationLevel CLASSIFICATION_LEVEL = new ClassificationLevel(CLASSIFICATION_LEVEL_ID, generateText());
|
||||
|
||||
@InjectMocks ContentClassificationServiceImpl contentClassificationServiceImpl;
|
||||
@Mock ClassificationLevelManager mockLevelManager;
|
||||
@Mock ClassificationReasonManager mockReasonManager;
|
||||
@Mock NodeService mockNodeService;
|
||||
@Mock DictionaryService mockDictionaryService;
|
||||
@Mock FreezeService mockFreezeService;
|
||||
@Mock SecurityClearanceService mockSecurityClearanceService;
|
||||
@Mock AuthenticationUtil mockAuthenticationUtil;
|
||||
@Mock ReferredMetadataService mockReferredMetadataService;
|
||||
@Mock ClassificationAspectProperties mockPropertiesDTO;
|
||||
@Captor ArgumentCaptor<Map<QName, Serializable>> propertiesCaptor;
|
||||
|
||||
@Before public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
MockAuthenticationUtilHelper.setup(mockAuthenticationUtil);
|
||||
contentClassificationServiceImpl.setAuthenticationUtil(mockAuthenticationUtil);
|
||||
}
|
||||
|
||||
/** Classify a piece of content with a couple of reasons and check the NodeService is called correctly. */
|
||||
@Test public void classifyContent_success()
|
||||
{
|
||||
// Create a level and two reasons.
|
||||
ClassificationLevel level = new ClassificationLevel("levelId1", "displayLabelKey");
|
||||
ClassificationReason reason1 = new ClassificationReason("reasonId1", "displayLabelKey1");
|
||||
ClassificationReason reason2 = new ClassificationReason("reasonId2", "displayLabelKey2");
|
||||
// Set up the managers to return these objects when the ids are provided.
|
||||
when(mockLevelManager.findLevelById("levelId1")).thenReturn(level);
|
||||
when(mockReasonManager.findReasonById("reasonId1")).thenReturn(reason1);
|
||||
when(mockReasonManager.findReasonById("reasonId2")).thenReturn(reason2);
|
||||
// Create a content node.
|
||||
NodeRef content = new NodeRef("fake://content/");
|
||||
when(mockDictionaryService.isSubClass(mockNodeService.getType(content), ContentModel.TYPE_CONTENT)).thenReturn(true);
|
||||
when(mockNodeService.hasAspect(content, ClassifiedContentModel.ASPECT_CLASSIFIED)).thenReturn(false);
|
||||
when(mockFreezeService.isFrozen(any(NodeRef.class))).thenReturn(false);
|
||||
when(mockSecurityClearanceService.isCurrentUserClearedForClassification("levelId1")).thenReturn(true);
|
||||
// Set up the mock DTO.
|
||||
when(mockPropertiesDTO.getClassificationLevelId()).thenReturn("levelId1");
|
||||
when(mockPropertiesDTO.getClassifiedBy()).thenReturn("classifiedBy");
|
||||
when(mockPropertiesDTO.getClassificationAgency()).thenReturn("classificationAgency");
|
||||
when(mockPropertiesDTO.getClassificationReasonIds()).thenReturn(Sets.newHashSet("reasonId1", "reasonId2"));
|
||||
|
||||
// Call the method under test
|
||||
contentClassificationServiceImpl.classifyContent(mockPropertiesDTO, content);
|
||||
|
||||
verify(mockNodeService).addAspect(eq(content), eq(ClassifiedContentModel.ASPECT_CLASSIFIED),
|
||||
propertiesCaptor.capture());
|
||||
// Check the properties that were received.
|
||||
Map<QName, Serializable> properties = propertiesCaptor.getValue();
|
||||
HashSet<QName> expectedPropertyKeys = Sets.newHashSet(
|
||||
PROP_INITIAL_CLASSIFICATION,
|
||||
PROP_CURRENT_CLASSIFICATION,
|
||||
PROP_CLASSIFICATION_AGENCY,
|
||||
PROP_CLASSIFIED_BY,
|
||||
PROP_CLASSIFICATION_REASONS,
|
||||
PROP_DOWNGRADE_DATE,
|
||||
PROP_DOWNGRADE_EVENT,
|
||||
PROP_DOWNGRADE_INSTRUCTIONS,
|
||||
PROP_DECLASSIFICATION_DATE,
|
||||
PROP_DECLASSIFICATION_EVENT,
|
||||
PROP_DECLASSIFICATION_EXEMPTIONS);
|
||||
assertEquals("Aspect created with unexpected set of keys.", expectedPropertyKeys, properties.keySet());
|
||||
assertEquals("Unexpected initial classification.", level.getId(), properties.get(ClassifiedContentModel.PROP_INITIAL_CLASSIFICATION));
|
||||
assertEquals("Unexpected current classification.", level.getId(), properties.get(ClassifiedContentModel.PROP_CURRENT_CLASSIFICATION));
|
||||
assertEquals("Unexpected classifiedBy.", "classifiedBy", properties.get(ClassifiedContentModel.PROP_CLASSIFIED_BY));
|
||||
assertEquals("Unexpected agency.", "classificationAgency", properties.get(ClassifiedContentModel.PROP_CLASSIFICATION_AGENCY));
|
||||
Set<String> expectedReasonIds = Sets.newHashSet("reasonId1", "reasonId2");
|
||||
assertEquals("Unexpected set of reasons.", expectedReasonIds, properties.get(ClassifiedContentModel.PROP_CLASSIFICATION_REASONS));
|
||||
}
|
||||
|
||||
/** Classify a folder using the <code>classifyContent</code> method and check that an exception is raised. */
|
||||
@Test(expected = InvalidNode.class)
|
||||
public void validateContent_notContent()
|
||||
{
|
||||
// Create a folder node.
|
||||
NodeRef notAPieceOfContent = new NodeRef("not://a/piece/of/content/");
|
||||
when(mockNodeService.getType(notAPieceOfContent)).thenReturn(ContentModel.TYPE_FOLDER);
|
||||
|
||||
// Call the method under test.
|
||||
contentClassificationServiceImpl.validateContent(notAPieceOfContent);
|
||||
}
|
||||
|
||||
/** Classify a piece of content that has already been shared */
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void validateContent_sharedContent()
|
||||
{
|
||||
NodeRef sharedContent = generateNodeRef(mockNodeService);
|
||||
when(mockDictionaryService.isSubClass(mockNodeService.getType(sharedContent), ContentModel.TYPE_CONTENT)).thenReturn(true);
|
||||
when(mockNodeService.hasAspect(sharedContent, QuickShareModel.ASPECT_QSHARE)).thenReturn(true);
|
||||
|
||||
// Call the method under test.
|
||||
contentClassificationServiceImpl.validateContent(sharedContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a user can't classify content with a level that either (a) doesn't exist, or (b) they don't have
|
||||
* clearance for. (Both cases are covered by the same flow through the code).
|
||||
*/
|
||||
@Test(expected = LevelIdNotFound.class)
|
||||
public void classifyContent_notFound()
|
||||
{
|
||||
// The user doesn't have level 1 clearance.
|
||||
when(mockSecurityClearanceService.isCurrentUserClearedForClassification("levelId1")).thenReturn(false);
|
||||
// Set up the mock DTO.
|
||||
when(mockPropertiesDTO.getClassificationLevelId()).thenReturn("levelId1");
|
||||
when(mockPropertiesDTO.getClassifiedBy()).thenReturn("classifiedBy");
|
||||
when(mockPropertiesDTO.getClassificationAgency()).thenReturn("classificationAgency");
|
||||
when(mockPropertiesDTO.getClassificationReasonIds()).thenReturn(Sets.newHashSet("reasonId1", "reasonId2"));
|
||||
|
||||
// Call the method under test.
|
||||
contentClassificationServiceImpl.validateProperties(mockPropertiesDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a user can't classify content that is frozen.
|
||||
*/
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
public void classifyContent_frozen()
|
||||
{
|
||||
ClassificationLevel level = new ClassificationLevel("levelId1", "displayLabelKey");
|
||||
ClassificationReason reason1 = new ClassificationReason("reasonId1", "displayLabelKey1");
|
||||
// Set up the managers to return these objects when the ids are provided.
|
||||
when(mockLevelManager.findLevelById("levelId1")).thenReturn(level);
|
||||
when(mockReasonManager.findReasonById("reasonId1")).thenReturn(reason1);
|
||||
// Create a content node.
|
||||
NodeRef content = new NodeRef("fake://content/");
|
||||
when(mockDictionaryService.isSubClass(mockNodeService.getType(content), ContentModel.TYPE_CONTENT)).thenReturn(true);
|
||||
when(mockNodeService.hasAspect(content, ClassifiedContentModel.ASPECT_CLASSIFIED)).thenReturn(false);
|
||||
when(mockFreezeService.isFrozen(any(NodeRef.class))).thenReturn(true);
|
||||
when(mockSecurityClearanceService.isCurrentUserClearedForClassification("levelId1")).thenReturn(true);
|
||||
// Set up the mock DTO.
|
||||
when(mockPropertiesDTO.getClassificationLevelId()).thenReturn("levelId1");
|
||||
when(mockPropertiesDTO.getClassifiedBy()).thenReturn("classifiedBy");
|
||||
when(mockPropertiesDTO.getClassificationAgency()).thenReturn("classificationAgency");
|
||||
when(mockPropertiesDTO.getClassificationReasonIds()).thenReturn(Sets.newHashSet("reasonId1", "reasonId2"));
|
||||
|
||||
// Call the method under test
|
||||
contentClassificationServiceImpl.classifyContent(mockPropertiesDTO, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that a node does not have the classify aspect applied
|
||||
* When I ask for the nodes classification
|
||||
* Then 'Unclassified' is returned
|
||||
*/
|
||||
@Test
|
||||
public void getCurrentClassificationWithoutAspectApplied()
|
||||
{
|
||||
NodeRef nodeRef = generateNodeRef(mockNodeService);
|
||||
when(mockNodeService.hasAspect(nodeRef, ClassifiedContentModel.ASPECT_CLASSIFIED))
|
||||
.thenReturn(false);
|
||||
|
||||
ClassificationLevel classificationLevel = contentClassificationServiceImpl.getCurrentClassification(nodeRef);
|
||||
|
||||
assertEquals(ClassificationLevelManager.UNCLASSIFIED, classificationLevel);
|
||||
verify(mockNodeService).hasAspect(nodeRef, ClassifiedContentModel.ASPECT_CLASSIFIED);
|
||||
verifyNoMoreInteractions(mockNodeService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that a node is classified
|
||||
* When I ask for the node classification
|
||||
* Then I get the correct classificationlevel
|
||||
*/
|
||||
@Test
|
||||
public void getCurrentClassification()
|
||||
{
|
||||
NodeRef nodeRef = generateNodeRef(mockNodeService);
|
||||
when(mockNodeService.hasAspect(nodeRef, ClassifiedContentModel.ASPECT_CLASSIFIED))
|
||||
.thenReturn(true);
|
||||
when(mockNodeService.getProperty(nodeRef, ClassifiedContentModel.PROP_CURRENT_CLASSIFICATION))
|
||||
.thenReturn(CLASSIFICATION_LEVEL_ID);
|
||||
when(mockLevelManager.findLevelById(CLASSIFICATION_LEVEL_ID))
|
||||
.thenReturn(CLASSIFICATION_LEVEL);
|
||||
|
||||
ClassificationLevel classificationLevel = contentClassificationServiceImpl.getCurrentClassification(nodeRef);
|
||||
|
||||
assertEquals(CLASSIFICATION_LEVEL, classificationLevel);
|
||||
verify(mockNodeService).hasAspect(nodeRef, ClassifiedContentModel.ASPECT_CLASSIFIED);
|
||||
verify(mockNodeService).getProperty(nodeRef, ClassifiedContentModel.PROP_CURRENT_CLASSIFICATION);
|
||||
verify(mockLevelManager).findLevelById(CLASSIFICATION_LEVEL_ID);
|
||||
verifyNoMoreInteractions(mockNodeService, mockLevelManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that the node is classified
|
||||
* And the user has no security clearance
|
||||
* When I ask if the current user has clearance
|
||||
* Then false
|
||||
*/
|
||||
@Test public void userWithNoClearanceIsntClearedOnClassifiedNode()
|
||||
{
|
||||
// assign test classification to node
|
||||
NodeRef nodeRef = generateNodeRef(mockNodeService);
|
||||
when(mockNodeService.hasAspect(nodeRef, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
String classificationLevelId = generateText();
|
||||
when(mockNodeService.getProperty(nodeRef, PROP_CURRENT_CLASSIFICATION)).thenReturn(classificationLevelId);
|
||||
ClassificationLevel classificationLevel = new ClassificationLevel(classificationLevelId, generateText());
|
||||
when(mockLevelManager.findLevelById(classificationLevelId)).thenReturn(classificationLevel);
|
||||
|
||||
// create user with no clearance
|
||||
SecurityClearance clearance = new SecurityClearance(mock(PersonInfo.class), ClearanceLevelManager.NO_CLEARANCE);
|
||||
when(mockSecurityClearanceService.getUserSecurityClearance()).thenReturn(clearance);
|
||||
|
||||
assertFalse(contentClassificationServiceImpl.hasClearance(nodeRef));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that the node is classified
|
||||
* And the user has clearance greater or equal to the the classification
|
||||
* When I ask if the user has clearance
|
||||
* Then true
|
||||
*/
|
||||
@Test public void classifiedNodeUserClearanceAtLeast()
|
||||
{
|
||||
// init classification levels
|
||||
ClassificationLevel topSecret = new ClassificationLevel("TopSecret", generateText());
|
||||
String secretId = "Secret";
|
||||
ClassificationLevel secret = new ClassificationLevel(secretId, generateText());
|
||||
ClassificationLevel confidential = new ClassificationLevel("Confidential", generateText());
|
||||
List<ClassificationLevel> classificationLevels = Arrays.asList(topSecret, secret, confidential, ClassificationLevelManager.UNCLASSIFIED);
|
||||
when(mockLevelManager.getClassificationLevels()).thenReturn(ImmutableList.copyOf(classificationLevels));
|
||||
|
||||
// set nodes classification
|
||||
NodeRef nodeRef = generateNodeRef(mockNodeService);
|
||||
when(mockNodeService.hasAspect(nodeRef, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(nodeRef, PROP_CURRENT_CLASSIFICATION)).thenReturn(secretId);
|
||||
when(mockLevelManager.findLevelById(secretId)).thenReturn(secret);
|
||||
|
||||
// set users security clearance
|
||||
when(mockSecurityClearanceService.isCurrentUserClearedForClassification("Secret")).thenReturn(true);
|
||||
|
||||
assertTrue(contentClassificationServiceImpl.hasClearance(nodeRef));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that the node is classified
|
||||
* And the user has clearance less than the classification
|
||||
* When I ask if the user has clearance
|
||||
* Then false
|
||||
*/
|
||||
@Test public void classifiedNodeUserClearanceLess()
|
||||
{
|
||||
// init classification levels
|
||||
ClassificationLevel topSecret = new ClassificationLevel("TopSecret", generateText());
|
||||
String secretId = "Secret";
|
||||
ClassificationLevel secret = new ClassificationLevel(secretId, generateText());
|
||||
ClassificationLevel confidential = new ClassificationLevel("Confidential", generateText());
|
||||
List<ClassificationLevel> classificationLevels = Arrays.asList(topSecret, secret, confidential, ClassificationLevelManager.UNCLASSIFIED);
|
||||
when(mockLevelManager.getClassificationLevels()).thenReturn(ImmutableList.copyOf(classificationLevels));
|
||||
|
||||
// set nodes classification
|
||||
NodeRef nodeRef = generateNodeRef(mockNodeService);
|
||||
when(mockNodeService.hasAspect(nodeRef, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(nodeRef, PROP_CURRENT_CLASSIFICATION)).thenReturn(secretId);
|
||||
when(mockLevelManager.findLevelById(secretId)).thenReturn(secret);
|
||||
|
||||
// set users security clearance
|
||||
when(mockSecurityClearanceService.isCurrentUserClearedForClassification("Secret")).thenReturn(false);
|
||||
|
||||
assertFalse(contentClassificationServiceImpl.hasClearance(nodeRef));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that I classify a node with a level not equal to "Unclassified"
|
||||
* When I ask if the node is classified
|
||||
* Then return true
|
||||
*/
|
||||
@Test public void contentClassified_levelNotUnclassified()
|
||||
{
|
||||
NodeRef nodeRef = generateNodeRef(mockNodeService);
|
||||
|
||||
when(mockNodeService.getProperty(nodeRef, PROP_CURRENT_CLASSIFICATION)).thenReturn("level1");
|
||||
when(mockNodeService.hasAspect(nodeRef, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
|
||||
assertTrue(contentClassificationServiceImpl.isClassified(nodeRef));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that I classify a node with level "Unclassified"
|
||||
* When I ask if the node is classified
|
||||
* Then return false
|
||||
*/
|
||||
@Test public void contentClassified_levelUnclassified()
|
||||
{
|
||||
NodeRef nodeRef = generateNodeRef(mockNodeService);
|
||||
|
||||
when(mockNodeService.getProperty(nodeRef, PROP_CURRENT_CLASSIFICATION)).thenReturn(ClassificationLevelManager.UNCLASSIFIED_ID);
|
||||
when(mockNodeService.hasAspect(nodeRef, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
|
||||
assertFalse(contentClassificationServiceImpl.isClassified(nodeRef));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that a node is not classified
|
||||
* When I ask if the node is classified
|
||||
* Then return false
|
||||
*/
|
||||
@Test public void contentNotClassified()
|
||||
{
|
||||
NodeRef nodeRef = generateNodeRef(mockNodeService);
|
||||
|
||||
when(mockNodeService.hasAspect(nodeRef, ASPECT_CLASSIFIED)).thenReturn(false);
|
||||
|
||||
assertFalse(contentClassificationServiceImpl.isClassified(nodeRef));
|
||||
}
|
||||
}
|
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.ExemptionCategoryIdNotFound;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ExemptionCategoryManager}.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ExemptionCategoryManagerUnitTest
|
||||
{
|
||||
private static final ExemptionCategory CATEGORY_1 = new ExemptionCategory("id1", "displayLabelKey1");
|
||||
private static final ExemptionCategory CATEGORY_2 = new ExemptionCategory("id2", "displayLabelKey2");
|
||||
private static final ExemptionCategory CATEGORY_3 = new ExemptionCategory("id3", "displayLabelKey3");
|
||||
private static final List<ExemptionCategory> CATEGORIES = Arrays.asList(CATEGORY_1, CATEGORY_2, CATEGORY_3);
|
||||
|
||||
private ExemptionCategoryManager exemptionCategoryManager;
|
||||
|
||||
@Before public void setup()
|
||||
{
|
||||
exemptionCategoryManager = new ExemptionCategoryManager();
|
||||
exemptionCategoryManager.setExemptionCategories(CATEGORIES);
|
||||
}
|
||||
|
||||
@Test public void findClassificationById_found()
|
||||
{
|
||||
ExemptionCategory actual = exemptionCategoryManager.findCategoryById("id2");
|
||||
assertEquals(CATEGORY_2, actual);
|
||||
}
|
||||
|
||||
@Test(expected = ExemptionCategoryIdNotFound.class) public void findClassificationById_notFound()
|
||||
{
|
||||
exemptionCategoryManager.findCategoryById("id_unknown");
|
||||
}
|
||||
}
|
@@ -1,327 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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.alfresco.util.GUID.generate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.LevelIdNotFound;
|
||||
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;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SecurityClearanceServiceImpl}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @author David Webster
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class SecurityClearanceServiceImplUnitTest
|
||||
{
|
||||
@InjectMocks private SecurityClearanceServiceImpl securityClearanceServiceImpl;
|
||||
|
||||
@Mock private AuthenticationUtil mockAuthenticationUtil;
|
||||
@Mock private ClassificationLevelManager mockClassificationLevelManager;
|
||||
@Mock private DictionaryService mockDictionaryService;
|
||||
@Mock private NodeService mockNodeService;
|
||||
@Mock private PersonService mockPersonService;
|
||||
@Mock private ClassificationSchemeService mockClassificationSchemeService;
|
||||
@Mock private ClearanceLevelManager mockClearanceLevelManager;
|
||||
@Mock private ClassificationLevelComparator mockClassificationLevelComparator;
|
||||
|
||||
@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(userNode)).thenReturn(info);
|
||||
|
||||
when(mockNodeService.hasAspect(userNode, ASPECT_SECURITY_CLEARANCE)).thenReturn(clearanceLevel != null);
|
||||
when(mockNodeService.getProperty(userNode, PROP_CLEARANCE_LEVEL)).thenReturn(clearanceLevel);
|
||||
|
||||
if (clearanceLevel != null)
|
||||
{
|
||||
final ClassificationLevel dummyValue = new ClassificationLevel(clearanceLevel, clearanceLevel);
|
||||
when(mockClassificationLevelManager.findLevelById(clearanceLevel)).thenReturn(dummyValue);
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
@Test public void userWithNoClearanceGetsDefaultClearance()
|
||||
{
|
||||
final PersonInfo user1 = createMockPerson("user1", "User", "One", null);
|
||||
MockAuthenticationUtilHelper.setup(mockAuthenticationUtil, user1.getUserName());
|
||||
when(mockClassificationSchemeService.getUnclassifiedClassificationLevel())
|
||||
.thenReturn(ClassificationLevelManager.UNCLASSIFIED);
|
||||
when(mockClearanceLevelManager.findLevelByClassificationLevelId(ClassificationLevelManager.UNCLASSIFIED_ID))
|
||||
.thenReturn(ClearanceLevelManager.NO_CLEARANCE);
|
||||
|
||||
final SecurityClearance clearance = securityClearanceServiceImpl.getUserSecurityClearance();
|
||||
|
||||
assertEquals(ClassificationLevelManager.UNCLASSIFIED, clearance.getClearanceLevel().getHighestClassificationLevel());
|
||||
|
||||
}
|
||||
|
||||
/** Check that a user can have their clearance set by an authorised user. */
|
||||
@Test public void setUserSecurityClearance_setClearance()
|
||||
{
|
||||
// Create the user who will have their clearance set.
|
||||
String userName = "User 1";
|
||||
NodeRef personNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, userName);
|
||||
PersonInfo personInfo = new PersonInfo(personNode, userName, "first", "last");
|
||||
when(mockPersonService.getPerson(userName, false)).thenReturn(personNode);
|
||||
when(mockPersonService.getPerson(personNode)).thenReturn(personInfo);
|
||||
|
||||
// Create the authorised user.
|
||||
String authorisedUserName = "authorisedUser";
|
||||
when(mockAuthenticationUtil.getRunAsUser()).thenReturn(authorisedUserName);
|
||||
NodeRef authorisedPersonNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, authorisedUserName);
|
||||
PersonInfo authorisedPersonInfo = new PersonInfo(authorisedPersonNode, authorisedUserName, "first", "last");
|
||||
when(mockPersonService.getPerson(authorisedUserName, false)).thenReturn(authorisedPersonNode);
|
||||
when(mockPersonService.getPerson(authorisedPersonNode)).thenReturn(authorisedPersonInfo);
|
||||
// The current user is not system.
|
||||
when(mockAuthenticationUtil.isRunAsUserTheSystemUser()).thenReturn(false);
|
||||
|
||||
// Create the clearance level.
|
||||
String topSecretId = "ClearanceId";
|
||||
ClassificationLevel level = new ClassificationLevel(topSecretId, "TopSecretKey");
|
||||
ClearanceLevel clearanceLevel = new ClearanceLevel(level, "TopSecretKey");
|
||||
when(mockClearanceLevelManager.findLevelByClassificationLevelId(topSecretId)).thenReturn(clearanceLevel);
|
||||
|
||||
// The authorised user is cleared to use this clearance.
|
||||
when(mockNodeService.hasAspect(authorisedPersonNode, ASPECT_SECURITY_CLEARANCE)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(authorisedPersonNode, PROP_CLEARANCE_LEVEL)).thenReturn(topSecretId);
|
||||
// The authenticated user's clearance level is at least as secure as the level being used.
|
||||
when(mockClassificationLevelComparator.compare(level, level)).thenReturn(0);
|
||||
|
||||
// Once the user's clearance has been set then the node service is queried about it.
|
||||
when(mockNodeService.hasAspect(personNode, ASPECT_SECURITY_CLEARANCE)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(personNode, PROP_CLEARANCE_LEVEL)).thenReturn(topSecretId);
|
||||
|
||||
// Call the method under test.
|
||||
SecurityClearance securityClearance = securityClearanceServiceImpl.setUserSecurityClearance(userName, topSecretId);
|
||||
|
||||
// Check the returned value.
|
||||
assertEquals(personInfo, securityClearance.getPersonInfo());
|
||||
assertEquals(clearanceLevel, securityClearance.getClearanceLevel());
|
||||
// Check the value stored in the node service.
|
||||
verify(mockNodeService).setProperty(personNode, PROP_CLEARANCE_LEVEL, topSecretId);
|
||||
}
|
||||
|
||||
/** Check that a user cannot raise someone else's clearance above their own. */
|
||||
@Test(expected = LevelIdNotFound.class)
|
||||
public void setUserSecurityClearance_insufficientClearance()
|
||||
{
|
||||
// Create the "Top Secret" and "Confidential" clearances.
|
||||
String topSecretId = "TopSecretClearanceId";
|
||||
ClassificationLevel topSecret = new ClassificationLevel(topSecretId, "TopSecretKey");
|
||||
when(mockClassificationLevelManager.findLevelById(topSecretId)).thenReturn(topSecret);
|
||||
String confidentialId = "ConfidentialClearanceId";
|
||||
ClassificationLevel confidential = new ClassificationLevel(confidentialId, "ConfidentialKey");
|
||||
ClearanceLevel confidentialClearance = new ClearanceLevel(confidential, "ConfidentialKey");
|
||||
when(mockClearanceLevelManager.findLevelByClassificationLevelId(confidentialId)).thenReturn(confidentialClearance);
|
||||
|
||||
// Create the user attempting to use the API with "Confidential" clearance.
|
||||
String userName = "unauthorisedUser";
|
||||
when(mockAuthenticationUtil.getRunAsUser()).thenReturn(userName);
|
||||
NodeRef personNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, userName);
|
||||
PersonInfo personInfo = new PersonInfo(personNode, userName, "first", "last");
|
||||
when(mockPersonService.getPerson(userName, false)).thenReturn(personNode);
|
||||
when(mockPersonService.getPerson(personNode)).thenReturn(personInfo);
|
||||
|
||||
// The authorised user is cleared to use this clearance.
|
||||
when(mockNodeService.hasAspect(personNode, ASPECT_SECURITY_CLEARANCE)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(personNode, PROP_CLEARANCE_LEVEL)).thenReturn(confidentialId);
|
||||
// The authenticated user's clearance level not high enough.
|
||||
when(mockClassificationLevelComparator.compare(confidential, topSecret)).thenReturn(-1);
|
||||
|
||||
// Create the user who will have their clearance set.
|
||||
String targetUserName = "Target User";
|
||||
NodeRef targetPersonNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, targetUserName);
|
||||
PersonInfo targetPersonInfo = new PersonInfo(targetPersonNode, targetUserName, "first", "last");
|
||||
when(mockPersonService.getPerson(targetUserName, false)).thenReturn(targetPersonNode);
|
||||
when(mockPersonService.getPerson(targetPersonNode)).thenReturn(targetPersonInfo);
|
||||
|
||||
// Call the method under test and expect an exception.
|
||||
securityClearanceServiceImpl.setUserSecurityClearance(targetUserName, topSecretId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a user with "Secret" clearance is cleared to use the "Secret" classification.
|
||||
*/
|
||||
@Test public void isCurrentUserClearedForClassification_hasClearance()
|
||||
{
|
||||
ClassificationLevel secret = new ClassificationLevel("2", "Secret");
|
||||
when(mockClassificationLevelManager.findLevelById("2")).thenReturn(secret);
|
||||
|
||||
createMockPerson("Cleared", "Cleared", "Cleared", "2");
|
||||
when(mockAuthenticationUtil.getRunAsUser()).thenReturn("Cleared");
|
||||
when(mockClearanceLevelManager.findLevelByClassificationLevelId("2")).thenReturn(new ClearanceLevel(secret, "Secret"));
|
||||
|
||||
// The authenticated user's clearance level is high enough to view the classification.
|
||||
when(mockClassificationLevelComparator.compare(secret, secret)).thenReturn(0);
|
||||
|
||||
// Call the method under test.
|
||||
boolean result = securityClearanceServiceImpl.isCurrentUserClearedForClassification("2");
|
||||
|
||||
assertTrue("A user with 'Secret' clearance should be able to access the 'Secret' classification.", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a user with no clearance is not cleared to use the "Secret" classification.
|
||||
*/
|
||||
@Test public void isCurrentUserClearedForClassification_noClearance()
|
||||
{
|
||||
ClassificationLevel secret = new ClassificationLevel("2", "Secret");
|
||||
when(mockClassificationLevelManager.findLevelById("2")).thenReturn(secret);
|
||||
|
||||
createMockPerson("Uncleared", "Uncleared", "Uncleared", ClassificationLevelManager.UNCLASSIFIED_ID);
|
||||
when(mockAuthenticationUtil.getRunAsUser()).thenReturn("Uncleared");
|
||||
when(mockClearanceLevelManager.findLevelByClassificationLevelId(ClassificationLevelManager.UNCLASSIFIED_ID)).thenReturn(ClearanceLevelManager.NO_CLEARANCE);
|
||||
|
||||
// The authenticated user's clearance level not high enough.
|
||||
when(mockClassificationLevelComparator.compare(ClassificationLevelManager.UNCLASSIFIED, secret)).thenReturn(-1);
|
||||
|
||||
// Call the method under test.
|
||||
boolean result = securityClearanceServiceImpl.isCurrentUserClearedForClassification("2");
|
||||
|
||||
assertFalse("A user with no clearance should not be able to access the 'Secret' classification.", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a user with "Top Secret" clearance is not cleared to use a non-existent classification.
|
||||
*/
|
||||
@Test public void isCurrentUserClearedForClassification_classificationNotFound()
|
||||
{
|
||||
ClassificationLevel topSecret = new ClassificationLevel("1", "TopSecret");
|
||||
createMockPerson("Cleared", "Cleared", "Cleared", "1");
|
||||
when(mockAuthenticationUtil.getRunAsUser()).thenReturn("Cleared");
|
||||
when(mockClearanceLevelManager.findLevelByClassificationLevelId("1")).thenReturn(new ClearanceLevel(topSecret, "TopSecret"));
|
||||
// Set up the made up classification.
|
||||
String madeUpId = "Made Up Id";
|
||||
when(mockClassificationLevelManager.findLevelById(madeUpId)).thenThrow(new LevelIdNotFound(madeUpId));
|
||||
|
||||
// Call the method under test.
|
||||
boolean result = securityClearanceServiceImpl.isCurrentUserClearedForClassification(madeUpId);
|
||||
|
||||
assertFalse("No one should be cleared to use a fictional classification.", result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that all levels are returned
|
||||
*/
|
||||
@Test public void getClearanceLevels()
|
||||
{
|
||||
|
||||
// Create a list of clearance levels
|
||||
ImmutableList<ClearanceLevel> mockClearanceLevels = ImmutableList.of(
|
||||
new ClearanceLevel(new ClassificationLevel("level1", "Level One"), "Clearance One"),
|
||||
new ClearanceLevel(new ClassificationLevel("level2", "Level Two"), "Clearance Two"),
|
||||
new ClearanceLevel(new ClassificationLevel("level3", "Level Three"), "Clearance Three")
|
||||
);
|
||||
|
||||
when(mockClearanceLevelManager.getClearanceLevels())
|
||||
.thenReturn(mockClearanceLevels);
|
||||
when(mockClearanceLevelManager.getMostSecureLevel())
|
||||
.thenReturn(mockClearanceLevels.get(0));
|
||||
|
||||
List<ClearanceLevel> actualClearanceLevels = securityClearanceServiceImpl.getClearanceLevels();
|
||||
|
||||
assertEquals(mockClearanceLevels.size(), actualClearanceLevels.size());
|
||||
assertEquals(mockClearanceLevels.get(0), actualClearanceLevels.get(0));
|
||||
assertEquals(mockClearanceLevels.get(1), actualClearanceLevels.get(1));
|
||||
assertEquals(mockClearanceLevels.get(2), actualClearanceLevels.get(2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a user with restricted access only gets some of the levels.
|
||||
*/
|
||||
@Test
|
||||
public void getRestrictedClearanceLevels()
|
||||
{
|
||||
|
||||
// Create a list of clearance levels
|
||||
ImmutableList<ClearanceLevel> mockClearanceLevels = ImmutableList.of(
|
||||
new ClearanceLevel(new ClassificationLevel("level1", "Level One"), "Clearance One"),
|
||||
new ClearanceLevel(new ClassificationLevel("level2", "Level Two"), "Clearance Two"),
|
||||
new ClearanceLevel(new ClassificationLevel("level3", "Level Three"), "Clearance Three")
|
||||
);
|
||||
|
||||
when(mockClearanceLevelManager.getClearanceLevels()).thenReturn(mockClearanceLevels);
|
||||
when(mockClearanceLevelManager.getMostSecureLevel()).thenReturn(mockClearanceLevels.get(1));
|
||||
|
||||
List<ClearanceLevel> restrictedClearanceLevels = securityClearanceServiceImpl.getClearanceLevels();
|
||||
|
||||
assertEquals(2, restrictedClearanceLevels.size());
|
||||
assertEquals(mockClearanceLevels.get(1), restrictedClearanceLevels.get(0));
|
||||
assertEquals(mockClearanceLevels.get(2), restrictedClearanceLevels.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasUserClearance()
|
||||
{
|
||||
// Check if the current user has clearance
|
||||
PersonInfo user1 = createMockPerson(generate(), generate(), generate(), null);
|
||||
MockAuthenticationUtilHelper.setup(mockAuthenticationUtil, user1.getUserName());
|
||||
assertFalse(securityClearanceServiceImpl.hasCurrentUserClearance());
|
||||
|
||||
// Check if a user with a given id has clearance
|
||||
String user2 = generate();
|
||||
String classificationLevelId = generate();
|
||||
ClassificationLevel classificationLevel = new ClassificationLevel(classificationLevelId, generate());
|
||||
ClearanceLevel clearanceLevel = new ClearanceLevel(classificationLevel, generate());
|
||||
|
||||
when(mockClearanceLevelManager.findLevelByClassificationLevelId(classificationLevelId)).thenReturn(clearanceLevel);
|
||||
|
||||
createMockPerson(user2, generate(), generate(), classificationLevelId);
|
||||
MockAuthenticationUtilHelper.setup(mockAuthenticationUtil, user2);
|
||||
assertTrue(securityClearanceServiceImpl.hasUserClearance(user2));
|
||||
}
|
||||
|
||||
}
|
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link UserQueryParams}.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class UserQueryParamsUnitTest
|
||||
{
|
||||
private static final QName QNAME1 = QName.createQName("1");
|
||||
private static final QName QNAME2 = QName.createQName("2");
|
||||
|
||||
/** Check that the constructor escapes backslashes correctly. */
|
||||
@Test
|
||||
public void testConstructor_backSlashes()
|
||||
{
|
||||
UserQueryParams userQueryParams = new UserQueryParams("\\Hello\\\\World!");
|
||||
assertEquals("\\\\Hello\\\\\\\\World!", userQueryParams.getSearchTerm());
|
||||
}
|
||||
|
||||
/** Check that the constructor rejects null. */
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testConstructor_null()
|
||||
{
|
||||
new UserQueryParams(null);
|
||||
}
|
||||
|
||||
/** Check that providing two properties results in a list being returned. */
|
||||
@Test
|
||||
public void testWithFilterProps_twoProperties()
|
||||
{
|
||||
UserQueryParams userQueryParams = new UserQueryParams("Search term");
|
||||
userQueryParams.withFilterProps(QNAME1, QNAME2);
|
||||
assertEquals(Arrays.asList(QNAME1, QNAME2), userQueryParams.getFilterProps());
|
||||
}
|
||||
|
||||
/** Check that the first parameter can't be null. */
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithFilterProps_firstPropertyNull()
|
||||
{
|
||||
UserQueryParams userQueryParams = new UserQueryParams("Search term");
|
||||
userQueryParams.withFilterProps(null);
|
||||
}
|
||||
|
||||
/** Check that providing a null after the first argument fails. */
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithFilterProps_containsNull()
|
||||
{
|
||||
UserQueryParams userQueryParams = new UserQueryParams("Search term");
|
||||
userQueryParams.withFilterProps(QNAME1, (QName) null);
|
||||
}
|
||||
}
|
@@ -1,193 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.permission;
|
||||
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceBootstrap;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ContentClassificationService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.AlfMock;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.security.AccessStatus;
|
||||
import org.alfresco.util.Triple;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
/**
|
||||
* Classification permission pre-processor unit test
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClassificationPermissionPreProcessorUnitTest extends BaseUnitTest
|
||||
{
|
||||
/** test artifact */
|
||||
private @InjectMocks ClassificationPermissionPreProcessor processor;
|
||||
|
||||
/** mocks */
|
||||
private @Mock ClassificationServiceBootstrap mockedClassificationServiceBootstrap;
|
||||
private @Mock ContentClassificationService mockedContentClassificationService;
|
||||
|
||||
/** test data */
|
||||
private NodeRef nodeRef;
|
||||
private String perm = AlfMock.generateText();
|
||||
private String user = AlfMock.generateText();
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void before() throws Exception
|
||||
{
|
||||
super.before();
|
||||
nodeRef = generateCmContent(AlfMock.generateText());
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that the classification hierarchy hasn't been bootstraped
|
||||
* When the classification permission pre processor is executed
|
||||
* Then an undetermined result will be returned
|
||||
*/
|
||||
@Test
|
||||
public void classificationServiceNotBootstraped()
|
||||
{
|
||||
when(mockedClassificationServiceBootstrap.isInitialised())
|
||||
.thenReturn(false);
|
||||
|
||||
assertEquals(AccessStatus.UNDETERMINED, processor.process(nodeRef, perm));
|
||||
|
||||
verify(mockedContentClassificationService, never())
|
||||
.hasClearance(nodeRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that the node being evaluated is already being processed
|
||||
* When the classification permission pre processor is executed
|
||||
* Then an undetermined result will be returned
|
||||
*/
|
||||
@Test
|
||||
public void nodeBeingProcessed()
|
||||
{
|
||||
when(mockedClassificationServiceBootstrap.isInitialised())
|
||||
.thenReturn(true);
|
||||
when(mockedTransactionalResourceHelper.getSet(ClassificationPermissionPreProcessor.KEY_PROCESSING))
|
||||
.thenReturn(Sets.newHashSet(nodeRef));
|
||||
|
||||
assertEquals(AccessStatus.UNDETERMINED, processor.process(nodeRef, perm));
|
||||
|
||||
verify(mockedContentClassificationService, never())
|
||||
.hasClearance(nodeRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that the node already exists in the transaction cache
|
||||
* When the classification permission pre processor is executed
|
||||
* Then the result will be returned from the cache
|
||||
*/
|
||||
@Test
|
||||
public void resultAlreadyCached()
|
||||
{
|
||||
when(mockedClassificationServiceBootstrap.isInitialised())
|
||||
.thenReturn(true);
|
||||
NodeRef notTheNodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
when(mockedTransactionalResourceHelper.getSet(ClassificationPermissionPreProcessor.KEY_PROCESSING))
|
||||
.thenReturn(Sets.newHashSet(notTheNodeRef));
|
||||
when(mockedAuthenticationUtil.getRunAsUser())
|
||||
.thenReturn(user);
|
||||
|
||||
Map<Object, Object> cache = new HashMap<Object, Object>(1);
|
||||
cache.put(new Triple<NodeRef, String, String>(nodeRef, perm, user), AccessStatus.ALLOWED);
|
||||
when(mockedTransactionalResourceHelper.getMap(ClassificationPermissionPreProcessor.KEY_CACHE))
|
||||
.thenReturn(cache);
|
||||
|
||||
assertEquals(AccessStatus.ALLOWED, processor.process(nodeRef, perm));
|
||||
|
||||
verify(mockedContentClassificationService, never())
|
||||
.hasClearance(nodeRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that the user does have clearance
|
||||
* When the classification permission pre processor is executed
|
||||
* Then an undetermined result will be returned
|
||||
*/
|
||||
@Test
|
||||
public void userHasClearance()
|
||||
{
|
||||
when(mockedClassificationServiceBootstrap.isInitialised())
|
||||
.thenReturn(true);
|
||||
NodeRef notTheNodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
when(mockedTransactionalResourceHelper.getSet(ClassificationPermissionPreProcessor.KEY_PROCESSING))
|
||||
.thenReturn(Sets.newHashSet(notTheNodeRef));
|
||||
when(mockedAuthenticationUtil.getRunAsUser())
|
||||
.thenReturn(user);
|
||||
|
||||
Map<Object, Object> cache = new HashMap<Object, Object>(1);
|
||||
cache.put(new Triple<NodeRef, String, String>(notTheNodeRef, perm, user), AccessStatus.ALLOWED);
|
||||
when(mockedTransactionalResourceHelper.getMap(ClassificationPermissionPreProcessor.KEY_CACHE))
|
||||
.thenReturn(cache);
|
||||
|
||||
when(mockedContentClassificationService.hasClearance(nodeRef))
|
||||
.thenReturn(true);
|
||||
|
||||
assertEquals(AccessStatus.UNDETERMINED, processor.process(nodeRef, perm));
|
||||
|
||||
verify(mockedContentClassificationService)
|
||||
.hasClearance(nodeRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that the user doesn't have clearance
|
||||
* When the classification permission pre processor is executed
|
||||
* Then an undetermined result will be returned
|
||||
*/
|
||||
@Test
|
||||
public void userDoesNotHaveClearance()
|
||||
{
|
||||
when(mockedClassificationServiceBootstrap.isInitialised())
|
||||
.thenReturn(true);
|
||||
NodeRef notTheNodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
when(mockedTransactionalResourceHelper.getSet(ClassificationPermissionPreProcessor.KEY_PROCESSING))
|
||||
.thenReturn(Sets.newHashSet(notTheNodeRef));
|
||||
when(mockedAuthenticationUtil.getRunAsUser())
|
||||
.thenReturn(user);
|
||||
|
||||
Map<Object, Object> cache = new HashMap<Object, Object>(1);
|
||||
cache.put(new Triple<NodeRef, String, String>(notTheNodeRef, perm, user), AccessStatus.ALLOWED);
|
||||
when(mockedTransactionalResourceHelper.getMap(ClassificationPermissionPreProcessor.KEY_CACHE))
|
||||
.thenReturn(cache);
|
||||
|
||||
when(mockedContentClassificationService.hasClearance(nodeRef))
|
||||
.thenReturn(false);
|
||||
|
||||
assertEquals(AccessStatus.DENIED, processor.process(nodeRef, perm));
|
||||
|
||||
verify(mockedContentClassificationService)
|
||||
.hasClearance(nodeRef);
|
||||
}
|
||||
}
|
@@ -1,103 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.classification.validation.FilenameFieldValidator.ILLEGAL_ABBREVIATION_CHARS;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
|
||||
import static org.hamcrest.CoreMatchers.allOf;
|
||||
import static org.hamcrest.core.IsCollectionContaining.hasItem;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalAbbreviationChars;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClassificationLevelFieldsValidator}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @author tpage
|
||||
*/
|
||||
public class ClassificationLevelFieldsValidatorUnitTest
|
||||
{
|
||||
private final ClassificationLevelFieldsValidator validator = new ClassificationLevelFieldsValidator();
|
||||
|
||||
/** Ensures that null, empty or whitespace-only IDs are rejected. */
|
||||
@Test public void nonEmptyAbbreviationsAreMandatory()
|
||||
{
|
||||
// A missing or empty level ID is illegal.
|
||||
for (String illegalID : asList(null, "", " ", "\t"))
|
||||
{
|
||||
expectedException(IllegalArgumentException.class, () ->
|
||||
{
|
||||
validator.validate(new ClassificationLevel(illegalID, "value.does.not.matter"));
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=IllegalConfiguration.class)
|
||||
public void systemUnclassifiedAbbreviationIsReserved()
|
||||
{
|
||||
validator.validate(new ClassificationLevel("U", "value.does.not.matter"));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalConfiguration.class)
|
||||
public void longAbbreviationsAreIllegal()
|
||||
{
|
||||
validator.validate(new ClassificationLevel("12345678901", "value.does.not.matter"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This test ensures that validation will catch any and all illegal characters in a
|
||||
* {@link ClassificationLevel#getId() level ID} and report them all.
|
||||
*/
|
||||
@Test public void someCharactersAreBannedInAbbreviations()
|
||||
{
|
||||
for (Character illegalChar : ILLEGAL_ABBREVIATION_CHARS)
|
||||
{
|
||||
IllegalAbbreviationChars e = expectedException(IllegalAbbreviationChars.class, () ->
|
||||
{
|
||||
validator.validate(new ClassificationLevel("Hello" + illegalChar, "value.does.not.matter"));
|
||||
return null;
|
||||
});
|
||||
assertTrue("Exception did not contain helpful example of illegal character",
|
||||
e.getIllegalChars().contains(illegalChar));
|
||||
}
|
||||
|
||||
// We also expect an abbreviation with multiple illegal chars in it to have them all reported in the exception.
|
||||
final List<Character> someIllegalChars = ILLEGAL_ABBREVIATION_CHARS.subList(0, 3);
|
||||
|
||||
IllegalAbbreviationChars e = expectedException(IllegalAbbreviationChars.class, () ->
|
||||
{
|
||||
validator.validate(new ClassificationLevel(someIllegalChars.toString(),
|
||||
"value.does.not.matter"));
|
||||
return null;
|
||||
});
|
||||
|
||||
assertThat(e.getIllegalChars(), allOf(hasItem('"'), hasItem('*'), hasItem('\\')));
|
||||
}
|
||||
}
|
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
|
||||
import static org.hamcrest.CoreMatchers.allOf;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClassificationSchemeEntityValidator}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @author tpage
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClassificationSchemeEntityValidatorUnitTest
|
||||
{
|
||||
private static final String ENTITY_NAME = "ENTITY_NAME";
|
||||
@Mock
|
||||
private EntityFieldsValidator<ClassificationLevel> mockFieldsValidator;
|
||||
/** The class under test. */
|
||||
private ClassificationSchemeEntityValidator<ClassificationLevel> classificationEntitySchemeValidator = new ClassificationSchemeEntityValidator<>(mockFieldsValidator );
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
initMocks(this);
|
||||
}
|
||||
|
||||
@Test(expected=MissingConfiguration.class)
|
||||
public void classificationLevelsAreRequired()
|
||||
{
|
||||
classificationEntitySchemeValidator.validate(Collections.emptyList(), ENTITY_NAME);
|
||||
}
|
||||
|
||||
@Test public void ensureUniquenessOfAbbreviationIds()
|
||||
{
|
||||
IllegalConfiguration e = expectedException(IllegalConfiguration.class, () ->
|
||||
{
|
||||
List<ClassificationLevel> objects = asList(new ClassificationLevel("FOO", "value.does.not.matter"),
|
||||
new ClassificationLevel("BAR", "value.does.not.matter"),
|
||||
new ClassificationLevel("---", "value.does.not.matter"),
|
||||
new ClassificationLevel("BAR", "value.does.not.matter"),
|
||||
new ClassificationLevel("FOO", "value.does.not.matter"));
|
||||
classificationEntitySchemeValidator.validate(objects, ENTITY_NAME);
|
||||
return null;
|
||||
});
|
||||
assertThat("Exception message did not identify the duplicate IDs", e.getMessage(),
|
||||
allOf(containsString("FOO"), containsString("BAR")));
|
||||
}
|
||||
}
|
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ExemptionCategory;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ExemptionCategoryFieldsValidator}.
|
||||
*
|
||||
* @author Oana Nechiforescu
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ExemptionCategoryFieldsValidatorUnitTest
|
||||
{
|
||||
/** Exemption category fields validator */
|
||||
ExemptionCategoryFieldsValidator exemptionCategoryFieldsValidator = new ExemptionCategoryFieldsValidator();
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testMissingExemptionCategoryID()
|
||||
{
|
||||
ExemptionCategory exemptionCategory = new ExemptionCategory("", "label");
|
||||
exemptionCategoryFieldsValidator.validate(exemptionCategory);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testMissingExemptionCategoryLabel()
|
||||
{
|
||||
ExemptionCategory exemptionCategory = new ExemptionCategory("12@1", "");
|
||||
exemptionCategoryFieldsValidator.validate(exemptionCategory);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalConfiguration.class)
|
||||
public void testExemptionIDStartingWithNonAlphanumericCharacter()
|
||||
{
|
||||
ExemptionCategory exemptionCategory = new ExemptionCategory(" 12", "critical");
|
||||
exemptionCategoryFieldsValidator.validate(exemptionCategory);
|
||||
}
|
||||
}
|
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link LengthFieldValidator}.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class LengthFieldValidatorUnitTest
|
||||
{
|
||||
private static final String FIELD_NAME = "FIELD_NAME";
|
||||
/** The class under test. */
|
||||
LengthFieldValidator lengthFieldValidator;
|
||||
|
||||
@Test
|
||||
public void testValidate_passMinOnly()
|
||||
{
|
||||
lengthFieldValidator = new LengthFieldValidator(1);
|
||||
lengthFieldValidator.validate("acceptable", FIELD_NAME);
|
||||
}
|
||||
|
||||
@Test(expected = MissingConfiguration.class)
|
||||
public void testValidate_missingConfiguration()
|
||||
{
|
||||
lengthFieldValidator = new LengthFieldValidator(1);
|
||||
lengthFieldValidator.validate("", FIELD_NAME);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalConfiguration.class)
|
||||
public void testValidate_tooShort()
|
||||
{
|
||||
lengthFieldValidator = new LengthFieldValidator(6);
|
||||
lengthFieldValidator.validate("short", FIELD_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidate_passMinAndMax()
|
||||
{
|
||||
lengthFieldValidator = new LengthFieldValidator(5,5);
|
||||
lengthFieldValidator.validate("super", FIELD_NAME);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalConfiguration.class)
|
||||
public void testValidate_tooLong()
|
||||
{
|
||||
lengthFieldValidator = new LengthFieldValidator(6, 7);
|
||||
lengthFieldValidator.validate("too long", FIELD_NAME);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testValidate_invalidArguments()
|
||||
{
|
||||
lengthFieldValidator = new LengthFieldValidator(3, 1);
|
||||
}
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.classification.validation;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.IllegalConfiguration;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link StartCharacterFieldValidator}.
|
||||
*
|
||||
* @author tpage
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class StartCharacterFieldValidatorUnitTest
|
||||
{
|
||||
private static final String FIELD_NAME = "FIELD_NAME";
|
||||
/** The class under test. */
|
||||
StartCharacterFieldValidator startCharacterFieldValidator = new StartCharacterFieldValidator();
|
||||
|
||||
@Test
|
||||
public void testValidate_pass()
|
||||
{
|
||||
startCharacterFieldValidator.validate("acceptable!", FIELD_NAME);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalConfiguration.class)
|
||||
public void testValidate_fail()
|
||||
{
|
||||
startCharacterFieldValidator.validate("!unacceptable", FIELD_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidate_passIfEmpty()
|
||||
{
|
||||
startCharacterFieldValidator.validate(null, FIELD_NAME);
|
||||
startCharacterFieldValidator.validate("", FIELD_NAME);
|
||||
}
|
||||
}
|
@@ -1,220 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.content;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ContentClassificationService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.content.cleanser.ContentCleanser;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.AlfMock;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest;
|
||||
import org.alfresco.service.cmr.repository.ContentData;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
/**
|
||||
* Content destruction component unit test.
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ContentDestructionComponentUnitTest extends BaseUnitTest
|
||||
{
|
||||
@InjectMocks private ContentDestructionComponent contentDestructionComponent;
|
||||
|
||||
@Mock private ContentClassificationService mockedContentClassificationService;
|
||||
@Mock private ContentCleanser mockedContentCleanser;
|
||||
@Mock private EagerContentStoreCleaner mockedEagerContentStoreCleaner;
|
||||
|
||||
/**
|
||||
* Given a non-sensitive node
|
||||
* When it is deleted
|
||||
* Then nothing happens
|
||||
*/
|
||||
@Test
|
||||
public void deleteNonSensitiveNode()
|
||||
{
|
||||
NodeRef nodeRef = generateCmContent("myContent.txt");
|
||||
|
||||
when(mockedRecordService.isRecord(nodeRef))
|
||||
.thenReturn(false);
|
||||
when(mockedContentClassificationService.isClassified(nodeRef))
|
||||
.thenReturn(false);
|
||||
|
||||
contentDestructionComponent.beforeDeleteNode(nodeRef);
|
||||
|
||||
verifyZeroInteractions(mockedEagerContentStoreCleaner, mockedDictionaryService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a record
|
||||
* And that by default cleansing is off
|
||||
* When it is deleted
|
||||
* Then it is sent for immediate destruction
|
||||
* And not cleansed
|
||||
*/
|
||||
@Test
|
||||
public void deleteRecord()
|
||||
{
|
||||
String contentURL = AlfMock.generateText();
|
||||
NodeRef nodeRef = generateDeletedNodeRef(contentURL);
|
||||
|
||||
when(mockedRecordService.isRecord(nodeRef))
|
||||
.thenReturn(true);
|
||||
when(mockedContentClassificationService.isClassified(nodeRef))
|
||||
.thenReturn(false);
|
||||
|
||||
contentDestructionComponent.beforeDeleteNode(nodeRef);
|
||||
|
||||
verify(mockedEagerContentStoreCleaner).registerOrphanedContentUrl(contentURL, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given classified content
|
||||
* And that by default cleansing is off
|
||||
* When it is deleted
|
||||
* Then it is send for immediate destruction
|
||||
* And not cleansed
|
||||
*/
|
||||
@Test
|
||||
public void deleteClassifiedContent()
|
||||
{
|
||||
String contentURL = AlfMock.generateText();
|
||||
NodeRef nodeRef = generateDeletedNodeRef(contentURL);
|
||||
|
||||
when(mockedRecordService.isRecord(nodeRef))
|
||||
.thenReturn(false);
|
||||
when(mockedContentClassificationService.isClassified(nodeRef))
|
||||
.thenReturn(true);
|
||||
|
||||
contentDestructionComponent.beforeDeleteNode(nodeRef);
|
||||
|
||||
verify(mockedEagerContentStoreCleaner).registerOrphanedContentUrl(contentURL, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that content cleansing is turned on
|
||||
* When a sensitive node is deleted
|
||||
* Then it is scheduled for cleansing before destruction
|
||||
*/
|
||||
@Test
|
||||
public void contentCleansingOn()
|
||||
{
|
||||
String contentURL = AlfMock.generateText();
|
||||
NodeRef nodeRef = generateDeletedNodeRef(contentURL);
|
||||
|
||||
when(mockedRecordService.isRecord(nodeRef))
|
||||
.thenReturn(false);
|
||||
when(mockedContentClassificationService.isClassified(nodeRef))
|
||||
.thenReturn(true);
|
||||
|
||||
contentDestructionComponent.setCleansingEnabled(true);
|
||||
contentDestructionComponent.beforeDeleteNode(nodeRef);
|
||||
|
||||
verify(mockedEagerContentStoreCleaner).registerOrphanedContentUrlForCleansing(contentURL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that content cleansing is turned off
|
||||
* When a sensitive node is deleted
|
||||
* Then it is not scheduled for cleansing before destruction
|
||||
*/
|
||||
@Test
|
||||
public void contentCleansingOff()
|
||||
{
|
||||
String contentURL = AlfMock.generateText();
|
||||
NodeRef nodeRef = generateDeletedNodeRef(contentURL);
|
||||
|
||||
when(mockedRecordService.isRecord(nodeRef))
|
||||
.thenReturn(false);
|
||||
when(mockedContentClassificationService.isClassified(nodeRef))
|
||||
.thenReturn(true);
|
||||
|
||||
contentDestructionComponent.setCleansingEnabled(false);
|
||||
contentDestructionComponent.beforeDeleteNode(nodeRef);
|
||||
|
||||
verify(mockedEagerContentStoreCleaner).registerOrphanedContentUrl(contentURL, true);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that a sensitive node has more than one content property
|
||||
* When is it deleted
|
||||
* Then all the content properties are scheduled for destruction
|
||||
*/
|
||||
@Test
|
||||
public void moreThanOneContentProperty()
|
||||
{
|
||||
String contentURL = AlfMock.generateText();
|
||||
NodeRef nodeRef = generateDeletedNodeRef(contentURL, 2);
|
||||
|
||||
when(mockedRecordService.isRecord(nodeRef))
|
||||
.thenReturn(false);
|
||||
when(mockedContentClassificationService.isClassified(nodeRef))
|
||||
.thenReturn(true);
|
||||
|
||||
contentDestructionComponent.setCleansingEnabled(true);
|
||||
contentDestructionComponent.beforeDeleteNode(nodeRef);
|
||||
|
||||
verify(mockedEagerContentStoreCleaner, times(2)).registerOrphanedContentUrlForCleansing(contentURL);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method that creates deleted node reference
|
||||
*/
|
||||
private NodeRef generateDeletedNodeRef(String contentURL)
|
||||
{
|
||||
return generateDeletedNodeRef(contentURL, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method that creates deleted node reference
|
||||
*/
|
||||
private NodeRef generateDeletedNodeRef(String contentURL, int contentPropertiesCount)
|
||||
{
|
||||
NodeRef nodeRef = generateCmContent("myContent.txt");
|
||||
|
||||
ContentData mockedContentData = mock(ContentData.class);
|
||||
when(mockedContentData.getContentUrl())
|
||||
.thenReturn(contentURL);
|
||||
|
||||
Map<QName, Serializable> propertiesMap = new HashMap<QName, Serializable>(contentPropertiesCount);
|
||||
for(int i = 0; i < contentPropertiesCount; i++)
|
||||
{
|
||||
propertiesMap.put(AlfMock.generateQName(), mockedContentData);
|
||||
}
|
||||
|
||||
when(mockedNodeService.getProperties(nodeRef))
|
||||
.thenReturn(propertiesMap);
|
||||
|
||||
return nodeRef;
|
||||
}
|
||||
}
|
@@ -1,154 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.model.clf.aspect;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
import org.alfresco.model.RenditionModel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationException.MissingDowngradeInstructions;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.model.ClassifiedContentModel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferralAdminService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.util.CoreServicesExtras;
|
||||
import org.alfresco.service.cmr.rendition.RenditionService;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link ClassifiedAspect}.
|
||||
*
|
||||
* @author Tom Page
|
||||
* @author Neil Mc Erlean
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClassifiedAspectUnitTest implements ClassifiedContentModel
|
||||
{
|
||||
private static final NodeRef NODE_REF = new NodeRef("node://Ref/");
|
||||
private static final NodeRef RENDITION_1 = new NodeRef("node://rendition1/");
|
||||
private static final NodeRef RENDITION_2 = new NodeRef("node://rendition2/");
|
||||
|
||||
@InjectMocks ClassifiedAspect classifiedAspect;
|
||||
@Mock ClassificationSchemeService mockClassificationSchemeService;
|
||||
@Mock CoreServicesExtras mockCoreServicesExtras;
|
||||
@Mock NodeService mockNodeService;
|
||||
@Mock RenditionService mockRenditionService;
|
||||
@Mock ReferralAdminService mockReferralAdminService;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
initMocks(this);
|
||||
}
|
||||
|
||||
/** Check that providing an event and instructions is valid. */
|
||||
@Test
|
||||
public void testCheckConsistencyOfProperties_success()
|
||||
{
|
||||
when(mockNodeService.hasAspect(NODE_REF, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_DATE)).thenReturn(null);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_EVENT)).thenReturn("Event");
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_INSTRUCTIONS)).thenReturn("Instructions");
|
||||
|
||||
classifiedAspect.checkConsistencyOfProperties(NODE_REF);
|
||||
}
|
||||
|
||||
/** Check that omitting all downgrade fields is valid. */
|
||||
@Test
|
||||
public void testCheckConsistencyOfProperties_notSpecified()
|
||||
{
|
||||
when(mockNodeService.hasAspect(NODE_REF, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_DATE)).thenReturn(null);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_EVENT)).thenReturn(null);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_INSTRUCTIONS)).thenReturn(null);
|
||||
|
||||
classifiedAspect.checkConsistencyOfProperties(NODE_REF);
|
||||
}
|
||||
|
||||
/** Check that a date without instructions throws an exception. */
|
||||
@Test(expected = MissingDowngradeInstructions.class)
|
||||
public void testCheckConsistencyOfProperties_dateMissingInstructions()
|
||||
{
|
||||
when(mockNodeService.hasAspect(NODE_REF, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_DATE)).thenReturn(new Date(123));
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_EVENT)).thenReturn(null);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_INSTRUCTIONS)).thenReturn(null);
|
||||
|
||||
classifiedAspect.checkConsistencyOfProperties(NODE_REF);
|
||||
}
|
||||
|
||||
/** Check that an event without instructions throws an exception. */
|
||||
@Test(expected = MissingDowngradeInstructions.class)
|
||||
public void testCheckConsistencyOfProperties_eventMissingInstructions()
|
||||
{
|
||||
when(mockNodeService.hasAspect(NODE_REF, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_DATE)).thenReturn(null);
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_EVENT)).thenReturn("Event");
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_INSTRUCTIONS)).thenReturn(null);
|
||||
|
||||
classifiedAspect.checkConsistencyOfProperties(NODE_REF);
|
||||
}
|
||||
|
||||
/** Check that blank instructions are treated in the same way as null instructions. */
|
||||
@Test(expected = MissingDowngradeInstructions.class)
|
||||
public void testCheckConsistencyOfProperties_emptyStringsSupplied()
|
||||
{
|
||||
for (NodeRef n : asList(NODE_REF, RENDITION_1, RENDITION_2))
|
||||
{
|
||||
when(mockNodeService.hasAspect(n, ASPECT_CLASSIFIED)).thenReturn(true);
|
||||
}
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_DATE)).thenReturn("");
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_EVENT)).thenReturn("Event");
|
||||
when(mockNodeService.getProperty(NODE_REF, PROP_DOWNGRADE_INSTRUCTIONS)).thenReturn("");
|
||||
|
||||
classifiedAspect.checkConsistencyOfProperties(NODE_REF);
|
||||
}
|
||||
|
||||
@Test public void newlyClassifiedNodeShouldLinkItsMetadataToAllRenditions()
|
||||
{
|
||||
when(mockRenditionService.getRenditions(NODE_REF))
|
||||
.thenReturn(asList(rendition(NODE_REF, RENDITION_1), rendition(NODE_REF, RENDITION_2)));
|
||||
for (final NodeRef rendition : asList(RENDITION_1, RENDITION_2))
|
||||
{
|
||||
when(mockRenditionService.getSourceNode(rendition)).thenReturn(rendition(NODE_REF, rendition));
|
||||
}
|
||||
|
||||
classifiedAspect.onAddAspect(NODE_REF, ASPECT_CLASSIFIED);
|
||||
|
||||
for (NodeRef rendition : asList(RENDITION_1, RENDITION_2))
|
||||
{
|
||||
verify(mockReferralAdminService).attachReferrer(rendition, NODE_REF, ASPECT_CLASSIFIED);
|
||||
}
|
||||
}
|
||||
|
||||
/** Creates a test Rendition ChildAssociationRef. */
|
||||
private ChildAssociationRef rendition(NodeRef source, NodeRef rendition)
|
||||
{
|
||||
return new ChildAssociationRef(RenditionModel.ASSOC_RENDITION, source, RenditionModel.ASSOC_RENDITION, rendition);
|
||||
}
|
||||
}
|
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.referredmetadata;
|
||||
|
||||
import static java.util.Collections.emptySet;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataException.InvalidMetadataReferral;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asListFrom;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSet;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.alfresco.service.cmr.dictionary.AspectDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MetadataReferral}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class MetadataReferralUnitTest
|
||||
{
|
||||
@Mock DictionaryService mockDictionaryService;
|
||||
@Mock NodeService mockNodeService;
|
||||
|
||||
private final ReferralAdminServiceImpl referralAdminService = new ReferralAdminServiceImpl();
|
||||
|
||||
private final QName aspect1 = QName.createQName("test", "aspect1");
|
||||
private final QName aspect2 = QName.createQName("test", "aspect2");
|
||||
private final QName assoc1 = QName.createQName("test", "assoc1");
|
||||
|
||||
@Before public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
referralAdminService.setNodeService(mockNodeService);
|
||||
}
|
||||
|
||||
@Test public void nullOrEmptyReferralsAreForbidden()
|
||||
{
|
||||
asListFrom(() -> new MetadataReferral(),
|
||||
() -> {
|
||||
MetadataReferral mr = new MetadataReferral();
|
||||
mr.setAssocType(assoc1);
|
||||
mr.setAspects(null);
|
||||
mr.setDictionaryService(mockDictionaryService);
|
||||
return mr;
|
||||
},
|
||||
() -> {
|
||||
MetadataReferral mr = new MetadataReferral();
|
||||
mr.setAssocType(assoc1);
|
||||
mr.setAspects(emptySet());
|
||||
mr.setDictionaryService(mockDictionaryService);
|
||||
return mr;
|
||||
},
|
||||
() -> {
|
||||
MetadataReferral mr = new MetadataReferral();
|
||||
mr.setAssocType(null);
|
||||
mr.setAspects(asSet(aspect1, aspect2));
|
||||
mr.setDictionaryService(mockDictionaryService);
|
||||
return mr;
|
||||
})
|
||||
.forEach(mr -> expectedException(InvalidMetadataReferral.class, () -> {
|
||||
mr.validateAndRegister();
|
||||
return null;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@Test(expected=InvalidMetadataReferral.class)
|
||||
public void referralMustHaveAssocThatExists()
|
||||
{
|
||||
when(mockDictionaryService.getAssociation(assoc1)).thenReturn(null);
|
||||
when(mockDictionaryService.getAspect(aspect1)).thenReturn(mock(AspectDefinition.class));
|
||||
|
||||
MetadataReferral mr = new MetadataReferral();
|
||||
mr.setAssocType(assoc1);
|
||||
mr.setAspects(asSet(aspect1));
|
||||
mr.setDictionaryService(mockDictionaryService);
|
||||
mr.validateAndRegister();
|
||||
}
|
||||
|
||||
@Test(expected=InvalidMetadataReferral.class)
|
||||
public void referralMustHaveAspectsAllOfWhichExist()
|
||||
{
|
||||
when(mockDictionaryService.getAssociation(assoc1)).thenReturn(mock(AssociationDefinition.class));
|
||||
when(mockDictionaryService.getAspect(aspect1)).thenReturn(mock(AspectDefinition.class));
|
||||
when(mockDictionaryService.getAspect(aspect2)).thenReturn(null);
|
||||
|
||||
MetadataReferral mr = new MetadataReferral();
|
||||
mr.setAssocType(assoc1);
|
||||
mr.setAspects(asSet(aspect1, aspect2));
|
||||
mr.setDictionaryService(mockDictionaryService);
|
||||
mr.validateAndRegister();
|
||||
}
|
||||
}
|
@@ -1,142 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.referredmetadata;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataException.ChainedMetadataReferralUnsupported;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSet;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.repository.AssociationRef;
|
||||
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.namespace.QName;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ReferralAdminServiceImpl}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ReferralAdminServiceImplUnitTest
|
||||
{
|
||||
@InjectMocks private final ReferralAdminServiceImpl referralAdminService = new ReferralAdminServiceImpl();
|
||||
|
||||
@Mock DictionaryService mockDictionaryService;
|
||||
@Mock NodeService mockNodeService;
|
||||
@Mock ReferralRegistry mockRegistry;
|
||||
@Mock ReferredMetadataServiceImpl mockReferredMetadataService;
|
||||
|
||||
private final NodeRef node1 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "node1");
|
||||
private final NodeRef node2 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "node2");
|
||||
private final NodeRef node3 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "node3");
|
||||
|
||||
private final QName assoc1 = QName.createQName("test", "assoc1");
|
||||
private final QName aspect1 = QName.createQName("test", "aspect1");
|
||||
private final QName aspect2 = QName.createQName("test", "aspect2");
|
||||
|
||||
private final QName assoc2 = QName.createQName("test", "assoc2");
|
||||
private final QName aspect3 = QName.createQName("test", "aspect3");
|
||||
|
||||
private final MetadataReferral referral1 = new MetadataReferral()
|
||||
{{
|
||||
this.setAssocType(assoc1);
|
||||
this.setAspects(asSet(aspect1, aspect2));
|
||||
}};
|
||||
private final MetadataReferral referral2 = new MetadataReferral()
|
||||
{{
|
||||
this.setAssocType(assoc2);
|
||||
this.setAspects(asSet(aspect3));
|
||||
}};
|
||||
|
||||
@Before public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
when(mockRegistry.getMetadataReferrals()).thenReturn(asSet(referral1, referral2));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void attachingReferrerWithNoAspectConfiguredShouldFail()
|
||||
{
|
||||
referralAdminService.attachReferrer(node2, node1, aspect1);
|
||||
}
|
||||
|
||||
@Test public void attachDetach()
|
||||
{
|
||||
when(mockRegistry.getReferralForAspect(aspect1)).thenReturn(referral1);
|
||||
|
||||
// attach
|
||||
MetadataReferral d = attachReferrer(node1, node2, aspect1);
|
||||
|
||||
// validate
|
||||
assertEquals(assoc1, d.getAssocType());
|
||||
assertEquals(asSet(aspect1, aspect2), d.getAspects());
|
||||
assertTrue(mockReferredMetadataService.isReferringMetadata(node1, aspect1));
|
||||
assertFalse(mockReferredMetadataService.isReferringMetadata(node1, aspect3));
|
||||
|
||||
// detach
|
||||
assertEquals(d, referralAdminService.detachReferrer(node1, aspect1));
|
||||
}
|
||||
|
||||
private MetadataReferral attachReferrer(NodeRef referrer, NodeRef referent, QName aspectName)
|
||||
{
|
||||
MetadataReferral mr = referralAdminService.attachReferrer(referrer, referent, aspectName);
|
||||
final QName assocType = mr.getAssocType();
|
||||
when(mockNodeService.getSourceAssocs(referent, assocType)).thenReturn(asList(new AssociationRef(referrer, assocType, referent)));
|
||||
when(mockNodeService.getTargetAssocs(referrer, assocType)).thenReturn(asList(new AssociationRef(referrer, assocType, referent)));
|
||||
for (QName aspect : mr.getAspects())
|
||||
{
|
||||
when(mockReferredMetadataService.isReferringMetadata(referrer, aspect)).thenReturn(true);
|
||||
}
|
||||
return mr;
|
||||
}
|
||||
|
||||
@Test public void chainsOfDelegationShouldBePrevented()
|
||||
{
|
||||
when(mockRegistry.getReferralForAspect(aspect1)).thenReturn(referral1);
|
||||
|
||||
// The node already has a delegation in place: node1 -> node2. We're trying to add to the
|
||||
// end of the chain: node2 -> node3
|
||||
when(mockNodeService.getSourceAssocs(node2, assoc1)).thenReturn(asList(new AssociationRef(node1, assoc1, node2)));
|
||||
when(mockNodeService.getTargetAssocs(node1, assoc1)).thenReturn(asList(new AssociationRef(node1, assoc1, node2)));
|
||||
|
||||
expectedException(ChainedMetadataReferralUnsupported.class, () -> {
|
||||
referralAdminService.attachReferrer(node2, node3, aspect1);
|
||||
return null;
|
||||
});
|
||||
|
||||
// Now try to add to the start of the chain: node3 -> node1
|
||||
expectedException(ChainedMetadataReferralUnsupported.class, () -> {
|
||||
referralAdminService.attachReferrer(node3, node1, aspect1);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
@@ -1,196 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.referredmetadata;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.FPUtils.asSet;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataException.MetadataReferralNotFound;
|
||||
import org.alfresco.module.org_alfresco_module_rm.referredmetadata.ReferredMetadataException.ReferentNodeNotFound;
|
||||
import org.alfresco.service.cmr.dictionary.ClassDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
|
||||
import org.alfresco.service.cmr.repository.AssociationRef;
|
||||
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.namespace.QName;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ReferredMetadataServiceImpl}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ReferredMetadataServiceImplUnitTest
|
||||
{
|
||||
@InjectMocks private final ReferredMetadataServiceImpl referredMetadataService = new ReferredMetadataServiceImpl();
|
||||
|
||||
@Mock DictionaryService mockDictionaryService;
|
||||
@Mock NodeService mockNodeService;
|
||||
@Mock ReferralAdminServiceImpl mockReferralAdminService;
|
||||
@Mock ReferralRegistry mockReferralRegistry;
|
||||
|
||||
/** This node has a referent node. */
|
||||
private final NodeRef referringNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "referringNode");
|
||||
/** This is the referent for {@link #referringNode}. */
|
||||
private final NodeRef referentNode = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "referentNode");
|
||||
/** This node has no referent node. */
|
||||
private final NodeRef nodeWithoutReferent = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "nodeWithoutReferent");
|
||||
|
||||
/** The type of the peer association that links the referringNode to its source. */
|
||||
private final QName referralAssocType = QName.createQName("test", "referralAssocType");
|
||||
/** The instance of the association between {@link #referringNode} and {@link #referentNode}. */
|
||||
private final AssociationRef attachedReferralAssocRef = new AssociationRef(referringNode, referralAssocType, referentNode);
|
||||
|
||||
/** Name of an aspect that has been referred. */
|
||||
private final QName referredAspect1 = QName.createQName("test", "referredAspect1");
|
||||
/** Name of an aspect that has been referred. */
|
||||
private final QName referredAspect2 = QName.createQName("test", "referredAspect2");
|
||||
/** Name of a content class (a type in this case) that has not been referred.
|
||||
* N.B. Types can't be referred currently. */
|
||||
private final QName unreferredType = QName.createQName("test", "unreferredType");
|
||||
|
||||
private final QName referredProp = QName.createQName("test", "referredProp");
|
||||
private final Serializable referredPropValue = "hello";
|
||||
private final QName unreferredProp = QName.createQName("test", "unreferredProp");
|
||||
|
||||
private final MetadataReferral referral = new MetadataReferral()
|
||||
{{
|
||||
this.setAssocType(referralAssocType);
|
||||
this.setAspects(asSet(referredAspect1, referredAspect2));
|
||||
}};
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@Before public void setUp()
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
final PropertyDefinition aspectProp = mock(PropertyDefinition.class);
|
||||
final ClassDefinition aspectDefn = mock(ClassDefinition.class);
|
||||
when(aspectDefn.getName()).thenReturn(referredAspect1);
|
||||
when(aspectProp.getContainerClass()).thenReturn(aspectDefn);
|
||||
when(aspectDefn.isAspect()).thenReturn(true);
|
||||
|
||||
final PropertyDefinition typeProp = mock(PropertyDefinition.class);
|
||||
final ClassDefinition typeDefn = mock(ClassDefinition.class);
|
||||
when(typeDefn.getName()).thenReturn(unreferredType);
|
||||
when(typeProp.getContainerClass()).thenReturn(typeDefn);
|
||||
when(typeDefn.isAspect()).thenReturn(false);
|
||||
|
||||
when(mockDictionaryService.getProperty(referredProp)).thenReturn(aspectProp);
|
||||
|
||||
when(mockReferralAdminService.getAttachedReferralsFrom(referringNode)).thenReturn(asSet(referral));
|
||||
for (QName referredAspect : asSet(referredAspect1, referredAspect2))
|
||||
{
|
||||
when(mockReferralRegistry.getReferralForAspect(referredAspect)).thenReturn(referral);
|
||||
when(mockNodeService.hasAspect(referentNode, referredAspect)).thenReturn(true);
|
||||
}
|
||||
when(mockNodeService.getSourceAssocs(referentNode, referralAssocType)).thenReturn(asList(attachedReferralAssocRef));
|
||||
when(mockNodeService.getTargetAssocs(referringNode, referralAssocType)).thenReturn(asList(attachedReferralAssocRef));
|
||||
when(mockNodeService.exists(any(NodeRef.class))).thenReturn(true);
|
||||
when(mockNodeService.getProperties(referentNode))
|
||||
.thenReturn(new HashMap<QName, Serializable>()
|
||||
{{
|
||||
this.put(referredProp, referredPropValue);
|
||||
}});
|
||||
}
|
||||
|
||||
@Test public void isReferringMetadata()
|
||||
{
|
||||
assertTrue(referredMetadataService.isReferringMetadata(referringNode, referredAspect1));
|
||||
expectedException(MetadataReferralNotFound.class,
|
||||
() -> referredMetadataService.isReferringMetadata(nodeWithoutReferent, unreferredType));
|
||||
assertFalse(referredMetadataService.isReferringMetadata(nodeWithoutReferent, referredAspect1));
|
||||
}
|
||||
|
||||
@Test public void getReferentNode()
|
||||
{
|
||||
assertEquals(referentNode, referredMetadataService.getReferentNode(referringNode, referredAspect1));
|
||||
expectedException(MetadataReferralNotFound.class,
|
||||
() -> {
|
||||
referredMetadataService.getReferentNode(referringNode, unreferredType);
|
||||
return null;
|
||||
});
|
||||
assertNull(referredMetadataService.getReferentNode(nodeWithoutReferent, referredAspect1));
|
||||
}
|
||||
|
||||
@Test public void getReferredProperties()
|
||||
{
|
||||
final Map<QName, Serializable> expectedProps = new HashMap<>();
|
||||
expectedProps.put(referredProp, referredPropValue);
|
||||
|
||||
assertEquals(expectedProps, referredMetadataService.getReferredProperties(referringNode, referredAspect1));
|
||||
|
||||
expectedException(MetadataReferralNotFound.class,
|
||||
() -> referredMetadataService.getReferredProperties(referringNode, unreferredType));
|
||||
|
||||
expectedException(ReferentNodeNotFound.class,
|
||||
() -> referredMetadataService.getReferredProperties(nodeWithoutReferent, referredAspect1));
|
||||
}
|
||||
|
||||
@Test public void getReferredProperty()
|
||||
{
|
||||
assertEquals(referredPropValue, referredMetadataService.getReferredProperty(referringNode, referredProp));
|
||||
|
||||
expectedException(IllegalArgumentException.class,
|
||||
() -> referredMetadataService.getReferredProperty(referringNode, unreferredProp));
|
||||
|
||||
expectedException(MetadataReferralNotFound.class,
|
||||
() -> referredMetadataService.getReferredProperties(nodeWithoutReferent, referredProp));
|
||||
}
|
||||
|
||||
@Test public void hasReferredAspect()
|
||||
{
|
||||
assertTrue(referredMetadataService.hasReferredAspect(referringNode, referredAspect1));
|
||||
|
||||
expectedException(MetadataReferralNotFound.class,
|
||||
() -> referredMetadataService.hasReferredAspect(referringNode, unreferredType));
|
||||
|
||||
expectedException(ReferentNodeNotFound.class,
|
||||
() -> referredMetadataService.hasReferredAspect(nodeWithoutReferent, referredAspect1));
|
||||
}
|
||||
|
||||
@Test public void getAttachedReferrals()
|
||||
{
|
||||
final Map<MetadataReferral, NodeRef> expectedReferrals = new HashMap<>();
|
||||
expectedReferrals.put(referral, referentNode);
|
||||
|
||||
assertEquals(expectedReferrals, referredMetadataService.getAttachedReferrals(referringNode));
|
||||
assertEquals(emptyMap(), referredMetadataService.getAttachedReferrals(nodeWithoutReferent));
|
||||
}
|
||||
}
|
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest;
|
||||
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 com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* Tests for the get classification levels API.
|
||||
*
|
||||
* @author Tuna Aksoy
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClassificationLevelsGetUnitTest extends BaseWebScriptUnitTest
|
||||
{
|
||||
/** Classpath location of ftl template for web script */
|
||||
private static final String WEBSCRIPT_TEMPLATE = WEBSCRIPT_ROOT_RM + "classification/classificationlevels.get.json.ftl";
|
||||
|
||||
/** ClassificationLevelsGet webscript instance */
|
||||
private @Spy @InjectMocks ClassificationLevelsGet webScript;
|
||||
private @Mock ClassificationSchemeService mockClassificationSchemeService;
|
||||
|
||||
private List<ClassificationLevel> classificationLevels;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected DeclarativeWebScript getWebScript()
|
||||
{
|
||||
return webScript;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected String getWebScriptTemplate()
|
||||
{
|
||||
return WEBSCRIPT_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the successful retrieval of two classification levels.
|
||||
*/
|
||||
@Test
|
||||
public void getClassificationLevels() throws Exception
|
||||
{
|
||||
// Create test data.
|
||||
classificationLevels = Arrays.asList(
|
||||
new ClassificationLevel("id1", "labelKey1"),
|
||||
new ClassificationLevel("id2", "labelKey2"));
|
||||
|
||||
// setup interactions
|
||||
doReturn(classificationLevels).when(mockClassificationSchemeService).getClassificationLevels();
|
||||
|
||||
// execute web script
|
||||
JSONObject json = executeJSONWebScript(new HashMap<String, String>());
|
||||
assertNotNull(json);
|
||||
String actualJSONString = json.toString();
|
||||
|
||||
// check the JSON result using Jackson to allow easy equality testing.
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String expectedJSONString = "{\"data\":{\"items\":[{\"displayLabel\":\"labelKey1\",\"id\":\"id1\"},{\"displayLabel\":\"labelKey2\",\"id\":\"id2\"}]}}";
|
||||
JsonNode expected = mapper.readTree(expectedJSONString);
|
||||
assertEquals(expected, mapper.readTree(actualJSONString));
|
||||
}
|
||||
}
|
@@ -1,266 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.classification;
|
||||
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentBase.CLASSIFICATION_AGENCY;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentBase.CLASSIFICATION_LEVEL_ID;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentBase.CLASSIFICATION_REASONS;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentBase.CLASSIFIED_BY;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentBase.LAST_RECLASSIFY_BY;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentBase.LAST_RECLASSIFY_REASON;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentBase.RECLASSIFY_BY;
|
||||
import static org.alfresco.module.org_alfresco_module_rm.script.classification.ClassifyContentBase.RECLASSIFY_REASON;
|
||||
import static org.alfresco.util.WebScriptUtils.getStringValueFromJSONObject;
|
||||
import static org.alfresco.util.WebScriptUtils.is4xxError;
|
||||
import static org.alfresco.util.WebScriptUtils.putValuetoJSONObject;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationAspectProperties;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevelManager;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationReasonManager;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ContentClassificationService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.extensions.webscripts.DeclarativeWebScript;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
|
||||
/**
|
||||
* Classify content REST API POST implementation unit test.
|
||||
*
|
||||
* @author Tuna Aksoy
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClassifyContentPostUnitTest extends BaseWebScriptUnitTest
|
||||
{
|
||||
/** Classpath location of ftl template for web script */
|
||||
private static final String WEBSCRIPT_TEMPLATE = WEBSCRIPT_ROOT_RM + "classification/classifycontent.post.json.ftl";
|
||||
|
||||
/** Constants */
|
||||
private static final String STORE_TYPE = "store_type";
|
||||
private static final String STORE_ID = "store_id";
|
||||
private static final String ID = "id";
|
||||
private static final String SUCCESS = "success";
|
||||
private static final String LEVEL_ID = "aLevelId";
|
||||
private static final String BY = "bySomeone";
|
||||
private static final String AGENCY = "anAgency";
|
||||
private static final String REASON1_ID = "reason1Id";
|
||||
private static final String REASON2_ID = "reason2Id";
|
||||
|
||||
/** ClassifyContentPost webscript instance */
|
||||
private @Spy @InjectMocks ClassifyContentPost webScript;
|
||||
|
||||
/** Mocked content classification service */
|
||||
private @Mock ContentClassificationService mockedContentClassificationService;
|
||||
/** Mocked classification level manager */
|
||||
private @Mock ClassificationLevelManager mockedClassificationLevelManager;
|
||||
/** Mocked classification reason manager */
|
||||
private @Mock ClassificationReasonManager mockedClassificationReasonManager;
|
||||
|
||||
/** Captor for the classification aspect properties. */
|
||||
private @Captor ArgumentCaptor<ClassificationAspectProperties> propertiesDTOCaptor;
|
||||
/**
|
||||
* @see org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest#getWebScript()
|
||||
*/
|
||||
@Override
|
||||
protected DeclarativeWebScript getWebScript()
|
||||
{
|
||||
return webScript;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest#getWebScriptTemplate()
|
||||
*/
|
||||
@Override
|
||||
protected String getWebScriptTemplate()
|
||||
{
|
||||
return WEBSCRIPT_TEMPLATE;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassifyContent() throws Exception
|
||||
{
|
||||
// Setup web script parameters
|
||||
Map<String, String> parameters = buildClassifyRecordParams();
|
||||
|
||||
// Build JSON to send to server
|
||||
String content = buildContent().toString();
|
||||
|
||||
// Execute web script
|
||||
JSONObject json = executeJSONWebScript(parameters, content);
|
||||
assertNotNull(json);
|
||||
assertTrue(json.has(SUCCESS));
|
||||
assertEquals(getStringValueFromJSONObject(json, SUCCESS), Boolean.TRUE.toString());
|
||||
|
||||
// Verify that the classify content method was called
|
||||
verify(mockedContentClassificationService, times(1)).classifyContent(propertiesDTOCaptor.capture(), eq(record));
|
||||
|
||||
// Check the values in the DTO.
|
||||
ClassificationAspectProperties propertiesDTO = propertiesDTOCaptor.getValue();
|
||||
assertEquals(LEVEL_ID, propertiesDTO.getClassificationLevelId());
|
||||
assertEquals(BY, propertiesDTO.getClassifiedBy());
|
||||
assertEquals(AGENCY, propertiesDTO.getClassificationAgency());
|
||||
assertEquals(Sets.newHashSet(REASON1_ID, REASON2_ID), propertiesDTO.getClassificationReasonIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void classifyingWithBlankClassifiedByShouldReturn4xxResponse() throws Exception
|
||||
{
|
||||
// Setup web script parameters
|
||||
Map<String, String> parameters = buildClassifyRecordParams();
|
||||
|
||||
final String whitespace = " \t ";
|
||||
|
||||
JSONObject jsonObj = buildContent();
|
||||
putValuetoJSONObject(jsonObj, CLASSIFIED_BY, whitespace);
|
||||
String json = jsonObj.toString();
|
||||
|
||||
// Execute web script
|
||||
boolean exceptionThrown = false;
|
||||
try
|
||||
{
|
||||
executeJSONWebScript(parameters, json);
|
||||
}
|
||||
catch (WebScriptException expected)
|
||||
{
|
||||
exceptionThrown = true;
|
||||
assertTrue("HTTP rsp should have been a 400 error. Was " + expected.getStatus(),
|
||||
is4xxError(expected));
|
||||
}
|
||||
|
||||
assertTrue("Expected exception was not thrown", exceptionThrown);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that no error is thrown if both the "Reclassify" and "Last Reclassify" sets of fields are blank (as would
|
||||
* be the case for the initial classification). Check that null is used for the last reclassification data.
|
||||
*/
|
||||
@Test
|
||||
public void testClassifyContent_lastReclassifyNullForFirstClassification() throws Exception
|
||||
{
|
||||
Map<String, String> parameters = buildClassifyRecordParams();
|
||||
|
||||
// Build JSON to send to server with no previous classification data.
|
||||
JSONObject jsonContent = buildContent();
|
||||
|
||||
// Execute web script
|
||||
executeJSONWebScript(parameters, jsonContent.toString());
|
||||
|
||||
// Check the last classification event data.
|
||||
verify(mockedContentClassificationService).classifyContent(propertiesDTOCaptor.capture(), eq(record));
|
||||
ClassificationAspectProperties propertiesDTO = propertiesDTOCaptor.getValue();
|
||||
assertNull("Expected last reclassfied by to be null.", propertiesDTO.getLastReclassifyBy());
|
||||
assertNull("Expected last reclassfied reason to be null.", propertiesDTO.getLastReclassifyReason());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that if the "Last Reclassify" fields are set (and the "Reclassify" fields aren't) then they are used for the
|
||||
* last reclassification data. This simulates editing the classification data (without changing the level).
|
||||
*/
|
||||
@Test
|
||||
public void testClassifyContent_lastReclassifyWhenEditingClassification() throws Exception
|
||||
{
|
||||
Map<String, String> parameters = buildClassifyRecordParams();
|
||||
JSONObject jsonContent = buildContent();
|
||||
|
||||
// Set the last reclassification data (as would be set if changing anything other than the level).
|
||||
putValuetoJSONObject(jsonContent, LAST_RECLASSIFY_BY, "user 1");
|
||||
putValuetoJSONObject(jsonContent, LAST_RECLASSIFY_REASON, "reason 1");
|
||||
|
||||
// Execute web script
|
||||
executeJSONWebScript(parameters, jsonContent.toString());
|
||||
|
||||
// Check the "Last Reclassify" values are used.
|
||||
verify(mockedContentClassificationService).classifyContent(propertiesDTOCaptor.capture(), eq(record));
|
||||
ClassificationAspectProperties propertiesDTO = propertiesDTOCaptor.getValue();
|
||||
assertEquals("user 1", propertiesDTO.getLastReclassifyBy());
|
||||
assertEquals("reason 1", propertiesDTO.getLastReclassifyReason());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that if the "Reclassify" fields are set then they are used for the last reclassification data, even if the
|
||||
* "Last Reclassify" fields are set too. This simulates changing the classification level.
|
||||
*/
|
||||
@Test
|
||||
public void testClassifyContent_lastReclassifyForReclassification() throws Exception
|
||||
{
|
||||
Map<String, String> parameters = buildClassifyRecordParams();
|
||||
JSONObject jsonContent = buildContent();
|
||||
|
||||
// Set the reclassification data (as if we've classified once and are reclassifying).
|
||||
putValuetoJSONObject(jsonContent, LAST_RECLASSIFY_BY, "user 1");
|
||||
putValuetoJSONObject(jsonContent, LAST_RECLASSIFY_REASON, "reason 1");
|
||||
putValuetoJSONObject(jsonContent, RECLASSIFY_BY, "user 2");
|
||||
putValuetoJSONObject(jsonContent, RECLASSIFY_REASON, "reason 2");
|
||||
|
||||
executeJSONWebScript(parameters, jsonContent.toString());
|
||||
|
||||
verify(mockedContentClassificationService).classifyContent(propertiesDTOCaptor.capture(), eq(record));
|
||||
ClassificationAspectProperties propertiesDTO = propertiesDTOCaptor.getValue();
|
||||
assertEquals("user 2", propertiesDTO.getLastReclassifyBy());
|
||||
assertEquals("reason 2", propertiesDTO.getLastReclassifyReason());
|
||||
}
|
||||
|
||||
/** Build the parameters map that is used when classifying a record. */
|
||||
private Map<String, String> buildClassifyRecordParams()
|
||||
{
|
||||
return buildParameters
|
||||
(
|
||||
STORE_TYPE, record.getStoreRef().getProtocol(),
|
||||
STORE_ID, record.getStoreRef().getIdentifier(),
|
||||
ID, record.getId()
|
||||
);
|
||||
}
|
||||
|
||||
/** Helper method to build the request content. */
|
||||
private JSONObject buildContent()
|
||||
{
|
||||
JSONObject content = new JSONObject();
|
||||
putValuetoJSONObject(content, CLASSIFICATION_LEVEL_ID, LEVEL_ID);
|
||||
putValuetoJSONObject(content, CLASSIFIED_BY, BY);
|
||||
putValuetoJSONObject(content, CLASSIFICATION_AGENCY, AGENCY);
|
||||
|
||||
JSONObject classificationReason1 = new JSONObject();
|
||||
putValuetoJSONObject(classificationReason1, ID, REASON1_ID);
|
||||
JSONObject classificationReason2 = new JSONObject();
|
||||
putValuetoJSONObject(classificationReason2, ID, REASON2_ID);
|
||||
|
||||
JSONArray classificationReasons = new JSONArray();
|
||||
classificationReasons.put(classificationReason1);
|
||||
classificationReasons.put(classificationReason2);
|
||||
putValuetoJSONObject(content, CLASSIFICATION_REASONS, classificationReasons);
|
||||
|
||||
return content;
|
||||
}
|
||||
}
|
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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.ClearanceLevel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.SecurityClearanceService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest;
|
||||
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 java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* Tests for the get clearance levels API.
|
||||
*
|
||||
* @author David Webster
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class ClearanceLevelsGetUnitTest extends BaseWebScriptUnitTest
|
||||
{
|
||||
/** Classpath location of ftl template for web script */
|
||||
private static final String WEBSCRIPT_TEMPLATE = WEBSCRIPT_ROOT_RM + "classification/clearancelevels.get.json.ftl";
|
||||
|
||||
/** ClearanceLevelsGet webscript instance */
|
||||
private @Spy @InjectMocks ClearanceLevelsGet webScript;
|
||||
private @Mock SecurityClearanceService mockSecurityClearanceService;
|
||||
|
||||
private List<ClearanceLevel> clearanceLevels;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected DeclarativeWebScript getWebScript()
|
||||
{
|
||||
return webScript;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected String getWebScriptTemplate()
|
||||
{
|
||||
return WEBSCRIPT_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the successful retrieval of two clearance levels.
|
||||
*/
|
||||
@Test
|
||||
public void getClearanceLevels() throws Exception
|
||||
{
|
||||
// Create test data.
|
||||
ClassificationLevel classificationLevelOne = new ClassificationLevel("id1", "classificationLabelKey1");
|
||||
ClassificationLevel classificationLevelTwo = new ClassificationLevel("id2", "classificationLabelKey2");
|
||||
clearanceLevels = Arrays.asList(
|
||||
new ClearanceLevel(classificationLevelOne, "labelKey1"),
|
||||
new ClearanceLevel(classificationLevelTwo, "labelKey2"));
|
||||
|
||||
// setup interactions
|
||||
doReturn(clearanceLevels).when(mockSecurityClearanceService).getClearanceLevels();
|
||||
|
||||
// execute web script
|
||||
JSONObject json = executeJSONWebScript(new HashMap<String, String>());
|
||||
assertNotNull(json);
|
||||
String actualJSONString = json.toString();
|
||||
|
||||
// check the JSON result using Jackson to allow easy equality testing.
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String expectedJSONString = "{\"data\":{\"items\":[{\"displayLabel\":\"labelKey1\",\"id\":\"id1\"},{\"displayLabel\":\"labelKey2\",\"id\":\"id2\"}]}}";
|
||||
JsonNode expected = mapper.readTree(expectedJSONString);
|
||||
assertEquals(expected, mapper.readTree(actualJSONString));
|
||||
}
|
||||
}
|
@@ -1,104 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.classification;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ExemptionCategory;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Tests for the get exemption categories API.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ExemptionCategoriesGetUnitTest extends BaseWebScriptUnitTest
|
||||
{
|
||||
/** Classpath location of ftl template for web script */
|
||||
private static final String WEBSCRIPT_TEMPLATE = WEBSCRIPT_ROOT_RM + "classification/exemptioncategories.get.json.ftl";
|
||||
|
||||
/** Webscript instance */
|
||||
private @Spy @InjectMocks ExemptionCategoriesGet webScript;
|
||||
private @Mock ClassificationSchemeService mockClassificationSchemeService;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected DeclarativeWebScript getWebScript()
|
||||
{
|
||||
return webScript;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected String getWebScriptTemplate()
|
||||
{
|
||||
return WEBSCRIPT_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the successful retrieval of two exemption categories.
|
||||
*/
|
||||
@Test
|
||||
public void getExemptionCategories() throws Exception
|
||||
{
|
||||
// Create test data.
|
||||
ExemptionCategory exemptionCategoryA = new ExemptionCategory("idA", "labelA");
|
||||
ExemptionCategory exemptionCategoryB = new ExemptionCategory("idB", "labelB");
|
||||
List<ExemptionCategory> exemptionCategories = asList(exemptionCategoryA, exemptionCategoryB);
|
||||
|
||||
// setup interactions
|
||||
doReturn(exemptionCategories).when(mockClassificationSchemeService).getExemptionCategories();
|
||||
|
||||
// setup web script parameters
|
||||
Map<String, String> parameters = new HashMap<>();
|
||||
|
||||
// execute web script
|
||||
JSONObject json = executeJSONWebScript(parameters);
|
||||
assertNotNull(json);
|
||||
String actualJSONString = json.toString();
|
||||
|
||||
// check the JSON result using Jackson to allow easy equality testing.
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String expectedJSONString = "{\"data\":{" +
|
||||
"\"items\":[" +
|
||||
"{\"displayLabel\":\"labelA\"," + "\"id\":\"idA\"," + "\"fullCategory\":\"idA: labelA\"}," +
|
||||
"{\"displayLabel\":\"labelB\",\"id\":\"idB\",\"fullCategory\":\"idB: labelB\"}" +
|
||||
"]" +
|
||||
"}}";
|
||||
JsonNode expected = mapper.readTree(expectedJSONString);
|
||||
assertEquals(expected, mapper.readTree(actualJSONString));
|
||||
}
|
||||
}
|
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationReason;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest;
|
||||
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 com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* Tests for the get classification reasons API.
|
||||
*
|
||||
* @author tpage
|
||||
*/
|
||||
public class ReasonsGetUnitTest extends BaseWebScriptUnitTest
|
||||
{
|
||||
/** Classpath location of ftl template for web script */
|
||||
private static final String WEBSCRIPT_TEMPLATE = WEBSCRIPT_ROOT_RM + "classification/reasons.get.json.ftl";
|
||||
|
||||
/** ReasonsGet webscript instance */
|
||||
private @Spy @InjectMocks ReasonsGet webScript;
|
||||
private @Mock ClassificationSchemeService mockClassificationSchemeService;
|
||||
|
||||
private List<ClassificationReason> reasonsList;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected DeclarativeWebScript getWebScript()
|
||||
{
|
||||
return webScript;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected String getWebScriptTemplate()
|
||||
{
|
||||
return WEBSCRIPT_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the successful retrieval of two classification reasons.
|
||||
*/
|
||||
@Test
|
||||
public void getReasons() throws Exception
|
||||
{
|
||||
// Create test data.
|
||||
ClassificationReason reasonA = new ClassificationReason("idA", "labelA");
|
||||
ClassificationReason reasonB = new ClassificationReason("idB", "labelB");
|
||||
reasonsList = Arrays.asList(reasonA, reasonB);
|
||||
|
||||
// setup interactions
|
||||
doReturn(reasonsList).when(mockClassificationSchemeService).getClassificationReasons();
|
||||
|
||||
// setup web script parameters
|
||||
Map<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
// execute web script
|
||||
JSONObject json = executeJSONWebScript(parameters);
|
||||
assertNotNull(json);
|
||||
String actualJSONString = json.toString();
|
||||
|
||||
// check the JSON result using Jackson to allow easy equality testing.
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String expectedJSONString = "{\"data\":{\"items\":[{\"displayLabel\":\"labelA\",\"id\":\"idA\",\"fullReason\":\"idA: labelA\"},{\"displayLabel\":\"labelB\",\"id\":\"idB\",\"fullReason\":\"idB: labelB\"}]}}";
|
||||
JsonNode expected = mapper.readTree(expectedJSONString);
|
||||
assertEquals(expected, mapper.readTree(actualJSONString));
|
||||
}
|
||||
}
|
@@ -1,327 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClearanceLevel;
|
||||
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.classification.UserQueryParams;
|
||||
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest;
|
||||
import org.alfresco.query.PagingResults;
|
||||
import org.alfresco.repo.jscript.ScriptNode;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.security.PersonService.PersonInfo;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.json.JSONArray;
|
||||
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.WebScriptRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* Test for get user security clearance API
|
||||
*
|
||||
* @author Tuna Aksoy
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class UserSecurityClearanceGetUnitTest extends BaseWebScriptUnitTest
|
||||
{
|
||||
/** Classpath location of ftl template for web script */
|
||||
private static final String WEBSCRIPT_TEMPLATE = WEBSCRIPT_ROOT_RM + "classification/usersecurityclearance.get.json.ftl";
|
||||
|
||||
/** User security clearance webscript instance*/
|
||||
private @Spy @InjectMocks UserSecurityClearanceGet webscript;
|
||||
|
||||
/** Mocked security clearance service */
|
||||
private @Mock SecurityClearanceService mockedSecurityClearanceService;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected DeclarativeWebScript getWebScript()
|
||||
{
|
||||
return webscript;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected String getWebScriptTemplate()
|
||||
{
|
||||
return WEBSCRIPT_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to get all security clearances (no filtering)
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void getUserSecurityClearances() throws Exception
|
||||
{
|
||||
String userName = "aUserName0";
|
||||
String firstName = "aFirstName0";
|
||||
String lastName = "aLastName0";
|
||||
String classificationLevelId = "id0";
|
||||
String classificationLevelDisplayLabel = "displayLabel0";
|
||||
|
||||
doReturn(new PagingResults<SecurityClearance>()
|
||||
{
|
||||
@Override
|
||||
public List<SecurityClearance> getPage()
|
||||
{
|
||||
return createSecurityClearances(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getQueryExecutionId()
|
||||
{
|
||||
return anyString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Integer, Integer> getTotalResultCount()
|
||||
{
|
||||
return new Pair<Integer, Integer>(1, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMoreItems()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}).when(mockedSecurityClearanceService).getUsersSecurityClearance(any(UserQueryParams.class));
|
||||
|
||||
JSONObject response = executeJSONWebScript(buildParameters("nameFilter", "userName"));
|
||||
assertNotNull(response);
|
||||
|
||||
JSONObject data = response.getJSONObject("data");
|
||||
assertNotNull(data);
|
||||
|
||||
assertEquals(1, data.getInt("total"));
|
||||
assertEquals(0, data.getInt("startIndex"));
|
||||
assertEquals(10, data.getInt("pageSize"));
|
||||
assertEquals(1, data.getInt("itemCount"));
|
||||
|
||||
JSONArray items = data.getJSONArray("items");
|
||||
assertNotNull(items);
|
||||
assertEquals(1, items.length());
|
||||
|
||||
JSONObject securityClearance = items.getJSONObject(0);
|
||||
assertNotNull(securityClearance);
|
||||
assertEquals(userName, securityClearance.getString("userName"));
|
||||
assertEquals(firstName, securityClearance.getString("firstName"));
|
||||
assertEquals(lastName, securityClearance.getString("lastName"));
|
||||
assertEquals(classificationLevelId, securityClearance.getString("classificationId"));
|
||||
assertEquals(classificationLevelDisplayLabel, securityClearance.getString("clearanceLabel"));
|
||||
String fullName = firstName + " " + lastName;
|
||||
assertEquals(fullName, securityClearance.getString("fullName"));
|
||||
assertEquals(fullName + " (" + userName + ")", securityClearance.getString("completeName"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to get all security clearances (with paging)
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void getUserSecurityClearancesWithPaging() throws Exception
|
||||
{
|
||||
int startIndex = 0;
|
||||
int pageSize = 5;
|
||||
int numberOfUsers = 25;
|
||||
int fromIndex = startIndex * pageSize;
|
||||
int toIndex = fromIndex + pageSize > numberOfUsers ? numberOfUsers : fromIndex + pageSize;
|
||||
List<SecurityClearance> securityClearances = createSecurityClearances(numberOfUsers);
|
||||
List<SecurityClearance> items = securityClearances.subList(fromIndex, toIndex);
|
||||
|
||||
doReturn(new PagingResults<SecurityClearance>()
|
||||
{
|
||||
@Override
|
||||
public List<SecurityClearance> getPage()
|
||||
{
|
||||
return items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getQueryExecutionId()
|
||||
{
|
||||
return anyString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Integer, Integer> getTotalResultCount()
|
||||
{
|
||||
return new Pair<>(numberOfUsers, numberOfUsers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMoreItems()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}).when(mockedSecurityClearanceService).getUsersSecurityClearance(any(UserQueryParams.class));
|
||||
|
||||
JSONObject response = executeJSONWebScript(buildParameters("nameFilter", "userName", "pageSize", Integer.toString(pageSize), "startIndex", Integer.toString(startIndex)));
|
||||
assertNotNull(response);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode expected = mapper.readTree(getExpectedResult(numberOfUsers, startIndex, pageSize, fromIndex, toIndex - 1, items.size()));
|
||||
assertEquals(expected, mapper.readTree(response.toString()));
|
||||
}
|
||||
|
||||
@Override protected Map<String, ScriptNode> getMockedPeople()
|
||||
{
|
||||
final Map<String, ScriptNode> people = super.getMockedPeople();
|
||||
|
||||
return people;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check that when supplying a single field with no sort direction, the UserQueryParams are populated with the
|
||||
* specified field and the default direction (true).
|
||||
*/
|
||||
@Test
|
||||
public void testSetSortProps_singleField()
|
||||
{
|
||||
UserQueryParams userQueryParams = mock(UserQueryParams.class);
|
||||
WebScriptRequest req = mock(WebScriptRequest.class);
|
||||
when(req.getParameter("sortField")).thenReturn("field");
|
||||
when(mockedNamespaceService.getNamespaceURI("")).thenReturn("namespace");
|
||||
|
||||
// Call the method under test.
|
||||
webscript.setSortProps(userQueryParams, req);
|
||||
|
||||
// Check the userQueryParams contains the field (and the direction has defaulted to ascending (TRUE)).
|
||||
Pair<QName, Boolean> sortPair = new Pair<>(QName.createQName("field", mockedNamespaceService), Boolean.TRUE);
|
||||
List<Pair<QName, Boolean>> expectedSortProps = Arrays.asList(sortPair);
|
||||
verify(userQueryParams).withSortProps(expectedSortProps);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that when supplying three fields with different sort directions (ascending, descending, unspecified), the
|
||||
* UserQueryParams gets populated correctly.
|
||||
*/
|
||||
@Test
|
||||
public void testSetSortProps_multipleFieldsAndDirections()
|
||||
{
|
||||
UserQueryParams userQueryParams = mock(UserQueryParams.class);
|
||||
WebScriptRequest req = mock(WebScriptRequest.class);
|
||||
when(req.getParameter("sortField")).thenReturn("fieldA,fieldB,fieldC");
|
||||
// The sort order for the fields is ascending, descending, unspecified (which should default to ascending).
|
||||
when(req.getParameter("sortAscending")).thenReturn("true,false");
|
||||
when(mockedNamespaceService.getNamespaceURI("")).thenReturn("namespace");
|
||||
|
||||
// Call the method under test.
|
||||
webscript.setSortProps(userQueryParams, req);
|
||||
|
||||
Pair<QName, Boolean> sortPairA = new Pair<>(QName.createQName("fieldA", mockedNamespaceService), Boolean.TRUE);
|
||||
Pair<QName, Boolean> sortPairB = new Pair<>(QName.createQName("fieldB", mockedNamespaceService), Boolean.FALSE);
|
||||
Pair<QName, Boolean> sortPairC = new Pair<>(QName.createQName("fieldC", mockedNamespaceService), Boolean.TRUE);
|
||||
List<Pair<QName, Boolean>> expectedSortProps = Arrays.asList(sortPairA, sortPairB, sortPairC);
|
||||
verify(userQueryParams).withSortProps(expectedSortProps);
|
||||
}
|
||||
|
||||
/** Check that if no sort information is given there are no exceptions. */
|
||||
@Test
|
||||
public void testSetSortProps_noFields()
|
||||
{
|
||||
UserQueryParams userQueryParams = mock(UserQueryParams.class);
|
||||
WebScriptRequest req = mock(WebScriptRequest.class);
|
||||
when(req.getParameter("sortField")).thenReturn(null);
|
||||
when(mockedNamespaceService.getNamespaceURI("")).thenReturn("namespace");
|
||||
|
||||
// Call the method under test.
|
||||
webscript.setSortProps(userQueryParams, req);
|
||||
|
||||
verifyNoMoreInteractions(userQueryParams);
|
||||
}
|
||||
|
||||
private String getExpectedResult(int total, int startIndex, int pageSize, int fromIndex, int toIndex, int itemCount)
|
||||
{
|
||||
return "{" +
|
||||
"\"data\": {" +
|
||||
"\"total\": " + total + "," +
|
||||
"\"startIndex\": " + startIndex + "," +
|
||||
"\"pageSize\": " + pageSize + "," +
|
||||
"\"items\": [" +
|
||||
getItems(fromIndex, toIndex) +
|
||||
"]," +
|
||||
"\"itemCount\": " + itemCount +
|
||||
"}" +
|
||||
"}";
|
||||
}
|
||||
|
||||
private String getItems(int fromIndex, int toIndex)
|
||||
{
|
||||
String items = "";
|
||||
for (; fromIndex <= toIndex; fromIndex++)
|
||||
{
|
||||
items += "{" +
|
||||
"\"firstName\": \"aFirstName" + fromIndex + "\"," +
|
||||
"\"lastName\": \"aLastName" + fromIndex + "\"," +
|
||||
"\"clearanceLabel\": \"displayLabel" + fromIndex + "\"," +
|
||||
"\"isEditable\": true," +
|
||||
"\"completeName\": \"aFirstName" + fromIndex + " aLastName" + fromIndex + " (aUserName" + fromIndex + ")\"," +
|
||||
"\"fullName\": \"aFirstName" + fromIndex + " aLastName" + fromIndex + "\"," +
|
||||
"\"userName\": \"aUserName" + fromIndex + "\"," +
|
||||
"\"classificationId\": \"id" + fromIndex + "\"" +
|
||||
"}";
|
||||
if (fromIndex <= toIndex - 1)
|
||||
{
|
||||
items += ",";
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private List<SecurityClearance> createSecurityClearances(int number)
|
||||
{
|
||||
List<SecurityClearance> securityClearances = new ArrayList<>();
|
||||
for (int i = 0; i < number; i++)
|
||||
{
|
||||
PersonInfo personInfo = new PersonInfo(new NodeRef("a://noderef/" + i), "aUserName" + i, "aFirstName" + i, "aLastName" + i);
|
||||
ClassificationLevel classificationLevel = new ClassificationLevel("id" + i, "displayLabel" + i);
|
||||
ClearanceLevel clearanceLevel = new ClearanceLevel(classificationLevel, "displayLabel" + i);
|
||||
SecurityClearance securityClearance = new SecurityClearance(personInfo, clearanceLevel);
|
||||
securityClearances.add(securityClearance);
|
||||
}
|
||||
return securityClearances;
|
||||
}
|
||||
}
|
@@ -1,157 +0,0 @@
|
||||
package org.alfresco.module.org_alfresco_module_rm.script.classification;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
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.ClassificationException;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClearanceLevel;
|
||||
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.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
|
||||
public class UserSecurityClearancePutUnitTest 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 classificationLevel = new ClassificationLevel(clearanceId, clearanceDisplay);
|
||||
ClearanceLevel clearanceLevel = new ClearanceLevel(classificationLevel, 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\"," +
|
||||
"\"clearanceLabel\":\"Don't tell anyone\"," +
|
||||
"\"isEditable\":true," +
|
||||
"\"userName\":\"user1\"," +
|
||||
"\"completeName\":\"Firstname Lastname (user1)\",\"fullName\":\"Firstname Lastname\"," +
|
||||
"\"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 ClassificationException.LevelIdNotFound(clearanceId));
|
||||
|
||||
// Execute web script - this should throw the expected exception.
|
||||
executeJSONWebScript(parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the Security Clearance WebScript throws an appropriate error if the clearanceId isn't known.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testIncorrectClearanceId() throws Exception
|
||||
{
|
||||
String username = "user1";
|
||||
String clearanceId = "ThisClearanceDoesNotExist";
|
||||
|
||||
// Setup web script parameters
|
||||
Map<String, String> parameters = buildParameters(USERNAME, username, CLEARANCE_ID, clearanceId);
|
||||
|
||||
when(mockSecurityClearanceService.setUserSecurityClearance(username, clearanceId)).thenThrow(
|
||||
new ClassificationException.LevelIdNotFound(clearanceId));
|
||||
|
||||
try
|
||||
{
|
||||
// This should throw an exception because the clearanceId doesn't exist
|
||||
executeJSONWebScript(parameters);
|
||||
|
||||
fail("WebScript didn't throw an error when this missing clearance ID was passed in: " + clearanceId);
|
||||
}
|
||||
catch (WebScriptException e)
|
||||
{
|
||||
assertEquals(e.getStatus(), Status.STATUS_BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,130 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.alfresco.module.org_alfresco_module_rm.util;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import static org.alfresco.module.org_alfresco_module_rm.test.util.ExceptionUtils.expectedException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
import org.alfresco.service.cmr.dictionary.AspectDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryException;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
|
||||
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.namespace.QName;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CoreServicesExtras}.
|
||||
*
|
||||
* @author Neil Mc Erlean
|
||||
* @since 2.4.a
|
||||
*/
|
||||
public class CoreServicesExtrasUnitTest
|
||||
{
|
||||
private CoreServicesExtras serviceExtras;
|
||||
|
||||
private final QName testAspect = QName.createQName("test", "aspect");
|
||||
private final QName testProp1 = QName.createQName("test", "prop1");
|
||||
private final QName testProp2 = QName.createQName("test", "prop2");
|
||||
private final QName testProp3 = QName.createQName("test", "prop3");
|
||||
private final NodeRef testNode1 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "1");
|
||||
private final NodeRef testNode2 = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "2");
|
||||
|
||||
@Before public void setUp()
|
||||
{
|
||||
serviceExtras = new CoreServicesExtras();
|
||||
}
|
||||
|
||||
@Test public void copyingAnUnknownAspectShouldResultInAnException()
|
||||
{
|
||||
final DictionaryService mockDS = mock(DictionaryService.class);
|
||||
|
||||
when(mockDS.getAspect(any(QName.class))).thenReturn(null);
|
||||
serviceExtras.setDictionaryService(mockDS);
|
||||
|
||||
expectedException(DictionaryException.class, () -> serviceExtras.copyAspect(testNode1, testNode2, testAspect));
|
||||
}
|
||||
|
||||
@Test public void copyingAnAspectWithNoProperties()
|
||||
{
|
||||
final DictionaryService mockDS = mock(DictionaryService.class);
|
||||
final NodeService mockNS = mock(NodeService.class);
|
||||
|
||||
final AspectDefinition mockAspect = mock(AspectDefinition.class);
|
||||
when(mockAspect.getProperties()).thenReturn(Collections.emptyMap());
|
||||
|
||||
when(mockDS.getAspect(eq(testAspect))).thenReturn(mockAspect);
|
||||
when(mockNS.getProperties(eq(testNode1))).thenReturn(Collections.emptyMap());
|
||||
serviceExtras.setDictionaryService(mockDS);
|
||||
serviceExtras.setNodeService(mockNS);
|
||||
|
||||
assertEquals(Collections.emptyMap(), serviceExtras.copyAspect(testNode1, testNode2, testAspect));
|
||||
}
|
||||
|
||||
@Test public void copyingAnAspectWithProperties()
|
||||
{
|
||||
final DictionaryService mockDS = mock(DictionaryService.class);
|
||||
final NodeService mockNS = mock(NodeService.class);
|
||||
|
||||
final AspectDefinition mockAspect = mock(AspectDefinition.class);
|
||||
when(mockAspect.getName()).thenReturn(testAspect);
|
||||
|
||||
final Map<QName, PropertyDefinition> props = new HashMap<>();
|
||||
final PropertyDefinition mockProp1 = mock(PropertyDefinition.class);
|
||||
final PropertyDefinition mockProp2 = mock(PropertyDefinition.class);
|
||||
for (PropertyDefinition p : asList(mockProp1, mockProp2))
|
||||
{
|
||||
when(p.getContainerClass()).thenReturn(mockAspect);
|
||||
}
|
||||
props.put(testProp1, mockProp1);
|
||||
props.put(testProp2, mockProp2);
|
||||
when(mockAspect.getProperties()).thenReturn(props);
|
||||
|
||||
final Map<QName, Serializable> propVals = new HashMap<>();
|
||||
propVals.put(testProp1, "one");
|
||||
propVals.put(testProp2, "two");
|
||||
propVals.put(testProp3, "three"); // Not defined on the aspect above.
|
||||
when(mockDS.getAspect(eq(testAspect))).thenReturn(mockAspect);
|
||||
when(mockNS.getProperties(eq(testNode1))).thenReturn(propVals);
|
||||
|
||||
serviceExtras.setDictionaryService(mockDS);
|
||||
serviceExtras.setNodeService(mockNS);
|
||||
|
||||
Map<QName, Serializable> expected = new HashMap<>();
|
||||
expected.put(testProp1, "one");
|
||||
expected.put(testProp2, "two");
|
||||
assertEquals(expected, serviceExtras.copyAspect(testNode1, testNode2, testAspect));
|
||||
}
|
||||
}
|
@@ -1,169 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.repo.jscript.app;
|
||||
|
||||
import static org.alfresco.repo.jscript.app.ClassificationReasonsPropertyDecorator.DISPLAY_LABEL;
|
||||
import static org.alfresco.repo.jscript.app.ClassificationReasonsPropertyDecorator.FULL_REASON;
|
||||
import static org.alfresco.repo.jscript.app.ClassificationReasonsPropertyDecorator.ID;
|
||||
import static org.alfresco.repo.jscript.app.ClassificationReasonsPropertyDecorator.LABEL;
|
||||
import static org.alfresco.repo.jscript.app.ClassificationReasonsPropertyDecorator.VALUE;
|
||||
import static org.alfresco.util.GUID.generate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationReason;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeService;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
/**
|
||||
* Classification reasons property decorator unit test
|
||||
*
|
||||
* @author Tuna Aksoy
|
||||
* @since 2.4.a
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ClassificationReasonsPropertyDecoratorUnitTest
|
||||
{
|
||||
/** Classification reasons property decorator */
|
||||
private @InjectMocks ClassificationReasonsPropertyDecorator decorator = new ClassificationReasonsPropertyDecorator();
|
||||
|
||||
/** Mocked classification scheme service */
|
||||
private @Mock ClassificationSchemeService mockedClassificationSchemeService;
|
||||
|
||||
/**
|
||||
* Given that no classification reason id is supplied
|
||||
* When decorated
|
||||
* Then an {@link IllegalArgumentException} is thrown
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testDecoratorWithoutClassificationId()
|
||||
{
|
||||
decorator.decorate(null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that a classification reason id in an invalid format is supplied
|
||||
* When decorated
|
||||
* Then an {@link JsonSyntaxException} is thrown
|
||||
*/
|
||||
@Test(expected = JsonSyntaxException.class)
|
||||
public void testDecoratorWithInvalidClassificationReasonId()
|
||||
{
|
||||
String classificationReasonId = generate();
|
||||
String classificationReasonDisplayLabel = generate();
|
||||
|
||||
ClassificationReason classificationReason = new ClassificationReason(classificationReasonId, classificationReasonDisplayLabel);
|
||||
doReturn(classificationReason).when(mockedClassificationSchemeService).getClassificationReasonById(classificationReasonId);
|
||||
|
||||
decorator.decorate(null, null, classificationReasonId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that a classification reason id is supplied
|
||||
* When decorated
|
||||
* Then the result is a {@link JSONArray} containing the classification reason id, label, value, display label and full reason
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testDecoratorWithClassificationReasonId()
|
||||
{
|
||||
String classificationReasonId = generate();
|
||||
String classificationReasonDisplayLabel = generate();
|
||||
String classificationFullReason = classificationReasonId + ": " + classificationReasonDisplayLabel;
|
||||
|
||||
ClassificationReason classificationReason = new ClassificationReason(classificationReasonId, classificationReasonDisplayLabel);
|
||||
doReturn(classificationReason).when(mockedClassificationSchemeService).getClassificationReasonById(classificationReasonId);
|
||||
|
||||
JSONArray classificationReasonIds = new JSONArray();
|
||||
classificationReasonIds.add(classificationReasonId);
|
||||
|
||||
JSONArray decoratedProperty = (JSONArray) decorator.decorate(null, null, new Gson().toJson(classificationReasonIds));
|
||||
assertNotNull(decoratedProperty);
|
||||
assertEquals(1, decoratedProperty.size());
|
||||
|
||||
JSONObject jsonObject = (JSONObject) decoratedProperty.get(0);
|
||||
assertNotNull(jsonObject);
|
||||
|
||||
assertEquals(classificationReasonId, jsonObject.get(ID));
|
||||
assertEquals(classificationFullReason, jsonObject.get(LABEL));
|
||||
assertEquals(classificationReasonId, jsonObject.get(VALUE));
|
||||
assertEquals(classificationReasonDisplayLabel, jsonObject.get(DISPLAY_LABEL));
|
||||
assertEquals(classificationFullReason, jsonObject.get(FULL_REASON));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that two classification reason id are supplied
|
||||
* When decorated
|
||||
* Then the result is a {@link JSONArray} containing the classification reason ids,
|
||||
* labels, values, display labels and full reasons of both classification reasons
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testDecorateWithMultipleClassificationReasonIds()
|
||||
{
|
||||
String classificationReasonId1 = generate();
|
||||
String classificationReasonDisplayLabel1 = generate();
|
||||
String classificationFullReason1 = classificationReasonId1 + ": " + classificationReasonDisplayLabel1;
|
||||
|
||||
String classificationReasonId2 = generate();
|
||||
String classificationReasonDisplayLabel2 = generate();
|
||||
String classificationFullReason2 = classificationReasonId2 + ": " + classificationReasonDisplayLabel2;
|
||||
|
||||
ClassificationReason classificationReason1 = new ClassificationReason(classificationReasonId1, classificationReasonDisplayLabel1);
|
||||
ClassificationReason classificationReason2 = new ClassificationReason(classificationReasonId2, classificationReasonDisplayLabel2);
|
||||
doReturn(classificationReason1).when(mockedClassificationSchemeService).getClassificationReasonById(classificationReasonId1);
|
||||
doReturn(classificationReason2).when(mockedClassificationSchemeService).getClassificationReasonById(classificationReasonId2);
|
||||
|
||||
JSONArray classificationReasonIds = new JSONArray();
|
||||
classificationReasonIds.add(classificationReasonId1);
|
||||
classificationReasonIds.add(classificationReasonId2);
|
||||
|
||||
JSONArray decoratedProperty = (JSONArray) decorator.decorate(null, null, new Gson().toJson(classificationReasonIds));
|
||||
assertNotNull(decoratedProperty);
|
||||
assertEquals(2, decoratedProperty.size());
|
||||
|
||||
JSONObject jsonObject1 = (JSONObject) decoratedProperty.get(0);
|
||||
assertNotNull(jsonObject1);
|
||||
|
||||
assertEquals(classificationReasonId1, jsonObject1.get(ID));
|
||||
assertEquals(classificationFullReason1, jsonObject1.get(LABEL));
|
||||
assertEquals(classificationReasonId1, jsonObject1.get(VALUE));
|
||||
assertEquals(classificationReasonDisplayLabel1, jsonObject1.get(DISPLAY_LABEL));
|
||||
assertEquals(classificationFullReason1, jsonObject1.get(FULL_REASON));
|
||||
|
||||
JSONObject jsonObject2 = (JSONObject) decoratedProperty.get(1);
|
||||
assertNotNull(jsonObject2);
|
||||
|
||||
assertEquals(classificationReasonId2, jsonObject2.get(ID));
|
||||
assertEquals(classificationFullReason2, jsonObject2.get(LABEL));
|
||||
assertEquals(classificationReasonId2, jsonObject2.get(VALUE));
|
||||
assertEquals(classificationReasonDisplayLabel2, jsonObject2.get(DISPLAY_LABEL));
|
||||
assertEquals(classificationFullReason2, jsonObject2.get(FULL_REASON));
|
||||
}
|
||||
}
|
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.repo.jscript.app;
|
||||
|
||||
import static org.alfresco.repo.jscript.app.CurrentClassificationPropertyDecorator.ID;
|
||||
import static org.alfresco.repo.jscript.app.CurrentClassificationPropertyDecorator.LABEL;
|
||||
import static org.alfresco.util.GUID.generate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevel;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeService;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Current classification property decorator unit test
|
||||
*
|
||||
* @author Tuna Aksoy
|
||||
* @since 2.4.a
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CurrentClassificationPropertyDecoratorUnitTest
|
||||
{
|
||||
/** Current classification property decorator */
|
||||
private @InjectMocks CurrentClassificationPropertyDecorator decorator = new CurrentClassificationPropertyDecorator();
|
||||
|
||||
/** Mocked classification scheme service */
|
||||
private @Mock ClassificationSchemeService mockedClassificationSchemeService;
|
||||
|
||||
/**
|
||||
* Given that no classification id is supplied
|
||||
* When decorated
|
||||
* Then an {@link IllegalArgumentException} is thrown
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testDecoratorWithoutClassificationId()
|
||||
{
|
||||
decorator.decorate(null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that a classification id is supplied
|
||||
* When decorated
|
||||
* Then the result is a {@link JSONObject} containing the classification id and display label
|
||||
*/
|
||||
@Test
|
||||
public void testDecoratorWithClassificationId()
|
||||
{
|
||||
String classificationLevelId = generate();
|
||||
String classificationDisplayLabel = generate();
|
||||
ClassificationLevel classificationLevel = new ClassificationLevel(classificationLevelId, classificationDisplayLabel);
|
||||
doReturn(classificationLevel).when(mockedClassificationSchemeService).getClassificationLevelById(classificationLevelId);
|
||||
|
||||
JSONObject decoratedProperty = (JSONObject) decorator.decorate(null, null, classificationLevelId);
|
||||
assertNotNull(decoratedProperty);
|
||||
assertEquals(classificationLevelId, decoratedProperty.get(ID));
|
||||
assertEquals(classificationDisplayLabel, decoratedProperty.get(LABEL));
|
||||
}
|
||||
}
|
@@ -1,169 +0,0 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.repo.jscript.app;
|
||||
|
||||
import static org.alfresco.repo.jscript.app.ExemptionsCategoriesPropertyDecorator.DISPLAY_LABEL;
|
||||
import static org.alfresco.repo.jscript.app.ExemptionsCategoriesPropertyDecorator.FULL_CATEGORY;
|
||||
import static org.alfresco.repo.jscript.app.ExemptionsCategoriesPropertyDecorator.ID;
|
||||
import static org.alfresco.repo.jscript.app.ExemptionsCategoriesPropertyDecorator.LABEL;
|
||||
import static org.alfresco.repo.jscript.app.ExemptionsCategoriesPropertyDecorator.VALUE;
|
||||
import static org.alfresco.util.GUID.generate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationSchemeService;
|
||||
import org.alfresco.module.org_alfresco_module_rm.classification.ExemptionCategory;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
/**
|
||||
* Exemption categories property decorator unit test
|
||||
*
|
||||
* @author Tuna Aksoy
|
||||
* @since 2.4.a
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ExemptionCategoriesPropertyDecoratorUnitTest
|
||||
{
|
||||
/** Exemption categories property decorator */
|
||||
private @InjectMocks ExemptionsCategoriesPropertyDecorator decorator = new ExemptionsCategoriesPropertyDecorator();
|
||||
|
||||
/** Mocked classification scheme service */
|
||||
private @Mock ClassificationSchemeService mockedClassificationSchemeService;
|
||||
|
||||
/**
|
||||
* Given that no exemption category id is supplied
|
||||
* When decorated
|
||||
* Then an {@link IllegalArgumentException} is thrown
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testDecoratorWithoutExemptionCategoryId()
|
||||
{
|
||||
decorator.decorate(null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that an exemption category id in an invalid format is supplied
|
||||
* When decorated
|
||||
* Then an {@link JsonSyntaxException} is thrown
|
||||
*/
|
||||
@Test(expected = JsonSyntaxException.class)
|
||||
public void testDecoratorWithInvalidExemptionCategoryId()
|
||||
{
|
||||
String exemptionCategoryId = generate();
|
||||
String exemptionCategoryDisplayLabel = generate();
|
||||
|
||||
ExemptionCategory exemptionCategory = new ExemptionCategory(exemptionCategoryId, exemptionCategoryDisplayLabel);
|
||||
doReturn(exemptionCategory).when(mockedClassificationSchemeService).getExemptionCategoryById(exemptionCategoryId);
|
||||
|
||||
decorator.decorate(null, null, exemptionCategoryId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that an exemption category id is supplied
|
||||
* When decorated
|
||||
* Then the result is a {@link JSONArray} containing the exemption category id, label, value, display label and full category
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testDecoratorWithExemptionCategoryId()
|
||||
{
|
||||
String exemptionCategoryId = generate();
|
||||
String exemptionCategoryDisplayLabel = generate();
|
||||
String exemptionCategoryFullCategory = exemptionCategoryId + ": " + exemptionCategoryDisplayLabel;
|
||||
|
||||
ExemptionCategory exemptionCategory = new ExemptionCategory(exemptionCategoryId, exemptionCategoryDisplayLabel);
|
||||
doReturn(exemptionCategory).when(mockedClassificationSchemeService).getExemptionCategoryById(exemptionCategoryId);
|
||||
|
||||
JSONArray exemptionCategoryIds = new JSONArray();
|
||||
exemptionCategoryIds.add(exemptionCategoryId);
|
||||
|
||||
JSONArray decoratedProperty = (JSONArray) decorator.decorate(null, null, new Gson().toJson(exemptionCategoryIds));
|
||||
assertNotNull(decoratedProperty);
|
||||
assertEquals(1, decoratedProperty.size());
|
||||
|
||||
JSONObject jsonObject = (JSONObject) decoratedProperty.get(0);
|
||||
assertNotNull(jsonObject);
|
||||
|
||||
assertEquals(exemptionCategoryId, jsonObject.get(ID));
|
||||
assertEquals(exemptionCategoryFullCategory, jsonObject.get(LABEL));
|
||||
assertEquals(exemptionCategoryId, jsonObject.get(VALUE));
|
||||
assertEquals(exemptionCategoryDisplayLabel, jsonObject.get(DISPLAY_LABEL));
|
||||
assertEquals(exemptionCategoryFullCategory, jsonObject.get(FULL_CATEGORY));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that two exemption category ids are supplied
|
||||
* When decorated
|
||||
* Then the result is a {@link JSONArray} containing the exemption category ids,
|
||||
* labels, values, display labels and full category of both exemption categories
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testDecorateWithMultipleExemptionCategoryIds()
|
||||
{
|
||||
String exemptionCategoryId1 = generate();
|
||||
String exemptionCategoryDisplayLabel1 = generate();
|
||||
String exemptionCategoryIdFullCategory1 = exemptionCategoryId1 + ": " + exemptionCategoryDisplayLabel1;
|
||||
|
||||
String exemptionCategoryId2 = generate();
|
||||
String exemptionCategoryDisplayLabel2 = generate();
|
||||
String exemptionCategoryFullCategory2 = exemptionCategoryId2 + ": " + exemptionCategoryDisplayLabel2;
|
||||
|
||||
ExemptionCategory exemptionCategory1 = new ExemptionCategory(exemptionCategoryId1, exemptionCategoryDisplayLabel1);
|
||||
ExemptionCategory exemptionCategory2 = new ExemptionCategory(exemptionCategoryId2, exemptionCategoryDisplayLabel2);
|
||||
doReturn(exemptionCategory1).when(mockedClassificationSchemeService).getExemptionCategoryById(exemptionCategoryId1);
|
||||
doReturn(exemptionCategory2).when(mockedClassificationSchemeService).getExemptionCategoryById(exemptionCategoryId2);
|
||||
|
||||
JSONArray exemptionCategoryIds = new JSONArray();
|
||||
exemptionCategoryIds.add(exemptionCategoryId1);
|
||||
exemptionCategoryIds.add(exemptionCategoryId2);
|
||||
|
||||
JSONArray decoratedProperty = (JSONArray) decorator.decorate(null, null, new Gson().toJson(exemptionCategoryIds));
|
||||
assertNotNull(decoratedProperty);
|
||||
assertEquals(2, decoratedProperty.size());
|
||||
|
||||
JSONObject jsonObject1 = (JSONObject) decoratedProperty.get(0);
|
||||
assertNotNull(jsonObject1);
|
||||
|
||||
assertEquals(exemptionCategoryId1, jsonObject1.get(ID));
|
||||
assertEquals(exemptionCategoryIdFullCategory1, jsonObject1.get(LABEL));
|
||||
assertEquals(exemptionCategoryId1, jsonObject1.get(VALUE));
|
||||
assertEquals(exemptionCategoryDisplayLabel1, jsonObject1.get(DISPLAY_LABEL));
|
||||
assertEquals(exemptionCategoryIdFullCategory1, jsonObject1.get(FULL_CATEGORY));
|
||||
|
||||
JSONObject jsonObject2 = (JSONObject) decoratedProperty.get(1);
|
||||
assertNotNull(jsonObject2);
|
||||
|
||||
assertEquals(exemptionCategoryId2, jsonObject2.get(ID));
|
||||
assertEquals(exemptionCategoryFullCategory2, jsonObject2.get(LABEL));
|
||||
assertEquals(exemptionCategoryId2, jsonObject2.get(VALUE));
|
||||
assertEquals(exemptionCategoryDisplayLabel2, jsonObject2.get(DISPLAY_LABEL));
|
||||
assertEquals(exemptionCategoryFullCategory2, jsonObject2.get(FULL_CATEGORY));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user