forked from syndesisio/connectors
/
SqlConnectorComponentTest.java
187 lines (160 loc) · 7.37 KB
/
SqlConnectorComponentTest.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
/**
* 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 io.syndesis.connector.sql;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import io.syndesis.connector.sql.SqlParam.SqlSampleValue;
import io.syndesis.connector.sql.stored.JSONBeanUtil;
public class SqlConnectorComponentTest {
private static Connection connection;
private static Properties properties = new Properties();
private static SqlCommon sqlCommon;
private static String schema;
private static Map<String, Object> parameters = new HashMap<String, Object>();
@BeforeClass
public static void setUpBeforeClass() throws Exception {
sqlCommon = new SqlCommon();
connection = sqlCommon.setupConnection(connection, properties);
for (final String name : properties.stringPropertyNames()) {
parameters.put(name.substring(name.indexOf(".") + 1), properties.getProperty(name));
}
schema = DatabaseMetaDataHelper.getDefaultSchema(
connection.getMetaData().getDatabaseProductName(), parameters.get("user").toString());
}
@AfterClass
public static void afterClass() throws SQLException {
sqlCommon.closeConnection(connection);
}
@Test
public void camelConnectorTest() throws Exception {
Statement stmt = connection.createStatement();
String createTable = "CREATE TABLE NAME (id INTEGER PRIMARY KEY, firstName VARCHAR(255), " +
"lastName VARCHAR(255))";
stmt.executeUpdate(createTable);
stmt.executeUpdate("INSERT INTO NAME VALUES (1, 'Joe', 'Jackson')");
stmt.executeUpdate("INSERT INTO NAME VALUES (2, 'Roger', 'Waters')");
BasicDataSource ds = new BasicDataSource();
ds.setUsername(properties.getProperty("sql-connector.user"));
ds.setPassword(properties.getProperty("sql-connector.password"));
ds.setUrl( properties.getProperty("sql-connector.url"));
SimpleRegistry registry = new SimpleRegistry();
registry.put("dataSource", ds);
CamelContext context = new DefaultCamelContext(registry);
CountDownLatch latch = new CountDownLatch(1);
final Result result = new Result();
try {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("sql-connector:SELECT * FROM NAME?dataSource=dataSource")
.process(new Processor() {
@Override
public void process(Exchange exchange)
throws Exception {
result.setResult(exchange.getIn().getBody(String.class));
latch.countDown();
}
}).to("stream:out");
}
});
context.start();
latch.await(5l,TimeUnit.SECONDS);
Assert.assertEquals("{ID=1, FIRSTNAME=Joe, LASTNAME=Jackson}", result.getJsonBean());
} finally {
context.stop();
}
}
@Test
public void camelConnectorInputParamTest() throws Exception {
Statement stmt = connection.createStatement();
String createTable = "CREATE TABLE ALLTYPES (charType CHAR, varcharType VARCHAR(255), " +
"numericType NUMERIC, decimalType DECIMAL, smallType SMALLINT," +
"dateType DATE, timeType TIME )";
stmt.executeUpdate(createTable);
BasicDataSource ds = new BasicDataSource();
ds.setUsername(properties.getProperty("sql-connector.user"));
ds.setPassword(properties.getProperty("sql-connector.password"));
ds.setUrl( properties.getProperty("sql-connector.url"));
SimpleRegistry registry = new SimpleRegistry();
registry.put("dataSource", ds);
CamelContext context = new DefaultCamelContext(registry);
CountDownLatch latch = new CountDownLatch(1);
final Result result = new Result();
Map<String,Object> values = new HashMap<>();
values.put("CHARVALUE", SqlSampleValue.charValue);
values.put("VARCHARVALUE", SqlSampleValue.stringValue);
values.put("NUMERICVALUE", SqlSampleValue.decimalValue);
values.put("DECIMALVALUE", SqlSampleValue.decimalValue);
values.put("SMALLINTVALUE", SqlSampleValue.integerValue);
String jsonBody = JSONBeanUtil.toJSONBean(values);
final String insertStatement = "INSERT INTO ALLTYPES "
+ "(charType, varcharType, numericType, decimalType, smallType) VALUES "
+ "(:CHARVALUE, :VARCHARVALUE, :NUMERICVALUE, :DECIMALVALUE, :SMALLINTVALUE)";
SqlStatementParser parser = new SqlStatementParser(connection, schema, insertStatement);
String camelStatement = parser.parse().getCamelSqlStatement();
try {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("timer://myTimer?period=2000")
.setBody().constant(jsonBody)
.to("sql-connector:" + camelStatement)
.process(new Processor() {
@Override
public void process(Exchange exchange)
throws Exception {
result.setResult(exchange.getIn().getBody(String.class));
latch.countDown();
}
}).to("stream:out");
}
});
context.start();
latch.await(5l,TimeUnit.SECONDS);
Properties props = JSONBeanUtil.parsePropertiesFromJSONBean(result.getJsonBean());
Assert.assertEquals(values.size(),props.size());
} finally {
context.stop();
}
}
class Result {
String jsonBean;
public String getJsonBean() {
return jsonBean;
}
public void setResult(String jsonBean) {
this.jsonBean = jsonBean;
}
}
}