allthingssecurity commented on code in PR #28072: URL: https://github.com/apache/camel/pull/28072#discussion_r4310844176
########## components/camel-azure/camel-azure-files/src/test/java/org/apache/camel/component/file/azure/FilesOperationsResumeDownloadTest.java: ########## @@ -0,0 +1,228 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.file.azure; + +import java.lang.reflect.Field; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpHeaderName; +import com.azure.core.http.HttpHeaders; +import com.azure.core.http.HttpRequest; +import com.azure.core.http.HttpResponse; +import com.azure.storage.common.policy.RequestRetryOptions; +import com.azure.storage.file.share.ShareDirectoryClient; +import com.azure.storage.file.share.ShareServiceClient; +import com.azure.storage.file.share.ShareServiceClientBuilder; +import com.azure.storage.file.share.models.ShareFileItem; +import org.apache.camel.Exchange; +import org.apache.camel.component.file.FileComponent; +import org.apache.camel.component.file.remote.RemoteFile; +import org.apache.camel.support.DefaultExchange; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * resumeDownload=true continues an interrupted download with a ranged GET, which the file service answers with 206 + * Partial Content. The fake service below answers like Azure Files: 200 without a range, 206 with a range that starts + * inside the file and 416 with a range that starts at or after its end. + */ +public class FilesOperationsResumeDownloadTest extends CamelTestSupport { + + private static final byte[] CONTENT = "0123456789".getBytes(StandardCharsets.US_ASCII); + + @TempDir + Path workDir; + + @Test + void resumedDownloadIsCompletedAndRenamed() throws Exception { + FakeFileService service = new FakeFileService(); + FilesOperations operations = operations(service); + // a previous download of the file was interrupted after 4 bytes + Files.write(workDir.resolve("hello.txt.inprogress"), Arrays.copyOf(CONTENT, 4)); + + boolean retrieved = operations.retrieveFile("hello.txt", exchange(), CONTENT.length); + + assertEquals(List.of("bytes=4-"), service.ranges, "the download is resumed with a range"); + assertTrue(retrieved, "a resumed download that returned the rest of the file must be retrieved"); + assertEquals("0123456789", Files.readString(workDir.resolve("hello.txt"), StandardCharsets.US_ASCII)); + assertFalse(Files.exists(workDir.resolve("hello.txt.inprogress"))); + } + + @Test + void downloadWithoutPartialFileIsRetrieved() throws Exception { + FakeFileService service = new FakeFileService(); + FilesOperations operations = operations(service); + + boolean retrieved = operations.retrieveFile("hello.txt", exchange(), CONTENT.length); + + assertEquals(1, service.ranges.size()); + assertEquals("none", service.ranges.get(0)); + assertTrue(retrieved); + assertEquals("0123456789", Files.readString(workDir.resolve("hello.txt"), StandardCharsets.US_ASCII)); + } Review Comment: Thanks. As Claus noted, an `.inprogress` file that already holds the whole file is listed under "Not changed" in the description: `retrieveFile` still throws for the 416, so this test would fail against this PR. Handling it needs a comparison with the remote file size, which I would rather do as a separate change than widen this one. _Claude Code on behalf of allthingssecurity_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
