|
| 1 | +/* |
| 2 | + * Copyright 2025 Alibaba Group Holding Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.alibaba.opensandbox.sandbox.infrastructure.adapters.service |
| 18 | + |
| 19 | +import com.alibaba.opensandbox.sandbox.HttpClientProvider |
| 20 | +import com.alibaba.opensandbox.sandbox.config.ConnectionConfig |
| 21 | +import com.alibaba.opensandbox.sandbox.domain.exceptions.SandboxApiException |
| 22 | +import com.alibaba.opensandbox.sandbox.domain.exceptions.SandboxError |
| 23 | +import com.alibaba.opensandbox.sandbox.domain.models.sandboxes.SandboxEndpoint |
| 24 | +import com.alibaba.opensandbox.sandbox.infrastructure.adapters.converter.isFileNotFound |
| 25 | +import okhttp3.mockwebserver.MockResponse |
| 26 | +import okhttp3.mockwebserver.MockWebServer |
| 27 | +import org.junit.jupiter.api.AfterEach |
| 28 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 29 | +import org.junit.jupiter.api.Assertions.assertFalse |
| 30 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 31 | +import org.junit.jupiter.api.BeforeEach |
| 32 | +import org.junit.jupiter.api.Test |
| 33 | +import org.junit.jupiter.api.assertThrows |
| 34 | + |
| 35 | +class FilesystemAdapterTest { |
| 36 | + private lateinit var mockWebServer: MockWebServer |
| 37 | + private lateinit var filesystemAdapter: FilesystemAdapter |
| 38 | + private lateinit var httpClientProvider: HttpClientProvider |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + fun setUp() { |
| 42 | + mockWebServer = MockWebServer() |
| 43 | + mockWebServer.start() |
| 44 | + |
| 45 | + val host = mockWebServer.hostName |
| 46 | + val port = mockWebServer.port |
| 47 | + val endpoint = SandboxEndpoint("$host:$port") |
| 48 | + |
| 49 | + val config = |
| 50 | + ConnectionConfig.builder() |
| 51 | + .domain("$host:$port") |
| 52 | + .protocol("http") |
| 53 | + .build() |
| 54 | + |
| 55 | + httpClientProvider = HttpClientProvider(config) |
| 56 | + filesystemAdapter = FilesystemAdapter(httpClientProvider, endpoint) |
| 57 | + } |
| 58 | + |
| 59 | + @AfterEach |
| 60 | + fun tearDown() { |
| 61 | + mockWebServer.shutdown() |
| 62 | + httpClientProvider.close() |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `readFile surfaces FILE_NOT_FOUND error code on 404 so callers can distinguish it`() { |
| 67 | + mockWebServer.enqueue( |
| 68 | + MockResponse() |
| 69 | + .setResponseCode(404) |
| 70 | + .setBody( |
| 71 | + """{"code":"FILE_NOT_FOUND","message":"file not found. open /tmp/missing.txt: no such file or directory"}""", |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + val exception = |
| 76 | + assertThrows<SandboxApiException> { |
| 77 | + filesystemAdapter.readFile("/tmp/missing.txt", "UTF-8", null) |
| 78 | + } |
| 79 | + |
| 80 | + assertEquals(404, exception.statusCode) |
| 81 | + assertEquals(SandboxError.FILE_NOT_FOUND, exception.error.code) |
| 82 | + // The exception itself is recognised as a "not found" condition, which is what the |
| 83 | + // adapter relies on to avoid emitting ERROR-level log noise for an expected outcome. |
| 84 | + assertTrue(exception.isFileNotFound()) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `readFile returns content on success`() { |
| 89 | + mockWebServer.enqueue( |
| 90 | + MockResponse() |
| 91 | + .setResponseCode(200) |
| 92 | + .setBody("hello world"), |
| 93 | + ) |
| 94 | + |
| 95 | + val content = filesystemAdapter.readFile("/tmp/hello.txt", "UTF-8", null) |
| 96 | + |
| 97 | + assertEquals("hello world", content) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `isFileNotFound is true for FILE_NOT_FOUND error code`() { |
| 102 | + val exception = |
| 103 | + SandboxApiException( |
| 104 | + message = "Failed to read file. Status code: 404", |
| 105 | + statusCode = 404, |
| 106 | + error = SandboxError(SandboxError.FILE_NOT_FOUND), |
| 107 | + ) |
| 108 | + |
| 109 | + assertTrue(exception.isFileNotFound()) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun `isFileNotFound is false for other API errors`() { |
| 114 | + val exception = |
| 115 | + SandboxApiException( |
| 116 | + message = "Internal server error", |
| 117 | + statusCode = 500, |
| 118 | + error = SandboxError(SandboxError.UNEXPECTED_RESPONSE), |
| 119 | + ) |
| 120 | + |
| 121 | + assertFalse(exception.isFileNotFound()) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `isFileNotFound is false for a 404 without an explicit FILE_NOT_FOUND code`() { |
| 126 | + // A 404 whose body could not be parsed is mapped to UNEXPECTED_RESPONSE. It may indicate a |
| 127 | + // real endpoint/routing regression, so it must NOT be downgraded to a not-found condition. |
| 128 | + val exception = |
| 129 | + SandboxApiException( |
| 130 | + message = "Failed to read file. Status code: 404", |
| 131 | + statusCode = 404, |
| 132 | + error = SandboxError(SandboxError.UNEXPECTED_RESPONSE), |
| 133 | + ) |
| 134 | + |
| 135 | + assertFalse(exception.isFileNotFound()) |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun `isFileNotFound is false for non-sandbox exceptions`() { |
| 140 | + assertFalse(RuntimeException("boom").isFileNotFound()) |
| 141 | + } |
| 142 | +} |
0 commit comments