Skip to content

Commit 5a32f6f

Browse files
RANGER-5349: Test Cases for agents-common Module: Packages (plugin.service, plugin.store)
1 parent ea5042a commit 5a32f6f

22 files changed

+5651
-33
lines changed

agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestRangerPluginCapability.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,52 @@
2525
import org.apache.commons.lang3.StringUtils;
2626
import org.apache.ranger.authorization.utils.JsonUtils;
2727
import org.apache.ranger.plugin.util.RangerPluginCapability;
28-
import org.junit.Test;
28+
import org.junit.jupiter.api.MethodOrderer;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.TestMethodOrder;
31+
import org.junit.jupiter.api.extension.ExtendWith;
32+
import org.mockito.junit.jupiter.MockitoExtension;
2933

3034
import java.io.InputStream;
3135
import java.io.InputStreamReader;
3236
import java.util.Arrays;
3337
import java.util.List;
3438

35-
import static org.junit.Assert.assertTrue;
39+
import static org.junit.jupiter.api.Assertions.assertNotNull;
40+
import static org.junit.jupiter.api.Assertions.assertTrue;
3641

42+
/**
43+
* @generated by Cursor
44+
* @description <Unit Test for RangerPluginCapability class>
45+
*/
46+
@ExtendWith(MockitoExtension.class)
47+
@TestMethodOrder(MethodOrderer.MethodName.class)
3748
public class TestRangerPluginCapability {
3849
private static final Gson gsonBuilder = new GsonBuilder().setDateFormat("yyyyMMdd-HH:mm:ss.SSS-Z").setPrettyPrinting().create();
3950

4051
@Test
41-
public void testRangerPluginCapabilities() {
52+
public void test01_RangerPluginCapabilitiesFromResource() {
4253
String[] tests = {"/policyengine/plugin/test_plugin_capability.json"};
4354

4455
runTestsFromResourceFiles(tests);
4556
}
4657

58+
@Test
59+
public void test02_toStringAndUnknownBit() {
60+
// Construct a capability with a bit beyond defined enum to trigger "unknown"
61+
long beyond = 1L << 62; // well beyond current enum size
62+
RangerPluginCapability me = new RangerPluginCapability(beyond);
63+
RangerPluginCapability other = new RangerPluginCapability();
64+
65+
List<String> diff = me.compare(other);
66+
assertTrue(diff.contains("unknown"));
67+
68+
String json = me.toString();
69+
assertNotNull(json);
70+
// toString returns JSON array
71+
assertTrue(json.startsWith("["));
72+
}
73+
4774
private void runTestsFromResourceFiles(String[] resourceNames) {
4875
for (String resourceName : resourceNames) {
4976
InputStream inStream = this.getClass().getResourceAsStream(resourceName);
@@ -69,7 +96,7 @@ private void runTests(InputStreamReader reader, String fileName) {
6996

7097
List<String> difference = me.compare(other);
7198

72-
assertTrue(fileName + "-" + testCase.name + "-" + Arrays.toString(difference.toArray()), StringUtils.equals(JsonUtils.listToJson(difference), JsonUtils.listToJson(testCase.difference)));
99+
assertTrue(StringUtils.equals(JsonUtils.listToJson(difference), JsonUtils.listToJson(testCase.difference)), fileName + "-" + testCase.name + "-" + Arrays.toString(difference.toArray()));
73100
}
74101
}
75102

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.ranger.plugin.service;
21+
22+
import org.apache.ranger.plugin.model.RangerRole;
23+
import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
24+
import org.apache.ranger.plugin.util.RangerCommonConstants;
25+
import org.apache.ranger.plugin.util.RangerRoles;
26+
import org.apache.ranger.plugin.util.RangerUserStore;
27+
import org.apache.ranger.ugsyncutil.transform.Mapper;
28+
import org.apache.ranger.ugsyncutil.util.UgsyncCommonConstants;
29+
import org.junit.jupiter.api.MethodOrderer;
30+
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.TestMethodOrder;
32+
import org.junit.jupiter.api.extension.ExtendWith;
33+
import org.mockito.junit.jupiter.MockitoExtension;
34+
35+
import java.util.Collections;
36+
import java.util.HashMap;
37+
import java.util.HashSet;
38+
import java.util.List;
39+
import java.util.Map;
40+
import java.util.Set;
41+
42+
import static org.junit.jupiter.api.Assertions.assertEquals;
43+
import static org.junit.jupiter.api.Assertions.assertNotNull;
44+
import static org.junit.jupiter.api.Assertions.assertTrue;
45+
46+
/**
47+
* @generated by Cursor
48+
* @description <Unit Test for RangerAuthContext class>
49+
*/
50+
@ExtendWith(MockitoExtension.class)
51+
@TestMethodOrder(MethodOrderer.MethodName.class)
52+
public class TestRangerAuthContext {
53+
public static class NoopMapper implements Mapper {
54+
@Override
55+
public void init(String baseProperty, List<String> regexPatterns, String regexSeparator) {
56+
}
57+
58+
@Override
59+
public String transform(String attrValue) {
60+
return attrValue;
61+
}
62+
}
63+
64+
@Test
65+
public void test1_RoleAggregationForUserAndGroups() {
66+
RangerRole roleA = new RangerRole();
67+
roleA.setName("roleA");
68+
roleA.setUsers(Collections.singletonList(new RangerRole.RoleMember("u1", false)));
69+
70+
RangerRole roleB = new RangerRole();
71+
roleB.setName("roleB");
72+
roleB.setGroups(Collections.singletonList(new RangerRole.RoleMember("g1", false)));
73+
74+
RangerRole roleC = new RangerRole();
75+
roleC.setName("roleC");
76+
roleC.setGroups(Collections.singletonList(new RangerRole.RoleMember(RangerPolicyEngine.GROUP_PUBLIC, false)));
77+
78+
Set<RangerRole> rset = new HashSet<>();
79+
rset.add(roleA);
80+
rset.add(roleB);
81+
rset.add(roleC);
82+
83+
RangerRoles roles = new RangerRoles();
84+
roles.setRoleVersion(5L);
85+
roles.setRangerRoles(rset);
86+
87+
RangerAuthContext ctx = new RangerAuthContext(new HashMap<>(), null, roles, new RangerUserStore());
88+
89+
Set<String> rolesOut = ctx.getRolesForUserAndGroups("u1", Collections.singleton("g1"));
90+
assertTrue(rolesOut.contains("roleA"));
91+
assertTrue(rolesOut.contains("roleB"));
92+
assertTrue(rolesOut.contains("roleC"));
93+
assertEquals(5L, ctx.getRoleVersion());
94+
}
95+
96+
@Test
97+
public void test2_OnServiceConfigsUpdateSetsTransformers() {
98+
RangerAuthContext ctx = new RangerAuthContext(new HashMap<>(), null, new RangerRoles(), new RangerUserStore());
99+
Map<String, String> cfg = new HashMap<>();
100+
cfg.put(RangerCommonConstants.PLUGINS_CONF_USERNAME_CASE_CONVERSION_PARAM, "lower");
101+
cfg.put(RangerCommonConstants.PLUGINS_CONF_GROUPNAME_CASE_CONVERSION_PARAM, "upper");
102+
ctx.onServiceConfigsUpdate(cfg);
103+
assertNotNull(ctx.getUserNameCaseConversion());
104+
assertNotNull(ctx.getGroupNameCaseConversion());
105+
assertEquals(UgsyncCommonConstants.CaseConversion.TO_LOWER, ctx.getUserNameCaseConversion());
106+
assertEquals(UgsyncCommonConstants.CaseConversion.TO_UPPER, ctx.getGroupNameCaseConversion());
107+
}
108+
109+
@Test
110+
public void test3_OnServiceConfigsUpdateRegexPatternsAggregation() {
111+
RangerAuthContext ctx = new RangerAuthContext(new HashMap<>(), null, new RangerRoles(), new RangerUserStore());
112+
Map<String, String> cfg = new HashMap<>();
113+
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_USERNAME_HANDLER, this.getClass().getName() + "$NoopMapper");
114+
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_USERNAME, "^(.+)@example\\.com$");
115+
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_USERNAME + ".1", "^user_(.+)$");
116+
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_SEPARATOR, ":");
117+
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_GROUPNAME_HANDLER, this.getClass().getName() + "$NoopMapper");
118+
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_GROUPNAME, "^grp_(.+)$");
119+
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_GROUPNAME + ".1", "^team_(.+)$");
120+
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_SEPARATOR, ":");
121+
122+
ctx.onServiceConfigsUpdate(cfg);
123+
124+
// handlers should be set
125+
assertNotNull(ctx.getUserNameTransformer());
126+
assertNotNull(ctx.getGroupNameTransformer());
127+
}
128+
129+
@Test
130+
public void test4_UserStoreVersionPassThrough() {
131+
RangerUserStore us = new RangerUserStore();
132+
us.setUserStoreVersion(42L);
133+
RangerAuthContext ctx = new RangerAuthContext(new HashMap<>(), null, new RangerRoles(), us);
134+
assertEquals(42L, ctx.getUserStoreVersion());
135+
}
136+
}

0 commit comments

Comments
 (0)