Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CVV_COLUMNS table integration test #16

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions dumper-integration-tests/redshift-tests/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
/*
* Copyright 2022 Google LLC
* Copyright 2013-2021 CompilerWorks
*
* 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.
*/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
}
}

plugins {
id 'java'
id "com.google.protobuf" version "0.8.18"
id 'com.adarshr.test-logger' version '3.0.0'
}

sourceCompatibility = 1.8
Expand All @@ -22,28 +35,30 @@ repositories {
}

dependencies {
implementation 'org.codehaus.groovy:groovy-all:3.0.5'
implementation 'org.testng:testng:7.5'
implementation 'org.codehaus.groovy:groovy-all:3.0.10'
implementation 'com.google.guava:guava:31.1-jre'
implementation 'junit:junit:4.13.2'
implementation 'org.slf4j:slf4j-api:2.0.0-alpha5'
implementation 'org.slf4j:slf4j-jdk14:2.0.0-alpha5'
implementation 'com.amazon.redshift:redshift-jdbc42:2.1.0.6'
implementation 'commons-io:commons-io:2.11.0'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'com.google.guava:guava:31.0.1-jre'
implementation 'com.opencsv:opencsv:5.6'
implementation 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
implementation 'com.google.protobuf:protobuf-java:3.20.1'
implementation 'org.apache.commons:commons-csv:1.9.0'
implementation 'com.google.truth:truth:1.1.3'
implementation 'com.google.truth.extensions:truth-java8-extension:1.1.3'
implementation 'com.amazon.redshift:redshift-jdbc42:2.1.0.7'
annotationProcessor 'com.google.auto.value:auto-value:1.9'
compileOnly 'com.google.auto.value:auto-value-annotations:1.9'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have typically set dependency versions globally using a Gradle build convention. Can we please follow that convention for these submodule(s)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of right now, this test automation framework isn't included as a submodule into the main project.
There are several reasons for that:

  1. We don't want to overlap in dependencies or cross-dependencies
  2. We use different libs (OpenCSV and Common-CSV)
  3. We may have different core level test frameworks - TestNG and Junit
  4. We may have different reporting and logging implementations
  5. Integration tests, in accordance with industry best practices, are considered to be a separate project and it doesn't necessarily have to be integrated with the code of the product. If that were Component or Unit tests, then yes, it's a valid scenario for submoduling and bundling.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2) and (3) are review comments elsewhere; our convention is to use commons-csv and junit.
(4) should be a review comment; we would like this code to follow the conventions of the repository.
(5) Gradle would normally represent an integration test as a TestSet, and this practice is sufficiently standard that it is now part of Gradle core.

}

