diff --git a/fit/src/main/java/org/apache/olingo/fit/utils/AbstractUtilities.java b/fit/src/main/java/org/apache/olingo/fit/utils/AbstractUtilities.java index 5b23366aa..b4d71b5c5 100644 --- a/fit/src/main/java/org/apache/olingo/fit/utils/AbstractUtilities.java +++ b/fit/src/main/java/org/apache/olingo/fit/utils/AbstractUtilities.java @@ -511,6 +511,19 @@ public abstract class AbstractUtilities { } } sequence.put(entitySetName, Integer.valueOf(res)); + } else if ("Orders".equals(entitySetName)) { + try { + final Map value = + getPropertyValues(entity, Collections.singletonList("OrderID")); + res = value.isEmpty() ? null : IOUtils.toString(value.values().iterator().next()); + } catch (Exception e) { + if (sequence.containsKey(entitySetName)) { + res = String.valueOf(sequence.get(entitySetName) + 1); + } else { + throw new Exception(String.format("Unable to retrieve entity key value for %s", entitySetName)); + } + } + sequence.put(entitySetName, Integer.valueOf(res)); } else if ("Customer".equals(entitySetName)) { try { final Map value = diff --git a/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java b/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java index c0031f11d..a1c658ada 100644 --- a/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java +++ b/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java @@ -67,6 +67,7 @@ public abstract class Commons { sequence.put("Order", 1000); sequence.put("ComputerDetail", 1000); sequence.put("AllGeoTypesSet", 1000); + sequence.put("Orders", 1000); mediaContent.put("CustomerInfo", "CustomerinfoId"); mediaContent.put("Car", "VIN"); diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v4/EntityCreateTestITCase.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v4/EntityCreateTestITCase.java new file mode 100644 index 000000000..0044e73f7 --- /dev/null +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v4/EntityCreateTestITCase.java @@ -0,0 +1,84 @@ +/* + * 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.client.core.it.v4; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest; +import org.apache.olingo.commons.api.domain.ODataCollectionValue; +import org.apache.olingo.commons.api.domain.v4.ODataEntity; +import org.apache.olingo.commons.api.domain.v4.ODataProperty; +import org.apache.olingo.commons.api.domain.ODataValue; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.format.ODataPubFormat; +import org.apache.olingo.commons.core.domain.v3.ODataCollectionValueImpl; +import org.apache.olingo.commons.core.domain.v4.ODataEntityImpl; +import org.junit.Test; + +public class EntityCreateTestITCase extends AbstractTestITCase { + + private void createOrder(final ODataPubFormat format, final int id) { + final ODataEntity order = new ODataEntityImpl("Microsoft.Test.OData.Services.ODataWCFService.Order"); + + final ODataProperty orderId = getClient().getObjectFactory().newPrimitiveProperty("OrderID", + getClient().getObjectFactory().newPrimitiveValueBuilder(). + setType(EdmPrimitiveTypeKind.Int32).setValue(id).build()); + order.getProperties().add(orderId); + + final ODataProperty orderDate = getClient().getObjectFactory().newPrimitiveProperty("OrderDate", + getClient().getObjectFactory().newPrimitiveValueBuilder(). + setType(EdmPrimitiveTypeKind.DateTimeOffset).setText("2011-03-04T16:03:57Z").build()); + order.getProperties().add(orderDate); + + final ODataProperty shelfLife = getClient().getObjectFactory().newPrimitiveProperty("ShelfLife", + getClient().getObjectFactory().newPrimitiveValueBuilder(). + setType(EdmPrimitiveTypeKind.Duration).setText("PT0.0000001S").build()); + order.getProperties().add(shelfLife); + + // TODO: this should be possible via getClient().getObjectFactory().newCollectionValue() + final ODataCollectionValue orderShelfLifesValue = + new ODataCollectionValueImpl("Collection(Duration)"); + orderShelfLifesValue.add(getClient().getObjectFactory().newPrimitiveValueBuilder(). + setType(EdmPrimitiveTypeKind.Duration).setText("PT0.0000001S").build()); + orderShelfLifesValue.add(getClient().getObjectFactory().newPrimitiveValueBuilder(). + setType(EdmPrimitiveTypeKind.Duration).setText("PT0.0000002S").build()); + final ODataProperty orderShelfLifes = getClient().getObjectFactory().newCollectionProperty("OrderShelfLifes", + orderShelfLifesValue); + order.getProperties().add(orderShelfLifes); + + final ODataEntityCreateRequest req = getClient().getCUDRequestFactory().getEntityCreateRequest( + getClient().getURIBuilder(testStaticServiceRootURL). + appendEntitySetSegment("Orders").build(), order); + req.setFormat(format); + final ODataEntity created = req.execute().getBody(); + assertNotNull(created); + assertEquals(2, created.getProperty("OrderShelfLifes").getCollectionValue().size()); + } + + @Test + public void atom() { + createOrder(ODataPubFormat.ATOM, 1000); + } + + @Test + public void json() { + createOrder(ODataPubFormat.JSON, 1001); + } +}