Skip to content

Commit 4d4d838

Browse files
refactor tests using error codes
1 parent 9a99cdf commit 4d4d838

File tree

8 files changed

+41
-79
lines changed

8 files changed

+41
-79
lines changed

src/test/java/net/snowflake/client/AbstractDriverIT.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ public class AbstractDriverIT {
4141

4242
protected final int ERROR_CODE_BIND_VARIABLE_NOT_ALLOWED_IN_VIEW_OR_UDF_DEF = 2210;
4343

44-
protected final int ERROR_CODE_DOMAIN_OBJECT_DOES_NOT_EXIST = 2003;
45-
46-
private static String getConnPropKeyFromEnv(String connectionType, String propKey) {
47-
String envKey = String.format("SNOWFLAKE_%s_%s", connectionType, propKey);
48-
return envKey;
49-
}
50-
5144
private static String getConnPropValueFromEnv(String connectionType, String propKey) {
5245
String envKey = String.format("SNOWFLAKE_%s_%s", connectionType, propKey);
5346
return TestUtil.systemGetEnv(envKey);

src/test/java/net/snowflake/client/jdbc/ConnectionIT.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@
5555
@Tag(TestTags.CONNECTION)
5656
public class ConnectionIT extends BaseJDBCWithSharedConnectionIT {
5757
// create a local constant for this code for testing purposes (already defined in GS)
58-
public static final int INVALID_CONNECTION_INFO_CODE = 390100;
5958
private static final int SESSION_CREATION_OBJECT_DOES_NOT_EXIST_NOT_AUTHORIZED = 390201;
6059
private static final int ROLE_IN_CONNECT_STRING_DOES_NOT_EXIST = 390189;
61-
public static final int BAD_REQUEST_GS_CODE = 390400;
62-
public static final int NETWORK_ERROR_CODE = 200015;
6360

6461
public static final int WAIT_FOR_TELEMETRY_REPORT_IN_MILLISECS = 5000;
6562

@@ -229,7 +226,7 @@ public void testConnectionClientInfo() throws SQLException {
229226
connection.setClientInfo(clientInfo);
230227
});
231228
assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState());
232-
assertEquals(200047, e.getErrorCode());
229+
assertEquals(ErrorCode.INVALID_PARAMETER_VALUE.getMessageCode(), e.getErrorCode());
233230
assertEquals(2, e.getFailedProperties().size());
234231

235232
e =
@@ -239,7 +236,7 @@ public void testConnectionClientInfo() throws SQLException {
239236
connection.setClientInfo("ApplicationName", "valueA");
240237
});
241238
assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState());
242-
assertEquals(200047, e.getErrorCode());
239+
assertEquals(ErrorCode.INVALID_PARAMETER_VALUE.getMessageCode(), e.getErrorCode());
243240
assertEquals(1, e.getFailedProperties().size());
244241
}
245242

