You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by oe...@apache.org on 2026/05/26 13:50:31 UTC
(streampipes) 02/02: Add tests
This is an automated email from the ASF dual-hosted git repository.
SvenO3 pushed a commit to branch 4505-postgresqlsink-quality-and-reliability-improvements
in repository https://gitbox.apache.org/repos/asf/streampipes.git
commit da8628b544f7ec097bf0119375acbe634316d023
Author: Sven Oehler <oe...@web.de>
AuthorDate: Tue May 26 15:50:18 2026 +0200
Add tests
---
.../databases/jvm/jdbcclient/JdbcClientTest.java | 53 ++++++++
.../jvm/jdbcclient/model/StatementHandlerTest.java | 133 +++++++++++++++++++++
.../jvm/jdbcclient/model/TableDescriptionTest.java | 78 ++++++++++++
3 files changed, 264 insertions(+)
diff --git a/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/JdbcClientTest.java b/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/JdbcClientTest.java
new file mode 100644
index 0000000000..943af5034c
--- /dev/null
+++ b/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/JdbcClientTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.streampipes.sinks.databases.jvm.jdbcclient;
+
+import org.junit.jupiter.api.Test;
+
+import java.sql.SQLException;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class JdbcClientTest {
+
+ @Test
+ public void testIsSqlStateClassMatchesClassPrefix() {
+ assertTrue(TestJdbcClient.isSqlStateClass(new SQLException("missing relation", "42P01"), "42"));
+ }
+
+ @Test
+ public void testIsSqlStateClassRejectsDifferentClass() {
+ assertFalse(TestJdbcClient.isSqlStateClass(new SQLException("constraint violation", "23P01"), "42"));
+ }
+
+ @Test
+ public void testIsSqlStateClassRejectsNullState() {
+ assertFalse(TestJdbcClient.isSqlStateClass(new SQLException("missing state", (String) null), "42"));
+ }
+
+ @Test
+ public void testIsSqlStateClassRejectsShortState() {
+ assertFalse(TestJdbcClient.isSqlStateClass(new SQLException("short state", "4"), "42"));
+ }
+
+ private static class TestJdbcClient extends JdbcClient {
+
+ }
+}
diff --git a/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/model/StatementHandlerTest.java b/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/model/StatementHandlerTest.java
new file mode 100644
index 0000000000..60d0e060f8
--- /dev/null
+++ b/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/model/StatementHandlerTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.streampipes.sinks.databases.jvm.jdbcclient.model;
+
+import org.apache.streampipes.commons.exceptions.SpRuntimeException;
+import org.apache.streampipes.model.schema.EventSchema;
+
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class StatementHandlerTest {
+
+ @Test
+ public void testGeneratePreparedStatementSortsAndQuotesColumns() throws SQLException, SpRuntimeException {
+ var connection = mock(Connection.class);
+ var preparedStatement = mock(PreparedStatement.class);
+ when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
+
+ var handler = new StatementHandler(null, null);
+ handler.executePreparedStatement(dbDescription(), tableDescription(), connection, event(
+ "b", "value",
+ "a", 1
+ ));
+
+ verify(connection).prepareStatement("INSERT INTO measurements ( \"a\", \"b\" ) VALUES ( ?, ? );");
+ verify(preparedStatement).setInt(1, 1);
+ verify(preparedStatement).setString(2, "value");
+ verify(preparedStatement).executeUpdate();
+ }
+
+ @Test
+ public void testGeneratePreparedStatementUsesFlattenedNestedNames() throws SQLException, SpRuntimeException {
+ var connection = mock(Connection.class);
+ var preparedStatement = mock(PreparedStatement.class);
+ when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
+
+ var nested = event("temperature", 21.5d);
+ var handler = new StatementHandler(null, null);
+ handler.executePreparedStatement(dbDescription(), tableDescription(), connection, event("device", nested));
+
+ verify(connection).prepareStatement("INSERT INTO measurements ( \"device_temperature\" ) VALUES ( ? );");
+ verify(preparedStatement).setDouble(1, 21.5d);
+ verify(preparedStatement).executeUpdate();
+ }
+
+ @Test
+ public void testGeneratePreparedStatementRejectsInvalidColumnNames() throws SQLException {
+ var connection = mock(Connection.class);
+
+ var handler = new StatementHandler(null, null);
+
+ assertThrows(SpRuntimeException.class, () -> handler.executePreparedStatement(
+ dbDescription(),
+ tableDescription(),
+ connection,
+ event("1invalid", "value")));
+ }
+
+ @Test
+ public void testExecutePreparedStatementRegeneratesWhenEventShapeChanges()
+ throws SQLException, SpRuntimeException {
+ var connection = mock(Connection.class);
+ var firstPreparedStatement = mock(PreparedStatement.class);
+ var secondPreparedStatement = mock(PreparedStatement.class);
+ when(connection.prepareStatement(anyString())).thenReturn(firstPreparedStatement, secondPreparedStatement);
+
+ var handler = new StatementHandler(null, null);
+ handler.executePreparedStatement(dbDescription(), tableDescription(), connection, event("a", 1));
+ handler.executePreparedStatement(dbDescription(), tableDescription(), connection, event(
+ "a", 1,
+ "b", 2
+ ));
+
+ verify(connection).prepareStatement("INSERT INTO measurements ( \"a\" ) VALUES ( ? );");
+ verify(connection).prepareStatement("INSERT INTO measurements ( \"a\", \"b\" ) VALUES ( ?, ? );");
+ verify(firstPreparedStatement).close();
+ verify(secondPreparedStatement).setInt(1, 1);
+ verify(secondPreparedStatement).setInt(2, 2);
+ }
+
+ private DbDescription dbDescription() {
+ return new DbDescription(new JdbcConnectionParameters(
+ null,
+ "localhost",
+ 5432,
+ "database",
+ "user",
+ "password",
+ "measurements",
+ false,
+ "",
+ true), SupportedDbEngines.POSTGRESQL);
+ }
+
+ private TableDescription tableDescription() {
+ return new TableDescription("measurements", new EventSchema());
+ }
+
+ private Map<String, Object> event(Object... keyValues) {
+ Map<String, Object> event = new LinkedHashMap<>();
+ for (int i = 0; i < keyValues.length; i += 2) {
+ event.put((String) keyValues[i], keyValues[i + 1]);
+ }
+ return event;
+ }
+}
diff --git a/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/model/TableDescriptionTest.java b/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/model/TableDescriptionTest.java
new file mode 100644
index 0000000000..310ec150a0
--- /dev/null
+++ b/streampipes-extensions/streampipes-sinks-databases-jvm/src/test/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/model/TableDescriptionTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.streampipes.sinks.databases.jvm.jdbcclient.model;
+
+import org.apache.streampipes.commons.exceptions.SpRuntimeException;
+import org.apache.streampipes.model.schema.EventSchema;
+
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class TableDescriptionTest {
+
+ @Test
+ public void testExtractTableInformationMapsColumnsAndClosesResources()
+ throws SQLException, SpRuntimeException {
+ var connection = mock(Connection.class);
+ var preparedStatement = mock(PreparedStatement.class);
+ var resultSet = mock(ResultSet.class);
+ when(connection.prepareStatement("query")).thenReturn(preparedStatement);
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, true, false);
+ when(resultSet.getString("COLUMN_NAME")).thenReturn("temperature", "active");
+ when(resultSet.getString("DATA_TYPE")).thenReturn("double precision", "boolean");
+
+ var tableDescription = new TableDescription("measurements", new EventSchema());
+ tableDescription.extractTableInformation(connection, "query", new String[]{"measurements"});
+
+ verify(preparedStatement).setString(1, "measurements");
+ verify(resultSet).close();
+ verify(preparedStatement).close();
+ assertEquals(DbDataTypes.DOUBLE_PRECISION, tableDescription.getDataTypesHashMap().get("temperature"));
+ assertEquals(DbDataTypes.BOOLEAN, tableDescription.getDataTypesHashMap().get("active"));
+ }
+
+ @Test
+ public void testExtractTableInformationThrowsWhenTableIsMissingAndClosesResources()
+ throws SQLException {
+ var connection = mock(Connection.class);
+ var preparedStatement = mock(PreparedStatement.class);
+ var resultSet = mock(ResultSet.class);
+ when(connection.prepareStatement("query")).thenReturn(preparedStatement);
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(false);
+
+ var tableDescription = new TableDescription("measurements", new EventSchema());
+
+ assertThrows(SpRuntimeException.class,
+ () -> tableDescription.extractTableInformation(connection, "query", new String[]{"measurements"}));
+ verify(resultSet).close();
+ verify(preparedStatement).close();
+ }
+}