Skip to content

Commit d12cd52

Browse files
committed
fixed issue#2: no baseParameterDeserialization for DocumentEntity
Conflicts: README.md
1 parent 0d6ae82 commit d12cd52

19 files changed

+296
-61
lines changed

README.md

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ To add the driver to your project with maven, add the following code to your pom
2020
<dependency>
2121
<groupId>com.arangodb</groupId>
2222
<artifactId>arangodb-java-driver</artifactId>
23-
<version>[2.4.1-SNAPSHOT,2.4.1]</version>
23+
<version>[2.4.2-SNAPSHOT,2.4.2]</version>
2424
</dependency>
2525
....
2626
</dependencies>
2727
```
2828

29-
If you want to test with a snapshot version (e.g. 2.4.1-SNAPSHOT), add the staging repository of oss.sonatype.org to your pom.xml:
29+
If you want to test with a snapshot version (e.g. 2.4.2-SNAPSHOT), add the staging repository of oss.sonatype.org to your pom.xml:
3030

3131
```XML
3232
<repositories>
@@ -349,24 +349,10 @@ Now an edge can be created to set a relation between vertices
349349
null);
350350
```
351351

352-
<<<<<<< HEAD
353-
# Known bugs
354-
Due to deserializition of some attribute combinations of name/type may lead to exceptions.
355-
These combinations are:
356-
357-
| attribute name | attribute type |
358-
|----------------|----------------|
359-
| error | boolean |
360-
| code | numeric |
361-
| errorNum | numeric |
362-
| errorMessage | String |
363-
| etag | numeric |
364-
=======
365352
# What's new in 2.4
366353

367354
## since 2.4.1
368355
httpclient version 4.3.6
369356

370357
## since 2.4.2
371358
Fixed issue#2
372-
>>>>>>> d20660a... some docu

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.arangodb</groupId>
66
<artifactId>arangodb-java-driver</artifactId>
7-
<version>2.4.1</version>
7+
<version>2.4.2</version>
88
<inceptionYear>2012</inceptionYear>
99
<packaging>jar</packaging>
1010

src/main/java/com/arangodb/ArangoException.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,16 @@ public boolean isUnauthorized() {
6767
return (entity != null && entity.isUnauthorized());
6868
}
6969

70-
public boolean isNotFound() {
71-
return (entity != null && entity.isNotFound());
70+
// public boolean isNotFound() {
71+
// return (entity != null && entity.isNotFound());
72+
// }
73+
74+
public void setCode(int code) {
75+
entity.setCode(code);
76+
}
77+
78+
public void setErrorNumber(int errorNumber) {
79+
entity.setErrorNumber(errorNumber);
7280
}
7381

7482
}

src/main/java/com/arangodb/BaseArangoDriver.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
import java.util.Map;
2222
import java.util.regex.Pattern;
2323

24+
import com.arangodb.entity.ArangoExceptionEntity;
25+
import com.arangodb.entity.BaseDocument;
2426
import com.arangodb.entity.BaseEntity;
27+
import com.arangodb.entity.DefaultEntity;
28+
import com.arangodb.entity.DocumentEntity;
2529
import com.arangodb.entity.EntityDeserializers;
2630
import com.arangodb.entity.EntityFactory;
2731
import com.arangodb.entity.KeyValueEntity;
@@ -32,6 +36,9 @@
3236
import com.arangodb.util.DateUtils;
3337
import com.arangodb.util.ReflectionUtils;
3438
import com.arangodb.util.StringUtils;
39+
import com.google.gson.JsonElement;
40+
import com.google.gson.JsonObject;
41+
import com.google.gson.JsonParser;
3542

3643
/**
3744
* @author tamtam180 - kirscheless at gmail.com
@@ -175,6 +182,77 @@ protected <T extends BaseEntity> T createEntity(
175182
if (res == null) {
176183
return null;
177184
}
185+
boolean isDocumentEntity = false;
186+
boolean requestSuccessful = true;
187+
// the following was added to ensure, that attributes with a key like "error", "code", "errorNum"
188+
// and "etag" will be serialized, when no error was thrown by the database
189+
if( clazz == DocumentEntity.class) {
190+
isDocumentEntity = true;
191+
}
192+
int statusCode = res.getStatusCode();
193+
if (statusCode >= 400) {
194+
requestSuccessful = false;
195+
DefaultEntity defaultEntity = new DefaultEntity();
196+
if (res.getText() != null && ! res.getText().equalsIgnoreCase("") && statusCode != 500) {
197+
JsonParser jsonParser = new JsonParser();
198+
JsonElement jsonElement = jsonParser.parse(res.getText());
199+
JsonObject jsonObject = jsonElement.getAsJsonObject();
200+
JsonElement errorMessage = jsonObject.get("errorMessage");
201+
defaultEntity.setErrorMessage(errorMessage.getAsString());
202+
JsonElement errorNumber = jsonObject.get("errorNum");
203+
defaultEntity.setErrorNumber(errorNumber.getAsInt());
204+
} else {
205+
String statusPhrase = "";
206+
switch (statusCode) {
207+
case 400:
208+
statusPhrase = "Bad Request";
209+
break;
210+
case 401:
211+
statusPhrase = "Unauthorized";
212+
break;
213+
case 403:
214+
statusPhrase = "Forbidden";
215+
break;
216+
case 404:
217+
statusPhrase = "Not Found";
218+
break;
219+
case 405:
220+
statusPhrase = "Method Not Allowed";
221+
break;
222+
case 406:
223+
statusPhrase = "Not Acceptable";
224+
break;
225+
case 407:
226+
statusPhrase = "Proxy Authentication Required";
227+
break;
228+
case 408:
229+
statusPhrase = "Request Time-out";
230+
break;
231+
case 409:
232+
statusPhrase = "Conflict";
233+
break;
234+
case 500:
235+
statusPhrase = "Internal Server Error";
236+
break;
237+
default:
238+
statusPhrase = "unknown error";
239+
break;
240+
}
241+
242+
defaultEntity.setErrorMessage(statusPhrase);
243+
if (statusCode == 500) {
244+
defaultEntity.setErrorMessage(statusPhrase + ": " + res.getText());
245+
}
246+
}
247+
248+
defaultEntity.setCode(statusCode);
249+
defaultEntity.setStatusCode(statusCode);
250+
defaultEntity.setError(true);
251+
ArangoException arangoException = new ArangoException(defaultEntity);
252+
arangoException.setCode(statusCode);
253+
throw arangoException;
254+
}
255+
178256
try {
179257
EntityDeserializers.setParameterized(pclazz);
180258

@@ -193,6 +271,14 @@ protected <T extends BaseEntity> T createEntity(
193271
if (validate) {
194272
validate(res, entity);
195273
}
274+
275+
if (isDocumentEntity && requestSuccessful) {
276+
entity.setCode(statusCode);
277+
entity.setErrorMessage(null);
278+
entity.setError(false);
279+
entity.setErrorNumber(0);
280+
}
281+
196282
return entity;
197283
} finally {
198284
EntityDeserializers.removeParameterized();

src/main/java/com/arangodb/entity/BaseEntity.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ public boolean isUnauthorized() {
8686
return statusCode == 401;
8787
}
8888

89-
/**
90-
* If the requested resource has not been modified it returns true
91-
*
92-
* @return boolean
93-
*/
94-
public boolean isNotFound() {
95-
return statusCode == 404;
96-
}
89+
// /**
90+
// * If the requested resource has not been modified it returns true
91+
// *
92+
// * @return boolean
93+
// */
94+
// public boolean isNotFound() {
95+
// return statusCode == 404;
96+
// }
9797

9898
/**
9999
* If this is the response of a batch request it returns true

src/main/java/com/arangodb/entity/EntityDeserializers.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.arangodb.entity;
1818

19+
import java.lang.reflect.Field;
20+
import java.lang.reflect.Modifier;
1921
import java.lang.reflect.Type;
2022
import java.math.BigDecimal;
2123
import java.util.*;
@@ -151,7 +153,7 @@ private static <T extends BaseEntity> T deserializeBaseParameter(JsonObject obj,
151153
if (obj.has("etag") && obj.getAsJsonPrimitive("errorNum").isNumber()) {
152154
entity.etag = obj.getAsJsonPrimitive("etag").getAsLong();
153155
}
154-
156+
155157
return entity;
156158
}
157159

@@ -585,7 +587,9 @@ public DocumentEntity<?> deserialize(JsonElement json, Type typeOfT, JsonDeseria
585587
}
586588

587589
JsonObject obj = json.getAsJsonObject();
588-
DocumentEntity<Object> entity = deserializeBaseParameter(obj, new DocumentEntity<Object>());
590+
// DocumentEntity<Object> entity = deserializeBaseParameter(obj, new DocumentEntity<Object>());
591+
// deserializeDocumentParameter(obj, entity);
592+
DocumentEntity<Object> entity = new DocumentEntity<Object>();
589593
deserializeDocumentParameter(obj, entity);
590594

591595
// 他のフィールドはリフレクションで。 (TODO: Annotationのサポートと上記パラメータを弾く)

src/main/java/com/arangodb/impl/InternalCursorDriverImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public CursorEntity<?> validateQuery(String database, String query) throws Arang
4646
createEndpointUrl(baseUrl, database, "/_api/query"),
4747
null,
4848
EntityFactory.toJsonString(new MapBuilder("query", query).get()));
49-
try {
49+
// try {
5050
CursorEntity<?> entity = createEntity(res, CursorEntity.class);
5151
return entity;
52-
} catch (ArangoException e) {
53-
return (CursorEntity<?>) e.getEntity();
54-
}
52+
// } catch (ArangoException e) {
53+
// return (CursorEntity<?>) e.getEntity();
54+
// }
5555

5656
}
5757

src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,16 @@ public long checkDocument(String database, String documentHandle) throws ArangoE
153153
}
154154

155155
@Override
156-
public <T> DocumentEntity<T> getDocument(String database, String documentHandle, Class<?> clazz, Long ifNoneMatchRevision, Long ifMatchRevision) throws ArangoException {
156+
public <T> DocumentEntity<T> getDocument(
157+
String database,
158+
String documentHandle,
159+
Class<?> clazz,
160+
Long ifNoneMatchRevision,
161+
Long ifMatchRevision) throws ArangoException {
162+
157163
validateDocumentHandle(documentHandle);
158-
HttpResponseEntity res = httpManager.doGet(
159-
createEndpointUrl(baseUrl, database, "/_api/document", documentHandle),
164+
HttpResponseEntity res = httpManager.doGet(
165+
createEndpointUrl(baseUrl, database, "/_api/document", documentHandle),
160166
new MapBuilder().put("If-None-Match", ifNoneMatchRevision, true).put("If-Match", ifMatchRevision).get(),
161167
null);
162168
DocumentEntity<T> entity = createEntity(res, DocumentEntity.class, clazz);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.arangodb;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.CoreMatchers.not;
5+
import static org.hamcrest.Matchers.nullValue;
6+
import static org.junit.Assert.assertThat;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import com.arangodb.entity.DocumentEntity;
13+
14+
/**
15+
*
16+
* @author gschwab
17+
*
18+
*/
19+
public class ArangoBaseParameterTest extends BaseTest {
20+
21+
public ArangoBaseParameterTest(ArangoConfigure configure, ArangoDriver driver) {
22+
super(configure, driver);
23+
}
24+
25+
final String collectionName = "unit_test_base_parameter";
26+
27+
@Before
28+
public void before() throws ArangoException {
29+
try {
30+
driver.deleteCollection(collectionName);
31+
} catch (ArangoException e) {
32+
33+
}
34+
try {
35+
driver.createCollection(collectionName);
36+
} catch (ArangoException e) {
37+
38+
}
39+
}
40+
41+
@After
42+
public void after() throws ArangoException {
43+
}
44+
45+
@Test
46+
public void test_base_parameters_1() {
47+
String errorMessage = "some message";
48+
TestBaseParameters testBaseParameters = new TestBaseParameters(true, 500, 4711, errorMessage, -1);
49+
50+
DocumentEntity<TestBaseParameters> documentEntity1 = null;
51+
DocumentEntity<TestBaseParameters> documentEntity2 = null;
52+
53+
try {
54+
documentEntity1 = driver.createDocument(collectionName, testBaseParameters);
55+
} catch (ArangoException e) {
56+
e.printStackTrace();
57+
}
58+
59+
try {
60+
documentEntity2 = driver.getDocument(documentEntity1.getDocumentHandle(), TestBaseParameters.class);
61+
} catch (ArangoException e) {
62+
e.printStackTrace();
63+
}
64+
65+
assertThat(documentEntity2.getEntity().isError(), is(true));
66+
assertThat(documentEntity2.getEntity().getCode(), is(500));
67+
assertThat(documentEntity2.getEntity().getErrorNum(), is(4711));
68+
assertThat(documentEntity2.getEntity().getErrorMessage(), is(errorMessage));
69+
70+
assertThat(documentEntity2.isError(), is(false));
71+
assertThat(documentEntity2.getCode(), is(200));
72+
assertThat(documentEntity2.getErrorNumber(), is(0));
73+
assertThat(documentEntity2.getErrorMessage(), is(nullValue()));
74+
75+
}
76+
77+
}

src/test/java/com/arangodb/ArangoConfigureTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void connect_timeout() throws ArangoException {
8787
}
8888

8989
@Test
90+
@Ignore
9091
public void so_connect_timeout() throws ArangoException {
9192

9293
ArangoConfigure configure = new ArangoConfigure();

0 commit comments

Comments
 (0)