Skip to content

Commit

Permalink
OAK-11317 : removed conversion to unmodifiableSet for test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Kumar committed Dec 20, 2024
1 parent 166fc2a commit 412cad0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void testGetPrincipalDynamicGroup() throws Exception {

@Test
public void testGetPrincipalInheritedGroups() throws Exception {
Set<ExternalIdentityRef> declared = Collections.unmodifiableSet(CollectionUtils.toSet(idp.getUser(USER_ID).getDeclaredGroups()));
Set<ExternalIdentityRef> declared = CollectionUtils.toSet(idp.getUser(USER_ID).getDeclaredGroups());

for (ExternalIdentityRef ref : declared) {
for (ExternalIdentityRef inheritedGroupRef : idp.getIdentity(ref).getDeclaredGroups()) {
Expand Down Expand Up @@ -526,7 +526,7 @@ public void testFindPrincipalsContainingPercentSign() throws Exception {
sync(externalUser);

Set<? extends Principal> expected = buildExpectedPrincipals("g%r%");
Set<? extends Principal> res = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals("%", PrincipalManager.SEARCH_TYPE_ALL)));
Set<? extends Principal> res = CollectionUtils.toSet(principalProvider.findPrincipals("%", PrincipalManager.SEARCH_TYPE_ALL));
assertEquals(expected, res);
Set<? extends Principal> res2 = CollectionUtils.toSet(principalProvider.findPrincipals("%", false, PrincipalManager.SEARCH_TYPE_ALL, 0, -1));
assertEquals(expected, res2);
Expand All @@ -540,16 +540,16 @@ public void testFindPrincipalsByTypeNotGroup() {

@Test
public void testFindPrincipalsByTypeGroup() throws Exception {
Set<? extends Principal> res = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(PrincipalManager.SEARCH_TYPE_GROUP)));
Set<? extends Principal> res = CollectionUtils.toSet(principalProvider.findPrincipals(PrincipalManager.SEARCH_TYPE_GROUP));
assertEquals(getExpectedAllSearchResult(USER_ID), res);

Set<? extends Principal> res2 = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, -1)));
Set<? extends Principal> res2 = CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, -1));
assertEquals(getExpectedAllSearchResult(USER_ID), res2);
}

@Test
public void testFindPrincipalsByTypeAll() throws Exception {
Set<? extends Principal> res = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(PrincipalManager.SEARCH_TYPE_ALL)));
Set<? extends Principal> res = CollectionUtils.toSet(principalProvider.findPrincipals(PrincipalManager.SEARCH_TYPE_ALL));
assertEquals(getExpectedAllSearchResult(USER_ID), res);
}

Expand All @@ -569,10 +569,10 @@ public void testFindPrincipalsFiltersDuplicates() throws Exception {
}

Iterator<? extends Principal> res = principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_ALL);
assertEquals(expected, Collections.unmodifiableSet(CollectionUtils.toSet(res)));
assertEquals(expected, CollectionUtils.toSet(res));
Iterator<? extends Principal> res2 = principalProvider.findPrincipals("a", false,
PrincipalManager.SEARCH_TYPE_ALL, 0, -1);
assertEquals(expected, Collections.unmodifiableSet(CollectionUtils.toSet(res2)));
assertEquals(expected, CollectionUtils.toSet(res2));
}

@Test
Expand All @@ -597,29 +597,29 @@ public void testFindPrincipalsWithOffset() throws Exception {

long offset = 2;
long expectedSize = (all.size() <= offset) ? 0 : all.size()-offset;
Set<? extends Principal> result = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, offset, -1)));
Set<? extends Principal> result = CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, offset, -1));
assertEquals(expectedSize, result.size());
}

@Test
public void testFindPrincipalsWithOffsetEqualsResultSize() throws Exception {
Set<Principal> all = getExpectedAllSearchResult(USER_ID);

Set<? extends Principal> result = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, all.size(), -1)));
Set<? extends Principal> result = CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, all.size(), -1));
assertTrue(result.isEmpty());
}

@Test
public void testFindPrincipalsWithOffsetExceedsResultSize() throws Exception {
Set<Principal> all = getExpectedAllSearchResult(USER_ID);

Set<? extends Principal> result = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, all.size()+1, -1)));
Set<? extends Principal> result = CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, all.size()+1, -1));
assertTrue(result.isEmpty());
}

@Test
public void testFindPrincipalsWithLimit() {
Set<? extends Principal> result = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, 1)));
Set<? extends Principal> result = CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, 1));
int expectedSize = (hasDynamicGroups()) ? 0 : 1;
assertEquals(expectedSize, result.size());
}
Expand All @@ -628,13 +628,13 @@ public void testFindPrincipalsWithLimit() {
public void testFindPrincipalsWithLimitExceedsResultSize() throws Exception {
Set<Principal> all = getExpectedAllSearchResult(USER_ID);

Set<? extends Principal> result = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, all.size()+1)));
Set<? extends Principal> result = CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, all.size()+1));
assertEquals(all, result);
}

