syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ilgro...@apache.org
Subject [15/26] syncope git commit: [SYNCOPE-652] Still several things to refine, but it starts taking shape
Date Thu, 13 Aug 2015 15:17:57 GMT
http://git-wip-us.apache.org/repos/asf/syncope/blob/6dfedd8f/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/MultitenancyTest.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/MultitenancyTest.java
b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/MultitenancyTest.java
new file mode 100644
index 0000000..a39de84
--- /dev/null
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/MultitenancyTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.persistence.jpa.inner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.Transformer;
+import org.apache.syncope.common.lib.SyncopeConstants;
+import org.apache.syncope.common.lib.types.CipherAlgorithm;
+import org.apache.syncope.common.lib.types.Entitlement;
+import org.apache.syncope.core.misc.security.SyncopeAuthenticationDetails;
+import org.apache.syncope.core.misc.security.SyncopeGrantedAuthority;
+import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
+import org.apache.syncope.core.persistence.api.dao.RealmDAO;
+import org.apache.syncope.core.persistence.api.dao.UserDAO;
+import org.apache.syncope.core.persistence.api.entity.user.User;
+import org.apache.syncope.core.persistence.jpa.AbstractTest;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.test.context.transaction.TransactionConfiguration;
+import org.springframework.transaction.annotation.Transactional;
+
+@TransactionConfiguration(transactionManager = "TwoTransactionManager")
+@Transactional
+public class MultitenancyTest extends AbstractTest {
+
+    @Autowired
+    private PlainSchemaDAO plainSchemaDAO;
+
+    @Autowired
+    private RealmDAO realmDAO;
+
+    @Autowired
+    private UserDAO userDAO;
+
+    @BeforeClass
+    public static void setAuthContext() {
+        List<GrantedAuthority> authorities = CollectionUtils.collect(Entitlement.values(),
+                new Transformer<String, GrantedAuthority>() {
+
+                    @Override
+                    public GrantedAuthority transform(final String entitlement) {
+                        return new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM);
+                    }
+                }, new ArrayList<GrantedAuthority>());
+
+        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
+                new org.springframework.security.core.userdetails.User(
+                        "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
+        auth.setDetails(new SyncopeAuthenticationDetails("Two"));
+        SecurityContextHolder.getContext().setAuthentication(auth);
+    }
+
+    @AfterClass
+    public static void unsetAuthContext() {
+        SecurityContextHolder.getContext().setAuthentication(null);
+    }
+
+    @Test
+    public void readPlainSchemas() {
+        assertEquals(18, plainSchemaDAO.findAll().size());
+    }
+
+    @Test
+    public void readRealm() {
+        assertEquals(1, realmDAO.findAll().size());
+        assertEquals(realmDAO.getRoot(), realmDAO.findAll().get(0));
+    }
+
+    @Test
+    public void createUser() {
+        assertNull(realmDAO.getRoot().getPasswordPolicy());
+        assertTrue(userDAO.findAll(SyncopeConstants.FULL_ADMIN_REALMS, 1, 1000).isEmpty());
+
+        User user = entityFactory.newEntity(User.class);
+        user.setRealm(realmDAO.getRoot());
+        user.setPassword("password", CipherAlgorithm.SHA256);
+        user.setUsername("username");
+
+        User actual = userDAO.save(user);
+        assertNotNull("expected save to work", actual);
+        assertEquals(0, actual.getPasswordHistory().size());
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/6dfedd8f/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/UserTest.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/UserTest.java
b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/UserTest.java
index a709638..a7e0d83 100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/UserTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/UserTest.java
@@ -142,6 +142,7 @@ public class UserTest extends AbstractTest {
         User user = entityFactory.newEntity(User.class);
         user.setUsername("username");
         user.setRealm(realmDAO.find("/even/two"));
+        user.setCreator("admin");
         user.setCreationDate(new Date());
 
         user.setPassword("pass", CipherAlgorithm.SHA256);
@@ -153,7 +154,7 @@ public class UserTest extends AbstractTest {
             assertNotNull(e);
         }
 
-        user.setPassword("password", CipherAlgorithm.SHA256);
+        user.setPassword("password123", CipherAlgorithm.SHA256);
 
         user.setUsername("username!");
 
@@ -186,6 +187,7 @@ public class UserTest extends AbstractTest {
         User user = entityFactory.newEntity(User.class);
         user.setUsername("username");
         user.setRealm(realmDAO.find("/even/two"));
+        user.setCreator("admin");
         user.setCreationDate(new Date());
 
         user.setPassword("password123", CipherAlgorithm.AES);

http://git-wip-us.apache.org/repos/asf/syncope/blob/6dfedd8f/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/AnyTypeClassTest.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/AnyTypeClassTest.java
b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/AnyTypeClassTest.java
index d0f0213..c9e00bb 100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/AnyTypeClassTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/AnyTypeClassTest.java
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertNotNull;
 
 import static org.junit.Assert.assertTrue;
 
-import javax.persistence.EntityManager;
 import org.apache.syncope.common.lib.types.AttrSchemaType;
 import org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO;
 import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
@@ -43,9 +42,6 @@ public class AnyTypeClassTest extends AbstractTest {
     @Autowired
     private AnyTypeClassDAO anyTypeClassDAO;
 
-    @Autowired
-    private EntityManager entityManager;
-
     @Test
     public void create() {
         PlainSchema newSchema = entityFactory.newEntity(PlainSchema.class);

http://git-wip-us.apache.org/repos/asf/syncope/blob/6dfedd8f/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/GroupTest.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/GroupTest.java
b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/GroupTest.java
index d208106..0777854 100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/GroupTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/GroupTest.java
@@ -63,9 +63,6 @@ import org.springframework.transaction.annotation.Transactional;
 public class GroupTest extends AbstractTest {
 
     @Autowired
-    private EntityManager entityManager;
-
-    @Autowired
     private AnyTypeDAO anyTypeDAO;
 
     @Autowired
@@ -144,7 +141,7 @@ public class GroupTest extends AbstractTest {
      * this test class is architected.
      */
     private Collection<Group> findDynGroupMemberships(final User user) {
-        TypedQuery<Group> query = entityManager.createQuery(
+        TypedQuery<Group> query = entityManager().createQuery(
                 "SELECT e.group FROM " + JPAUDynGroupMembership.class.getSimpleName()
                 + " e WHERE :user MEMBER OF e.users", Group.class);
         query.setParameter("user", user);
@@ -226,7 +223,7 @@ public class GroupTest extends AbstractTest {
 
         groupDAO.flush();
 
-        assertNull(entityManager.find(JPAUDynGroupMembership.class, dynMembershipKey));
+        assertNull(entityManager().find(JPAUDynGroupMembership.class, dynMembershipKey));
 
         dynGroupMemberships = findDynGroupMemberships(user);
         assertTrue(dynGroupMemberships.isEmpty());
@@ -238,7 +235,7 @@ public class GroupTest extends AbstractTest {
      * this test class is architected.
      */
     private List<Group> findDynGroupMemberships(final AnyObject anyObject) {
-        TypedQuery<Group> query = entityManager.createQuery(
+        TypedQuery<Group> query = entityManager().createQuery(
                 "SELECT e.group FROM " + JPAADynGroupMembership.class.getSimpleName()
                 + " e WHERE :anyObject MEMBER OF e.anyObjects", Group.class);
         query.setParameter("anyObject", anyObject);
@@ -319,7 +316,7 @@ public class GroupTest extends AbstractTest {
 
         groupDAO.flush();
 
-        assertNull(entityManager.find(JPAADynGroupMembership.class, dynMembershipKey));
+        assertNull(entityManager().find(JPAADynGroupMembership.class, dynMembershipKey));
 
         dynGroupMemberships = findDynGroupMemberships(anyObject);
         assertTrue(dynGroupMemberships.isEmpty());

http://git-wip-us.apache.org/repos/asf/syncope/blob/6dfedd8f/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/ResourceTest.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/ResourceTest.java
b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/ResourceTest.java
index d092f3d..0e9c8db 100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/ResourceTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/ResourceTest.java
@@ -57,9 +57,6 @@ import org.springframework.transaction.annotation.Transactional;
 public class ResourceTest extends AbstractTest {
 
     @Autowired
-    private EntityManager entityManager;
-
-    @Autowired
     private ExternalResourceDAO resourceDAO;
 
     @Autowired
@@ -279,7 +276,7 @@ public class ResourceTest extends AbstractTest {
         resourceDAO.flush();
 
         for (Long itemId : itemKeys) {
-            assertNull(entityManager.find(JPAMappingItem.class, itemId));
+            assertNull(entityManager().find(JPAMappingItem.class, itemId));
         }
     }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/6dfedd8f/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/RoleTest.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/RoleTest.java
b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/RoleTest.java
index 11b572c..c9c7ef2 100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/RoleTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/RoleTest.java
@@ -26,7 +26,6 @@ import static org.junit.Assert.assertTrue;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
-import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.Transformer;
@@ -51,9 +50,6 @@ import org.springframework.transaction.annotation.Transactional;
 public class RoleTest extends AbstractTest {
 
     @Autowired
-    private EntityManager entityManager;
-
-    @Autowired
     private RoleDAO roleDAO;
 
     @Autowired
@@ -74,7 +70,7 @@ public class RoleTest extends AbstractTest {
      * this test class is architected.
      */
     private Collection<Role> findDynRoleMemberships(final User user) {
-        TypedQuery<Role> query = entityManager.createQuery(
+        TypedQuery<Role> query = entityManager().createQuery(
                 "SELECT e.role FROM " + JPADynRoleMembership.class.getSimpleName()
                 + " e WHERE :user MEMBER OF e.users", Role.class);
         query.setParameter("user", user);
@@ -159,7 +155,7 @@ public class RoleTest extends AbstractTest {
 
         roleDAO.flush();
 
-        assertNull(entityManager.find(JPADynRoleMembership.class, dynMembershipKey));
+        assertNull(entityManager().find(JPADynRoleMembership.class, dynMembershipKey));
 
         dynRoleMemberships = findDynRoleMemberships(user);
         assertTrue(dynRoleMemberships.isEmpty());

http://git-wip-us.apache.org/repos/asf/syncope/blob/6dfedd8f/core/persistence-jpa/src/test/resources/META-INF/persistence-enhance.xml
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/resources/META-INF/persistence-enhance.xml b/core/persistence-jpa/src/test/resources/META-INF/persistence-enhance.xml
index 8407b06..6b72247 100644
--- a/core/persistence-jpa/src/test/resources/META-INF/persistence-enhance.xml
+++ b/core/persistence-jpa/src/test/resources/META-INF/persistence-enhance.xml
@@ -23,7 +23,7 @@ under the License.
                                  http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
              version="2.0">
   
-  <persistence-unit name="syncopePersistenceUnit">
+  <persistence-unit name="Master">
     <mapping-file>META-INF/spring-orm.xml</mapping-file>
     <validation-mode>NONE</validation-mode>    
   </persistence-unit>


Mime
View raw message