-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathDaprHttpTest.java
More file actions
582 lines (481 loc) · 22.2 KB
/
Copy pathDaprHttpTest.java
File metadata and controls
582 lines (481 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
* Copyright 2021 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/
package io.dapr.client;
import io.dapr.config.Properties;
import io.dapr.exceptions.DaprErrorDetails;
import io.dapr.exceptions.DaprException;
import io.dapr.utils.TypeRef;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import reactor.util.context.Context;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import static io.dapr.utils.TestUtils.formatIpAddress;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(SystemStubsExtension.class)
public class DaprHttpTest {
private static final int HTTP_OK = 200;
private static final int HTTP_SERVER_ERROR = 500;
private static final int HTTP_NO_CONTENT = 204;
private static final int HTTP_NOT_FOUND = 404;
private static final String EXPECTED_RESULT =
"{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}";
private static final Duration READ_TIMEOUT = Duration.ofSeconds(60);
@SystemStub
private final EnvironmentVariables environmentVariables = new EnvironmentVariables();
private String sidecarIp;
private String daprTokenApi;
private HttpClient httpClient;
private final ObjectSerializer serializer = new ObjectSerializer();
@BeforeEach
public void setUp() {
sidecarIp = formatIpAddress(Properties.SIDECAR_IP.get());
daprTokenApi = Properties.API_TOKEN.get();
httpClient = mock(HttpClient.class);
}
@Test
public void invokeApi_daprApiToken_present() throws IOException {
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
environmentVariables.set(Properties.API_TOKEN.getEnvName(), "xyz");
assertEquals("xyz", Properties.API_TOKEN.get());
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, Properties.API_TOKEN.get(), READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/state".split("/"),
null,
(byte[]) null,
null,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("POST", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
assertEquals("xyz", request.headers().firstValue(Headers.DAPR_API_TOKEN).get());
}
@Test
public void invokeApi_daprApiToken_absent() throws IOException {
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
assertNull(Properties.API_TOKEN.get());
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/state".split("/"),
null,
(byte[]) null,
null,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("POST", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
assertFalse(request.headers().map().containsKey(Headers.DAPR_API_TOKEN));
}
@Test
public void invokeMethod() throws IOException {
Map<String, String> headers = Map.of(
"content-type", "text/html",
"header1", "value1"
);
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/state".split("/"),
null,
(byte[]) null,
headers,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("POST", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
assertEquals("text/html", request.headers().firstValue("content-type").get());
assertEquals("value1", request.headers().firstValue("header1").get());
}
@Test
public void invokeMethodIPv6() throws IOException {
sidecarIp = formatIpAddress("2001:db8:3333:4444:5555:6666:7777:8888");
Map<String, String> headers = Map.of(
"content-type", "text/html",
"header1", "value1"
);
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/state".split("/"),
null,
(byte[]) null,
headers,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("POST", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
assertEquals("text/html", request.headers().firstValue("content-type").get());
assertEquals("value1", request.headers().firstValue("header1").get());
}
@Test
public void invokePostMethod() throws IOException {
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/state".split("/"),
null,
"",
null,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("POST", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
}
@Test
public void invokeDeleteMethod() throws IOException {
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"DELETE",
"v1.0/state".split("/"),
null,
(String) null,
null,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("DELETE", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
}
@Test
public void invokePatchMethod() throws IOException {
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"PATCH",
"v1.0/state".split("/"),
null,
"",
null,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("PATCH", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
}
@Test
public void invokeHeadMethod() {
MockHttpResponse mockHttpResponse = new MockHttpResponse(HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"HEAD",
"v1.0/state".split("/"),
null,
(String) null,
null,
Context.empty()
);
DaprHttp.Response response = mono.block();
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals("HEAD", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
assertEquals(HTTP_OK, response.getStatusCode());
}
@Test
public void invokeGetMethod() throws IOException {
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"GET",
"v1.0/state".split("/"),
null,
null,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("GET", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state", request.uri().toString());
}
@Test
public void invokeMethodWithHeaders() throws IOException {
Map<String, String> headers = Map.of(
"header", "value",
"header1", "value1"
);
Map<String, List<String>> urlParameters = Map.of(
"orderId", List.of("41")
);
byte[] content = serializer.serialize(EXPECTED_RESULT);
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_OK);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
ArgumentCaptor<HttpRequest> requestCaptor = ArgumentCaptor.forClass(HttpRequest.class);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"GET",
"v1.0/state/order".split("/"),
urlParameters,
headers,
Context.empty()
);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
verify(httpClient).sendAsync(requestCaptor.capture(), any());
HttpRequest request = requestCaptor.getValue();
assertEquals(EXPECTED_RESULT, body);
assertEquals("GET", request.method());
assertEquals("http://" + sidecarIp + ":3500/v1.0/state/order?orderId=41", request.uri().toString());
assertEquals("value", request.headers().firstValue("header").get());
assertEquals("value1", request.headers().firstValue("header1").get());
}
@Test
public void invokePostMethodRuntime() {
MockHttpResponse mockHttpResponse = new MockHttpResponse(HTTP_SERVER_ERROR);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/state".split("/"),
null,
null,
Context.empty());
StepVerifier.create(mono).expectError(RuntimeException.class).verify();
}
@Test
public void invokePostDaprError() {
byte[] content = "{\"errorCode\":null,\"message\":null}".getBytes();
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_SERVER_ERROR);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/state".split("/"),
null,
null,
Context.empty()
);
StepVerifier.create(mono).expectError(RuntimeException.class).verify();
}
@Test
public void invokePostMethodUnknownError() {
byte[] content = "{\"errorCode\":null,\"message\":null}".getBytes();
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_SERVER_ERROR);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/state".split("/"),
null,
null,
Context.empty()
);
StepVerifier.create(mono).expectError(RuntimeException.class).verify();
}
@Test
public void validateExceptionParsing() {
String payload = "{" +
"\"errorCode\":\"ERR_PUBSUB_NOT_FOUND\"," +
"\"message\":\"pubsub abc is not found\"," +
"\"details\":[" +
"{" +
"\"@type\":\"type.googleapis.com/google.rpc.ErrorInfo\"," +
"\"domain\":\"dapr.io\"," +
"\"metadata\":{}," +
"\"reason\":\"DAPR_PUBSUB_NOT_FOUND\"" +
"}]}";
byte[] content = payload.getBytes();
MockHttpResponse mockHttpResponse = new MockHttpResponse(content, HTTP_SERVER_ERROR);
CompletableFuture<HttpResponse<Object>> mockResponse = CompletableFuture.completedFuture(mockHttpResponse);
when(httpClient.sendAsync(any(), any())).thenReturn(mockResponse);
DaprHttp daprHttp = new DaprHttp(Properties.SIDECAR_IP.get(), 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeApi(
"POST",
"v1.0/pubsub/publish".split("/"),
null,
null,
Context.empty()
);
StepVerifier.create(mono).expectErrorMatches(e -> {
assertEquals(DaprException.class, e.getClass());
DaprException daprException = (DaprException)e;
assertEquals("ERR_PUBSUB_NOT_FOUND", daprException.getErrorCode());
assertEquals("DAPR_PUBSUB_NOT_FOUND",
daprException.getErrorDetails()
.get(DaprErrorDetails.ErrorDetailType.ERROR_INFO, "reason", TypeRef.STRING));
return true;
}).verify();
}
/**
* The purpose of this test is to show that it doesn't matter when the client is called, the actual call to DAPR
* will be done when the output Mono response call the Mono.block method.
* Like for instance if you call getState, without blocking for the response, and then call delete for the same state
* you just retrieved but block for the delete response, when later you block for the response of the getState, you will
* not find the state.
* <p>This test will execute the following flow:</p>
* <ol>
* <li>Execute client getState for Key=key1</li>
* <li>Block for result to the state</li>
* <li>Assert the Returned State is the expected to key1</li>
* <li>Execute client getState for Key=key2</li>
* <li>Execute client deleteState for Key=key2</li>
* <li>Block for deleteState call.</li>
* <li>Block for getState for Key=key2 and Assert they 2 was not found.</li>
* </ol>
*
* @throws IOException - Test will fail if any unexpected exception is being thrown
*/
@Test
public void testCallbackCalledAtTheExpectedTimeTest() throws IOException {
String existingState = "existingState";
String urlExistingState = "v1.0/state/" + existingState;
String deletedStateKey = "deletedKey";
String urlDeleteState = "v1.0/state/" + deletedStateKey;
when(httpClient.sendAsync(any(), any())).thenAnswer(invocation -> {
HttpRequest request = invocation.getArgument(0);
String url = request.uri().toString();
if (request.method().equals("GET") && url.contains(urlExistingState)) {
MockHttpResponse mockHttpResponse = new MockHttpResponse(serializer.serialize(existingState), HTTP_OK);
return CompletableFuture.completedFuture(mockHttpResponse);
}
if (request.method().equals("DELETE")) {
return CompletableFuture.completedFuture(new MockHttpResponse(HTTP_NO_CONTENT));
}
if (request.method().equals("GET")) {
byte [] content = "{\"errorCode\":\"404\",\"message\":\"State Not Found\"}".getBytes();
return CompletableFuture.completedFuture(new MockHttpResponse(content, HTTP_NOT_FOUND));
}
return CompletableFuture.failedFuture(new RuntimeException("Unexpected call"));
});
DaprHttp daprHttp = new DaprHttp(sidecarIp, 3500, daprTokenApi, READ_TIMEOUT, httpClient);
Mono<DaprHttp.Response> response = daprHttp.invokeApi(
"GET",
urlExistingState.split("/"),
null,
null,
Context.empty()
);
assertEquals(existingState, serializer.deserialize(response.block().getBody(), String.class));
Mono<DaprHttp.Response> responseDeleted = daprHttp.invokeApi(
"GET",
urlDeleteState.split("/"),
null,
null,
Context.empty()
);
Mono<DaprHttp.Response> responseDeleteKey = daprHttp.invokeApi(
"DELETE",
urlDeleteState.split("/"),
null,
null,
Context.empty()
);
assertNull(serializer.deserialize(responseDeleteKey.block().getBody(), String.class));
try {
responseDeleted.block();
fail("Expected DaprException");
} catch (Exception ex) {
assertEquals(DaprException.class, ex.getClass());
}
}
}