@@ -418,13 +415,7 @@ public void testConnectUsingKeyPair() throws Exception {
418415
PublicKey publicKey2 = keyPair.getPublic();
419416
PrivateKey privateKey2 = keyPair.getPrivate();
420417
properties.put("privateKey", privateKey2);
421-
SQLException e =
422-
assertThrows(
423-
SQLException.class,
424-
() -> {
425-
DriverManager.getConnection(uri, properties);
426-
});
427-
assertEquals(390144, e.getErrorCode());
418+
connectExpectingInvalidJWTError(uri, properties);
428419

429420
// test multiple key pair
430421
try (Connection connection = getConnection();
@@ -929,6 +920,13 @@ public void testFailOverOrgAccount() throws SQLException {
929920
}
930921
}
931922

923+
private void connectExpectingInvalidJWTError(String fullUri, Properties properties) {
924+
SQLException e =
925+
assertThrows(
926+
SQLException.class, () -> DriverManager.getConnection(fullUri, properties).close());
927+
assertEquals(390144, e.getErrorCode());
928+
}
929+
932930
private class ConcurrentConnections implements Runnable {
933931

934932
ConcurrentConnections() {}

src/test/java/net/snowflake/client/jdbc/ConnectionLatestIT.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package net.snowflake.client.jdbc;
22

33
import static net.snowflake.client.core.SessionUtil.CLIENT_SESSION_KEEP_ALIVE_HEARTBEAT_FREQUENCY;
4-
import static net.snowflake.client.jdbc.ConnectionIT.BAD_REQUEST_GS_CODE;
5-
import static net.snowflake.client.jdbc.ConnectionIT.INVALID_CONNECTION_INFO_CODE;
6-
import static net.snowflake.client.jdbc.ConnectionIT.NETWORK_ERROR_CODE;
74
import static net.snowflake.client.jdbc.ConnectionIT.WAIT_FOR_TELEMETRY_REPORT_IN_MILLISECS;
85
import static org.awaitility.Awaitility.await;
96
import static org.hamcrest.CoreMatchers.equalTo;
107
import static org.hamcrest.CoreMatchers.is;
118
import static org.hamcrest.CoreMatchers.not;
129
import static org.hamcrest.MatcherAssert.assertThat;
1310
import static org.hamcrest.Matchers.instanceOf;
14-
import static org.hamcrest.core.AnyOf.anyOf;
1511
import static org.junit.jupiter.api.Assertions.assertEquals;
1612
import static org.junit.jupiter.api.Assertions.assertFalse;
1713
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -599,21 +595,14 @@ public void testHttpsLoginTimeoutWithOutSSL() throws InterruptedException {
599595

600596
DriverManager.getConnection(wrongUri, properties);
601597
});
598+
assertThat(
599+
"Communication error", e.getErrorCode(), equalTo(ErrorCode.NETWORK_ERROR.getMessageCode()));
602600
if (TelemetryService.getInstance()
603601
.getServerDeploymentName()
604602
.equals(TelemetryService.TELEMETRY_SERVER_DEPLOYMENT.DEV.getName())
605603
|| TelemetryService.getInstance()
606604
.getServerDeploymentName()
607605
.equals(TelemetryService.TELEMETRY_SERVER_DEPLOYMENT.REG.getName())) {
608-
// a connection error response (wrong user and password)
609-
// with status code 200 is returned in RT
610-
assertThat(
611-
"Communication error",
612-
e.getErrorCode(),
613-
anyOf(
614-
equalTo(INVALID_CONNECTION_INFO_CODE),
615-
equalTo(BAD_REQUEST_GS_CODE),
616-
equalTo(NETWORK_ERROR_CODE)));
617606

618607
// since it returns normal response,
619608
// the telemetry does not create new event
@@ -625,12 +614,6 @@ public void testHttpsLoginTimeoutWithOutSSL() throws InterruptedException {
625614
equalTo(count));
626615
}
627616
} else {
628-
// in qa1 and others, 404 http status code should be returned
629-
assertThat(
630-
"Communication error",
631-
e.getErrorCode(),
632-
equalTo(ErrorCode.NETWORK_ERROR.getMessageCode()));
633-
634617
if (TelemetryService.getInstance().isDeploymentEnabled()) {
635618
assertThat(
636619
"Telemetry event has not been reported successfully. Error: "
@@ -848,7 +831,7 @@ public void testPrivateKeyInConnectionString() throws SQLException, IOException
848831

849832
privateKeyLocation = getFullPathFileInResource("encrypted_rsa_key.p8");
850833
uri = uriWithPrivateKeyFileAndPassword(baseUri, privateKeyLocation, "test");
851-
connectExpectingError390144(uri, properties);
834+
connectExpectingInvalidJWTError(uri, properties);
852835

853836
// test with invalid private key
854837
privateKeyLocation = getFullPathFileInResource("invalid_private_key.pem");
@@ -877,7 +860,7 @@ private static String uriWithPrivateKeyFileAndPassword(
877860
+ password;
878861
}
879862

880-
private static void connectExpectingError390144(String fullUri, Properties properties) {
863+
private static void connectExpectingInvalidJWTError(String fullUri, Properties properties) {
881864
SQLException e =
882865
assertThrows(
883866
SQLException.class, () -> DriverManager.getConnection(fullUri, properties).close());
@@ -963,7 +946,7 @@ public void testPrivateKeyBase64InConnectionString() throws SQLException, IOExce
963946

964947
privateKeyBase64 = readPrivateKeyFileToBase64Content("encrypted_rsa_key.p8");
965948
uri = uriWithPrivateKeyBase64AndPassword(baseUri, privateKeyBase64, "test");
966-
connectExpectingError390144(uri, properties);
949+
connectExpectingInvalidJWTError(uri, properties);
967950

968951
// test with invalid private key
969952
privateKeyBase64 = readPrivateKeyFileToBase64Content("invalid_private_key.pem");

src/test/java/net/snowflake/client/jdbc/ResultSetIT.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void testGetShort(String queryResultFormat) throws SQLException {
182182
int finalI = i;
183183
SQLException ex =
184184
assertThrows(SQLException.class, () -> resultSet.getShort(finalI), "Failing on " + i);
185-
assertEquals(200038, ex.getErrorCode());
185+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
186186
}
187187
assertTrue(resultSet.next());
188188
// certain column types can only have certain values when called by getShort() or else a
@@ -193,7 +193,7 @@ public void testGetShort(String queryResultFormat) throws SQLException {
193193
int finalI = i;
194194
SQLException ex =
195195
assertThrows(SQLException.class, () -> resultSet.getShort(finalI), "Failing on " + i);
196-
assertEquals(200038, ex.getErrorCode());
196+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
197197
}
198198
}
199199
}
@@ -221,7 +221,7 @@ public void testGetInt(String queryResultFormat) throws SQLException {
221221
int finalI = i;
222222
SQLException ex =
223223
assertThrows(SQLException.class, () -> resultSet.getInt(finalI), "Failing on " + i);
224-
assertEquals(200038, ex.getErrorCode());
224+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
225225
}
226226
assertTrue(resultSet.next());
227227
// certain column types can only have certain values when called by getInt() or else a
@@ -231,7 +231,7 @@ public void testGetInt(String queryResultFormat) throws SQLException {
231231
int finalI = i;
232232
SQLException ex =
233233
assertThrows(SQLException.class, () -> resultSet.getInt(finalI), "Failing on " + i);
234-
assertEquals(200038, ex.getErrorCode());
234+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
235235
}
236236
}
237237
}
@@ -259,7 +259,7 @@ public void testGetLong(String queryResultFormat) throws SQLException {
259259
int finalI = i;
260260
SQLException ex =
261261
assertThrows(SQLException.class, () -> resultSet.getLong(finalI), "Failing on " + i);
262-
assertEquals(200038, ex.getErrorCode());
262+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
263263
}
264264
assertTrue(resultSet.next());
265265
// certain column types can only have certain values when called by getLong() or else a
@@ -269,7 +269,7 @@ public void testGetLong(String queryResultFormat) throws SQLException {
269269
int finalI = i;
270270
SQLException ex =
271271
assertThrows(SQLException.class, () -> resultSet.getLong(finalI), "Failing on " + i);
272-
assertEquals(200038, ex.getErrorCode());
272+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
273273
}
274274
}
275275
}
@@ -297,7 +297,7 @@ public void testGetFloat(String queryResultFormat) throws SQLException {
297297
int finalI = i;
298298
SQLException ex =
299299
assertThrows(SQLException.class, () -> resultSet.getFloat(finalI), "Failing on " + i);
300-
assertEquals(200038, ex.getErrorCode());
300+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
301301
}
302302
assertTrue(resultSet.next());
303303
// certain column types can only have certain values when called by getFloat() or else a
@@ -307,7 +307,7 @@ public void testGetFloat(String queryResultFormat) throws SQLException {
307307
int finalI = i;
308308
SQLException ex =
309309
assertThrows(SQLException.class, () -> resultSet.getFloat(finalI), "Failing on " + i);
310-
assertEquals(200038, ex.getErrorCode());
310+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
311311
}
312312
}
313313
}
@@ -335,7 +335,7 @@ public void testGetDouble(String queryResultFormat) throws SQLException {
335335
int finalI = i;
336336
SQLException ex =
337337
assertThrows(SQLException.class, () -> resultSet.getDouble(finalI), "Failing on " + i);
338-
assertEquals(200038, ex.getErrorCode());
338+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
339339
}
340340
assertTrue(resultSet.next());
341341
// certain column types can only have certain values when called by getDouble() or else a
@@ -345,7 +345,7 @@ public void testGetDouble(String queryResultFormat) throws SQLException {
345345
int finalI = i;
346346
SQLException ex =
347347
assertThrows(SQLException.class, () -> resultSet.getDouble(finalI), "Failing on " + i);
348-
assertEquals(200038, ex.getErrorCode());
348+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
349349
}
350350
}
351351
}
@@ -393,15 +393,15 @@ public void testGetBigDecimal(String queryResultFormat) throws SQLException {
393393
SQLException ex =
394394
assertThrows(
395395
SQLException.class, () -> resultSet.getBigDecimal(finalI), "Failing on " + i);
396-
assertEquals(200038, ex.getErrorCode());
396+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
397397
}
398398
assertTrue(resultSet.next());
399399
for (int i = 5; i < 7; i++) {
400400
int finalI = i;
401401
SQLException ex =
402402
assertThrows(
403403
SQLException.class, () -> resultSet.getBigDecimal(finalI), "Failing on " + i);
404-
assertEquals(200038, ex.getErrorCode());
404+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
405405
}
406406
}
407407
}
@@ -423,7 +423,7 @@ public void testGetBigDecimalNegative(String queryResultFormat) throws SQLExcept
423423
assertTrue(resultSet.next());
424424
SQLException ex =
425425
assertThrows(SQLException.class, () -> resultSet.getBigDecimal(2, 38));
426-
assertEquals(200032, ex.getErrorCode());
426+
assertEquals(ErrorCode.COLUMN_DOES_NOT_EXIST.getMessageCode(), ex.getErrorCode());
427427
}
428428
}
429429
} finally {
@@ -701,7 +701,7 @@ public void testGetBoolean(String queryResultFormat) throws SQLException {
701701
SQLException ex =
702702
assertThrows(
703703
SQLException.class, () -> resultSet.getBoolean(finalI), "Failing on " + i);
704-
assertEquals(200038, ex.getErrorCode());
704+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
705705
}
706706

707707
assertTrue(resultSet.next());
@@ -710,7 +710,7 @@ public void testGetBoolean(String queryResultFormat) throws SQLException {
710710
SQLException ex =
711711
assertThrows(
712712
SQLException.class, () -> resultSet.getBoolean(finalI), "Failing on " + i);
713-
assertEquals(200038, ex.getErrorCode());
713+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
714714
}
715715
}
716716
}
@@ -842,9 +842,9 @@ public void testInvalidColumnIndex(String queryResultFormat) throws SQLException
842842