test {
useTestNG {
preserveOrder true
ignoreFailures = true

useJUnit() {
systemProperty 'java.util.logging.config.file', 'src/main/resources/logging.properties'
}
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.20.1'
}
def envFiles = ['EXPORT_PATH']
envFiles.each { if (System.env[it] != null) inputs.dir(System.env[it]) }

def envProperties = ['DB_URL', 'USERNAME', 'PASSWORD']
envProperties.each { if (System.env[it] != null) inputs.property it, System.env[it] }
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2022 Google LLC
*
* 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 com.google.edwmigration.dumper.base;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;

import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multisets;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Base class with general values for all TestNG test suites */
public abstract class TestBase {

private static final Logger LOGGER = LoggerFactory.getLogger(TestBase.class);

/**
* @param dbMultiset List of extracted from DB items.
* @param csvMultiset List of uploaded from Avro items.
* <p>This custom method exists due to Java out of heap memory error in
* Truth.assertThat(dbMultiset).containsExactlyElementsIn(csvMultiset); It probably happens
* because .containsExactlyElementsIn() tries to print out not only the diff, let's say 1
* element, but entire collections.
*/
public static void assertDbCsvDataEqual(
LinkedHashMultiset<?> dbMultiset, LinkedHashMultiset<?> csvMultiset) {
Multiset<?> dbReducedOnCsv = Multisets.difference(dbMultiset, csvMultiset);
Multiset<?> csvReducedOnDb = Multisets.difference(csvMultiset, dbMultiset);

String dbDiffEntries =
dbReducedOnCsv.stream().map(e -> e.toString() + "\n").collect(toList()).toString();
String csvDiffEntries =
csvReducedOnDb.stream().map(e -> e.toString() + "\n").collect(toList()).toString();

if (dbReducedOnCsv.isEmpty() && csvReducedOnDb.isEmpty()) {
LOGGER.info("DB view and CSV file are equal");
} else if (!dbReducedOnCsv.isEmpty() && !csvReducedOnDb.isEmpty()) {
Assert.fail(
format(
"DB view and CSV file have mutually exclusive row(s)%n"
+ "DB view has %d different row(s): %s%n"
+ "CSV file has %d different row(s): %s",
dbReducedOnCsv.size(), dbDiffEntries, csvReducedOnDb.size(), csvDiffEntries));
} else if (!dbReducedOnCsv.isEmpty()) {
Assert.fail(format("DB view has %d extra row(s):%n%s", dbReducedOnCsv.size(), dbDiffEntries));
} else if (!csvReducedOnDb.isEmpty()) {
Assert.fail(
format("CSV file has %d extra row(s):%n%s", csvReducedOnDb.size(), csvDiffEntries));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright 2022 Google LLC
* Copyright 2013-2021 CompilerWorks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,24 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.base;
package com.google.edwmigration.dumper.base;

import static java.lang.System.getenv;

import java.util.regex.Pattern;

/**
* Stores constants common among all tests.
*/
/** Stores constants common among all tests. */
public final class TestConstants {

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

public static final String ET_OUTPUT_PATH = getenv("EXPORT_PATH");
public static final Pattern TRAILING_SPACES_REGEX = Pattern.compile("\\s+$");
public static final String EXPORTED_FILES_BASE_PATH = getenv("EXPORT_PATH");
paolomorandini marked this conversation as resolved.
Show resolved Hide resolved

public static final String SQL_REQUESTS_BASE_PATH = "sql/";

private TestConstants() {
}
private TestConstants() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 Google LLC
*
* 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 com.google.edwmigration.dumper.jdbc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* A helper class for checking Null values returned by executing SELECT request against a database.
*/
public final class JdbcUtil {

private JdbcUtil() {}

/**
* @param rs A row with SELECT results.
* @param column Database column name.
* @return String or an empty string if null.
*/
public static String getStringNotNull(ResultSet rs, String column) throws SQLException {
String string = rs.getString(column);
return rs.wasNull() ? "" : string;
}

/**
* @param rs A row with SELECT results.
* @param columnIndex Database column index.
* @return String or an empty string if null.
*/
public static String getStringNotNull(ResultSet rs, int columnIndex) throws SQLException {
String string = rs.getString(columnIndex);
return rs.wasNull() ? "" : string;
}

/**
* @param rsmd Metadata of the executed SQL query.
* @return List of column names.
* @throws SQLException
*/
public static List<String> getDbColumnNames(ResultSetMetaData rsmd) throws SQLException {
List<String> columnNames = new ArrayList<>();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
columnNames.add(rsmd.getColumnName(i));
}
return columnNames;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright 2022 Google LLC
* Copyright 2013-2021 CompilerWorks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,16 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.sql;
package com.google.edwmigration.dumper.sql;

import static com.google.base.TestConstants.URL_DB;
import static com.google.edwmigration.dumper.base.TestConstants.URL_DB;
import static java.lang.String.format;
import static org.apache.commons.io.FileUtils.readFileToString;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.File;
import com.google.common.io.Resources;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
Expand All @@ -32,26 +29,19 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A helper class for reading .sql files.
*/
/** A helper class for reading .sql files. */
public final class SqlUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(SqlUtil.class);

private SqlUtil() {
}
private SqlUtil() {}

/**
* @param sqlPath Path to an .sql file.
* @return File contents, never null.
*/
public static String getSql(String sqlPath) {
try {
return readFileToString(new File(sqlPath), StandardCharsets.UTF_8);
} catch (IOException exception) {
throw new UncheckedIOException(format("Error while reading sql file %s", sqlPath), exception);
}
public static String getSql(String sqlPath) throws IOException {
return Resources.toString(Resources.getResource(sqlPath), UTF_8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Copyright 2022 Google LLC
--
-- 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.

SELECT * FROM SVV_COLUMNS;
Loading