Skip to content

Commit 05f0b9c

Browse files
author
a-brandt
committed
added warning messages to cursor result
1 parent 8d2422b commit 05f0b9c

File tree

5 files changed

+154
-4
lines changed

5 files changed

+154
-4
lines changed

src/main/java/com/arangodb/CursorResult.java

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.NoSuchElementException;
2323

2424
import com.arangodb.entity.CursorEntity;
25+
import com.arangodb.entity.WarningEntity;
2526

2627
/**
2728
* @author tamtam180 - kirscheless at gmail.com
@@ -174,4 +175,22 @@ public void remove() {
174175

175176
}
176177

178+
/**
179+
* Returns true, if there are AQL warnings
180+
*
181+
* @return true, if there are AQL warnings
182+
*/
183+
public boolean hasWarning() {
184+
return entity.hasWarnings();
185+
}
186+
187+
/**
188+
* Returns a list of AQL warnings (code and message)
189+
*
190+
* @return list of AQL warnings
191+
*/
192+
public List<WarningEntity> getWarnings() {
193+
return entity.getWarnings();
194+
}
195+
177196
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public class CursorEntity<T> extends BaseEntity implements Iterable<T> {
7575
*/
7676
List<? extends T> results;
7777

78+
/**
79+
* A list of warnings
80+
*/
81+
List<WarningEntity> warnings;
82+
7883
@SuppressWarnings("unchecked")
7984
@Override
8085
public Iterator<T> iterator() {
@@ -204,4 +209,15 @@ public void setCached(boolean cached) {
204209
this.cached = cached;
205210
}
206211

212+
public List<WarningEntity> getWarnings() {
213+
return warnings;
214+
}
215+
216+
public void setWarnings(List<WarningEntity> warnings) {
217+
this.warnings = warnings;
218+
}
219+
220+
public boolean hasWarnings() {
221+
return CollectionUtils.isNotEmpty(this.warnings);
222+
}
207223
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ public CursorEntity<?> deserialize(JsonElement json, Type typeOfT, JsonDeseriali
568568
entity.bindVars = context.deserialize(obj.get("bindVars"), bindVarsType);
569569
}
570570

571+
entity.warnings = new ArrayList<WarningEntity>();
571572
if (obj.has("extra")) {
572573
entity.extra = context.deserialize(obj.get("extra"), extraType);
573574

@@ -585,6 +586,24 @@ public CursorEntity<?> deserialize(JsonElement json, Type typeOfT, JsonDeseriali
585586
}
586587
}
587588
}
589+
if (entity.extra.containsKey("warnings")) {
590+
Object object = entity.extra.get("warnings");
591+
if (object instanceof List<?>) {
592+
List<?> l = (List<?>) entity.extra.get("warnings");
593+
for (Object o : l) {
594+
if (o instanceof Map<?, ?>) {
595+
Map<?, ?> m = (Map<?, ?>) o;
596+
if (m.containsKey("code") && m.get("code") instanceof Double && m.containsKey("message")
597+
&& m.get("message") instanceof String) {
598+
Long code = ((Double) m.get("code")).longValue();
599+
String message = (String) m.get("message");
600+
entity.warnings.add(new WarningEntity(code, message));
601+
}
602+
}
603+
}
604+
}
605+
}
606+
588607
}
589608

590609
return entity;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2012,2013 tamtam180
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb.entity;
18+
19+
/**
20+
* @author a-brandt
21+
*/
22+
public class WarningEntity {
23+
24+
/**
25+
* a warning code
26+
*/
27+
Long code;
28+
29+
/**
30+
* a warning message
31+
*/
32+
String message;
33+
34+
public WarningEntity() {
35+
}
36+
37+
public WarningEntity(Long code, String message) {
38+
this.code = code;
39+
this.message = message;
40+
}
41+
42+
/**
43+
* returns the warning code
44+
*
45+
* @return a warning code
46+
*/
47+
public Long getCode() {
48+
return code;
49+
}
50+
51+
public void setCode(Long code) {
52+
this.code = code;
53+
}
54+
55+
/**
56+
* returns a warning message
57+
*
58+
* @return a warning message
59+
*/
60+
public String getMessage() {
61+
return message;
62+
}
63+
64+
public void setMessage(String message) {
65+
this.message = message;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "WarningEntity [code=" + code + ", message=" + message + "]";
71+
}
72+
73+
}

src/test/java/com/arangodb/ArangoDriverCursorTest.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertThat;
2424

25+
import java.util.HashMap;
26+
import java.util.List;
2527
import java.util.Map;
2628

2729
import org.junit.Ignore;
2830
import org.junit.Test;
2931

3032
import com.arangodb.entity.CursorEntity;
33+
import com.arangodb.entity.WarningEntity;
3134
import com.arangodb.util.MapBuilder;
3235

3336
/**
@@ -44,8 +47,9 @@ public ArangoDriverCursorTest(ArangoConfigure configure, ArangoDriver driver) {
4447
public void test_validateQuery() throws ArangoException {
4548

4649
CursorEntity<?> entity = driver.validateQuery(
47-
// "SELECT t FROM unit_test_cursor t WHERE t.name == @name@ && t.age >= @age@"
48-
"FOR t IN unit_test_cursor FILTER t.name == @name && t.age >= @age RETURN t");
50+
// "SELECT t FROM unit_test_cursor t WHERE t.name == @name@ && t.age
51+
// >= @age@"
52+
"FOR t IN unit_test_cursor FILTER t.name == @name && t.age >= @age RETURN t");
4953

5054
assertThat(entity.getCode(), is(200));
5155
assertThat(entity.getBindVars().size(), is(2));
@@ -60,8 +64,8 @@ public void test_validateQuery_400_1() throws ArangoException {
6064
// =じゃなくて==じゃないとダメ。文法間違いエラー
6165
try {
6266
driver.validateQuery(
63-
// "SELECT t FROM unit_test_cursor t WHERE t.name = @name@"
64-
"FOR t IN unit_test_cursor FILTER t.name = @name@");
67+
// "SELECT t FROM unit_test_cursor t WHERE t.name = @name@"
68+
"FOR t IN unit_test_cursor FILTER t.name = @name@");
6569

6670
} catch (ArangoException e) {
6771
assertThat(e.getCode(), is(400));
@@ -258,4 +262,23 @@ public void test_executeQueryUniqueResult() throws ArangoException {
258262
assertThat(entity.getAge(), is(10));
259263
}
260264
}
265+
266+
@Test
267+
public void test_warning() throws ArangoException {
268+
String collectionName = "unit_test_query_test";
269+
try {
270+
driver.createCollection(collectionName);
271+
} catch (ArangoException e) {
272+
}
273+
driver.truncateCollection(collectionName);
274+
275+
String query = "return _users + 1";
276+
Map<String, Object> bindVars = new HashMap<String, Object>();
277+
CursorResult<Map> cursor = driver.executeAqlQuery(query, bindVars, null, Map.class);
278+
assertThat(cursor.hasWarning(), is(true));
279+
280+
List<WarningEntity> warnings = cursor.getWarnings();
281+
assertThat(warnings.size(), is(1));
282+
}
283+
261284
}

0 commit comments

Comments
 (0)