[OLINGO-260] async tests

This commit is contained in:
Francesco Chicchiriccò 2014-05-14 16:34:19 +02:00
parent 6e9fb661c0
commit 569996aadd
4 changed files with 311 additions and 25 deletions

View File

@ -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<V> implements Future<V> {
private final Future<V> future;
public AsyncCall(final CommonConfiguration configuration) {
this.future = configuration.getExecutor().submit(new Callable<V>() {
@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);
}
}

View File

@ -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<ProductCollection> futureProds =
new AsyncCall<ProductCollection>(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<Void> futureFlush = new AsyncCall<Void>(containerFactory.getConfiguration()) {
@Override
public Void call() {
container.flush();
return null;
}
};
assertNotNull(futureFlush);
while (!futureFlush.isDone()) {
Thread.sleep(1000L);
}
final Future<Product> futureProd = new AsyncCall<Product>(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<Query<Employee, EmployeeCollection>> queryEmployee =
new AsyncCall<Query<Employee, EmployeeCollection>>(containerFactory.getConfiguration()) {
@Override
public Query<Employee, EmployeeCollection> call() {
return container.getPerson().createQuery(EmployeeCollection.class);
}
};
assertFalse(queryEmployee.get().getResult().isEmpty());
final Future<Query<SpecialEmployee, SpecialEmployeeCollection>> querySpecialEmployee =
new AsyncCall<Query<SpecialEmployee, SpecialEmployeeCollection>>(containerFactory.getConfiguration()) {
@Override
public Query<SpecialEmployee, SpecialEmployeeCollection> 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());
}
}

View File

@ -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<CustomerCollection> futureCustomers =
new AsyncCall<CustomerCollection>(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<Void> futureFlush = new AsyncCall<Void>(containerFactory.getConfiguration()) {
@Override
public Void call() {
container.flush();
return null;
}
};
assertNotNull(futureFlush);
while (!futureFlush.isDone()) {
Thread.sleep(1000L);
}
final Future<Person> futureProd = new AsyncCall<Person>(containerFactory.getConfiguration()) {
@Override
public Person call() {
return container.getPeople().get(1);
}
};
assertEquals(randomFirstName, person.getFirstName());
}
}

View File

@ -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<ODataRetrieveResponse<ODataEntitySet>> 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);
}
}