From 277d9aabbd75870cc32f703e510f15a3ce9c6e60 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Thu, 3 Aug 2017 08:56:04 +0100 Subject: [PATCH] RM-5481 Handle empty string as a reason or exemption id. If the empty string is passed as a classification reason or exemption id then the API framework converts it to null for us. We should throw a 404 (not found) error, because there are no reasons or exemptions with the empty string as their id (or null for that matter). Add two new REST API tests for these cases. Also stopped using the helper method addParam in TopicsWithInstructionsTest, as it is only saving us about ten characters, causes an extra import and includes an explicit mention of the topicsApi inside it (i.e. it doesn't work at all with the guidesAPI). --- .../util/RMCollectionUtils.java | 25 +++++++++++++++++++ .../util/RMCollectionUtilsUnitTest.java | 18 +++++++++++++ 2 files changed, 43 insertions(+) diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtils.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtils.java index 81e9652d72..6b1643f7fa 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtils.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtils.java @@ -37,6 +37,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.google.common.collect.ImmutableSet; + /** * Various common helper methods for Collections. This class is probably only appropriate for use with relatively * small collections as it has not been optimised for dealing with large collections. @@ -201,4 +203,27 @@ public final class RMCollectionUtils else { return Difference.UNCHANGED; } } } + + /** + * Convert a collection to an immutable set. Any instances of null in the original collection will be removed. + * + * @param collection The original collection. + * @param The type of the object in the collection. + * @return The immutable set. + */ + public static ImmutableSet toImmutableSet(Collection collection) + { + if (collection == null) + { + return null; + } + // Guava immutable collections can't contain null. + if (collection.contains(null)) + { + // Make sure we're not changing the original collection (which might not be editable anyway). + collection = new HashSet<>(collection); + collection.remove(null); + } + return ImmutableSet.copyOf(collection); + } } diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtilsUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtilsUnitTest.java index c7f2ede934..490dba4248 100644 --- a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtilsUnitTest.java +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/util/RMCollectionUtilsUnitTest.java @@ -46,6 +46,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils.Difference; import org.junit.Test; @@ -122,4 +123,21 @@ public class RMCollectionUtilsUnitTest assertEquals(s, l); } + + @Test public void toImmutableSet_nullInput() + { + Set output = RMCollectionUtils.toImmutableSet(null); + assertNull("toImmutableSet should pass through with null.", output); + } + + @Test public void toImmutableSet_nullElement() + { + Set input = newHashSet("One", null, "Three"); + + // Call the method under test. + Set output = RMCollectionUtils.toImmutableSet(input); + + assertEquals("Unexpected handling of null input element", output, newHashSet("One", "Three")); + assertEquals("Input should not have been changed.", input, newHashSet("One", null, "Three")); + } }