@Test
public void testFindPrincipalsWithZeroLimit() {
Set<? extends Principal> result = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, 0)));
Set<? extends Principal> result = CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, 0, 0));
assertTrue(result.isEmpty());
}

Expand All @@ -644,7 +644,7 @@ public void testFindPrincipalsWithOffsetAndLimit() throws Exception {

long offset = all.size()-1;
long limit = all.size();
Set<? extends Principal> result = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, offset, limit)));
Set<? extends Principal> result = CollectionUtils.toSet(principalProvider.findPrincipals(null, false, PrincipalManager.SEARCH_TYPE_GROUP, offset, limit));
int expectedSize = (hasDynamicGroups()) ? 0 : 1;
assertEquals(expectedSize, result.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.jackrabbit.oak.spi.security.authentication.external.impl.principal;

import java.security.Principal;
import java.util.Collections;
import java.util.Set;

import org.apache.jackrabbit.api.security.principal.GroupPrincipal;
Expand Down Expand Up @@ -86,7 +85,7 @@ public void testGetPrincipalInheritedGroups() throws Exception {
@Test
public void testFindPrincipalsByHintTypeGroup() {
Set<? extends Principal> expected = Set.of(new PrincipalImpl("a"), new PrincipalImpl("aa"), new PrincipalImpl("aaa"));
Set<? extends Principal> res = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_GROUP)));
Set<? extends Principal> res = CollectionUtils.toSet(principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_GROUP));

assertEquals(expected, res);
}
Expand All @@ -98,7 +97,7 @@ public void testFindPrincipalsByHintTypeAll() {
new PrincipalImpl("a"),
new PrincipalImpl("aa"),
new PrincipalImpl("aaa"));
Set<? extends Principal> res = Collections.unmodifiableSet(CollectionUtils.toSet(principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_ALL)));
Set<? extends Principal> res = CollectionUtils.toSet(principalProvider.findPrincipals("a", PrincipalManager.SEARCH_TYPE_ALL));

