|
1 |
| -package resilience.http; |
| 1 | +package resilience.mock; |
2 | 2 |
|
3 | 3 | import ch.qos.logback.classic.Level;
|
4 |
| -import com.arangodb.ArangoDB; |
5 | 4 | import com.arangodb.ArangoDBException;
|
6 |
| -import com.arangodb.Protocol; |
7 |
| -import com.arangodb.internal.net.Communication; |
| 5 | +import com.arangodb.entity.MultiDocumentEntity; |
8 | 6 | import com.fasterxml.jackson.core.JsonParseException;
|
9 |
| -import org.junit.jupiter.api.AfterEach; |
10 |
| -import org.junit.jupiter.api.BeforeEach; |
| 7 | +import com.fasterxml.jackson.databind.JsonNode; |
11 | 8 | import org.junit.jupiter.api.Test;
|
12 |
| -import org.mockserver.integration.ClientAndServer; |
13 |
| -import org.mockserver.matchers.Times; |
14 |
| -import resilience.SingleServerTest; |
| 9 | +import resilience.MockTest; |
15 | 10 |
|
16 |
| -import java.util.Collections; |
17 |
| -import java.util.concurrent.ExecutionException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
18 | 14 |
|
19 | 15 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 16 | import static org.assertj.core.api.Assertions.catchThrowable;
|
21 |
| -import static org.mockserver.integration.ClientAndServer.startClientAndServer; |
22 | 17 | import static org.mockserver.model.HttpRequest.request;
|
23 | 18 | import static org.mockserver.model.HttpResponse.response;
|
24 | 19 |
|
25 |
| -class MockTest extends SingleServerTest { |
26 |
| - |
27 |
| - private ClientAndServer mockServer; |
28 |
| - private ArangoDB arangoDB; |
29 |
| - |
30 |
| - public MockTest() { |
31 |
| - super(Collections.singletonMap(Communication.class, Level.DEBUG)); |
32 |
| - } |
33 |
| - |
34 |
| - @BeforeEach |
35 |
| - void before() { |
36 |
| - mockServer = startClientAndServer(getEndpoint().getHost(), getEndpoint().getPort()); |
37 |
| - arangoDB = new ArangoDB.Builder() |
38 |
| - .protocol(Protocol.HTTP_JSON) |
39 |
| - .password(PASSWORD) |
40 |
| - .host("127.0.0.1", mockServer.getPort()) |
41 |
| - .build(); |
42 |
| - } |
43 |
| - |
44 |
| - @AfterEach |
45 |
| - void after() { |
46 |
| - arangoDB.shutdown(); |
47 |
| - mockServer.stop(); |
48 |
| - } |
49 |
| - |
50 |
| - @Test |
51 |
| - void retryOn503() { |
52 |
| - arangoDB.getVersion(); |
53 |
| - |
54 |
| - mockServer |
55 |
| - .when( |
56 |
| - request() |
57 |
| - .withMethod("GET") |
58 |
| - .withPath("/.*/_api/version"), |
59 |
| - Times.exactly(2) |
60 |
| - ) |
61 |
| - .respond( |
62 |
| - response() |
63 |
| - .withStatusCode(503) |
64 |
| - .withBody("{\"error\":true,\"errorNum\":503,\"errorMessage\":\"boom\",\"code\":503}") |
65 |
| - ); |
66 |
| - |
67 |
| - logs.reset(); |
68 |
| - arangoDB.getVersion(); |
69 |
| - assertThat(logs.getLogs()) |
70 |
| - .filteredOn(e -> e.getLevel().equals(Level.WARN)) |
71 |
| - .anyMatch(e -> e.getFormattedMessage().contains("Could not connect to host")); |
72 |
| - } |
73 |
| - |
74 |
| - @Test |
75 |
| - void retryOn503Async() throws ExecutionException, InterruptedException { |
76 |
| - arangoDB.async().getVersion().get(); |
77 |
| - |
78 |
| - mockServer |
79 |
| - .when( |
80 |
| - request() |
81 |
| - .withMethod("GET") |
82 |
| - .withPath("/.*/_api/version"), |
83 |
| - Times.exactly(2) |
84 |
| - ) |
85 |
| - .respond( |
86 |
| - response() |
87 |
| - .withStatusCode(503) |
88 |
| - .withBody("{\"error\":true,\"errorNum\":503,\"errorMessage\":\"boom\",\"code\":503}") |
89 |
| - ); |
90 |
| - |
91 |
| - logs.reset(); |
92 |
| - arangoDB.async().getVersion().get(); |
93 |
| - assertThat(logs.getLogs()) |
94 |
| - .filteredOn(e -> e.getLevel().equals(Level.WARN)) |
95 |
| - .anyMatch(e -> e.getFormattedMessage().contains("Could not connect to host")); |
96 |
| - } |
| 20 | +public class SerdeTest extends MockTest { |
97 | 21 |
|
98 | 22 | @Test
|
99 | 23 | void unparsableData() {
|
@@ -178,4 +102,35 @@ void textPlainDataWithCharset() {
|
178 | 102 | .hasMessageContaining("upstream timed out");
|
179 | 103 | }
|
180 | 104 |
|
| 105 | + @Test |
| 106 | + void getDocumentsWithErrorField() { |
| 107 | + List<String> keys = Arrays.asList("1", "2", "3"); |
| 108 | + |
| 109 | + String resp = "[" + |
| 110 | + "{\"error\":true,\"_key\":\"1\",\"_id\":\"col/1\",\"_rev\":\"_i4otI-q---\"}," + |
| 111 | + "{\"_key\":\"2\",\"_id\":\"col/2\",\"_rev\":\"_i4otI-q--_\"}," + |
| 112 | + "{\"_key\":\"3\",\"_id\":\"col/3\",\"_rev\":\"_i4otI-q--A\"}" + |
| 113 | + "]"; |
| 114 | + |
| 115 | + mockServer |
| 116 | + .when( |
| 117 | + request() |
| 118 | + .withMethod("PUT") |
| 119 | + .withPath("/.*/_api/document/col") |
| 120 | + .withQueryStringParameter("onlyget", "true") |
| 121 | + ) |
| 122 | + .respond( |
| 123 | + response() |
| 124 | + .withStatusCode(200) |
| 125 | + .withHeader("Content-Type", "application/json; charset=utf-8") |
| 126 | + .withBody(resp.getBytes(StandardCharsets.UTF_8)) |
| 127 | + ); |
| 128 | + |
| 129 | + MultiDocumentEntity<JsonNode> res = arangoDB.db().collection("col").getDocuments(keys, JsonNode.class); |
| 130 | + assertThat(res.getErrors()).isEmpty(); |
| 131 | + assertThat(res.getDocuments()).hasSize(3) |
| 132 | + .anySatisfy(d -> assertThat(d.get("_key").textValue()).isEqualTo("1")) |
| 133 | + .anySatisfy(d -> assertThat(d.get("_key").textValue()).isEqualTo("2")) |
| 134 | + .anySatisfy(d -> assertThat(d.get("_key").textValue()).isEqualTo("3")); |
| 135 | + } |
181 | 136 | }
|
0 commit comments