From cb4823f9eb4b83eab8aeaa6fabe7adbcb440d430 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Mon, 12 Oct 2015 10:35:22 +0000 Subject: [PATCH] RM-2604 Unit tests for loading the caveat configuration JSON. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/DEV/caveatmarkdatatype@114111 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../caveat/rm-caveats.json | 44 +++++++++ .../caveat/dao/CaveatDAOFromJSON.java | 27 ++++- .../caveat/dao/CaveatDAOFromJSONUnitTest.java | 99 +++++++++++++++++++ .../caveat/rm-caveats-duplicateGroupId.json | 22 +++++ .../caveat/rm-caveats-duplicateMarkId.json | 19 ++++ ...eats-duplicateMarkIdInDifferentGroups.json | 28 ++++++ .../caveat/rm-caveats-malformedJSON.json | 44 +++++++++ .../alfresco/caveat/rm-caveats-missingId.json | 28 ++++++ 8 files changed, 307 insertions(+), 4 deletions(-) create mode 100644 rm-server/config/alfresco/module/org_alfresco_module_rm/caveat/rm-caveats.json create mode 100644 rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java create mode 100644 rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateGroupId.json create mode 100644 rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateMarkId.json create mode 100644 rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateMarkIdInDifferentGroups.json create mode 100644 rm-server/unit-test/resources/alfresco/caveat/rm-caveats-malformedJSON.json create mode 100644 rm-server/unit-test/resources/alfresco/caveat/rm-caveats-missingId.json diff --git a/rm-server/config/alfresco/module/org_alfresco_module_rm/caveat/rm-caveats.json b/rm-server/config/alfresco/module/org_alfresco_module_rm/caveat/rm-caveats.json new file mode 100644 index 0000000000..f495b3bda0 --- /dev/null +++ b/rm-server/config/alfresco/module/org_alfresco_module_rm/caveat/rm-caveats.json @@ -0,0 +1,44 @@ +[ + { + "id" : "classification", + "displayLabel" : "rm.caveat.classification.group", + "description" : "rm.caveat.classification.description", + "type" : "HIERARCHICAL", + "marks" : + [ + { + "id" : "TS", + "displayLabel" : "rm.caveat.classification.mark.ts" + }, + { + "id" : "S", + "displayLabel" : "rm.caveat.classification.mark.s" + }, + { + "id" : "C", + "displayLabel" : "rm.caveat.classification.mark.c" + } + ] + }, + { + "id" : "nationality", + "displayLabel" : "rm.caveat.nationality.group", + "description" : "rm.caveat.nationality.description", + "type" : "CUMULATIVE_AVAILABILITY", + "marks" : + [ + { + "id" : "GBR", + "displayLabel" : "rm.caveat.nationality.mark.gbr" + }, + { + "id" : "CAN", + "displayLabel" : "rm.caveat.nationality.mark.can" + }, + { + "id" : "AUS", + "displayLabel" : "rm.caveat.nationality.mark.aus" + } + ] + } +] \ No newline at end of file diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java index 63b441fe6a..dc46586097 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSON.java @@ -23,8 +23,10 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.alfresco.module.org_alfresco_module_rm.caveat.CaveatException.MalformedConfiguration; import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; @@ -35,6 +37,8 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * An object that provides access to the configured caveat groups and marks, which it retrieves from JSON files. @@ -58,6 +62,8 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface private static final String MARK_ID_JSON_KEY = "id"; /** JSON key for the mark display label key. */ private static final String MARK_DISPLAY_LABEL_JSON_KEY = "displayLabel"; + /** Logging utility for the class. */ + private static final Logger LOGGER = LoggerFactory.getLogger(CaveatDAOFromJSON.class); /** The location of the configuration file relative to the classpath. */ String configLocation; @@ -94,11 +100,14 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface result.put(caveatGroupId, caveatGroup); } } + else + { + LOGGER.warn("Could not find caveat configuration file: " + configLocation); + } } catch (IOException | JSONException e) { - String message = "Could not read caveat configuration: " + configLocation; - throw new MalformedConfiguration(message, e); + throw new MalformedConfiguration("Could not read caveat configuration: " + configLocation, e); } return result; } @@ -110,7 +119,7 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface * @return The created group. * @throws JSONException If there is an issue reading the JSON. */ - private CaveatGroup createGroup(JSONObject jsonGroup) throws JSONException + protected CaveatGroup createGroup(JSONObject jsonGroup) throws JSONException { String id = jsonGroup.getString(GROUP_ID_JSON_KEY); String displayLabelKey = jsonGroup.getString(GROUP_DISPLAY_LABEL_JSON_KEY); @@ -128,11 +137,21 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface // Create a list of the configured caveat marks. List caveatMarks = new ArrayList<>(); + Set markIds = new HashSet<>(); JSONArray jsonMarks = jsonGroup.getJSONArray(MARKS_JSON_KEY); for (int i = 0; i < jsonMarks.length(); i++) { JSONObject jsonMark = jsonMarks.getJSONObject(i); - caveatMarks.add(createMark(jsonMark)); + CaveatMark caveatMark = createMark(jsonMark); + caveatMarks.add(caveatMark); + if (!markIds.contains(caveatMark.getId())) + { + markIds.add(caveatMark.getId()); + } + else + { + throw new MalformedConfiguration("Duplicate caveat mark id " + caveatMark.getId() + " within a group."); + } } // Instantiate the group (and associate the marks with the group). diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java new file mode 100644 index 0000000000..d426320d95 --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/caveat/dao/CaveatDAOFromJSONUnitTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ + +package org.alfresco.module.org_alfresco_module_rm.caveat.dao; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +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.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(); + + /** Test that loading the default caveat configuration file doesn't throw any exceptions. */ + @Test + public void testGetCaveatGroups() + { + caveatDAOFromJSON.setConfigLocation("/alfresco/module/org_alfresco_module_rm/caveat/rm-caveats.json"); + Map 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 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-missingId.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. */ + public void testGetCaveatGroups_duplicateMarkIdInDifferentGroups() + { + caveatDAOFromJSON.setConfigLocation("/alfresco/caveat/rm-caveats-duplicateMarkIdInDifferentGroups.json"); + Map caveatGroups = caveatDAOFromJSON.getCaveatGroups(); + assertNotNull(caveatGroups); + } +} diff --git a/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateGroupId.json b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateGroupId.json new file mode 100644 index 0000000000..01a5d8f985 --- /dev/null +++ b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateGroupId.json @@ -0,0 +1,22 @@ +[ + { + "id" : "classification", + "displayLabel" : "rm.caveat.classification.group", + "description" : "rm.caveat.classification.description", + "type" : "HIERARCHICAL", + "marks" : + [ + { + "id" : "TS", + "displayLabel" : "rm.caveat.classification.mark.ts" + }, + { + "displayLabel" : "rm.caveat.classification.mark.s" + }, + { + "id" : "C", + "displayLabel" : "rm.caveat.classification.mark.c" + } + ] + } +] \ No newline at end of file diff --git a/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateMarkId.json b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateMarkId.json new file mode 100644 index 0000000000..45be6742ed --- /dev/null +++ b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateMarkId.json @@ -0,0 +1,19 @@ +[ + { + "id" : "classification", + "displayLabel" : "rm.caveat.classification.group", + "description" : "rm.caveat.classification.description", + "type" : "HIERARCHICAL", + "marks" : + [ + { + "id" : "TS", + "displayLabel" : "rm.caveat.classification.mark.ts" + }, + { + "id" : "TS", + "displayLabel" : "rm.caveat.classification.mark.s" + } + ] + } +] \ No newline at end of file diff --git a/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateMarkIdInDifferentGroups.json b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateMarkIdInDifferentGroups.json new file mode 100644 index 0000000000..c5b8b6ce01 --- /dev/null +++ b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-duplicateMarkIdInDifferentGroups.json @@ -0,0 +1,28 @@ +[ + { + "id" : "classification", + "displayLabel" : "rm.caveat.classification.group", + "description" : "rm.caveat.classification.description", + "type" : "HIERARCHICAL", + "marks" : + [ + { + "id" : "CO", + "displayLabel" : "rm.caveat.classification.mark.confidential" + } + ] + }, + { + "id" : "nationality", + "displayLabel" : "rm.caveat.nationality.group", + "description" : "rm.caveat.nationality.description", + "type" : "CUMULATIVE_AVAILABILITY", + "marks" : + [ + { + "id" : "CO", + "displayLabel" : "rm.caveat.nationality.mark.colombia" + } + ] + } +] \ No newline at end of file diff --git a/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-malformedJSON.json b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-malformedJSON.json new file mode 100644 index 0000000000..9296c5d2a7 --- /dev/null +++ b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-malformedJSON.json @@ -0,0 +1,44 @@ +[ + { + "id" : "classification", + "displayLabel" : "rm.caveat.classification.group", + "description" : "rm.caveat.classification.description", + "type" : "HIERARCHICAL", + "marks" : + [[ + { + "id" : "TS", + "displayLabel" : "rm.caveat.classification.mark.ts" + }, + { + "id" : "S", + "displayLabel" : "rm.caveat.classification.mark.s" + }, + { + "id" : "C", + "displayLabel" : "rm.caveat.classification.mark.c" + } + ] + }, + { + "id" : "nationality", + "displayLabel" : "rm.caveat.nationality.group", + "description" : "rm.caveat.nationality.description", + "type" : "CUMULATIVE_AVAILABILITY", + "marks" : + [ + { + "id" : "GBR", + "displayLabel" : "rm.caveat.nationality.mark.gbr" + }, + { + "id" : "CAN", + "displayLabel" : "rm.caveat.nationality.mark.can" + }, + { + "id" : "AUS", + "displayLabel" : "rm.caveat.nationality.mark.aus" + } + ] + } +] \ No newline at end of file diff --git a/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-missingId.json b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-missingId.json new file mode 100644 index 0000000000..2bcded6c7b --- /dev/null +++ b/rm-server/unit-test/resources/alfresco/caveat/rm-caveats-missingId.json @@ -0,0 +1,28 @@ +[ + { + "id" : "classification", + "displayLabel" : "rm.caveat.classification.group", + "description" : "rm.caveat.classification.description", + "type" : "HIERARCHICAL", + "marks" : + [ + { + "id" : "TS", + "displayLabel" : "rm.caveat.classification.mark.ts" + } + ] + }, + { + "id" : "classification", + "displayLabel" : "rm.caveat.classification.group", + "description" : "rm.caveat.classification.description", + "type" : "HIERARCHICAL", + "marks" : + [ + { + "id" : "TS", + "displayLabel" : "rm.caveat.classification.mark.ts" + } + ] + } +] \ No newline at end of file