|
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; |
| 6 | +import static org.mockito.Mockito.doThrow; |
| 7 | +import static org.mockito.Mockito.never; |
5 | 8 | import static org.mockito.Mockito.verify;
|
6 | 9 | import static org.mockito.Mockito.when;
|
7 | 10 |
|
@@ -125,6 +128,25 @@ public void whenScanOperationExecutedAndScannerClosed_shouldCallJdbcService() th
|
125 | 128 | verify(connection).close();
|
126 | 129 | }
|
127 | 130 |
|
| 131 | + @Test |
| 132 | + public void |
| 133 | + whenScanOperationExecutedAndJdbcServiceThrowsIllegalArgumentException_shouldCloseConnectionAndThrowIllegalArgumentException() |
| 134 | + throws Exception { |
| 135 | + // Arrange |
| 136 | + Exception cause = new IllegalArgumentException("Table not found"); |
| 137 | + // Simulate the table not found scenario. |
| 138 | + when(jdbcService.getScanner(any(), any())).thenThrow(cause); |
| 139 | + |
| 140 | + // Act Assert |
| 141 | + assertThatThrownBy( |
| 142 | + () -> { |
| 143 | + Scan scan = new Scan(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE); |
| 144 | + jdbcDatabase.scan(scan); |
| 145 | + }) |
| 146 | + .isInstanceOf(IllegalArgumentException.class); |
| 147 | + verify(connection).close(); |
| 148 | + } |
| 149 | + |
128 | 150 | @Test
|
129 | 151 | public void whenPutOperationExecuted_shouldCallJdbcService() throws Exception {
|
130 | 152 | // Arrange
|
@@ -330,4 +352,30 @@ public void mutate_withConflictError_shouldThrowRetriableExecutionException()
|
330 | 352 | .isInstanceOf(RetriableExecutionException.class);
|
331 | 353 | verify(connection).close();
|
332 | 354 | }
|
| 355 | + |
| 356 | + @Test |
| 357 | + public void mutate_WhenSettingAutoCommitFails_ShouldThrowExceptionAndCloseConnection() |
| 358 | + throws SQLException, ExecutionException { |
| 359 | + // Arrange |
| 360 | + Exception exception = new RuntimeException("Failed to set auto-commit"); |
| 361 | + doThrow(exception).when(connection).setAutoCommit(anyBoolean()); |
| 362 | + |
| 363 | + // Act Assert |
| 364 | + assertThatThrownBy( |
| 365 | + () -> { |
| 366 | + Put put = |
| 367 | + new Put(new Key("p1", "val1")) |
| 368 | + .withValue("v1", "val2") |
| 369 | + .forNamespace(NAMESPACE) |
| 370 | + .forTable(TABLE); |
| 371 | + Delete delete = |
| 372 | + new Delete(new Key("p1", "val1")).forNamespace(NAMESPACE).forTable(TABLE); |
| 373 | + jdbcDatabase.mutate(Arrays.asList(put, delete)); |
| 374 | + }) |
| 375 | + .isEqualTo(exception); |
| 376 | + verify(connection).setAutoCommit(false); |
| 377 | + verify(jdbcService, never()).mutate(any(), any()); |
| 378 | + verify(connection, never()).rollback(); |
| 379 | + verify(connection).close(); |
| 380 | + } |
333 | 381 | }
|
0 commit comments