← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21800: Implemented PreheatService for id objects, supports UID and CODE identifiers

 

------------------------------------------------------------
revno: 21800
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2016-01-25 10:56:13 +0700
message:
  Implemented PreheatService for id objects, supports UID and CODE identifiers
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatParams.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java
  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
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatParams.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatParams.java	2016-01-21 09:05:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatParams.java	2016-01-25 03:56:13 +0000
@@ -28,10 +28,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Collection;
+import org.hisp.dhis.common.IdentifiableObject;
+
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -42,9 +44,9 @@
 
     private PreheatIdentifier preheatIdentifier = PreheatIdentifier.UID;
 
-    private Collection<Class<?>> classes = new ArrayList<>();
+    private Set<Class<? extends IdentifiableObject>> classes = new HashSet<>();
 
-    private Map<Class<?>, Collection<String>> references = new HashMap<>();
+    private Map<Class<? extends IdentifiableObject>, Set<String>> references = new HashMap<>();
 
     public PreheatParams()
     {
@@ -72,23 +74,23 @@
         return this;
     }
 
-    public Collection<Class<?>> getClasses()
+    public Set<Class<? extends IdentifiableObject>> getClasses()
     {
         return classes;
     }
 
-    public PreheatParams setClasses( Collection<Class<?>> classes )
+    public PreheatParams setClasses( Set<Class<? extends IdentifiableObject>> classes )
     {
         this.classes = classes;
         return this;
     }
 
-    public Map<Class<?>, Collection<String>> getReferences()
+    public Map<Class<? extends IdentifiableObject>, Set<String>> getReferences()
     {
         return references;
     }
 
-    public PreheatParams setReferences( Map<Class<?>, Collection<String>> references )
+    public PreheatParams setReferences( Map<Class<? extends IdentifiableObject>, Set<String>> references )
     {
         this.references = references;
         return this;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-01-22 05:01:07 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/PreheatService.java	2016-01-25 03:56:13 +0000
@@ -32,6 +32,7 @@
 
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -59,5 +60,5 @@
      * @param identifier Identifier to collect
      * @return Maps classes to collections of identifiers
      */
-    Map<Class<? extends IdentifiableObject>, List<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier );
+    Map<Class<? extends IdentifiableObject>, Set<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2016-01-25 03:56:13 +0000
@@ -681,6 +681,7 @@
         return "{" +
             "\"id\":\"" + id + "\", " +
             "\"uid\":\"" + uid + "\", " +
+            "\"code\":\"" + code + "\", " +
             "\"created\":\"" + created + "\", " +
             "\"lastUpdated\":\"" + lastUpdated + "\", " +
             "\"surname\":\"" + surname + "\", " +

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-01-22 05:01:07 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/preheat/DefaultPreheatService.java	2016-01-25 03:56:13 +0000
@@ -29,6 +29,7 @@
  */
 
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.PropertyType;
 import org.hisp.dhis.schema.Schema;
@@ -37,11 +38,12 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -53,10 +55,43 @@
     @Autowired
     private SchemaService schemaService;
 
+    @Autowired
+    private IdentifiableObjectManager manager;
+
     @Override
     public Preheat preheat( PreheatParams params )
     {
-        return null;
+        Preheat preheat = new Preheat();
+
+        if ( PreheatMode.ALL == params.getPreheatMode() )
+        {
+            for ( Class<? extends IdentifiableObject> klass : params.getClasses() )
+            {
+                List<? extends IdentifiableObject> objects = manager.getAllNoAcl( klass ); // should we use getAll here? are we allowed to reference unshared objects?
+                preheat.put( params.getPreheatIdentifier(), objects );
+            }
+        }
+        else if ( PreheatMode.REFERENCE == params.getPreheatMode() )
+        {
+            for ( Class<? extends IdentifiableObject> klass : params.getReferences().keySet() )
+            {
+                Collection<String> identifiers = params.getReferences().get( klass );
+
+                if ( PreheatIdentifier.UID == params.getPreheatIdentifier() )
+                {
+                    List<? extends IdentifiableObject> objects = manager.getByUid( klass, identifiers );
+                    preheat.put( params.getPreheatIdentifier(), objects );
+                }
+                else if ( PreheatIdentifier.CODE == params.getPreheatIdentifier() )
+                {
+                    List<? extends IdentifiableObject> objects = manager.getByCode( klass, identifiers );
+                    System.err.println( "objects: " + objects );
+                    preheat.put( params.getPreheatIdentifier(), objects );
+                }
+            }
+        }
+
+        return preheat;
     }
 
     @Override
