Repository: syncope
Updated Branches:
  refs/heads/master 3d017e2c3 -> f7d23341e


[SYNCOPE-595] Implementation completed


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/f7d23341
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/f7d23341
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/f7d23341

Branch: refs/heads/master
Commit: f7d23341e57a89f3d41643fb910478407626e2ca
Parents: 3d017e2
Author: Francesco Chicchiriccò <[email protected]>
Authored: Fri Sep 18 17:20:27 2015 +0200
Committer: Francesco Chicchiriccò <[email protected]>
Committed: Fri Sep 18 17:20:27 2015 +0200

----------------------------------------------------------------------
 .../common/rest/api/service/AnyService.java     | 66 +++++++++++++-
 .../rest/cxf/service/AbstractAnyService.java    | 95 ++++++++++++++++++++
 .../rest/cxf/service/AnyObjectServiceImpl.java  |  7 ++
 .../core/rest/cxf/service/GroupServiceImpl.java |  7 ++
 .../core/rest/cxf/service/UserServiceImpl.java  |  7 ++
 .../fit/core/reference/AnyObjectITCase.java     | 48 +++++++++-
 6 files changed, 228 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/f7d23341/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/AnyService.java
----------------------------------------------------------------------
diff --git 
a/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/AnyService.java
 
b/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/AnyService.java
index 3e342a8..dd41f24 100644
--- 
a/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/AnyService.java
+++ 
b/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/AnyService.java
@@ -18,6 +18,7 @@
  */
 package org.apache.syncope.common.rest.api.service;
 
+import java.util.Set;
 import javax.validation.constraints.NotNull;
 import javax.ws.rs.BeanParam;
 import javax.ws.rs.Consumes;
@@ -35,19 +36,49 @@ import org.apache.syncope.common.lib.patch.AnyPatch;
 import org.apache.syncope.common.lib.patch.AssociationPatch;
 import org.apache.syncope.common.lib.patch.DeassociationPatch;
 import org.apache.syncope.common.lib.to.AnyTO;
+import org.apache.syncope.common.lib.to.AttrTO;
 import org.apache.syncope.common.lib.to.BulkAction;
 import org.apache.syncope.common.lib.to.BulkActionResult;
 import org.apache.syncope.common.lib.to.PagedResult;
+import org.apache.syncope.common.lib.types.SchemaType;
 import org.apache.syncope.common.rest.api.beans.AnyListQuery;
 import org.apache.syncope.common.rest.api.beans.AnySearchQuery;
 
 public interface AnyService<TO extends AnyTO, P extends AnyPatch> extends 
