Skip to content

Commit 5c72fd1

Browse files
RANGER-5350: Test Cases for agents-common Module: Packages (plugin.util, services.gds, services.tag)
1 parent 370edde commit 5c72fd1

Some content is hidden

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

42 files changed

+4176
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ranger.plugin.util;
19+
20+
import org.junit.jupiter.api.MethodOrderer;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.TestMethodOrder;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.junit.jupiter.MockitoExtension;
25+
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.locks.ReentrantLock;
28+
import java.util.concurrent.locks.ReentrantReadWriteLock;
29+
30+
import static org.junit.jupiter.api.Assertions.assertFalse;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
33+
/**
34+
* @generated by Cursor
35+
* @description <Unit Test for AutoClosableLock class>
36+
*/
37+
@ExtendWith(MockitoExtension.class)
38+
@TestMethodOrder(MethodOrderer.MethodName.class)
39+
public class TestAutoClosableLock {
40+
@Test
41+
public void test01_simpleLockUnlock() {
42+
ReentrantLock lock = new ReentrantLock();
43+
assertFalse(lock.isLocked());
44+
try (AutoClosableLock ignored = new AutoClosableLock(lock)) {
45+
assertTrue(lock.isLocked());
46+
}
47+
assertFalse(lock.isLocked());
48+
}
49+
50+
@Test
51+
public void test02_tryLock_withTimeout_acquired() {
52+
ReentrantLock lock = new ReentrantLock();
53+
try (AutoClosableLock.AutoClosableTryLock tryLock = new AutoClosableLock.AutoClosableTryLock(lock, 50,
54+
TimeUnit.MILLISECONDS)) {
55+
assertTrue(tryLock.isLocked());
56+
}
57+
assertFalse(lock.isLocked());
58+
}
59+
60+
@Test
61+
public void test03_tryWriteLock_acquiredAndReleased() {
62+
ReentrantReadWriteLock rw = new ReentrantReadWriteLock();
63+
try (AutoClosableLock.AutoClosableTryWriteLock tryWrite = new AutoClosableLock.AutoClosableTryWriteLock(rw)) {
64+
assertTrue(tryWrite.isLocked());
65+
}
66+
assertFalse(rw.isWriteLocked());
67+
}
68+
69+
@Test
70+
public void test04_readAndWriteLock_scopes() {
71+
ReentrantReadWriteLock rw = new ReentrantReadWriteLock();
72+
assertFalse(rw.isWriteLocked());
73+
try (AutoClosableLock.AutoClosableReadLock ignored = new AutoClosableLock.AutoClosableReadLock(rw)) {
74+
assertTrue(rw.getReadLockCount() > 0);
75+
}
76+
assertTrue(rw.getReadLockCount() == 0);
77+
78+
try (AutoClosableLock.AutoClosableWriteLock ignored = new AutoClosableLock.AutoClosableWriteLock(rw)) {
79+
assertTrue(rw.isWriteLocked());
80+
}
81+
assertFalse(rw.isWriteLocked());
82+
}
83+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ranger.plugin.util;
19+
20+
import org.junit.jupiter.api.MethodOrderer;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.TestMethodOrder;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.junit.jupiter.MockitoExtension;
25+
26+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
29+
/**
30+
* @generated by Cursor
31+
* @description <Unit Test for DownloadTrigger class>
32+
*/
33+
@ExtendWith(MockitoExtension.class)
34+
@TestMethodOrder(MethodOrderer.MethodName.class)
35+
public class TestDownloadTrigger {
36+
@Test
37+
public void test01_waitAndSignal() throws Exception {
38+
DownloadTrigger trigger = new DownloadTrigger();
39+
40+
Thread waiter = new Thread(() -> assertDoesNotThrow(() -> trigger.waitForCompletion()));
41+
waiter.start();
42+
43+
Thread.sleep(50L);
44+
trigger.signalCompletion();
45+
46+
waiter.join(2000L);
47+
assertTrue(!waiter.isAlive());
48+
}
49+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ranger.plugin.util;
19+
20+
import org.junit.jupiter.api.MethodOrderer;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.TestMethodOrder;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.junit.jupiter.MockitoExtension;
25+
26+
import java.util.concurrent.LinkedBlockingQueue;
27+
import java.util.concurrent.TimeUnit;
28+
29+
import static org.junit.jupiter.api.Assertions.assertNotNull;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
/**
33+
* @generated by Cursor
34+
* @description <Unit Test for DownloaderTask class>
35+
*/
36+
@ExtendWith(MockitoExtension.class)
37+
@TestMethodOrder(MethodOrderer.MethodName.class)
38+
public class TestDownloaderTask {
39+
@Test
40+
public void test01_run_putsTriggerAndWaitsUntilSignaled() throws Exception {
41+
LinkedBlockingQueue<DownloadTrigger> queue = new LinkedBlockingQueue<>();
42+
DownloaderTask task = new DownloaderTask(queue);
43+
44+
Thread t = new Thread(task::run);
45+
t.start();
46+
47+
DownloadTrigger trigger = queue.poll(2, TimeUnit.SECONDS);
48+
assertNotNull(trigger);
49+
50+
Thread.sleep(50L);
51+
trigger.signalCompletion();
52+
53+
t.join(2000L);
54+
assertTrue(!t.isAlive());
55+
}
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ranger.plugin.util;
19+
20+
import org.junit.jupiter.api.MethodOrderer;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.TestMethodOrder;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.junit.jupiter.MockitoExtension;
25+
26+
import javax.script.ScriptEngine;
27+
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
30+
/**
31+
* @generated by Cursor
32+
* @description <Unit Test for GraalScriptEngineCreator class>
33+
*/
34+
@ExtendWith(MockitoExtension.class)
35+
@TestMethodOrder(MethodOrderer.MethodName.class)
36+
public class TestGraalScriptEngineCreator {
37+
@Test
38+
public void test01_getScriptEngine_withNullClassLoader_returnsGracefully() {
39+
GraalScriptEngineCreator creator = new GraalScriptEngineCreator();
40+
ScriptEngine engine = creator.getScriptEngine(null);
41+
// engine may be null if graal.js is not available; ensure no exception and type
42+
// if present
43+
assertTrue(engine == null || engine.getFactory() != null);
44+
}
45+
46+
@Test
47+
public void test02_getScriptEngine_withCustomClassLoader_returnsGracefully() {
48+
ClassLoader cl = this.getClass().getClassLoader();
49+
GraalScriptEngineCreator creator = new GraalScriptEngineCreator();
50+
ScriptEngine engine = creator.getScriptEngine(cl);
51+
assertTrue(engine == null || engine.getFactory() != null);
52+
}
53+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ranger.plugin.util;
19+
20+
import org.junit.jupiter.api.MethodOrderer;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.TestMethodOrder;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.junit.jupiter.MockitoExtension;
25+
26+
import java.util.Arrays;
27+
import java.util.HashMap;
28+
import java.util.HashSet;
29+
import java.util.Set;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
34+
/**
35+
* @generated by Cursor
36+
* @description <Unit Test for GrantRevokeRequest class>
37+
*/
38+
@ExtendWith(MockitoExtension.class)
39+
@TestMethodOrder(MethodOrderer.MethodName.class)
40+
public class TestGrantRevokeRequest {
41+
@Test
42+
public void test01_defaultsAndSetters() {
43+
GrantRevokeRequest req = new GrantRevokeRequest();
44+
req.setGrantor("g");
45+
req.setGrantorGroups(null);
46+
req.setResource(null);
47+
req.setUsers(null);
48+
req.setGroups(null);
49+
req.setRoles(null);
50+
req.setAccessTypes(null);
51+
req.setDelegateAdmin(null);
52+
req.setEnableAudit(null);
53+
req.setReplaceExistingPermissions(null);
54+
req.setIsRecursive(null);
55+
req.setClientIPAddress("1.1.1.1");
56+
req.setClientType("app");
57+
req.setRequestData("rd");
58+
req.setSessionId("sid");
59+
req.setClusterName("cluster");
60+
req.setZoneName("zone");
61+
req.setOwnerUser("owner");
62+
63+
assertEquals("g", req.getGrantor());
64+
assertEquals(0, req.getGrantorGroups().size());
65+
assertEquals(0, req.getResource().size());
66+
assertEquals(0, req.getUsers().size());
67+
assertEquals(0, req.getRoles().size());
68+
assertEquals(0, req.getAccessTypes().size());
69+
assertEquals(Boolean.FALSE, req.getDelegateAdmin());
70+
assertEquals(Boolean.TRUE, req.getEnableAudit());
71+
assertEquals(Boolean.FALSE, req.getReplaceExistingPermissions());
72+
assertEquals(Boolean.FALSE, req.getIsRecursive());
73+
assertNotNull(req.toString());
74+
}
75+
76+
@Test
77+
public void test02_constructorAssignsFields() {
78+
Set<String> ggs = new HashSet<>(Arrays.asList("gg"));
79+
Set<String> us = new HashSet<>(Arrays.asList("u"));
80+
Set<String> gs = new HashSet<>(Arrays.asList("gr"));
81+
Set<String> rs = new HashSet<>(Arrays.asList("rl"));
82+
Set<String> ats = new HashSet<>(Arrays.asList("a"));
83+
GrantRevokeRequest req = new GrantRevokeRequest("g", ggs, new HashMap<>(), us, gs, rs, ats, Boolean.TRUE,
84+
Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, "ip", "cli", "rd", "sid", "cluster", "zone", "owner");
85+
assertEquals("g", req.getGrantor());
86+
assertEquals(Boolean.TRUE, req.getDelegateAdmin());
87+
assertEquals(Boolean.FALSE, req.getEnableAudit());
88+
assertEquals(Boolean.TRUE, req.getReplaceExistingPermissions());
89+
assertEquals(Boolean.TRUE, req.getIsRecursive());
90+
}
91+
}

0 commit comments

Comments
 (0)