/
SqlStartConnectorTest.java
154 lines (138 loc) · 6.5 KB
/
SqlStartConnectorTest.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
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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.syndesis.connector.sql;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import io.syndesis.common.model.integration.Step;
import io.syndesis.connector.sql.common.DbEnum;
import io.syndesis.connector.sql.common.JSONBeanUtil;
import io.syndesis.connector.sql.util.SqlConnectorTestSupport;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@SuppressWarnings({"PMD.SignatureDeclareThrowsException"})
@RunWith(Parameterized.class)
public class SqlStartConnectorTest extends SqlConnectorTestSupport {
private final String sqlQuery;
private final List<Map<String, String[]>> expectedResults;
private final Map<String, Object> parameters;
public SqlStartConnectorTest(String sqlQuery, List<Map<String, String[]>> expectedResults, Map<String, Object> parameters) {
this.sqlQuery = sqlQuery;
this.expectedResults = expectedResults;
this.parameters = parameters;
}
@Override
protected List<String> cleanupStatements() {
return Collections.singletonList("DROP TABLE NAME");
}
@Override
protected List<String> setupStatements() {
String dbProductName = null;
try {
dbProductName = db.connection.getMetaData().getDatabaseProductName();
} catch (SQLException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
if (DbEnum.POSTGRESQL.equals(DbEnum.fromName(dbProductName))) {
return Arrays.asList("CREATE TABLE NAME ("
+ "ID SERIAL PRIMARY KEY, "
+ "firstName VARCHAR(255), lastName VARCHAR(255))",
"INSERT INTO NAME (firstname, lastname) VALUES ('Joe', 'Jackson')",
"INSERT INTO NAME (firstname, lastname) VALUES ('Roger', 'Waters')");
} else if (DbEnum.MYSQL.equals(DbEnum.fromName(dbProductName))) {
return Arrays.asList("CREATE TABLE NAME ("
+ "ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
+ "firstName VARCHAR(255), lastName VARCHAR(255))",
"INSERT INTO NAME (firstname, lastname) VALUES ('Joe', 'Jackson')",
"INSERT INTO NAME (firstname, lastname) VALUES ('Roger', 'Waters')");
} else if (DbEnum.APACHE_DERBY.equals(DbEnum.fromName(dbProductName))) {
return Arrays.asList("CREATE TABLE NAME (ID INTEGER NOT NULL "
+ "GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
+ "firstName VARCHAR(255), lastName VARCHAR(255))",
"INSERT INTO NAME (firstname, lastname) VALUES ('Joe', 'Jackson')",
"INSERT INTO NAME (firstname, lastname) VALUES ('Roger', 'Waters')");
} else {
return Arrays.asList("CREATE TABLE NAME ("
+ "ID NUMBER GENERATED ALWAYS AS IDENTITY, "
+ "firstName VARCHAR(255), lastName VARCHAR(255))",
"INSERT INTO NAME (firstname, lastname) VALUES ('Joe', 'Jackson')",
"INSERT INTO NAME (firstname, lastname) VALUES ('Roger', 'Waters')");
}
}
@Override
protected List<Step> createSteps() {
return Arrays.asList(
newSimpleEndpointStep(
"direct",
builder -> builder.putConfiguredProperty("name", "start")),
newSqlEndpointStep(
"sql-start-connector",
builder -> builder.putConfiguredProperty("query", sqlQuery)),
newSimpleEndpointStep(
"log",
builder -> builder.putConfiguredProperty("loggerName", "test")),
newSimpleEndpointStep(
"mock",
builder -> builder.putConfiguredProperty("name", "result"))
);
}
// **************************
// Parameters
// **************************
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "SELECT * FROM NAME ORDER BY id", Arrays.asList(Collections.singletonMap("ID", new String[] { "1", "2" }),
Collections.singletonMap("FIRSTNAME", new String[] { "Joe", "Roger" }),
Collections.singletonMap("LASTNAME", new String[] { "Jackson", "Waters" })),
Collections.emptyMap()},
{ "SELECT * FROM NAME WHERE id = 2", Arrays.asList(Collections.singletonMap("ID", new String[] { "2" }),
Collections.singletonMap("FIRSTNAME", new String[] { "Roger" }),
Collections.singletonMap("LASTNAME", new String[] { "Waters" })),
Collections.emptyMap()},
{ "SELECT * FROM NAME WHERE id = 99", Collections.emptyList(), Collections.emptyMap()},
{ "INSERT INTO NAME (firstname, lastname) VALUES ('Kurt', 'Cobain')",
Collections.singletonList(Collections.singletonMap("ID", new String[]{"3"})),
Collections.emptyMap()}
});
}
// **************************
// Tests
// **************************
@Test
public void sqlStartConnectorTest() throws Exception {
String body;
if (parameters.isEmpty()) {
body = null;
} else {
body = JSONBeanUtil.toJSONBean(parameters);
}
@SuppressWarnings("unchecked")
List<String> jsonBeans = template.requestBody("direct:start", body, List.class);
Assert.assertEquals(expectedResults.isEmpty(), jsonBeans.isEmpty());
for (Map<String, String[]> result : expectedResults) {
for (Map.Entry<String, String[]> resultEntry : result.entrySet()) {
validateJson(jsonBeans, resultEntry.getKey(), resultEntry.getValue());
}
}
}
}