Skip to content

Commit 57a883f

Browse files
committed
RANGER-5311: Test Cases for Security-Admin Module: Package[service, service.filter, json, common.db, authentication.unix.jaas, javax.ws.rs.core]
1 parent 00837d0 commit 57a883f

File tree

49 files changed

+4485
-306
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4485
-306
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
package javax.ws.rs.core;
20+
21+
import org.junit.jupiter.api.MethodOrderer;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.TestMethodOrder;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.mockito.junit.jupiter.MockitoExtension;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
import static org.junit.jupiter.api.Assertions.assertSame;
30+
31+
/**
32+
* @generated by Cursor
33+
* @description : Unit Test cases for NoContentException
34+
*/
35+
36+
@ExtendWith(MockitoExtension.class)
37+
@TestMethodOrder(MethodOrderer.MethodName.class)
38+
public class TestNoContentException {
39+
@Test
40+
public void test1_ctor_withMessage() {
41+
NoContentException e = new NoContentException("m");
42+
assertNotNull(e);
43+
assertEquals("m", e.getMessage());
44+
}
45+
46+
@Test
47+
public void test2_ctor_withMessageAndCause() {
48+
Throwable cause = new RuntimeException("c");
49+
NoContentException e = new NoContentException("m", cause);
50+
assertNotNull(e);
51+
assertEquals("m", e.getMessage());
52+
assertSame(cause, e.getCause());
53+
}
54+
55+
@Test
56+
public void test3_ctor_withCauseOnly() {
57+
Throwable cause = new RuntimeException("c");
58+
NoContentException e = new NoContentException(cause);
59+
assertNotNull(e);
60+
assertSame(cause, e.getCause());
61+
}
62+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
package org.apache.ranger.authentication.unix.jaas;
20+
21+
import org.junit.jupiter.api.MethodOrderer;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.TestMethodOrder;
24+
import org.junit.jupiter.api.extension.ExtendWith;
25+
import org.mockito.junit.jupiter.MockitoExtension;
26+
27+
import java.security.Principal;
28+
import java.util.Set;
29+
30+
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
33+
/**
34+
* @generated by Cursor
35+
* @description : Unit Test cases for RoleUserAuthorityGranter
36+
*/
37+
38+
@ExtendWith(MockitoExtension.class)
39+
@TestMethodOrder(MethodOrderer.MethodName.class)
40+
public class TestRoleUserAuthorityGranter {
41+
@Test
42+
public void test1_grant_returnsGroupName_forUnixGroupPrincipal() {
43+
RoleUserAuthorityGranter g = new RoleUserAuthorityGranter();
44+
UnixGroupPrincipal grp = new UnixGroupPrincipal("hadoop");
45+
Set<String> auths = g.grant(grp);
46+
assertEquals(1, auths.size());
47+
assertTrue(auths.contains("hadoop"));
48+
}
49+
50+
@Test
51+
public void test2_grant_returnsROLE_USER_forNonGroupPrincipal() {
52+
RoleUserAuthorityGranter g = new RoleUserAuthorityGranter();
53+
Principal p = () -> "someone"; // lambda Principal
54+
Set<String> auths = g.grant(p);
55+
assertEquals(1, auths.size());
56+
assertTrue(auths.contains("ROLE_USER"));
57+
}
58+
}

security-admin/src/test/java/org/apache/ranger/biz/TestRangerTagDBRetriever.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public void test04_constructor_separateThreadInitialization() throws Exception {
196196
when(tagDefDao.findByServiceId(9L)).thenReturn(new ArrayList<XXTagDef>());
197197

198198
RangerTagDBRetriever r = newRetriever(daoMgr, txMgr, service);
199+
// After separate thread initialization, collections should be non-null (possibly empty)
199200
Assertions.assertNotNull(r.getServiceResources());
200201
Assertions.assertNotNull(r.getTagDefs());
201202
}

security-admin/src/test/java/org/apache/ranger/common/TestTimedExecutor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public void test() throws InterruptedException {
121121
* - at least 3 and no more than 5 tasks must get rejected.
122122
* - at least 3 and no more than 5 tasks must get timed out
123123
*/
124-
int successCount = results.get("success").get();
125-
int timeoutCount = results.get("java.util.concurrent.TimeoutException").get();
126-
int rejectedCount = results.get("java.util.concurrent.RejectedExecutionException").get();
124+
int successCount = results.getOrDefault("success", new AtomicInteger(0)).get();
125+
int timeoutCount = results.getOrDefault("java.util.concurrent.TimeoutException", new AtomicInteger(0)).get();
126+
int rejectedCount = results.getOrDefault("java.util.concurrent.RejectedExecutionException", new AtomicInteger(0)).get();
127127
assertEquals(2, successCount, "success count");
128128
assertTrue(timeoutCount >= 3 && timeoutCount <= 5, "timeout[" + timeoutCount + "]: 3 <= count(timeout) <= 5");
129129
assertTrue(rejectedCount >= 3 && rejectedCount <= 5, "rejected[" + rejectedCount + "]: 3 <= count(timeout) <= 5");
@@ -134,8 +134,8 @@ public void test() throws InterruptedException {
134134
@Test
135135
public void testLocalUncaughtExceptionHandlerDoesNotThrow() {
136136
UncaughtExceptionHandler h = new TimedExecutor.LocalUncaughtExceptionHandler();
137-
// Should not throw despite nulls; but pass a dummy thread and exception
138-
h.uncaughtException(new Thread("t1"), new RuntimeException("boom"));
137+
// Should not throw; pass a dummy thread and exception
138+
h.uncaughtException(Thread.currentThread(), new RuntimeException("boom"));
139139
}
140140

141141
@Test
@@ -144,7 +144,7 @@ public void testTimedTaskSuccess() throws Exception {
144144
TimedExecutor exec = new TimedExecutor();
145145
exec.initialize(cfg);
146146
try {
147-
String v = exec.timedTask(() -> "ok", 1, TimeUnit.SECONDS);
147+
String v = exec.timedTask(() -> "ok", 2, TimeUnit.SECONDS);
148148
Assertions.assertEquals("ok", v);
149149
} finally {
150150
exec.shutdown();

0 commit comments

Comments
 (0)