001/**
002 * Copyright 2011-2013 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015package org.kuali.mobility.security.group.dao;
016
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019import org.junit.AfterClass;
020import org.junit.Before;
021import org.junit.BeforeClass;
022import org.junit.Test;
023import org.junit.runner.RunWith;
024import org.kuali.mobility.security.group.api.Group;
025import org.kuali.mobility.security.group.api.GroupDao;
026import org.kuali.mobility.security.group.entity.GroupImpl;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.beans.factory.annotation.Qualifier;
029import org.springframework.test.context.ContextConfiguration;
030import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
031
032import javax.persistence.EntityManager;
033import javax.persistence.PersistenceContext;
034
035import static org.junit.Assert.assertFalse;
036import static org.junit.Assert.assertTrue;
037
038/**
039 * @author Kuali Mobility Team (mobility.collab@kuali.org)
040 */
041@RunWith(SpringJUnit4ClassRunner.class)
042@ContextConfiguration(value = "classpath:TestSpringBeans.xml")
043public class GroupDaoImplTest {
044        private static final Logger LOG = LoggerFactory.getLogger(GroupDaoImplTest.class);
045
046        private static final String[] GROUP_NAME = {"KME-ADMINISTRATORS","KME-BACKDOOR","TEST-GROUP"};
047
048        @PersistenceContext
049        private EntityManager entityManager;
050
051        @Autowired
052        @Qualifier("kmeGroupDao")
053        private GroupDao dao;
054
055        @BeforeClass
056        public static void setUpClass() throws Exception {
057        }
058
059        @AfterClass
060        public static void tearDownClass() throws Exception {
061        }
062
063        @Before
064        public void preTest() {
065        }
066
067        @Test
068        public void testGetGroupById() {
069                Group group1 = getDao().getGroup(new Long(1));
070                assertTrue("Group is not null.",group1!=null);
071                assertTrue("Group name does not match expected one.",GROUP_NAME[0].equals(group1.getName()));
072        }
073
074        @Test
075        public void testGetGroupByName() {
076                Group group2 = getDao().getGroup(GROUP_NAME[1]);
077                assertTrue("Group is not null.",group2!=null);
078                assertTrue("Group name does not match expected one.", GROUP_NAME[1].equals(group2.getName()));
079        }
080
081        @Test
082        public void testSaveGroup() {
083                Group group3 = new GroupImpl();
084                group3.setName(GROUP_NAME[2]);
085                group3.setDescription("Test description.");
086                assertTrue("Group id is null.", group3.getId() == null);
087                Long id = getDao().saveGroup(group3);
088                assertFalse("Group id is null and should not be.",id==null);
089        }
090
091        @Test
092        public void testSaveNullGroup() {
093                Long id = getDao().saveGroup(null);
094                assertTrue("Saved a null group. How did that happen?",id==null);
095        }
096
097        @Test
098        public void testUpdateGroup() {
099                Group group = getDao().getGroup(new Long(1));
100                assertTrue("Group is not null.",group!=null);
101                group.setDescription("Default Kuali Mobility Administrator Group TEST");
102                Long id = getDao().saveGroup(group);
103                assertTrue("Failed to update the group description",id!=null&&group.getId().compareTo(id)==0);
104        }
105
106        @Test
107        public void testGroupEquals() {
108                Group[] groups = new GroupImpl[2];
109                groups[0] = getDao().getGroup(new Long(2));
110                groups[1] = getDao().getGroup(new Long(2));
111                assertTrue("Groups are not equal and should be.",groups[0].equals(groups[1]));
112                assertTrue("Group is not equal to itself.",groups[0].equals(groups[0]));
113                assertFalse("Groups are equal to null and shouldn't be.",groups[1].equals(null));
114                assertFalse("Group is equal to a String? Shouldn't be.",groups[0].equals(GROUP_NAME[1]));
115        }
116
117        public GroupDao getDao() {
118                return dao;
119        }
120
121        public void setDao(GroupDao dao) {
122                this.dao = dao;
123        }
124
125        public EntityManager getEntityManager() {
126                return entityManager;
127        }
128
129        public void setEntityManager(EntityManager entityManager) {
130                this.entityManager = entityManager;
131        }
132}