directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1533727 - in /directory/escimo/trunk: client/src/main/java/org/apache/directory/scim/ common/src/main/java/org/apache/directory/scim/ ldap/src/main/java/org/apache/directory/scim/ldap/ ldap/src/main/resources/ server/src/main/java/org/apac...
Date Sat, 19 Oct 2013 10:42:35 GMT
Author: kayyagari
Date: Sat Oct 19 10:42:34 2013
New Revision: 1533727

URL: http://svn.apache.org/r1533727
Log:
o fixed the modify/PATCH/ operation
o added tests

Added:
    directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/AttributeNotFoundException.java
Modified:
    directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java
    directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
    directory/escimo/trunk/ldap/src/main/resources/log4j.properties
    directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java
    directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java

Modified: directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java?rev=1533727&r1=1533726&r2=1533727&view=diff
==============================================================================
--- directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java
(original)
+++ directory/escimo/trunk/client/src/main/java/org/apache/directory/scim/EscimoClient.java
Sat Oct 19 10:42:34 2013
@@ -31,6 +31,7 @@ import org.apache.http.StatusLine;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPatch;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpPut;
 import org.apache.http.entity.ContentType;
@@ -123,6 +124,17 @@ public class EscimoClient
         return putResource( resource, GROUPS_URI );
     }
 
+    public CoreResource patchUser( CoreResource resource ) throws Exception
+    {
+        return patchResource( resource, USERS_URI );
+    }
+
+    
+    public CoreResource patchGroup( CoreResource resource ) throws Exception
+    {
+        return patchResource( resource, GROUPS_URI );
+    }
+
     
     private boolean deleteResource( String id, String uri ) throws Exception
     {
@@ -270,6 +282,49 @@ public class EscimoClient
         return null;
     }
 
+    
+    private CoreResource patchResource( CoreResource resource, String uri ) throws Exception
+    {
+        if ( resource == null )
+        {
+            throw new IllegalArgumentException( "resource cannot be null" );
+        }
+
+        HttpPatch put = new HttpPatch( providerUrl + uri );
+
+        String payload = serialize( resource ).toString();
+
+        LOG.debug( "sending JSON payload to URI {} for adding resource:\n{}", uri, payload
);
+
+        put.setEntity( new StringEntity( payload, ContentType.APPLICATION_JSON ) );
+
+        HttpClient client = HttpClients.createDefault();
+
+        try
+        {
+            HttpResponse resp = client.execute( put );
+            StatusLine sl = resp.getStatusLine();
+            
+            if ( sl.getStatusCode() == 200 )
+            {
+                String retVal = EntityUtils.toString( resp.getEntity() );
+                
+                return deserialize( retVal );
+            }
+            else if ( sl.getStatusCode() == 209 )
+            {
+                return null;
+            }
+        }
+        catch ( Exception e )
+        {
+            LOG.warn( "", e );
+            throw e;
+        }
+        
+        return null;
+    }
+
     private CoreResource deserialize( String json )
     {
         JsonParser parser = new JsonParser();

Added: directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/AttributeNotFoundException.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/AttributeNotFoundException.java?rev=1533727&view=auto
==============================================================================
--- directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/AttributeNotFoundException.java
(added)
+++ directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/AttributeNotFoundException.java
Sat Oct 19 10:42:34 2013
@@ -0,0 +1,46 @@
+/*
+ *   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.directory.scim;
+
+/**
+ * TODO AttributeNotFoundException.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class AttributeNotFoundException extends ResourceNotFoundException
+{
+    public AttributeNotFoundException( String message )
+    {
+        super( message );
+    }
+    
+    public AttributeNotFoundException( Throwable t )
+    {
+        super( t );
+    }
+
+
+    public AttributeNotFoundException( String message, Throwable t )
+    {
+        super( t );
+        this.message = message;
+    }
+
+}

Modified: directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java?rev=1533727&r1=1533726&r2=1533727&view=diff
==============================================================================
--- directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java
(original)
+++ directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/ProviderService.java
Sat Oct 19 10:42:34 2013
@@ -52,7 +52,7 @@ public interface ProviderService 
     
     GroupResource putGroup( String jsonData, RequestContext ctx ) throws Exception;
 
-    /*UserResource patchUser( String jsonData, RequestContext ctx ) throws Exception;
+    UserResource patchUser( String jsonData, RequestContext ctx ) throws Exception;
     
-    GroupResource patchGroup( String jsonData, RequestContext ctx ) throws Exception;*/
+    GroupResource patchGroup( String jsonData, RequestContext ctx ) throws Exception;
 }

