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 4e83237f15764a8f07ebd629f57436db3f696799
Author: Nick Reich <nre...@pivotal.io>
AuthorDate: Tue Mar 27 10:38:10 2018 -0700

    GEODE-4947: Add Postgres test rule and test utilizing it
---
 geode-connectors/build.gradle                      |   3 +-
 ...ableMetaDataManagerPostgresIntegrationTest.java | 114 +++++++++++++++++++++
 .../test/junit/rules/DatabaseConnectionRule.java   |  10 +-
 .../test/junit/rules/MySqlConnectionRule.java      |  14 +++
 ...ectionRule.java => PostgresConnectionRule.java} |  13 +--
 .../src/test/resources/docker/postgres.yml         |  11 ++
 6 files changed, 153 insertions(+), 12 deletions(-)

diff --git a/geode-connectors/build.gradle b/geode-connectors/build.gradle
index 432fbd5..005f498 100644
--- a/geode-connectors/build.gradle
+++ b/geode-connectors/build.gradle
@@ -25,13 +25,14 @@ 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 group: 'mysql', name: 'mysql-connector-java', version: '5.1.46'
+    testCompile group: 'org.postgresql', name: 'postgresql', version: '42.2.2'
     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'
diff --git 
a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManagerPostgresIntegrationTest.java
 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManagerPostgresIntegrationTest.java
new file mode 100644
index 0000000..0538e13
--- /dev/null
+++ 
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManagerPostgresIntegrationTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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;
+import org.apache.geode.test.junit.rules.PostgresConnectionRule;
+
+@Category(IntegrationTest.class)
+public class TableMetaDataManagerPostgresIntegrationTest {
+
+  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 
PostgresConnectionRule.Builder().file("src/test/resources/docker/postgres.yml")
+          .serviceName("db").port(5432).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
index 1869172..0bae5fa 100644
--- 
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
@@ -57,15 +57,15 @@ public abstract class DatabaseConnectionRule extends 
ExternalResource {
     return dockerRule.containers().container(serviceName).port(port);
   }
 
+  protected String getDbName() {
+    return dbName;
+  }
+
   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;
   }
 
@@ -104,7 +104,7 @@ public abstract class DatabaseConnectionRule extends 
ExternalResource {
     }
 
     protected String getServiceName() {
-      return  serviceName;
+      return serviceName;
     }
 
     protected int getPort() {
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
index 4cda016..4925c6d 100644
--- 
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
@@ -14,6 +14,9 @@
  */
 package org.apache.geode.test.junit.rules;
 
+import java.sql.Connection;
+import java.sql.SQLException;
+
 import com.palantir.docker.compose.DockerComposeRule;
 
 public class MySqlConnectionRule extends DatabaseConnectionRule {
@@ -26,6 +29,17 @@ public class MySqlConnectionRule extends 
DatabaseConnectionRule {
   }
 
   @Override
+  public Connection getConnection() throws SQLException {
+    Connection connection = super.getConnection();
+    String dbName = getDbName();
+    if (dbName != null) {
+      connection.createStatement().execute("CREATE DATABASE IF NOT EXISTS " + 
dbName);
+      connection.setCatalog(dbName);
+    }
+    return connection;
+  }
+
+  @Override
   protected String getConnectionString() {
     return getDockerPort().inFormat(CONNECTION_STRING);
   }
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/PostgresConnectionRule.java
similarity index 72%
copy from 
geode-connectors/src/test/java/org/apache/geode/test/junit/rules/MySqlConnectionRule.java
copy to 
geode-connectors/src/test/java/org/apache/geode/test/junit/rules/PostgresConnectionRule.java
index 4cda016..557d617 100644
--- 
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/PostgresConnectionRule.java
@@ -16,18 +16,18 @@ package org.apache.geode.test.junit.rules;
 
 import com.palantir.docker.compose.DockerComposeRule;
 
-public class MySqlConnectionRule extends DatabaseConnectionRule {
+public class PostgresConnectionRule extends DatabaseConnectionRule {
   private static final String CONNECTION_STRING =
-      "jdbc:mysql://$HOST:$EXTERNAL_PORT?user=root&useSSL=false";
+      "jdbc:postgresql://$HOST:$EXTERNAL_PORT/%s?user=postgres";
 
-  protected MySqlConnectionRule(DockerComposeRule dockerRule, String 
serviceName, int port,
+  protected PostgresConnectionRule(DockerComposeRule dockerRule, String 
serviceName, int port,
       String dbName) {
     super(dockerRule, serviceName, port, dbName);
   }
 
   @Override
   protected String getConnectionString() {
-    return getDockerPort().inFormat(CONNECTION_STRING);
+    return getDockerPort().inFormat(String.format(CONNECTION_STRING, 
getDbName()));
   }
 
   public static class Builder extends DatabaseConnectionRule.Builder {
@@ -37,8 +37,9 @@ public class MySqlConnectionRule extends 
DatabaseConnectionRule {
     }
 
     @Override
-    public MySqlConnectionRule build() {
-      return new MySqlConnectionRule(createDockerRule(), getServiceName(), 
getPort(), getDbName());
+    public PostgresConnectionRule build() {
+      return new PostgresConnectionRule(createDockerRule(), getServiceName(), 
getPort(),
+          getDbName());
     }
   }
 }
diff --git a/geode-connectors/src/test/resources/docker/postgres.yml 
b/geode-connectors/src/test/resources/docker/postgres.yml
new file mode 100644
index 0000000..1b2ce80
--- /dev/null
+++ b/geode-connectors/src/test/resources/docker/postgres.yml
@@ -0,0 +1,11 @@
+version: '3.1'
+
+services:
+
+  db:
+    image: postgres
+    restart: always
+    ports:
+      - 5432
+    environment:
+      POSTGRES_DB: test

-- 
To stop receiving notification emails like this one, please contact
aging...@apache.org.

Reply via email to