Skip to content

Commit

Permalink
Add tests for new code in AutoCreateTestUsersConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
gicig committed Jul 8, 2024
1 parent d16272c commit 68c14f6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> DEFAULT_PRODUCTION_RUNMODES = Arrays.asList("prod", "production");

Expand Down Expand Up @@ -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<String>) impersonationAllowedForObj;
} else {
throw new IllegalArgumentException("Property \"" + KEY_IMPERSONATION_ALLOWED_FOR + "\" must be a list");
}
}

Expand Down Expand Up @@ -116,8 +123,4 @@ public String getEmail() {
public List<String> getImpersonationAllowedFor() {
return impersonationAllowedFor;
}

public void setImpersonationAllowedFor(List<String> impersonationAllowedFor) {
this.impersonationAllowedFor = impersonationAllowedFor;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> configMap = initializeConfigMap(null);
assertEquals(new ArrayList<>(), (new AutoCreateTestUsersConfig(configMap)).getImpersonationAllowedFor());
}

@Test()
void shouldNotContainImpersonalizationAllowedFor2() {
Map<String, Object> 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<String, Object> map = initializeConfigMap(Arrays.asList("user1"));
assertEquals(Arrays.asList("user1"), (new AutoCreateTestUsersConfig(map)).getImpersonationAllowedFor());
}

@NotNull
private static Map<String, Object> initializeConfigMap(Object allowedFor) {
Map<String, Object> 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;
}
}

0 comments on commit 68c14f6

Please sign in to comment.