Modified: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java?rev=1533727&r1=1533726&r2=1533727&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
(original)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapResourceProvider.java
Sat Oct 19 10:42:34 2013
@@ -44,11 +44,11 @@ import org.apache.directory.api.ldap.mod
 import org.apache.directory.api.ldap.model.exception.LdapException;
 import org.apache.directory.api.ldap.model.ldif.LdifEntry;
 import org.apache.directory.api.ldap.model.ldif.LdifReader;
+import org.apache.directory.api.ldap.model.message.LdapResult;
 import org.apache.directory.api.ldap.model.message.ModifyRequest;
 import org.apache.directory.api.ldap.model.message.ModifyRequestImpl;
 import org.apache.directory.api.ldap.model.message.ModifyResponse;
 import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
-import org.apache.directory.api.ldap.model.name.Dn;
 import org.apache.directory.api.ldap.model.schema.AttributeType;
 import org.apache.directory.api.ldap.model.schema.LdapSyntax;
 import org.apache.directory.api.ldap.model.schema.SchemaManager;
@@ -66,6 +66,7 @@ import org.apache.directory.ldap.client.
 import org.apache.directory.ldap.client.api.LdapConnectionConfig;
 import org.apache.directory.ldap.client.api.LdapNetworkConnection;
 import org.apache.directory.scim.AttributeHandler;
+import org.apache.directory.scim.AttributeNotFoundException;
 import org.apache.directory.scim.ComplexAttribute;
 import org.apache.directory.scim.GroupResource;
 import org.apache.directory.scim.MissingParameterException;
@@ -77,8 +78,6 @@ import org.apache.directory.scim.ServerR
 import org.apache.directory.scim.SimpleAttribute;
 import org.apache.directory.scim.SimpleAttributeGroup;
 import org.apache.directory.scim.UserResource;
-import org.apache.directory.scim.ldap.LdapSchemaMapper;
-import org.apache.directory.scim.ldap.LdapUtil;
 import org.apache.directory.scim.ldap.schema.ComplexType;
 import org.apache.directory.scim.ldap.schema.GroupSchema;
 import org.apache.directory.scim.ldap.schema.MultiValType;
@@ -253,6 +252,18 @@ public class LdapResourceProvider implem
     }
 
 
+    public UserResource patchUser( String jsonData, RequestContext ctx ) throws Exception
+    {
+        return ( UserResource ) patchResource( jsonData, ctx, userSchema );
+    }
+
+
+    public GroupResource patchGroup( String jsonData, RequestContext ctx ) throws Exception
+    {
+        return ( GroupResource ) patchResource( jsonData, ctx, groupSchema );
+    }
+
+    
     public InputStream getUserPhoto( String id, String atName ) throws MissingParameterException
     {
         if ( Strings.isEmpty( id ) )
@@ -611,7 +622,7 @@ public class LdapResourceProvider implem
     }
     
     
