Skip to content

Commit ef19f7b

Browse files
author
Mark
committed
changed return value of getVertex/getEdge to null if not exists
1 parent dd13fec commit ef19f7b

File tree

4 files changed

+70
-53
lines changed

4 files changed

+70
-53
lines changed

src/main/java/com/arangodb/ArangoEdgeCollection.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020

2121
package com.arangodb;
2222

23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
2326
import com.arangodb.entity.EdgeEntity;
2427
import com.arangodb.entity.EdgeUpdateEntity;
25-
import com.arangodb.internal.InternalArangoEdgeCollection;
2628
import com.arangodb.internal.ArangoExecutorSync;
29+
import com.arangodb.internal.InternalArangoEdgeCollection;
2730
import com.arangodb.internal.velocystream.ConnectionSync;
2831
import com.arangodb.model.DocumentReadOptions;
2932
import com.arangodb.model.EdgeCreateOptions;
@@ -38,6 +41,8 @@
3841
*/
3942
public class ArangoEdgeCollection extends InternalArangoEdgeCollection<ArangoExecutorSync, Response, ConnectionSync> {
4043

44+
private static final Logger LOGGER = LoggerFactory.getLogger(ArangoEdgeCollection.class);
45+
4146
protected ArangoEdgeCollection(final ArangoGraph graph, final String name) {
4247
super(graph.executor(), graph.db(), graph.name(), name);
4348
}
@@ -83,7 +88,14 @@ public <T> EdgeEntity insertEdge(final T value, final EdgeCreateOptions options)
8388
* @throws ArangoDBException
8489
*/
8590
public <T> T getEdge(final String key, final Class<T> type) throws ArangoDBException {
86-
return executor.execute(getEdgeRequest(key, new DocumentReadOptions()), getEdgeResponseDeserializer(type));
91+
try {
92+
return executor.execute(getEdgeRequest(key, new DocumentReadOptions()), getEdgeResponseDeserializer(type));
93+
} catch (final ArangoDBException e) {
94+
if (LOGGER.isDebugEnabled()) {
95+
LOGGER.debug(e.getMessage(), e);
96+
}
97+
return null;
98+
}
8799
}
88100

89101
/**
@@ -101,7 +113,14 @@ public <T> T getEdge(final String key, final Class<T> type) throws ArangoDBExcep
101113
*/
102114
public <T> T getEdge(final String key, final Class<T> type, final DocumentReadOptions options)
103115
throws ArangoDBException {
104-
return executor.execute(getEdgeRequest(key, options), getEdgeResponseDeserializer(type));
116+
try {
117+
return executor.execute(getEdgeRequest(key, options), getEdgeResponseDeserializer(type));
118+
} catch (final ArangoDBException e) {
119+
if (LOGGER.isDebugEnabled()) {
120+
LOGGER.debug(e.getMessage(), e);
121+
}
122+
return null;
123+
}
105124
}
106125

107126
/**

src/main/java/com/arangodb/ArangoVertexCollection.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
package com.arangodb;
2222

23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
2326
import com.arangodb.entity.VertexEntity;
2427
import com.arangodb.entity.VertexUpdateEntity;
2528
import com.arangodb.internal.ArangoExecutorSync;
@@ -39,6 +42,8 @@
3942
public class ArangoVertexCollection
4043
extends InternalArangoVertexCollection<ArangoExecutorSync, Response, ConnectionSync> {
4144

45+
private static final Logger LOGGER = LoggerFactory.getLogger(ArangoVertexCollection.class);
46+
4247
protected ArangoVertexCollection(final ArangoGraph graph, final String name) {
4348
super(graph.executor(), graph.db(), graph.name(), name);
4449
}
@@ -96,7 +101,15 @@ public <T> VertexEntity insertVertex(final T value, final VertexCreateOptions op
96101
* @throws ArangoDBException
97102
*/
98103
public <T> T getVertex(final String key, final Class<T> type) throws ArangoDBException {
99-
return executor.execute(getVertexRequest(key, new DocumentReadOptions()), getVertexResponseDeserializer(type));
104+
try {
105+
return executor.execute(getVertexRequest(key, new DocumentReadOptions()),
106+
getVertexResponseDeserializer(type));
107+
} catch (final ArangoDBException e) {
108+
if (LOGGER.isDebugEnabled()) {
109+
LOGGER.debug(e.getMessage(), e);
110+
}
111+
return null;
112+
}
100113
}
101114

102115
/**
@@ -114,7 +127,14 @@ public <T> T getVertex(final String key, final Class<T> type) throws ArangoDBExc
114127
*/
115128
public <T> T getVertex(final String key, final Class<T> type, final DocumentReadOptions options)
116129
throws ArangoDBException {
117-
return executor.execute(getVertexRequest(key, options), getVertexResponseDeserializer(type));
130+
try {
131+
return executor.execute(getVertexRequest(key, options), getVertexResponseDeserializer(type));
132+
} catch (final ArangoDBException e) {
133+
if (LOGGER.isDebugEnabled()) {
134+
LOGGER.debug(e.getMessage(), e);
135+
}
136+
return null;
137+
}
118138
}
119139

120140
/**

src/test/java/com/arangodb/ArangoEdgeCollectionTest.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.hamcrest.Matchers.is;
2525
import static org.hamcrest.Matchers.not;
2626
import static org.hamcrest.Matchers.notNullValue;
27+
import static org.hamcrest.Matchers.nullValue;
2728
import static org.junit.Assert.assertThat;
2829
import static org.junit.Assert.fail;
2930

@@ -137,12 +138,9 @@ public void getEdgeIfMatchFail() {
137138
final BaseEdgeDocument value = createEdgeValue();
138139
final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null);
139140
final DocumentReadOptions options = new DocumentReadOptions().ifMatch("no");
140-
try {
141-
db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(edge.getKey(), BaseEdgeDocument.class,
142-
options);
143-
fail();
144-
} catch (final ArangoDBException e) {
145-
}
141+
final BaseEdgeDocument edge2 = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(edge.getKey(),
142+
BaseEdgeDocument.class, options);
143+
assertThat(edge2, is(nullValue()));
146144
}
147145

148146
@Test
@@ -161,12 +159,9 @@ public void getEdgeIfNoneMatchFail() {
161159
final BaseEdgeDocument value = createEdgeValue();
162160
final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null);
163161
final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch(edge.getRev());
164-
try {
165-
db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(edge.getKey(), BaseEdgeDocument.class,
166-
options);
167-
fail();
168-
} catch (final ArangoDBException e) {
169-
}
162+
final BaseEdgeDocument edge2 = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(edge.getKey(),
163+
BaseEdgeDocument.class, options);
164+
assertThat(edge2, is(nullValue()));
170165
}
171166

172167
@Test
@@ -351,12 +346,9 @@ public void deleteEdge() {
351346
final BaseEdgeDocument doc = createEdgeValue();
352347
final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null);
353348
db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).deleteEdge(createResult.getKey(), null);
354-
try {
355-
db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(createResult.getKey(),
356-
BaseEdgeDocument.class, null);
357-
fail();
358-
} catch (final ArangoDBException e) {
359-
}
349+
final BaseEdgeDocument edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME)
350+
.getEdge(createResult.getKey(), BaseEdgeDocument.class, null);
351+
assertThat(edge, is(nullValue()));
360352
}
361353

362354
@Test
@@ -365,12 +357,9 @@ public void deleteEdgeIfMatch() {
365357
final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null);
366358
final EdgeDeleteOptions options = new EdgeDeleteOptions().ifMatch(createResult.getRev());
367359
db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).deleteEdge(createResult.getKey(), options);
368-
try {
369-
db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).getEdge(createResult.getKey(),
370-
BaseEdgeDocument.class, null);
371-
fail();
372-
} catch (final ArangoDBException e) {
373-
}
360+
final BaseEdgeDocument edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME)
361+
.getEdge(createResult.getKey(), BaseEdgeDocument.class, null);
362+
assertThat(edge, is(nullValue()));
374363
}
375364

376365
@Test

src/test/java/com/arangodb/ArangoVertexCollectionTest.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.hamcrest.Matchers.is;
2525
import static org.hamcrest.Matchers.not;
2626
import static org.hamcrest.Matchers.notNullValue;
27+
import static org.hamcrest.Matchers.nullValue;
2728
import static org.junit.Assert.assertThat;
2829
import static org.junit.Assert.fail;
2930

@@ -114,12 +115,9 @@ public void getVertexIfMatchFail() {
114115
final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
115116
.insertVertex(new BaseDocument(), null);
116117
final DocumentReadOptions options = new DocumentReadOptions().ifMatch("no");
117-
try {
118-
db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(vertex.getKey(), BaseDocument.class,
119-
options);
120-
fail();
121-
} catch (final ArangoDBException e) {
122-
}
118+
final BaseDocument vertex2 = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(vertex.getKey(),
119+
BaseDocument.class, options);
120+
assertThat(vertex2, is(nullValue()));
123121
}
124122

125123
@Test
@@ -138,12 +136,9 @@ public void getVertexIfNoneMatchFail() {
138136
final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
139137
.insertVertex(new BaseDocument(), null);
140138
final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch(vertex.getRev());
141-
try {
142-
db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(vertex.getKey(), BaseDocument.class,
143-
options);
144-
fail();
145-
} catch (final ArangoDBException e) {
146-
}
139+
final BaseDocument vertex2 = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(vertex.getKey(),
140+
BaseDocument.class, options);
141+
assertThat(vertex2, is(nullValue()));
147142
}
148143

149144
@Test
@@ -337,12 +332,9 @@ public void deleteVertex() {
337332
final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc,
338333
null);
339334
db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).deleteVertex(createResult.getKey(), null);
340-
try {
341-
db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(createResult.getKey(), BaseDocument.class,
342-
null);
343-
fail();
344-
} catch (final ArangoDBException e) {
345-
}
335+
final BaseDocument vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
336+
.getVertex(createResult.getKey(), BaseDocument.class, null);
337+
assertThat(vertex, is(nullValue()));
346338
}
347339

348340
@Test
@@ -352,12 +344,9 @@ public void deleteVertexIfMatch() {
352344
null);
353345
final VertexDeleteOptions options = new VertexDeleteOptions().ifMatch(createResult.getRev());
354346
db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).deleteVertex(createResult.getKey(), options);
355-
try {
356-
db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).getVertex(createResult.getKey(), BaseDocument.class,
357-
null);
358-
fail();
359-
} catch (final ArangoDBException e) {
360-
}
347+
final BaseDocument vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME)
348+
.getVertex(createResult.getKey(), BaseDocument.class, null);
349+
assertThat(vertex, is(nullValue()));
361350
}
362351

363352
@Test

0 commit comments

Comments
 (0)