diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataMetadataValidationImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataMetadataValidationImpl.java index 97e2d6d46..325cf1f27 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataMetadataValidationImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataMetadataValidationImpl.java @@ -27,7 +27,6 @@ import org.apache.olingo.client.api.serialization.ODataMetadataValidation; import org.apache.olingo.client.core.metadatavalidator.CsdlTypeValidator; import org.apache.olingo.client.core.metadatavalidator.EdmTypeValidator; import org.apache.olingo.commons.api.edm.Edm; -import org.apache.olingo.commons.api.edm.EdmAction; import org.apache.olingo.commons.api.edm.EdmComplexType; import org.apache.olingo.commons.api.edm.EdmEntityContainer; import org.apache.olingo.commons.api.edm.EdmEntityType; diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/AtomTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/AtomTest.java index 8f70768b8..85607e72f 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/AtomTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/AtomTest.java @@ -35,13 +35,27 @@ import javax.xml.transform.stream.StreamSource; import org.apache.commons.io.IOUtils; import org.apache.olingo.client.api.data.ResWrap; +import org.apache.olingo.client.api.domain.ClientAnnotation; import org.apache.olingo.client.api.domain.ClientCollectionValue; import org.apache.olingo.client.api.domain.ClientComplexValue; import org.apache.olingo.client.api.domain.ClientEntity; +import org.apache.olingo.client.api.domain.ClientEnumValue; +import org.apache.olingo.client.api.domain.ClientLink; +import org.apache.olingo.client.api.domain.ClientOperation; +import org.apache.olingo.client.api.domain.ClientPrimitiveValue; import org.apache.olingo.client.api.domain.ClientValue; +import org.apache.olingo.client.core.domain.ClientAnnotationImpl; import org.apache.olingo.client.core.serialization.AtomDeserializer; import org.apache.olingo.commons.api.data.ComplexValue; +import org.apache.olingo.commons.api.data.Delta; import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntityCollection; +import org.apache.olingo.commons.api.data.Link; +import org.apache.olingo.commons.api.data.Property; +import org.apache.olingo.commons.api.data.ValueType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.format.ContentType; import org.custommonkey.xmlunit.Diff; @@ -281,4 +295,354 @@ public class AtomTest extends JSONTest { i++; } } + + @Test + public void issue2OLINGO1073_WithEntitySet() throws Exception { + final ClientEntity message = createClientEntity(); + + InputStream inputStream = client.getWriter().writeEntity(message, ContentType.APPLICATION_ATOM_XML); + ResWrap entity = new AtomDeserializer().toEntity(inputStream); + assertNotNull(entity); + assertEquals(7, entity.getPayload().getProperties().size()); + assertEquals(3, entity.getPayload().getProperty("AddressInfo").asCollection().size()); + assertEquals("#Microsoft.OData.SampleService.Models.TripPin.Location", + ((ComplexValue)entity.getPayload().getProperty("AddressInfo").asCollection().get(0)).getTypeName()); + assertEquals("#Microsoft.OData.SampleService.Models.TripPin.EventLocation", + ((ComplexValue)entity.getPayload().getProperty("AddressInfo").asCollection().get(1)).getTypeName()); + assertEquals("#Microsoft.OData.SampleService.Models.TripPin.AirportLocation", + ((ComplexValue)entity.getPayload().getProperty("AddressInfo").asCollection().get(2)).getTypeName()); + assertEquals("Collection(Microsoft.OData.SampleService.Models.TripPin.Location)", + entity.getPayload().getProperty("AddressInfo").getType()); + + StringWriter writer = new StringWriter(); + setNavigationBindingLinkOnEntity(entity); + + client.getSerializer(ContentType.APPLICATION_ATOM_XML).write(writer, entity); + assertNotNull(writer.toString()); + writer = new StringWriter(); + client.getSerializer(ContentType.APPLICATION_ATOM_XML).write(writer, + new ResWrap(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/"), null, + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')"))); + assertNotNull(writer.toString()); + assertEquals("" + + "", writer.toString()); + + writer = new StringWriter(); + Link linkPayload = new Link(); + linkPayload.setBindingLink("Photos"); + linkPayload.setMediaETag("xyz"); + linkPayload.setInlineEntity(createEntity()); + linkPayload.setTitle("Photos"); + linkPayload.setHref("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/Photos"); + client.getSerializer(ContentType.APPLICATION_ATOM_XML).write(writer, + new ResWrap(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/"), linkPayload.getMediaETag(), linkPayload)); + assertNotNull(writer.toString()); + assertEquals("" + + "" + + "http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/Photos", writer.toString()); + } + + /** + * @return + */ + private ClientEntity createClientEntity() { + final ClientEntity message = client.getObjectFactory(). + newEntity(new FullQualifiedName("Microsoft.OData.SampleService.Models.TripPin.Person")); + + final ClientComplexValue cityComplexType = getCityComplexType(); + + final ClientComplexValue locationComplexType = client.getObjectFactory(). + newComplexValue("Microsoft.OData.SampleService.Models.TripPin.Location"); + locationComplexType.add(client.getObjectFactory().newPrimitiveProperty("Address", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("187 Suffolk Ln."))); + locationComplexType.add(client.getObjectFactory().newComplexProperty("City",cityComplexType)); + + final ClientComplexValue eventLocationComplexType = client.getObjectFactory(). + newComplexValue("Microsoft.OData.SampleService.Models.TripPin.EventLocation"); + eventLocationComplexType.add(client.getObjectFactory().newPrimitiveProperty("BuildingInfo", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("187 Suffolk Ln12."))); + eventLocationComplexType.add(client.getObjectFactory().newPrimitiveProperty("Address", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("187 Suffolk Ln12."))); + eventLocationComplexType.add(client.getObjectFactory().newComplexProperty("City",cityComplexType)); + + final ClientComplexValue airportLocationComplexType = client.getObjectFactory(). + newComplexValue("Microsoft.OData.SampleService.Models.TripPin.AirportLocation"); + airportLocationComplexType.add(client.getObjectFactory().newPrimitiveProperty("Address", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("187 Suffolk Ln123."))); + airportLocationComplexType.add(client.getObjectFactory().newComplexProperty("City",cityComplexType)); + + final ClientCollectionValue collectionAddressInfo = client.getObjectFactory(). + newCollectionValue("Microsoft.OData.SampleService.Models.TripPin.Location"); + collectionAddressInfo.add(locationComplexType); + collectionAddressInfo.add(eventLocationComplexType); + collectionAddressInfo.add(airportLocationComplexType); + + message.getProperties().add(client.getObjectFactory().newPrimitiveProperty("UserName", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("russellwhyte"))); + message.getProperties().add(client.getObjectFactory().newPrimitiveProperty("FirstName", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("Russell"))); + message.getProperties().add(client.getObjectFactory().newPrimitiveProperty("LastName", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("Whyte"))); + final ClientLink messageLink1 = client.getObjectFactory().newEntityNavigationLink("Photo", + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')/Photo")); + final ClientAnnotation messageLink1Annotation = createAnnotation(); + messageLink1.getAnnotations().add(messageLink1Annotation); + + final ClientLink messageLink2 = client.getObjectFactory().newEntitySetNavigationLink("Friends", + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')/Friends")); + final ClientAnnotation messageLink2Annotation = createAnnotation(); + messageLink2.getAnnotations().add(messageLink2Annotation); + + final ClientLink messageLink3 = client.getObjectFactory().newEntitySetNavigationLink("Trips", + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')/Trips")); + final ClientAnnotation messageLink3Annotation = createAnnotation(); + messageLink3.getAnnotations().add(messageLink3Annotation); + + message.getNavigationLinks().add(messageLink1); + message.getNavigationLinks().add(messageLink2); + message.getNavigationLinks().add(messageLink3); + + final ClientAnnotation messageAnnotation = createAnnotation(); + message.getAnnotations().add(messageAnnotation); + + final ClientCollectionValue emailCollectionValue = client.getObjectFactory(). + newCollectionValue("String"); + emailCollectionValue.add(client.getObjectFactory().newPrimitiveValueBuilder().buildString("Russell@example.com")); + emailCollectionValue.add(client.getObjectFactory().newPrimitiveValueBuilder().buildString("Russell@contoso.com")); + message.getProperties().add(client.getObjectFactory().newCollectionProperty("Emails", emailCollectionValue)); + + message.getProperties().add(client.getObjectFactory().newCollectionProperty("AddressInfo", collectionAddressInfo)); + message.getProperties().add(client.getObjectFactory().newEnumProperty("Gender", + client.getObjectFactory().newEnumValue( + "Microsoft.OData.SampleService.Models.TripPin.PersonGender", "Male"))); + message.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Concurrency", + client.getObjectFactory().newPrimitiveValueBuilder().buildInt64(Long.valueOf("636293755917400747")))); + message.setId(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')")); + message.setETag("W/\"08D491CCBE417AAB\""); + message.setEditLink(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')")); + + final ClientEntity innerEntity = client.getObjectFactory(). + newEntity(new FullQualifiedName("Microsoft.OData.SampleService.Models.TripPin.Photo")); + innerEntity.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Id", + client.getObjectFactory().newPrimitiveValueBuilder().buildInt64(Long.valueOf(123)))); + innerEntity.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Name", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("ABC"))); + innerEntity.getAnnotations().add(createAnnotation()); + final ClientLink link = client.getObjectFactory().newDeepInsertEntity("Photos", innerEntity); + final ClientAnnotation linkAnnotation = createAnnotation(); + link.getAnnotations().add(linkAnnotation); + message.getNavigationLinks().add(link); + + final ClientLink assoLink = client.getObjectFactory().newAssociationLink("Photos", + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')/Photo")); + final ClientAnnotation assoLinkAnnotation = createAnnotation(); + assoLink.getAnnotations().add(assoLinkAnnotation); + + message.getAssociationLinks().add(assoLink); + final ClientOperation operation = new ClientOperation(); + operation.setTarget(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/Photos")); + operation.setTitle("Photos"); + message.getOperations().add(operation); + return message; + } + + /** + * @param entity + */ + private void setNavigationBindingLinkOnEntity(ResWrap entity) { + Link entityLink = new Link(); + Entity en = createEntity(); + + entityLink.setBindingLink("Photos"); + entityLink.setInlineEntity(en); + entityLink.setType("Microsoft.OData.SampleService.Models.TripPin.Photos"); + + Link entityColLink = new Link(); + EntityCollection enCol = new EntityCollection(); + enCol.getEntities().add(en); + + entityColLink.setBindingLink("Friends"); + entityColLink.setInlineEntitySet(enCol); + entityColLink.setType("Microsoft.OData.SampleService.Models.TripPin.Friends"); + + Link link = new Link(); + link.setBindingLink("Trips"); + link.setType("Microsoft.OData.SampleService.Models.TripPin.Trips"); + + entity.getPayload().getNavigationBindings().add(entityLink); + entity.getPayload().getNavigationBindings().add(entityColLink); + entity.getPayload().getNavigationBindings().add(link); + } + + /** + * @return + */ + private Entity createEntity() { + Entity en = new Entity(); + Property p1 = new Property(); + p1.setName("Id"); + p1.setType("Int64"); + p1.setValue(ValueType.PRIMITIVE, Long.valueOf(123)); + en.addProperty(p1); + + Property p2 = new Property(); + p2.setName("Name"); + p2.setType("String"); + p2.setValue(ValueType.PRIMITIVE, "ABC"); + en.addProperty(p2); + return en; + } + + /** + * @return + */ + private ClientAnnotation createAnnotation() { + final ClientAnnotation messageAnnotation = + new ClientAnnotationImpl("Org.OData.Core.V1.Permissions", new ClientPrimitiveValue() { + + @Override + public boolean isPrimitive() { + return false; + } + + @Override + public boolean isEnum() { + return true; + } + + @Override + public boolean isComplex() { + return false; + } + + @Override + public boolean isCollection() { + return false; + } + + @Override + public String getTypeName() { + return "String"; + } + + @Override + public ClientPrimitiveValue asPrimitive() { + return null; + } + + @Override + public ClientEnumValue asEnum() { + return client.getObjectFactory().newEnumValue("Org.OData.Core.V1.Permissions", "Read"); + } + + @Override + public ClientComplexValue asComplex() { + return null; + } + + @Override + public ClientCollectionValue asCollection() { + return null; + } + + @Override + public Object toValue() { + return client.getObjectFactory().newEnumValue("Org.OData.Core.V1.Permissions", "Read"); + } + + @Override + public T toCastValue(Class reference) throws EdmPrimitiveTypeException { + return null; + } + + @Override + public EdmPrimitiveTypeKind getTypeKind() { + return null; + } + + @Override + public EdmPrimitiveType getType() { + return null; + } + }); + return messageAnnotation; + } + + @Test + public void testEntitySet() throws Exception { + final StringWriter writer = new StringWriter(); + client.getSerializer(ContentType.APPLICATION_ATOM_XML).write(writer, + client.getDeserializer(ContentType.APPLICATION_ATOM_XML).toEntitySet( + getClass().getResourceAsStream("Customers.xml"))); + + assertNotNull(writer.toString()); + } + + private void property(String fileName) throws Exception { + final StringWriter writer = new StringWriter(); + client.getSerializer(ContentType.APPLICATION_ATOM_XML).write(writer, + client.getDeserializer(ContentType.APPLICATION_ATOM_XML). + toProperty(getClass().getResourceAsStream(fileName))); + + assertNotNull(writer.toString()); + } + + @Test + public void testProperties() throws Exception { + property("Products_5_SkinColor.xml"); + property("Products_5_CoverColors.xml"); + property("Employees_3_HomeAddress.xml"); + property("Employees_3_HomeAddress.xml"); + } + + protected void delta(final String filename) throws Exception { + ResWrap resDelta = client.getDeserializer(ContentType.APPLICATION_ATOM_XML).toDelta( + getClass().getResourceAsStream(filename)); + final Delta delta = resDelta.getPayload(); + + assertNotNull(delta); + assertNotNull(delta.getDeltaLink()); + assertEquals(5, delta.getCount(), 0); + + assertEquals(1, delta.getDeletedEntities().size()); + assertTrue(delta.getDeletedEntities().get(0).getId().toASCIIString().endsWith("Customers('ANTON')")); + + assertEquals(1, delta.getAddedLinks().size()); + assertTrue(delta.getAddedLinks().get(0).getSource().toASCIIString().endsWith("Customers('BOTTM')")); + assertEquals("Orders", delta.getAddedLinks().get(0).getRelationship()); + + assertEquals(1, delta.getDeletedLinks().size()); + assertTrue(delta.getDeletedLinks().get(0).getSource().toASCIIString().endsWith("Customers('ALFKI')")); + assertEquals("Orders", delta.getDeletedLinks().get(0).getRelationship()); + + assertEquals(2, delta.getEntities().size()); + Property property = delta.getEntities().get(0).getProperty("ContactName"); + assertNotNull(property); + assertTrue(property.isPrimitive()); + property = delta.getEntities().get(1).getProperty("ShippingAddress"); + assertNotNull(property); + assertTrue(property.isComplex()); + } + + @Test + public void deltas() throws Exception { + delta("delta.xml"); + } } diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/EntitySetTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/EntitySetTest.java index 254e83f87..b968c5f9d 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/EntitySetTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/EntitySetTest.java @@ -51,6 +51,21 @@ public class EntitySetTest extends AbstractTest { assertEquals(entitySet, written); } + @Test + public void testOperations() throws Exception { + final InputStream input = getClass(). + getResourceAsStream("CustomersWithOperations." + getSuffix(ContentType.APPLICATION_JSON)); + final ClientEntitySet entitySet = client.getBinder().getODataEntitySet( + client.getDeserializer(ContentType.APPLICATION_JSON).toEntitySet(input)); + assertNotNull(entitySet); + + assertEquals(2, entitySet.getEntities().size()); + assertNull(entitySet.getNext()); + assertEquals(1, entitySet.getOperations().size()); + assertEquals("Microsoft.Test.OData.Services.ODataWCFService.BAESAllPrimRTETAllPrim", + entitySet.getOperations().get(0).getTitle()); + } + @Test public void fromAtom() throws Exception { read(ContentType.APPLICATION_ATOM_XML); diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/EntityTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/EntityTest.java index b3f1b5e20..52e8e706e 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/EntityTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/EntityTest.java @@ -52,6 +52,9 @@ import org.junit.Ignore; import org.junit.Test; public class EntityTest extends AbstractTest { + + private static final String SERVICE_URI = "http://services.odata.org/V4/" + + "(S(jf0ernw5hgyg1ekhqmzrdsch))/TripPinServiceRW/"; private EdmEnabledODataClient getEdmEnabledClient() { return new EdmEnabledODataClientImpl(null, null, null) { @@ -401,4 +404,17 @@ public class EntityTest extends AbstractTest { assertTrue(property.isPrimitive()); assertEquals(property.getValueType(), ValueType.PRIMITIVE); } + + @Test + public void testFindTypeInEdmEnabledClient() throws Exception { + final Edm edm = client.getReader().readMetadata(getClass().getResourceAsStream("metadata_TripInService.xml")); + assertNotNull(edm); + final InputStream input = getClass().getResourceAsStream("tripinServEntity.json"); + EdmEnabledODataClient edmClient = ODataClientFactory.getEdmEnabledClient(SERVICE_URI, ContentType.APPLICATION_JSON); + ClientEntity entity = edmClient.getBinder().getODataEntity( + edmClient.getDeserializer(ContentType.APPLICATION_JSON).toEntity(input)); + assertNotNull(entity); + assertEquals(entity.getTypeName().getFullQualifiedNameAsString(), + "Microsoft.OData.SampleService.Models.TripPin.Person"); + } } diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/JSONTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/JSONTest.java index 646c562ed..e645f1657 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/JSONTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/JSONTest.java @@ -33,17 +33,29 @@ import java.util.Map; import org.apache.commons.io.IOUtils; import org.apache.olingo.client.api.data.ResWrap; +import org.apache.olingo.client.api.domain.ClientAnnotation; import org.apache.olingo.client.api.domain.ClientCollectionValue; import org.apache.olingo.client.api.domain.ClientComplexValue; import org.apache.olingo.client.api.domain.ClientEntity; +import org.apache.olingo.client.api.domain.ClientEnumValue; +import org.apache.olingo.client.api.domain.ClientLink; +import org.apache.olingo.client.api.domain.ClientOperation; +import org.apache.olingo.client.api.domain.ClientPrimitiveValue; import org.apache.olingo.client.api.domain.ClientProperty; import org.apache.olingo.client.api.domain.ClientValue; +import org.apache.olingo.client.core.domain.ClientAnnotationImpl; import org.apache.olingo.client.core.serialization.JsonDeserializer; import org.apache.olingo.commons.api.Constants; import org.apache.olingo.commons.api.data.ComplexValue; import org.apache.olingo.commons.api.data.Delta; import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntityCollection; +import org.apache.olingo.commons.api.data.Link; import org.apache.olingo.commons.api.data.Property; +import org.apache.olingo.commons.api.data.ValueType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.format.ContentType; import org.junit.Test; @@ -128,6 +140,15 @@ public class JSONTest extends AbstractTest { assertEquals(expected, actualNode); } + private void assertJSONSimilar(final String filename, final String actual) throws Exception { + final JsonNode expected = OBJECT_MAPPER.readTree(IOUtils.toString(getClass().getResourceAsStream(filename)). + replace(Constants.JSON_NAVIGATION_LINK, Constants.JSON_BIND_LINK_SUFFIX)); + cleanup((ObjectNode) expected); + final ObjectNode actualNode = (ObjectNode) OBJECT_MAPPER.readTree(new ByteArrayInputStream(actual.getBytes())); + cleanup(actualNode); + assertEquals(expected, actualNode); + } + protected void entitySet(final String filename, final ContentType contentType) throws Exception { final StringWriter writer = new StringWriter(); client.getSerializer(contentType).write(writer, client.getDeserializer(contentType).toEntitySet( @@ -497,4 +518,337 @@ public class JSONTest extends AbstractTest { assertTrue(value.asEnum() == null); } + + @Test + public void issue2OLINGO1073_WithEntitySet() throws Exception { + final ClientEntity message = createClientEntity(); + + InputStream inputStream = client.getWriter().writeEntity(message, ContentType.APPLICATION_JSON); + ResWrap entity = new JsonDeserializer(false).toEntity(inputStream); + assertNotNull(entity); + + StringWriter writer = new StringWriter(); + setNavigationBindingLinkOnEntity(entity); + + client.getSerializer(ContentType.APPLICATION_JSON).write(writer, entity); + assertNotNull(writer.toString()); + writer = new StringWriter(); + client.getSerializer(ContentType.APPLICATION_JSON).write(writer, + new ResWrap(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/"), null, + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')"))); + assertNotNull(writer.toString()); + assertEquals("{\"@odata.context\":\"http://services.odata.org/V4/(" + + "S(fe5rsnxo3fkkkk2bvmh1nl1y))/TripPinServiceRW/\",\"@odata.id\":" + + "\"http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/TripPinServiceRW/" + + "People('russellwhyte')\"}", writer.toString()); + + writer = new StringWriter(); + Link linkPayload = new Link(); + linkPayload.setBindingLink("Photos"); + linkPayload.setMediaETag("xyz"); + linkPayload.setInlineEntity(createEntity()); + linkPayload.setTitle("Photos"); + linkPayload.setHref("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/Photos"); + client.getSerializer(ContentType.APPLICATION_JSON).write(writer, + new ResWrap(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/"), linkPayload.getMediaETag(), linkPayload)); + assertNotNull(writer.toString()); + assertEquals("{\"url\":\"http://services.odata.org/V4/" + + "(S(fe5rsnxo3fkkkk2bvmh1nl1y))/TripPinServiceRW/Photos\"}", writer.toString()); + } + + /** + * @return + */ + private ClientEntity createClientEntity() { + final ClientEntity message = client.getObjectFactory(). + newEntity(new FullQualifiedName("Microsoft.OData.SampleService.Models.TripPin.Person")); + + final ClientComplexValue cityComplexType = getCityComplexType(); + + final ClientComplexValue locationComplexType = client.getObjectFactory(). + newComplexValue("Microsoft.OData.SampleService.Models.TripPin.Location"); + locationComplexType.add(client.getObjectFactory().newPrimitiveProperty("Address", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("187 Suffolk Ln."))); + locationComplexType.add(client.getObjectFactory().newComplexProperty("City",cityComplexType)); + + final ClientComplexValue eventLocationComplexType = client.getObjectFactory(). + newComplexValue("Microsoft.OData.SampleService.Models.TripPin.EventLocation"); + eventLocationComplexType.add(client.getObjectFactory().newPrimitiveProperty("BuildingInfo", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("187 Suffolk Ln12."))); + eventLocationComplexType.add(client.getObjectFactory().newPrimitiveProperty("Address", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("187 Suffolk Ln12."))); + eventLocationComplexType.add(client.getObjectFactory().newComplexProperty("City",cityComplexType)); + + final ClientComplexValue airportLocationComplexType = client.getObjectFactory(). + newComplexValue("Microsoft.OData.SampleService.Models.TripPin.AirportLocation"); + airportLocationComplexType.add(client.getObjectFactory().newPrimitiveProperty("Address", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("187 Suffolk Ln123."))); + airportLocationComplexType.add(client.getObjectFactory().newComplexProperty("City",cityComplexType)); + + final ClientCollectionValue collectionAddressInfo = client.getObjectFactory(). + newCollectionValue("Microsoft.OData.SampleService.Models.TripPin.Location"); + collectionAddressInfo.add(locationComplexType); + collectionAddressInfo.add(eventLocationComplexType); + collectionAddressInfo.add(airportLocationComplexType); + + message.getProperties().add(client.getObjectFactory().newPrimitiveProperty("UserName", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("russellwhyte"))); + message.getProperties().add(client.getObjectFactory().newPrimitiveProperty("FirstName", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("Russell"))); + message.getProperties().add(client.getObjectFactory().newPrimitiveProperty("LastName", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("Whyte"))); + final ClientLink messageLink1 = client.getObjectFactory().newEntityNavigationLink("Photo", + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')/Photo")); + final ClientAnnotation messageLink1Annotation = createAnnotation(); + messageLink1.getAnnotations().add(messageLink1Annotation); + + final ClientLink messageLink2 = client.getObjectFactory().newEntitySetNavigationLink("Friends", + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')/Friends")); + final ClientAnnotation messageLink2Annotation = createAnnotation(); + messageLink2.getAnnotations().add(messageLink2Annotation); + + final ClientLink messageLink3 = client.getObjectFactory().newEntitySetNavigationLink("Trips", + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')/Trips")); + final ClientAnnotation messageLink3Annotation = createAnnotation(); + messageLink3.getAnnotations().add(messageLink3Annotation); + + message.getNavigationLinks().add(messageLink1); + message.getNavigationLinks().add(messageLink2); + message.getNavigationLinks().add(messageLink3); + + final ClientAnnotation messageAnnotation = createAnnotation(); + message.getAnnotations().add(messageAnnotation); + + final ClientCollectionValue emailCollectionValue = client.getObjectFactory(). + newCollectionValue("String"); + emailCollectionValue.add(client.getObjectFactory().newPrimitiveValueBuilder().buildString("Russell@example.com")); + emailCollectionValue.add(client.getObjectFactory().newPrimitiveValueBuilder().buildString("Russell@contoso.com")); + message.getProperties().add(client.getObjectFactory().newCollectionProperty("Emails", emailCollectionValue)); + + message.getProperties().add(client.getObjectFactory().newCollectionProperty("AddressInfo", collectionAddressInfo)); + message.getProperties().add(client.getObjectFactory().newEnumProperty("Gender", + client.getObjectFactory().newEnumValue( + "Microsoft.OData.SampleService.Models.TripPin.PersonGender", "Male"))); + message.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Concurrency", + client.getObjectFactory().newPrimitiveValueBuilder().buildInt64(Long.valueOf("636293755917400747")))); + message.setId(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')")); + message.setETag("W/\"08D491CCBE417AAB\""); + message.setEditLink(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')")); + + final ClientEntity innerEntity = client.getObjectFactory(). + newEntity(new FullQualifiedName("Microsoft.OData.SampleService.Models.TripPin.Photo")); + innerEntity.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Id", + client.getObjectFactory().newPrimitiveValueBuilder().buildInt64(Long.valueOf(123)))); + innerEntity.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Name", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("ABC"))); + innerEntity.getAnnotations().add(createAnnotation()); + final ClientLink link = client.getObjectFactory().newDeepInsertEntity("Photos", innerEntity); + final ClientAnnotation linkAnnotation = createAnnotation(); + link.getAnnotations().add(linkAnnotation); + message.getNavigationLinks().add(link); + + final ClientLink assoLink = client.getObjectFactory().newAssociationLink("Photos", + URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/People('russellwhyte')/Photo")); + final ClientAnnotation assoLinkAnnotation = createAnnotation(); + assoLink.getAnnotations().add(assoLinkAnnotation); + + message.getAssociationLinks().add(assoLink); + final ClientOperation operation = new ClientOperation(); + operation.setTarget(URI.create("http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/" + + "TripPinServiceRW/Photos")); + operation.setTitle("Photos"); + message.getOperations().add(operation); + return message; + } + + /** + * @param entity + */ + private void setNavigationBindingLinkOnEntity(ResWrap entity) { + Link entityLink = new Link(); + Entity en = createEntity(); + + entityLink.setBindingLink("Photos"); + entityLink.setInlineEntity(en); + entityLink.setType("Microsoft.OData.SampleService.Models.TripPin.Photos"); + + Link entityColLink = new Link(); + EntityCollection enCol = new EntityCollection(); + enCol.getEntities().add(en); + + entityColLink.setBindingLink("Friends"); + entityColLink.setInlineEntitySet(enCol); + entityColLink.setType("Microsoft.OData.SampleService.Models.TripPin.Friends"); + + Link link = new Link(); + link.setBindingLink("Trips"); + link.setType("Microsoft.OData.SampleService.Models.TripPin.Trips"); + + entity.getPayload().getNavigationBindings().add(entityLink); + entity.getPayload().getNavigationBindings().add(entityColLink); + entity.getPayload().getNavigationBindings().add(link); + } + + /** + * @return + */ + private Entity createEntity() { + Entity en = new Entity(); + Property p1 = new Property(); + p1.setName("Id"); + p1.setType("Int64"); + p1.setValue(ValueType.PRIMITIVE, Long.valueOf(123)); + en.addProperty(p1); + + Property p2 = new Property(); + p2.setName("Name"); + p2.setType("String"); + p2.setValue(ValueType.PRIMITIVE, "ABC"); + en.addProperty(p2); + return en; + } + + /** + * @return + */ + private ClientAnnotation createAnnotation() { + final ClientAnnotation messageAnnotation = + new ClientAnnotationImpl("Org.OData.Core.V1.Permissions", new ClientPrimitiveValue() { + + @Override + public boolean isPrimitive() { + return false; + } + + @Override + public boolean isEnum() { + return true; + } + + @Override + public boolean isComplex() { + return false; + } + + @Override + public boolean isCollection() { + return false; + } + + @Override + public String getTypeName() { + return "String"; + } + + @Override + public ClientPrimitiveValue asPrimitive() { + return null; + } + + @Override + public ClientEnumValue asEnum() { + return client.getObjectFactory().newEnumValue("Org.OData.Core.V1.Permissions", "Read"); + } + + @Override + public ClientComplexValue asComplex() { + return null; + } + + @Override + public ClientCollectionValue asCollection() { + return null; + } + + @Override + public Object toValue() { + return client.getObjectFactory().newEnumValue("Org.OData.Core.V1.Permissions", "Read"); + } + + @Override + public T toCastValue(Class reference) throws EdmPrimitiveTypeException { + return null; + } + + @Override + public EdmPrimitiveTypeKind getTypeKind() { + return null; + } + + @Override + public EdmPrimitiveType getType() { + return null; + } + }); + return messageAnnotation; + } + + protected void property1(final String filename) throws Exception { + final StringWriter writer = new StringWriter(); + client.getSerializer(ContentType.APPLICATION_JSON).write(writer, + client.getDeserializer(ContentType.APPLICATION_JSON). + toProperty(getClass().getResourceAsStream(filename + ".json"))); + + assertJSONSimilar(filename + ".json", writer.toString()); + } + + @Test + public void properties1() throws Exception { + property1("Products_5_SkinColor"); + property1("Products_5_CoverColors"); + property1("Employees_3_HomeAddress"); + property1("Employees_3_HomeAddress"); + } + + protected void entity1(final String filename) throws Exception { + final StringWriter writer = new StringWriter(); + client.getSerializer(ContentType.APPLICATION_JSON).write(writer, client.getDeserializer( + ContentType.APPLICATION_JSON).toEntity( + getClass().getResourceAsStream(filename + ".json"))); + assertJSONSimilar(filename + ".json", writer.toString()); + } + + @Test + public void additionalEntities1() throws Exception { + entity1("entity.minimal"); + entity1("entity.primitive"); + entity1("entity.complex"); + entity1("entity.collection.primitive"); + entity1("entity.collection.complex"); + } + + @Test + public void entities1() throws Exception { + entity1("Products_5"); + entity1("VipCustomer"); + entity1("Advertisements_f89dee73-af9f-4cd4-b330-db93c25ff3c7"); + entity1("entityReference"); + entity1("entity.withcomplexnavigation"); + entity1("annotated"); + } + + protected void entitySet1(final String filename) throws Exception { + final StringWriter writer = new StringWriter(); + client.getSerializer(ContentType.APPLICATION_JSON).write(writer, + client.getDeserializer(ContentType.APPLICATION_JSON).toEntitySet( + getClass().getResourceAsStream(filename + ".json"))); + + assertJSONSimilar(filename + ".json", writer.toString()); + } + + @Test + public void entitySets1() throws Exception { + entitySet1("Customers"); + entitySet1("collectionOfEntityReferences"); + } } diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/MetadataTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/MetadataTest.java index d62496836..1288b08b2 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/MetadataTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/MetadataTest.java @@ -258,7 +258,7 @@ public class MetadataTest extends AbstractTest { EdmEntityContainer demoService = schema.getEntityContainer(); assertNotNull(demoService); - for (EdmFunction function : schema.getFunctions()) { + EdmFunction function = schema.getFunctions().get(0); final EdmFunctionImport fi = demoService.getFunctionImport(function.getName()); assertNotNull(fi); assertEquals(demoService.getEntitySet("Products"), fi.getReturnedEntitySet()); @@ -276,7 +276,6 @@ public class MetadataTest extends AbstractTest { fi.getUnboundFunction(function.getParameterNames()).getReturnType().getType().getName()); assertEquals(edmFunction.getReturnType().getType().getNamespace(), fi.getUnboundFunction(function.getParameterNames()).getReturnType().getType().getNamespace()); - } final EdmTypeDefinition weight = edm.getTypeDefinition(new FullQualifiedName("ODataDemo", "Weight")); assertNotNull(weight); diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/uri/FilterFactoryTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/uri/FilterFactoryTest.java index 22ad4525f..0bd55106c 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/uri/FilterFactoryTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/uri/FilterFactoryTest.java @@ -21,7 +21,6 @@ package org.apache.olingo.client.core.uri; import static org.junit.Assert.assertEquals; import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.util.Calendar; import java.util.TimeZone; @@ -29,7 +28,6 @@ import org.apache.olingo.client.api.uri.FilterArgFactory; import org.apache.olingo.client.api.uri.FilterFactory; import org.apache.olingo.client.api.uri.URIFilter; import org.apache.olingo.client.core.AbstractTest; -import org.apache.olingo.commons.api.Constants; import org.apache.olingo.commons.api.edm.EdmEnumType; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.provider.CsdlEnumType; diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/uri/URIEscapeTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/uri/URIEscapeTest.java index f25c82fbf..969836575 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/uri/URIEscapeTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/uri/URIEscapeTest.java @@ -26,7 +26,6 @@ import java.util.Calendar; import java.util.Collections; import java.util.TimeZone; -import org.apache.olingo.commons.api.Constants; import org.apache.olingo.commons.api.edm.EdmEnumType; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.geo.Geospatial; diff --git a/lib/client-core/src/test/resources/org/apache/olingo/client/core/CustomersWithOperations.json b/lib/client-core/src/test/resources/org/apache/olingo/client/core/CustomersWithOperations.json new file mode 100644 index 000000000..15abbdef5 --- /dev/null +++ b/lib/client-core/src/test/resources/org/apache/olingo/client/core/CustomersWithOperations.json @@ -0,0 +1,61 @@ +{ + "@odata.context": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/$metadata#Customers", + "@odata.count": 2, + "value": [{ + "@odata.id": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)", + "@odata.editLink": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)", + "PersonID": 1, + "FirstName": "Bob", + "LastName": "Cat", + "MiddleName": null, + "HomeAddress": { + "@odata.type": "#Microsoft.Test.OData.Services.ODataWCFService.HomeAddress", + "Street": "1 Microsoft Way", + "City": "London", + "PostalCode": "98052", + "FamilyName": "Cats" + }, + "Home": { + "type": "Point", + "coordinates": [23.1, 32.1], + "crs": { + "type": "name", + "properties": { + "name": "EPSG:4326" + } + } + }, + "Numbers": ["111-111-1111"], + "Emails": ["abc@abc.com"], + "City": "London", + "Birthday": "1957-04-03T00:00:00Z", + "TimeBetweenLastTwoOrders": "PT0.0000001S" + }, { + "@odata.id": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)", + "@odata.editLink": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)", + "PersonID": 2, + "FirstName": "Jill", + "LastName": "Jones", + "MiddleName": null, + "HomeAddress": null, + "Home": { + "type": "Point", + "coordinates": [161.8, 15.0], + "crs": { + "type": "name", + "properties": { + "name": "EPSG:4326" + } + } + }, + "Numbers": [], + "Emails": [], + "City": "Sydney", + "Birthday": "1983-01-15T00:00:00Z", + "TimeBetweenLastTwoOrders": "PT0.0000002S" + }], + "#Microsoft.Test.OData.Services.ODataWCFService.BAESAllPrimRTETAllPrim": { + "title": "Microsoft.Test.OData.Services.ODataWCFService.BAESAllPrimRTETAllPrim", + "target": "Customers/Microsoft.Test.OData.Services.ODataWCFService.BAESAllPrimRTETAllPrim" + } +} diff --git a/lib/client-core/src/test/resources/org/apache/olingo/client/core/VOC_Core.xml b/lib/client-core/src/test/resources/org/apache/olingo/client/core/VOC_Core.xml index 4e30cadbf..d3a296db1 100644 --- a/lib/client-core/src/test/resources/org/apache/olingo/client/core/VOC_Core.xml +++ b/lib/client-core/src/test/resources/org/apache/olingo/client/core/VOC_Core.xml @@ -28,12 +28,12 @@ - + - + @@ -105,7 +105,7 @@ - + diff --git a/lib/client-core/src/test/resources/org/apache/olingo/client/core/fromdoc1-metadata.xml b/lib/client-core/src/test/resources/org/apache/olingo/client/core/fromdoc1-metadata.xml index 74a71d4fe..59e2cee09 100644 --- a/lib/client-core/src/test/resources/org/apache/olingo/client/core/fromdoc1-metadata.xml +++ b/lib/client-core/src/test/resources/org/apache/olingo/client/core/fromdoc1-metadata.xml @@ -28,12 +28,13 @@ - + + @@ -96,8 +97,20 @@ - - + + + + + + + + + + + + + + @@ -120,6 +133,9 @@ + + + diff --git a/lib/client-core/src/test/resources/org/apache/olingo/client/core/fromdoc3-metadata.xml b/lib/client-core/src/test/resources/org/apache/olingo/client/core/fromdoc3-metadata.xml index 36e5216d5..8a7623aa5 100644 --- a/lib/client-core/src/test/resources/org/apache/olingo/client/core/fromdoc3-metadata.xml +++ b/lib/client-core/src/test/resources/org/apache/olingo/client/core/fromdoc3-metadata.xml @@ -57,11 +57,18 @@ - + Average + + + Testing Cast of Average Field + + + + MasterData @@ -81,8 +88,13 @@ - + Customer + + + IsPreferred + + diff --git a/lib/client-core/src/test/resources/org/apache/olingo/client/core/tripinServEntity.json b/lib/client-core/src/test/resources/org/apache/olingo/client/core/tripinServEntity.json new file mode 100644 index 000000000..7983f11c2 --- /dev/null +++ b/lib/client-core/src/test/resources/org/apache/olingo/client/core/tripinServEntity.json @@ -0,0 +1,60 @@ +{ + "@odata.context": "$metadata#People/$entity", + "@odata.id": "http://services.odata.org/V4/(S(fe5rsnxo3fkkkk2bvmh1nl1y))/TripPinServiceRW/People('russellwhyte')", + "UserName@odata.type": "String", + "UserName": "russellwhyte", + "FirstName@odata.type": "String", + "FirstName": "Russell", + "LastName@odata.type": "String", + "LastName": "Whyte", + "AddressInfo": [ + { + "@odata.type": "#Microsoft.OData.SampleService.Models.TripPin.Location", + "Address@odata.type": "String", + "Address": "187 Suffolk Ln.", + "City": { + "@odata.type": "#Microsoft.OData.SampleService.Models.TripPin.City", + "CountryRegion@odata.type": "String", + "CountryRegion": "United States", + "Name@odata.type": "String", + "Name": "Boise", + "Region@odata.type": "String", + "Region": "ID" + } + }, + { + "@odata.type": "#Microsoft.OData.SampleService.Models.TripPin.EventLocation", + "BuildingInfo@odata.type": "String", + "BuildingInfo": "187 Suffolk Ln12.", + "Address@odata.type": "String", + "Address": "187 Suffolk Ln12.", + "City": { + "@odata.type": "#Microsoft.OData.SampleService.Models.TripPin.City", + "CountryRegion@odata.type": "String", + "CountryRegion": "United States", + "Name@odata.type": "String", + "Name": "Boise", + "Region@odata.type": "String", + "Region": "ID" + } + }, + { + "@odata.type": "#Microsoft.OData.SampleService.Models.TripPin.AirportLocation", + "Address@odata.type": "String", + "Address": "187 Suffolk Ln123.", + "City": { + "@odata.type": "#Microsoft.OData.SampleService.Models.TripPin.City", + "CountryRegion@odata.type": "String", + "CountryRegion": "United States", + "Name@odata.type": "String", + "Name": "Boise", + "Region@odata.type": "String", + "Region": "ID" + } + } + ], + "Gender@odata.type": "#Microsoft.OData.SampleService.Models.TripPin.PersonGender", + "Gender": "Male", + "Concurrency@odata.type": "Int64", + "Concurrency": 636293755917400747 +} \ No newline at end of file