-    public void patchResource( String jsonData, RequestContext ctx, ResourceSchema resourceSchema
) throws Exception
+    public ServerResource patchResource( String jsonData, RequestContext ctx, ResourceSchema
resourceSchema ) throws Exception
     {
         JsonParser parser = new JsonParser();
         JsonObject obj = ( JsonObject ) parser.parse( jsonData );
@@ -631,12 +642,16 @@ public class LdapResourceProvider implem
         
         List<String> deleteModAtOids = new ArrayList<String>();
         
+        boolean hasAttributesInMeta = false;
+        
         JsonObject metaObj = ( JsonObject ) obj.get( "meta" );
         if( metaObj != null )
         {
             JsonArray metaAtNames = ( JsonArray ) metaObj.get( "attributes" );
             if( metaAtNames != null )
             {
+                hasAttributesInMeta = true;
+                
                 for( JsonElement e : metaAtNames )
                 {
                     String name = e.getAsString();
@@ -644,8 +659,7 @@ public class LdapResourceProvider implem
                     
                     if( bt == null )
                     {
-                        //FIXME should throw attribute not found exception
-                        throw new ResourceNotFoundException( "No definition found for the
attribute " + name );
+                        throw new AttributeNotFoundException( "No definition found for the
attribute " + name );
                     }
                     
                     if( bt.isReadOnly() || ( ! ( bt instanceof SimpleType ) ) )
@@ -667,31 +681,47 @@ public class LdapResourceProvider implem
             }
         }
 
-        Entry entry = new DefaultEntry( ldapSchema );
-        
-        LdapUtil.patchAttributes( entry, obj, ctx, resourceSchema, modReq );
-        
+        try
+        {
+            LdapUtil.patchAttributes( existingEntry, obj, ctx, resourceSchema, modReq );
+            
+            ModifyResponse modResp = connection.modify( modReq );
+            
+            LdapResult result = modResp.getLdapResult();
+            if( result.getResultCode() != ResultCodeEnum.SUCCESS )
+            {
+                throw new Exception( result.getDiagnosticMessage() );
+            }
+            
+            if( hasAttributesInMeta )
+            {
+                Entry entry = fetchEntryById( resourceId, resourceSchema );
+                
+                ServerResource resource = null;
+                
+                if( resourceSchema == userSchema )
+                {
+                    resource = new UserResource();
+                }
+                else
+                {
+                    resource = new GroupResource();
+                }
+
+                ctx.setCoreResource( resource );
 
-//        for( Attribute ldapAt : entry )
-//        {
-//            AttributeType type = ldapAt.getAttributeType();
-//            
-//            if( deleteModAtOids.contains( type.getOid() ) )
-//            {
-//                modReq.add( ldapAt );
-//            }
-//            else if( existingEntry.containsAttribute( type ) )
-//            {
-//                if( type.isSingleValued() )
-//                {
-//                    modReq.replace( ldapAt );
-//                }
-//                else
-//                {
-//                    modReq.add( ldapAt );
-//                }
-//            }
-//        }
+                _loadCoreResource( ctx, entry, resourceSchema );
+                
+                return resource;
+            }
+            
+            return null;
+        }
+        catch( Exception e )
+        {
+            LOG.warn( "Failed to patch the resource with ID {}", resourceId, e );
+            throw e;
+        }
     }
     
     public UserResource toUser( RequestContext ctx, Entry entry ) throws Exception

Modified: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java?rev=1533727&r1=1533726&r2=1533727&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
(original)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapUtil.java
Sat Oct 19 10:42:34 2013
@@ -43,6 +43,7 @@ import org.slf4j.LoggerFactory;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
+import static org.apache.directory.api.ldap.model.entry.ModificationOperation.*;
 
 /**
  * TODO LdapUtil.
@@ -169,7 +170,7 @@ public class LdapUtil
     
     //================ PATCH ========
     
-    public static void patchAttributes( Entry entry, JsonObject obj, RequestContext ctx,
ResourceSchema resourceSchema, ModifyRequest modReq ) throws Exception
+    public static void patchAttributes( Entry existingEntry, JsonObject obj, RequestContext
ctx, ResourceSchema resourceSchema, ModifyRequest modReq ) throws Exception
     {
         for( java.util.Map.Entry<String, JsonElement> e : obj.entrySet() )
         {
@@ -196,11 +197,11 @@ public class LdapUtil
             
             if( handler != null )
             {
-                handler.patch( bt, e.getValue(), entry, ctx, modReq );
+                handler.patch( bt, e.getValue(), existingEntry, ctx, modReq );
             }
             else
             {
-                patchLdapAttribute( bt, e.getValue(), entry, ctx, modReq );
+                patchLdapAttribute( bt, e.getValue(), existingEntry, ctx, modReq );
             }
         }
     }
@@ -236,7 +237,7 @@ public class LdapUtil
                 // e.x "emails":['elecharny@apache.org', 'pajbam@apache.org']
                 if( je.isJsonPrimitive() )
                 {
-                    patchSimpleAttribute( multiStg.getValueType(), je, entry, ldapSchema,
modReq, ModificationOperation.REPLACE_ATTRIBUTE );
+                    patchSimpleAttribute( multiStg.getValueType(), je, entry, ldapSchema,
modReq, false );
                 }
                 else
                 {
@@ -247,7 +248,7 @@ public class LdapUtil
         }
         else
         {
-            patchSimpleAttribute( ( SimpleType ) bt, el, entry, ldapSchema, modReq, ModificationOperation.REPLACE_ATTRIBUTE
);
+            patchSimpleAttribute( ( SimpleType ) bt, el, entry, ldapSchema, modReq, false
);
         }
     }
 
@@ -255,14 +256,17 @@ public class LdapUtil
     // BaseType atType, JsonElement jsonData, Object targetEntry, Object patchCtx 
     private static void patchSimpleTypeGroup( SimpleTypeGroup stg, JsonObject jo, Entry entry,
SchemaManager ldapSchema, String scimComplexAtName, ModifyRequest modReq ) throws LdapException
     {
-        ModificationOperation operation = ModificationOperation.REPLACE_ATTRIBUTE; // the
default
+        //ModificationOperation operation = REPLACE_ATTRIBUTE; // the default
+        
+        boolean delete = false;
         
         JsonElement atOperation = jo.get( "operation" );
         if( atOperation != null )
         {
             if( atOperation.getAsString().equalsIgnoreCase( "delete" ) )
             {
-                operation = ModificationOperation.REMOVE_ATTRIBUTE;
+                //operation = REMOVE_ATTRIBUTE;
+                delete = true;
             }
             
             jo.remove( "operation" );
@@ -285,7 +289,7 @@ public class LdapUtil
             
             if( st != null )
             {
-                patchSimpleAttribute( st, e.getValue(), entry, ldapSchema, modReq, operation
);
+                patchSimpleAttribute( st, e.getValue(), entry, ldapSchema, modReq, delete
);
             }
             else
             {
@@ -294,12 +298,14 @@ public class LdapUtil
         }
     }
     
-    public static void patchSimpleAttribute( SimpleType st, JsonElement el, Entry entry,
SchemaManager ldapSschema, ModifyRequest modReq, ModificationOperation operation ) throws
LdapException
+    public static void patchSimpleAttribute( SimpleType st, JsonElement el, Entry entry,
SchemaManager ldapSschema, ModifyRequest modReq, boolean delete ) throws LdapException
     {
         if( st.isReadOnly() )
         {
             return;
         }
+
+        ModificationOperation operation = ( delete ? REMOVE_ATTRIBUTE : REPLACE_ATTRIBUTE
);
         
         String ldapAtName = st.getMappedTo();
         
@@ -314,15 +320,11 @@ public class LdapUtil
         {
             byte[] value = Base64.decode( el.getAsString().toCharArray() );
             
-            if( operation == ModificationOperation.REPLACE_ATTRIBUTE )
-            {
-                modReq.replace( ldapAtName, value );
-            }
-            if( operation == ModificationOperation.REMOVE_ATTRIBUTE )
+            if( delete )
             {
                 modReq.remove( ldapAtName, value );
             }
-            else
+            else if( !entry.contains( ldapType, value ) )
             {
                 modReq.add( ldapAtName, value );
             }
@@ -330,19 +332,28 @@ public class LdapUtil
         else
         {
             String value = el.getAsString();
-            
-            if( operation == ModificationOperation.REPLACE_ATTRIBUTE )
-            {
-                modReq.replace( ldapAtName, value );
-            }
-            if( operation == ModificationOperation.REMOVE_ATTRIBUTE )
+
+            if( delete )
             {
                 modReq.remove( ldapAtName, value );
             }
-            else
+            else if( !entry.contains( ldapType, value ) )
             {
                 modReq.add( ldapAtName, value );
             }
+            
+//            if( operation == REPLACE_ATTRIBUTE )
+//            {
+//                modReq.replace( ldapAtName, value );
+//            }
+//            else if( operation == REMOVE_ATTRIBUTE )
+//            {
+//                modReq.remove( ldapAtName, value );
+//            }
+//            else
+//            {
+//                modReq.add( ldapAtName, value );
+//            }
         }
     }
 

Modified: directory/escimo/trunk/ldap/src/main/resources/log4j.properties
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/resources/log4j.properties?rev=1533727&r1=1533726&r2=1533727&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/resources/log4j.properties (original)
+++ directory/escimo/trunk/ldap/src/main/resources/log4j.properties Sat Oct 19 10:42:34 2013
@@ -40,4 +40,7 @@ log4j.logger.org.apache.directory.api=FA
 log4j.logger.org.apache.directory.server=FATAL
 log4j.logger.net.sf.ehcache=FATAL
 log4j.logger.org.apache.mina=FATAL
-log4j.logger.org.apache.directory.ldap=WARN
\ No newline at end of file
+log4j.logger.org.apache.directory.ldap=WARN
+log4j.logger.org.apache.http=WARN
+log4j.logger.org.eclipse=WARN
+log4j.logger.org.apache.wink=WARN

Modified: directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java?rev=1533727&r1=1533726&r2=1533727&view=diff
==============================================================================
--- directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java
(original)
+++ directory/escimo/trunk/server/src/main/java/org/apache/directory/scim/rest/UserService.java
Sat Oct 19 10:42:34 2013
@@ -27,7 +27,6 @@ import java.net.URI;
 
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
-import javax.ws.rs.HttpMethod;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
@@ -44,6 +43,7 @@ import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.StreamingOutput;
 import javax.ws.rs.core.UriInfo;
 
+import org.apache.directory.scim.AttributeNotFoundException;
 import org.apache.directory.scim.MissingParameterException;
 import org.apache.directory.scim.ProviderService;
 import org.apache.directory.scim.RequestContext;
@@ -181,6 +181,49 @@ public class UserService
     }
 
     
+    @PATCH
+    @Produces({MediaType.APPLICATION_JSON})
+    public Response patchUser( String jsonData, @Context UriInfo uriInfo, @Context HttpHeaders
headers )
+    {
+        ResponseBuilder rb = null;
+
+        if( ( jsonData == null ) || ( jsonData.trim().length() == 0 ) )
+        {
+            rb = Response.status( Status.BAD_REQUEST ).entity( "No data is present with the
call to " + uriInfo.getAbsolutePath() );
+            return rb.build();
+        }
+        
+        LOG.debug( "Data received at the URI {}\n{}", uriInfo.getAbsolutePath(), jsonData
);
+        
+        try
+        {
+            RequestContext ctx = new RequestContext( provider, uriInfo, headers );
+            
+            ServerResource resource = provider.patchUser( jsonData, ctx );
+            
+            if( resource == null )
+            {
+                rb = Response.status( Status.NO_CONTENT );
+            }
+            else
+            {
+                String json = ResourceSerializer.serialize( resource );
+                rb = Response.ok().entity( json );
+            }
+        }
+        catch( AttributeNotFoundException e )
+        {
+            rb = Response.status( Status.NOT_FOUND ).entity( exceptionToStr( e ) );
+        }
+        catch( Exception e )
+        {
+            rb = Response.status( Status.INTERNAL_SERVER_ERROR ).entity( exceptionToStr(
e ) );
+        }
+        
+        return rb.build();
+    }
+
+    
     @GET
     @Produces({MediaType.APPLICATION_OCTET_STREAM})
     @Path("photo")

Modified: directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java?rev=1533727&r1=1533726&r2=1533727&view=diff
==============================================================================
--- directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java
(original)
+++ directory/escimo/trunk/tests/src/test/java/org/apache/directory/scim/UserResourceTest.java
Sat Oct 19 10:42:34 2013
@@ -85,7 +85,7 @@ public class UserResourceTest
     public void testAddGetAndDeleteUser() throws Exception
     {
         User user = new User();
-        user.setUserName( "test2" );
+        user.setUserName( "test1" );
         user.setDisplayName( "Test UserResource" );
         user.setPassword( "secret01" );
         
@@ -152,4 +152,57 @@ public class UserResourceTest
         assertEquals( 1, replacedUser.getEmails().size() );
         assertEquals( newEmail.getValue(), replacedUser.getEmails().get( 0 ).getValue() );
     }
+    
+    
+    @Test
+    public void testPatch() throws Exception
+    {
+        User user = new User();
+        user.setUserName( "testPatch" );
+        user.setDisplayName( "Test UserResource" );
+        user.setPassword( "secret01" );
+        
+        Name name = new Name();
+        name.setFamilyName( "UserResource" );
+        name.setGivenName( "Test" );
+        
+        user.setName( name );
+        
+        List<Email> emails = new ArrayList<Email>();
+        Email mail = new Email();
+        mail.setValue( "test@example.com" );
+        emails.add( mail );
+        user.setEmails( emails );
+
+        User addedUser = ( User ) client.addUser( user );
+        assertNotNull( addedUser );
+
+        addedUser.getEmails().get( 0 ).setOperation( "delete" );
+        
+        List<String> newEmails = new ArrayList<String>();
+        newEmails.add( "newemail@example.com" );
+        newEmails.add( "anothermail@example.com" );
+        for( String e : newEmails )
+        {
+            Email newEmail = new Email();
+            newEmail.setValue( e );
+            addedUser.getEmails().add( newEmail );
+        }
+        
+        User patchedUser = ( User ) client.patchUser( addedUser );
+
+        assertNull( patchedUser );
+        
+        patchedUser = ( User ) client.getUser( addedUser.getId() );
+        assertNotNull( patchedUser );
+        
+        for( Email e : patchedUser.getEmails() )
+        {
+            System.out.println(e.getValue());
+            assertTrue( newEmails.contains( e.getValue() ) );
+        }
+        
+        assertEquals( 2, patchedUser.getEmails().size() );
+    }
+
 }



Mime
View raw message