This is an automated email from the ASF dual-hosted git repository. dschneider pushed a commit to branch feature/GEODE-3781 in repository https://gitbox.apache.org/repos/asf/geode.git
The following commit(s) were added to refs/heads/feature/GEODE-3781 by this push: new 0d77029 added upsert tests 0d77029 is described below commit 0d7702931943be68c83abdc301c3392272bfe4c8 Author: Darrel Schneider <dschnei...@pivotal.io> AuthorDate: Mon Oct 30 14:53:28 2017 -0700 added upsert tests --- .../connectors/jdbc/JDBCManagerUnitTestTest.java | 86 ++++++++++++++++++++-- 1 file changed, 81 insertions(+), 5 deletions(-) diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCManagerUnitTestTest.java b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCManagerUnitTestTest.java index 8dd28a3..aeca590 100644 --- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCManagerUnitTestTest.java +++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCManagerUnitTestTest.java @@ -23,6 +23,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; +import java.util.List; import java.util.Properties; import org.junit.After; @@ -46,6 +47,7 @@ public class JDBCManagerUnitTestTest { private String regionName = "jdbcRegion"; Connection connection; PreparedStatement preparedStatement; + PreparedStatement preparedStatement2; private static final String ID_COLUMN_NAME = "ID"; private static final String NAME_COLUMN_NAME = "name"; @@ -80,7 +82,39 @@ public class JDBCManagerUnitTestTest { return connection; } + } + + public class TestableUpsertJDBCManager extends JDBCManager { + + TestableUpsertJDBCManager(JDBCConfiguration config) { + super(config); + } + + @Override + protected Connection createConnection(String url) throws SQLException { + ResultSet rsKeys = mock(ResultSet.class); + when(rsKeys.next()).thenReturn(true, false); + when(rsKeys.getString("COLUMN_NAME")).thenReturn(ID_COLUMN_NAME); + + ResultSet rs = mock(ResultSet.class); + when(rs.next()).thenReturn(true, false); + when(rs.getString("TABLE_NAME")).thenReturn(regionName.toUpperCase()); + + DatabaseMetaData metaData = mock(DatabaseMetaData.class); + when(metaData.getPrimaryKeys(null, null, regionName.toUpperCase())).thenReturn(rsKeys); + when(metaData.getTables(any(), any(), any(), any())).thenReturn(rs); + + preparedStatement = mock(PreparedStatement.class); + preparedStatement2 = mock(PreparedStatement.class); + when(preparedStatement.getUpdateCount()).thenReturn(0); + when(preparedStatement2.getUpdateCount()).thenReturn(1); + connection = mock(Connection.class); + when(connection.getMetaData()).thenReturn(metaData); + when(connection.prepareStatement(any())).thenReturn(preparedStatement, preparedStatement2); + + return connection; + } } @Before @@ -90,17 +124,24 @@ public class JDBCManagerUnitTestTest { public void tearDown() throws Exception {} private void createManager(String driver, String url) { - Properties props = new Properties(); - props.setProperty("url", url); - props.setProperty("driver", driver); - JDBCConfiguration config = new JDBCConfiguration(props); - this.mgr = new TestableJDBCManager(config); + this.mgr = new TestableJDBCManager(createConfiguration(driver, url)); } private void createDefaultManager() { createManager("java.lang.String", "fakeURL"); } + private void createUpsertManager() { + this.mgr = new TestableUpsertJDBCManager(createConfiguration("java.lang.String", "fakeURL")); + } + + private JDBCConfiguration createConfiguration(String driver, String url) { + Properties props = new Properties(); + props.setProperty("url", url); + props.setProperty("driver", driver); + return new JDBCConfiguration(props); + } + @Test public void verifySimpleCreateCallsExecute() throws SQLException { createDefaultManager(); @@ -186,4 +227,39 @@ public class JDBCManagerUnitTestTest { assertThat(caughtException().getMessage()).isEqualTo("unsupported operation INVALIDATE"); } + @Test + public void verifyInsertUpdate() throws SQLException { + createUpsertManager(); + GemFireCacheImpl cache = Fakes.cache(); + Region region = Fakes.region(regionName, cache); + PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21); + this.mgr.write(region, Operation.CREATE, "1", pdx1); + verify(this.preparedStatement).execute(); + verify(this.preparedStatement2).execute(); + ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class); + verify(this.connection, times(2)).prepareStatement(sqlCaptor.capture()); + List<String> allArgs = sqlCaptor.getAllValues(); + assertThat(allArgs.get(0)).isEqualTo("INSERT INTO " + regionName + "(" + NAME_COLUMN_NAME + ", " + + AGE_COLUMN_NAME + ", " + ID_COLUMN_NAME + ") VALUES (?,?,?)"); + assertThat(allArgs.get(1)).isEqualTo("UPDATE " + regionName + " SET " + NAME_COLUMN_NAME + + " = ?, " + AGE_COLUMN_NAME + " = ? WHERE " + ID_COLUMN_NAME + " = ?"); + } + + @Test + public void verifyUpdateInsert() throws SQLException { + createUpsertManager(); + GemFireCacheImpl cache = Fakes.cache(); + Region region = Fakes.region(regionName, cache); + PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21); + this.mgr.write(region, Operation.UPDATE, "1", pdx1); + verify(this.preparedStatement).execute(); + verify(this.preparedStatement2).execute(); + ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class); + verify(this.connection, times(2)).prepareStatement(sqlCaptor.capture()); + List<String> allArgs = sqlCaptor.getAllValues(); + assertThat(allArgs.get(0)).isEqualTo("UPDATE " + regionName + " SET " + NAME_COLUMN_NAME + + " = ?, " + AGE_COLUMN_NAME + " = ? WHERE " + ID_COLUMN_NAME + " = ?"); + assertThat(allArgs.get(1)).isEqualTo("INSERT INTO " + regionName + "(" + NAME_COLUMN_NAME + ", " + + AGE_COLUMN_NAME + ", " + ID_COLUMN_NAME + ") VALUES (?,?,?)"); + } } -- To stop receiving notification emails like this one, please contact ['"commits@geode.apache.org" <commits@geode.apache.org>'].