@@ -84,9 +119,9 @@
 
     @Override
     @SuppressWarnings( "unchecked" )
-    public Map<Class<? extends IdentifiableObject>, List<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier )
+    public Map<Class<? extends IdentifiableObject>, Set<String>> scanObjectForReferences( Object object, PreheatIdentifier identifier )
     {
-        Map<Class<? extends IdentifiableObject>, List<String>> map = new HashMap<>();
+        Map<Class<? extends IdentifiableObject>, Set<String>> map = new HashMap<>();
 
         if ( object == null )
         {
@@ -102,7 +137,7 @@
             if ( !p.isCollection() )
             {
                 Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getKlass();
-                if ( !map.containsKey( klass ) ) map.put( klass, new ArrayList<>() );
+                if ( !map.containsKey( klass ) ) map.put( klass, new HashSet<>() );
                 Object reference = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
 
                 if ( reference != null )
@@ -128,7 +163,7 @@
             else
             {
                 Class<? extends IdentifiableObject> klass = (Class<? extends IdentifiableObject>) p.getItemKlass();
-                if ( !map.containsKey( klass ) ) map.put( klass, new ArrayList<>() );
+                if ( !map.containsKey( klass ) ) map.put( klass, new HashSet<>() );
                 Collection<IdentifiableObject> reference = ReflectionUtils.invokeMethod( object, p.getGetterMethod() );
 
                 for ( IdentifiableObject identifiableObject : reference )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-01-22 05:28:36 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/preheat/PreheatServiceTest.java	2016-01-25 03:56:13 +0000
@@ -28,9 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 import org.hisp.dhis.dataelement.DataElementGroup;
@@ -40,11 +41,11 @@
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import java.util.List;
+import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -55,6 +56,9 @@
     @Autowired
     private PreheatService preheatService;
 
+    @Autowired
+    private IdentifiableObjectManager manager;
+
     @Test( expected = PreheatException.class )
     public void testValidateAllFail()
     {
@@ -82,7 +86,7 @@
     public void testValidateRef()
     {
         PreheatParams params = new PreheatParams().setPreheatMode( PreheatMode.REFERENCE );
-        params.getReferences().put( DataElement.class, Lists.newArrayList( "ID1", "ID2" ) );
+        params.getReferences().put( DataElement.class, Sets.newHashSet( "ID1", "ID2" ) );
 
         preheatService.validate( params );
     }
@@ -93,7 +97,7 @@
         DataElement dataElement = new DataElement( "DataElementA" );
         dataElement.setAutoFields();
 
-        Map<Class<? extends IdentifiableObject>, List<String>> references = preheatService.scanObjectForReferences( dataElement, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.scanObjectForReferences( dataElement, PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( OptionSet.class ) );
         assertTrue( references.containsKey( LegendSet.class ) );
@@ -107,7 +111,7 @@
         DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
         dataElementGroup.setAutoFields();
 
-        Map<Class<? extends IdentifiableObject>, List<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertTrue( references.containsKey( User.class ) );
@@ -136,7 +140,7 @@
 
         dataElementGroup.setUser( user );
 
-        Map<Class<? extends IdentifiableObject>, List<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertTrue( references.containsKey( User.class ) );
@@ -147,7 +151,8 @@
         assertTrue( references.get( DataElement.class ).contains( de1.getUid() ) );
         assertTrue( references.get( DataElement.class ).contains( de2.getUid() ) );
         assertTrue( references.get( DataElement.class ).contains( de3.getUid() ) );
-        assertEquals( user.getUid(), references.get( User.class ).get( 0 ) );
+
+        assertTrue( references.get( User.class ).contains( user.getUid() ) );
     }
 
     @Test
@@ -176,7 +181,7 @@
 
         dataElementGroup.setUser( user );
 
-        Map<Class<? extends IdentifiableObject>, List<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.CODE );
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.CODE );
 
         assertTrue( references.containsKey( DataElement.class ) );
         assertTrue( references.containsKey( User.class ) );
@@ -188,4 +193,195 @@
         assertTrue( references.get( DataElement.class ).contains( de2.getCode() ) );
         assertTrue( references.get( DataElement.class ).contains( de3.getCode() ) );
     }
+
+    @Test
+    @SuppressWarnings( "unchecked" )
+    public void testPreheatAllUID()
+    {
+        DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
+        dataElementGroup.setAutoFields();
+
+        DataElement de1 = createDataElement( 'A' );
+        DataElement de2 = createDataElement( 'B' );
+        DataElement de3 = createDataElement( 'C' );
+
+        manager.save( de1 );
+        manager.save( de2 );
+        manager.save( de3 );
+
+        User user = createUser( 'A' );
+        manager.save( user );
+
+        dataElementGroup.addDataElement( de1 );
+        dataElementGroup.addDataElement( de2 );
+        dataElementGroup.addDataElement( de3 );
+
+        dataElementGroup.setUser( user );
+        manager.save( dataElementGroup );
+
+        PreheatParams params = new PreheatParams();
+        params.setPreheatMode( PreheatMode.ALL );
+        params.setClasses( Sets.newHashSet( DataElement.class, DataElementGroup.class, User.class ) );
+
+        preheatService.validate( params );
+        Preheat preheat = preheatService.preheat( params );
+
+        assertFalse( preheat.isEmpty() );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID, DataElement.class ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID, DataElementGroup.class ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID, User.class ) );
+
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de1.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de2.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de3.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElementGroup.class, dataElementGroup.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, User.class, user.getUid() ) );
+    }
+
+    @Test
+    @SuppressWarnings( "unchecked" )
+    public void testPreheatReferenceUID()
+    {
+        DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
+        dataElementGroup.setAutoFields();
+
+        DataElement de1 = createDataElement( 'A' );
+        DataElement de2 = createDataElement( 'B' );
+        DataElement de3 = createDataElement( 'C' );
+
+        manager.save( de1 );
+        manager.save( de2 );
+        manager.save( de3 );
+
+        User user = createUser( 'A' );
+        manager.save( user );
+
+        dataElementGroup.addDataElement( de1 );
+        dataElementGroup.addDataElement( de2 );
+        dataElementGroup.addDataElement( de3 );
+
+        dataElementGroup.setUser( user );
+        manager.save( dataElementGroup );
+
+        PreheatParams params = new PreheatParams();
+        params.setPreheatMode( PreheatMode.REFERENCE );
+
+        Map<Class<? extends IdentifiableObject>, Set<String>> referenceMap = new HashMap<>();
+        referenceMap.put( DataElement.class, Sets.newHashSet( de1.getUid(), de2.getUid() ) );
+        referenceMap.put( User.class, Sets.newHashSet( user.getUid() ) );
+        params.setReferences( referenceMap );
+
+        preheatService.validate( params );
+        Preheat preheat = preheatService.preheat( params );
+
+        assertFalse( preheat.isEmpty() );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID, DataElement.class ) );
+        assertTrue( preheat.isEmpty( PreheatIdentifier.UID, DataElementGroup.class ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID, User.class ) );
+
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de1.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de2.getUid() ) );
+        assertFalse( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de3.getUid() ) );
+        assertFalse( preheat.containsKey( PreheatIdentifier.UID, DataElementGroup.class, dataElementGroup.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, User.class, user.getUid() ) );
+    }
+
+    @Test
+    @SuppressWarnings( "unchecked" )
+    public void testPreheatReferenceCODE()
+    {
+        DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
+        dataElementGroup.setAutoFields();
+
+        DataElement de1 = createDataElement( 'A' );
+        DataElement de2 = createDataElement( 'B' );
+        DataElement de3 = createDataElement( 'C' );
+
+        manager.save( de1 );
+        manager.save( de2 );
+        manager.save( de3 );
+
+        User user = createUser( 'A' );
+        manager.save( user );
+
+        dataElementGroup.addDataElement( de1 );
+        dataElementGroup.addDataElement( de2 );
+        dataElementGroup.addDataElement( de3 );
+
+        dataElementGroup.setUser( user );
+        manager.save( dataElementGroup );
+
+        PreheatParams params = new PreheatParams();
+        params.setPreheatIdentifier( PreheatIdentifier.CODE );
+        params.setPreheatMode( PreheatMode.REFERENCE );
+
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = new HashMap<>();
+        references.put( DataElement.class, Sets.newHashSet( de1.getCode(), de2.getCode() ) );
+        references.put( User.class, Sets.newHashSet( user.getCode() ) );
+        params.setReferences( references );
+
+        preheatService.validate( params );
+        Preheat preheat = preheatService.preheat( params );
+
+        assertFalse( preheat.isEmpty() );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.CODE ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.CODE, DataElement.class ) );
+        assertTrue( preheat.isEmpty( PreheatIdentifier.CODE, DataElementGroup.class ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.CODE, User.class ) );
+
+        assertTrue( preheat.containsKey( PreheatIdentifier.CODE, DataElement.class, de1.getCode() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.CODE, DataElement.class, de2.getCode() ) );
+        assertFalse( preheat.containsKey( PreheatIdentifier.CODE, DataElement.class, de3.getCode() ) );
+        assertFalse( preheat.containsKey( PreheatIdentifier.CODE, DataElementGroup.class, dataElementGroup.getCode() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.CODE, User.class, user.getCode() ) );
+    }
+
+    @Test
+    @SuppressWarnings( "unchecked" )
+    public void testPreheatReferenceWithScanUID()
+    {
+        DataElementGroup dataElementGroup = new DataElementGroup( "DataElementGroupA" );
+        dataElementGroup.setAutoFields();
+
+        DataElement de1 = createDataElement( 'A' );
+        DataElement de2 = createDataElement( 'B' );
+        DataElement de3 = createDataElement( 'C' );
+
+        manager.save( de1 );
+        manager.save( de2 );
+        manager.save( de3 );
+
+        User user = createUser( 'A' );
+        manager.save( user );
+
+        dataElementGroup.addDataElement( de1 );
+        dataElementGroup.addDataElement( de2 );
+        dataElementGroup.addDataElement( de3 );
+
+        dataElementGroup.setUser( user );
+        manager.save( dataElementGroup );
+
+        Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.scanObjectForReferences( dataElementGroup, PreheatIdentifier.UID );
+
+        PreheatParams params = new PreheatParams();
+        params.setPreheatMode( PreheatMode.REFERENCE );
+        params.setReferences( references );
+
+        preheatService.validate( params );
+        Preheat preheat = preheatService.preheat( params );
+
+        assertFalse( preheat.isEmpty() );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID, DataElement.class ) );
+        assertTrue( preheat.isEmpty( PreheatIdentifier.UID, DataElementGroup.class ) );
+        assertFalse( preheat.isEmpty( PreheatIdentifier.UID, User.class ) );
+
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de1.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de2.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, DataElement.class, de3.getUid() ) );
+        assertFalse( preheat.containsKey( PreheatIdentifier.UID, DataElementGroup.class, dataElementGroup.getUid() ) );
+        assertTrue( preheat.containsKey( PreheatIdentifier.UID, User.class, user.getUid() ) );
+    }
 }

