This is an automated email from the ASF dual-hosted git repository.

angela pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 9c1fd98d68 OAK-10471 Implement ConflictHandler for 
UserPrincipalProvider Cache - fix tests (#1154)
9c1fd98d68 is described below

commit 9c1fd98d689dd798b5b7063eb46b4df54f0830d7
Author: Nicola Scendoni <[email protected]>
AuthorDate: Tue Oct 17 10:59:01 2023 +0200

    OAK-10471 Implement ConflictHandler for UserPrincipalProvider Cache - fix 
tests (#1154)
---
 .../security/user/CacheConflictHandlerTest.java    | 134 ++++++---------------
 1 file changed, 37 insertions(+), 97 deletions(-)

diff --git 
a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheConflictHandlerTest.java
 
b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheConflictHandlerTest.java
index d32bf4813d..736db26381 100644
--- 
a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheConflictHandlerTest.java
+++ 
b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/CacheConflictHandlerTest.java
@@ -19,134 +19,74 @@
 
 package org.apache.jackrabbit.oak.security.user;
 
-import org.apache.jackrabbit.api.security.user.Group;
 import org.apache.jackrabbit.oak.AbstractSecurityTest;
-import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.PropertyState;
-import org.apache.jackrabbit.oak.api.Root;
-import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.api.Type;
-import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
-import org.apache.jackrabbit.oak.spi.security.authentication.SystemSubject;
-import org.apache.jackrabbit.oak.spi.security.principal.PrincipalConfiguration;
-import org.apache.jackrabbit.oak.spi.security.principal.PrincipalProvider;
-import org.apache.jackrabbit.oak.spi.security.user.UserConfiguration;
+import org.apache.jackrabbit.oak.plugins.memory.PropertyBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
-import org.jetbrains.annotations.NotNull;
 import org.junit.Test;
 
-import javax.security.auth.Subject;
-import java.security.Principal;
-import java.security.PrivilegedExceptionAction;
-import java.util.Set;
-import java.util.UUID;
-
 import static 
org.apache.jackrabbit.oak.security.user.CacheConstants.REP_EXPIRATION;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 public class CacheConflictHandlerTest extends AbstractSecurityTest {
 
-    static final String PARAM_CACHE_EXPIRATION = "cacheExpiration";
-
-    @Override
-    public void before() throws Exception {
-        super.before();
-
-        String groupId = "testGroup" + UUID.randomUUID();
-        @NotNull Group testGroup = getUserManager(root).createGroup(groupId);
-        testGroup.addMember(getTestUser());
-
-        String groupId2 = "testGroup" + UUID.randomUUID() + "2";
-        @NotNull Group testGroup2 = getUserManager(root).createGroup(groupId2);
-        testGroup.addMember(testGroup2);
-
-        String groupId3 = "testGroup" + UUID.randomUUID() + "3";
-        @NotNull Group testGroup3 = getUserManager(root).createGroup(groupId3);
-
-        root.commit();
-    }
-
-    private Tree getCacheTree(Root root) throws Exception {
-        return getCacheTree(root, getTestUser().getPath());
-    }
-
-    private Tree getCacheTree(Root root, String authorizablePath) {
-        return root.getTree(authorizablePath + '/' + CacheConstants.REP_CACHE);
-    }
-
-    @Override
-    protected ConfigurationParameters getSecurityConfigParameters() {
-        return ConfigurationParameters.of(
-                UserConfiguration.NAME,
-                ConfigurationParameters.of(PARAM_CACHE_EXPIRATION, 3600 * 1000)
-        );
-    }
-
     @Test
-    public void testChangeChangedPropertyLower() throws Exception {
-
-        PrincipalConfiguration pc = getConfig(PrincipalConfiguration.class);
-
-        Root oursRoot = Subject.doAs(SystemSubject.INSTANCE, 
(PrivilegedExceptionAction<ContentSession>) () -> login(null)).getLatestRoot();
-        Root theirsRoot = Subject.doAs(SystemSubject.INSTANCE, 
(PrivilegedExceptionAction<ContentSession>) () -> login(null)).getLatestRoot();
-
-        PrincipalProvider oursPP = pc.getPrincipalProvider(oursRoot, 
namePathMapper);
-        PrincipalProvider theirsPP = pc.getPrincipalProvider(theirsRoot, 
namePathMapper);
-
-        // set of principals that read from user + membership-provider -> 
cache being filled
-        oursPP.getPrincipals(getTestUser().getID());
-        assertTrue(getCacheTree(oursRoot).exists());
-        
getCacheTree(oursRoot).getProperty("rep:expiration").getValue(Type.LONG).longValue();
+    public void testChangeChangedPropertyTheirs() {
+        NodeBuilder parent = mock(NodeBuilder.class);
 
-        theirsPP.getPrincipals(getTestUser().getID());
-        assertTrue(getCacheTree(theirsRoot).exists());
-        long theirExpiration = 
getCacheTree(theirsRoot).getProperty("rep:expiration").getValue(Type.LONG).longValue();
+        PropertyState ours = mock(PropertyState.class);
+        PropertyState base = mock(PropertyState.class);
+        PropertyState theirs = mock(PropertyState.class);
 
+        when(ours.getName()).thenReturn(REP_EXPIRATION);
+        when(base.getName()).thenReturn(REP_EXPIRATION);
+        when(theirs.getName()).thenReturn(REP_EXPIRATION);
 
-        Tree ourCache = getCacheTree(oursRoot);
-        ourCache.setProperty(REP_EXPIRATION, 2);
-        oursRoot.commit(CacheValidatorProvider.asCommitAttributes());
+        when(ours.getValue(Type.LONG)).thenReturn(1000L);
+        when(base.getValue(Type.LONG)).thenReturn(500L);
+        when(theirs.getValue(Type.LONG)).thenReturn(2000L);
 
-        root.commit();
-        
assertEquals(getCacheTree(root).getProperty(REP_EXPIRATION).getValue(Type.LONG).longValue(),
 theirExpiration);
+        CacheConflictHandler handler = new CacheConflictHandler();
+        assertEquals(CacheConflictHandler.Resolution.MERGED, 
handler.changeChangedProperty(parent, ours, theirs, base));
+        PropertyBuilder<Long> merged = PropertyBuilder.scalar(Type.LONG);
+        merged.setName(CacheConstants.REP_EXPIRATION);
+        merged.setValue(2000L);
+        verify(parent).setProperty(merged.getPropertyState());
 
     }
 
     @Test
-    public void testChangeChangedPropertyHigher() throws Exception {
-
-        PrincipalConfiguration pc = getConfig(PrincipalConfiguration.class);
-
-        Root oursRoot = Subject.doAs(SystemSubject.INSTANCE, 
(PrivilegedExceptionAction<ContentSession>) () -> login(null)).getLatestRoot();
-        Root theirsRoot = Subject.doAs(SystemSubject.INSTANCE, 
(PrivilegedExceptionAction<ContentSession>) () -> login(null)).getLatestRoot();
+    public void testChangeChangedPropertyOur() {
 
-        PrincipalProvider oursPP = pc.getPrincipalProvider(oursRoot, 
namePathMapper);
-        PrincipalProvider theirsPP = pc.getPrincipalProvider(theirsRoot, 
namePathMapper);
-
-        // set of principals that read from user + membership-provider -> 
cache being filled
-        Set<? extends Principal> ourPrincipals = 
oursPP.getPrincipals(getTestUser().getID());
-        assertTrue(getCacheTree(oursRoot).exists());
-        
getCacheTree(oursRoot).getProperty("rep:expiration").getValue(Type.LONG).longValue();
+        NodeBuilder parent = mock(NodeBuilder.class);
 
-        Set<? extends Principal> theirPrincipals = 
theirsPP.getPrincipals(getTestUser().getID());
-        assertTrue(getCacheTree(theirsRoot).exists());
-        long theirExpiration = 
getCacheTree(theirsRoot).getProperty("rep:expiration").getValue(Type.LONG).longValue();
+        PropertyState ours = mock(PropertyState.class);
+        PropertyState base = mock(PropertyState.class);
+        PropertyState theirs = mock(PropertyState.class);
 
+        when(ours.getName()).thenReturn(REP_EXPIRATION);
+        when(base.getName()).thenReturn(REP_EXPIRATION);
+        when(theirs.getName()).thenReturn(REP_EXPIRATION);
 
-        Tree ourCache = getCacheTree(oursRoot);
-        ourCache.setProperty(REP_EXPIRATION, theirExpiration + 1000);
-        oursRoot.commit(CacheValidatorProvider.asCommitAttributes());
+        when(ours.getValue(Type.LONG)).thenReturn(2000L);
+        when(base.getValue(Type.LONG)).thenReturn(500L);
+        when(theirs.getValue(Type.LONG)).thenReturn(1000L);
 
-        root.commit();
-        
assertEquals(getCacheTree(root).getProperty(REP_EXPIRATION).getValue(Type.LONG).longValue(),
 theirExpiration + 1000);
+        CacheConflictHandler handler = new CacheConflictHandler();
+        assertEquals(CacheConflictHandler.Resolution.MERGED, 
handler.changeChangedProperty(parent, ours, theirs, base));
+        PropertyBuilder<Long> merged = PropertyBuilder.scalar(Type.LONG);
+        merged.setName(CacheConstants.REP_EXPIRATION);
+        merged.setValue(2000L);
+        verify(parent).setProperty(merged.getPropertyState());
 
     }
 
     @Test
-    public void testChangeChangedPropertyBaseHigher() {
+    public void testChangeChangedPropertyBase() {
         NodeBuilder parent = mock(NodeBuilder.class);
 
         PropertyState ours = mock(PropertyState.class);

Reply via email to