2
2
3
3
import ch .qos .logback .classic .Level ;
4
4
import com .arangodb .ArangoDB ;
5
+ import com .arangodb .ArangoDBException ;
5
6
import com .arangodb .Protocol ;
7
+ import com .arangodb .internal .net .Communication ;
8
+ import com .fasterxml .jackson .core .JsonParseException ;
6
9
import org .junit .jupiter .api .AfterEach ;
7
10
import org .junit .jupiter .api .BeforeEach ;
8
11
import org .junit .jupiter .api .Test ;
9
12
import org .mockserver .integration .ClientAndServer ;
10
13
import org .mockserver .matchers .Times ;
11
14
import resilience .SingleServerTest ;
12
15
16
+ import java .util .Collections ;
13
17
import java .util .concurrent .ExecutionException ;
14
18
15
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .catchThrowable ;
16
21
import static org .mockserver .integration .ClientAndServer .startClientAndServer ;
17
22
import static org .mockserver .model .HttpRequest .request ;
18
23
import static org .mockserver .model .HttpResponse .response ;
@@ -22,6 +27,10 @@ class MockTest extends SingleServerTest {
22
27
private ClientAndServer mockServer ;
23
28
private ArangoDB arangoDB ;
24
29
30
+ public MockTest () {
31
+ super (Collections .singletonMap (Communication .class , Level .DEBUG ));
32
+ }
33
+
25
34
@ BeforeEach
26
35
void before () {
27
36
mockServer = startClientAndServer (getEndpoint ().getHost (), getEndpoint ().getPort ());
@@ -85,4 +94,88 @@ void retryOn503Async() throws ExecutionException, InterruptedException {
85
94
.filteredOn (e -> e .getLevel ().equals (Level .WARN ))
86
95
.anyMatch (e -> e .getFormattedMessage ().contains ("Could not connect to host" ));
87
96
}
97
+
98
+ @ Test
99
+ void unparsableData () {
100
+ arangoDB .getVersion ();
101
+
102
+ mockServer
103
+ .when (
104
+ request ()
105
+ .withMethod ("GET" )
106
+ .withPath ("/.*/_api/version" )
107
+ )
108
+ .respond (
109
+ response ()
110
+ .withStatusCode (504 )
111
+ .withBody ("upstream timed out" )
112
+ );
113
+
114
+ logs .reset ();
115
+ Throwable thrown = catchThrowable (() -> arangoDB .getVersion ());
116
+ assertThat (thrown )
117
+ .isInstanceOf (ArangoDBException .class )
118
+ .hasMessageContaining ("[Unparsable data]" )
119
+ .hasMessageContaining ("Response: {statusCode=504," );
120
+ Throwable [] suppressed = thrown .getCause ().getSuppressed ();
121
+ assertThat (suppressed ).hasSize (1 );
122
+ assertThat (suppressed [0 ])
123
+ .isInstanceOf (ArangoDBException .class )
124
+ .cause ()
125
+ .isInstanceOf (JsonParseException .class );
126
+ assertThat (logs .getLogs ())
127
+ .filteredOn (e -> e .getLevel ().equals (Level .DEBUG ))
128
+ .anySatisfy (e -> assertThat (e .getFormattedMessage ())
129
+ .contains ("Received Response" )
130
+ .contains ("statusCode=504" )
131
+ .contains ("[Unparsable data]" )
132
+ );
133
+ }
134
+
135
+ @ Test
136
+ void textPlainData () {
137
+ arangoDB .getVersion ();
138
+
139
+ mockServer
140
+ .when (
141
+ request ()
142
+ .withMethod ("GET" )
143
+ .withPath ("/.*/_api/version" )
144
+ )
145
+ .respond (
146
+ response ()
147
+ .withStatusCode (504 )
148
+ .withHeader ("Content-Type" , "text/plain" )
149
+ .withBody ("upstream timed out" )
150
+ );
151
+
152
+ Throwable thrown = catchThrowable (() -> arangoDB .getVersion ());
153
+ assertThat (thrown )
154
+ .isInstanceOf (ArangoDBException .class )
155
+ .hasMessageContaining ("upstream timed out" );
156
+ }
157
+
158
+ @ Test
159
+ void textPlainDataWithCharset () {
160
+ arangoDB .getVersion ();
161
+
162
+ mockServer
163
+ .when (
164
+ request ()
165
+ .withMethod ("GET" )
166
+ .withPath ("/.*/_api/version" )
167
+ )
168
+ .respond (
169
+ response ()
170
+ .withStatusCode (504 )
171
+ .withHeader ("Content-Type" , "text/plain; charset=utf-8" )
172
+ .withBody ("upstream timed out" )
173
+ );
174
+
175
+ Throwable thrown = catchThrowable (() -> arangoDB .getVersion ());
176
+ assertThat (thrown )
177
+ .isInstanceOf (ArangoDBException .class )
178
+ .hasMessageContaining ("upstream timed out" );
179
+ }
180
+
88
181
}
0 commit comments