Repository: olingo-odata4 Updated Branches: refs/heads/master 93af10589 -> 8a968923b
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/8a968923/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerv01Test.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerv01Test.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerv01Test.java new file mode 100644 index 0000000..3b41873 --- /dev/null +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerv01Test.java @@ -0,0 +1,2533 @@ +/* + * 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.server.core.serializer.json; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.nio.ByteBuffer; +import java.nio.channels.WritableByteChannel; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.olingo.commons.api.IConstants; +import org.apache.olingo.commons.api.constants.Constantsv01; +import org.apache.olingo.commons.api.data.ComplexValue; +import org.apache.olingo.commons.api.data.ContextURL; +import org.apache.olingo.commons.api.data.ContextURL.Suffix; +import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntityCollection; +import org.apache.olingo.commons.api.data.EntityIterator; +import org.apache.olingo.commons.api.data.Operation; +import org.apache.olingo.commons.api.data.Property; +import org.apache.olingo.commons.api.data.ValueType; +import org.apache.olingo.commons.api.edm.EdmComplexType; +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.EdmProperty; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.geo.Point; +import org.apache.olingo.commons.api.edm.geo.Polygon; +import org.apache.olingo.commons.api.edm.geo.SRID; +import org.apache.olingo.commons.api.edm.geo.Geospatial.Dimension; +import org.apache.olingo.commons.api.edm.geo.GeospatialCollection; +import org.apache.olingo.commons.api.edm.geo.LineString; +import org.apache.olingo.commons.api.edm.geo.MultiLineString; +import org.apache.olingo.commons.api.edm.geo.MultiPoint; +import org.apache.olingo.commons.api.edm.geo.MultiPolygon; +import org.apache.olingo.commons.api.edmx.EdmxReference; +import org.apache.olingo.commons.api.format.ContentType; +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataContent; +import org.apache.olingo.server.api.ODataContentWriteErrorCallback; +import org.apache.olingo.server.api.ODataContentWriteErrorContext; +import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.serializer.ComplexSerializerOptions; +import org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions; +import org.apache.olingo.server.api.serializer.EntitySerializerOptions; +import org.apache.olingo.server.api.serializer.ODataSerializer; +import org.apache.olingo.server.api.serializer.PrimitiveSerializerOptions; +import org.apache.olingo.server.api.serializer.ReferenceCollectionSerializerOptions; +import org.apache.olingo.server.api.serializer.ReferenceSerializerOptions; +import org.apache.olingo.server.api.serializer.SerializerException; +import org.apache.olingo.server.api.serializer.SerializerResult; +import org.apache.olingo.server.api.uri.UriHelper; +import org.apache.olingo.server.api.uri.queryoption.CountOption; +import org.apache.olingo.server.api.uri.queryoption.ExpandItem; +import org.apache.olingo.server.api.uri.queryoption.ExpandOption; +import org.apache.olingo.server.api.uri.queryoption.LevelsExpandOption; +import org.apache.olingo.server.api.uri.queryoption.SelectItem; +import org.apache.olingo.server.api.uri.queryoption.SelectOption; +import org.apache.olingo.server.core.serializer.ExpandSelectMock; +import org.apache.olingo.server.tecsvc.MetadataETagSupport; +import org.apache.olingo.server.tecsvc.data.DataProvider; +import org.apache.olingo.server.tecsvc.provider.EdmTechProvider; +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +public class ODataJsonSerializerv01Test { + private static final OData odata = OData.newInstance(); + private static final ServiceMetadata metadata = odata.createServiceMetadata( + new EdmTechProvider(), Collections.<EdmxReference> emptyList(), new MetadataETagSupport("W/\"metadataETag\"")); + private static final EdmEntityContainer entityContainer = metadata.getEdm().getEntityContainer(); + private final DataProvider data = new DataProvider(odata, metadata.getEdm()); + private final IConstants constants = new Constantsv01(); + private final ODataSerializer serializer = new ODataJsonSerializer(ContentType.JSON, constants); + private final ODataSerializer serializerNoMetadata = new ODataJsonSerializer( + ContentType.JSON_NO_METADATA, constants); + private final ODataSerializer serializerFullMetadata = new ODataJsonSerializer( + ContentType.JSON_FULL_METADATA, constants); + private final ODataSerializer serializerIEEECompatible = + new ODataJsonSerializer(ContentType.create(ContentType.JSON, + ContentType.PARAMETER_IEEE754_COMPATIBLE, "true"), + constants); + private final UriHelper helper = odata.createUriHelper(); + + @Test + public void entitySimple() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{" + + "\"@context\":\"$metadata#ESAllPrim/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32767," + + "\"PropertyString\":\"First Resource - positive values\"," + + "\"PropertyBoolean\":true," + + "\"PropertyByte\":255," + + "\"PropertySByte\":127," + + "\"PropertyInt32\":2147483647," + + "\"PropertyInt64\":9223372036854775807," + + "\"PropertySingle\":1.79E20," + + "\"PropertyDouble\":-1.79E19," + + "\"PropertyDecimal\":34," + + "\"PropertyBinary\":\"ASNFZ4mrze8=\"," + + "\"PropertyDate\":\"2012-12-03\"," + + "\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\"," + + "\"PropertyDuration\":\"PT6S\"," + + "\"PropertyGuid\":\"01234567-89ab-cdef-0123-456789abcdef\"," + + "\"PropertyTimeOfDay\":\"03:26:05\"" + + "}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entitySimpleMetadataFull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + InputStream result = serializerFullMetadata.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expected = "{\"@context\":\"$metadata#ESAllPrim/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@type\":\"#olingo.odata.test1.ETAllPrim\"," + + "\"@id\":\"ESAllPrim(32767)\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":32767," + + "\"PropertyString\":\"First Resource - positive values\"," + + "\"PropertyBoolean\":true," + + "\"PropertyByte@type\":\"#Byte\"," + + "\"PropertyByte\":255," + + "\"PropertySByte@type\":\"#SByte\"," + + "\"PropertySByte\":127," + + "\"PropertyInt32@type\":\"#Int32\"," + + "\"PropertyInt32\":2147483647," + + "\"PropertyInt64@type\":\"#Int64\"," + + "\"PropertyInt64\":9223372036854775807," + + "\"PropertySingle@type\":\"#Single\"," + + "\"PropertySingle\":1.79E20," + + "\"PropertyDouble\":-1.79E19," + + "\"PropertyDecimal@type\":\"#Decimal\"," + + "\"PropertyDecimal\":34," + + "\"PropertyBinary@type\":\"#Binary\"," + + "\"PropertyBinary\":\"ASNFZ4mrze8=\"," + + "\"PropertyDate@type\":\"#Date\"," + + "\"PropertyDate\":\"2012-12-03\"," + + "\"PropertyDateTimeOffset@type\":\"#DateTimeOffset\"," + + "\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\"," + + "\"PropertyDuration@type\":\"#Duration\"," + + "\"PropertyDuration\":\"PT6S\"," + + "\"PropertyGuid@type\":\"#Guid\"," + + "\"PropertyGuid\":\"01234567-89ab-cdef-0123-456789abcdef\"," + + "\"PropertyTimeOfDay@type\":\"#TimeOfDay\"," + + "\"PropertyTimeOfDay\":\"03:26:05\"," + + "\"NavPropertyETTwoPrimOne@navigationLink\":\"ESTwoPrim(32767)\"," + + "\"NavPropertyETTwoPrimMany@navigationLink\":\"ESAllPrim(32767)/NavPropertyETTwoPrimMany\"," + + "\"#olingo.odata.test1.BAETAllPrimRT\":{" + + "\"title\":\"olingo.odata.test1.BAETAllPrimRT\"," + + "\"target\":\"ESAllPrim(32767)/olingo.odata.test1.BAETAllPrimRT\"" + + "}}"; + + Assert.assertEquals(expected, resultString); + } + + @Test + public void entitySetMetadataFull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final EntityCollection entityCol = data.readAll(edmEntitySet); + InputStream result = serializerFullMetadata.entityCollection(metadata, edmEntitySet.getEntityType(), entityCol, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expected = "{" + + "\"@context\":\"$metadata#ESAllPrim\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"#olingo.odata.test1.BAESAllPrimRTETAllPrim\":{" + + "\"title\":\"olingo.odata.test1.BAESAllPrimRTETAllPrim\"," + + "\"target\":\"ESAllPrim/olingo.odata.test1.BAESAllPrimRTETAllPrim\"" + + "}," + + "\"#olingo.odata.test1.BAESAllPrimRT\":{" + + "\"title\":\"olingo.odata.test1.BAESAllPrimRT\"," + + "\"target\":\"ESAllPrim/olingo.odata.test1.BAESAllPrimRT\"" + + "}," + + "\"#olingo.odata.test1.BFNESAllPrimRTCTAllPrim\":{" + + "\"title\":\"olingo.odata.test1.BFNESAllPrimRTCTAllPrim\"," + + "\"target\":\"ESAllPrim/olingo.odata.test1.BFNESAllPrimRTCTAllPrim\"" + + "}," + + "\"value\":[" + + "{" + + "\"@type\":\"#olingo.odata.test1.ETAllPrim\"," + + "\"@id\":\"ESAllPrim(32767)\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":32767," + + "\"PropertyString\":\"First Resource - positive values\"," + + "\"PropertyBoolean\":true," + + "\"PropertyByte@type\":\"#Byte\"," + + "\"PropertyByte\":255," + + "\"PropertySByte@type\":\"#SByte\"," + + "\"PropertySByte\":127," + + "\"PropertyInt32@type\":\"#Int32\"," + + "\"PropertyInt32\":2147483647," + + "\"PropertyInt64@type\":\"#Int64\"," + + "\"PropertyInt64\":9223372036854775807," + + "\"PropertySingle@type\":\"#Single\"," + + "\"PropertySingle\":1.79E20," + + "\"PropertyDouble\":-1.79E19," + + "\"PropertyDecimal@type\":\"#Decimal\"," + + "\"PropertyDecimal\":34," + + "\"PropertyBinary@type\":\"#Binary\"," + + "\"PropertyBinary\":\"ASNFZ4mrze8=\"," + + "\"PropertyDate@type\":\"#Date\"," + + "\"PropertyDate\":\"2012-12-03\"," + + "\"PropertyDateTimeOffset@type\":\"#DateTimeOffset\"," + + "\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\"," + + "\"PropertyDuration@type\":\"#Duration\"," + + "\"PropertyDuration\":\"PT6S\"," + + "\"PropertyGuid@type\":\"#Guid\"," + + "\"PropertyGuid\":\"01234567-89ab-cdef-0123-456789abcdef\"," + + "\"PropertyTimeOfDay@type\":\"#TimeOfDay\"," + + "\"PropertyTimeOfDay\":\"03:26:05\"," + + "\"NavPropertyETTwoPrimOne@navigationLink\":\"ESTwoPrim(32767)\"," + + "\"NavPropertyETTwoPrimMany@navigationLink\":\"ESAllPrim(32767)/NavPropertyETTwoPrimMany\"," + + "\"#olingo.odata.test1.BAETAllPrimRT\":{" + + "\"title\":\"olingo.odata.test1.BAETAllPrimRT\"," + + "\"target\":\"ESAllPrim(32767)/olingo.odata.test1.BAETAllPrimRT\"" + + "}},"; + + Assert.assertTrue(resultString.startsWith(expected)); + } + + @Test + public void entitySetMetadataFullWithExpand() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final EntityCollection entityCol = data.readAll(edmEntitySet); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList( + ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETTwoPrimOne"))); + InputStream result = serializerFullMetadata.entityCollection(metadata, edmEntitySet.getEntityType(), entityCol, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .expand(expand) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expected = "{" + + "\"@context\":\"$metadata#ESAllPrim\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"#olingo.odata.test1.BAESAllPrimRTETAllPrim\":{" + + "\"title\":\"olingo.odata.test1.BAESAllPrimRTETAllPrim\"," + + "\"target\":\"ESAllPrim/olingo.odata.test1.BAESAllPrimRTETAllPrim\"" + + "}," + + "\"#olingo.odata.test1.BAESAllPrimRT\":{" + + "\"title\":\"olingo.odata.test1.BAESAllPrimRT\"," + + "\"target\":\"ESAllPrim/olingo.odata.test1.BAESAllPrimRT\"" + + "}," + + "\"#olingo.odata.test1.BFNESAllPrimRTCTAllPrim\":{" + + "\"title\":\"olingo.odata.test1.BFNESAllPrimRTCTAllPrim\"," + + "\"target\":\"ESAllPrim/olingo.odata.test1.BFNESAllPrimRTCTAllPrim\"" + + "}," + + "\"value\":[" + + "{" + + "\"@type\":\"#olingo.odata.test1.ETAllPrim\"," + + "\"@id\":\"ESAllPrim(32767)\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":32767," + + "\"PropertyString\":\"First Resource - positive values\"," + + "\"PropertyBoolean\":true," + + "\"PropertyByte@type\":\"#Byte\"," + + "\"PropertyByte\":255," + + "\"PropertySByte@type\":\"#SByte\"," + + "\"PropertySByte\":127," + + "\"PropertyInt32@type\":\"#Int32\"," + + "\"PropertyInt32\":2147483647," + + "\"PropertyInt64@type\":\"#Int64\"," + + "\"PropertyInt64\":9223372036854775807," + + "\"PropertySingle@type\":\"#Single\"," + + "\"PropertySingle\":1.79E20," + + "\"PropertyDouble\":-1.79E19," + + "\"PropertyDecimal@type\":\"#Decimal\"," + + "\"PropertyDecimal\":34," + + "\"PropertyBinary@type\":\"#Binary\"," + + "\"PropertyBinary\":\"ASNFZ4mrze8=\"," + + "\"PropertyDate@type\":\"#Date\"," + + "\"PropertyDate\":\"2012-12-03\"," + + "\"PropertyDateTimeOffset@type\":\"#DateTimeOffset\"," + + "\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\"," + + "\"PropertyDuration@type\":\"#Duration\"," + + "\"PropertyDuration\":\"PT6S\"," + + "\"PropertyGuid@type\":\"#Guid\"," + + "\"PropertyGuid\":\"01234567-89ab-cdef-0123-456789abcdef\"," + + "\"PropertyTimeOfDay@type\":\"#TimeOfDay\"," + + "\"PropertyTimeOfDay\":\"03:26:05\"," + + "\"NavPropertyETTwoPrimOne@navigationLink\":\"ESTwoPrim(32767)\"," + + "\"NavPropertyETTwoPrimMany@navigationLink\":\"ESAllPrim(32767)/NavPropertyETTwoPrimMany\"," + + "\"NavPropertyETTwoPrimOne\":{" + + "\"@type\":\"#olingo.odata.test1.ETTwoPrim\"," + + "\"@id\":\"ESTwoPrim(32767)\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":32767," + + "\"PropertyString\":\"Test String4\"," + + "\"NavPropertyETAllPrimOne@navigationLink\":\"ESAllPrim(32767)\"," + + "\"#olingo.odata.test1.BAETTwoPrimRTString\":{" + + "\"title\":\"olingo.odata.test1.BAETTwoPrimRTString\"," + + "\"target\":\"ESTwoPrim(32767)/olingo.odata.test1.BAETTwoPrimRTString\"" + + "}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollString\":{" + + "\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollString\"," + + "\"target\":\"ESTwoPrim(32767)/olingo.odata.test1.BAETTwoPrimRTCollString\"" + + "}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCTAllPrim\":{" + + "\"title\":\"olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"," + + "\"target\":\"ESTwoPrim(32767)/olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"" + + "}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\":{" + + "\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"," + + "\"target\":\"ESTwoPrim(32767)/olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"" + + "}" + + "}," + + "\"#olingo.odata.test1.BAETAllPrimRT\":{" + + "\"title\":\"olingo.odata.test1.BAETAllPrimRT\"," + + "\"target\":\"ESAllPrim(32767)/olingo.odata.test1.BAETAllPrimRT\"" + + "}},"; + + Assert.assertTrue(resultString.startsWith(expected)); + } + + @Test + public void entityAllPrimAllNull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + entity.getProperties().retainAll(Collections.singletonList(entity.getProperties().get(0))); + final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), + entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent()); + final String expectedResult = "{\"@context\":\"$metadata#ESAllPrim/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32767," + + "\"PropertyString\":null,\"PropertyBoolean\":null," + + "\"PropertyByte\":null,\"PropertySByte\":null," + + "\"PropertyInt32\":null,\"PropertyInt64\":null," + + "\"PropertySingle\":null,\"PropertyDouble\":null,\"PropertyDecimal\":null," + + "\"PropertyBinary\":null," + + "\"PropertyDate\":null,\"PropertyDateTimeOffset\":null,\"PropertyDuration\":null," + + "\"PropertyGuid\":null,\"PropertyTimeOfDay\":null}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test(expected = SerializerException.class) + public void entityAllPrimKeyNull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + entity.getProperties().clear(); + serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()); + } + + @Test + public void entityWrongData() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + entity.getProperties().get(0).setValue(ValueType.PRIMITIVE, false); + try { + serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()); + Assert.fail("Expected exception not thrown!"); + } catch (final SerializerException e) { + Assert.assertEquals(SerializerException.MessageKeys.WRONG_PROPERTY_VALUE, e.getMessageKey()); + final String message = e.getLocalizedMessage(); + Assert.assertThat(message, CoreMatchers.containsString("PropertyInt16")); + Assert.assertThat(message, CoreMatchers.containsString("false")); + } + } + + @Test + public void entitySetCompAllPrim() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompAllPrim"); + EntityCollection entitySet = data.readAll(edmEntitySet); + entitySet.setCount(entitySet.getEntities().size()); + entitySet.setNext(URI.create("/next")); + CountOption countOption = Mockito.mock(CountOption.class); + Mockito.when(countOption.getValue()).thenReturn(true); + InputStream result = serializer.entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .count(countOption) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + + Assert.assertThat(resultString, CoreMatchers.startsWith("{" + + "\"@context\":\"$metadata#ESCompAllPrim\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@count\":4,\"value\":[" + + "{\"@etag\":\"W/\\\"32767\\\"\",")); + Assert.assertThat(resultString, CoreMatchers.endsWith("}}]," + + "\"@nextLink\":\"/next\"}")); + + int count = 0; + int index = -1; + while ((index = resultString.indexOf("PropertyInt16\":", ++index)) > 0) { + count++; + } + Assert.assertEquals(8, count); + } + + @Test + public void entityCollectionStreamed() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final EntityIterator entityIterator = new EntityIterator() { + EntityCollection entityCollection = data.readAll(edmEntitySet); + Iterator<Entity> innerIterator = entityCollection.iterator(); + + @Override + public List<Operation> getOperations() { + return entityCollection.getOperations(); + } + + @Override + public boolean hasNext() { + return innerIterator.hasNext(); + } + @Override + public Entity next() { + return innerIterator.next(); + } + }; + CountOption countOption = Mockito.mock(CountOption.class); + Mockito.when(countOption.getValue()).thenReturn(true); + + ODataContent result = serializer.entityCollectionStreamed( + metadata, edmEntitySet.getEntityType(), entityIterator, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()).getODataContent(); + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + result.write(bout); + final String resultString = new String(bout.toByteArray(), "UTF-8"); + + Assert.assertThat(resultString, CoreMatchers.startsWith("{" + + "\"@context\":\"$metadata#ESAllPrim\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{\"PropertyInt16\":32767,\"PropertyString\"")); + Assert.assertThat(resultString, CoreMatchers.endsWith( + "\"PropertyTimeOfDay\":\"00:01:01\"}]}")); + + int count = 0; + int index = -1; + while ((index = resultString.indexOf("PropertyInt16\":", ++index)) > 0) { + count++; + } + Assert.assertEquals(3, count); + } + + @Test + public void entityCollectionStreamedWithError() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final EntityIterator entityIterator = new EntityIterator() { + EntityCollection entityCollection = data.readAll(edmEntitySet); + Iterator<Entity> innerIterator = entityCollection.iterator(); + + @Override + public List<Operation> getOperations() { + return entityCollection.getOperations(); + } + + @Override + public boolean hasNext() { + return innerIterator.hasNext(); + } + @Override + public Entity next() { + Entity e = new Entity(); + e.setId(URI.create("id")); + return e; + } + }; + CountOption countOption = Mockito.mock(CountOption.class); + Mockito.when(countOption.getValue()).thenReturn(true); + + ODataContentWriteErrorCallback errorCallback = new ODataContentWriteErrorCallback() { + @Override + public void handleError(ODataContentWriteErrorContext context, WritableByteChannel channel) { + try { + Exception ex = context.getException(); + Assert.assertEquals(ex, context.getODataLibraryException()); + String msgKey = context.getODataLibraryException().getMessageKey().getKey(); + String toChannel = "ERROR: " + msgKey; + channel.write(ByteBuffer.wrap(toChannel.getBytes("UTF-8"))); + } catch (IOException e) { + throw new RuntimeException("Error in error."); + } + } + }; + + ODataContent result = serializer.entityCollectionStreamed( + metadata, edmEntitySet.getEntityType(), entityIterator, + EntityCollectionSerializerOptions.with() + .writeContentErrorCallback(errorCallback) + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()).getODataContent(); + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + result.write(bout); + final String resultString = new String(bout.toByteArray(), "UTF-8"); + Assert.assertEquals("ERROR: MISSING_PROPERTY", resultString); + } + + + @Test + public void entityCollAllPrim() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCollAllPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().serviceRoot(URI.create("http://host/service/")) + .entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{" + + "\"@context\":\"http://host/service/$metadata#ESCollAllPrim/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":1," + + "\"CollPropertyString\":" + + "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]," + + "\"CollPropertyBoolean\":[true,false,true]," + + "\"CollPropertyByte\":[50,200,249]," + + "\"CollPropertySByte\":[-120,120,126]," + + "\"CollPropertyInt16\":[1000,2000,30112]," + + "\"CollPropertyInt32\":[23232323,11223355,10000001]," + + "\"CollPropertyInt64\":[929292929292,333333333333,444444444444]," + + "\"CollPropertySingle\":[1790.0,26600.0,3210.0]," + + "\"CollPropertyDouble\":[-17900.0,-2.78E7,3210.0]," + + "\"CollPropertyDecimal\":[12,-2,1234]," + + "\"CollPropertyBinary\":[\"q83v\",\"ASNF\",\"VGeJ\"]," + + "\"CollPropertyDate\":[\"1958-12-03\",\"1999-08-05\",\"2013-06-25\"]," + + "\"CollPropertyDateTimeOffset\":[\"2015-08-12T03:08:34Z\",\"1970-03-28T12:11:10Z\"," + + "\"1948-02-17T09:09:09Z\"]," + + "\"CollPropertyDuration\":[\"PT13S\",\"PT5H28M0S\",\"PT1H0S\"]," + + "\"CollPropertyGuid\":[\"ffffff67-89ab-cdef-0123-456789aaaaaa\",\"eeeeee67-89ab-cdef-0123-456789bbbbbb\"," + + "\"cccccc67-89ab-cdef-0123-456789cccccc\"]," + + "\"CollPropertyTimeOfDay\":[\"04:14:13\",\"23:59:59\",\"01:12:33\"]" + + "}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entityCompAllPrim() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompAllPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{" + + "\"@context\":\"$metadata#ESCompAllPrim/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@etag\":\"W/\\\"32767\\\"\"," + + "\"PropertyInt16\":32767," + + "\"PropertyComp\":{" + + "\"PropertyString\":\"First Resource - first\"," + + "\"PropertyBinary\":\"ASNFZ4mrze8=\"," + + "\"PropertyBoolean\":true," + + "\"PropertyByte\":255," + + "\"PropertyDate\":\"2012-10-03\"," + + "\"PropertyDateTimeOffset\":\"2012-10-03T07:16:23.1234567Z\"," + + "\"PropertyDecimal\":34.27," + + "\"PropertySingle\":1.79E20," + + "\"PropertyDouble\":-1.79E19," + + "\"PropertyDuration\":\"PT6S\"," + + "\"PropertyGuid\":\"01234567-89ab-cdef-0123-456789abcdef\"," + + "\"PropertyInt16\":32767," + + "\"PropertyInt32\":2147483647," + + "\"PropertyInt64\":9223372036854775807," + + "\"PropertySByte\":127," + + "\"PropertyTimeOfDay\":\"01:00:01\"" + + "}}"; + 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(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{" + + "\"@context\":\"$metadata#ESMixPrimCollComp/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32767," + + "\"CollPropertyString\":" + + "[\"[email protected]\",\"[email protected]\",\"[email protected]\"]," + + "\"PropertyComp\":{\"PropertyInt16\":111,\"PropertyString\":\"TEST A\"}," + + "\"CollPropertyComp\":[" + + "{\"PropertyInt16\":123,\"PropertyString\":\"TEST 1\"}," + + "{\"PropertyInt16\":456,\"PropertyString\":\"TEST 2\"}," + + "{\"@type\":\"#olingo.odata.test1.CTBase\",\"PropertyInt16\":789," + + "\"PropertyString\":\"TEST 3\",\"AdditionalPropString\":\"ADD TEST\"}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void derivedEntityESCompCollDerived() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollDerived"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(1); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{" + + "\"@context\":\"$metadata#ESCompCollDerived/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":12345," + + "\"PropertyCompAno\":{" + + "\"@type\":\"#olingo.odata.test1.CTBaseAno\"," + + "\"PropertyString\":\"Num111\"," + + "\"AdditionalPropString\":\"Test123\"" + + "}," + + "\"CollPropertyCompAno\":[" + + "{\"@type\":\"#olingo.odata.test1.CTBaseAno\"," + + "\"PropertyString\":\"TEST12345\"," + + "\"AdditionalPropString\":\"Additional12345\"}," + + "{\"PropertyString\":\"TESTabcd\"}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void deriveEntityESAllPrimDerivedOne() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrimDerived"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList( + ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETTwoPrimOne"))); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .expand(expand) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESAllPrimDerived/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32767,\"PropertyString\":\"First Resource - positive values\",\"PropertyBoolean\":true," + + "\"PropertyByte\":255,\"PropertySByte\":127,\"PropertyInt32\":2147483647," + + "\"PropertyInt64\":9223372036854775807," + + "\"PropertySingle\":1.79E20,\"PropertyDouble\":-1.79E19," + + "\"PropertyDecimal\":34,\"PropertyBinary\":\"ASNFZ4mrze8=\"," + + "\"PropertyDate\":\"2012-12-03\",\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\"," + + "\"PropertyDuration\":\"PT6S\"," + + "\"PropertyGuid\":\"01234567-89ab-cdef-0123-456789abcdef\",\"PropertyTimeOfDay\":\"03:26:05\"," + + "\"NavPropertyETTwoPrimOne\":{\"@type\":\"#olingo.odata.test1.ETBase\",\"PropertyInt16\":32766," + + "\"PropertyString\":\"Test String1\",\"AdditionalPropertyString_5\":\"Additional String1\"}}"; + Assert.assertEquals(expectedResult, resultString); + } + + + @Test + public void deriveEntityWithNull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrimDerived"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(1); + + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList( + ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETTwoPrimOne"))); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .expand(expand) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESAllPrimDerived/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\",\"PropertyInt16\":-32768," + + "\"PropertyString\":\"Second Resource - negative values\"," + + "\"PropertyBoolean\":false,\"PropertyByte\":0,\"PropertySByte\":-128," + + "\"PropertyInt32\":-2147483648,\"PropertyInt64\":-9223372036854775808," + + "\"PropertySingle\":-1.79E8,\"PropertyDouble\":-179000.0,\"PropertyDecimal\":-34," + + "\"PropertyBinary\":\"ASNFZ4mrze8=\",\"PropertyDate\":\"2015-11-05\"," + + "\"PropertyDateTimeOffset\":\"2005-12-03T07:17:08Z\",\"PropertyDuration\":\"PT9S\"," + + "\"PropertyGuid\":\"76543201-23ab-cdef-0123-456789dddfff\"," + + "\"PropertyTimeOfDay\":\"23:49:14\",\"NavPropertyETTwoPrimOne\":null}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void deriveEntityESAllPrimDerived() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrimDerived"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(2); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList( + ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETTwoPrimMany"))); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .expand(expand) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + + final String expectedResult = "{\"@context\":\"$metadata#ESAllPrimDerived/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\",\"PropertyInt16\":0,\"PropertyString\":\"\"," + + "\"PropertyBoolean\":false,\"PropertyByte\":0,\"PropertySByte\":0,\"PropertyInt32\":0,\"PropertyInt64\":0," + + "\"PropertySingle\":0.0,\"PropertyDouble\":0.0,\"PropertyDecimal\":0,\"PropertyBinary\":\"\"," + + "\"PropertyDate\":\"1970-01-01\"," + + "\"PropertyDateTimeOffset\":\"2005-12-03T00:00:00Z\",\"PropertyDuration\":\"PT0S\"," + + "\"PropertyGuid\":\"76543201-23ab-cdef-0123-456789cccddd\"," + + "\"PropertyTimeOfDay\":\"00:01:01\"," + + "\"NavPropertyETTwoPrimMany\":[" + + "{\"PropertyInt16\":-365,\"PropertyString\":\"Test String2\"}," + + "{\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"}," + + "{\"@type\":\"#olingo.odata.test1.ETBase\"," + + "\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"," + + "\"AdditionalPropertyString_5\":\"Additional String1\"}]}"; + Assert.assertEquals(expectedResult, resultString); + + } + + @Test + public void entityMixPrimCollCompAllNull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp"); + Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + entity.getProperties().retainAll(Collections.singletonList(entity.getProperties().get(0))); + final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent()); + final String expectedResult = "{\"@context\":\"$metadata#ESMixPrimCollComp/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32767," + + "\"CollPropertyString\":[],\"PropertyComp\":null,\"CollPropertyComp\":[]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void nullCollectionButInDataMap() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixEnumDefCollComp"); + Entity entity = new Entity(); + entity.setId(URI.create("id")); + entity.addProperty(new Property(null, "PropertyEnumString", ValueType.ENUM, 6)); + entity.addProperty(new Property(null, "CollPropertyEnumString", ValueType.COLLECTION_ENUM, null)); + entity.addProperty(new Property(null, "PropertyDefString", ValueType.PRIMITIVE, "Test")); + entity.addProperty(new Property(null, "CollPropertyDefString", ValueType.COLLECTION_PRIMITIVE, null)); + ComplexValue complexValue = new ComplexValue(); + complexValue.getValue().add(entity.getProperty("PropertyEnumString")); + complexValue.getValue().add(entity.getProperty("CollPropertyEnumString")); + complexValue.getValue().add(entity.getProperty("PropertyDefString")); + complexValue.getValue().add(entity.getProperty("CollPropertyDefString")); + entity.addProperty(new Property(null, "PropertyCompMixedEnumDef", ValueType.COMPLEX, complexValue)); + entity.addProperty(new Property(null, "CollPropertyCompMixedEnumDef", ValueType.COLLECTION_COMPLEX, null)); + final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent()); + Assert.assertEquals("{\"@context\":\"$metadata#ESMixEnumDefCollComp/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyEnumString\":\"String2,String3\"," + + "\"CollPropertyEnumString\":[]," + + "\"PropertyDefString\":\"Test\"," + + "\"CollPropertyDefString\":[]," + + "\"PropertyCompMixedEnumDef\":{\"PropertyEnumString\":\"String2,String3\"," + + "\"CollPropertyEnumString\":[]," + + "\"PropertyDefString\":\"Test\",\"CollPropertyDefString\":[]}," + + "\"CollPropertyCompMixedEnumDef\":[]}", + resultString); + } + + @Test + public void nullComplexValueButInDataMapAndNullCollectionsNotInDataMap() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixEnumDefCollComp"); + Entity entity = new Entity(); + entity.setId(URI.create("id")); + entity.addProperty(new Property(null, "PropertyEnumString", ValueType.ENUM, 6)); + entity.addProperty(new Property(null, "PropertyDefString", ValueType.PRIMITIVE, "Test")); + entity.addProperty(new Property(null, "PropertyCompMixedEnumDef", ValueType.COMPLEX, null)); + final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent()); + Assert.assertEquals("{\"@context\":\"$metadata#ESMixEnumDefCollComp/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyEnumString\":\"String2,String3\"," + + "\"CollPropertyEnumString\":[]," + + "\"PropertyDefString\":\"Test\"," + + "\"CollPropertyDefString\":[]," + + "\"PropertyCompMixedEnumDef\":null," + + "\"CollPropertyCompMixedEnumDef\":[]}", + resultString); + } + + @Test + public void enumAndTypeDefinition() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixEnumDefCollComp"); + Entity entity = new Entity(); + entity.setId(URI.create("id")); + entity.addProperty(new Property(null, "PropertyEnumString", ValueType.ENUM, 6)); + entity.addProperty(new Property(null, "CollPropertyEnumString", ValueType.COLLECTION_ENUM, + Arrays.asList(2, 4, 6))); + entity.addProperty(new Property(null, "PropertyDefString", ValueType.PRIMITIVE, "Test")); + entity.addProperty(new Property(null, "CollPropertyDefString", ValueType.COLLECTION_PRIMITIVE, + Arrays.asList("Test1", "Test2"))); + ComplexValue complexValue = new ComplexValue(); + complexValue.getValue().add(entity.getProperty("PropertyEnumString")); + complexValue.getValue().add(entity.getProperty("CollPropertyEnumString")); + complexValue.getValue().add(entity.getProperty("PropertyDefString")); + complexValue.getValue().add(entity.getProperty("CollPropertyDefString")); + entity.addProperty(new Property(null, "PropertyCompMixedEnumDef", ValueType.COMPLEX, complexValue)); + entity.addProperty(new Property(null, "CollPropertyCompMixedEnumDef", ValueType.COLLECTION_COMPLEX, + Collections.singletonList(complexValue))); + final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent()); + Assert.assertEquals("{\"@context\":\"$metadata#ESMixEnumDefCollComp/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyEnumString\":\"String2,String3\"," + + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"]," + + "\"PropertyDefString\":\"Test\"," + + "\"CollPropertyDefString\":[\"Test1\",\"Test2\"]," + + "\"PropertyCompMixedEnumDef\":{\"PropertyEnumString\":\"String2,String3\"," + + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"]," + + "\"PropertyDefString\":\"Test\",\"CollPropertyDefString\":[\"Test1\",\"Test2\"]}," + + "\"CollPropertyCompMixedEnumDef\":[{\"PropertyEnumString\":\"String2,String3\"," + + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"]," + + "\"PropertyDefString\":\"Test\",\"CollPropertyDefString\":[\"Test1\",\"Test2\"]}]}", + resultString); + } + + @Test + public void entityTwoPrimNoMetadata() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + final String resultString = IOUtils.toString(serializerNoMetadata + .entity(metadata, edmEntitySet.getEntityType(), entity, null).getContent()); + final String expectedResult = "{\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entityTwoPrimWithMetadataMinimal() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + InputStream result = serializer + .entity(metadata, edmEntitySet.getEntityType(), entity, EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESTwoPrim/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entitySetTwoPrimWithMetadataFull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + InputStream result = serializerFullMetadata + .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()) + .getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESTwoPrim\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{\"@type\":\"#olingo.odata.test1.ETTwoPrim\",\"@id\":\"ESTwoPrim(32766)\"," + + "\"PropertyInt16@type\":\"#Int16\",\"PropertyInt16\":32766," + + "\"PropertyString\":\"Test String1\"," + + "\"#olingo.odata.test1.BAETTwoPrimRTString\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTString\"," + + "\"target\":\"ESTwoPrim(32766)/olingo.odata.test1.BAETTwoPrimRTString\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollString\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollString\"," + + "\"target\":\"ESTwoPrim(32766)/olingo.odata.test1.BAETTwoPrimRTCollString\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCTAllPrim\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"," + + "\"target\":\"ESTwoPrim(32766)/olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"," + + "\"target\":\"ESTwoPrim(32766)/olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"}" + + "}," + + "{\"@type\":\"#olingo.odata.test1.ETTwoPrim\",\"@id\":\"ESTwoPrim(-365)\"," + + "\"PropertyInt16@type\":\"#Int16\",\"PropertyInt16\":-365," + + "\"PropertyString\":\"Test String2\"," + + "\"NavPropertyETAllPrimMany@navigationLink\":\"ESTwoPrim(-365)/NavPropertyETAllPrimMany\"," + + "\"#olingo.odata.test1.BAETTwoPrimRTString\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTString\"," + + "\"target\":\"ESTwoPrim(-365)/olingo.odata.test1.BAETTwoPrimRTString\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollString\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollString\"," + + "\"target\":\"ESTwoPrim(-365)/olingo.odata.test1.BAETTwoPrimRTCollString\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCTAllPrim\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"," + + "\"target\":\"ESTwoPrim(-365)/olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"," + + "\"target\":\"ESTwoPrim(-365)/olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"}}," + + "{\"@type\":\"#olingo.odata.test1.ETTwoPrim\",\"@id\":\"ESTwoPrim(-32766)\"," + + "\"PropertyInt16@type\":\"#Int16\",\"PropertyInt16\":-32766," + + "\"PropertyString\":null," + + "\"#olingo.odata.test1.BAETTwoPrimRTString\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTString\"," + + "\"target\":\"ESTwoPrim(-32766)/olingo.odata.test1.BAETTwoPrimRTString\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollString\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollString\"," + + "\"target\":\"ESTwoPrim(-32766)/olingo.odata.test1.BAETTwoPrimRTCollString\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCTAllPrim\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"," + + "\"target\":\"ESTwoPrim(-32766)/olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"," + + "\"target\":\"ESTwoPrim(-32766)/olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"}}," + + "{\"@type\":\"#olingo.odata.test1.ETTwoPrim\",\"@id\":\"ESTwoPrim(32767)\"," + + "\"PropertyInt16@type\":\"#Int16\",\"PropertyInt16\":32767," + + "\"PropertyString\":\"Test String4\"," + + "\"NavPropertyETAllPrimOne@navigationLink\":\"ESAllPrim(32767)\"," + + "\"#olingo.odata.test1.BAETTwoPrimRTString\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTString\"," + + "\"target\":\"ESTwoPrim(32767)/olingo.odata.test1.BAETTwoPrimRTString\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollString\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollString\"," + + "\"target\":\"ESTwoPrim(32767)/olingo.odata.test1.BAETTwoPrimRTCollString\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCTAllPrim\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"," + + "\"target\":\"ESTwoPrim(32767)/olingo.odata.test1.BAETTwoPrimRTCTAllPrim\"}," + + "\"#olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\":" + + "{\"title\":\"olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"," + + "\"target\":\"ESTwoPrim(32767)/olingo.odata.test1.BAETTwoPrimRTCollCTAllPrim\"}" + + "}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entityWithStreamMetadataMinimal() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream"); + final EntityCollection collection = data.readAll(edmEntitySet); + InputStream result = serializer.entityCollection(metadata, edmEntitySet.getEntityType(), collection, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESWithStream\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{\"PropertyInt16\":32767}," + + "{\"PropertyInt16\":7,\"PropertyStream@mediaEtag\":\"eTag\"," + + "\"PropertyStream@mediaContentType\":\"image/jpeg\"}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entityWithStreamMetadataNone() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream"); + final EntityCollection collection = data.readAll(edmEntitySet); + InputStream result = serializerNoMetadata.entityCollection(metadata, edmEntitySet.getEntityType(), collection, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{" + + "\"value\":[{\"PropertyInt16\":32767}," + + "{\"PropertyInt16\":7}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entityWithStreamMetadataFull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream"); + final EntityCollection collection = data.readAll(edmEntitySet); + InputStream result = serializerFullMetadata.entityCollection(metadata, edmEntitySet.getEntityType(), collection, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESWithStream\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{" + + "\"@type\":\"#olingo.odata.test1.ETWithStream\"," + + "\"@id\":\"ESWithStream(32767)\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":32767," + + "\"PropertyStream@type\":\"#Stream\"," + + "\"PropertyStream@mediaReadLink\":\"readLink\"}," + + "{" + + "\"@type\":\"#olingo.odata.test1.ETWithStream\"," + + "\"@id\":\"ESWithStream(7)\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":7," + + "\"PropertyStream@type\":\"#Stream\"," + + "\"PropertyStream@mediaEtag\":\"eTag\"," + + "\"PropertyStream@mediaContentType\":\"image/jpeg\"," + + "\"PropertyStream@mediaEditLink\":\"http://mediaserver:1234/editLink\"" + + "}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + + @Test + public void entityWithStreamExpand() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream"); + final EntityCollection collection = data.readAll(edmEntitySet); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList( + ExpandSelectMock.mockExpandItem(edmEntitySet, "PropertyStream"))); + InputStream result = serializer.entityCollection(metadata, edmEntitySet.getEntityType(), collection, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .expand(expand).build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESWithStream\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{\"PropertyInt16\":32767,\"PropertyStream\":\"�ioz�\\\"�\"}," + + "{\"PropertyInt16\":7,\"PropertyStream@mediaEtag\":\"eTag\"," + + "\"PropertyStream@mediaContentType\":\"image/jpeg\",\"PropertyStream\":\"�ioz�\\\"�\"}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entitySetTwoPrimNoMetadata() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + final String resultString = IOUtils.toString(serializerNoMetadata + .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()).getContent()); + final String expectedResult = "{\"value\":[" + + "{\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"}," + + "{\"PropertyInt16\":-365,\"PropertyString\":\"Test String2\"}," + + "{\"PropertyInt16\":-32766,\"PropertyString\":null}," + + "{\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entityMedia() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMedia"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), + entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .build()).getContent()); + final String expectedResult = "{\"@context\":\"$metadata#ESMedia/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@mediaEtag\":\"W/\\\"1\\\"\"," + + "\"@mediaContentType\":\"image/svg+xml\"," + + "\"@mediaEditLink\":\"ESMedia(1)/$value\"," + + "\"PropertyInt16\":1}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entitySetMedia() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMedia"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + final String resultString = IOUtils.toString(serializer.entityCollection(metadata, + edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()).build()).getContent()); + final String expectedResult = "{\"@context\":\"$metadata#ESMedia\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[" + + "{\"@mediaEtag\":\"W/\\\"1\\\"\",\"@mediaContentType\":\"image/svg+xml\"," + + "\"@mediaEditLink\":\"ESMedia(1)/$value\",\"PropertyInt16\":1}," + + "{\"@mediaEtag\":\"W/\\\"2\\\"\",\"@mediaContentType\":\"image/svg+xml\"," + + "\"@mediaEditLink\":\"ESMedia(2)/$value\",\"PropertyInt16\":2}," + + "{\"@mediaEtag\":\"W/\\\"3\\\"\",\"@mediaContentType\":\"image/svg+xml\"," + + "\"@mediaEditLink\":\"ESMedia(3)/$value\",\"PropertyInt16\":3}," + + "{\"@mediaEtag\":\"W/\\\"4\\\"\",\"@mediaContentType\":\"image/svg+xml\"," + + "\"@mediaEditLink\":\"ESMedia(4)/$value\",\"PropertyInt16\":4}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void entityMediaWithMetadataFull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMedia"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + entity.setMediaETag("W/\\\"08D25949E3BFB7AB\\\""); + InputStream result = serializerFullMetadata + .entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with().contextURL(ContextURL.with() + .entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()).build()) + .getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESMedia/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@mediaEtag\":\"W/\\\\\\\"08D25949E3BFB7AB\\\\\\\"\",\"@mediaContentType\":\"image/svg+xml\"," + + "\"@mediaEditLink\":\"ESMedia(1)/$value\"," + + "\"@type\":\"#olingo.odata.test1.ETMedia\",\"@id\":\"ESMedia(1)\"," + + "\"PropertyInt16@type\":\"#Int16\",\"PropertyInt16\":1}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void primitiveValuesAllNull() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllNullable"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + final String resultString = IOUtils.toString(serializer.entityCollection(metadata, + edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()).build()).getContent()); + + final String expected = "{\"@context\":\"$metadata#ESAllNullable\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{\"PropertyKey\":1," + + "\"PropertyInt16\":null,\"PropertyString\":null,\"PropertyBoolean\":null,\"PropertyByte\":null," + + "\"PropertySByte\":null,\"PropertyInt32\":null,\"PropertyInt64\":null,\"PropertySingle\":null," + + "\"PropertyDouble\":null,\"PropertyDecimal\":null,\"PropertyBinary\":null,\"PropertyDate\":null," + + "\"PropertyDateTimeOffset\":null,\"PropertyDuration\":null,\"PropertyGuid\":null,\"PropertyTimeOfDay\":null," + + "\"CollPropertyString\":[\"[email protected]\",null,\"[email protected]\"]," + + "\"CollPropertyBoolean\":[true,null,false],\"CollPropertyByte\":[50,null,249]," + + "\"CollPropertySByte\":[-120,null,126],\"CollPropertyInt16\":[1000,null,30112]," + + "\"CollPropertyInt32\":[23232323,null,10000001],\"CollPropertyInt64\":[929292929292,null,444444444444]," + + "\"CollPropertySingle\":[1790.0,null,3210.0],\"CollPropertyDouble\":[-17900.0,null,3210.0]," + + "\"CollPropertyDecimal\":" + + "[12,null,1234],\"CollPropertyBinary\":[\"q83v\",null,\"VGeJ\"],\"CollPropertyDate\":" + + "[\"1958-12-03\",null,\"2013-06-25\"],\"CollPropertyDateTimeOffset\":[\"2015-08-12T03:08:34Z\",null," + + "\"1948-02-17T09:09:09Z\"],\"CollPropertyDuration\":[\"PT13S\",null,\"PT1H0S\"],\"CollPropertyGuid\":" + + "[\"ffffff67-89ab-cdef-0123-456789aaaaaa\",null,\"cccccc67-89ab-cdef-0123-456789cccccc\"]," + + "\"CollPropertyTimeOfDay\":[\"04:14:13\",null,\"00:37:13\"]}]}"; + + Assert.assertEquals(expected, resultString); + } + + @Test + public void select() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + final SelectItem selectItem1 = ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyDate"); + final SelectItem selectItem2 = ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyBoolean"); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList( + selectItem1, selectItem2, selectItem2)); + InputStream result = serializer + .entity(metadata, entityType, entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet) + .selectList(helper.buildContextURLSelectList(entityType, null, select)) + .suffix(Suffix.ENTITY).build()) + .select(select) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{" + + "\"@context\":\"$metadata#ESAllPrim(PropertyBoolean,PropertyDate)/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@id\":\"ESAllPrim(32767)\"," + + "\"PropertyBoolean\":true,\"PropertyDate\":\"2012-12-03\"}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void selectAll() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + final SelectItem selectItem1 = ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyString"); + SelectItem selectItem2 = Mockito.mock(SelectItem.class); + Mockito.when(selectItem2.isStar()).thenReturn(true); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(selectItem1, selectItem2)); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .select(select) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESTwoPrim/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void selectComplex() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESFourKeyAlias"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final EntityCollection entitySet = data.readAll(edmEntitySet); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList( + ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyInt16"), + ExpandSelectMock.mockSelectItem(edmEntitySet,"PropertyCompComp", "PropertyComp", "PropertyString"))); + InputStream result = serializer + .entityCollection(metadata, entityType, entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet) + .selectList(helper.buildContextURLSelectList(entityType, null, select)) + .build()) + .select(select) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expected = "{" + + "\"@context\":\"$metadata#ESFourKeyAlias" + + "(PropertyInt16,PropertyCompComp/PropertyComp/PropertyString)\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[" + + "{" + + "\"@id\":\"" + + "ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\"," + + "\"PropertyInt16\":1," + + "\"PropertyCompComp\":{" + + "\"PropertyComp\":{" + + "\"@type\":\"#olingo.odata.test1.CTBase\"," + + "\"PropertyString\":\"Num111\"" + + "}}}]}"; + + Assert.assertEquals(expected, resultString); + } + + @Test + public void selectExtendedComplexType() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESFourKeyAlias"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final EntityCollection entitySet = data.readAll(edmEntitySet); + InputStream result = serializer + .entityCollection(metadata, entityType, entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + + final String expected = "{" + + "\"@context\":\"$metadata#ESFourKeyAlias\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{" + + "\"PropertyInt16\":1," + + "\"PropertyComp\":{" + + "\"PropertyInt16\":11," + + "\"PropertyString\":\"Num11\"" + + "}," + + "\"PropertyCompComp\":{" + + "\"PropertyComp\":{" + + "\"@type\":\"#olingo.odata.test1.CTBase\"," + + "\"PropertyInt16\":111," + + "\"PropertyString\":\"Num111\"," + + "\"AdditionalPropString\":\"Test123\"" + + "}}}]}"; + + Assert.assertEquals(expected, resultString); + } + + @Test + public void selectComplexTwice() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESFourKeyAlias"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final EntityCollection entitySet = data.readAll(edmEntitySet); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList( + ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyComp", "PropertyString"), + ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyCompComp", "PropertyComp"))); + final String resultString = IOUtils.toString(serializer + .entityCollection(metadata, entityType, entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet) + .selectList(helper.buildContextURLSelectList(entityType, null, select)) + .build()) + .select(select) + .build()).getContent()); + + String expected = "{" + + "\"@context\":\"$metadata#ESFourKeyAlias" + + "(PropertyComp/PropertyString,PropertyCompComp/PropertyComp)\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{" + + "\"@id\":\"ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\"," + + "\"PropertyComp\":{" + + "\"PropertyString\":\"Num11\"" + + "}," + + "\"PropertyCompComp\":{" + + "\"PropertyComp\":{" + + "\"@type\":\"#olingo.odata.test1.CTBase\"," + + "\"PropertyInt16\":111," + + "\"PropertyString\":\"Num111\"," + + "\"AdditionalPropString\":\"Test123\"" + + "}}}]}"; + + Assert.assertEquals(expected, resultString); + } + + @Test + public void selectComplexNestedCollectionOfComplexWithMetadataFull() throws Exception{ + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + InputStream result = serializerFullMetadata + .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()) + .getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESCompCollComp\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{\"@type\":\"#olingo.odata.test1.ETCompCollComp\"," + + "\"@id\":\"ESCompCollComp(32767)\"," + + "\"PropertyInt16@type\":\"#Int16\",\"PropertyInt16\":32767," + + "\"PropertyComp\":{" + + "\"@type\":\"#olingo.odata.test1.CTCompCollComp\"," + + "\"CollPropertyComp@type\":\"#Collection(olingo.odata.test1.CTTwoPrim)\"," + + "\"CollPropertyComp\":[" + + "{" + + "\"@type\":\"#olingo.odata.test1.CTTwoPrim\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":555," + + "\"PropertyString\":\"1 Test Complex in Complex Property\"" + + "},{" + + "\"@type\":\"#olingo.odata.test1.CTTwoPrim\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":666," + + "\"PropertyString\":\"2 Test Complex in Complex Property\"" + + "},{" + + "\"@type\":\"#olingo.odata.test1.CTTwoPrim\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":777," + + "\"PropertyString\":\"3 Test Complex in Complex Property\"" + + "}]}},{" + + "\"@type\":\"#olingo.odata.test1.ETCompCollComp\"," + + "\"@id\":\"ESCompCollComp(12345)\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":12345," + + "\"PropertyComp\":{" + + "\"@type\":\"#olingo.odata.test1.CTCompCollComp\"," + + "\"CollPropertyComp@type\":\"#Collection(olingo.odata.test1.CTTwoPrim)\"," + + "\"CollPropertyComp\":[" + + "{" + + "\"@type\":\"#olingo.odata.test1.CTTwoPrim\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":888," + + "\"PropertyString\":\"11 Test Complex in Complex Property\"" + + "},{" + + "\"@type\":\"#olingo.odata.test1.CTTwoPrim\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":999," + + "\"PropertyString\":\"12 Test Complex in Complex Property\"" + + "},{" + + "\"@type\":\"#olingo.odata.test1.CTTwoPrim\"," + + "\"PropertyInt16@type\":\"#Int16\"," + + "\"PropertyInt16\":0," + + "\"PropertyString\":\"13 Test Complex in Complex Property\"" + + "}]}}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void selectComplexNestedCollectionOfComplexWithMetadataMinimal() throws Exception{ + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + InputStream result = serializer + .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()) + .getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@context\":\"$metadata#ESCompCollComp\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{" + + "\"PropertyInt16\":32767," + + "\"PropertyComp\":{" + + "\"CollPropertyComp\":[" + + "{" + + "\"PropertyInt16\":555," + + "\"PropertyString\":\"1 Test Complex in Complex Property\"" + + "},{" + + "\"PropertyInt16\":666," + + "\"PropertyString\":\"2 Test Complex in Complex Property\"" + + "},{" + + "\"PropertyInt16\":777," + + "\"PropertyString\":\"3 Test Complex in Complex Property\"" + + "}]}},{" + + "\"PropertyInt16\":12345," + + "\"PropertyComp\":{" + + "\"CollPropertyComp\":[" + + "{" + + "\"PropertyInt16\":888," + + "\"PropertyString\":\"11 Test Complex in Complex Property\"" + + "},{" + + "\"PropertyInt16\":999," + + "\"PropertyString\":\"12 Test Complex in Complex Property\"" + + "},{" + + "\"PropertyInt16\":0," + + "\"PropertyString\":\"13 Test Complex in Complex Property\"" + + "}]}}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void selectComplexNestedCollectionOfComplexWithMetadataNone() throws Exception{ + final String METADATA_TEXT = "@"; + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + InputStream result = serializerNoMetadata + .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()) + .getContent(); + final String resultString = IOUtils.toString(result); + Assert.assertEquals(false, resultString.contains(METADATA_TEXT)); + } + + @Test + public void selectMissingId() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + entity.setId(null); + final SelectItem selectItem1 = ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyDate"); + final SelectItem selectItem2 = ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyBoolean"); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList( + selectItem1, selectItem2, selectItem2)); + InputStream result = serializer.entity(metadata, entityType, entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet) + .selectList(helper.buildContextURLSelectList(entityType, null, select)) + .suffix(Suffix.ENTITY).build()) + .select(select) + .build()).getContent(); + Assert.assertNotNull(result); + final String resultString = IOUtils.toString(result); + Assert.assertEquals( "{\"@context\":\"$metadata#ESAllPrim(PropertyBoolean,PropertyDate)/$entity\","+ + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\",\"@id\":\"ESAllPrim(32767)\","+ + "\"PropertyBoolean\":true,\"PropertyDate\":\"2012-12-03\"}", + resultString); + } + + @Test + public void expand() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(3); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList( + ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimOne"))); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .expand(expand) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + Assert.assertEquals("{\"@context\":\"$metadata#ESTwoPrim/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"," + + "\"NavPropertyETAllPrimOne\":{" + + "\"PropertyInt16\":32767," + + "\"PropertyString\":\"First Resource - positive values\"," + + "\"PropertyBoolean\":true," + + "\"PropertyByte\":255," + + "\"PropertySByte\":127," + + "\"PropertyInt32\":2147483647," + + "\"PropertyInt64\":9223372036854775807," + + "\"PropertySingle\":1.79E20," + + "\"PropertyDouble\":-1.79E19," + + "\"PropertyDecimal\":34," + + "\"PropertyBinary\":\"ASNFZ4mrze8=\"," + + "\"PropertyDate\":\"2012-12-03\"," + + "\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\"," + + "\"PropertyDuration\":\"PT6S\"," + + "\"PropertyGuid\":\"01234567-89ab-cdef-0123-456789abcdef\"," + + "\"PropertyTimeOfDay\":\"03:26:05\"}}", + resultString); + } + + @Test + public void expandSelect() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(3); + final SelectOption select = ExpandSelectMock.mockSelectOption(Collections.singletonList( + ExpandSelectMock.mockSelectItem(entityContainer.getEntitySet("ESAllPrim"), "PropertyDate"))); + ExpandItem expandItem = ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimOne"); + Mockito.when(expandItem.getSelectOption()).thenReturn(select); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(expandItem)); + final String resultString = IOUtils.toString(serializer + .entity(metadata, entityType, entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet) + .selectList(helper.buildContextURLSelectList(entityType, expand, select)) + .suffix(Suffix.ENTITY).build()) + .expand(expand) + .build()).getContent()); + Assert.assertEquals("{" + + "\"@context\":\"$metadata#ESTwoPrim(NavPropertyETAllPrimOne(PropertyDate))/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"," + + "\"NavPropertyETAllPrimOne\":{\"@id\":\"ESAllPrim(32767)\",\"PropertyDate\":\"2012-12-03\"}}", + resultString); + } + + @Test + public void expandAll() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + final ExpandItem expandItem = ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETTwoPrimOne"); + ExpandItem expandItemAll = Mockito.mock(ExpandItem.class); + Mockito.when(expandItemAll.isStar()).thenReturn(true); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Arrays.asList( + expandItem, expandItem, expandItemAll)); + final SelectOption select = ExpandSelectMock.mockSelectOption(Collections.singletonList( + ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertySByte"))); + final String resultString = IOUtils.toString(serializer + .entity(metadata, entityType, entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet) + .selectList(helper.buildContextURLSelectList(entityType, expand, select)) + .suffix(Suffix.ENTITY).build()) + .expand(expand) + .select(select) + .build()).getContent()); + Assert.assertEquals("{" + + "\"@context\":\"$metadata#ESAllPrim(PropertySByte)/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@id\":\"ESAllPrim(32767)\"," + + "\"PropertySByte\":127," + + "\"NavPropertyETTwoPrimOne\":{\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"}," + + "\"NavPropertyETTwoPrimMany\":[{\"PropertyInt16\":-365,\"PropertyString\":\"Test String2\"}]}", + resultString); + } + + @Test + public void expandNoData() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(1); + ExpandItem expandItemAll = Mockito.mock(ExpandItem.class); + Mockito.when(expandItemAll.isStar()).thenReturn(true); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(expandItemAll)); + final SelectOption select = ExpandSelectMock.mockSelectOption(Collections.singletonList( + ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyTimeOfDay"))); + final String resultString = IOUtils.toString(serializer + .entity(metadata, entityType, entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet) + .selectList(helper.buildContextURLSelectList(entityType, expand, select)) + .suffix(Suffix.ENTITY).build()) + .expand(expand) + .select(select) + .build()).getContent()); + Assert.assertEquals("{" + + "\"@context\":\"$metadata#ESAllPrim(PropertyTimeOfDay)/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@id\":\"ESAllPrim(-32768)\"," + + "\"PropertyTimeOfDay\":\"23:49:14\"," + + "\"NavPropertyETTwoPrimOne\":null,\"NavPropertyETTwoPrimMany\":[]}", + resultString); + } + + @Test + public void expandTwoLevels() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final EdmEntitySet innerEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(1); + ExpandItem expandItemSecond = Mockito.mock(ExpandItem.class); + Mockito.when(expandItemSecond.isStar()).thenReturn(true); + final ExpandOption expandInner = ExpandSelectMock.mockExpandOption(Collections.singletonList(expandItemSecond)); + ExpandItem expandItemFirst = ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimMany"); + Mockito.when(expandItemFirst.getExpandOption()).thenReturn(expandInner); + final SelectOption select = ExpandSelectMock.mockSelectOption(Collections.singletonList( + ExpandSelectMock.mockSelectItem(innerEntitySet, "PropertyInt32"))); + Mockito.when(expandItemFirst.getSelectOption()).thenReturn(select); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(expandItemFirst)); + final String resultString = IOUtils.toString(serializer + .entity(metadata, entityType, entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet) + .selectList(helper.buildContextURLSelectList(entityType, expand, select)) + .suffix(Suffix.ENTITY).build()) + .expand(expand) + .build()).getContent()); + Assert.assertEquals("{" + + "\"@context\":\"$metadata#ESTwoPrim(NavPropertyETAllPrimMany(PropertyInt32))/$entity\"," + + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"PropertyInt16\":-365,\"PropertyString\":\"Test String2\"," + + "\"NavPropertyETAllPrimMany\":[" + + "{\"@id\":\"ESAllPrim(-32768)\",\"PropertyInt32\":-2147483648," + + "\"NavPropertyETTwoPrimOne\":null,\"NavPropertyETTwoPrimMany\":[]}," + + "{\"@id\":\"ESAllPrim(0)\",\"PropertyInt32\":0,\"NavPropertyETTwoPrimOne\":{" + + "\"@type\":\"#olingo.odata.test1.ETBase\",\"PropertyInt16\":111," + + "\"PropertyString\":\"TEST A\",\"AdditionalPropertyString_5\":\"TEST A 0815\"}," + + "\"NavPropertyETTwoPrimMany\":[" + + "{\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"}," + + "{\"PropertyInt16\":-32766,\"PropertyString\":null}," + + "{\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"}]}]}", + resultString); + } + + @Test + public void expandStarTwoLevels() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim"); + final EdmEntityType entityType = edmEntitySet.getEntityType(); + final EdmEntitySet innerEntitySet = entityContainer.getEntitySet("ESAllPrim"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(1); + ExpandItem expandItem = Mockito.mock(ExpandItem.class); + Mockito.when(expandItem.isStar()).thenReturn(true); + LevelsExpandOption levels = Mockito.mock(LevelsExpandOption.class); + Mockito.when(levels.getVal <TRUNCATED>
