/
DbTriggerTest.java
277 lines (230 loc) · 14.3 KB
/
DbTriggerTest.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric.db;
import java.sql.Types;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jumpmind.symmetric.AbstractDatabaseTest;
import org.jumpmind.symmetric.SymmetricEngine;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.TestConstants;
import org.jumpmind.symmetric.db.oracle.OracleDbDialect;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.service.IBootstrapService;
import org.jumpmind.symmetric.service.IConfigurationService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public class DbTriggerTest extends AbstractDatabaseTest {
static final Log logger = LogFactory.getLog(DbTriggerTest.class);
private static final String TEST_TRIGGERS_TABLE = "test_triggers_table";
final static String INSERT = "insert into "
+ TEST_TRIGGERS_TABLE
+ " (string_One_Value,string_Two_Value,long_String_Value,time_Value,date_Value,boolean_Value,bigInt_Value,decimal_Value) "
+ "values(?,?,?,?,?,?,?,?)"; //'\\\\','\"','\"1\"',null,null,1,1,1)";
final static int[] INSERT_TYPES = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP,
Types.DATE, Types.BOOLEAN, Types.INTEGER, Types.DECIMAL };
final static Object[] INSERT1_VALUES = new Object[] { "\\\\", "\"", "\"1\"", null, null, Boolean.TRUE, 1, 1 };
final static Object[] INSERT2_VALUES = new Object[] { "here", "here", "1", null, null, Boolean.TRUE, 1, 1 };
final static String EXPECTED_INSERT1_CSV_ENDSWITH = "\"\\\\\\\\\",\"\\\"\",\"\\\"1\\\"\",,,\"1\",\"1\",\"1\"";
final static String EXPECTED_INSERT2_CSV_ENDSWITH = "\"here\",\"here\",\"1\",,,\"1\",\"1\"";
final static String TEST_TRIGGER_WHERE_CLAUSE = "where source_table_name='" + TEST_TRIGGERS_TABLE
+ "' and source_node_group_id='" + TestConstants.TEST_ROOT_NODE_GROUP + "' and target_node_group_id='"
+ TestConstants.TEST_ROOT_NODE_GROUP + "' and channel_id='" + TestConstants.TEST_CHANNEL_ID + "'";
@Test(groups = "continuous")
public void testBootstrapSchemaSync() throws Exception {
IBootstrapService service = (IBootstrapService) getSymmetricEngine().getApplicationContext().getBean(
"bootstrapService");
// baseline
service.syncTriggers();
// get the current number of hist rows
int count = getTriggerHistTableRowCount(getSymmetricEngine());
Thread.sleep(1000);
// force the triggers to rebuild
count = count
+ getJdbcTemplate(getSymmetricEngine())
.update(
"update "
+ TestConstants.TEST_PREFIX
+ "trigger set last_updated_time=current_timestamp where inactive_time is null and source_node_group_id='"
+ TestConstants.TEST_ROOT_NODE_GROUP
+ "' and (sync_on_update = 1 or sync_on_insert = 1 or sync_on_delete = 1)");
service.syncTriggers();
// check to see that we recorded the rebuilds
Assert.assertEquals(getTriggerHistTableRowCount(getSymmetricEngine()), count,
"Wrong trigger_hist row count. engine=" + getDbDialect(getSymmetricEngine()).getPlatform().getName());
}
private JdbcTemplate getJdbcTemplate(SymmetricEngine engine) {
return (JdbcTemplate) engine.getApplicationContext().getBean(Constants.JDBC);
}
private IDbDialect getDbDialect(SymmetricEngine engine) {
return (IDbDialect) engine.getApplicationContext().getBean(Constants.DB_DIALECT);
}
private int getTriggerHistTableRowCount(SymmetricEngine engine) {
return getJdbcTemplate(engine)
.queryForInt("select count(*) from " + TestConstants.TEST_PREFIX + "trigger_hist");
}
@Test(groups = "continuous", dependsOnMethods = "testBootstrapSchemaSync")
public void validateTestTableTriggers() throws Exception {
JdbcTemplate jdbcTemplate = getJdbcTemplate(getSymmetricEngine());
int count = jdbcTemplate.update(INSERT, filterValues(INSERT1_VALUES), filterTypes(INSERT_TYPES));
assert count == 1;
String csvString = getNextDataRow(getSymmetricEngine());
boolean match = false;
match = csvString.endsWith(EXPECTED_INSERT1_CSV_ENDSWITH);
assert match : "Received " + csvString + ", Expected the string to end with " + EXPECTED_INSERT1_CSV_ENDSWITH;
}
@SuppressWarnings("unchecked")
@Test(groups = "continuous", dependsOnMethods = "validateTestTableTriggers")
public void testInitialLoadSql() throws Exception {
IConfigurationService service = (IConfigurationService) getSymmetricEngine().getApplicationContext().getBean(
"configurationService");
service.getTriggerFor(TEST_TRIGGERS_TABLE, TestConstants.TEST_ROOT_NODE_GROUP);
String sql = getDbDialect(getSymmetricEngine()).createInitalLoadSqlFor(new Node("1", null, "1.0"),
service.getTriggerFor(TEST_TRIGGERS_TABLE, TestConstants.TEST_ROOT_NODE_GROUP));
List<String> csvStrings = getJdbcTemplate(getSymmetricEngine()).queryForList(sql, String.class);
Assert.assertTrue(csvStrings.size() > 0);
String csvString = csvStrings.get(0);
Assert.assertTrue(csvString.endsWith(EXPECTED_INSERT1_CSV_ENDSWITH), "Received " + csvString + ", Expected the string to end with " + EXPECTED_INSERT1_CSV_ENDSWITH);
}
@Test(groups = "continuous", dependsOnMethods = "testInitialLoadSql")
public void validateTransactionFunctionailty() throws Exception {
final JdbcTemplate jdbcTemplate = getJdbcTemplate(getSymmetricEngine());
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(
getDataSource()));
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
jdbcTemplate.update("update " + TEST_TRIGGERS_TABLE + " set time_value=current_timestamp");
jdbcTemplate.update(INSERT, filterValues(INSERT2_VALUES), filterTypes(INSERT_TYPES));
return null;
}
});
String sql = "select transaction_id from " + TestConstants.TEST_PREFIX
+ "data_event where transaction_id is not null group by transaction_id having count(*)>1";
String batchId = (String) jdbcTemplate.queryForObject(sql, String.class);
IDbDialect dbDialect = (IDbDialect) getSymmetricEngine().getApplicationContext().getBean(
Constants.DB_DIALECT);
if (dbDialect.supportsTransactionId()) {
Assert.assertNotNull(batchId);
}
}
@Test(groups = "continuous", dependsOnMethods = "validateTransactionFunctionailty")
public void testExcludedColumnsFunctionality() throws Exception {
IBootstrapService service = (IBootstrapService) getSymmetricEngine().getApplicationContext().getBean(
Constants.BOOTSTRAP_SERVICE);
// need to wait for a second to make sure enough time has passed so the update of the config
// table will have a greater timestamp than the audit table.
Thread.sleep(1000);
JdbcTemplate jdbcTemplate = getJdbcTemplate(getSymmetricEngine());
Assert.assertEquals(1, jdbcTemplate.update("update " + TestConstants.TEST_PREFIX
+ "trigger set excluded_column_names='BOOLEAN_VALUE', last_updated_time=current_timestamp "
+ TEST_TRIGGER_WHERE_CLAUSE));
service.syncTriggers();
IConfigurationService configService = (IConfigurationService) getSymmetricEngine().getApplicationContext()
.getBean(Constants.CONFIG_SERVICE);
Trigger trigger = configService.getTriggerFor(TEST_TRIGGERS_TABLE, TestConstants.TEST_ROOT_NODE_GROUP);
Assert.assertEquals(jdbcTemplate.queryForInt("select count(*) from " + TestConstants.TEST_PREFIX
+ "trigger_hist where trigger_id=" + trigger.getTriggerId() + " and inactive_time is null"), 1,
"We expected only one active record in the trigger_hist table for " + TEST_TRIGGERS_TABLE);
Assert.assertEquals(1, jdbcTemplate.update(INSERT, filterValues(INSERT2_VALUES), filterTypes(INSERT_TYPES)));
String csvString = getNextDataRow(getSymmetricEngine());
boolean match = csvString.endsWith(EXPECTED_INSERT2_CSV_ENDSWITH);
assert match : "Received " + csvString + ", Expected the string to end with " + EXPECTED_INSERT2_CSV_ENDSWITH;
}
@Test(groups = "continuous", dependsOnMethods = "testExcludedColumnsFunctionality")
public void testDisableTriggers() throws Exception {
JdbcTemplate jdbcTemplate = getJdbcTemplate(getSymmetricEngine());
getDbDialect(getSymmetricEngine()).disableSyncTriggers();
int count = jdbcTemplate.update(INSERT, filterValues(INSERT1_VALUES), filterTypes(INSERT_TYPES));
getDbDialect(getSymmetricEngine()).enableSyncTriggers();
assert count == 1;
String csvString = getNextDataRow(getSymmetricEngine());
boolean match = csvString.endsWith(EXPECTED_INSERT2_CSV_ENDSWITH);
assert match : "Received " + csvString + ", Expected the string to end with " + EXPECTED_INSERT2_CSV_ENDSWITH;
}
@Test(groups = "continuous", dependsOnMethods = "testDisableTriggers")
public void testTargetTableNameFunctionality() throws Exception {
final String TARGET_TABLE_NAME = "SOME_OTHER_TABLE_NAME";
IBootstrapService service = (IBootstrapService) getSymmetricEngine().getApplicationContext().getBean(
Constants.BOOTSTRAP_SERVICE);
// need to wait for a second to make sure enough time has passed so the update of the trigger
// table will have a greater timestamp than the audit table.
Thread.sleep(1000);
JdbcTemplate jdbcTemplate = getJdbcTemplate(getSymmetricEngine());
Assert.assertEquals(1, jdbcTemplate.update("update " + TestConstants.TEST_PREFIX
+ "trigger set target_table_name='" + TARGET_TABLE_NAME + "', last_updated_time=current_timestamp "
+ TEST_TRIGGER_WHERE_CLAUSE));
service.syncTriggers();
IConfigurationService configService = (IConfigurationService) getSymmetricEngine().getApplicationContext()
.getBean(Constants.CONFIG_SERVICE);
Trigger trigger = configService.getTriggerFor(TEST_TRIGGERS_TABLE, TestConstants.TEST_ROOT_NODE_GROUP);
Assert.assertEquals(jdbcTemplate.queryForInt("select count(*) from " + TestConstants.TEST_PREFIX
+ "trigger_hist where trigger_id=" + trigger.getTriggerId() + " and inactive_time is null"), 1,
"We expected only one active record in the trigger_hist table for " + TEST_TRIGGERS_TABLE);
Assert.assertEquals(1, jdbcTemplate.update(INSERT, filterValues(INSERT2_VALUES), filterTypes(INSERT_TYPES)));
String tableName = getNextDataRowTableName(getSymmetricEngine());
Assert.assertEquals(tableName, TARGET_TABLE_NAME, "Received " + tableName + ", Expected " + TARGET_TABLE_NAME);
}
private int[] filterTypes(int[] types) {
boolean isBooleanSupported = ! (getDbDialect() instanceof OracleDbDialect);
int[] filteredTypes = new int[types.length];
for (int i = 0; i < types.length; i++) {
if (types[i] == Types.BOOLEAN && ! isBooleanSupported) {
filteredTypes[i] = Types.INTEGER;
} else {
filteredTypes[i] = types[i];
}
}
return filteredTypes;
}
private Object[] filterValues(Object[] values) {
boolean isBooleanSupported = ! (getDbDialect() instanceof OracleDbDialect);
Object[] filteredValues = new Object[values.length];
for (int i = 0; i < values.length; i++) {
if (values[i] instanceof Boolean && ! isBooleanSupported) {
filteredValues[i] = ((Boolean) values[i]) ? new Integer(1) : new Integer(0);
} else {
filteredValues[i] = values[i];
}
}
return filteredValues;
}
private String getNextDataRow(SymmetricEngine engine) {
JdbcTemplate jdbcTemplate = getJdbcTemplate(engine);
return (String) jdbcTemplate
.queryForObject("select row_data from " + TestConstants.TEST_PREFIX
+ "data where data_id = (select max(data_id) from " + TestConstants.TEST_PREFIX + "data)",
String.class);
}
private String getNextDataRowTableName(SymmetricEngine engine) {
JdbcTemplate jdbcTemplate = getJdbcTemplate(engine);
return (String) jdbcTemplate
.queryForObject("select table_name from " + TestConstants.TEST_PREFIX
+ "data where data_id = (select max(data_id) from " + TestConstants.TEST_PREFIX + "data)",
String.class);
}
}