forked from apache/camel-quarkus
-
Notifications
You must be signed in to change notification settings - Fork 9
/
SqlTest.java
253 lines (214 loc) · 10 KB
/
SqlTest.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
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.quarkus.component.sql.it;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.apache.camel.component.sql.SqlConstants;
import org.apache.camel.util.CollectionHelper;
import org.hamcrest.Matcher;
import org.hamcrest.collection.IsMapContaining;
import org.hamcrest.text.IsEqualIgnoringCase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import static io.restassured.RestAssured.given;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.*;
@QuarkusTest
class SqlTest {
@Test
public void testSqlComponent() {
// Create Camel species
RestAssured.given()
.contentType(ContentType.JSON)
.queryParam("table", "camel")
.body(CollectionHelper.mapOf("species", "Dromedarius"))
.post("/sql/insert")
.then()
.statusCode(201);
// Retrieve camel species as map
RestAssured.get("/sql/get/Dromedarius")
.then()
.statusCode(200)
.body(containsStringIgnoringCase("[{ID=1, SPECIES=Dromedarius}]"));
// Retrieve camel species as list
RestAssured.get("/sql/get/Dromedarius/list")
.then()
.statusCode(200)
.body(is("Dromedarius 1"));
// Retrieve camel species as type
RestAssured.get("/sql/get/Dromedarius/list/type")
.then()
.statusCode(200)
.body(is("Dromedarius 1"));
}
@Test
@DisabledIfSystemProperty(named = "cq.sqlJdbcKind", matches = "[^h][^2].*", disabledReason = "https://github.com/apache/camel-quarkus/issues/3080")
public void testSqlStoredComponent() {
// Invoke ADD_NUMS stored procedure
RestAssured.given()
.queryParam("numA", 10)
.queryParam("numB", 5)
.get("/sql/storedproc")
.then()
.statusCode(200)
.body(is("15"));
}
@Test
public void testConsumer() throws InterruptedException {
testConsumer(1, "consumerRoute", "ViaSql");
}
@Test
public void testClasspathConsumer() throws InterruptedException {
testConsumer(2, "consumerClasspathRoute", "ViaClasspath");
}
@Test
public void testFileConsumer() throws InterruptedException {
testConsumer(3, "consumerFileRoute", "ViaFile");
}
@SuppressWarnings("unchecked")
private void testConsumer(int id, String routeId, String via) throws InterruptedException {
Map project = CollectionHelper.mapOf("ID", id, "PROJECT", routeId, "LICENSE", "222", "PROCESSED", false);
Map updatedProject = CollectionHelper.mapOf("ID", id, "PROJECT", routeId, "LICENSE", "XXX", "PROCESSED", false);
postMapWithParam("/sql/insert",
"table", "projects" + via,
project)
.statusCode(201);
//wait for the record to be caught
await().atMost(30, TimeUnit.SECONDS).until(() -> (Iterable<Object>) RestAssured
.get("/sql/get/results/" + routeId).then().extract().as(List.class),
hasItem(matchMapIgnoringCase(project)));
//update
postMapWithParam("/sql/update",
"table", "projects" + via,
updatedProject)
.statusCode(201);
//wait for the record to be caught
await().atMost(30, TimeUnit.SECONDS).until(() -> (Iterable<Object>) RestAssured
.get("/sql/get/results/" + routeId).then().extract().as(List.class),
hasItem(matchMapIgnoringCase(updatedProject)));
}
@Test
public void testTransacted() throws InterruptedException {
postMap("/sql/toDirect/transacted", CollectionHelper.mapOf(SqlConstants.SQL_QUERY,
"insert into projectsViaSql values (5, 'Transacted', 'ASF', BOOLEAN_FALSE)",
"rollback", false))
.statusCode(204);
postMap("/sql/toDirect/transacted", CollectionHelper.mapOf(SqlConstants.SQL_QUERY,
"select * from projectsViaSql where project = 'Transacted'"))
.statusCode(200)
.body("size()", is(1));
postMap("/sql/toDirect/transacted", CollectionHelper.mapOf(SqlConstants.SQL_QUERY,
"insert into projectsViaSql values (6, 'Transacted', 'ASF', BOOLEAN_FALSE)",
"rollback", true))
.statusCode(200)
.body(is("java.lang.Exception:forced Exception"));
postMap("/sql/toDirect/transacted",
CollectionHelper.mapOf(SqlConstants.SQL_QUERY, "select * from projectsViaSql where project = 'Transacted'"))
.statusCode(200)
.body("size()", is(1));
}
@Test
public void testDefaultErrorCode() throws InterruptedException {
postMap("/sql/toDirect/transacted", CollectionHelper.mapOf(SqlConstants.SQL_QUERY, "select * from NOT_EXIST"))
.statusCode(200)
.body(startsWith("org.springframework.jdbc.BadSqlGrammarException"));
}
@Test
@SuppressWarnings("unchecked")
public void testIdempotentRepository() {
// add value with key 1
postMapWithParam("/sql/toDirect/idempotent",
"body", "one",
CollectionHelper.mapOf("messageId", "1"))
.statusCode(200);
// add value with key 2
postMapWithParam("/sql/toDirect/idempotent",
"body", "two",
CollectionHelper.mapOf("messageId", "2"))
.statusCode(200);
// add same value with key 3
postMapWithParam("/sql/toDirect/idempotent",
"body", "three",
CollectionHelper.mapOf("messageId", "3"))
.statusCode(200);
// add another value with key 1 -- this one is supposed to be skipped
postMapWithParam("/sql/toDirect/idempotent",
"body", "four",
CollectionHelper.mapOf("messageId", "1"))
.statusCode(200);
// get all values from the result map
await().atMost(5, TimeUnit.SECONDS).until(() -> (Iterable<? extends String>) RestAssured
.get("/sql/get/results/idempotentRoute").then().extract().as(List.class),
containsInAnyOrder("one", "two", "three"));
}
@Test
@SuppressWarnings("unchecked")
public void testAggregationRepository() {
postMapWithParam("/sql/toDirect/aggregation", "body", "A", CollectionHelper.mapOf("messageId", "123"))
.statusCode(200);
postMapWithParam("/sql/toDirect/aggregation", "body", "B", CollectionHelper.mapOf("messageId", "123"))
.statusCode(200);
postMapWithParam("/sql/toDirect/aggregation", "body", "C", CollectionHelper.mapOf("messageId", "123"))
.statusCode(200);
postMapWithParam("/sql/toDirect/aggregation", "body", "D", CollectionHelper.mapOf("messageId", "123"))
.statusCode(200);
// get all values from the result map
await().atMost(5, TimeUnit.SECONDS).until(() -> (Iterable<? extends String>) RestAssured
.get("/sql/get/results/aggregationRoute").then().extract().as(List.class),
containsInAnyOrder("ABCD"));
}
private ValidatableResponse postMap(String toUrl, Map<String, String> body) {
return postMapWithParam(toUrl, null, null, body);
}
private ValidatableResponse postMapWithParam(String toUrl, String param, String paramValue, Map<String, String> body) {
RequestSpecification rs = RestAssured.given()
.contentType(ContentType.JSON);
if (param != null) {
rs = rs.queryParam(param, paramValue);
}
return rs.body(body)
.post(toUrl)
.then();
}
@SuppressWarnings("unchecked")
public static org.hamcrest.Matcher<java.util.Map<String, Object>> matchMapIgnoringCase(Map<String, Object> map) {
Matcher m = null;
for (Map.Entry<String, Object> entry : map.entrySet()) {
Matcher fieldCondition;
if (entry.getValue() instanceof Boolean) {
//it is boolean type and different dbs return different representations of boolean
fieldCondition = either(new IsMapContaining(new IsEqualIgnoringCase(entry.getKey()), is(entry.getValue())))
.or(new IsMapContaining(new IsEqualIgnoringCase(entry.getKey()),
is((Boolean) entry.getValue() ? 1 : 0)));
} else {
fieldCondition = new IsMapContaining(new IsEqualIgnoringCase(entry.getKey()), is(entry.getValue()));
}
if (m == null) {
m = fieldCondition;
} else {
m = both(m).and(fieldCondition);
}
}
return m;
}
}