843843
assertTrue(resultSet.next());
844844
SQLException e = assertThrows(SQLException.class, () -> resultSet.getString(0));
845-
assertEquals(200032, e.getErrorCode());
845+
assertEquals(ErrorCode.COLUMN_DOES_NOT_EXIST.getMessageCode(), e.getErrorCode());
846846
e = assertThrows(SQLException.class, () -> resultSet.getString(2));
847-
assertEquals(200032, e.getErrorCode());
847+
assertEquals(ErrorCode.COLUMN_DOES_NOT_EXIST.getMessageCode(), e.getErrorCode());
848848
}
849849
}
850850

src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public void testBytesCrossTypeTests(String queryResultFormat) throws Exception {
531531
int finalI = i;
532532
SQLException ex =
533533
assertThrows(SQLException.class, () -> resultSet.getBytes(finalI), "Failing on " + i);
534-
assertEquals(200038, ex.getErrorCode());
534+
assertEquals(ErrorCode.INVALID_VALUE_CONVERT.getMessageCode(), ex.getErrorCode());
535535
}
536536

537537
byte[] decoded = SFBinary.fromHex("48454C4C4F").getBytes();

src/test/java/net/snowflake/client/jdbc/SnowflakeDriverIT.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,22 +1888,11 @@ public void testNullBind() throws Throwable {
18881888

18891889
preparedStatement.clearBatch();
18901890

1891-
// this test causes query count in GS not to be decremented because
1892-
// the exception is thrown before registerQC. Discuss with Johnston
1893-
// to resolve the issue before enabling the test.
1894-
preparedStatement.setObject(1, "Null", Types.DOUBLE);
1895-
preparedStatement.addBatch();
1896-
SnowflakeSQLException ex =
1897-
assertThrows(SnowflakeSQLException.class, preparedStatement::executeBatch);
1898-
assertEquals(2086, ex.getErrorCode());
1899-
1900-
preparedStatement.clearBatch();
1901-
19021891
preparedStatement.setString(1, "hello");
19031892
preparedStatement.addBatch();
1904-
19051893
preparedStatement.setDouble(1, 1.2);
1906-
ex = assertThrows(SnowflakeSQLException.class, preparedStatement::addBatch);
1894+
SnowflakeSQLException ex =
1895+
assertThrows(SnowflakeSQLException.class, preparedStatement::addBatch);
19071896
assertEquals(
19081897
(int) ErrorCode.ARRAY_BIND_MIXED_TYPES_NOT_SUPPORTED.getMessageCode(),
19091898
ex.getErrorCode());

src/test/java/net/snowflake/client/jdbc/StatementIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,15 @@ public void testExecuteBatch() throws Exception {
330330
assertThat(resultSet.getInt("B"), is(7));
331331
statement.clearBatch();
332332

333-
// one of the batch is query instead of ddl/dml
334-
// it should continuing processing
333+
// one of the batch query returns error
334+
// it should continue processing
335335
statement.addBatch("insert into test_batch values('str3', 3)");
336336
statement.addBatch("select * from test_batch");
337337
statement.addBatch("select * from test_batch_not_exist");
338338
statement.addBatch("insert into test_batch values('str4', 4)");
339339
BatchUpdateException e =
340340
assertThrows(BatchUpdateException.class, statement::executeBatch);
341341
rowCounts = e.getUpdateCounts();
342-
assertThat(e.getErrorCode(), is(ERROR_CODE_DOMAIN_OBJECT_DOES_NOT_EXIST));
343342
assertThat(rowCounts[0], is(1));
344343
assertThat(rowCounts[1], is(Statement.SUCCESS_NO_INFO));
345344
assertThat(rowCounts[2], is(Statement.EXECUTE_FAILED));

src/test/java/net/snowflake/client/pooling/ConnectionPoolingDataSourceIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public void testPooledConnection() throws SQLException {
4040
TestingConnectionListener listener = new TestingConnectionListener();
4141
pooledConnection.addConnectionEventListener(listener);
4242

43+
int thrownErrorCode;
4344
try (Connection connection = pooledConnection.getConnection();
4445
Statement statement = connection.createStatement()) {
4546
statement.execute("select 1");
4647

4748
SQLException e =
4849
assertThrows(SQLException.class, () -> connection.setCatalog("nonexistent_database"));
49-
assertThat(e.getErrorCode(), is(2043));
50-
50+
thrownErrorCode = e.getErrorCode();
5151
// should not close underlying physical connection
5252
// and fire connection closed events
5353
}
@@ -68,8 +68,8 @@ public void testPooledConnection() throws SQLException {
6868

6969
assertThat(errorEvent.getSource(), instanceOf(SnowflakePooledConnection.class));
7070
assertThat((PooledConnection) errorEvent.getSource(), sameInstance(pooledConnection));
71-
// 2043 is the error code for object not existed
72-
assertThat(errorEvent.getSQLException().getErrorCode(), is(2043));
71+
// error event error code match thrown error code
72+
assertThat(errorEvent.getSQLException().getErrorCode(), is(thrownErrorCode));
7373

7474
// assert physical connection is not closed
7575
Connection physicalConnection =

0 commit comments

Comments
 (0)