← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2459: Unit test for UserGroup.

 

------------------------------------------------------------
revno: 2459
committer: Hieu <hieu.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-01-04 11:11:22 +0700
message:
  Unit test for UserGroup.
added:
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserGroupServiceTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserGroupTest.java
modified:
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserGroupServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserGroupServiceTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserGroupServiceTest.java	2011-01-04 04:11:22 +0000
@@ -0,0 +1,211 @@
+package org.hisp.dhis.user;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Test;
+
+/**
+ * @author Dang Duy Hieu
+ * @version $Id$
+ */
+public class UserGroupServiceTest
+    extends UserGroupTest
+{
+    @Override
+    public void setUpTest()
+        throws Exception
+    {
+        setUpUserGroupTest();
+    }
+
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
+    private void assertEq( char uniqueCharacter, UserGroup userGroup )
+    {
+        assertEquals( "UserGroup" + uniqueCharacter, userGroup.getName() );
+    }
+
+    // -------------------------------------------------------------------------
+    // Tests
+    // -------------------------------------------------------------------------
+
+    @Test
+    public void testAddUserGroup()
+    {
+        Set<User> members = new HashSet<User>();
+
+        members.add( user1 );
+        members.add( user2 );
+        members.add( user3 );
+
+        UserGroup userGroup = createUserGroup( 'A', members );
+
+        userGroupService.addUserGroup( userGroup );
+
+        assertEq( 'A', userGroup );
+        assertNotNull( userGroup.getMembers() );
+        assertEquals( members, userGroup.getMembers() );
+    }
+
+    @Test
+    public void testDeleteUserGroup()
+    {
+        Set<User> members = new HashSet<User>();
+
+        members.add( user1 );
+        members.add( user2 );
+
+        UserGroup userGroup = createUserGroup( 'A', members );
+
+        userGroupService.addUserGroup( userGroup );
+
+        userGroup = userGroupService.getUserGroupByName( "UserGroupA" );
+
+        int id = userGroup.getId();
+
+        assertEq( 'A', userGroup );
+        assertTrue( members.size() == userGroup.getMembers().size() );
+
+        userGroupService.deleteUserGroup( userGroup );
+
+        assertNull( userGroupService.getUserGroup( id ) );
+    }
+
+    @Test
+    public void testUpdateUserGroup()
+    {
+        Set<User> members = new HashSet<User>();
+
+        members.add( user1 );
+        members.add( user3 );
+
+        UserGroup userGroup = createUserGroup( 'A', members );
+
+        userGroupService.addUserGroup( userGroup );
+
+        userGroup = userGroupService.getUserGroupByName( "UserGroupA" );
+
+        int id = userGroup.getId();
+
+        assertEq( 'A', userGroup );
+        assertEquals( members, userGroup.getMembers() );
+
+        userGroup.setName( "UserGroupB" );
+        userGroup.getMembers().add( user2 );
+
+        userGroupService.updateUserGroup( userGroup );
+
+        userGroup = userGroupService.getUserGroup( id );
+
+        assertEq( 'B', userGroup );
+
+        assertEquals( 3, userGroup.getMembers().size() );
+
+        assertTrue( userGroup.getMembers().contains( user1 ) );
+        assertTrue( userGroup.getMembers().contains( user2 ) );
+        assertTrue( userGroup.getMembers().contains( user3 ) );
+    }
+
+    @Test
+    public void testGetAllUserGroups()
+    {
+        List<UserGroup> userGroups = new ArrayList<UserGroup>();
+        
+        Set<User> members = new HashSet<User>();
+
+        members.add( user1 );
+        members.add( user3 );
+
+        UserGroup userGroupA = createUserGroup( 'A', members );
+        userGroups.add( userGroupA );
+        
+        userGroupService.addUserGroup( userGroupA );
+
+        members = new HashSet<User>();
+        
+        members.add( user1 );
+        members.add( user2 );
+        
+        UserGroup userGroupB = createUserGroup( 'B', members );
+        userGroups.add( userGroupB );
+        
+        userGroupService.addUserGroup( userGroupB );
+        
+        assertEquals( userGroupService.getAllUserGroups(), userGroups );
+    }
+    
+    @Test
+    public void testGetUserGroupById()
+    {
+        Set<User> members = new HashSet<User>();
+
+        members.add( user1 );
+        members.add( user2 );
+        members.add( user3 );
+
+        UserGroup userGroup = createUserGroup( 'A', members );
+
+        userGroupService.addUserGroup( userGroup );
+        
+        int id = userGroupService.getUserGroupByName( "UserGroupA" ).getId();
+
+        userGroup = userGroupService.getUserGroup( id );
+        
+        assertEq( 'A', userGroup );
+        assertNotNull( userGroup.getMembers() );
+    }
+    
+    @Test
+    public void testGetUserGroupByName()
+    {
+        Set<User> members = new HashSet<User>();
+
+        members.add( user1 );
+
+        UserGroup userGroup = createUserGroup( 'B', members );
+
+        userGroupService.addUserGroup( userGroup );
+
+        userGroup = userGroupService.getUserGroupByName( "UserGroupB" );
+        
+        assertEq( 'B', userGroup );
+        assertNotNull( userGroup.getMembers() );
+    }
+}

=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserGroupTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserGroupTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserGroupTest.java	2011-01-04 04:11:22 +0000
@@ -0,0 +1,64 @@
+package org.hisp.dhis.user;
+
+/*
+ * Copyright (c) 2004-2008, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.DhisSpringTest;
+
+/**
+ * @author Dang Duy Hieu
+ * @version $Id$
+ */
+public abstract class UserGroupTest
+    extends DhisSpringTest
+{
+    protected UserGroupService userGroupService;
+    
+    protected User user1;
+    
+    protected User user2;
+    
+    protected User user3;
+
+    public void setUpUserGroupTest()
+        throws Exception
+    {
+        userGroupService = (UserGroupService) getBean( UserGroupService.ID );
+
+        // ---------------------------------------------------------------------
+        // Setup User
+        // ---------------------------------------------------------------------
+
+        user1 = createUser( 'A' );
+        user2 = createUser( 'B' );
+        user3 = createUser( 'C' );
+        
+        user1.setId( 1 );
+        user2.setId( 2 );
+        user3.setId( 3 );
+    }
+}

=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2010-12-29 10:06:33 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2011-01-04 04:11:22 +0000
@@ -91,6 +91,7 @@
 import org.hisp.dhis.source.SourceStore;
 import org.hisp.dhis.sqlview.SqlView;
 import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserGroup;
 import org.hisp.dhis.validation.ValidationCriteria;
 import org.hisp.dhis.validation.ValidationCriteriaService;
 import org.hisp.dhis.validation.ValidationRule;
@@ -883,6 +884,16 @@
 
         return user;
     }
+    
+    public static UserGroup createUserGroup( char uniqueCharacter, Set<User> users )
+    {
+        UserGroup userGroup = new UserGroup();
+
+        userGroup.setName( "UserGroup" + uniqueCharacter );
+        userGroup.setMembers( users );
+
+        return userGroup;
+    }
 
     protected static Program createProgram( char uniqueCharacter, Set<ProgramStage> programStages,
         OrganisationUnit organisationUnit )