From 68c14f6958f72c7124b37a1a65734e240d199c79 Mon Sep 17 00:00:00 2001 From: Igor Rogic Date: Mon, 8 Jul 2024 14:48:25 +0200 Subject: [PATCH] Add tests for new code in AutoCreateTestUsersConfig --- .../AutoCreateTestUsersConfig.java | 21 ++++++---- .../AutoCreateTestUsersConfigTest.java | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/configmodel/AutoCreateTestUsersConfigTest.java diff --git a/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/configmodel/AutoCreateTestUsersConfig.java b/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/configmodel/AutoCreateTestUsersConfig.java index 77299acc..3d023e90 100644 --- a/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/configmodel/AutoCreateTestUsersConfig.java +++ b/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/configmodel/AutoCreateTestUsersConfig.java @@ -8,6 +8,7 @@ */ package biz.netcentric.cq.tools.actool.configmodel; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -19,15 +20,15 @@ /** Allows to automatically create test users. */ public class AutoCreateTestUsersConfig { - private static final String KEY_PREFIX = "prefix"; + static final String KEY_PREFIX = "prefix"; private static final String KEY_NAME = "name"; private static final String KEY_EMAIL = "email"; private static final String KEY_DESCRIPTION = "description"; private static final String KEY_PASSWORD = "password"; private static final String KEY_SKIP_FOR_RUNMODES = "skipForRunmodes"; - private static final String KEY_CREATE_FOR_GROUP_NAMES_REG_EX = "createForGroupNamesRegEx"; - private static final String KEY_PATH = "path"; - private static final String KEY_IMPERSONATION_ALLOWED_FOR = "impersonationAllowedFor"; + static final String KEY_CREATE_FOR_GROUP_NAMES_REG_EX = "createForGroupNamesRegEx"; + static final String KEY_PATH = "path"; + static final String KEY_IMPERSONATION_ALLOWED_FOR = "impersonationAllowedFor"; private static final List DEFAULT_PRODUCTION_RUNMODES = Arrays.asList("prod", "production"); @@ -75,9 +76,15 @@ public AutoCreateTestUsersConfig(Map map) { } this.path = String.valueOf(map.get(KEY_PATH)); + Object impersonationAllowedForObj = map.get(KEY_IMPERSONATION_ALLOWED_FOR); - if (impersonationAllowedForObj instanceof List) { + if (impersonationAllowedForObj == null) { + this.impersonationAllowedFor = new ArrayList<>(); + } + else if (impersonationAllowedForObj instanceof List) { this.impersonationAllowedFor = (List) impersonationAllowedForObj; + } else { + throw new IllegalArgumentException("Property \"" + KEY_IMPERSONATION_ALLOWED_FOR + "\" must be a list"); } } @@ -116,8 +123,4 @@ public String getEmail() { public List getImpersonationAllowedFor() { return impersonationAllowedFor; } - - public void setImpersonationAllowedFor(List impersonationAllowedFor) { - this.impersonationAllowedFor = impersonationAllowedFor; - } } diff --git a/accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/configmodel/AutoCreateTestUsersConfigTest.java b/accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/configmodel/AutoCreateTestUsersConfigTest.java new file mode 100644 index 00000000..2fc97c98 --- /dev/null +++ b/accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/configmodel/AutoCreateTestUsersConfigTest.java @@ -0,0 +1,42 @@ +package biz.netcentric.cq.tools.actool.configmodel; + +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.util.*; + +import static biz.netcentric.cq.tools.actool.configmodel.AutoCreateTestUsersConfig.*; +import static org.junit.jupiter.api.Assertions.*; + +class AutoCreateTestUsersConfigTest { + + @Test + void shouldNotContainImpersonalizationAllowedFor() { + Map configMap = initializeConfigMap(null); + assertEquals(new ArrayList<>(), (new AutoCreateTestUsersConfig(configMap)).getImpersonationAllowedFor()); + } + + @Test() + void shouldNotContainImpersonalizationAllowedFor2() { + Map configMap = initializeConfigMap("invalidValue"); + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + new AutoCreateTestUsersConfig(configMap); + }); + assertEquals("Property \"" + KEY_IMPERSONATION_ALLOWED_FOR + "\" must be a list", exception.getMessage()); + } + @Test + void shouldNotImpersonalizationAllowedFor() { + Map map = initializeConfigMap(Arrays.asList("user1")); + assertEquals(Arrays.asList("user1"), (new AutoCreateTestUsersConfig(map)).getImpersonationAllowedFor()); + } + + @NotNull + private static Map initializeConfigMap(Object allowedFor) { + Map map = new HashMap<>(); + map.put(KEY_PATH, "/"); + map.put(KEY_PREFIX, "prefix"); + map.put(KEY_CREATE_FOR_GROUP_NAMES_REG_EX, ""); + map.put(KEY_IMPERSONATION_ALLOWED_FOR, allowedFor); + return map; + } +} \ No newline at end of file