This is an automated email from the ASF dual-hosted git repository. agingade pushed a commit to branch feature/GEODE-4947 in repository https://gitbox.apache.org/repos/asf/geode.git
commit a2b6962d1092ff8b4fb582000b08a65ab9601edc Author: Nick Reich <nre...@pivotal.io> AuthorDate: Tue Mar 27 10:00:02 2018 -0700 GEODE-4947: Add test using MySQL for JDBC connector * Add rule for standing up Docker instance of MySQL to enable testing --- build.gradle | 1 + geode-connectors/build.gradle | 9 ++ .../TableMetaDataManagerMySqlIntegrationTest.java | 113 +++++++++++++++++++ .../test/junit/rules/DatabaseConnectionRule.java | 121 +++++++++++++++++++++ .../test/junit/rules/MySqlConnectionRule.java | 44 ++++++++ .../src/test/resources/docker/mysql.yml | 12 ++ 6 files changed, 300 insertions(+) diff --git a/build.gradle b/build.gradle index cd76ede..c3f1604 100755 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } + maven { url "https://dl.bintray.com/palantir/releases" } jcenter() } diff --git a/geode-connectors/build.gradle b/geode-connectors/build.gradle index c2b55ba..432fbd5 100644 --- a/geode-connectors/build.gradle +++ b/geode-connectors/build.gradle @@ -15,15 +15,24 @@ * limitations under the License. */ +repositories { + maven { + url 'https://dl.bintray.com/palantir/releases' // docker-compose-rule is published on bintray + } +} + dependencies { compile project(':geode-core') compile project(':geode-common') compile group: 'com.zaxxer', name: 'HikariCP', version: project.'HikariCP.version' + compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.46' + testCompile project(':geode-junit') //Connectors test framework. testRuntime 'org.apache.derby:derby:' + project.'derby.version' + testCompile 'com.palantir.docker.compose:docker-compose-rule-junit4:0.31.1' testCompile 'com.pholser:junit-quickcheck-core:' + project.'junit-quickcheck.version' testCompile 'com.pholser:junit-quickcheck-generators:' + project.'junit-quickcheck.version' testCompile files(project(':geode-core').sourceSets.test.output) diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManagerMySqlIntegrationTest.java b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManagerMySqlIntegrationTest.java new file mode 100644 index 0000000..25cac1b --- /dev/null +++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManagerMySqlIntegrationTest.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 org.apache.geode.connectors.jdbc.internal; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.Statement; +import java.sql.Types; + +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.geode.test.junit.categories.IntegrationTest; +import org.apache.geode.test.junit.rules.DatabaseConnectionRule; +import org.apache.geode.test.junit.rules.MySqlConnectionRule; + +@Category(IntegrationTest.class) +public class TableMetaDataManagerMySqlIntegrationTest { + + private static final String DB_NAME = "test"; + private static final String REGION_TABLE_NAME = "employees"; + + private TableMetaDataManager manager; + private Connection connection; + private Statement statement; + + @ClassRule + public static DatabaseConnectionRule dbRule = + new MySqlConnectionRule.Builder().file("src/test/resources/docker/mysql.yml") + .serviceName("db").port(3306).database(DB_NAME).build(); + + @Before + public void setup() throws Exception { + connection = dbRule.getConnection(); + statement = connection.createStatement(); + statement.execute("Create Table " + REGION_TABLE_NAME + + " (id VARCHAR(10) primary key not null, name VARCHAR(10), age int)"); + manager = new TableMetaDataManager(); + DatabaseMetaData data = connection.getMetaData(); + data.getConnection(); + } + + @After + public void tearDown() throws Exception { + closeDB(); + } + + private void closeDB() throws Exception { + if (statement == null) { + statement = connection.createStatement(); + } + statement.execute("Drop table " + REGION_TABLE_NAME); + statement.close(); + + if (connection != null) { + connection.close(); + } + } + + @Test + public void validateKeyColumnName() { + TableMetaDataView metaData = manager.getTableMetaDataView(connection, REGION_TABLE_NAME); + + String keyColumnName = metaData.getKeyColumnName(); + + assertThat(keyColumnName).isEqualToIgnoringCase("id"); + } + + @Test + public void validateColumnDataTypeForName() { + TableMetaDataView metaData = manager.getTableMetaDataView(connection, REGION_TABLE_NAME); + + int nameDataType = metaData.getColumnDataType("name"); + + assertThat(nameDataType).isEqualTo(Types.VARCHAR); + } + + @Test + public void validateColumnDataTypeForId() { + TableMetaDataView metaData = manager.getTableMetaDataView(connection, REGION_TABLE_NAME); + + int nameDataType = metaData.getColumnDataType("id"); + + assertThat(nameDataType).isEqualTo(Types.VARCHAR); + } + + @Test + public void validateColumnDataTypeForAge() { + TableMetaDataView metaData = manager.getTableMetaDataView(connection, REGION_TABLE_NAME); + + int nameDataType = metaData.getColumnDataType("age"); + + assertThat(nameDataType).isEqualTo(Types.INTEGER); + } + +} diff --git a/geode-connectors/src/test/java/org/apache/geode/test/junit/rules/DatabaseConnectionRule.java b/geode-connectors/src/test/java/org/apache/geode/test/junit/rules/DatabaseConnectionRule.java new file mode 100644 index 0000000..1869172 --- /dev/null +++ b/geode-connectors/src/test/java/org/apache/geode/test/junit/rules/DatabaseConnectionRule.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 org.apache.geode.test.junit.rules; + +import static org.awaitility.Awaitility.matches; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.concurrent.TimeUnit; + +import com.palantir.docker.compose.DockerComposeRule; +import com.palantir.docker.compose.connection.DockerPort; +import com.palantir.docker.compose.connection.waiting.HealthChecks; +import org.awaitility.Awaitility; +import org.junit.rules.ExternalResource; + +public abstract class DatabaseConnectionRule extends ExternalResource { + + private final DockerComposeRule dockerRule; + private final String serviceName; + private final int port; + private final String dbName; + + protected DatabaseConnectionRule(DockerComposeRule dockerRule, String serviceName, int port, + String dbName) { + this.dockerRule = dockerRule; + this.serviceName = serviceName; + this.port = port; + this.dbName = dbName; + } + + @Override + public void before() throws IOException, InterruptedException { + dockerRule.before(); + } + + @Override + public void after() { + dockerRule.after(); + } + + protected DockerPort getDockerPort() { + return dockerRule.containers().container(serviceName).port(port); + } + + public Connection getConnection() throws SQLException { + String connectionUrl = getConnectionString(); + Awaitility.await().ignoreExceptions().atMost(10, TimeUnit.SECONDS) + .until(matches(() -> DriverManager.getConnection(connectionUrl))); + Connection connection = DriverManager.getConnection(connectionUrl); + if (dbName != null) { + connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS " + dbName); + connection.setCatalog(dbName); + } + return connection; + } + + protected abstract String getConnectionString(); + + public abstract static class Builder { + private String filePath; + private String serviceName; + private int port; + private String dbName; + + public abstract DatabaseConnectionRule build(); + + public Builder file(String filePath) { + this.filePath = filePath; + return this; + } + + public Builder serviceName(String serviceName) { + this.serviceName = serviceName; + return this; + } + + public Builder port(int port) { + this.port = port; + return this; + } + + public Builder database(String dbName) { + this.dbName = dbName; + return this; + } + + protected String getDbName() { + return dbName; + } + + protected String getServiceName() { + return serviceName; + } + + protected int getPort() { + return port; + } + + protected DockerComposeRule createDockerRule() { + return DockerComposeRule.builder().file(filePath) + .waitingForService(serviceName, HealthChecks.toHaveAllPortsOpen()).build(); + } + + } + +} diff --git a/geode-connectors/src/test/java/org/apache/geode/test/junit/rules/MySqlConnectionRule.java b/geode-connectors/src/test/java/org/apache/geode/test/junit/rules/MySqlConnectionRule.java new file mode 100644 index 0000000..4cda016 --- /dev/null +++ b/geode-connectors/src/test/java/org/apache/geode/test/junit/rules/MySqlConnectionRule.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 org.apache.geode.test.junit.rules; + +import com.palantir.docker.compose.DockerComposeRule; + +public class MySqlConnectionRule extends DatabaseConnectionRule { + private static final String CONNECTION_STRING = + "jdbc:mysql://$HOST:$EXTERNAL_PORT?user=root&useSSL=false"; + + protected MySqlConnectionRule(DockerComposeRule dockerRule, String serviceName, int port, + String dbName) { + super(dockerRule, serviceName, port, dbName); + } + + @Override + protected String getConnectionString() { + return getDockerPort().inFormat(CONNECTION_STRING); + } + + public static class Builder extends DatabaseConnectionRule.Builder { + + public Builder() { + super(); + } + + @Override + public MySqlConnectionRule build() { + return new MySqlConnectionRule(createDockerRule(), getServiceName(), getPort(), getDbName()); + } + } +} diff --git a/geode-connectors/src/test/resources/docker/mysql.yml b/geode-connectors/src/test/resources/docker/mysql.yml new file mode 100644 index 0000000..1bebd2b --- /dev/null +++ b/geode-connectors/src/test/resources/docker/mysql.yml @@ -0,0 +1,12 @@ +version: '3.1' + +services: + + db: + image: mysql:5.7 + restart: always + ports: + - 3306 + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: "true" + #MYSQL_ROOT_PASSWORD: "secret" \ No newline at end of file -- To stop receiving notification emails like this one, please contact aging...@apache.org.