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
This commit is contained in:
Tom Page
2015-10-12 10:35:22 +00:00
parent 14d9e3a532
commit cb4823f9eb
8 changed files with 307 additions and 4 deletions

View File

@@ -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"
}
]
}
]

View File

@@ -23,8 +23,10 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; 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.CaveatException.MalformedConfiguration;
import org.alfresco.module.org_alfresco_module_rm.caveat.scheme.CaveatGroup; 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.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener; 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. * 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"; private static final String MARK_ID_JSON_KEY = "id";
/** JSON key for the mark display label key. */ /** JSON key for the mark display label key. */
private static final String MARK_DISPLAY_LABEL_JSON_KEY = "displayLabel"; 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. */ /** The location of the configuration file relative to the classpath. */
String configLocation; String configLocation;
@@ -94,11 +100,14 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface
result.put(caveatGroupId, caveatGroup); result.put(caveatGroupId, caveatGroup);
} }
} }
else
{
LOGGER.warn("Could not find caveat configuration file: " + configLocation);
}
} }
catch (IOException | JSONException e) catch (IOException | JSONException e)
{ {
String message = "Could not read caveat configuration: " + configLocation; throw new MalformedConfiguration("Could not read caveat configuration: " + configLocation, e);
throw new MalformedConfiguration(message, e);
} }
return result; return result;
} }
@@ -110,7 +119,7 @@ public class CaveatDAOFromJSON implements CaveatDAOInterface
* @return The created group. * @return The created group.
* @throws JSONException If there is an issue reading the JSON. * @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 id = jsonGroup.getString(GROUP_ID_JSON_KEY);
String displayLabelKey = jsonGroup.getString(GROUP_DISPLAY_LABEL_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. // Create a list of the configured caveat marks.
List<CaveatMark> caveatMarks = new ArrayList<>(); List<CaveatMark> caveatMarks = new ArrayList<>();
Set<String> markIds = new HashSet<>();
JSONArray jsonMarks = jsonGroup.getJSONArray(MARKS_JSON_KEY); JSONArray jsonMarks = jsonGroup.getJSONArray(MARKS_JSON_KEY);
for (int i = 0; i < jsonMarks.length(); i++) for (int i = 0; i < jsonMarks.length(); i++)
{ {
JSONObject jsonMark = jsonMarks.getJSONObject(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). // Instantiate the group (and associate the marks with the group).

View File

@@ -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 <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 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<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-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<String, CaveatGroup> caveatGroups = caveatDAOFromJSON.getCaveatGroups();
assertNotNull(caveatGroups);
}
}

View File

@@ -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"
}
]
}
]

View File

@@ -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"
}
]
}
]

View File

@@ -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"
}
]
}
]

View File

@@ -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"
}
]
}
]

View File

@@ -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"
}
]
}
]