JAXRSService {
 
     /**
+     * Reads the list of attributes owned by the given any object for the 
given schema type.
+     *
+     * @param key any object key
+     * @param schemaType schema type
+     * @return list of attributes, owned by the given any object, for the 
given schema type
+     */
+    @GET
+    @Path("{key}/{schemaType}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    Set<AttrTO> read(@NotNull @PathParam("key") Long key, @NotNull 
@PathParam("schemaType") SchemaType schemaType);
+
+    /**
+     * Reads the attribute, owned by the given any object, for the given 
schema type and schema.
+     *
+     * @param key any object key
+     * @param schemaType schema type
+     * @param schema schema
+     * @return attribute, owned by the given any object, for the given schema 
type and schema
+     */
+    @GET
+    @Path("{key}/{schemaType}/{schema}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    AttrTO read(
+            @NotNull @PathParam("key") Long key,
+            @NotNull @PathParam("schemaType") SchemaType schemaType,
+            @NotNull @PathParam("schema") String schema);
+
+    /**
      * Reads the any object matching the provided key.
      *
      * @param key key of any object to be read
-     * @return any object with matching id
+     * @return any object with matching key
      */
     @GET
     @Path("{key}")
@@ -101,6 +132,23 @@ public interface AnyService<TO extends AnyTO, P extends 
AnyPatch> extends JAXRSS
     Response update(@NotNull P anyPatch);
 
     /**
+     * Adds or replaces the attribute, owned by the given any object, for the 
given schema type and schema.
+     *
+     * @param key any object key
+     * @param schemaType schema type
+     * @param attrTO attribute
+     * @return <tt>Response</tt> object featuring the updated any object 
attribute - <tt>AttrTO</tt> as <tt>Entity</tt>
+     */
+    @PUT
+    @Path("{key}/{schemaType}/{schema}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    Response update(
+            @NotNull @PathParam("key") Long key,
+            @NotNull @PathParam("schemaType") SchemaType schemaType,
+            @NotNull AttrTO attrTO);
+
+    /**
      * Updates any object matching the provided key.
      *
      * @param anyTO complete update
@@ -114,6 +162,22 @@ public interface AnyService<TO extends AnyTO, P extends 
AnyPatch> extends JAXRSS
     Response update(@NotNull TO anyTO);
 
     /**
+     * Deletes the attribute, owned by the given any object, for the given 
schema type and schema.
+     *
+     * @param key any object key
+     * @param schemaType schema type
+     * @param schema schema
+     */
+    @DELETE
+    @Path("{key}/{schemaType}/{schema}")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    void delete(
+            @NotNull @PathParam("key") Long key,
+            @NotNull @PathParam("schemaType") SchemaType schemaType,
+            @NotNull @PathParam("schema") String schema);
+
+    /**
      * Deletes any object matching provided key.
      *
      * @param key key of any object to be deleted

http://git-wip-us.apache.org/repos/asf/syncope/blob/f7d23341/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AbstractAnyService.java
----------------------------------------------------------------------
diff --git 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AbstractAnyService.java
 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AbstractAnyService.java
index 9fb9252..d33cde2 100644
--- 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AbstractAnyService.java
+++ 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AbstractAnyService.java
@@ -18,6 +18,7 @@
  */
 package org.apache.syncope.core.rest.cxf.service;
 
+import java.util.Set;
 import javax.ws.rs.core.Response;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.Transformer;
@@ -26,21 +27,26 @@ import org.apache.syncope.common.lib.AnyOperations;
 import org.apache.syncope.common.lib.SyncopeConstants;
 import org.apache.syncope.common.lib.patch.AnyPatch;
 import org.apache.syncope.common.lib.patch.AssociationPatch;
+import org.apache.syncope.common.lib.patch.AttrPatch;
 import org.apache.syncope.common.lib.patch.DeassociationPatch;
 import org.apache.syncope.common.lib.patch.StatusPatch;
 import org.apache.syncope.common.lib.to.AnyTO;
+import org.apache.syncope.common.lib.to.AttrTO;
 import org.apache.syncope.common.lib.to.BulkAction;
 import org.apache.syncope.common.lib.to.BulkActionResult;
 import org.apache.syncope.common.lib.to.PagedResult;
 import org.apache.syncope.common.lib.to.PropagationStatus;
+import org.apache.syncope.common.lib.types.PatchOperation;
 import org.apache.syncope.common.lib.types.ResourceAssociationAction;
 import org.apache.syncope.common.lib.types.ResourceDeassociationAction;
+import org.apache.syncope.common.lib.types.SchemaType;
 import org.apache.syncope.common.lib.types.StatusPatchType;
 import org.apache.syncope.common.rest.api.beans.AnyListQuery;
 import org.apache.syncope.common.rest.api.beans.AnySearchQuery;
 import org.apache.syncope.common.rest.api.service.AnyService;
 import org.apache.syncope.core.logic.AbstractAnyLogic;
 import org.apache.syncope.core.logic.UserLogic;
+import org.apache.syncope.core.persistence.api.dao.NotFoundException;
 import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
 
 public abstract class AbstractAnyService<TO extends AnyTO, P extends AnyPatch>
@@ -49,6 +55,54 @@ public abstract class AbstractAnyService<TO extends AnyTO, P 
extends AnyPatch>
 
     protected abstract AbstractAnyLogic<TO, P> getAnyLogic();
 
+    protected abstract P newPatch(Long key);
+
+    @Override
+    public Set<AttrTO> read(final Long key, final SchemaType schemaType) {
+        TO any = read(key);
+        Set<AttrTO> result;
+        switch (schemaType) {
+            case DERIVED:
+                result = any.getDerAttrs();
+                break;
+
+            case VIRTUAL:
+                result = any.getVirAttrs();
+                break;
+
+            case PLAIN:
+            default:
+                result = any.getPlainAttrs();
+        }
+
+        return result;
+    }
+
+    @Override
+    public AttrTO read(final Long key, final SchemaType schemaType, final 
String schema) {
+        TO any = read(key);
+        AttrTO result;
+        switch (schemaType) {
+            case DERIVED:
+                result = any.getDerAttrMap().get(schema);
+                break;
+
+            case VIRTUAL:
+                result = any.getVirAttrMap().get(schema);
+                break;
+
+            case PLAIN:
+            default:
+                result = any.getPlainAttrMap().get(schema);
+        }
+
+        if (result == null) {
+            throw new NotFoundException("Attribute for type " + schemaType + " 
and schema " + schema);
+        }
+
+        return result;
+    }
+
     @Override
     public TO read(final Long key) {
         return getAnyLogic().read(key);
@@ -116,6 +170,42 @@ public abstract class AbstractAnyService<TO extends AnyTO, 
P extends AnyPatch>
         return modificationResponse(updated);
     }
 
+    private void addUpdateOrReplaceAttr(
+            final Long key, final SchemaType schemaType, final AttrTO attrTO, 
final PatchOperation operation) {
+
+        if (attrTO.getSchema() == null) {
+            throw new NotFoundException("Must specify schema");
+        }
+
+        P patch = newPatch(key);
+
+        Set<AttrPatch> patches;
+        switch (schemaType) {
+            case DERIVED:
+                patches = patch.getDerAttrs();
+                break;
+
+            case VIRTUAL:
+                patches = patch.getVirAttrs();
+                break;
+
+            case PLAIN:
+            default:
+                patches = patch.getPlainAttrs();
+        }
+
+        patches.add(new 
AttrPatch.Builder().operation(operation).attrTO(attrTO).build());
+
+        update(patch);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public Response update(final Long key, final SchemaType schemaType, final 
AttrTO attrTO) {
+        addUpdateOrReplaceAttr(key, schemaType, attrTO, 
PatchOperation.ADD_REPLACE);
+        return modificationResponse(read(key, schemaType, attrTO.getSchema()));
+    }
+
     @Override
     public Response update(final TO anyTO) {
         TO before = getAnyLogic().read(anyTO.getKey());
@@ -128,6 +218,11 @@ public abstract class AbstractAnyService<TO extends AnyTO, 
P extends AnyPatch>
     }
 
     @Override
+    public void delete(final Long key, final SchemaType schemaType, final 
String schema) {
+        addUpdateOrReplaceAttr(key, schemaType, new 
AttrTO.Builder().schema(schema).build(), PatchOperation.DELETE);
+    }
+
+    @Override
     public Response delete(final Long key) {
         TO group = getAnyLogic().read(key);
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/f7d23341/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AnyObjectServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AnyObjectServiceImpl.java
 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AnyObjectServiceImpl.java
index 4a57cbd..357db77 100644
--- 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AnyObjectServiceImpl.java
+++ 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/AnyObjectServiceImpl.java
@@ -44,6 +44,13 @@ public class AnyObjectServiceImpl extends 
AbstractAnyService<AnyObjectTO, AnyObj
     }
 
     @Override
+    protected AnyObjectPatch newPatch(final Long key) {
+        AnyObjectPatch patch = new AnyObjectPatch();
+        patch.setKey(key);
+        return patch;
+    }
+
+    @Override
     public PagedResult<AnyObjectTO> list(final String type, final AnyListQuery 
listQuery) {
         if (StringUtils.isBlank(type)) {
             return super.list(listQuery);

http://git-wip-us.apache.org/repos/asf/syncope/blob/f7d23341/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/GroupServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/GroupServiceImpl.java
 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/GroupServiceImpl.java
index 929b1fa..f2915f7 100644
--- 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/GroupServiceImpl.java
+++ 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/GroupServiceImpl.java
@@ -39,6 +39,13 @@ public class GroupServiceImpl extends 
AbstractAnyService<GroupTO, GroupPatch> im
     }
 
     @Override
+    protected GroupPatch newPatch(final Long key) {
+        GroupPatch patch = new GroupPatch();
+        patch.setKey(key);
+        return patch;
+    }
+
+    @Override
     public List<GroupTO> own() {
         return logic.own();
     }

http://git-wip-us.apache.org/repos/asf/syncope/blob/f7d23341/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserServiceImpl.java
 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserServiceImpl.java
index 1770b41..7be0f29 100644
--- 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserServiceImpl.java
+++ 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserServiceImpl.java
@@ -42,6 +42,13 @@ public class UserServiceImpl extends 
AbstractAnyService<UserTO, UserPatch> imple
     }
 
     @Override
+    protected UserPatch newPatch(final Long key) {
+        UserPatch patch = new UserPatch();
+        patch.setKey(key);
+        return patch;
+    }
+
+    @Override
     public Response getUsername(final Long key) {
         return Response.ok().header(HttpHeaders.ALLOW, OPTIONS_ALLOW).
                 header(RESTHeaders.USERNAME, logic.getUsername(key)).

http://git-wip-us.apache.org/repos/asf/syncope/blob/f7d23341/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AnyObjectITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AnyObjectITCase.java
 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AnyObjectITCase.java
index 94716e7..5474744 100644
--- 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AnyObjectITCase.java
+++ 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AnyObjectITCase.java
@@ -22,7 +22,9 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
+import java.util.Set;
 import javax.ws.rs.core.Response;
 import org.apache.syncope.client.lib.SyncopeClient;
 import org.apache.syncope.common.lib.SyncopeClientException;
@@ -30,7 +32,10 @@ import org.apache.syncope.common.lib.SyncopeConstants;
 import org.apache.syncope.common.lib.patch.AnyObjectPatch;
 import org.apache.syncope.common.lib.to.ConnObjectTO;
 import org.apache.syncope.common.lib.to.AnyObjectTO;
+import org.apache.syncope.common.lib.to.AttrTO;
 import org.apache.syncope.common.lib.to.PagedResult;
+import org.apache.syncope.common.lib.types.ClientExceptionType;
+import org.apache.syncope.common.lib.types.SchemaType;
 import org.junit.FixMethodOrder;
 import org.junit.Test;
 import org.junit.runners.MethodSorters;
@@ -100,7 +105,6 @@ public class AnyObjectITCase extends AbstractITCase {
     @Test
     public void read() {
         AnyObjectTO anyObjectTO = anyObjectService.read(1L);
-
         assertNotNull(anyObjectTO);
         assertNotNull(anyObjectTO.getPlainAttrs());
         assertFalse(anyObjectTO.getPlainAttrs().isEmpty());
@@ -123,4 +127,46 @@ public class AnyObjectITCase extends AbstractITCase {
         assertEquals(newLocation, 
anyObjectTO.getPlainAttrMap().get("location").getValues().get(0));
     }
 
+    @Test
+    public void readAttrs() {
+        AnyObjectTO anyObjectTO = getSampleTO("readAttrs");
+        anyObjectTO = createAnyObject(anyObjectTO);
+        assertNotNull(anyObjectTO);
+
+        Set<AttrTO> attrs = anyObjectService.read(anyObjectTO.getKey(), 
SchemaType.PLAIN);
+        assertEquals(anyObjectTO.getPlainAttrs(), attrs);
+
+        AttrTO location = anyObjectService.read(anyObjectTO.getKey(), 
SchemaType.PLAIN, "location");
+        assertEquals(anyObjectTO.getPlainAttrMap().get("location"), location);
+    }
+
+    @Test
+    public void updateAttr() {
+        AnyObjectTO anyObjectTO = getSampleTO("updateAttr");
+        anyObjectTO = createAnyObject(anyObjectTO);
+        assertNotNull(anyObjectTO);
+
+        AttrTO updated = attrTO("location", "newlocation");
+        anyObjectService.update(anyObjectTO.getKey(), SchemaType.PLAIN, 
updated);
+
+        AttrTO location = anyObjectService.read(anyObjectTO.getKey(), 
SchemaType.PLAIN, "location");
+        assertEquals(updated, location);
+    }
+
+    @Test
+    public void deleteAttr() {
+        AnyObjectTO anyObjectTO = getSampleTO("deleteAttr");
+        anyObjectTO = createAnyObject(anyObjectTO);
+        assertNotNull(anyObjectTO);
+        assertNotNull(anyObjectTO.getPlainAttrMap().get("location"));
+
+        anyObjectService.delete(anyObjectTO.getKey(), SchemaType.PLAIN, 
"location");
+
+        try {
+            anyObjectService.read(anyObjectTO.getKey(), SchemaType.PLAIN, 
"location");
+            fail();
+        } catch (SyncopeClientException e) {
+            assertEquals(ClientExceptionType.NotFound, e.getType());
+        }
+    }
 }

Reply via email to