You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by sk...@apache.org on 2014/02/14 10:06:08 UTC
[48/50] [abbrv] git commit: [OLINGO-154] Fixed handling of complex
values
[OLINGO-154] Fixed handling of complex values
Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/a8461c78
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/a8461c78
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/a8461c78
Branch: refs/heads/ODataServlet
Commit: a8461c78b71b871bb2d0bf5351a02adcc2ad101b
Parents: 3613c17
Author: Michael Bolz <mi...@apache.org>
Authored: Tue Feb 11 09:02:54 2014 +0100
Committer: Michael Bolz <mi...@apache.org>
Committed: Tue Feb 11 12:55:17 2014 +0100
----------------------------------------------------------------------
.../processor/core/ListsProcessor.java | 35 +++-
.../processor/core/util/AnnotationHelper.java | 30 ++--
.../annotation/processor/ref/model/City.java | 2 +
.../processor/ref/model/Employee.java | 2 +-
.../processor/ref/model/Location.java | 2 +
.../processor/ref/AbstractRefJsonTest.java | 38 +++++
.../processor/ref/EntryJsonCreateTest.java | 161 +++++++++++++++++++
7 files changed, 254 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/a8461c78/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessor.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessor.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessor.java
index e65fd2b..e63ecf6 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessor.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/ListsProcessor.java
@@ -1569,9 +1569,17 @@ public class ListsProcessor extends DataSourceProcessor {
for (final String propertyName : type.getPropertyNames()) {
final EdmProperty property = (EdmProperty) type.getProperty(propertyName);
if (property.isSimple()) {
- typeMap.put(propertyName, valueAccess.getPropertyType(data, property));
+ Object value = valueAccess.getPropertyType(data, property);
+ if(value != null) {
+ typeMap.put(propertyName, value);
+ }
} else {
- typeMap.put(propertyName, getStructuralTypeTypeMap(valueAccess.getPropertyValue(data, property),
+ Object value = valueAccess.getPropertyValue(data, property);
+ if(value == null) {
+ Class<?> complexClass = valueAccess.getPropertyType(data, property);
+ value = createInstance(complexClass);
+ }
+ typeMap.put(propertyName, getStructuralTypeTypeMap(value,
(EdmStructuralType) property.getType()));
}
}
@@ -1583,6 +1591,9 @@ public class ListsProcessor extends DataSourceProcessor {
private <T> void setStructuralTypeValuesFromMap(final T data, final EdmStructuralType type,
final Map<String, Object> valueMap, final boolean merge) throws ODataException {
+ if(data == null) {
+ throw new ODataException("Unable to set structural type values to NULL data.");
+ }
ODataContext context = getContext();
final int timingHandle =
context.startRuntimeMeasurement(getClass().getSimpleName(), "setStructuralTypeValuesFromMap");
@@ -1603,7 +1614,13 @@ public class ListsProcessor extends DataSourceProcessor {
} else {
@SuppressWarnings("unchecked")
final Map<String, Object> values = (Map<String, Object>) value;
- setStructuralTypeValuesFromMap(valueAccess.getPropertyValue(data, property),
+ Object complexData = valueAccess.getPropertyValue(data, property);
+ if(complexData == null) {
+ Class<?> complexClass = valueAccess.getPropertyType(data, property);
+ complexData = createInstance(complexClass);
+ valueAccess.setPropertyValue(data, property, complexData);
+ }
+ setStructuralTypeValuesFromMap(complexData,
(EdmStructuralType) property.getType(), values, merge);
}
}
@@ -1612,6 +1629,18 @@ public class ListsProcessor extends DataSourceProcessor {
context.stopRuntimeMeasurement(timingHandle);
}
+ private Object createInstance(Class<?> complexClass) throws ODataException {
+ try {
+ return complexClass.newInstance();
+ } catch (InstantiationException e) {
+ throw new ODataException("Unable to create instance for complex data class '"
+ + complexClass + "'.", e);
+ } catch (IllegalAccessException e) {
+ throw new ODataException("Unable to create instance for complex data class '"
+ + complexClass + "'.", e);
+ }
+ }
+
@Override
public ODataResponse executeBatch(final BatchHandler handler, final String contentType, final InputStream content)
throws ODataException {
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/a8461c78/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
index d8e5fb6..d7a165f 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
@@ -405,26 +405,35 @@ public class AnnotationHelper {
return null;
}
- public Class<?> getFieldTypeForProperty(final Object instance, final String propertyName)
+ public Class<?> getFieldTypeForProperty(final Class<?> clazz, final String propertyName)
throws ODataAnnotationException {
- if (instance == null) {
+ if (clazz == null) {
return null;
}
- Field field = getFieldForPropertyName(instance, propertyName, instance.getClass(), true);
+ Field field = getFieldForPropertyName(propertyName, clazz, true);
if (field == null) {
throw new ODataAnnotationException("No field for property '" + propertyName
- + "' found at class '" + instance.getClass() + "'.");
+ + "' found at class '" + clazz + "'.");
}
return field.getType();
}
+ public Class<?> getFieldTypeForProperty(final Object instance, final String propertyName)
+ throws ODataAnnotationException {
+ if (instance == null) {
+ return null;
+ }
+
+ return getFieldTypeForProperty(instance.getClass(), propertyName);
+ }
+
public Object getValueForProperty(final Object instance, final String propertyName) throws ODataAnnotationException {
if (instance == null) {
return null;
}
- Field field = getFieldForPropertyName(instance, propertyName, instance.getClass(), true);
+ Field field = getFieldForPropertyName(propertyName, instance.getClass(), true);
if (field == null) {
throw new ODataAnnotationException("No field for property '" + propertyName
+ "' found at class '" + instance.getClass() + "'.");
@@ -434,19 +443,16 @@ public class AnnotationHelper {
public void setValueForProperty(final Object instance, final String propertyName, final Object propertyValue) {
if (instance != null) {
- Field field = getFieldForPropertyName(instance, propertyName, instance.getClass(), true);
+ Field field = getFieldForPropertyName(propertyName, instance.getClass(), true);
if (field != null) {
setFieldValue(instance, field, propertyValue);
}
}
}
- private Field getFieldForPropertyName(final Object instance, final String propertyName,
+ private Field getFieldForPropertyName(final String propertyName,
final Class<?> resultClass, final boolean inherited) {
- if (instance == null) {
- return null;
- }
-
+
Field[] fields = resultClass.getDeclaredFields();
for (Field field : fields) {
EdmProperty property = field.getAnnotation(EdmProperty.class);
@@ -461,7 +467,7 @@ public class AnnotationHelper {
Class<?> superClass = resultClass.getSuperclass();
if (inherited && superClass != Object.class) {
- return getFieldForPropertyName(instance, propertyName, superClass, true);
+ return getFieldForPropertyName(propertyName, superClass, true);
}
return null;
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/a8461c78/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/City.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/City.java b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/City.java
index 7a72178..33add0a 100644
--- a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/City.java
+++ b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/City.java
@@ -32,6 +32,8 @@ public class City {
@EdmProperty
private String cityName;
+ public City() {}
+
public City(final String postalCode, final String name) {
this.postalCode = postalCode;
cityName = name;
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/a8461c78/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Employee.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Employee.java b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Employee.java
index 98d57ca..b8bf457 100644
--- a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Employee.java
+++ b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Employee.java
@@ -43,7 +43,7 @@ public class Employee {
@EdmProperty(name = "EmployeeName")
private String employeeName;
@EdmProperty
- private int age;
+ private Integer age;
@EdmNavigationProperty(name = "ne_Manager", association = "ManagerEmployees")
private Manager manager;
@EdmNavigationProperty(name = "ne_Team", association = "TeamEmployees")
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/a8461c78/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Location.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Location.java b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Location.java
index b131f7e..9a2b46b 100644
--- a/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Location.java
+++ b/odata2-annotation-processor/annotation-processor-ref/src/main/java/org/apache/olingo/odata2/annotation/processor/ref/model/Location.java
@@ -31,6 +31,8 @@ public class Location {
@EdmProperty
private City city;
+ public Location() {}
+
public Location(final String country, final String postalCode, final String cityName) {
this.country = country;
city = new City(postalCode, cityName);
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/a8461c78/odata2-annotation-processor/annotation-processor-ref/src/test/java/org/apache/olingo/odata2/annotation/processor/ref/AbstractRefJsonTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/src/test/java/org/apache/olingo/odata2/annotation/processor/ref/AbstractRefJsonTest.java b/odata2-annotation-processor/annotation-processor-ref/src/test/java/org/apache/olingo/odata2/annotation/processor/ref/AbstractRefJsonTest.java
new file mode 100644
index 0000000..b6a9330
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-ref/src/test/java/org/apache/olingo/odata2/annotation/processor/ref/AbstractRefJsonTest.java
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * 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.olingo.odata2.annotation.processor.ref;
+
+import com.google.gson.Gson;
+import com.google.gson.internal.StringMap;
+import com.google.gson.reflect.TypeToken;
+
+/**
+ *
+ */
+public class AbstractRefJsonTest extends AbstractRefTest {
+ public StringMap<?> getStringMap(final String body) {
+ Gson gson = new Gson();
+ final StringMap<?> map = gson.fromJson(body, new TypeToken<StringMap<?>>() {}.getType());
+ if (map.get("d") instanceof StringMap<?>) {
+ return (StringMap<?>) map.get("d");
+ } else {
+ return map;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/a8461c78/odata2-annotation-processor/annotation-processor-ref/src/test/java/org/apache/olingo/odata2/annotation/processor/ref/EntryJsonCreateTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-ref/src/test/java/org/apache/olingo/odata2/annotation/processor/ref/EntryJsonCreateTest.java b/odata2-annotation-processor/annotation-processor-ref/src/test/java/org/apache/olingo/odata2/annotation/processor/ref/EntryJsonCreateTest.java
new file mode 100644
index 0000000..0adb3a7
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-ref/src/test/java/org/apache/olingo/odata2/annotation/processor/ref/EntryJsonCreateTest.java
@@ -0,0 +1,161 @@
+/*******************************************************************************
+ * 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.olingo.odata2.annotation.processor.ref;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.UUID;
+
+import org.apache.http.HttpHeaders;
+import org.apache.http.HttpResponse;
+import org.apache.olingo.odata2.api.commons.HttpContentType;
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.junit.Test;
+
+import com.google.gson.internal.StringMap;
+
+/**
+ *
+ */
+public class EntryJsonCreateTest extends AbstractRefJsonTest {
+
+ @Test
+ public void createEntryRoom() throws Exception {
+ String id = UUID.randomUUID().toString();
+ String content = "{\"d\":{\"__metadata\":{\"id\":\"" + getEndpoint() + "Rooms('1')\","
+ + "\"uri\":\"" + getEndpoint() + "Rooms('1')\",\"type\":\"RefScenario.Room\","
+ + "\"etag\":\"W/\\\"3\\\"\"},"
+ + "\"Id\":\"" + id + "\",\"Name\":\"Room 104\",\"Seats\":4,\"Version\":2,"
+ + "\"nr_Employees\":{\"__deferred\":{\"uri\":\"" + getEndpoint() + "Rooms('1')/nr_Employees\"}},"
+ + "\"nr_Building\":{\"__deferred\":{\"uri\":\"" + getEndpoint() + "Rooms('1')/nr_Building\"}}}}";
+ assertNotNull(content);
+ HttpResponse response =
+ postUri("Rooms", content, HttpContentType.APPLICATION_JSON, HttpHeaders.ACCEPT,
+ HttpContentType.APPLICATION_JSON, HttpStatusCodes.CREATED);
+ checkMediaType(response, HttpContentType.APPLICATION_JSON);
+
+ String body = getBody(response);
+ StringMap<?> map = getStringMap(body);
+ assertEquals(id, map.get("Id"));
+ assertEquals("Room 104", map.get("Name"));
+ @SuppressWarnings("unchecked")
+ StringMap<String> metadataMap = (StringMap<String>) map.get("__metadata");
+ assertNotNull(metadataMap);
+ String expectedRoomId = getEndpoint() + "Rooms('" + id + "')";
+ assertEquals(expectedRoomId, metadataMap.get("id"));
+ assertEquals("RefScenario.Room", metadataMap.get("type"));
+ assertEquals(expectedRoomId, metadataMap.get("uri"));
+
+ response = callUri("Rooms('" + id + "')/Seats/$value");
+ body = getBody(response);
+ assertEquals("4", body);
+ }
+
+ @Test
+ public void createEntryRoomWithLink() throws Exception {
+ final String id = UUID.randomUUID().toString();
+ String content = "{\"d\":{\"__metadata\":{\"id\":\"" + getEndpoint() + "Rooms('1')\","
+ + "\"uri\":\"" + getEndpoint() + "Rooms('1')\",\"type\":\"RefScenario.Room\","
+ + "\"etag\":\"W/\\\"3\\\"\"},"
+ + "\"Id\":\"" + id + "\",\"Name\":\"Room 104\","
+ + "\"nr_Employees\":{\"__deferred\":{\"uri\":\"" + getEndpoint() + "Rooms('1')/nr_Employees\"}},"
+ + "\"nr_Building\":{\"__deferred\":{\"uri\":\"" + getEndpoint() + "Rooms('1')/nr_Building\"}}}}";
+ assertNotNull(content);
+ HttpResponse response =
+ postUri("Rooms", content, HttpContentType.APPLICATION_JSON, HttpHeaders.ACCEPT,
+ HttpContentType.APPLICATION_JSON, HttpStatusCodes.CREATED);
+ checkMediaType(response, HttpContentType.APPLICATION_JSON);
+
+ String body = getBody(response);
+ StringMap<?> map = getStringMap(body);
+ assertEquals(id, map.get("Id"));
+ assertEquals("Room 104", map.get("Name"));
+ @SuppressWarnings("unchecked")
+ StringMap<Object> employeesMap = (StringMap<Object>) map.get("nr_Employees");
+ assertNotNull(employeesMap);
+ @SuppressWarnings("unchecked")
+ StringMap<String> deferredMap = (StringMap<String>) employeesMap.get("__deferred");
+ assertNotNull(deferredMap);
+ assertEquals(getEndpoint() + "Rooms('" + id + "')/nr_Employees", deferredMap.get("uri"));
+ }
+
+ @Test
+ public void createAndModifyEntryEmployee() throws Exception {
+ String content = "{iVBORw0KGgoAAAANSUhEUgAAAB4AAAAwCAIAAACJ9F2zAAAAA}";
+
+ assertNotNull(content);
+ HttpResponse createResponse =
+ postUri("Employees", content, HttpContentType.TEXT_PLAIN, HttpHeaders.ACCEPT, HttpContentType.APPLICATION_JSON,
+ HttpStatusCodes.CREATED);
+ checkMediaType(createResponse, HttpContentType.APPLICATION_JSON);
+
+ String body = getBody(createResponse);
+ StringMap<?> map = getStringMap(body);
+ String id = (String) map.get("EmployeeId");
+ assertNull(map.get("EmployeeName"));
+
+ putUri("Employees('" + id + "')", JSON_EMPLOYEE, HttpContentType.APPLICATION_JSON, HttpStatusCodes.NO_CONTENT);
+
+ HttpResponse updateResponse = callUri("Employees('" + id + "')", "Accept", HttpContentType.APPLICATION_JSON);
+ checkMediaType(updateResponse, HttpContentType.APPLICATION_JSON);
+ String updatedBody = getBody(updateResponse);
+ StringMap<?> updatedMap = getStringMap(updatedBody);
+ assertEquals("1", updatedMap.get("EmployeeId"));
+ assertEquals("Douglas", updatedMap.get("EmployeeName"));
+ assertNull(updatedMap.get("EntryData"));
+
+ StringMap<?> location = (StringMap<?>) updatedMap.get("Location");
+ assertEquals("Britian", location.get("Country"));
+
+ StringMap<?> city = (StringMap<?>) location.get("City");
+ assertEquals("12345", city.get("PostalCode"));
+ assertEquals("Sample", city.get("CityName"));
+ }
+
+ private static final String JSON_EMPLOYEE = "{" +
+ " \"d\": {" +
+// " \"__metadata\": {" +
+// " \"id\": \"http://localhost:19000/abc/EntryJsonCreateTest/Employees('1')\"," +
+// " \"uri\": \"http://localhost:19000/abc/EntryJsonCreateTest/Employees('1')\"," +
+// " \"type\": \"RefScenario.Employee\"," +
+// " \"content_type\": \"application/octet-stream\"," +
+// " \"media_src\": \"Employees('1')/$value\"," +
+// " \"edit_media\": \"http://localhost:19000/abc/EntryJsonCreateTest/Employees('1')/$value\"" +
+// " }," +
+ " \"EmployeeId\": \"1\"," +
+ " \"EmployeeName\": \"Douglas\"," +
+ " \"Age\": 42," +
+ " \"Location\": {" +
+ " \"__metadata\": {" +
+ " \"type\": \"RefScenario.c_Location\"" +
+ " }," +
+ " \"Country\": \"Britian\"," +
+ " \"City\": {" +
+ " \"__metadata\": {" +
+ " \"type\": \"RefScenario.c_City\"" +
+ " }," +
+ " \"PostalCode\": \"12345\"," +
+ " \"CityName\": \"Sample\"" +
+ " }" +
+ " }" +
+ " }" +
+ "}";
+}