|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.component.docling; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | + |
| 23 | +import ai.docling.serve.api.convert.response.ConvertDocumentResponse; |
| 24 | +import ai.docling.serve.api.convert.response.DocumentResponse; |
| 25 | +import org.apache.camel.CamelExecutionException; |
| 26 | +import org.apache.camel.Exchange; |
| 27 | +import org.apache.camel.builder.RouteBuilder; |
| 28 | +import org.apache.camel.support.DefaultExchange; |
| 29 | +import org.apache.camel.test.junit6.CamelTestSupport; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.*; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests the SUBMIT_ASYNC_CONVERSION and CHECK_CONVERSION_STATUS two-step async workflow. |
| 36 | + * |
| 37 | + * <p> |
| 38 | + * Before the fix, the {@code CompletionStage} returned by {@code convertSourceAsync()} was discarded and a fabricated |
| 39 | + * task ID with no server-side correlation was returned. CHECK_CONVERSION_STATUS would then fail because the server had |
| 40 | + * no record of the fake ID, and the error was silently masked by returning COMPLETED. |
| 41 | + * |
| 42 | + * <p> |
| 43 | + * After the fix, the {@code CompletableFuture} is stored in a local map keyed by the generated task ID. When |
| 44 | + * CHECK_CONVERSION_STATUS is called, it checks the local map first and returns the actual status of the async task. |
| 45 | + */ |
| 46 | +class DoclingAsyncConversionTest extends CamelTestSupport { |
| 47 | + |
| 48 | + @Test |
| 49 | + void submitReturnsTaskIdLinkedToFuture() throws Exception { |
| 50 | + DoclingEndpoint endpoint = context.getEndpoint( |
| 51 | + "docling:convert?operation=SUBMIT_ASYNC_CONVERSION&useDoclingServe=true", DoclingEndpoint.class); |
| 52 | + DoclingProducer producer = (DoclingProducer) endpoint.createProducer(); |
| 53 | + |
| 54 | + // Access the pendingAsyncTasks map via reflection to verify the future is stored |
| 55 | + Map<String, CompletableFuture<ConvertDocumentResponse>> pendingTasks = getPendingAsyncTasks(producer); |
| 56 | + assertNotNull(pendingTasks, "pendingAsyncTasks map should exist"); |
| 57 | + assertTrue(pendingTasks.isEmpty(), "pendingAsyncTasks should start empty"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void checkStatusReturnsFailedForUnknownTaskId() throws Exception { |
| 62 | + // When CHECK_CONVERSION_STATUS is called with an unknown task ID and the server |
| 63 | + // is not available, it should return FAILED — not COMPLETED (the old bug). |
| 64 | + try { |
| 65 | + Exchange exchange = new DefaultExchange(context); |
| 66 | + exchange.getIn().setHeader(DoclingHeaders.TASK_ID, "nonexistent-task-id"); |
| 67 | + exchange.getIn().setHeader(DoclingHeaders.OPERATION, DoclingOperations.CHECK_CONVERSION_STATUS); |
| 68 | + |
| 69 | + template.send("direct:check-status", exchange); |
| 70 | + |
| 71 | + Object body = exchange.getIn().getBody(); |
| 72 | + assertInstanceOf(ConversionStatus.class, body); |
| 73 | + ConversionStatus status = (ConversionStatus) body; |
| 74 | + |
| 75 | + // The key assertion: unknown task IDs should NOT return COMPLETED |
| 76 | + assertNotEquals(ConversionStatus.Status.COMPLETED, status.getStatus(), |
| 77 | + "Unknown task ID should not return COMPLETED status"); |
| 78 | + assertEquals(ConversionStatus.Status.FAILED, status.getStatus(), |
| 79 | + "Unknown task ID with unavailable server should return FAILED"); |
| 80 | + assertNotNull(status.getErrorMessage(), "Error message should be populated"); |
| 81 | + } catch (CamelExecutionException e) { |
| 82 | + // If the exchange throws instead of setting FAILED status, that's also acceptable — |
| 83 | + // the important thing is it doesn't silently return COMPLETED |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void checkStatusReturnsCompletedForFinishedLocalTask() throws Exception { |
| 89 | + DoclingEndpoint endpoint = context.getEndpoint( |
| 90 | + "docling:convert?operation=CHECK_CONVERSION_STATUS&useDoclingServe=true", DoclingEndpoint.class); |
| 91 | + DoclingProducer producer = (DoclingProducer) endpoint.createProducer(); |
| 92 | + |
| 93 | + // Manually insert a completed future into the pending tasks map |
| 94 | + Map<String, CompletableFuture<ConvertDocumentResponse>> pendingTasks = getPendingAsyncTasks(producer); |
| 95 | + |
| 96 | + // Create a completed future with a mock response |
| 97 | + ConvertDocumentResponse mockResponse = ConvertDocumentResponse.builder() |
| 98 | + .document(DocumentResponse.builder() |
| 99 | + .markdownContent("# Converted Document") |
| 100 | + .build()) |
| 101 | + .build(); |
| 102 | + CompletableFuture<ConvertDocumentResponse> completedFuture = CompletableFuture.completedFuture(mockResponse); |
| 103 | + pendingTasks.put("test-task-1", completedFuture); |
| 104 | + |
| 105 | + // Check the status — should find it in local map and return COMPLETED with result |
| 106 | + Exchange exchange = new DefaultExchange(context); |
| 107 | + exchange.getIn().setHeader(DoclingHeaders.TASK_ID, "test-task-1"); |
| 108 | + |
| 109 | + producer.process(exchange); |
| 110 | + |
| 111 | + Object body = exchange.getIn().getBody(); |
| 112 | + assertInstanceOf(ConversionStatus.class, body); |
| 113 | + ConversionStatus status = (ConversionStatus) body; |
| 114 | + assertEquals(ConversionStatus.Status.COMPLETED, status.getStatus()); |
| 115 | + assertNotNull(status.getResult(), "Result should contain the converted content"); |
| 116 | + |
| 117 | + // Future should be removed from map after completion |
| 118 | + assertFalse(pendingTasks.containsKey("test-task-1"), |
| 119 | + "Completed task should be removed from pending map"); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void checkStatusReturnsInProgressForPendingLocalTask() throws Exception { |
| 124 | + DoclingEndpoint endpoint = context.getEndpoint( |
| 125 | + "docling:convert?operation=CHECK_CONVERSION_STATUS&useDoclingServe=true", DoclingEndpoint.class); |
| 126 | + DoclingProducer producer = (DoclingProducer) endpoint.createProducer(); |
| 127 | + |
| 128 | + Map<String, CompletableFuture<ConvertDocumentResponse>> pendingTasks = getPendingAsyncTasks(producer); |
| 129 | + |
| 130 | + // Insert an incomplete future |
| 131 | + CompletableFuture<ConvertDocumentResponse> incompleteFuture = new CompletableFuture<>(); |
| 132 | + pendingTasks.put("test-task-2", incompleteFuture); |
| 133 | + |
| 134 | + Exchange exchange = new DefaultExchange(context); |
| 135 | + exchange.getIn().setHeader(DoclingHeaders.TASK_ID, "test-task-2"); |
| 136 | + |
| 137 | + producer.process(exchange); |
| 138 | + |
| 139 | + Object body = exchange.getIn().getBody(); |
| 140 | + assertInstanceOf(ConversionStatus.class, body); |
| 141 | + ConversionStatus status = (ConversionStatus) body; |
| 142 | + assertEquals(ConversionStatus.Status.IN_PROGRESS, status.getStatus()); |
| 143 | + |
| 144 | + // Future should remain in map since it's not done yet |
| 145 | + assertTrue(pendingTasks.containsKey("test-task-2"), |
| 146 | + "In-progress task should remain in pending map"); |
| 147 | + |
| 148 | + // Clean up |
| 149 | + incompleteFuture.cancel(true); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + void checkStatusReturnsFailedForExceptionalLocalTask() throws Exception { |
| 154 | + DoclingEndpoint endpoint = context.getEndpoint( |
| 155 | + "docling:convert?operation=CHECK_CONVERSION_STATUS&useDoclingServe=true", DoclingEndpoint.class); |
| 156 | + DoclingProducer producer = (DoclingProducer) endpoint.createProducer(); |
| 157 | + |
| 158 | + Map<String, CompletableFuture<ConvertDocumentResponse>> pendingTasks = getPendingAsyncTasks(producer); |
| 159 | + |
| 160 | + // Insert a failed future |
| 161 | + CompletableFuture<ConvertDocumentResponse> failedFuture = new CompletableFuture<>(); |
| 162 | + failedFuture.completeExceptionally(new RuntimeException("Server connection refused")); |
| 163 | + pendingTasks.put("test-task-3", failedFuture); |
| 164 | + |
| 165 | + Exchange exchange = new DefaultExchange(context); |
| 166 | + exchange.getIn().setHeader(DoclingHeaders.TASK_ID, "test-task-3"); |
| 167 | + |
| 168 | + producer.process(exchange); |
| 169 | + |
| 170 | + Object body = exchange.getIn().getBody(); |
| 171 | + assertInstanceOf(ConversionStatus.class, body); |
| 172 | + ConversionStatus status = (ConversionStatus) body; |
| 173 | + assertEquals(ConversionStatus.Status.FAILED, status.getStatus()); |
| 174 | + assertNotNull(status.getErrorMessage()); |
| 175 | + assertTrue(status.getErrorMessage().contains("Server connection refused")); |
| 176 | + |
| 177 | + // Failed task should be removed from map |
| 178 | + assertFalse(pendingTasks.containsKey("test-task-3"), |
| 179 | + "Failed task should be removed from pending map"); |
| 180 | + } |
| 181 | + |
| 182 | + @SuppressWarnings("unchecked") |
| 183 | + private Map<String, CompletableFuture<ConvertDocumentResponse>> getPendingAsyncTasks(DoclingProducer producer) |
| 184 | + throws Exception { |
| 185 | + Field field = DoclingProducer.class.getDeclaredField("pendingAsyncTasks"); |
| 186 | + field.setAccessible(true); |
| 187 | + return (Map<String, CompletableFuture<ConvertDocumentResponse>>) field.get(producer); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + protected RouteBuilder createRouteBuilder() { |
| 192 | + return new RouteBuilder() { |
| 193 | + @Override |
| 194 | + public void configure() { |
| 195 | + from("direct:check-status") |
| 196 | + .to("docling:convert?operation=CHECK_CONVERSION_STATUS&useDoclingServe=true"); |
| 197 | + } |
| 198 | + }; |
| 199 | + } |
| 200 | +} |
0 commit comments