|
| 1 | +package biz.netcentric.cq.tools.actool.authorizableinstaller.impl; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.mockito.Matchers.any; |
| 5 | +import static org.mockito.Mockito.times; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import java.security.Principal; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Iterator; |
| 12 | + |
| 13 | +import javax.jcr.RepositoryException; |
| 14 | + |
| 15 | +import org.apache.jackrabbit.api.security.user.Authorizable; |
| 16 | +import org.apache.jackrabbit.api.security.user.Group; |
| 17 | +import org.apache.jackrabbit.api.security.user.Query; |
| 18 | +import org.apache.jackrabbit.api.security.user.QueryBuilder; |
| 19 | +import org.apache.jackrabbit.api.security.user.User; |
| 20 | +import org.apache.jackrabbit.api.security.user.UserManager; |
| 21 | +import org.apache.jackrabbit.oak.spi.security.principal.PrincipalImpl; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.mockito.Mock; |
| 26 | +import org.mockito.Mockito; |
| 27 | +import org.mockito.runners.MockitoJUnitRunner; |
| 28 | + |
| 29 | +import biz.netcentric.cq.tools.actool.history.InstallationLogger; |
| 30 | + |
| 31 | +@RunWith(MockitoJUnitRunner.class) |
| 32 | +public class PrefetchedAuthorizablesUserManagerTest { |
| 33 | + |
| 34 | + @Mock |
| 35 | + UserManager userManager; |
| 36 | + |
| 37 | + @Mock |
| 38 | + InstallationLogger installationLogger; |
| 39 | + |
| 40 | + @Mock |
| 41 | + Group group1; |
| 42 | + |
| 43 | + @Mock |
| 44 | + Group group2; |
| 45 | + |
| 46 | + @Mock |
| 47 | + Group group3; |
| 48 | + |
| 49 | + @Mock |
| 50 | + User user1; |
| 51 | + |
| 52 | + @Mock |
| 53 | + User user2; |
| 54 | + |
| 55 | + @Mock |
| 56 | + User userNonCached; |
| 57 | + |
| 58 | + PrefetchedAuthorizablesUserManager prefetchedAuthorizablesUserManager; |
| 59 | + |
| 60 | + @Before |
| 61 | + public void before() throws RepositoryException { |
| 62 | + |
| 63 | + when(group1.getID()).thenReturn("group1"); |
| 64 | + when(group2.getID()).thenReturn("group2"); |
| 65 | + when(group3.getID()).thenReturn("group3"); |
| 66 | + when(user1.getID()).thenReturn("user1"); |
| 67 | + when(user1.getID()).thenReturn("user2"); |
| 68 | + when(userNonCached.getID()).thenReturn("userNonCached"); |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + when(userManager.findAuthorizables(PrefetchedAuthorizablesUserManager.ALL_AUTHORIZABLES_QUERY)).thenReturn(Arrays.asList(group1, group2, group3, user1, user2).iterator()); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testPrefetchedAuthorizablesUserManager() throws RepositoryException { |
| 78 | + prefetchedAuthorizablesUserManager = new PrefetchedAuthorizablesUserManager(userManager, installationLogger); |
| 79 | + |
| 80 | + assertEquals(5, prefetchedAuthorizablesUserManager.getCacheSize()); |
| 81 | + assertEquals(group1, prefetchedAuthorizablesUserManager.getAuthorizable("group1")); |
| 82 | + assertEquals(null, prefetchedAuthorizablesUserManager.getAuthorizable("userNonCached")); |
| 83 | + |
| 84 | + when(userManager.getAuthorizable("userNonCached")).thenReturn(userNonCached); |
| 85 | + assertEquals(userNonCached, prefetchedAuthorizablesUserManager.getAuthorizable("userNonCached")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testDelegation() throws RepositoryException { |
| 90 | + prefetchedAuthorizablesUserManager = new PrefetchedAuthorizablesUserManager(userManager, installationLogger); |
| 91 | + |
| 92 | + String testuser = "test"; |
| 93 | + PrincipalImpl testPrincipal = new PrincipalImpl(testuser); |
| 94 | + prefetchedAuthorizablesUserManager.getAuthorizable(testPrincipal); |
| 95 | + verify(userManager, times(1)).getAuthorizable(testPrincipal); |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + String userPath = "/home/users/path"; |
| 100 | + prefetchedAuthorizablesUserManager.getAuthorizableByPath(userPath); |
| 101 | + verify(userManager, times(1)).getAuthorizableByPath(userPath); |
| 102 | + |
| 103 | + String relPath = "profile/test"; |
| 104 | + String testVal = "testval"; |
| 105 | + prefetchedAuthorizablesUserManager.findAuthorizables(relPath, testVal); |
| 106 | + verify(userManager, times(1)).findAuthorizables(relPath, testVal); |
| 107 | + prefetchedAuthorizablesUserManager.findAuthorizables(relPath, testVal, UserManager.SEARCH_TYPE_AUTHORIZABLE); |
| 108 | + verify(userManager, times(1)).findAuthorizables(relPath, testVal, UserManager.SEARCH_TYPE_AUTHORIZABLE); |
| 109 | + |
| 110 | + Query testQuery = new Query() { |
| 111 | + public <T> void build(QueryBuilder<T> builder) { |
| 112 | + // fetch all authorizables |
| 113 | + } |
| 114 | + }; |
| 115 | + prefetchedAuthorizablesUserManager.findAuthorizables(testQuery); |
| 116 | + verify(userManager, times(1)).findAuthorizables(testQuery); |
| 117 | + |
| 118 | + String testpw = "pw"; |
| 119 | + prefetchedAuthorizablesUserManager.createUser(testuser, testpw); |
| 120 | + verify(userManager, times(1)).createUser(testuser, testpw); |
| 121 | + |
| 122 | + prefetchedAuthorizablesUserManager.createUser(testuser, testpw, testPrincipal, userPath); |
| 123 | + verify(userManager, times(1)).createUser(testuser, testpw, testPrincipal, userPath); |
| 124 | + |
| 125 | + prefetchedAuthorizablesUserManager.createSystemUser(testuser, userPath); |
| 126 | + verify(userManager, times(1)).createSystemUser(testuser, userPath); |
| 127 | + |
| 128 | + prefetchedAuthorizablesUserManager.createGroup(testuser); |
| 129 | + verify(userManager, times(1)).createGroup(testuser); |
| 130 | + |
| 131 | + prefetchedAuthorizablesUserManager.createGroup(testPrincipal); |
| 132 | + verify(userManager, times(1)).createGroup(testPrincipal); |
| 133 | + |
| 134 | + prefetchedAuthorizablesUserManager.createGroup(testPrincipal, userPath); |
| 135 | + verify(userManager, times(1)).createGroup(testPrincipal, userPath); |
| 136 | + |
| 137 | + prefetchedAuthorizablesUserManager.createGroup(testuser, testPrincipal, userPath); |
| 138 | + verify(userManager, times(1)).createGroup(testuser, testPrincipal, userPath); |
| 139 | + |
| 140 | + prefetchedAuthorizablesUserManager.autoSave(true); |
| 141 | + verify(userManager, times(1)).autoSave(true); |
| 142 | + |
| 143 | + prefetchedAuthorizablesUserManager.isAutoSave(); |
| 144 | + verify(userManager, times(1)).isAutoSave(); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +} |
0 commit comments