assertEquals(expected, res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import java.util.Collections;
import java.util.Set;

import static org.apache.jackrabbit.oak.spi.security.authentication.external.impl.SyncHandlerMapping.PARAM_IDP_NAME;
Expand Down Expand Up @@ -73,23 +72,23 @@ public void testAddingServiceWithProperties() {
when(ref.getProperty(PARAM_IDP_NAME)).thenReturn("testIDP");

tracker.addingService(ref);
assertEquals(Set.of("testIDP"), Collections.unmodifiableSet(CollectionUtils.toSet(tracker.getIdpNames("testSH"))));
assertEquals(Set.of("testIDP"), CollectionUtils.toSet(tracker.getIdpNames("testSH")));

ServiceReference ref2 = mock(ServiceReference.class);
when(ref2.getProperty(PARAM_SYNC_HANDLER_NAME)).thenReturn("testSH-2");
when(ref2.getProperty(PARAM_IDP_NAME)).thenReturn("testIDP-2");
tracker.addingService(ref2);

assertEquals(Set.of("testIDP"), Collections.unmodifiableSet(CollectionUtils.toSet(tracker.getIdpNames("testSH"))));
assertEquals(Set.of("testIDP-2"), Collections.unmodifiableSet(CollectionUtils.toSet(tracker.getIdpNames("testSH-2"))));
assertEquals(Set.of("testIDP"), CollectionUtils.toSet(tracker.getIdpNames("testSH")));
assertEquals(Set.of("testIDP-2"), CollectionUtils.toSet(tracker.getIdpNames("testSH-2")));

ServiceReference ref3 = mock(ServiceReference.class);
when(ref3.getProperty(PARAM_SYNC_HANDLER_NAME)).thenReturn("testSH");
when(ref3.getProperty(PARAM_IDP_NAME)).thenReturn("testIDP-3");
tracker.addingService(ref3);

assertEquals(Set.of("testIDP", "testIDP-3"), Collections.unmodifiableSet(CollectionUtils.toSet(tracker.getIdpNames("testSH"))));
assertEquals(Set.of("testIDP-2"), Collections.unmodifiableSet(CollectionUtils.toSet(tracker.getIdpNames("testSH-2"))));
assertEquals(Set.of("testIDP", "testIDP-3"), CollectionUtils.toSet(tracker.getIdpNames("testSH")));
assertEquals(Set.of("testIDP-2"), CollectionUtils.toSet(tracker.getIdpNames("testSH-2")));
}

@Test
Expand Down Expand Up @@ -122,13 +121,13 @@ public void testModifiedServiceWithProperties() {
when(ref.getProperty(PARAM_SYNC_HANDLER_NAME)).thenReturn("testSH");
when(ref.getProperty(PARAM_IDP_NAME)).thenReturn("testIDP-2");
tracker.modifiedService(ref, service);
assertEquals(Set.of("testIDP-2"), Collections.unmodifiableSet(CollectionUtils.toSet(tracker.getIdpNames("testSH"))));
assertEquals(Set.of("testIDP-2"), CollectionUtils.toSet(tracker.getIdpNames("testSH")));

when(ref.getProperty(PARAM_SYNC_HANDLER_NAME)).thenReturn("testSH-3");
when(ref.getProperty(PARAM_IDP_NAME)).thenReturn("testIDP-3");
tracker.modifiedService(ref, service);
assertTrue(Iterables.isEmpty(tracker.getIdpNames("testSH")));
assertEquals(Set.of("testIDP-3"), Collections.unmodifiableSet(CollectionUtils.toSet(tracker.getIdpNames("testSH-3"))));
assertEquals(Set.of("testIDP-3"), CollectionUtils.toSet(tracker.getIdpNames("testSH-3")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.jackrabbit.oak.spi.security.authorization.cug.impl;

import java.security.Principal;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -270,7 +269,7 @@ static void assertNestedCugs(@NotNull Root root, @NotNull RootProvider rootProvi
assertFalse(tree.hasProperty(HIDDEN_NESTED_CUGS));
} else {
assertTrue(tree.hasProperty(HIDDEN_NESTED_CUGS));
assertEquals(Set.of(expectedNestedPaths), Collections.unmodifiableSet(CollectionUtils.toSet(tree.getProperty(HIDDEN_NESTED_CUGS).getValue(Type.PATHS))));
assertEquals(Set.of(expectedNestedPaths), CollectionUtils.toSet(tree.getProperty(HIDDEN_NESTED_CUGS).getValue(Type.PATHS)));

assertTrue(tree.hasProperty(HIDDEN_TOP_CUG_CNT));
assertEquals(Long.valueOf(expectedNestedPaths.length), tree.getProperty(HIDDEN_TOP_CUG_CNT).getValue(Type.LONG));
Expand All @@ -283,7 +282,7 @@ static void assertNestedCugs(@NotNull Root root, @NotNull RootProvider rootProvi
assertFalse(tree.hasProperty(HIDDEN_NESTED_CUGS));
} else {
assertTrue(tree.hasProperty(HIDDEN_NESTED_CUGS));
assertEquals(Set.of(expectedNestedPaths), Collections.unmodifiableSet(CollectionUtils.toSet(tree.getProperty(HIDDEN_NESTED_CUGS).getValue(Type.PATHS))));
assertEquals(Set.of(expectedNestedPaths), CollectionUtils.toSet(tree.getProperty(HIDDEN_NESTED_CUGS).getValue(Type.PATHS)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public void testCreationFromDictionary() {
dict.put("a", "b");

ConfigurationParameters cp = ConfigurationParameters.of(dict);
assertEquals(Collections.unmodifiableSet(CollectionUtils.toSet(Iterators.forEnumeration(dict.keys()))), Set.copyOf(cp.keySet()));
assertEquals(Collections.unmodifiableSet(CollectionUtils.toSet(Iterators.forEnumeration(dict.elements()))), Set.copyOf(cp.values()));
assertEquals(CollectionUtils.toSet(Iterators.forEnumeration(dict.keys())), Set.copyOf(cp.keySet()));
assertEquals(CollectionUtils.toSet(Iterators.forEnumeration(dict.elements())), Set.copyOf(cp.values()));

}

Expand Down Expand Up @@ -463,10 +463,10 @@ public void testInvalidConversionToBoolean() {
@Test
public void testConversionToStringArray() {
String[] stringArray = new String[] {"a", "b"};
Set<String> stringSet = Collections.unmodifiableSet(CollectionUtils.toSet(stringArray));
Set<String> stringSet = CollectionUtils.toSet(stringArray);

TestObject[] testObjectArray = new TestObject[] {new TestObject("a"), new TestObject("b")};
Set<TestObject> testObjectSet = Collections.unmodifiableSet(CollectionUtils.toSet(testObjectArray));
Set<TestObject> testObjectSet = CollectionUtils.toSet(testObjectArray);

String[] defaultStrings = new String[]{"abc", "def", "ghi"};

Expand Down

0 comments on commit 412cad0

Please sign in to comment.