Skip to content

Commit 075d30d

Browse files
RANGER-5347: Test Cases for agents-common Module: Packages (plugin.conditionevaluator, plugin.contextenricher, plugin.contextenricher.externalretrievers, plugin.geo)
1 parent 370edde commit 075d30d

28 files changed

+4360
-186
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.conditionevaluator;
21+
22+
import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition;
23+
import org.apache.ranger.plugin.model.RangerServiceDef;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.MethodOrderer;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.TestMethodOrder;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.mockito.junit.jupiter.MockitoExtension;
30+
31+
import static org.mockito.Mockito.mock;
32+
33+
/**
34+
* @generated by Cursor
35+
* @description <Unit Test for RangerAbstractConditionEvaluator class>
36+
*/
37+
@ExtendWith(MockitoExtension.class)
38+
@TestMethodOrder(MethodOrderer.MethodName.class)
39+
public class TestRangerAbstractConditionEvaluator {
40+
@Test
41+
public void test01_settersAndAlwaysMatchPaths() {
42+
RangerSimpleMatcher matcher = new RangerSimpleMatcher();
43+
44+
RangerServiceDef serviceDef = mock(RangerServiceDef.class);
45+
matcher.setServiceDef(serviceDef);
46+
47+
RangerPolicyItemCondition condition = new RangerPolicyItemCondition("__simple", null);
48+
matcher.setPolicyItemCondition(condition);
49+
matcher.setConditionDef(null);
50+
matcher.init();
51+
52+
Assertions.assertEquals(condition, matcher.getPolicyItemCondition());
53+
}
54+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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.conditionevaluator;
21+
22+
import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition;
23+
import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.MethodOrderer;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.TestMethodOrder;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.mockito.junit.jupiter.MockitoExtension;
30+
31+
import java.util.Arrays;
32+
import java.util.Collections;
33+
34+
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.when;
36+
37+
/**
38+
* @generated by Cursor
39+
* @description <Unit Test for RangerAccessedFromClusterConditions class>
40+
*/
41+
@ExtendWith(MockitoExtension.class)
42+
@TestMethodOrder(MethodOrderer.MethodName.class)
43+
public class TestRangerAccessedFromClusterConditions {
44+
@Test
45+
public void test01_fromCluster_withEmptyList_isAlwaysTrue() {
46+
RangerAccessedFromClusterCondition evaluator = new RangerAccessedFromClusterCondition();
47+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
48+
when(condition.getValues()).thenReturn(Collections.emptyList());
49+
evaluator.setPolicyItemCondition(condition);
50+
evaluator.init();
51+
52+
RangerAccessRequest req = mock(RangerAccessRequest.class);
53+
Assertions.assertTrue(evaluator.isMatched(req));
54+
55+
Assertions.assertTrue(evaluator.isMatched(req));
56+
}
57+
58+
@Test
59+
public void test02_fromCluster_withValues_matchesOnlyIfContained() {
60+
RangerAccessedFromClusterCondition evaluator = new RangerAccessedFromClusterCondition();
61+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
62+
when(condition.getValues()).thenReturn(Arrays.asList("c1", "c2"));
63+
evaluator.setPolicyItemCondition(condition);
64+
evaluator.init();
65+
66+
RangerAccessRequest req = mock(RangerAccessRequest.class);
67+
when(req.getClusterName()).thenReturn("c2");
68+
Assertions.assertTrue(evaluator.isMatched(req));
69+
when(req.getClusterName()).thenReturn("c3");
70+
Assertions.assertFalse(evaluator.isMatched(req));
71+
}
72+
73+
@Test
74+
public void test03_notFromCluster_withEmptyList_alwaysTrue() {
75+
RangerAccessedNotFromClusterCondition evaluator = new RangerAccessedNotFromClusterCondition();
76+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
77+
when(condition.getValues()).thenReturn(Collections.emptyList());
78+
evaluator.setPolicyItemCondition(condition);
79+
evaluator.init();
80+
81+
RangerAccessRequest req = mock(RangerAccessRequest.class);
82+
Assertions.assertTrue(evaluator.isMatched(req));
83+
}
84+
85+
@Test
86+
public void test04_notFromCluster_withValues_trueWhenNotContained() {
87+
RangerAccessedNotFromClusterCondition evaluator = new RangerAccessedNotFromClusterCondition();
88+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
89+
when(condition.getValues()).thenReturn(Arrays.asList("c1", "c2"));
90+
evaluator.setPolicyItemCondition(condition);
91+
evaluator.init();
92+
93+
RangerAccessRequest req = mock(RangerAccessRequest.class);
94+
when(req.getClusterName()).thenReturn("c3");
95+
Assertions.assertTrue(evaluator.isMatched(req));
96+
when(req.getClusterName()).thenReturn("c1");
97+
Assertions.assertFalse(evaluator.isMatched(req));
98+
}
99+
100+
@Test
101+
public void test05_fromClusterType_withValues() {
102+
RangerAccessedFromClusterTypeCondition evaluator = new RangerAccessedFromClusterTypeCondition();
103+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
104+
when(condition.getValues()).thenReturn(Arrays.asList("onprem", "cloud"));
105+
evaluator.setPolicyItemCondition(condition);
106+
evaluator.init();
107+
108+
RangerAccessRequest req = mock(RangerAccessRequest.class);
109+
when(req.getClusterType()).thenReturn("cloud");
110+
Assertions.assertTrue(evaluator.isMatched(req));
111+
when(req.getClusterType()).thenReturn("edge");
112+
Assertions.assertFalse(evaluator.isMatched(req));
113+
}
114+
115+
@Test
116+
public void test06_notFromClusterType_withValues() {
117+
RangerAccessedNotFromClusterTypeCondition evaluator = new RangerAccessedNotFromClusterTypeCondition();
118+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
119+
when(condition.getValues()).thenReturn(Arrays.asList("onprem", "cloud"));
120+
evaluator.setPolicyItemCondition(condition);
121+
evaluator.init();
122+
123+
RangerAccessRequest req = mock(RangerAccessRequest.class);
124+
when(req.getClusterType()).thenReturn("edge");
125+
Assertions.assertTrue(evaluator.isMatched(req));
126+
when(req.getClusterType()).thenReturn("cloud");
127+
Assertions.assertFalse(evaluator.isMatched(req));
128+
}
129+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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.conditionevaluator;
21+
22+
import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition;
23+
import org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef;
24+
import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.MethodOrderer;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.TestMethodOrder;
29+
import org.junit.jupiter.api.extension.ExtendWith;
30+
import org.mockito.junit.jupiter.MockitoExtension;
31+
32+
import java.util.Arrays;
33+
import java.util.Collections;
34+
import java.util.HashMap;
35+
import java.util.Map;
36+
37+
import static org.mockito.Mockito.mock;
38+
import static org.mockito.Mockito.when;
39+
40+
/**
41+
* @generated by Cursor
42+
* @description <Unit Test for RangerContextAttributeValueConditions class>
43+
*/
44+
@ExtendWith(MockitoExtension.class)
45+
@TestMethodOrder(MethodOrderer.MethodName.class)
46+
public class TestRangerContextAttributeValueConditions {
47+
@Test
48+
public void test01_inCondition_attributeMissing_returnsTrue() {
49+
RangerContextAttributeValueInCondition evaluator = new RangerContextAttributeValueInCondition();
50+
51+
RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
52+
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "site"));
53+
evaluator.setConditionDef(conditionDef);
54+
55+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
56+
when(condition.getValues()).thenReturn(Arrays.asList("10", "20"));
57+
evaluator.setPolicyItemCondition(condition);
58+
59+
evaluator.init();
60+
61+
RangerAccessRequest request = mock(RangerAccessRequest.class);
62+
when(request.getContext()).thenReturn(new HashMap<>());
63+
64+
Assertions.assertTrue(evaluator.isMatched(request));
65+
}
66+
67+
@Test
68+
public void test02_inCondition_attributePresent_withMatch_returnsTrue() {
69+
RangerContextAttributeValueInCondition evaluator = new RangerContextAttributeValueInCondition();
70+
71+
RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
72+
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "dept"));
73+
evaluator.setConditionDef(conditionDef);
74+
75+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
76+
when(condition.getValues()).thenReturn(Arrays.asList("ENGG", "PROD"));
77+
evaluator.setPolicyItemCondition(condition);
78+
79+
evaluator.init();
80+
81+
Map<String, Object> ctx = new HashMap<>();
82+
ctx.put("dept", "ENGG");
83+
RangerAccessRequest request = mock(RangerAccessRequest.class);
84+
when(request.getContext()).thenReturn(ctx);
85+
86+
Assertions.assertTrue(evaluator.isMatched(request));
87+
}
88+
89+
@Test
90+
public void test03_inCondition_attributePresent_noMatch_returnsFalse() {
91+
RangerContextAttributeValueInCondition evaluator = new RangerContextAttributeValueInCondition();
92+
93+
RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
94+
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "dept"));
95+
evaluator.setConditionDef(conditionDef);
96+
97+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
98+
when(condition.getValues()).thenReturn(Arrays.asList("ENGG", "PROD"));
99+
evaluator.setPolicyItemCondition(condition);
100+
101+
evaluator.init();
102+
103+
Map<String, Object> ctx = new HashMap<>();
104+
ctx.put("dept", "SALES");
105+
RangerAccessRequest request = mock(RangerAccessRequest.class);
106+
when(request.getContext()).thenReturn(ctx);
107+
108+
Assertions.assertFalse(evaluator.isMatched(request));
109+
}
110+
111+
@Test
112+
public void test04_notInCondition_attributeMissing_returnsTrue() {
113+
RangerContextAttributeValueNotInCondition evaluator = new RangerContextAttributeValueNotInCondition();
114+
115+
RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
116+
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "site"));
117+
evaluator.setConditionDef(conditionDef);
118+
119+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
120+
when(condition.getValues()).thenReturn(Arrays.asList("10", "20"));
121+
evaluator.setPolicyItemCondition(condition);
122+
123+
evaluator.init();
124+
125+
RangerAccessRequest request = mock(RangerAccessRequest.class);
126+
when(request.getContext()).thenReturn(new HashMap<>());
127+
128+
Assertions.assertTrue(evaluator.isMatched(request));
129+
}
130+
131+
@Test
132+
public void test05_notInCondition_attributePresent_withMatch_returnsFalse() {
133+
RangerContextAttributeValueNotInCondition evaluator = new RangerContextAttributeValueNotInCondition();
134+
135+
RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
136+
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "dept"));
137+
evaluator.setConditionDef(conditionDef);
138+
139+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
140+
when(condition.getValues()).thenReturn(Arrays.asList("ENGG", "PROD"));
141+
evaluator.setPolicyItemCondition(condition);
142+
143+
evaluator.init();
144+
145+
Map<String, Object> ctx = new HashMap<>();
146+
ctx.put("dept", "PROD");
147+
RangerAccessRequest request = mock(RangerAccessRequest.class);
148+
when(request.getContext()).thenReturn(ctx);
149+
150+
Assertions.assertFalse(evaluator.isMatched(request));
151+
}
152+
153+
@Test
154+
public void test06_notInCondition_attributePresent_noMatch_returnsTrue() {
155+
RangerContextAttributeValueNotInCondition evaluator = new RangerContextAttributeValueNotInCondition();
156+
157+
RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
158+
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "dept"));
159+
evaluator.setConditionDef(conditionDef);
160+
161+
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
162+
when(condition.getValues()).thenReturn(Arrays.asList("ENGG", "PROD"));
163+
evaluator.setPolicyItemCondition(condition);
164+
165+
evaluator.init();
166+
167+
Map<String, Object> ctx = new HashMap<>();
168+
ctx.put("dept", "SALES");
169+
RangerAccessRequest request = mock(RangerAccessRequest.class);
170+
when(request.getContext()).thenReturn(ctx);
171+
172+
Assertions.assertTrue(evaluator.isMatched(request));
173+
}
174+
}

0 commit comments

Comments
 (0)