-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SimpleQuestDBTest.java
71 lines (61 loc) · 2.82 KB
/
SimpleQuestDBTest.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
package org.testcontainers.junit.questdb;
import io.questdb.client.Sender;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.testcontainers.QuestDBTestImages;
import org.testcontainers.containers.QuestDBContainer;
import org.testcontainers.db.AbstractContainerDatabaseTest;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
public class SimpleQuestDBTest extends AbstractContainerDatabaseTest {
private static final String TABLE_NAME = "mytable";
@Test
public void testSimple() throws SQLException {
try (QuestDBContainer questDB = new QuestDBContainer(QuestDBTestImages.QUESTDB_IMAGE)) {
questDB.start();
ResultSet resultSet = performQuery(questDB, questDB.getTestQueryString());
int resultSetInt = resultSet.getInt(1);
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
}
}
@Test
public void testRest() throws IOException {
try (QuestDBContainer questdb = new QuestDBContainer(QuestDBTestImages.QUESTDB_IMAGE)) {
questdb.start();
populateByInfluxLineProtocol(questdb, 1_000);
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
String encodedSql = URLEncoder.encode("select * from " + TABLE_NAME, "UTF-8");
HttpGet httpGet = new HttpGet(questdb.getHttpUrl() + "/exec?query=" + encodedSql);
await()
.untilAsserted(() -> {
try (CloseableHttpResponse response = client.execute(httpGet)) {
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
assertThat(json.contains("\"count\":1000")).isTrue();
}
});
}
}
}
private static void populateByInfluxLineProtocol(QuestDBContainer questdb, int rowCount) {
try (Sender sender = Sender.builder().address(questdb.getIlpUrl()).build()) {
for (int i = 0; i < rowCount; i++) {
sender
.table(TABLE_NAME)
.symbol("sym", "sym1" + i)
.stringColumn("str", "str1" + i)
.longColumn("long", i)
.atNow();
}
}
}
}