|
18 | 18 | import com.scalar.db.api.Result; |
19 | 19 | import com.scalar.db.api.Scan; |
20 | 20 | import com.scalar.db.api.Scanner; |
| 21 | +import com.scalar.db.api.TransactionManagerCrudOperable; |
21 | 22 | import com.scalar.db.api.Update; |
22 | 23 | import com.scalar.db.api.Upsert; |
23 | 24 | import com.scalar.db.config.DatabaseConfig; |
|
28 | 29 | import com.scalar.db.exception.transaction.TransactionException; |
29 | 30 | import com.scalar.db.exception.transaction.UnsatisfiedConditionException; |
30 | 31 | import com.scalar.db.io.Key; |
| 32 | +import java.io.IOException; |
31 | 33 | import java.util.Arrays; |
| 34 | +import java.util.Iterator; |
32 | 35 | import java.util.List; |
33 | 36 | import java.util.Optional; |
34 | 37 | import org.junit.jupiter.api.BeforeEach; |
@@ -151,6 +154,178 @@ public void scan_ExecutionExceptionThrownByStorage_ShouldThrowCrudException() |
151 | 154 | .hasCause(exception); |
152 | 155 | } |
153 | 156 |
|
| 157 | + @Test |
| 158 | + public void getScannerAndScannerOne_ShouldReturnScannerAndShouldReturnProperResult() |
| 159 | + throws ExecutionException, TransactionException, IOException { |
| 160 | + // Arrange |
| 161 | + Scan scan = |
| 162 | + Scan.newBuilder().namespace("ns").table("tbl").partitionKey(Key.ofInt("id", 0)).build(); |
| 163 | + |
| 164 | + Result result1 = mock(Result.class); |
| 165 | + Result result2 = mock(Result.class); |
| 166 | + Result result3 = mock(Result.class); |
| 167 | + |
| 168 | + Scanner scanner = mock(Scanner.class); |
| 169 | + when(scanner.one()) |
| 170 | + .thenReturn(Optional.of(result1)) |
| 171 | + .thenReturn(Optional.of(result2)) |
| 172 | + .thenReturn(Optional.of(result3)) |
| 173 | + .thenReturn(Optional.empty()); |
| 174 | + |
| 175 | + when(storage.scan(scan)).thenReturn(scanner); |
| 176 | + |
| 177 | + // Act Assert |
| 178 | + TransactionManagerCrudOperable.Scanner actual = transactionManager.getScanner(scan); |
| 179 | + assertThat(actual.one()).hasValue(result1); |
| 180 | + assertThat(actual.one()).hasValue(result2); |
| 181 | + assertThat(actual.one()).hasValue(result3); |
| 182 | + assertThat(actual.one()).isEmpty(); |
| 183 | + actual.close(); |
| 184 | + |
| 185 | + verify(storage).scan(scan); |
| 186 | + verify(scanner).close(); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void getScannerAndScannerAll_ShouldReturnScannerAndShouldReturnProperResults() |
| 191 | + throws ExecutionException, TransactionException, IOException { |
| 192 | + // Arrange |
| 193 | + Scan scan = |
| 194 | + Scan.newBuilder().namespace("ns").table("tbl").partitionKey(Key.ofInt("id", 0)).build(); |
| 195 | + |
| 196 | + Result result1 = mock(Result.class); |
| 197 | + Result result2 = mock(Result.class); |
| 198 | + Result result3 = mock(Result.class); |
| 199 | + |
| 200 | + Scanner scanner = mock(Scanner.class); |
| 201 | + when(scanner.all()).thenReturn(Arrays.asList(result1, result2, result3)); |
| 202 | + |
| 203 | + when(storage.scan(scan)).thenReturn(scanner); |
| 204 | + |
| 205 | + // Act Assert |
| 206 | + TransactionManagerCrudOperable.Scanner actual = transactionManager.getScanner(scan); |
| 207 | + assertThat(actual.all()).containsExactly(result1, result2, result3); |
| 208 | + actual.close(); |
| 209 | + |
| 210 | + verify(storage).scan(scan); |
| 211 | + verify(scanner).close(); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void getScannerAndScannerIterator_ShouldReturnScannerAndShouldReturnProperResults() |
| 216 | + throws ExecutionException, TransactionException, IOException { |
| 217 | + // Arrange |
| 218 | + Scan scan = |
| 219 | + Scan.newBuilder().namespace("ns").table("tbl").partitionKey(Key.ofInt("id", 0)).build(); |
| 220 | + |
| 221 | + Result result1 = mock(Result.class); |
| 222 | + Result result2 = mock(Result.class); |
| 223 | + Result result3 = mock(Result.class); |
| 224 | + |
| 225 | + Scanner scanner = mock(Scanner.class); |
| 226 | + when(scanner.one()) |
| 227 | + .thenReturn(Optional.of(result1)) |
| 228 | + .thenReturn(Optional.of(result2)) |
| 229 | + .thenReturn(Optional.of(result3)) |
| 230 | + .thenReturn(Optional.empty()); |
| 231 | + |
| 232 | + when(storage.scan(scan)).thenReturn(scanner); |
| 233 | + |
| 234 | + // Act Assert |
| 235 | + TransactionManagerCrudOperable.Scanner actual = transactionManager.getScanner(scan); |
| 236 | + |
| 237 | + Iterator<Result> iterator = actual.iterator(); |
| 238 | + assertThat(iterator.hasNext()).isTrue(); |
| 239 | + assertThat(iterator.next()).isEqualTo(result1); |
| 240 | + assertThat(iterator.hasNext()).isTrue(); |
| 241 | + assertThat(iterator.next()).isEqualTo(result2); |
| 242 | + assertThat(iterator.hasNext()).isTrue(); |
| 243 | + assertThat(iterator.next()).isEqualTo(result3); |
| 244 | + assertThat(iterator.hasNext()).isFalse(); |
| 245 | + actual.close(); |
| 246 | + |
| 247 | + verify(storage).scan(scan); |
| 248 | + verify(scanner).close(); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void getScanner_WhenExecutionExceptionThrownByJdbcService_ShouldThrowCrudException() |
| 253 | + throws ExecutionException { |
| 254 | + // Arrange |
| 255 | + Scan scan = |
| 256 | + Scan.newBuilder().namespace("ns").table("tbl").partitionKey(Key.ofInt("id", 0)).build(); |
| 257 | + |
| 258 | + ExecutionException executionException = mock(ExecutionException.class); |
| 259 | + when(executionException.getMessage()).thenReturn("error"); |
| 260 | + when(storage.scan(scan)).thenThrow(executionException); |
| 261 | + |
| 262 | + // Act Assert |
| 263 | + assertThatThrownBy(() -> transactionManager.getScanner(scan)).isInstanceOf(CrudException.class); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void |
| 268 | + getScannerAndScannerOne_WhenExecutionExceptionThrownByScannerOne_ShouldThrowCrudException() |
| 269 | + throws ExecutionException, CrudException { |
| 270 | + // Arrange |
| 271 | + Scan scan = |
| 272 | + Scan.newBuilder().namespace("ns").table("tbl").partitionKey(Key.ofInt("id", 0)).build(); |
| 273 | + |
| 274 | + Scanner scanner = mock(Scanner.class); |
| 275 | + |
| 276 | + ExecutionException executionException = mock(ExecutionException.class); |
| 277 | + when(executionException.getMessage()).thenReturn("error"); |
| 278 | + when(scanner.one()).thenThrow(executionException); |
| 279 | + |
| 280 | + when(storage.scan(scan)).thenReturn(scanner); |
| 281 | + |
| 282 | + // Act Assert |
| 283 | + TransactionManagerCrudOperable.Scanner actual = transactionManager.getScanner(scan); |
| 284 | + assertThatThrownBy(actual::one).isInstanceOf(CrudException.class); |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + public void |
| 289 | + getScannerAndScannerAll_WhenExecutionExceptionThrownByScannerAll_ShouldThrowCrudException() |
| 290 | + throws ExecutionException, CrudException { |
| 291 | + // Arrange |
| 292 | + Scan scan = |
| 293 | + Scan.newBuilder().namespace("ns").table("tbl").partitionKey(Key.ofInt("id", 0)).build(); |
| 294 | + |
| 295 | + Scanner scanner = mock(Scanner.class); |
| 296 | + |
| 297 | + ExecutionException executionException = mock(ExecutionException.class); |
| 298 | + when(executionException.getMessage()).thenReturn("error"); |
| 299 | + when(scanner.all()).thenThrow(executionException); |
| 300 | + |
| 301 | + when(storage.scan(scan)).thenReturn(scanner); |
| 302 | + |
| 303 | + // Act Assert |
| 304 | + TransactionManagerCrudOperable.Scanner actual = transactionManager.getScanner(scan); |
| 305 | + assertThatThrownBy(actual::all).isInstanceOf(CrudException.class); |
| 306 | + } |
| 307 | + |
| 308 | + @Test |
| 309 | + public void |
| 310 | + getScannerAndScannerClose_WhenIOExceptionThrownByScannerClose_ShouldThrowCrudException() |
| 311 | + throws ExecutionException, CrudException, IOException { |
| 312 | + // Arrange |
| 313 | + Scan scan = |
| 314 | + Scan.newBuilder().namespace("ns").table("tbl").partitionKey(Key.ofInt("id", 0)).build(); |
| 315 | + |
| 316 | + Scanner scanner = mock(Scanner.class); |
| 317 | + |
| 318 | + IOException ioException = mock(IOException.class); |
| 319 | + when(ioException.getMessage()).thenReturn("error"); |
| 320 | + doThrow(ioException).when(scanner).close(); |
| 321 | + |
| 322 | + when(storage.scan(scan)).thenReturn(scanner); |
| 323 | + |
| 324 | + // Act Assert |
| 325 | + TransactionManagerCrudOperable.Scanner actual = transactionManager.getScanner(scan); |
| 326 | + assertThatThrownBy(actual::close).isInstanceOf(CrudException.class); |
| 327 | + } |
| 328 | + |
154 | 329 | @Test |
155 | 330 | public void put_ShouldCallStorageProperly() throws ExecutionException, TransactionException { |
156 | 331 | // Arrange |
|
0 commit comments