From 569996aaddcce9fd698ff68c06e6e1dbc52b36e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesco=20Chicchiricc=C3=B2?= <--global> Date: Wed, 14 May 2014 16:34:19 +0200 Subject: [PATCH] [OLINGO-260] async tests --- .../olingo/ext/proxy/api/AsyncCall.java | 70 ++++++++++ .../olingo/fit/proxy/v3/AsyncTestITCase.java | 126 ++++++++++++++++++ .../olingo/fit/proxy/v4/AsyncTestITCase.java | 92 +++++++++++++ .../apache/olingo/fit/v4/AsyncTestITCase.java | 48 ++++--- 4 files changed, 311 insertions(+), 25 deletions(-) create mode 100644 ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/api/AsyncCall.java create mode 100644 fit/src/test/java/org/apache/olingo/fit/proxy/v3/AsyncTestITCase.java create mode 100644 fit/src/test/java/org/apache/olingo/fit/proxy/v4/AsyncTestITCase.java diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/api/AsyncCall.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/api/AsyncCall.java new file mode 100644 index 000000000..e3eea9e5e --- /dev/null +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/api/AsyncCall.java @@ -0,0 +1,70 @@ +/* + * 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.ext.proxy.api; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.olingo.client.api.CommonConfiguration; + +public abstract class AsyncCall implements Future { + + private final Future future; + + public AsyncCall(final CommonConfiguration configuration) { + this.future = configuration.getExecutor().submit(new Callable() { + + @Override + public V call() throws Exception { + return AsyncCall.this.call(); + } + }); + } + + public abstract V call(); + + @Override + public boolean cancel(final boolean mayInterruptIfRunning) { + return this.future.cancel(mayInterruptIfRunning); + } + + @Override + public boolean isCancelled() { + return this.future.isCancelled(); + } + + @Override + public boolean isDone() { + return this.future.isDone(); + } + + @Override + public V get() throws InterruptedException, ExecutionException { + return this.future.get(); + } + + @Override + public V get(final long timeout, final TimeUnit unit) + throws InterruptedException, ExecutionException, TimeoutException { + + return this.future.get(timeout, unit); + } +} diff --git a/fit/src/test/java/org/apache/olingo/fit/proxy/v3/AsyncTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/proxy/v3/AsyncTestITCase.java new file mode 100644 index 000000000..3cd51bcd1 --- /dev/null +++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v3/AsyncTestITCase.java @@ -0,0 +1,126 @@ +/* + * 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.fit.proxy.v3; + +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 java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import org.apache.olingo.ext.proxy.api.AsyncCall; +import org.apache.olingo.ext.proxy.api.Query; +import org.apache.olingo.fit.proxy.v3.staticservice.microsoft.test.odata.services.astoriadefaultservice.types.Employee; +import org.apache.olingo.fit.proxy.v3.staticservice.microsoft.test.odata.services.astoriadefaultservice.types. + EmployeeCollection; +import org.apache.olingo.fit.proxy.v3.staticservice.microsoft.test.odata.services.astoriadefaultservice.types.Product; +import org.apache.olingo.fit.proxy.v3.staticservice.microsoft.test.odata.services.astoriadefaultservice.types. + ProductCollection; +import org.apache.olingo.fit.proxy.v3.staticservice.microsoft.test.odata.services.astoriadefaultservice.types. + SpecialEmployee; +import org.apache.olingo.fit.proxy.v3.staticservice.microsoft.test.odata.services.astoriadefaultservice.types. + SpecialEmployeeCollection; +import org.junit.Test; + +public class AsyncTestITCase extends AbstractTestITCase { + + @Test + public void retrieveEntitySet() throws InterruptedException, ExecutionException { + final Future futureProds = + new AsyncCall(containerFactory.getConfiguration()) { + + @Override + public ProductCollection call() { + return container.getProduct().getAll(); + } + }; + assertNotNull(futureProds); + + while (!futureProds.isDone()) { + Thread.sleep(1000L); + } + + final ProductCollection products = futureProds.get(); + assertNotNull(products); + assertFalse(products.isEmpty()); + for (Product product : products) { + assertNotNull(product); + } + } + + @Test + public void updateEntity() throws InterruptedException, ExecutionException { + final String random = UUID.randomUUID().toString(); + + final Product product = container.getProduct().get(-10); + product.setDescription("AsyncTest#updateEntity " + random); + + final Future futureFlush = new AsyncCall(containerFactory.getConfiguration()) { + + @Override + public Void call() { + container.flush(); + return null; + } + }; + assertNotNull(futureFlush); + + while (!futureFlush.isDone()) { + Thread.sleep(1000L); + } + + final Future futureProd = new AsyncCall(containerFactory.getConfiguration()) { + + @Override + public Product call() { + return container.getProduct().get(-10); + } + }; + + assertEquals("AsyncTest#updateEntity " + random, futureProd.get().getDescription()); + } + + @Test + public void polymorphQuery() throws Exception { + final Future> queryEmployee = + new AsyncCall>(containerFactory.getConfiguration()) { + + @Override + public Query call() { + return container.getPerson().createQuery(EmployeeCollection.class); + } + }; + assertFalse(queryEmployee.get().getResult().isEmpty()); + + final Future> querySpecialEmployee = + new AsyncCall>(containerFactory.getConfiguration()) { + + @Override + public Query call() { + return container.getPerson().createQuery(SpecialEmployeeCollection.class); + } + }; + assertFalse(querySpecialEmployee.get().getResult().isEmpty()); + + assertTrue(container.getPerson().getAll().size() + > queryEmployee.get().getResult().size() + querySpecialEmployee.get().getResult().size()); + } +} diff --git a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/AsyncTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/AsyncTestITCase.java new file mode 100644 index 000000000..f5f45616f --- /dev/null +++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/AsyncTestITCase.java @@ -0,0 +1,92 @@ +/* + * 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.fit.proxy.v4; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.olingo.ext.proxy.api.AsyncCall; +import org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.Customer; +import org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types. + CustomerCollection; +import org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.Person; +import org.junit.Test; + +public class AsyncTestITCase extends AbstractTestITCase { + + @Test + public void retrieveEntitySet() throws InterruptedException, ExecutionException { + final Future futureCustomers = + new AsyncCall(containerFactory.getConfiguration()) { + + @Override + public CustomerCollection call() { + return container.getCustomers().getAll(); + } + }; + assertNotNull(futureCustomers); + + while (!futureCustomers.isDone()) { + Thread.sleep(1000L); + } + + final CustomerCollection customers = futureCustomers.get(); + assertNotNull(customers); + assertFalse(customers.isEmpty()); + for (Customer customer : customers) { + assertNotNull(customer); + } + } + + @Test + public void updateEntity() throws InterruptedException { + final String randomFirstName = RandomStringUtils.random(10); + + Person person = container.getPeople().get(1); + person.setFirstName(randomFirstName); + + final Future futureFlush = new AsyncCall(containerFactory.getConfiguration()) { + + @Override + public Void call() { + container.flush(); + return null; + } + }; + assertNotNull(futureFlush); + + while (!futureFlush.isDone()) { + Thread.sleep(1000L); + } + + final Future futureProd = new AsyncCall(containerFactory.getConfiguration()) { + + @Override + public Person call() { + return container.getPeople().get(1); + } + }; + + assertEquals(randomFirstName, person.getFirstName()); + } +} diff --git a/fit/src/test/java/org/apache/olingo/fit/v4/AsyncTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/v4/AsyncTestITCase.java index 373a489a8..4960a48b7 100644 --- a/fit/src/test/java/org/apache/olingo/fit/v4/AsyncTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/v4/AsyncTestITCase.java @@ -18,9 +18,13 @@ */ package org.apache.olingo.fit.v4; +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 java.net.URI; import java.util.List; - import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest; @@ -28,9 +32,7 @@ import org.apache.olingo.client.api.communication.request.retrieve.ODataEntitySe import org.apache.olingo.client.api.communication.request.v4.AsyncRequestWrapper; import org.apache.olingo.client.api.communication.response.ODataRetrieveResponse; import org.apache.olingo.client.api.communication.response.v4.AsyncResponseWrapper; -import org.apache.olingo.client.api.uri.CommonURIBuilder; import org.apache.olingo.client.api.uri.v4.URIBuilder; -import static org.apache.olingo.fit.v4.AbstractTestITCase.client; import org.apache.olingo.commons.api.domain.CommonODataEntity; import org.apache.olingo.commons.api.domain.CommonODataProperty; import org.apache.olingo.commons.api.domain.ODataInlineEntity; @@ -38,17 +40,13 @@ import org.apache.olingo.commons.api.domain.ODataLink; import org.apache.olingo.commons.api.domain.v4.ODataEntity; import org.apache.olingo.commons.api.domain.v4.ODataEntitySet; import org.apache.olingo.commons.api.format.ODataPubFormat; -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.junit.Test; public class AsyncTestITCase extends AbstractTestITCase { @Test - public void asyncRequestV3Style() throws InterruptedException, ExecutionException { - final CommonURIBuilder uriBuilder = client.getURIBuilder(testStaticServiceRootURL). + public void clientAsync() throws InterruptedException, ExecutionException { + final URIBuilder uriBuilder = client.getURIBuilder(testStaticServiceRootURL). appendEntitySetSegment("Customers"); final Future> futureRes = client.getRetrieveRequestFactory().getEntitySetRequest(uriBuilder.build()).asyncExecute(); @@ -120,7 +118,18 @@ public class AsyncTestITCase extends AbstractTestITCase { assertTrue(found); } - private void asynOrders(final ODataPubFormat format) { + @Test + public void withInlineEntryAsAtom() { + withInlineEntry(ODataPubFormat.ATOM); + } + + @Test + public void withInlineEntryAsJSON() { + // this needs to be full, otherwise there is no mean to recognize links + withInlineEntry(ODataPubFormat.JSON_FULL_METADATA); + } + + private void asyncOrders(final ODataPubFormat format) { final URIBuilder uriBuilder = client.getURIBuilder(testStaticServiceRootURL). appendEntitySetSegment("async").appendEntitySetSegment("Orders"); @@ -144,23 +153,12 @@ public class AsyncTestITCase extends AbstractTestITCase { } @Test - public void withInlineEntryAsAtom() { - withInlineEntry(ODataPubFormat.ATOM); + public void asyncOrdersAsAtom() { + asyncOrders(ODataPubFormat.ATOM); } @Test - public void withInlineEntryAsJSON() { - // this needs to be full, otherwise there is no mean to recognize links - withInlineEntry(ODataPubFormat.JSON_FULL_METADATA); - } - - @Test - public void asynOrdersAsAtom() { - asynOrders(ODataPubFormat.ATOM); - } - - @Test - public void asynOrdersAsJSON() { - asynOrders(ODataPubFormat.JSON); + public void asyncOrdersAsJSON() { + asyncOrders(ODataPubFormat.JSON); } }