Repository: olingo-odata4
Updated Branches:
  refs/heads/master 87fa79ad9 -> d2b5449f7


OLINGO-573: validating to make sure key predicates are supplied for update, 
delete entity requests


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

Branch: refs/heads/master
Commit: b78343bd09e6b73bd6777c4ae90971dd109a6fcb
Parents: 0d015cb
Author: Ramesh Reddy <[email protected]>
Authored: Mon Apr 27 11:16:34 2015 -0500
Committer: Ramesh Reddy <[email protected]>
Committed: Mon Apr 27 11:16:34 2015 -0500

----------------------------------------------------------------------
 .../server/core/requests/DataRequest.java       |  5 +++
 .../olingo/server/example/TripPinDataModel.java | 42 ++++++++++++++++++++
 .../olingo/server/example/TripPinHandler.java   | 20 +++++++++-
 .../server/example/TripPinServiceTest.java      | 27 +++++++++++++
 4 files changed, 92 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b78343bd/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
 
b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
index b3be91c..98daf7a 100644
--- 
a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
+++ 
b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
@@ -273,6 +273,11 @@ public class DataRequest extends ServiceRequest {
         return false;
       }
       
+      // in update, delete entity cases, predicate must be there
+      if ((isPATCH() || isPUT() || isDELETE()) 
+          && (getKeyPredicates() == null || getKeyPredicates().isEmpty())) {
+        return false;
+      }
       return true;
     }
 

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b78343bd/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinDataModel.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinDataModel.java
 
b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinDataModel.java
index 78bd012..290f38d 100644
--- 
a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinDataModel.java
+++ 
b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinDataModel.java
@@ -630,6 +630,48 @@ public class TripPinDataModel {
     return copy;
   }
 
+  public boolean updateEntity(EdmEntitySet edmEntitySet, String eTag, String 
key, Object keyValue, 
+      boolean merge, Entity changes, String baseURL) throws 
ODataApplicationException {
+    boolean updated = false;
+
+    if (merge) {
+      EntityCollection set = getEntitySet(edmEntitySet.getName());
+      Iterator<Entity> it = set.getEntities().iterator();
+      while (it.hasNext()) {
+        Entity entity = it.next();
+        if (entity.getProperty(key).getValue().equals(keyValue) && 
eTag.equals("*")
+            || eTag.equals(entity.getETag())) {
+          
+          for (Property p :changes.getProperties()) {
+            for (Property p1: entity.getProperties()) {
+              if (p.getName().equals(p1.getName())) {
+                p1.setValue(p1.getValueType(), p.getValue());
+                updated = true;
+                break;
+              }
+            }
+          }
+          break;
+        }
+      }
+    } else {
+      // this is delete, then insert      
+      EntityCollection set = getEntitySet(edmEntitySet.getName());
+      Iterator<Entity> it = set.getEntities().iterator();
+      while (it.hasNext()) {
+        Entity entity = it.next();
+        if (entity.getProperty(key).getValue().equals(keyValue) && 
eTag.equals("*")
+            || eTag.equals(entity.getETag())) {
+          Property p = entity.getProperty(key);
+          changes.addProperty(p);
+          createEntity(edmEntitySet, changes, baseURL);
+          updated = true;
+        }
+      }
+    }
+    return updated;
+  }
+  
   public boolean deleteEntity(String entitySetName, String eTag, String key, 
Object keyValue) {
     EntityCollection set = getEntitySet(entitySetName);
     Iterator<Entity> it = set.getEntities().iterator();

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b78343bd/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinHandler.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinHandler.java
 
b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinHandler.java
index c180a06..df2b645 100644
--- 
a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinHandler.java
+++ 
b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinHandler.java
@@ -273,9 +273,25 @@ public class TripPinHandler implements ServiceHandler {
   }
 
   @Override
-  public void updateEntity(DataRequest request, Entity entity, boolean merge, 
String entityETag,
+  public void updateEntity(DataRequest request, Entity entity, boolean merge, 
String eTag,
       EntityResponse response) throws ODataTranslatedException, 
ODataApplicationException {
-    response.writeServerError(true);
+    EdmEntitySet edmEntitySet = request.getEntitySet();
+    
+    Entity currentEntity = this.dataModel.getEntity(edmEntitySet.getName(), 
request.getKeyPredicates());
+    if (currentEntity == null) {
+      response.writeNotFound(true);
+      return;
+    }
+    String key = edmEntitySet.getEntityType().getKeyPredicateNames().get(0);
+    String baseURL = request.getODataRequest().getRawBaseUri();
+    boolean updated = this.dataModel.updateEntity(edmEntitySet, eTag, key, 
currentEntity
+        .getProperty(key).getValue(), merge, entity, baseURL);
+
+    if (updated) {
+      response.writeUpdatedEntity();
+    } else {
+      response.writeNotModified();
+    }
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b78343bd/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
 
b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
index c64bfb5..9476f09 100644
--- 
a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
+++ 
b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java
@@ -34,6 +34,7 @@ import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 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.ByteArrayEntity;
@@ -418,6 +419,32 @@ public class TripPinServiceTest {
     EntityUtils.consumeQuietly(response.getEntity());
   }
 
+  @Test
+  public void testUpdateEntity() throws Exception {
+    String payload = "{" + 
+        "  \"Emails\":[" + 
+        "     \"[email protected]\"," +
+        "     \"[email protected]\"" +        
+        "         ]" + 
+        "}";
+    HttpPatch updateRequest = new HttpPatch(baseURL+"/People('kristakemp')");
+    updateRequest.setEntity(new StringEntity(payload, 
ContentType.APPLICATION_JSON));
+    httpSend(updateRequest, 204);
+    
+    HttpResponse response = httpGET(baseURL + "/People('kristakemp')", 200);
+    JsonNode node = getJSONNode(response);
+    assertEquals("$metadata#People/$entity", 
node.get("@odata.context").asText());
+    assertEquals("[email protected]", node.get("Emails").get(0).asText());
+    assertEquals("[email protected]", node.get("Emails").get(1).asText());
+  }  
+  
+  @Test
+  public void testDeleteEntity() throws Exception{
+    // fail because no key predicates supplied
+    HttpDelete deleteRequest = new HttpDelete(baseURL+"/People");
+    HttpResponse response = httpSend(deleteRequest, 405);
+    EntityUtils.consumeQuietly(response.getEntity());
+  }
 
   @Test
   public void testCreateEntityWithLinkToRelatedEntities() throws Exception {

Reply via email to