=== 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	2016-01-11 06:09:26 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2016-01-25 03:56:13 +0000
@@ -964,7 +964,7 @@
      *                              evaluated by this rule.
      * @param sequentialSampleCount How many sequential past periods to sample.
      * @param annualSampleCount     How many years of past periods to sample.
-    */
+     */
     public static ValidationRule createMonitoringRule( char uniqueCharacter, Operator operator, Expression leftSide,
         Expression rightSide, PeriodType periodType, int organisationUnitLevel, int sequentialSampleCount,
         int annualSampleCount )
@@ -1083,6 +1083,7 @@
         user.setSurname( "Surname" + uniqueCharacter );
         user.setEmail( "Email" + uniqueCharacter );
         user.setPhoneNumber( "PhoneNumber" + uniqueCharacter );
+        user.setCode( "UserCode" + uniqueCharacter );
 
         return user;
     }
@@ -1125,7 +1126,7 @@
     {
         return createProgram( uniqueCharacter, null, null );
     }
-    
+
     public static Program createProgram( char uniqueCharacter, Set<ProgramStage> programStages,
         OrganisationUnit unit )
     {
@@ -1610,7 +1611,7 @@
         List<GrantedAuthority> authorities = new ArrayList<>();
         authorities.add( new SimpleGrantedAuthority( "ALL" ) );
 
-        UserDetails userDetails = new org.springframework.security.core.userdetails.User( 
+        UserDetails userDetails = new org.springframework.security.core.userdetails.User(
             user.getUserCredentials().getUsername(), user.getUserCredentials().getPassword(), authorities );
 
         Authentication authentication = new UsernamePasswordAuthenticationToken( userDetails, "", authorities );