aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-server/src/test/java/org/onap/music/mdbc/test/CrossSiteTest.java
blob: d4a7a27782fc35157a25d87bc1297335bfc10ac7 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * ============LICENSE_START====================================================
 * org.onap.music.mdbc
 * =============================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * =============================================================================
 * 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.
 * ============LICENSE_END======================================================
 */
package org.onap.music.mdbc.test;

import static org.junit.Assert.*;

import java.io.Reader;
import java.io.StringReader;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Random;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


/**
 * This test tests a copy of data from DB1 to DB2.  It tests the following H2 data types:
 * VARCHAR, VARBINARY, INTEGER, BOOLEAN, DOUBLE, CLOB, TIMESTAMP.
 */
public class CrossSiteTest extends TestCommon {
	private static final String DB_CONNECTION1 = "avatica://" + "mem:db1";
	private static final String DB_CONNECTION2 = "avatica://" + "mem:db2";
	private static final String KEYSPACE       = "CrossSite_Test";
	private final static Logger logger = Logger.getLogger(CrossSiteTest.class);

	private Connection db1, db2;

	//@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		// drop the keyspace
	}

	//@Before
	public void setUp() throws Exception {
		db1 = getDBConnection(DB_CONNECTION1, KEYSPACE, "0");
		db2 = getDBConnection(DB_CONNECTION2, KEYSPACE, "1");
	}

	//@After
	public void tearDown() throws Exception {
		db1.close();
		db2.close();
	}

	//@Test
	public void testCopyOneToTwo() {
		String sql = "CREATE TABLE IF NOT EXISTS DATA(KEY VARCHAR(255), PRIMARY KEY (KEY))";
		createTable(sql);

		// Put data in DB1
		try {
			Statement s = db1.createStatement();
			s.execute("INSERT INTO DATA(KEY) VALUES('AAA')");
			s.execute("INSERT INTO DATA(KEY) VALUES('BBB')");
			s.execute("INSERT INTO DATA(KEY) VALUES('CCC')");
			s.execute("INSERT INTO DATA(KEY) VALUES('DDD')");
			db1.commit();
			s.close();
		} catch (Exception e) {
			fail("1: " + e.toString());
		}
		// Get data in DB2
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM DATA");
			if (rs.next()) {
				int n = rs.getInt(1);
				assertEquals(4, n);
			} else {
				fail("SELECT COUNT(*) produced no result");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			fail("2: " + e.toString());
		}
		// Delete a row
		try {
			Statement s = db1.createStatement();
			s.execute("DELETE FROM DATA WHERE KEY = 'CCC'");
			db1.commit();
			s.close();
		} catch (Exception e) {
			fail("1: " + e.toString());
		}
		// Recheck
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM DATA");
			if (rs.next()) {
				int n = rs.getInt(1);
				assertEquals(3, n);
			} else {
				fail("SELECT COUNT(*) produced no result");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			fail("2: " + e.toString());
		}
		System.out.println("CrossSiteTest.testCopyOneToTwo OK");
	}

	//@Test
	public void testCopyWithPreparedStatement() {
		String sql = "CREATE TABLE IF NOT EXISTS DATA2(KEY VARCHAR(255), PRIMARY KEY (KEY))";
		createTable(sql);

		// Put data in DB1
		try {
			Statement s = db1.createStatement();
			PreparedStatement ps =  db1.prepareStatement("INSERT INTO DATA2(KEY) VALUES(?)");
			for (String v : new String[] { "WWW", "XXX", "YYY", "ZZZ" } ) {
				ps.setString(1, v);
				ps.execute();
			}
			db1.commit();
			s.close();
		} catch (Exception e) {
			fail("1: " + e.toString());
		}
		// Get data in DB2
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM DATA2");
			if (rs.next()) {
				int n = rs.getInt(1);
				assertEquals(4, n);
			} else {
				fail("SELECT COUNT(*) produced no result");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			fail("2: " + e.toString());
		}
		System.out.println("CrossSiteTest.testCopyWithPreparedStatement OK");
	}

	//@Test
	public void testDataTypes() {
		String sql = "CREATE TABLE IF NOT EXISTS DATATYPES(KEY VARCHAR(255), I1 INTEGER, B1 BOOLEAN, D1 DOUBLE, S1 VARCHAR, PRIMARY KEY (KEY))";
		createTable(sql);

		String key  = "ThIs Is ThE KeY";
		String key2 = "ThIs Is another KeY";
		String s1 = "The Rain in Spain";
		int i1 = 696969;
		boolean b1 = true;
		double pi = Math.PI;
		double e  = Math.E;

		// Put data in DB1
		try {
			PreparedStatement ps =  db1.prepareStatement("INSERT INTO DATATYPES(KEY, I1, B1, D1, S1) VALUES(?, ?, ?, ?, ?)");
			ps.setString(1, key);
			ps.setInt(2, i1);
			ps.setBoolean(3, b1);
			ps.setDouble(4, pi);
			ps.setString(5, s1);
			ps.execute();

			ps.setString(1, key2);
			ps.setInt(2, 123456);
			ps.setBoolean(3, false);
			ps.setDouble(4, e);
			ps.setString(5, "Fee fi fo fum!");
			ps.execute();
			db1.commit();
			ps.close();
		} catch (Exception ex) {
			fail("1: " + ex.toString());
		}
		// Get data in DB2
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT * FROM DATATYPES");
			if (rs.next()) {
				assertEquals(key, rs.getString(1));
				assertEquals(i1,  rs.getInt(2));
				assertEquals(b1,  rs.getBoolean(3));
				assertEquals(pi,  rs.getDouble(4), 0.0);
				assertEquals(s1,  rs.getString(5));
			} else {
				fail("SELECT * FROM DATATYPES");
			}
		} catch (Exception ex) {
			logger.error(ex);
			ex.printStackTrace();
			fail("2: " + ex.toString());
		}
		System.out.println("CrossSiteTest.testDataTypes OK");
	}

	//@Test
	public void testIdentityColumn() {
		String sql = "CREATE TABLE IF NOT EXISTS IDENTITYTEST(KEY IDENTITY, S1 VARCHAR, T1 TIMESTAMP, PRIMARY KEY (KEY))";
		createTable(sql);

		String s1  = "ThIs Is ThE IDENTITY test";
		Timestamp ts = new Timestamp(-3535344000L);

		// Put data in DB1
		try {
			PreparedStatement ps =  db1.prepareStatement("INSERT INTO IDENTITYTEST(S1, T1) VALUES(?, ?)");
			ps.setString(1, s1);
			ps.setTimestamp(2, ts);
			ps.execute();
			db1.commit();
			ps.close();
		} catch (Exception ex) {
			fail("testIdentity 1: " + ex.toString());
		}
		// Get data in DB2
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT * FROM IDENTITYTEST");
			if (rs.next()) {
				assertEquals(s1, rs.getString("s1"));
				assertEquals(ts, rs.getTimestamp("t1"));
			} else {
				fail("SELECT * FROM DATATYPES");
			}
		} catch (Exception ex) {
			logger.error(ex);
			ex.printStackTrace();
			fail("testIdentity 2: " + ex.toString());
		}
		System.out.println("CrossSiteTest.testIdentityColumn OK");
	}

	//@Test
	public void testBLOBColumn() {
		String sql = "CREATE TABLE IF NOT EXISTS BLOBTEST (KEY VARCHAR, V1 VARBINARY, C1 CLOB, PRIMARY KEY (KEY))";// add
		createTable(sql);

		String key = "BLOB test";
		byte[] v1 = new byte[4096];
		new Random().nextBytes(v1);
		String constitution =
			"We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defense, promote the "+
			"general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America."+
			"Section 1"+
			"All legislative Powers herein granted shall be vested in a Congress of the United States, which shall consist of a Senate and House of Representatives."+
			""+
			"Section 2"+
			"1: The House of Representatives shall be composed of Members chosen every second Year by the People of the several States, and the Electors in each State shall "+
			"have the Qualifications requisite for Electors of the most numerous Branch of the State Legislature."+
			""+
			"2: No Person shall be a Representative who shall not have attained to the Age of twenty five Years, and been seven Years a Citizen of the United States, "+
			"and who shall not, when elected, be an Inhabitant of that State in which he shall be chosen."+
			""+
			"3: Representatives and direct Taxes shall be apportioned among the several States which may be included within this Union, according to their respective Numbers, which shall be determined "+
			"by adding to the whole Number of free Persons, including those bound to Service for a Term of Years, and excluding Indians not taxed, three fifths of all other Persons. "+
			"2  The actual Enumeration shall be made within three Years after the first Meeting of the Congress of the United States, and within every subsequent Term of ten Years, in such Manner as "+
			"they shall by Law direct. The Number of Representatives shall not exceed one for every thirty Thousand, but each State shall have at Least one Representative; and until such enumeration "+
			"shall be made, the State of New Hampshire shall be entitled to chuse three, Massachusetts eight, Rhode-Island and Providence Plantations one, Connecticut five, New-York six, New Jersey four, "+
			"Pennsylvania eight, Delaware one, Maryland six, Virginia ten, North Carolina five, South Carolina five, and Georgia three."+
			""+
			"4: When vacancies happen in the Representation from any State, the Executive Authority thereof shall issue Writs of Election to fill such Vacancies."+
			""+
			"5: The House of Representatives shall chuse their Speaker and other Officers; and shall have the sole Power of Impeachment."+
			"etc., etc. ...";
		Reader c1 = new StringReader(constitution);

		// Put data in DB1
		try {
			CallableStatement ps =  db1.prepareCall("INSERT INTO BLOBTEST(KEY, V1, C1) VALUES (?, ?, ?)");
			ps.setString(1, key);
			ps.setBytes(2, v1);
			ps.setClob(3, c1);
			ps.execute();
			db1.commit();
			ps.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			fail("testBLOBColumn 1: " + ex.toString());
		}
		// Get data in DB2
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT * FROM BLOBTEST");
			if (rs.next()) {
				String v1s = new String(v1);
				assertEquals(key, rs.getString("key"));
				assertEquals(v1s, new String(rs.getBytes("v1")));
				assertEquals(constitution, new String(rs.getBytes("c1")));
			} else {
				fail("SELECT * FROM BLOBTEST");
			}
		} catch (Exception ex) {
			logger.error(ex);
			ex.printStackTrace();
			fail("testBLOBColumn 2: " + ex.toString());
		}
		System.out.println("CrossSiteTest.testBLOBColumn OK");
	}

	//@Test
	public void testSecondaryIndex() {
		String sql = "CREATE TABLE IF NOT EXISTS ARTISTS (ARTIST VARCHAR, GENRE VARCHAR, AGE INT, PRIMARY KEY (ARTIST))";
		createTable(sql);

		// Put data in DB1
		try {
			Statement s = db1.createStatement();
			s.execute("INSERT INTO ARTISTS(ARTIST, GENRE, AGE) VALUES('Anne-Sophie', 'classical', 53)");
			s.execute("INSERT INTO ARTISTS(ARTIST, GENRE, AGE) VALUES('Dizz', 'jazz', 99)");
			s.execute("INSERT INTO ARTISTS(ARTIST, GENRE, AGE) VALUES('Esperanza', 'jazz', 32)");
			s.execute("INSERT INTO ARTISTS(ARTIST, GENRE, AGE) VALUES('Miles', 'jazz', 90)");
			s.execute("INSERT INTO ARTISTS(ARTIST, GENRE, AGE) VALUES('Yo-yo', 'classical', 61)");
			s.execute("CREATE INDEX BYGENRE on ARTISTS(GENRE)");
			db1.commit();
			s.close();
		} catch (Exception e) {
			fail("1: " + e.toString());
		}
		// Get data in DB2
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM ARTISTS WHERE GENRE = 'jazz'");
			if (rs.next()) {
				int n = rs.getInt(1);
				assertEquals(3, n);
			} else {
				fail("SELECT COUNT(*) produced no result");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			fail("2: " + e.toString());
		}
		// Delete a row
		try {
			Statement s = db1.createStatement();
			s.execute("DELETE FROM ARTISTS WHERE ARTIST = 'Miles'");
			db1.commit();
			s.close();
		} catch (Exception e) {
			fail("1: " + e.toString());
		}
		// Recheck
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM ARTISTS WHERE GENRE = 'jazz'");
			if (rs.next()) {
				int n = rs.getInt(1);
				assertEquals(2, n);
			} else {
				fail("SELECT COUNT(*) produced no result");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			fail("2: " + e.toString());
		}
		System.out.println("CrossSiteTest.testSecondaryIndex OK");
	}

	//@Test
	public void testUpdate() {
		String sql = "CREATE TABLE IF NOT EXISTS UPDATETEST(KEY VARCHAR(255), OTHER VARCHAR(255), PRIMARY KEY (KEY))";
		createTable(sql);

		// Put data in DB1
		try {
			Statement s = db1.createStatement();
			s.execute("INSERT INTO UPDATETEST(KEY, OTHER) VALUES('foo', 'bar')");
			s.execute("INSERT INTO UPDATETEST(KEY, OTHER) VALUES('bar', 'nixon')");
			db1.commit();
			s.close();
		} catch (Exception e) {
			fail("1: " + e.toString());
		}
		// Get data in DB2
		logger.info("    Get data in DB2");
		try {
			Statement s = db2.createStatement();
			ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM UPDATETEST");
			if (rs.next()) {
				int n = rs.getInt(1);
				assertEquals(2, n);
			} else {
				fail("SELECT COUNT(*) produced no result");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			fail("2: " + e.toString());
		}
		// Update a row
		try {
			Statement s = db2.createStatement();
			s.execute("UPDATE UPDATETEST SET OTHER = 'obama' WHERE KEY = 'bar'");
			db2.commit();
			s.close();
		} catch (Exception e) {
			fail("1: " + e.toString());
		}
		// Recheck
		logger.info("    Get data in DB2");
		try {
			Statement s = db1.createStatement();
			ResultSet rs = s.executeQuery("SELECT OTHER FROM UPDATETEST WHERE KEY = 'bar'");
			if (rs.next()) {
				String str = rs.getString("OTHER");
				assertEquals("obama", str);
			} else {
				fail("SELECT OTHER produced no result");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			fail("2: " + e.toString());
		}
		System.out.println("CrossSiteTest.testUpdate OK");
	}

	private void createTable(String sql) {
		try {
			for (Connection db : new Connection[] { db1, db2 }) {
				logger.info("    start: "+db);
				Statement s = db.createStatement();
				s.execute(sql);
				db.commit();
				s.close();
				logger.info("    Tables created");
			}
		} catch (Exception e) {
			fail(e.toString());
		}
	}
}