Skip to content

Commit

Permalink
Bugfix/catch exceptions (#24)
Browse files Browse the repository at this point in the history
* removed empty catch blocks in tests

* bugfix collection tests

* bugfix ArangoDatabaseTest

* bugfix ArangoEdgeCollectionTest

* bugfix ArangoVertexCollectionTest
  • Loading branch information
rashtao authored Aug 28, 2019
1 parent 4b07572 commit fa75f4e
Show file tree
Hide file tree
Showing 11 changed files with 1,612 additions and 1,617 deletions.
9 changes: 3 additions & 6 deletions src/main/java/com/arangodb/internal/ArangoExecutorAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.concurrent.*;

/**
* @author Mark Vollmary
Expand Down Expand Up @@ -64,8 +61,8 @@ public <T> CompletableFuture<T> execute(
final ResponseDeserializer<T> responseDeserializer,
final HostHandle hostHandle) {

return CompletableFuture.supplyAsync(() -> communication.execute(request, hostHandle), outgoingExecutor)
.thenCompose(Function.identity())
return CompletableFuture.completedFuture(null)
.thenComposeAsync((it) -> communication.execute(request, hostHandle), outgoingExecutor)
.thenApplyAsync(responseDeserializer::deserialize);
}

Expand Down
37 changes: 22 additions & 15 deletions src/test/java/com/arangodb/ArangoCollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;

import static org.hamcrest.CoreMatchers.notNullValue;
Expand Down Expand Up @@ -331,7 +332,8 @@ public void updateDocumentIfMatchFail() throws InterruptedException, ExecutionEx
final DocumentUpdateOptions options = new DocumentUpdateOptions().ifMatch("no");
db.collection(COLLECTION_NAME).updateDocument(createResult.getKey(), doc, options).get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
}

Expand Down Expand Up @@ -508,7 +510,8 @@ public void updateDocumentIgnoreRevsFalse() throws InterruptedException, Executi
final DocumentUpdateOptions options = new DocumentUpdateOptions().ignoreRevs(false);
db.collection(COLLECTION_NAME).updateDocument(createResult.getKey(), doc, options).get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
}

Expand Down Expand Up @@ -581,7 +584,8 @@ public void replaceDocumentIfMatchFail() throws InterruptedException, ExecutionE
final DocumentReplaceOptions options = new DocumentReplaceOptions().ifMatch("no");
db.collection(COLLECTION_NAME).replaceDocument(createResult.getKey(), doc, options).get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
}

Expand All @@ -598,7 +602,8 @@ public void replaceDocumentIgnoreRevsFalse() throws InterruptedException, Execut
final DocumentReplaceOptions options = new DocumentReplaceOptions().ignoreRevs(false);
db.collection(COLLECTION_NAME).replaceDocument(createResult.getKey(), doc, options).get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
}

Expand Down Expand Up @@ -703,7 +708,8 @@ public void deleteDocumentIfMatchFail() throws InterruptedException, ExecutionEx
try {
db.collection(COLLECTION_NAME).deleteDocument(createResult.getKey(), null, options).get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
}

Expand Down Expand Up @@ -843,9 +849,7 @@ public void createGeo2Index() throws ExecutionException, InterruptedException {
} else {
assertThat(indexResult.getType(), is(IndexType.geo2));
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}).get();
Expand Down Expand Up @@ -1250,16 +1254,17 @@ public void importDocumentsDuplicateUpdate() throws InterruptedException, Execut
}

@Test
public void importDocumentsCompleteFail() {
public void importDocumentsCompleteFail() throws InterruptedException {
final Collection<BaseDocument> values = new ArrayList<>();
values.add(new BaseDocument("1"));
values.add(new BaseDocument("2"));
values.add(new BaseDocument("2"));
try {
db.collection(COLLECTION_NAME).importDocuments(values, new DocumentImportOptions().complete(true)).get();
fail();
} catch (InterruptedException | ExecutionException e) {
assertThat(e.getMessage(), containsString("1210"));
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
assertThat(((ArangoDBException) e.getCause()).getErrorNum(), is(1210));
}
}

Expand Down Expand Up @@ -1434,13 +1439,14 @@ public void importDocumentsJsonDuplicateUpdate() throws InterruptedException, Ex
}

@Test
public void importDocumentsJsonCompleteFail() {
public void importDocumentsJsonCompleteFail() throws InterruptedException {
final String values = "[{\"_key\":\"1\"},{\"_key\":\"2\"},{\"_key\":\"2\"}]";
try {
db.collection(COLLECTION_NAME).importDocuments(values, new DocumentImportOptions().complete(true)).get();
fail();
} catch (InterruptedException | ExecutionException e) {
assertThat(e.getMessage(), containsString("1210"));
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
assertThat(((ArangoDBException) e.getCause()).getErrorNum(), is(1210));
}
}

Expand Down Expand Up @@ -1887,7 +1893,8 @@ public void rename() throws InterruptedException, ExecutionException {
try {
db.collection(COLLECTION_NAME).getInfo().get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
} finally {
db.collection(COLLECTION_NAME + "1").rename(COLLECTION_NAME).get();
Expand Down
27 changes: 16 additions & 11 deletions src/test/java/com/arangodb/ArangoDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -181,7 +180,8 @@ public void deleteCollection() throws InterruptedException, ExecutionException {
try {
db.collection(COLLECTION_NAME).getInfo().get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
}

Expand All @@ -196,7 +196,8 @@ public void deleteSystemCollection() throws InterruptedException, ExecutionExcep
try {
db.collection(name).getInfo().get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
}

Expand All @@ -210,13 +211,15 @@ public void deleteSystemCollectionFail() throws InterruptedException, ExecutionE
try {
db.collection(name).drop().get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
db.collection(name).drop(true).get();
try {
db.collection(name).getInfo().get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
}

Expand Down Expand Up @@ -563,9 +566,10 @@ public void queryWithTTL() throws InterruptedException, ExecutionException {
Thread.sleep(wait * 1000);
}
}
fail("this should fail");
fail();
} catch (final ArangoDBException ex) {
assertThat(ex.getMessage(), is("Response: 404, Error: 1600 - cursor not found"));
assertThat(ex.getResponseCode(), is(404));
assertThat(ex.getErrorNum(), is(1600));
} finally {
db.collection(COLLECTION_NAME).drop().get();
}
Expand Down Expand Up @@ -934,7 +938,8 @@ public void transactionallowImplicit() throws InterruptedException, ExecutionExc
options.allowImplicit(false);
db.transaction(action, VPackSlice.class, options).get();
fail();
} catch (final Exception e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
}
} finally {
db.collection("someCollection").drop().get();
Expand Down Expand Up @@ -1046,13 +1051,13 @@ public void getDocument() throws InterruptedException, ExecutionException {
}

@Test
public void shouldIncludeExceptionMessage() {
public void shouldIncludeExceptionMessage() throws InterruptedException {
final String exceptionMessage = "My error context";
final String action = "function (params) {" + "throw '" + exceptionMessage + "';" + "}";
try {
db.transaction(action, VPackSlice.class, null).join();
db.transaction(action, VPackSlice.class, null).get();
fail();
} catch (final CompletionException e) {
} catch (final ExecutionException e) {
assertThat(e.getCause(), instanceOf(ArangoDBException.class));
ArangoDBException cause = ((ArangoDBException) e.getCause());
assertThat(cause.getErrorMessage(), is(exceptionMessage));
Expand Down
Loading

0 comments on commit fa75f4e

Please sign in to comment.