You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2020/01/03 12:07:46 UTC

[syncope] branch 2_1_X updated: Setting up test class for LDAPMembershipPullActions (#148)

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

ilgrosso pushed a commit to branch 2_1_X
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/2_1_X by this push:
     new 67b11d9  Setting up test class for LDAPMembershipPullActions (#148)
67b11d9 is described below

commit 67b11d96e201d1356c239d05814f4ed4dd9694cb
Author: Davide Cortellucci <da...@gmail.com>
AuthorDate: Fri Jan 3 13:07:41 2020 +0100

    Setting up test class for LDAPMembershipPullActions (#148)
---
 core/provisioning-java/pom.xml                     |   5 +
 .../core/provisioning/java/AbstractTest.java       |  10 +
 .../pushpull/LDAPMembershipPullActionsTest.java    | 244 +++++++++++++++++++++
 .../org.mockito.plugins.MockMaker                  |  18 ++
 pom.xml                                            |   1 +
 5 files changed, 278 insertions(+)

diff --git a/core/provisioning-java/pom.xml b/core/provisioning-java/pom.xml
index 6230bbe..4adf26e 100644
--- a/core/provisioning-java/pom.xml
+++ b/core/provisioning-java/pom.xml
@@ -142,6 +142,11 @@ under the License.
       <artifactId>junit-jupiter</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-junit-jupiter</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/core/provisioning-java/src/test/java/org/apache/syncope/core/provisioning/java/AbstractTest.java b/core/provisioning-java/src/test/java/org/apache/syncope/core/provisioning/java/AbstractTest.java
index 186910d..abed343 100644
--- a/core/provisioning-java/src/test/java/org/apache/syncope/core/provisioning/java/AbstractTest.java
+++ b/core/provisioning-java/src/test/java/org/apache/syncope/core/provisioning/java/AbstractTest.java
@@ -24,6 +24,10 @@ import org.apache.syncope.core.provisioning.api.EntitlementsHolder;
 import org.apache.syncope.core.spring.ApplicationContextProvider;
 import org.apache.syncope.core.spring.security.AuthContextUtils;
 import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.MockitoAnnotations;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.springframework.orm.jpa.EntityManagerFactoryUtils;
 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
 
@@ -33,8 +37,14 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
     "classpath:workflowContext.xml",
     "classpath:provisioningTest.xml"
 })
+@ExtendWith(MockitoExtension.class)
 public abstract class AbstractTest {
 
+    @BeforeEach 
+    public void initMocks() {
+        MockitoAnnotations.initMocks(this);
+    }
+
     protected EntityManager entityManager() {
         EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(
                 EntityManagerFactoryUtils.findEntityManagerFactory(
diff --git a/core/provisioning-java/src/test/java/org/apache/syncope/core/provisioning/java/pushpull/LDAPMembershipPullActionsTest.java b/core/provisioning-java/src/test/java/org/apache/syncope/core/provisioning/java/pushpull/LDAPMembershipPullActionsTest.java
new file mode 100644
index 0000000..2b43b3f
--- /dev/null
+++ b/core/provisioning-java/src/test/java/org/apache/syncope/core/provisioning/java/pushpull/LDAPMembershipPullActionsTest.java
@@ -0,0 +1,244 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.core.provisioning.java.pushpull;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.UUID;
+import org.apache.syncope.common.lib.patch.AnyPatch;
+import org.apache.syncope.common.lib.patch.UserPatch;
+import org.apache.syncope.common.lib.to.EntityTO;
+import org.apache.syncope.common.lib.to.GroupTO;
+import org.apache.syncope.common.lib.to.UserTO;
+import org.apache.syncope.common.lib.types.ConnConfPropSchema;
+import org.apache.syncope.common.lib.types.ConnConfProperty;
+import org.apache.syncope.common.lib.types.MatchType;
+import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
+import org.apache.syncope.core.persistence.api.dao.GroupDAO;
+import org.apache.syncope.core.persistence.api.dao.PullMatch;
+import org.apache.syncope.core.persistence.api.entity.AnyType;
+import org.apache.syncope.core.persistence.api.entity.ConnInstance;
+import org.apache.syncope.core.persistence.api.entity.resource.ExternalResource;
+import org.apache.syncope.core.persistence.api.entity.task.ProvisioningTask;
+import org.apache.syncope.core.persistence.api.entity.user.UMembership;
+import org.apache.syncope.core.persistence.api.entity.user.User;
+import org.apache.syncope.core.persistence.jpa.entity.JPAAnyType;
+import org.apache.syncope.core.persistence.jpa.entity.resource.JPAProvision;
+import org.apache.syncope.core.persistence.jpa.entity.user.JPAUMembership;
+import org.apache.syncope.core.persistence.jpa.entity.user.JPAUser;
+import org.apache.syncope.core.provisioning.api.Connector;
+import org.apache.syncope.core.provisioning.api.pushpull.ProvisioningProfile;
+import org.apache.syncope.common.lib.to.ProvisioningReport;
+import org.apache.syncope.core.provisioning.java.AbstractTest;
+import org.identityconnectors.framework.common.objects.Attribute;
+import org.identityconnectors.framework.common.objects.ConnectorObject;
+import org.identityconnectors.framework.common.objects.SyncDelta;
+import org.identityconnectors.framework.common.objects.Uid;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.quartz.JobExecutionException;
+import org.quartz.Scheduler;
+import org.quartz.SchedulerException;
+import org.springframework.scheduling.quartz.SchedulerFactoryBean;
+import org.springframework.test.util.ReflectionTestUtils;
+
+public class LDAPMembershipPullActionsTest extends AbstractTest {
+
+    @Mock
+    private AnyTypeDAO anyTypeDAO;
+
+    @Mock
+    private GroupDAO groupDAO;
+
+    @Mock
+    private InboundMatcher inboundMatcher;
+
+    @InjectMocks
+    private LDAPMembershipPullActions ldapMembershipPullActions;
+
+    @Mock
+    private SyncDelta syncDelta;
+
+    @Mock
+    private ProvisioningProfile<?, ?> profile;
+
+    @Mock
+    private ProvisioningReport result;
+
+    @Mock
+    private Map<String, Set<String>> membershipsAfter;
+
+    @Mock
+    private ProvisioningTask provisioningTask;
+
+    @Mock
+    private ExternalResource externalResource;
+
+    @Mock
+    private Connector connector;
+
+    @Mock
+    private ConnectorObject connectorObj;
+
+    @Mock
+    private ConnInstance connInstance;
+
+    private EntityTO entity;
+
+    private AnyPatch anyPatch;
+
+    private Map<String, Set<String>> membershipsBefore;
+
+    private User user;
+
+    Set<ConnConfProperty> connConfProperties;
+
+    @BeforeEach
+    public void initTest() {
+        List<UMembership> uMembList = new ArrayList<>();
+        UMembership uMembership = new JPAUMembership();
+        user = new JPAUser();
+        uMembership.setLeftEnd(user);
+        ReflectionTestUtils.setField(user, "id", UUID.randomUUID().toString());
+        uMembList.add(uMembership);
+
+        anyPatch = new UserPatch();
+        membershipsBefore = new HashMap<>();
+        ReflectionTestUtils.setField(ldapMembershipPullActions, "membershipsBefore", membershipsBefore);
+        ReflectionTestUtils.setField(ldapMembershipPullActions, "membershipsAfter", membershipsAfter);
+
+        lenient().when(groupDAO.findUMemberships(groupDAO.find(anyString()))).thenReturn(uMembList);
+
+        ConnConfPropSchema connConfPropSchema = new ConnConfPropSchema();
+        connConfPropSchema.setName("testSchemaName");
+        ConnConfProperty connConfProperty = new ConnConfProperty();
+        connConfProperty.setSchema(connConfPropSchema);
+        connConfProperties = new HashSet<>();
+        connConfProperties.add(connConfProperty);
+
+        lenient().when(profile.getTask()).thenReturn(provisioningTask);
+        lenient().when(provisioningTask.getResource()).thenReturn(externalResource);
+        lenient().when(anyTypeDAO.findUser()).thenReturn(new JPAAnyType());
+
+        lenient().when(profile.getConnector()).thenReturn(connector);
+        lenient().when(syncDelta.getObject()).thenReturn(connectorObj);
+        lenient().when(connector.getConnInstance()).thenReturn(connInstance);
+        lenient().when(connInstance.getConf()).thenReturn(connConfProperties);
+    }
+
+    @Test
+    public void beforeUpdateWithGroupTOAndEmptyMemberships() throws JobExecutionException {
+        entity = new GroupTO();
+        entity.setKey(UUID.randomUUID().toString());
+        Set<String> expected = new HashSet<>();
+        expected.add(entity.getKey());
+
+        ldapMembershipPullActions.beforeUpdate(profile, syncDelta, entity, anyPatch);
+
+        assertTrue(entity instanceof GroupTO);
+        assertEquals(1, membershipsBefore.get(user.getKey()).size());
+        assertEquals(expected, membershipsBefore.get(user.getKey()));
+    }
+
+    @Test
+    public void beforeUpdate() throws JobExecutionException {
+        entity = new UserTO();
+        entity.setKey(UUID.randomUUID().toString());
+        Set<String> memb = new HashSet<>();
+        memb.add(entity.getKey());
+        membershipsBefore.put(user.getKey(), memb);
+
+        ldapMembershipPullActions.beforeUpdate(profile, syncDelta, entity, anyPatch);
+
+        assertTrue(!(entity instanceof GroupTO));
+        assertEquals(1, membershipsBefore.get(user.getKey()).size());
+    }
+
+    @Test
+    @SuppressWarnings(value = { "rawtypes", "unchecked" })
+    public void afterWithEmptyAttributes(@Mock Attribute attribute) throws JobExecutionException {
+        entity = new GroupTO();
+        Optional provision = Optional.of(new JPAProvision());
+
+        when(connectorObj.getAttributeByName(anyString())).thenReturn(attribute);
+        when(externalResource.getProvision(any(AnyType.class))).thenReturn(provision);
+
+        ldapMembershipPullActions.after(profile, syncDelta, entity, result);
+
+        assertTrue(entity instanceof GroupTO);
+        assertTrue(provision.isPresent());
+        assertEquals(new LinkedList<>(), attribute.getValue());
+    }
+
+    @Test
+    @SuppressWarnings(value = { "rawtypes", "unchecked" })
+    public void after() throws JobExecutionException {
+        entity = new UserTO();
+        Optional provision = Optional.empty();
+        Optional match = Optional.of(new PullMatch(MatchType.ANY, user));
+        String expectedUid = UUID.randomUUID().toString();
+        Attribute attribute = new Uid(expectedUid);
+        List<Object> expected = new LinkedList<>();
+        expected.add(expectedUid);
+
+        when(connectorObj.getAttributeByName(anyString())).thenReturn(attribute);
+        when(externalResource.getProvision(any(AnyType.class))).thenReturn(provision);
+        when(inboundMatcher.match(any(AnyType.class), anyString(), any(ExternalResource.class), any(Connector.class))).
+                thenReturn(match);
+
+        ldapMembershipPullActions.after(profile, syncDelta, entity, result);
+
+        verify(membershipsAfter).get(anyString());
+        verify(membershipsAfter).put(anyString(), any());
+        assertTrue(!(entity instanceof GroupTO));
+        assertTrue(!provision.isPresent());
+        assertEquals(expected, attribute.getValue());
+        assertTrue(match.isPresent());
+    }
+
+    @Test
+    public void afterAll(
+            @Mock Map<String, Object> jobMap,
+            @Mock SchedulerFactoryBean schedulerFactoryBean,
+            @Mock Scheduler scheduler) throws JobExecutionException, SchedulerException {
+        ReflectionTestUtils.setField(ldapMembershipPullActions, "scheduler", schedulerFactoryBean);
+        when(schedulerFactoryBean.getScheduler()).thenReturn(scheduler);
+
+        ldapMembershipPullActions.afterAll(profile);
+
+        verify(scheduler).scheduleJob(any(), any());
+    }
+
+}
diff --git a/core/provisioning-java/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/core/provisioning-java/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
new file mode 100644
index 0000000..5895d20
--- /dev/null
+++ b/core/provisioning-java/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+mock-maker-inline
diff --git a/pom.xml b/pom.xml
index 744fa92..286c18d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -452,6 +452,7 @@ under the License.
     <h2.version>1.4.200</h2.version>
 
     <junit.version>5.5.2</junit.version>
+
     <mockito.version>3.2.4</mockito.version>
 
     <conf.directory>${project.build.directory}/test-classes</conf.directory>