[OLINGO-260] provided create with back navigation link integration test on proxy
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/3af2c8ce Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/3af2c8ce Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/3af2c8ce Branch: refs/heads/olingo-266-ref Commit: 3af2c8ce4fdfadfe5a3bf180decc81204dd37f2c Parents: 322a3ef Author: fmartelli <[email protected]> Authored: Mon May 12 17:03:30 2014 +0200 Committer: fmartelli <[email protected]> Committed: Mon May 12 17:03:30 2014 +0200 ---------------------------------------------------------------------- .../olingo/ext/proxy/commons/ContainerImpl.java | 10 +-- .../commons/EntityTypeInvocationHandler.java | 30 ++++---- .../org/apache/olingo/fit/AbstractServices.java | 55 ++++++++------ .../org/apache/olingo/fit/utils/Commons.java | 2 +- .../org/apache/olingo/fit/utils/FSManager.java | 1 + .../fit/proxy/v3/EntityCreateTestITCase.java | 3 +- .../apache/olingo/fit/v3/BatchTestITCase.java | 79 +++++++++++++++++++- 7 files changed, 130 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3af2c8ce/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ContainerImpl.java ---------------------------------------------------------------------- diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ContainerImpl.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ContainerImpl.java index e89c843..8959351 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ContainerImpl.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/ContainerImpl.java @@ -139,11 +139,11 @@ class ContainerImpl implements Container { if (handler != null) { if (res instanceof ODataEntityCreateResponse) { - LOG.debug("Upgrade created object '{}'", handler); handler.setEntity(((ODataEntityCreateResponse) res).getBody()); + LOG.debug("Upgrade created object '{}'", handler); } else if (res instanceof ODataEntityUpdateResponse) { - LOG.debug("Upgrade updated object '{}'", handler); handler.setEntity(((ODataEntityUpdateResponse) res).getBody()); + LOG.debug("Upgrade updated object '{}'", handler); } } } @@ -308,8 +308,7 @@ class ContainerImpl implements Container { final CommonODataEntity entity = handler.getEntity(); entity.getNavigationLinks().clear(); - final AttachedEntityStatus currentStatus = EntityContainerFactory.getContext().entityContext(). - getStatus(handler); + final AttachedEntityStatus currentStatus = EntityContainerFactory.getContext().entityContext().getStatus(handler); if (AttachedEntityStatus.DELETED != currentStatus) { entity.getProperties().clear(); @@ -335,8 +334,7 @@ class ContainerImpl implements Container { final URI editLink = target.getEntity().getEditLink(); - if ((status == AttachedEntityStatus.ATTACHED || status == AttachedEntityStatus.LINKED) - && !target.isChanged()) { + if ((status == AttachedEntityStatus.ATTACHED || status == AttachedEntityStatus.LINKED) && !target.isChanged()) { entity.addLink(buildNavigationLink( property.getKey().name(), URIUtils.getURI(serviceRoot, editLink.toASCIIString()), type)); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3af2c8ce/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityTypeInvocationHandler.java ---------------------------------------------------------------------- diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityTypeInvocationHandler.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityTypeInvocationHandler.java index 449a8d1..3a825e8 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityTypeInvocationHandler.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntityTypeInvocationHandler.java @@ -56,8 +56,6 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? private static final long serialVersionUID = 2629912294765040037L; - private CommonODataEntity entity; - protected Map<String, Object> propertyChanges = new HashMap<String, Object>(); protected Map<NavigationProperty, Object> linkChanges = new HashMap<NavigationProperty, Object>(); @@ -103,8 +101,8 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? super(containerHandler.getClient(), typeRef, (ODataLinked) entity, containerHandler); - this.entity = entity; - this.entity.setMediaEntity(typeRef.getAnnotation(EntityType.class).hasStream()); + this.internal = entity; + getEntity().setMediaEntity(typeRef.getAnnotation(EntityType.class).hasStream()); this.uuid = new EntityUUID( containerHandler.getEntityContainerName(), @@ -116,8 +114,8 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? } public void setEntity(final CommonODataEntity entity) { - this.entity = entity; - this.entity.setMediaEntity(typeRef.getAnnotation(EntityType.class).hasStream()); + this.internal = entity; + getEntity().setMediaEntity(typeRef.getAnnotation(EntityType.class).hasStream()); this.uuid = new EntityUUID( getUUID().getContainerName(), @@ -139,7 +137,7 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? @Override public FullQualifiedName getName() { - return this.entity.getTypeName(); + return getEntity().getTypeName(); } public String getEntityContainerName() { @@ -150,8 +148,8 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? return uuid.getEntitySetName(); } - public CommonODataEntity getEntity() { - return entity; + public final CommonODataEntity getEntity() { + return (CommonODataEntity) internal; } /** @@ -160,7 +158,7 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? * @return */ public String getETag() { - return this.entity.getETag(); + return getEntity().getETag(); } /** @@ -169,7 +167,7 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? * @param eTag ETag. */ public void setETag(final String eTag) { - this.entity.setETag(eTag); + getEntity().setETag(eTag); } public Map<String, Object> getPropertyChanges() { @@ -196,7 +194,7 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? protected Object getPropertyValue(final String name, final Type type) { try { final Object res; - final CommonODataProperty property = entity.getProperty(name); + final CommonODataProperty property = getEntity().getProperty(name); if (propertyChanges.containsKey(name)) { res = propertyChanges.get(name); @@ -266,7 +264,7 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? } } - for (CommonODataProperty property : entity.getProperties()) { + for (CommonODataProperty property : getEntity().getProperties()) { if (!propertyNames.contains(property.getName())) { res.add(property.getName()); } @@ -328,14 +326,14 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? public InputStream getStream() { - final URI contentSource = entity.getMediaContentSource(); + final URI contentSource = getEntity().getMediaContentSource(); if (this.stream == null && typeRef.getAnnotation(EntityType.class).hasStream() && contentSource != null) { final String contentType = - StringUtils.isBlank(entity.getMediaContentType()) ? "*/*" : entity.getMediaContentType(); + StringUtils.isBlank(getEntity().getMediaContentType()) ? "*/*" : getEntity().getMediaContentType(); final ODataMediaRequest retrieveReq = client.getRetrieveRequestFactory().getMediaRequest(contentSource); retrieveReq.setFormat(ODataMediaFormat.fromFormat(contentType)); @@ -354,7 +352,7 @@ public class EntityTypeInvocationHandler<C extends CommonEdmEnabledODataClient<? if (res == null) { final URI link = URIUtils.getURI( containerHandler.getFactory().getServiceRoot(), - CoreUtils.getEditMediaLink(property.name(), this.entity).toASCIIString()); + CoreUtils.getEditMediaLink(property.name(), getEntity()).toASCIIString()); final ODataMediaRequest req = client.getRetrieveRequestFactory().getMediaRequest(link); res = req.execute().getBody(); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3af2c8ce/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java ---------------------------------------------------------------------- diff --git a/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java b/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java index bdaaf0e..948bcd9 100644 --- a/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java +++ b/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java @@ -117,6 +117,8 @@ public abstract class AbstractServices { private static final Pattern BATCH_REQUEST_REF_PATTERN = Pattern.compile("(.*) ([$].*) HTTP/.*"); + private static final Pattern REF_PATTERN = Pattern.compile("([$]\\d+)"); + protected static final String BOUNDARY = "batch_243234_25424_ef_892u748"; protected final ODataServiceVersion version; @@ -231,41 +233,48 @@ public abstract class AbstractServices { } final Response res; + final String url; + final String method; if (matcher.find()) { - final String url = matcher.group(2); + url = matcher.group(2); + method = matcher.group(1); + } else if (matcherRef.find()) { + url = references.get(matcherRef.group(2)); + method = matcherRef.group(1); + } else { + url = null; + method = null; + } + + if (url == null) { + res = null; + } else { final WebClient client = WebClient.create(url); client.headers(headers); - final String method = matcher.group(1); if ("DELETE".equals(method)) { res = client.delete(); - } else if ("PATCH".equals(method) || "MERGE".equals(method)) { - client.header("X-HTTP-METHOD", method); - res = client.invoke("POST", body.getDataHandler().getInputStream()); } else { - res = client.invoke(method, body.getDataHandler().getInputStream()); - } + final InputStream is = body.getDataHandler().getInputStream(); + String content = IOUtils.toString(is); + IOUtils.closeQuietly(is); - client.close(); - } else if (matcherRef.find()) { - final String url = matcherRef.group(2); - final WebClient client = WebClient.create(references.get(url)); - client.headers(headers); + final Matcher refs = REF_PATTERN.matcher(content); - String method = matcherRef.group(1); - if ("DELETE".equals(method)) { - res = client.delete(); - } else if ("PATCH".equals(method) || "MERGE".equals(method)) { - client.header("X-HTTP-METHOD", method); - res = client.invoke("POST", body.getDataHandler().getInputStream()); - } else { - res = client.invoke(method, body.getDataHandler().getInputStream()); + while (refs.find()) { + content = content.replace(refs.group(1), references.get(refs.group(1))); + } + + if ("PATCH".equals(method) || "MERGE".equals(method)) { + client.header("X-HTTP-METHOD", method); + res = client.invoke("POST", IOUtils.toInputStream(content)); + } else { + res = client.invoke(method, IOUtils.toInputStream(content)); + } } client.close(); - } else { - res = null; } return res; @@ -621,7 +630,7 @@ public abstract class AbstractServices { writer.close(); final InputStream serialization = - xml.addOrReplaceEntity(null, entitySetName, new ByteArrayInputStream(content.toByteArray()), entry); + xml.addOrReplaceEntity(entityKey, entitySetName, new ByteArrayInputStream(content.toByteArray()), entry); ResWrap<AtomEntityImpl> result = atomDeserializer.read(serialization, AtomEntityImpl.class); result = new ResWrap<AtomEntityImpl>( http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3af2c8ce/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java ---------------------------------------------------------------------- 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 74c2284..262ab36 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 @@ -100,7 +100,7 @@ public abstract class Commons { SEQUENCE.put("People", 1000); MEDIA_CONTENT.put("CustomerInfo", - new ImmutablePair<String, EdmPrimitiveTypeKind>("CustomerinfoId", EdmPrimitiveTypeKind.Int32)); + new ImmutablePair<String, EdmPrimitiveTypeKind>("CustomerInfoId", EdmPrimitiveTypeKind.Int32)); MEDIA_CONTENT.put("Car", new ImmutablePair<String, EdmPrimitiveTypeKind>("VIN", EdmPrimitiveTypeKind.Int32)); MEDIA_CONTENT.put("Car/Photo", null); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3af2c8ce/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java ---------------------------------------------------------------------- diff --git a/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java b/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java index 4ad3871..2599b8f 100644 --- a/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java +++ b/fit/src/main/java/org/apache/olingo/fit/utils/FSManager.java @@ -80,6 +80,7 @@ public class FSManager { } public FileObject putInMemory(final InputStream is, final String path) throws IOException { + LOG.info("Write in memory {}", path); final FileObject memObject = fsManager.resolveFile(MEM_PREFIX + path); if (memObject.exists()) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3af2c8ce/fit/src/test/java/org/apache/olingo/fit/proxy/v3/EntityCreateTestITCase.java ---------------------------------------------------------------------- diff --git a/fit/src/test/java/org/apache/olingo/fit/proxy/v3/EntityCreateTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/proxy/v3/EntityCreateTestITCase.java index 4118706..a930100 100644 --- a/fit/src/test/java/org/apache/olingo/fit/proxy/v3/EntityCreateTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v3/EntityCreateTestITCase.java @@ -18,6 +18,8 @@ */ package org.apache.olingo.fit.proxy.v3; +import java.lang.reflect.Proxy; +import org.apache.olingo.ext.proxy.commons.EntityTypeInvocationHandler; import static org.apache.olingo.fit.proxy.v3.AbstractTest.container; import org.apache.olingo.fit.proxy.v3.staticservice.microsoft.test.odata.services.astoriadefaultservice. types.Customer; @@ -129,7 +131,6 @@ public class EntityCreateTestITCase extends AbstractTest { } @Test - @Ignore public void createWithBackNavigation() { final String sampleName = "sample customer from proxy with back navigation"; final Integer id = 102; http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3af2c8ce/fit/src/test/java/org/apache/olingo/fit/v3/BatchTestITCase.java ---------------------------------------------------------------------- diff --git a/fit/src/test/java/org/apache/olingo/fit/v3/BatchTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/v3/BatchTestITCase.java index 1ed6d22..3b73671 100644 --- a/fit/src/test/java/org/apache/olingo/fit/v3/BatchTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/v3/BatchTestITCase.java @@ -56,6 +56,7 @@ import org.apache.olingo.client.core.communication.request.retrieve.ODataEntityR import org.apache.olingo.client.core.uri.URIUtils; import org.apache.olingo.commons.api.domain.v3.ODataEntity; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.format.ODataPubFormat; import static org.apache.olingo.fit.v3.AbstractTestITCase.client; import org.junit.Test; @@ -230,6 +231,78 @@ public class BatchTestITCase extends AbstractTestITCase { @Test @SuppressWarnings("unchecked") + public void updateLinkWithReference() throws EdmPrimitiveTypeException { + // create your request + final ODataBatchRequest request = client.getBatchRequestFactory().getBatchRequest(testStaticServiceRootURL); + final BatchStreamManager streamManager = request.execute(); + + final ODataChangeset changeset = streamManager.addChangeset(); + + final ODataEntity info = + client.getObjectFactory().newEntity( + new FullQualifiedName("Microsoft.Test.OData.Services.AstoriaDefaultService.CustomerInfo")); + + info.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Information", + client.getObjectFactory().newPrimitiveValueBuilder().buildString("Sample information about customer 30"))); + + URIBuilder uriBuilder = client.getURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("CustomerInfo"); + + ODataEntityCreateRequest<ODataEntity> createReq = + client.getCUDRequestFactory().getEntityCreateRequest(uriBuilder.build(), info); + + changeset.addRequest(createReq); + + // retrieve request reference + int createRequestRef = changeset.getLastContentId(); + + ODataEntity customer = getSampleCustomerProfile(30, "sample customer", false); + customer.getNavigationLinks().add( + client.getObjectFactory().newEntityNavigationLink("Info", URI.create("$" + createRequestRef))); + + uriBuilder = client.getURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("Customer"); + + // add create request + createReq = client.getCUDRequestFactory().getEntityCreateRequest(uriBuilder.build(), customer); + + changeset.addRequest(createReq); + + final ODataBatchResponse response = streamManager.getResponse(); + assertEquals(202, response.getStatusCode()); + assertEquals("Accepted", response.getStatusMessage()); + + // verify response payload ... + final Iterator<ODataBatchResponseItem> iter = response.getBody(); + + final ODataBatchResponseItem item = iter.next(); + assertTrue(item instanceof ODataChangesetResponseItem); + + ODataChangesetResponseItem chgitem = (ODataChangesetResponseItem) item; + + ODataResponse res = chgitem.next(); + assertEquals(201, res.getStatusCode()); + assertTrue(res instanceof ODataEntityCreateResponse); + + final ODataEntity infoEntity = ((ODataEntityCreateResponse<ODataEntity>) res).getBody(); + + chgitem = (ODataChangesetResponseItem) item; + + res = chgitem.next(); + assertEquals(201, res.getStatusCode()); + assertTrue(res instanceof ODataEntityCreateResponse); + + uriBuilder = client.getURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("Customer").appendKeySegment(30); + + final ODataEntityRequest<ODataEntity> req = client.getRetrieveRequestFactory().getEntityRequest( + URIUtils.getURI(testStaticServiceRootURL, uriBuilder.build() + "/Info")); + + final ODataEntity navigatedInfo = req.execute().getBody(); + + assertEquals(infoEntity.getProperty("CustomerInfoId").getPrimitiveValue().toCastValue(Integer.class), + navigatedInfo.getProperty("CustomerInfoId").getPrimitiveValue().toCastValue(Integer.class)); + } + + @Test + @SuppressWarnings("unchecked") public void batchRequest() throws EdmPrimitiveTypeException { // create your request final ODataBatchRequest request = client.getBatchRequestFactory().getBatchRequest(testStaticServiceRootURL); @@ -284,9 +357,9 @@ public class BatchTestITCase extends AbstractTestITCase { client.getCUDRequestFactory().getEntityCreateRequest(targetURI.build(), original); createReq.setFormat(ODataPubFormat.ATOM); changeset.addRequest(createReq); - + // Delete customer created above - targetURI = + targetURI = client.getURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("Customer").appendKeySegment(1000); final ODataDeleteRequest deleteReq = client.getCUDRequestFactory().getDeleteRequest(targetURI.build()); changeset.addRequest(deleteReq); @@ -350,7 +423,7 @@ public class BatchTestITCase extends AbstractTestITCase { res = chgitem.next(); assertTrue(res instanceof ODataDeleteResponse); assertEquals(204, res.getStatusCode()); - + // retrive the third item (ODataRetrieve) item = iter.next(); assertTrue(item instanceof ODataRetrieveResponseItem);
