This repository was archived by the owner on Feb 12, 2023. It is now read-only.
forked from influxdata/influxdb-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkingExceptionTest.java
92 lines (73 loc) · 3.23 KB
/
ChunkingExceptionTest.java
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
package org.influxdb.impl;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.EOFException;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.influxdb.InfluxDB;
import org.influxdb.TestUtils;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okio.Buffer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@RunWith(JUnitPlatform.class)
public class ChunkingExceptionTest {
@Test
public void testChunkingIOException() throws IOException, InterruptedException {
testChunkingException(new IOException(), "java.io.IOException");
}
@Test
public void testChunkingEOFException() throws IOException, InterruptedException {
testChunkingException(new EOFException(), "DONE");
}
public void testChunkingException(Exception ex, String message) throws IOException, InterruptedException {
InfluxDBService influxDBService = mock(InfluxDBService.class);
JsonAdapter<QueryResult> adapter = mock(JsonAdapter.class);
Call<ResponseBody> call = mock(Call.class);
ResponseBody responseBody = mock(ResponseBody.class);
when(influxDBService.query(any(String.class), any(String.class), anyInt())).thenReturn(call);
when(responseBody.source()).thenReturn(new Buffer());
doThrow(ex).when(adapter).fromJson(any(JsonReader.class));
String url = "http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true);
InfluxDB influxDB = new InfluxDBImpl(url, "admin", "admin", new OkHttpClient.Builder(), influxDBService, adapter) {
@Override
public String version() {
return "9.99";
}
};
String dbName = "write_unittest_" + System.currentTimeMillis();
final BlockingQueue<QueryResult> queue = new LinkedBlockingQueue<>();
Query query = new Query("SELECT * FROM disk", dbName);
influxDB.query(query, 2, new Consumer<QueryResult>() {
@Override
public void accept(QueryResult result) {
queue.add(result);
}
});
ArgumentCaptor<Callback<ResponseBody>> argumentCaptor = ArgumentCaptor.forClass(Callback.class);
verify(call).enqueue(argumentCaptor.capture());
Callback<ResponseBody> callback = argumentCaptor.getValue();
callback.onResponse(call, Response.success(responseBody));
QueryResult result = queue.poll(20, TimeUnit.SECONDS);
Assertions.assertNotNull(result);
Assertions.assertEquals(message, result.getError());
}
}