|
27 | 27 | import au.csiro.pathling.config.ServerConfiguration; |
28 | 28 | import au.csiro.pathling.errors.AccessDeniedError; |
29 | 29 | import au.csiro.pathling.errors.ResourceNotFoundError; |
| 30 | +import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; |
30 | 31 | import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; |
31 | 32 | import jakarta.servlet.http.HttpServletResponse; |
32 | 33 | import java.util.Optional; |
33 | 34 | import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.ExecutionException; |
| 36 | +import java.util.concurrent.Future; |
34 | 37 | import org.hl7.fhir.instance.model.api.IBaseResource; |
35 | 38 | import org.hl7.fhir.r4.model.Parameters; |
36 | 39 | import org.hl7.fhir.r4.model.StringType; |
@@ -235,4 +238,76 @@ void cancelledJobReturns404() { |
235 | 238 | .isInstanceOf(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException.class) |
236 | 239 | .hasMessageContaining("DELETE request cancelled this job"); |
237 | 240 | } |
| 241 | + |
| 242 | + @Test |
| 243 | + void interruptedJobThrowsInternalError() { |
| 244 | + // A job that was interrupted should throw an InternalErrorException. |
| 245 | + // Create a future that will throw InterruptedException when get() is called. |
| 246 | + @SuppressWarnings("unchecked") |
| 247 | + final Future<IBaseResource> mockFuture = mock(Future.class); |
| 248 | + try { |
| 249 | + when(mockFuture.isDone()).thenReturn(true); |
| 250 | + when(mockFuture.isCancelled()).thenReturn(false); |
| 251 | + when(mockFuture.get()).thenThrow(new InterruptedException("Thread was interrupted")); |
| 252 | + } catch (final InterruptedException | ExecutionException e) { |
| 253 | + throw new RuntimeException(e); |
| 254 | + } |
| 255 | + |
| 256 | + final Job<IBaseResource> job = new Job<>(JOB_ID, "view-export", mockFuture, Optional.empty()); |
| 257 | + job.setRedirectOnComplete(true); |
| 258 | + jobRegistry.register(job); |
| 259 | + |
| 260 | + assertThatThrownBy(() -> jobResultProvider.jobResult(JOB_ID, request, response)) |
| 261 | + .isInstanceOf(InternalErrorException.class) |
| 262 | + .hasMessageContaining("Job was interrupted"); |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + void errorUnwrappingHandlesDirectCause() { |
| 267 | + // Test that errors with a direct cause are properly unwrapped. |
| 268 | + final CompletableFuture<IBaseResource> future = new CompletableFuture<>(); |
| 269 | + future.completeExceptionally(new InvalidRequestException("Direct error")); |
| 270 | + |
| 271 | + final Job<IBaseResource> job = new Job<>(JOB_ID, "view-export", future, Optional.empty()); |
| 272 | + job.setRedirectOnComplete(true); |
| 273 | + jobRegistry.register(job); |
| 274 | + |
| 275 | + assertThatThrownBy(() -> jobResultProvider.jobResult(JOB_ID, request, response)) |
| 276 | + .isInstanceOf(InvalidRequestException.class) |
| 277 | + .hasMessageContaining("Direct error"); |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + void errorUnwrappingHandlesNestedCause() { |
| 282 | + // Test that errors with a nested cause (wrapped in IllegalStateException) are properly |
| 283 | + // unwrapped. |
| 284 | + final CompletableFuture<IBaseResource> future = new CompletableFuture<>(); |
| 285 | + future.completeExceptionally( |
| 286 | + new IllegalStateException( |
| 287 | + "Outer wrapper", new InvalidRequestException("Nested error message"))); |
| 288 | + |
| 289 | + final Job<IBaseResource> job = new Job<>(JOB_ID, "view-export", future, Optional.empty()); |
| 290 | + job.setRedirectOnComplete(true); |
| 291 | + jobRegistry.register(job); |
| 292 | + |
| 293 | + assertThatThrownBy(() -> jobResultProvider.jobResult(JOB_ID, request, response)) |
| 294 | + .isInstanceOf(InvalidRequestException.class) |
| 295 | + .hasMessageContaining("Nested error message"); |
| 296 | + } |
| 297 | + |
| 298 | + @Test |
| 299 | + void nullJobIdReturns404() { |
| 300 | + // A null job ID should return 404 Not Found. |
| 301 | + assertThatThrownBy(() -> jobResultProvider.jobResult(null, request, response)) |
| 302 | + .isInstanceOf(ResourceNotFoundError.class) |
| 303 | + .hasMessageContaining("Job ID not found"); |
| 304 | + } |
| 305 | + |
| 306 | + @Test |
| 307 | + void invalidJobIdFormatReturns404() { |
| 308 | + // An invalid job ID format should return 404 Not Found. |
| 309 | + assertThatThrownBy(() -> jobResultProvider.jobResult("not-a-valid-uuid", request, response)) |
| 310 | + .isInstanceOf(ResourceNotFoundError.class) |
| 311 | + .hasMessageContaining("Job ID not found"); |
| 312 | + } |
238 | 313 | } |
0 commit comments