|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
4 | 4 | import static org.mockito.ArgumentMatchers.any;
|
| 5 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
5 | 6 | import static org.mockito.Mockito.doThrow;
|
| 7 | +import static org.mockito.Mockito.never; |
6 | 8 | import static org.mockito.Mockito.verify;
|
7 | 9 | import static org.mockito.Mockito.when;
|
8 | 10 |
|
@@ -138,6 +140,25 @@ public void whenScanOperationExecutedAndScannerClosed_shouldCallJdbcService() th
|
138 | 140 | verify(connection).close();
|
139 | 141 | }
|
140 | 142 |
|
| 143 | + @Test |
| 144 | + public void |
| 145 | + whenScanOperationExecutedAndJdbcServiceThrowsIllegalArgumentException_shouldCloseConnectionAndThrowIllegalArgumentException() |
| 146 | + throws Exception { |
| 147 | + // Arrange |
| 148 | + Exception cause = new IllegalArgumentException("Table not found"); |
| 149 | + // Simulate the table not found scenario. |
| 150 | + when(jdbcService.getScanner(any(), any())).thenThrow(cause); |
| 151 | + |
| 152 | + // Act Assert |
| 153 | + assertThatThrownBy( |
| 154 | + () -> { |
| 155 | + Scan scan = new Scan(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE); |
| 156 | + jdbcDatabase.scan(scan); |
| 157 | + }) |
| 158 | + .isInstanceOf(IllegalArgumentException.class); |
| 159 | + verify(connection).close(); |
| 160 | + } |
| 161 | + |
141 | 162 | @Test
|
142 | 163 | public void
|
143 | 164 | whenScanOperationExecutedAndScannerClosed_SQLExceptionThrownByConnectionCommit_shouldThrowIOException()
|
@@ -382,4 +403,30 @@ public void mutate_withConflictError_shouldThrowRetriableExecutionException()
|
382 | 403 | verify(connection).rollback();
|
383 | 404 | verify(connection).close();
|
384 | 405 | }
|
| 406 | + |
| 407 | + @Test |
| 408 | + public void mutate_WhenSettingAutoCommitFails_ShouldThrowExceptionAndCloseConnection() |
| 409 | + throws SQLException, ExecutionException { |
| 410 | + // Arrange |
| 411 | + Exception exception = new RuntimeException("Failed to set auto-commit"); |
| 412 | + doThrow(exception).when(connection).setAutoCommit(anyBoolean()); |
| 413 | + |
| 414 | + // Act Assert |
| 415 | + assertThatThrownBy( |
| 416 | + () -> { |
| 417 | + Put put = |
| 418 | + new Put(new Key("p1", "val1")) |
| 419 | + .withValue("v1", "val2") |
| 420 | + .forNamespace(NAMESPACE) |
| 421 | + .forTable(TABLE); |
| 422 | + Delete delete = |
| 423 | + new Delete(new Key("p1", "val1")).forNamespace(NAMESPACE).forTable(TABLE); |
| 424 | + jdbcDatabase.mutate(Arrays.asList(put, delete)); |
| 425 | + }) |
| 426 | + .isEqualTo(exception); |
| 427 | + verify(connection).setAutoCommit(false); |
| 428 | + verify(jdbcService, never()).mutate(any(), any()); |
| 429 | + verify(connection, never()).rollback(); |
| 430 | + verify(connection).close(); |
| 431 | + } |
385 | 432 | }
|
0 commit comments