Skip to content

Commit 8d6aeaa

Browse files
committed
CVV_COLUMNS table integration test
1. Added JdbcUtils and methods 2. Added svv_columns.sql for retrieving table's data 3. Added SvvColumnsTest test 4. Added SvvColumnsRow POJO object 5. Added CsvUtil and methods 6. Added Gradle reporting plugin
1 parent d9b385a commit 8d6aeaa

File tree

11 files changed

+553
-173
lines changed

11 files changed

+553
-173
lines changed

dumper-integration-tests/redshift-tests/build.gradle

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ buildscript {
22
repositories {
33
mavenCentral()
44
}
5-
dependencies {
6-
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
7-
}
85
}
96

107
plugins {
118
id 'java'
12-
id "com.google.protobuf" version "0.8.18"
9+
id 'com.adarshr.test-logger' version '3.2.0'
1310
}
1411

1512
sourceCompatibility = 1.8
@@ -22,28 +19,24 @@ repositories {
2219
}
2320

2421
dependencies {
25-
implementation 'org.codehaus.groovy:groovy-all:3.0.5'
22+
implementation 'org.codehaus.groovy:groovy-all:3.0.10'
23+
implementation 'com.google.guava:guava:31.1-jre'
2624
implementation 'org.testng:testng:7.5'
2725
implementation 'org.slf4j:slf4j-api:2.0.0-alpha5'
2826
implementation 'org.slf4j:slf4j-jdk14:2.0.0-alpha5'
29-
implementation 'com.amazon.redshift:redshift-jdbc42:2.1.0.6'
27+
implementation 'com.amazon.redshift:redshift-jdbc42:2.1.0.7'
3028
implementation 'commons-io:commons-io:2.11.0'
3129
implementation 'org.apache.commons:commons-collections4:4.4'
32-
implementation 'com.google.guava:guava:31.0.1-jre'
3330
implementation 'com.opencsv:opencsv:5.6'
34-
implementation 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
35-
implementation 'com.google.protobuf:protobuf-java:3.20.1'
31+
annotationProcessor 'com.google.auto.value:auto-value:1.9'
32+
compileOnly 'com.google.auto.value:auto-value-annotations:1.9'
3633
}
3734

3835
test {
36+
ignoreFailures = true
37+
3938
useTestNG {
4039
preserveOrder true
4140
systemProperty 'java.util.logging.config.file', 'src/main/resources/logging.properties'
4241
}
43-
}
44-
45-
protobuf {
46-
protoc {
47-
artifact = 'com.google.protobuf:protoc:3.20.1'
48-
}
4942
}

