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

Add thorough database metadata test #42

Closed
wants to merge 5 commits into from
Closed
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
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<slf4j.version>1.7.36</slf4j.version>
<testcontainers.version>1.19.2</testcontainers.version>
<astra-sdk.version>0.6.11</astra-sdk.version>
<schemacrawler.version>16.20.6</schemacrawler.version>
<!-- Versions for plugins -->
<maven-checkstyle-plugin.version>3.3.0</maven-checkstyle-plugin.version>
<maven-clean-plugin.version>3.3.1</maven-clean-plugin.version>
Expand Down Expand Up @@ -276,6 +277,26 @@
<version>${lombok.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>us.fatehi</groupId>
<artifactId>schemacrawler</artifactId>
<version>${schemacrawler.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>us.fatehi</groupId>
<artifactId>schemacrawler-api</artifactId>
<version>${schemacrawler.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>us.fatehi</groupId>
<artifactId>schemacrawler-tools</artifactId>
<version>${schemacrawler.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
101 changes: 101 additions & 0 deletions src/test/java/com/ing/data/cassandra/jdbc/CompleteMetadataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

package com.ing.data.cassandra.jdbc;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static schemacrawler.test.utility.ExecutableTestUtility.executableExecution;
import static schemacrawler.test.utility.FileHasContent.classpathResource;
import static schemacrawler.test.utility.FileHasContent.hasSameContentAs;
import static schemacrawler.test.utility.FileHasContent.outputOf;
import java.net.InetSocketAddress;
import java.util.regex.Pattern;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import schemacrawler.inclusionrule.IncludeAll;
import schemacrawler.schemacrawler.InfoLevel;
import schemacrawler.schemacrawler.LimitOptionsBuilder;
import schemacrawler.schemacrawler.LoadOptionsBuilder;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder;
import schemacrawler.schemacrawler.SchemaInfoLevelBuilder;
import schemacrawler.tools.command.text.schema.options.SchemaTextOptions;
import schemacrawler.tools.command.text.schema.options.SchemaTextOptionsBuilder;
import schemacrawler.tools.executable.SchemaCrawlerExecutable;
import us.fatehi.utility.datasource.DatabaseConnectionSource;
import us.fatehi.utility.datasource.DatabaseConnectionSources;
import us.fatehi.utility.datasource.MultiUseUserCredentials;

/****
* Runs extensive database metadata tests.
****/
public class CompleteMetadataTest extends UsingCassandraContainerTest {

private DatabaseConnectionSource dbConnectionSource;

/****
* Sets up a data source to connect to the Cassandra database.
*
****/
@BeforeEach
public void createDatabase() {

if (!cassandraContainer.isRunning()) {
fail("Testcontainer for database is not available");
}

final InetSocketAddress contactPoint = cassandraContainer.getContactPoint();
final String host = contactPoint.getHostName();
final int port = contactPoint.getPort();
final String keyspace = "test_keyspace";
final String localDatacenter = cassandraContainer.getLocalDatacenter();
final String connectionUrl = String.format("jdbc:cassandra://%s:%d/%s?localdatacenter=%s",
host, port, keyspace, localDatacenter);
createDataSource(connectionUrl);
}

/****
* Uses SchemaCrawler to obtain database metadata in "maximum" mode. Compares actual results to
* an expected file with results. The test will produce a new expected results file if the
* actual and expected results do not match.
*
* @throws Exception on an error when running the test
****/
@Test
public void givenDatabase_whenCompleteMetadataExtracted_shouldMatchExpectedOutput()
throws Exception {

final LimitOptionsBuilder limitOptionsBuilder =
LimitOptionsBuilder.builder().includeSchemas(Pattern.compile("test_keyspace.*"))
.includeRoutines(new IncludeAll());
final SchemaInfoLevelBuilder schemaInfoLevelBuilder =
SchemaInfoLevelBuilder.builder().withInfoLevel(InfoLevel.maximum);
final LoadOptionsBuilder loadOptionsBuilder =
LoadOptionsBuilder.builder().withSchemaInfoLevelBuilder(schemaInfoLevelBuilder);
final SchemaCrawlerOptions schemaCrawlerOptions = SchemaCrawlerOptionsBuilder
.newSchemaCrawlerOptions().withLimitOptions(limitOptionsBuilder.toOptions())
.withLoadOptions(loadOptionsBuilder.toOptions());
final SchemaTextOptionsBuilder textOptionsBuilder = SchemaTextOptionsBuilder.builder();
textOptionsBuilder.showDatabaseInfo().showJdbcDriverInfo();
final SchemaTextOptions textOptions = textOptionsBuilder.toOptions();

final SchemaCrawlerExecutable executable = new SchemaCrawlerExecutable("details");
executable.setSchemaCrawlerOptions(schemaCrawlerOptions);
executable.setAdditionalConfiguration(
SchemaTextOptionsBuilder.builder(textOptions).toConfig());

final String expectedResource = "expected_metadata_output.txt";
assertThat(outputOf(executableExecution(getDataSource(), executable)),
hasSameContentAs(classpathResource(expectedResource)));
}

private void createDataSource(final String connectionUrl) {
dbConnectionSource = DatabaseConnectionSources.newDatabaseConnectionSource(connectionUrl,
new MultiUseUserCredentials(cassandraContainer.getUsername(),
cassandraContainer.getPassword()));

}

private DatabaseConnectionSource getDataSource() {
return dbConnectionSource;
}
}
Loading
Loading