diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AsyncSupportITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AsyncSupportITCase.java index 2263e9b47..8694605d4 100644 --- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AsyncSupportITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/AsyncSupportITCase.java @@ -18,10 +18,17 @@ */ package org.apache.olingo.fit.tecsvc.client; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.net.URI; +import java.util.Calendar; +import java.util.Collections; +import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -30,32 +37,34 @@ import java.util.concurrent.TimeoutException; import org.apache.olingo.client.api.ODataClient; import org.apache.olingo.client.api.communication.ODataClientErrorException; import org.apache.olingo.client.api.communication.request.AsyncBatchRequestWrapper; +import org.apache.olingo.client.api.communication.request.AsyncRequestWrapper; import org.apache.olingo.client.api.communication.request.ODataBatchableRequest; import org.apache.olingo.client.api.communication.request.ODataRequest; import org.apache.olingo.client.api.communication.request.batch.ODataBatchRequest; import org.apache.olingo.client.api.communication.request.batch.ODataBatchResponseItem; import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest; +import org.apache.olingo.client.api.communication.request.invoke.ODataInvokeRequest; import org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest; import org.apache.olingo.client.api.communication.response.AsyncResponseWrapper; import org.apache.olingo.client.api.communication.response.ODataBatchResponse; import org.apache.olingo.client.api.communication.response.ODataEntityCreateResponse; +import org.apache.olingo.client.api.communication.response.ODataInvokeResponse; import org.apache.olingo.client.api.communication.response.ODataResponse; import org.apache.olingo.client.api.communication.response.ODataRetrieveResponse; import org.apache.olingo.client.api.data.ResWrap; import org.apache.olingo.client.api.domain.ClientEntity; import org.apache.olingo.client.api.domain.ClientEntitySet; import org.apache.olingo.client.api.domain.ClientProperty; +import org.apache.olingo.client.api.domain.ClientValue; import org.apache.olingo.client.api.uri.URIBuilder; import org.apache.olingo.commons.api.data.Entity; import org.apache.olingo.commons.api.data.EntityCollection; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.format.PreferenceName; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpStatusCode; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import org.apache.olingo.fit.tecsvc.TecSvcConst; import org.junit.Test; public final class AsyncSupportITCase extends AbstractParamTecSvcITCase { @@ -242,6 +251,33 @@ public final class AsyncSupportITCase extends AbstractParamTecSvcITCase { assertEquals("First Resource - positive values", entity.getProperty("PropertyString").getPrimitiveValue().toValue()); } + + @Test + public void entityAction() throws Exception { + Calendar dateTime = Calendar.getInstance(); + dateTime.clear(); + dateTime.set(1012, 2, 0, 0, 0, 0); + final Map parameters = Collections.singletonMap( + "ParameterDate", + (ClientValue) getFactory().newPrimitiveValueBuilder() + .setType(EdmPrimitiveTypeKind.Date).setValue(dateTime).build()); + ODataClient client = getClient(); + URI uri = client.newURIBuilder(TecSvcConst.BASE_URI) + .appendActionCallSegment("AIRTESAllPrimParam").build(); + + ODataInvokeRequest req = client.getInvokeRequestFactory() + .getActionInvokeRequest(uri, ClientEntity.class, parameters); + AsyncRequestWrapper> + asyncReqWrp = client.getAsyncRequestFactory().getAsyncRequestWrapper(req); + AsyncResponseWrapper> + asyncRespWrp = asyncReqWrp.execute(); + waitTillDone(asyncRespWrp, 5); + @SuppressWarnings("unchecked") + ODataInvokeResponse response = (ODataInvokeResponse)asyncRespWrp.getODataResponse(); + + assertEquals(HttpStatusCode.CREATED.getStatusCode(), response.getStatusCode()); + assertEquals(TecSvcConst.BASE_URI +"/ESAllPrim(1)", response.getHeader(HttpHeader.LOCATION).iterator().next()); + } /** * Test delete with async prefer header but without async support from TecSvc. diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java index 8bd33637a..5cf938540 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java @@ -55,7 +55,7 @@ public abstract class AbstractODataInvokeRequest extends AbstractODataBasicRequest> implements ODataInvokeRequest, ODataBatchableRequest { - private final Class reference; + protected final Class reference; /** * Function parameters. diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/ODataInvokeRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/ODataInvokeRequestImpl.java index 6d6cc01e5..f6eaebf86 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/ODataInvokeRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/ODataInvokeRequestImpl.java @@ -18,10 +18,22 @@ */ package org.apache.olingo.client.core.communication.request.invoke; +import java.io.IOException; +import java.io.InputStream; import java.net.URI; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; import org.apache.olingo.client.api.ODataClient; +import org.apache.olingo.client.api.communication.request.invoke.ClientNoContent; +import org.apache.olingo.client.api.communication.response.ODataInvokeResponse; +import org.apache.olingo.client.api.domain.ClientEntity; +import org.apache.olingo.client.api.domain.ClientEntitySet; import org.apache.olingo.client.api.domain.ClientInvokeResult; +import org.apache.olingo.client.api.domain.ClientProperty; +import org.apache.olingo.client.api.http.HttpClientException; +import org.apache.olingo.client.api.serialization.ODataDeserializerException; +import org.apache.olingo.client.core.communication.response.AbstractODataResponse; import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.commons.api.http.HttpMethod; @@ -45,4 +57,51 @@ public class ODataInvokeRequestImpl extends Abstra protected ContentType getPOSTParameterFormat() { return contentType == null ? getDefaultFormat() : contentType; } + /** + * Response class about an ODataInvokeRequest. + */ + protected class ODataInvokeResponseImpl extends AbstractODataResponse implements ODataInvokeResponse { + + private T invokeResult = null; + + private ODataInvokeResponseImpl(final ODataClient odataClient, final HttpClient httpClient, + final HttpResponse res) { + + super(odataClient, httpClient, res); + } + + /** + * {@inheritDoc } + */ + @Override + public T getBody() { + if (invokeResult == null) { + try { + if (ClientNoContent.class.isAssignableFrom(reference)) { + invokeResult = reference.cast(new ClientNoContent()); + } else { + // avoid getContent() twice:IllegalStateException: Content has been consumed + final InputStream responseStream = this.payload == null ? res.getEntity().getContent() : this.payload; + if (ClientEntitySet.class.isAssignableFrom(reference)) { + invokeResult = reference.cast(odataClient.getReader().readEntitySet(responseStream, + ContentType.parse(getContentType()))); + } else if (ClientEntity.class.isAssignableFrom(reference)) { + invokeResult = reference.cast(odataClient.getReader().readEntity(responseStream, + ContentType.parse(getContentType()))); + } else if (ClientProperty.class.isAssignableFrom(reference)) { + invokeResult = reference.cast(odataClient.getReader().readProperty(responseStream, + ContentType.parse(getContentType()))); + } + } + } catch (IOException e) { + throw new HttpClientException(e); + } catch (final ODataDeserializerException e) { + throw new IllegalArgumentException(e); + } finally { + this.close(); + } + } + return invokeResult; + } + } }