dumper-integration-tests/redshift-tests/src/main/java/com/google/base/TestBase.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.edwmigration.dumper.base;
17+
18+
import static java.lang.String.format;
19+
import static java.lang.System.lineSeparator;
20+
21+
import com.google.common.base.Joiner;
22+
import com.google.common.collect.LinkedHashMultiset;
23+
import com.google.edwmigration.dumper.pojo.SvvColumnsRow;
24+
import com.opencsv.CSVParser;
25+
import com.opencsv.CSVParserBuilder;
26+
import org.junit.Assert;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
/** Base class with general values for all TestNG test suites */
31+
public abstract class TestBase {
32+
33+
public static final CSVParser CSV_PARSER = new CSVParserBuilder().withEscapeChar('\0').build();
34+
private static final Logger LOGGER = LoggerFactory.getLogger(TestBase.class);
35+
36+
/**
37+
* @param dbMultiset List of extracted from DB items
38+
* @param csvMultiset List of uploaded from Avro items
39+
*/
40+
public static void assertMultisetsEqual(
41+
LinkedHashMultiset dbMultiset, LinkedHashMultiset csvMultiset) {
42+
LinkedHashMultiset<SvvColumnsRow> dbMultisetCopy = LinkedHashMultiset.create(dbMultiset);
43+
csvMultiset.forEach(dbMultiset::remove);
44+
dbMultisetCopy.forEach(csvMultiset::remove);
45+
46+
String dbListForLogs = lineSeparator() + Joiner.on("").join(dbMultiset);
47+
String csvListForLogs = lineSeparator() + Joiner.on("").join(csvMultiset);
48+
49+
if (dbMultiset.isEmpty() && csvMultiset.isEmpty()) {
50+
LOGGER.info("DB view and CSV file are equal");
51+
} else if (!dbMultiset.isEmpty() && !csvMultiset.isEmpty()) {
52+
Assert.fail(
53+
format(
54+
"DB view and CSV file have mutually exclusive row(s)%n"
55+
+ "DB view has %d different row(s): %s%n"
56+
+ "CSV file has %d different row(s): %s",
57+
dbMultiset.size(), dbListForLogs, csvMultiset.size(), csvListForLogs));
58+
} else if (!dbMultiset.isEmpty()) {
59+
Assert.fail(format("DB view has %d extra row(s):%n%s", dbMultiset.size(), dbListForLogs));
60+
} else if (!csvMultiset.isEmpty()) {
61+
Assert.fail(format("CSV file has %d extra row(s):%n%s", csvMultiset.size(), csvListForLogs));
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
* Copyright 2022 Google LLC
3-
* Copyright 2013-2021 CompilerWorks
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -14,24 +13,24 @@
1413
* See the License for the specific language governing permissions and
1514
* limitations under the License.
1615
*/
17-
package com.google.base;
16+
package com.google.edwmigration.dumper.base;
1817

1918
import static java.lang.System.getenv;
2019

2120
import java.util.regex.Pattern;
2221

23-
/**
24-
* Stores constants common among all tests.
25-
*/
22+
/** Stores constants common among all tests. */
2623
public final class TestConstants {
2724

2825
public static final String URL_DB = getenv("DB_URL");
2926
public static final String USERNAME_DB = getenv("USERNAME");
3027
public static final String PASSWORD_DB = getenv("PASSWORD");
3128

32-
public static final String ET_OUTPUT_PATH = getenv("EXPORT_PATH");
29+
public static final String EXPORTED_FILES_BASE_PATH = getenv("EXPORT_PATH");
30+
31+
public static final String SQL_REQUESTS_BASE_PATH = "sql/";
32+
3333
public static final Pattern TRAILING_SPACES_REGEX = Pattern.compile("\\s+$");
3434

35-
private TestConstants() {
36-
}
35+
private TestConstants() {}
3736
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.edwmigration.dumper.csv;
17+
18+
import static com.google.edwmigration.dumper.base.TestConstants.TRAILING_SPACES_REGEX;
19+
import static java.lang.Integer.parseInt;
20+
21+
/** A helper class for reading and extracting data from CSV files. */
22+
public final class CsvUtil {
23+
24+
private CsvUtil() {}
25+
26+
/**
27+
* @return String or an empty string if null.
28+
*/
29+
public static String getStringNotNull(String value) {
30+
return value == null ? "" : TRAILING_SPACES_REGEX.matcher(value).replaceFirst("");
31+
}
32+
33+
/**
34+
* @return int or 0 if "".
35+
*/
36+
public static int getIntNotNull(String value) {
37+
return getStringNotNull(value).equals("") ? 0 : parseInt(value);
38+
}
39+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.edwmigration.dumper.jdbc;
17+
18+
import static com.google.edwmigration.dumper.base.TestConstants.TRAILING_SPACES_REGEX;
19+
20+
import java.math.BigDecimal;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
import java.sql.Timestamp;
24+
import java.time.ZonedDateTime;
25+
import java.util.Calendar;
26+
import java.util.TimeZone;
27+
28+
/**
29+
* A helper class for checking Null values returned by executing SELECT request against a database.
30+
*/
31+
public final class JdbcUtil {
32+
33+
private JdbcUtil() {}
34+
35+
/**
36+
* @param rs A row with SELECT results.
37+
* @param column Database column name.
38+
* @return String or an empty string if null.
39+
*/
40+
public static String getStringNotNull(ResultSet rs, String column) throws SQLException {
41+
String string = rs.getString(column);
42+
return rs.wasNull() ? "" : TRAILING_SPACES_REGEX.matcher(string).replaceFirst("");
43+
}
44+
45+
/**
46+
* @param rs A row with SELECT results.
47+
* @param column Database column name.
48+
* @return int or 0 if null.
49+
*/
50+
public static int getIntNotNull(ResultSet rs, String column) throws SQLException {
51+
return rs.getInt(column);
52+
}
53+
54+
/**
55+
* @param rs A row with SELECT results.
56+
* @param column Database column name.
57+
* @return long or 0L if null.
58+
*/
59+
public static long getLongNotNull(ResultSet rs, String column) throws SQLException {
60+
return rs.getLong(column);
61+
}
62+
63+
/**
64+
* @param rs A row with SELECT results.
65+
* @param column Database column name.
66+
* @return byte[] or empty byte[] if null.
67+
*/
68+
public static byte[] getBytesNotNull(ResultSet rs, String column) throws SQLException {
69+
try {
70+
byte[] bytesValue = rs.getBytes(column);
71+
return rs.wasNull() ? new byte[0] : bytesValue;
72+
} catch (SQLException e) {
73+
BigDecimal bigDecimal = rs.getBigDecimal(column);
74+
return rs.wasNull() ? new byte[0] : bigDecimal.toBigInteger().toByteArray();
75+
}
76+
}
77+
78+
/**
79+
* @param rs A row with SELECT results.
80+
* @param column Database column name.
81+
* @return double or 0.0 if null.
82+
*/
83+
public static double getDoubleNotNull(ResultSet rs, String column) throws SQLException {
84+
return rs.getDouble(column);
85+
}
86+
87+
/**
88+
* @param rs A row with SELECT results.
89+
* @param column Database column name.
90+
* @return long or 0L if null.
91+
*/
92+
public static long getTimestampNotNull(ResultSet rs, String column) throws SQLException {
93+
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
94+
Timestamp timestamp = rs.getTimestamp(column, cal);
95+
if (rs.wasNull()) {
96+
return 0L;
97+
}
98+
return Timestamp.from(
99+
ZonedDateTime.of(timestamp.toLocalDateTime(), cal.getTimeZone().toZoneId()).toInstant())
100+
.getTime();
101+
}
102+
}

0 commit comments

Comments
 (0)