OLINGO-573: validating to make sure key predicates are supplied for update, delete entity requests
This commit is contained in:
parent
0d015cb7b6
commit
b78343bd09
|
@ -273,6 +273,11 @@ public class DataRequest extends ServiceRequest {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// in update, delete entity cases, predicate must be there
|
||||||
|
if ((isPATCH() || isPUT() || isDELETE())
|
||||||
|
&& (getKeyPredicates() == null || getKeyPredicates().isEmpty())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -630,6 +630,48 @@ public class TripPinDataModel {
|
||||||
return copy;
|
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) {
|
public boolean deleteEntity(String entitySetName, String eTag, String key, Object keyValue) {
|
||||||
EntityCollection set = getEntitySet(entitySetName);
|
EntityCollection set = getEntitySet(entitySetName);
|
||||||
Iterator<Entity> it = set.getEntities().iterator();
|
Iterator<Entity> it = set.getEntities().iterator();
|
||||||
|
|
|
@ -273,9 +273,25 @@ public class TripPinHandler implements ServiceHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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 {
|
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
|
@Override
|
||||||
|
|
|
@ -34,6 +34,7 @@ import org.apache.http.HttpRequest;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.methods.HttpDelete;
|
import org.apache.http.client.methods.HttpDelete;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
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.HttpPost;
|
||||||
import org.apache.http.client.methods.HttpPut;
|
import org.apache.http.client.methods.HttpPut;
|
||||||
import org.apache.http.entity.ByteArrayEntity;
|
import org.apache.http.entity.ByteArrayEntity;
|
||||||
|
@ -418,6 +419,32 @@ public class TripPinServiceTest {
|
||||||
EntityUtils.consumeQuietly(response.getEntity());
|
EntityUtils.consumeQuietly(response.getEntity());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateEntity() throws Exception {
|
||||||
|
String payload = "{" +
|
||||||
|
" \"Emails\":[" +
|
||||||
|
" \"Krista@example.com\"," +
|
||||||
|
" \"Krista@gmail.com\"" +
|
||||||
|
" ]" +
|
||||||
|
"}";
|
||||||
|
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("Krista@example.com", node.get("Emails").get(0).asText());
|
||||||
|
assertEquals("Krista@gmail.com", 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
|
@Test
|
||||||
public void testCreateEntityWithLinkToRelatedEntities() throws Exception {
|
public void testCreateEntityWithLinkToRelatedEntities() throws Exception {
|
||||||
|
|
Loading…
Reference in New Issue