[OLINGO-317] Added more data and tests

This commit is contained in:
Michael Bolz 2014-07-10 13:06:58 +02:00
parent 88cfce7e7f
commit a82f399c5f
4 changed files with 168 additions and 92 deletions

View File

@ -87,6 +87,37 @@ public class ODataJsonSerializer implements ODataSerializer {
throw new ODataRuntimeException("Metadata in JSON format not supported!");
}
@Override
public InputStream entitySet(final EdmEntitySet edmEntitySet, final EntitySet entitySet,
final ContextURL contextURL) {
CircleStreamBuffer buffer = new CircleStreamBuffer();
try {
JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
json.writeStartObject();
if (contextURL != null) {
json.writeStringField(Constants.JSON_CONTEXT, contextURL.getURI().toASCIIString());
}
if (entitySet.getCount() != null) {
json.writeNumberField("@odata.count", entitySet.getCount());
}
json.writeFieldName(Constants.VALUE);
json.writeStartArray();
for (Entity entity : entitySet.getEntities()) {
writeEntity(edmEntitySet.getEntityType(), entity, null, json);
}
json.writeEndArray();
if (entitySet.getNext() != null) {
json.writeStringField("@odata.nextLink", entitySet.getNext().toASCIIString());
}
json.close();
} catch (final IOException e) {
throw new ODataRuntimeException(e);
} catch (final EdmPrimitiveTypeException e) {
throw new ODataRuntimeException(e);
}
return buffer.getInputStream();
}
@Override
public InputStream entity(final EdmEntityType edmEntityType, final Entity entity, final ContextURL contextURL) {
CircleStreamBuffer buffer = new CircleStreamBuffer();
@ -136,9 +167,9 @@ public class ODataJsonSerializer implements ODataSerializer {
}
} else {
if (edmProperty.isCollection()) {
handleCollection(edmProperty, property, json);
writeCollection(edmProperty, property, json);
} else if (edmProperty.isPrimitive()) {
handlePrimitive(edmProperty, property, json);
writePrimitive(edmProperty, property, json);
} else if (property.isLinkedComplex()) {
writeComplexValue(edmProperty, property.asLinkedComplex().getValue(), json);
} else if(property.isComplex()) {
@ -149,7 +180,7 @@ public class ODataJsonSerializer implements ODataSerializer {
}
}
private void handleCollection(EdmProperty edmProperty, Property property, JsonGenerator json)
private void writeCollection(EdmProperty edmProperty, Property property, JsonGenerator json)
throws IOException, EdmPrimitiveTypeException {
json.writeStartArray();
for (Object value : property.asCollection()) {
@ -175,14 +206,14 @@ public class ODataJsonSerializer implements ODataSerializer {
json.writeEndArray();
}
private void handlePrimitive(EdmProperty edmProperty, Property property, JsonGenerator json)
private void writePrimitive(EdmProperty edmProperty, Property property, JsonGenerator json)
throws EdmPrimitiveTypeException, IOException {
if (property.isPrimitive()) {
writePrimitiveValue(edmProperty, property.asPrimitive(), json);
} else if (property.isGeospatial()) {
throw new ODataRuntimeException("Property type not yet supported!");
} else if (property.isEnum()) {
json.writeString(property.asEnum().toString());
writePrimitiveValue(edmProperty, property.asEnum(), json);
} else {
throw new ODataRuntimeException("Inconsistent property type!");
}
@ -230,35 +261,4 @@ public class ODataJsonSerializer implements ODataSerializer {
}
return null;
}
@Override
public InputStream entitySet(final EdmEntitySet edmEntitySet, final EntitySet entitySet,
final ContextURL contextURL) {
CircleStreamBuffer buffer = new CircleStreamBuffer();
try {
JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
json.writeStartObject();
if (contextURL != null) {
json.writeStringField(Constants.JSON_CONTEXT, contextURL.getURI().toASCIIString());
}
if (entitySet.getCount() != null) {
json.writeNumberField("@odata.count", entitySet.getCount());
}
json.writeFieldName(Constants.VALUE);
json.writeStartArray();
for (Entity entity : entitySet.getEntities()) {
writeEntity(edmEntitySet.getEntityType(), entity, null, json);
}
json.writeEndArray();
if (entitySet.getNext() != null) {
json.writeStringField("@odata.nextLink", entitySet.getNext().toASCIIString());
}
json.close();
} catch (final IOException e) {
throw new ODataRuntimeException(e);
} catch (final EdmPrimitiveTypeException e) {
throw new ODataRuntimeException(e);
}
return buffer.getInputStream();
}
}

View File

@ -47,6 +47,8 @@ import org.apache.olingo.server.api.uri.UriParameter;
public class DataProvider {
private static final UUID GUID = UUID.fromString("01234567-89ab-cdef-0123-456789abcdef");
private final Edm edm;
private Map<String, EntitySet> data;
@ -58,6 +60,7 @@ public class DataProvider {
data.put("ESCompAllPrim", createESCompAllPrim());
data.put("ESCollAllPrim", createESCollAllPrim());
data.put("ESMixPrimCollComp", createESMixPrimCollComp());
data.put("ESAllKey", createESAllKey());
}
public EntitySet readAll(final EdmEntitySet edmEntitySet) throws DataProviderException {
@ -126,7 +129,7 @@ public class DataProvider {
entitySet.getEntities().add(entity);
entity = new EntityImpl();
entity.addProperty(createPrimitive("PropertyInt16", 32767));
entity.addProperty(createPrimitive("PropertyInt16", Short.MAX_VALUE));
entity.addProperty(createPrimitive("PropertyString", "Test String4"));
entitySet.getEntities().add(entity);
@ -137,13 +140,13 @@ public class DataProvider {
EntitySet entitySet = new EntitySetImpl();
Entity entity = new EntityImpl();
entity.addProperty(createPrimitive("PropertyInt16", 32767));
entity.addProperty(createPrimitive("PropertyInt16", Short.MAX_VALUE));
entity.addProperty(createPrimitive("PropertyString", "First Resource - positive values"));
entity.addProperty(createPrimitive("PropertyBoolean", true));
entity.addProperty(createPrimitive("PropertyByte", 255));
entity.addProperty(createPrimitive("PropertySByte", 127));
entity.addProperty(createPrimitive("PropertyInt32", 2147483647));
entity.addProperty(createPrimitive("PropertyInt64", 9223372036854775807L));
entity.addProperty(createPrimitive("PropertySByte", Byte.MAX_VALUE));
entity.addProperty(createPrimitive("PropertyInt32", Integer.MAX_VALUE));
entity.addProperty(createPrimitive("PropertyInt64", Long.MAX_VALUE));
entity.addProperty(createPrimitive("PropertySingle", 1.79000000E+20));
entity.addProperty(createPrimitive("PropertyDouble", -1.7900000000000000E+19));
entity.addProperty(createPrimitive("PropertyDecimal", 34));
@ -152,18 +155,18 @@ public class DataProvider {
entity.addProperty(createPrimitive("PropertyDate", getDateTime(2012, 12, 3, 0, 0, 0)));
entity.addProperty(createPrimitive("PropertyDateTimeOffset", getDateTime(2012, 12, 3, 7, 16, 23)));
entity.addProperty(createPrimitive("PropertyDuration", 6));
entity.addProperty(createPrimitive("PropertyGuid", UUID.fromString("01234567-89ab-cdef-0123-456789abcdef")));
entity.addProperty(createPrimitive("PropertyGuid", GUID));
entity.addProperty(createPrimitive("PropertyTimeOfDay", getTime(3, 26, 5)));
entitySet.getEntities().add(entity);
entity = new EntityImpl();
entity.addProperty(createPrimitive("PropertyInt16", -32768));
entity.addProperty(createPrimitive("PropertyInt16", Short.MIN_VALUE));
entity.addProperty(createPrimitive("PropertyString", "Second Resource - negative values"));
entity.addProperty(createPrimitive("PropertyBoolean", false));
entity.addProperty(createPrimitive("PropertyByte", 0));
entity.addProperty(createPrimitive("PropertySByte", -128));
entity.addProperty(createPrimitive("PropertyInt32", -2147483648));
entity.addProperty(createPrimitive("PropertyInt64", -9223372036854775808L));
entity.addProperty(createPrimitive("PropertySByte", Byte.MIN_VALUE));
entity.addProperty(createPrimitive("PropertyInt32", Integer.MIN_VALUE));
entity.addProperty(createPrimitive("PropertyInt64", Long.MIN_VALUE));
entity.addProperty(createPrimitive("PropertySingle", -1.79000000E+08));
entity.addProperty(createPrimitive("PropertyDouble", -1.7900000000000000E+05));
entity.addProperty(createPrimitive("PropertyDecimal", -34));
@ -202,7 +205,7 @@ public class DataProvider {
EntitySet entitySet = new EntitySetImpl();
Entity entity = new EntityImpl();
entity.addProperty(createPrimitive("PropertyInt16", 32767));
entity.addProperty(createPrimitive("PropertyInt16", Short.MAX_VALUE));
LinkedComplexValue complexValue = new LinkedComplexValueImpl();
complexValue.getValue().add(createPrimitive("PropertyString", "First Resource - first"));
complexValue.getValue().add(createPrimitive("PropertyBinary",
@ -216,12 +219,11 @@ public class DataProvider {
complexValue.getValue().add(createPrimitive("PropertySingle", 1.79000000E+20));
complexValue.getValue().add(createPrimitive("PropertyDouble", -1.7900000000000000E+19));
complexValue.getValue().add(createPrimitive("PropertyDuration", 6));
complexValue.getValue().add(createPrimitive("PropertyGuid",
UUID.fromString("01234567-89ab-cdef-0123-456789abcdef")));
complexValue.getValue().add(createPrimitive("PropertyInt16", 32767));
complexValue.getValue().add(createPrimitive("PropertyInt32", 2147483647));
complexValue.getValue().add(createPrimitive("PropertyInt64", 9223372036854775807L));
complexValue.getValue().add(createPrimitive("PropertySByte", 127));
complexValue.getValue().add(createPrimitive("PropertyGuid", GUID));
complexValue.getValue().add(createPrimitive("PropertyInt16", Short.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertyInt32", Integer.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertyInt64", Long.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertySByte", Byte.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertyTimeOfDay", getTime(1, 0, 1)));
entity.addProperty(new PropertyImpl(null, "PropertyComp", ValueType.LINKED_COMPLEX, complexValue));
entitySet.getEntities().add(entity);
@ -241,14 +243,12 @@ public class DataProvider {
complexValue.getValue().add(createPrimitive("PropertySingle", 1.79000000E+20));
complexValue.getValue().add(createPrimitive("PropertyDouble", -1.7900000000000000E+02));
complexValue.getValue().add(createPrimitive("PropertyDuration", 6));
complexValue.getValue().add(createPrimitive("PropertyGuid",
UUID.fromString("01234567-89ab-cdef-0123-456789abcdef")));
complexValue.getValue().add(createPrimitive("PropertyGuid", GUID));
complexValue.getValue().add(createPrimitive("PropertyInt16", 25));
complexValue.getValue().add(createPrimitive("PropertyInt32", 2147483647));
complexValue.getValue().add(createPrimitive("PropertyInt64", 9223372036854775807L));
complexValue.getValue().add(createPrimitive("PropertySByte", 127));
complexValue.getValue().add(createPrimitive("PropertyTimeOfDay",
getTimestamp(1, 1, 1, 7, 45, 12, 765432100)));
complexValue.getValue().add(createPrimitive("PropertyInt32", Integer.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertyInt64", Long.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertySByte", Byte.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertyTimeOfDay", getTimestamp(1, 1, 1, 7, 45, 12, 765432100)));
entity.addProperty(new PropertyImpl(null, "PropertyComp", ValueType.LINKED_COMPLEX, complexValue));
entitySet.getEntities().add(entity);
@ -267,12 +267,11 @@ public class DataProvider {
complexValue.getValue().add(createPrimitive("PropertySingle", 1.79000000E+20));
complexValue.getValue().add(createPrimitive("PropertyDouble", -1.7900000000000000E+02));
complexValue.getValue().add(createPrimitive("PropertyDuration", 6));
complexValue.getValue().add(createPrimitive("PropertyGuid",
UUID.fromString("01234567-89ab-cdef-0123-456789abcdef")));
complexValue.getValue().add(createPrimitive("PropertyGuid", GUID));
complexValue.getValue().add(createPrimitive("PropertyInt16", -25));
complexValue.getValue().add(createPrimitive("PropertyInt32", 2147483647));
complexValue.getValue().add(createPrimitive("PropertyInt64", 9223372036854775807L));
complexValue.getValue().add(createPrimitive("PropertySByte", 127));
complexValue.getValue().add(createPrimitive("PropertyInt32", Integer.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertyInt64", Long.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertySByte", Byte.MAX_VALUE));
complexValue.getValue().add(createPrimitive("PropertyTimeOfDay", getTime(13, 27, 45)));
entity.addProperty(new PropertyImpl(null, "PropertyComp", ValueType.LINKED_COMPLEX, complexValue));
entitySet.getEntities().add(entity);
@ -331,7 +330,7 @@ public class DataProvider {
EntitySet entitySet = new EntitySetImpl();
Entity entity = new EntityImpl();
entity.addProperty(createPrimitive("PropertyInt16", 32767));
entity.addProperty(createPrimitive("PropertyInt16", Short.MAX_VALUE));
entity.addProperty(createCollection("CollPropertyString",
"spiderman@comic.com", "spidermaus@comic.com", "spidergirl@comic.com"));
LinkedComplexValue complexValue = new LinkedComplexValueImpl();
@ -382,6 +381,44 @@ public class DataProvider {
return entitySet;
}
private EntitySet createESAllKey() {
EntitySet entitySet = new EntitySetImpl();
Entity entity = new EntityImpl();
entity.addProperty(createPrimitive("PropertyString", "First"));
entity.addProperty(createPrimitive("PropertyBoolean", true));
entity.addProperty(createPrimitive("PropertyByte", 255));
entity.addProperty(createPrimitive("PropertySByte", Byte.MAX_VALUE));
entity.addProperty(createPrimitive("PropertyInt16", Short.MAX_VALUE));
entity.addProperty(createPrimitive("PropertyInt32", Integer.MAX_VALUE));
entity.addProperty(createPrimitive("PropertyInt64", Long.MAX_VALUE));
entity.addProperty(createPrimitive("PropertyDecimal", 34));
entity.addProperty(createPrimitive("PropertyDate", getDateTime(2012, 12, 3, 0, 0, 0)));
entity.addProperty(createPrimitive("PropertyDateTimeOffset", getDateTime(2012, 12, 3, 7, 16, 23)));
entity.addProperty(createPrimitive("PropertyDuration", 6));
entity.addProperty(createPrimitive("PropertyGuid", GUID));
entity.addProperty(createPrimitive("PropertyTimeOfDay", getTime(2, 48, 21)));
entitySet.getEntities().add(entity);
entity = new EntityImpl();
entity.addProperty(createPrimitive("PropertyString", "Second"));
entity.addProperty(createPrimitive("PropertyBoolean", true));
entity.addProperty(createPrimitive("PropertyByte", 254));
entity.addProperty(createPrimitive("PropertySByte", 124));
entity.addProperty(createPrimitive("PropertyInt16", 32764));
entity.addProperty(createPrimitive("PropertyInt32", 2147483644));
entity.addProperty(createPrimitive("PropertyInt64", 9223372036854775804L));
entity.addProperty(createPrimitive("PropertyDecimal", 34));
entity.addProperty(createPrimitive("PropertyDate", getDateTime(2012, 12, 3, 0, 0, 0)));
entity.addProperty(createPrimitive("PropertyDateTimeOffset", getDateTime(2012, 12, 3, 7, 16, 23)));
entity.addProperty(createPrimitive("PropertyDuration", 6));
entity.addProperty(createPrimitive("PropertyGuid", GUID));
entity.addProperty(createPrimitive("PropertyTimeOfDay", getTime(2, 48, 21)));
entitySet.getEntities().add(entity);
return entitySet;
}
private Property createPrimitive(final String name, final Object value) {
return new PropertyImpl(null, name, ValueType.PRIMITIVE, value);
}

View File

@ -38,9 +38,9 @@ import org.apache.olingo.server.api.processor.CollectionProcessor;
import org.apache.olingo.server.api.processor.EntityProcessor;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.UriInfoResource;
import org.apache.olingo.server.api.uri.UriResource;
import org.apache.olingo.server.api.uri.UriResourceEntitySet;
import org.apache.olingo.server.api.uri.UriResourceKind;
import org.apache.olingo.server.tecsvc.data.DataProvider;
public class TechnicalProcessor implements CollectionProcessor, EntityProcessor {
@ -62,8 +62,12 @@ public class TechnicalProcessor implements CollectionProcessor, EntityProcessor
@Override
public void readCollection(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
final ContentType requestedContentType) {
if (!validateOptions(uriInfo.asUriInfoResource())) {
response.setStatusCode(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode());
return;
}
ODataSerializer serializer = odata.createSerializer(ODataFormat.JSON);
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo);
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
try {
final EntitySet entitySet = readEntitySetInternal(edmEntitySet, request.getRawBaseUri());
if (entitySet == null) {
@ -82,10 +86,14 @@ public class TechnicalProcessor implements CollectionProcessor, EntityProcessor
@Override
public void readEntity(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
final ContentType requestedContentType) {
if (!validateOptions(uriInfo.asUriInfoResource())) {
response.setStatusCode(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode());
return;
}
ODataSerializer serializer = odata.createSerializer(ODataFormat.JSON);
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo);
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
try {
final Entity entity = readEntityInternal(uriInfo, edmEntitySet);
final Entity entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
if (entity == null) {
response.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode());
} else {
@ -99,23 +107,6 @@ public class TechnicalProcessor implements CollectionProcessor, EntityProcessor
}
}
private Entity readEntityInternal(final UriInfo uriInfo, final EdmEntitySet entitySet)
throws DataProvider.DataProviderException {
final List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
if (!resourcePaths.isEmpty()) {
UriResource res = resourcePaths.get(resourcePaths.size() - 1);
if (res.getKind() == UriResourceKind.entitySet) {
UriResourceEntitySet resourceEntitySet = (UriResourceEntitySet) res;
return dataProvider.read(entitySet, resourceEntitySet.getKeyPredicates());
}
}
throw new RuntimeException("Invalid resource paths.. " + resourcePaths);
}
private ContextURL getContextUrl(final ODataRequest request, final EdmEntityType entityType) {
return ContextURL.getInstance(URI.create(request.getRawBaseUri() + "/" + entityType.getName()));
}
private EntitySet readEntitySetInternal(final EdmEntitySet edmEntitySet, final String serviceRoot)
throws DataProvider.DataProviderException {
EntitySet entitySet = dataProvider.readAll(edmEntitySet);
@ -123,13 +114,42 @@ public class TechnicalProcessor implements CollectionProcessor, EntityProcessor
return entitySet;
}
private EdmEntitySet getEdmEntitySet(final UriInfo uriInfo) {
private Entity readEntityInternal(final UriInfoResource uriInfo, final EdmEntitySet entitySet)
throws DataProvider.DataProviderException {
final UriResourceEntitySet resourceEntitySet = (UriResourceEntitySet) uriInfo.getUriResourceParts().get(0);
return dataProvider.read(entitySet, resourceEntitySet.getKeyPredicates());
}
private boolean validateOptions(final UriInfoResource uriInfo) {
return uriInfo.getCountOption() == null
&& uriInfo.getCustomQueryOptions().isEmpty()
&& uriInfo.getExpandOption() == null
&& uriInfo.getFilterOption() == null
&& uriInfo.getIdOption() == null
&& uriInfo.getOrderByOption() == null
&& uriInfo.getSearchOption() == null
&& uriInfo.getSelectOption() == null
&& uriInfo.getSkipOption() == null
&& uriInfo.getSkipTokenOption() == null
&& uriInfo.getTopOption() == null;
}
private EdmEntitySet getEdmEntitySet(final UriInfoResource uriInfo) {
final List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
if (resourcePaths.isEmpty()) {
if (resourcePaths.size() != 1) {
throw new RuntimeException("Invalid resource path.");
}
final UriResource uriResource = resourcePaths.get(resourcePaths.size() - 1);
return uriResource instanceof UriResourceEntitySet ?
((UriResourceEntitySet) uriResource).getEntitySet() : null;
if (!(resourcePaths.get(0) instanceof UriResourceEntitySet)) {
throw new RuntimeException("Invalid resource type.");
}
final UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);
if (uriResource.getTypeFilterOnCollection() != null || uriResource.getTypeFilterOnEntry() != null) {
throw new RuntimeException("Type filters are not supported.");
}
return uriResource.getEntitySet();
}
private ContextURL getContextUrl(final ODataRequest request, final EdmEntityType entityType) {
return ContextURL.getInstance(URI.create(request.getRawBaseUri() + "/" + entityType.getName()));
}
}

View File

@ -161,6 +161,25 @@ public class ODataJsonSerializerTest {
Assert.assertEquals(expectedResult, resultString);
}
@Test
public void entityMixPrimCollComp() throws Exception {
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
InputStream result = serializer.entity(edmEntitySet.getEntityType(), entity,
ContextURL.getInstance(URI.create("$metadata#ESMixPrimCollComp/$entity")));
final String resultString = streamToString(result);
final String expectedResult = "{"
+ "\"@odata.context\":\"$metadata#ESMixPrimCollComp/$entity\","
+ "\"PropertyInt16\":32767,"
+ "\"CollPropertyString\":[\"spiderman@comic.com\",\"spidermaus@comic.com\",\"spidergirl@comic.com\"],"
+ "\"PropertyComp\":{\"PropertyInt16\":111,\"PropertyString\":\"TEST A\"},"
+ "\"CollPropertyComp\":["
+ "{\"PropertyInt16\":123,\"PropertyString\":\"TEST 1\"},"
+ "{\"PropertyInt16\":456,\"PropertyString\":\"TEST 2\"},"
+ "{\"PropertyInt16\":789,\"PropertyString\":\"TEST 3\"}]}";
Assert.assertEquals(expectedResult, resultString);
}
private String streamToString(InputStream input) throws IOException {
byte[] buffer = new byte[8192];
StringBuilder result = new StringBuilder();