|
| 1 | +package org.flossware.filetransfer; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | + |
| 7 | +import java.lang.reflect.Method; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * Comprehensive tests for SftpFileTransferClient to achieve 100% coverage. |
| 13 | + * Note: Most methods require JSch connection which needs a live SFTP server or complex mocking. |
| 14 | + * These tests focus on builder validation, configuration, and path resolution logic. |
| 15 | + */ |
| 16 | +class SftpFileTransferClientTest { |
| 17 | + |
| 18 | + private SftpFileTransferClient client; |
| 19 | + |
| 20 | + @AfterEach |
| 21 | + void tearDown() throws Exception { |
| 22 | + if (client != null) { |
| 23 | + try { |
| 24 | + client.close(); |
| 25 | + } catch (Exception e) { |
| 26 | + // Ignore close errors in tearDown |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + @DisplayName("Should support builder chaining") |
| 33 | + void testBuilderChaining() { |
| 34 | + SftpFileTransferClient.Builder builder = SftpFileTransferClient.builder(); |
| 35 | + assertSame(builder, builder.host("example.com")); |
| 36 | + assertSame(builder, builder.port(22)); |
| 37 | + assertSame(builder, builder.username("user")); |
| 38 | + assertSame(builder, builder.password("pass")); |
| 39 | + assertSame(builder, builder.privateKey("/path/to/key")); |
| 40 | + assertSame(builder, builder.basePath("/base")); |
| 41 | + assertSame(builder, builder.knownHostsFile("/path/to/known_hosts")); |
| 42 | + assertSame(builder, builder.strictHostKeyChecking(true)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @DisplayName("Should throw NullPointerException when host is null") |
| 47 | + void testBuilderNullHost() { |
| 48 | + assertThrows(NullPointerException.class, |
| 49 | + () -> SftpFileTransferClient.builder() |
| 50 | + .username("user") |
| 51 | + .password("pass") |
| 52 | + .build()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @DisplayName("Should throw NullPointerException when username is null") |
| 57 | + void testBuilderNullUsername() { |
| 58 | + assertThrows(NullPointerException.class, |
| 59 | + () -> SftpFileTransferClient.builder() |
| 60 | + .host("example.com") |
| 61 | + .password("pass") |
| 62 | + .build()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @DisplayName("Should throw IllegalStateException when both password and privateKey are null") |
| 67 | + void testBuilderMissingAuth() { |
| 68 | + assertThrows(IllegalStateException.class, |
| 69 | + () -> SftpFileTransferClient.builder() |
| 70 | + .host("example.com") |
| 71 | + .username("user") |
| 72 | + .build()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @DisplayName("Should throw IllegalArgumentException for port less than 1") |
| 77 | + void testBuilderInvalidPortTooLow() { |
| 78 | + assertThrows(IllegalArgumentException.class, |
| 79 | + () -> SftpFileTransferClient.builder() |
| 80 | + .host("example.com") |
| 81 | + .port(0) |
| 82 | + .username("user") |
| 83 | + .password("pass") |
| 84 | + .build()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + @DisplayName("Should throw IllegalArgumentException for port greater than 65535") |
| 89 | + void testBuilderInvalidPortTooHigh() { |
| 90 | + assertThrows(IllegalArgumentException.class, |
| 91 | + () -> SftpFileTransferClient.builder() |
| 92 | + .host("example.com") |
| 93 | + .port(65536) |
| 94 | + .username("user") |
| 95 | + .password("pass") |
| 96 | + .build()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @DisplayName("Should build client with password authentication") |
| 101 | + void testBuildWithPassword() throws Exception { |
| 102 | + client = createTestClient("pass", null, null); |
| 103 | + assertNotNull(client); |
| 104 | + String description = client.getDescription(); |
| 105 | + assertTrue(description.contains("SFTP[")); |
| 106 | + assertTrue(description.contains("user@example.com:22")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @DisplayName("Should build client with private key authentication") |
| 111 | + void testBuildWithPrivateKey() throws Exception { |
| 112 | + client = createTestClient(null, "/path/to/key", null); |
| 113 | + assertNotNull(client); |
| 114 | + String description = client.getDescription(); |
| 115 | + assertTrue(description.contains("SFTP[")); |
| 116 | + assertTrue(description.contains("user@example.com:22")); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @DisplayName("Should build client with base path") |
| 121 | + void testBuildWithBasePath() throws Exception { |
| 122 | + client = createTestClient("pass", null, "/base"); |
| 123 | + assertNotNull(client); |
| 124 | + String description = client.getDescription(); |
| 125 | + assertTrue(description.contains("/base]")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + @DisplayName("Should build client with default port 22") |
| 130 | + void testBuildWithDefaultPort() throws Exception { |
| 131 | + client = SftpFileTransferClient.builder() |
| 132 | + .host("example.com") |
| 133 | + .username("user") |
| 134 | + .password("pass") |
| 135 | + .build(); |
| 136 | + |
| 137 | + assertNotNull(client); |
| 138 | + assertTrue(client.getDescription().contains(":22")); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + @DisplayName("Should build client with custom port") |
| 143 | + void testBuildWithCustomPort() throws Exception { |
| 144 | + client = createTestClientWithPort(2222); |
| 145 | + assertNotNull(client); |
| 146 | + assertTrue(client.getDescription().contains(":2222")); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + @DisplayName("Should resolve path without base path") |
| 151 | + void testResolvePathWithoutBasePath() throws Exception { |
| 152 | + client = createTestClient("pass", null, ""); |
| 153 | + |
| 154 | + Method resolvePath = SftpFileTransferClient.class.getDeclaredMethod("resolvePath", String.class); |
| 155 | + resolvePath.setAccessible(true); |
| 156 | + |
| 157 | + String result = (String) resolvePath.invoke(client, "test.txt"); |
| 158 | + assertEquals("/test.txt", result); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + @DisplayName("Should resolve path with base path") |
| 163 | + void testResolvePathWithBasePath() throws Exception { |
| 164 | + client = createTestClient("pass", null, "/base"); |
| 165 | + |
| 166 | + Method resolvePath = SftpFileTransferClient.class.getDeclaredMethod("resolvePath", String.class); |
| 167 | + resolvePath.setAccessible(true); |
| 168 | + |
| 169 | + String result = (String) resolvePath.invoke(client, "test.txt"); |
| 170 | + assertEquals("/base/test.txt", result); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + @DisplayName("Should resolve path with trailing slash in base path") |
| 175 | + void testResolvePathTrailingSlash() throws Exception { |
| 176 | + client = createTestClient("pass", null, "/base/"); |
| 177 | + |
| 178 | + Method resolvePath = SftpFileTransferClient.class.getDeclaredMethod("resolvePath", String.class); |
| 179 | + resolvePath.setAccessible(true); |
| 180 | + |
| 181 | + String result = (String) resolvePath.invoke(client, "test.txt"); |
| 182 | + assertEquals("/base/test.txt", result); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + @DisplayName("Should resolve path with multiple slashes") |
| 187 | + void testResolvePathMultipleSlashes() throws Exception { |
| 188 | + client = createTestClient("pass", null, "/base/"); |
| 189 | + |
| 190 | + Method resolvePath = SftpFileTransferClient.class.getDeclaredMethod("resolvePath", String.class); |
| 191 | + resolvePath.setAccessible(true); |
| 192 | + |
| 193 | + String result = (String) resolvePath.invoke(client, "/test.txt"); |
| 194 | + // Should handle leading slash in filename |
| 195 | + assertTrue(result.equals("/base//test.txt") || result.equals("/base/test.txt")); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + @DisplayName("Should return description with password auth") |
| 200 | + void testGetDescriptionPassword() throws Exception { |
| 201 | + client = createTestClient("pass", null, "/base"); |
| 202 | + |
| 203 | + String description = client.getDescription(); |
| 204 | + assertTrue(description.contains("SFTP[")); |
| 205 | + assertTrue(description.contains("user@example.com:22")); |
| 206 | + assertTrue(description.contains("/base]")); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + @DisplayName("Should return description with private key auth") |
| 211 | + void testGetDescriptionPrivateKey() throws Exception { |
| 212 | + client = createTestClient(null, "/path/to/key", ""); |
| 213 | + |
| 214 | + String description = client.getDescription(); |
| 215 | + assertTrue(description.contains("SFTP[")); |
| 216 | + assertTrue(description.contains("user@example.com:22")); |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + @DisplayName("Should close without error when not connected") |
| 221 | + void testCloseNotConnected() throws Exception { |
| 222 | + client = createTestClient("pass", null, null); |
| 223 | + |
| 224 | + assertDoesNotThrow(() -> client.close()); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + @DisplayName("Should throw NullPointerException when constructor receives null host") |
| 229 | + void testConstructorNullHost() throws Exception { |
| 230 | + java.lang.reflect.Constructor<SftpFileTransferClient> constructor = |
| 231 | + SftpFileTransferClient.class.getDeclaredConstructor( |
| 232 | + String.class, int.class, String.class, String.class, String.class, |
| 233 | + String.class, String.class, boolean.class); |
| 234 | + constructor.setAccessible(true); |
| 235 | + |
| 236 | + java.lang.reflect.InvocationTargetException exception = assertThrows( |
| 237 | + java.lang.reflect.InvocationTargetException.class, |
| 238 | + () -> constructor.newInstance(null, 22, "user", "pass", null, null, null, false)); |
| 239 | + |
| 240 | + assertTrue(exception.getCause() instanceof NullPointerException); |
| 241 | + assertTrue(exception.getCause().getMessage().contains("host cannot be null")); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + @DisplayName("Should throw NullPointerException when constructor receives null username") |
| 246 | + void testConstructorNullUsername() throws Exception { |
| 247 | + java.lang.reflect.Constructor<SftpFileTransferClient> constructor = |
| 248 | + SftpFileTransferClient.class.getDeclaredConstructor( |
| 249 | + String.class, int.class, String.class, String.class, String.class, |
| 250 | + String.class, String.class, boolean.class); |
| 251 | + constructor.setAccessible(true); |
| 252 | + |
| 253 | + java.lang.reflect.InvocationTargetException exception = assertThrows( |
| 254 | + java.lang.reflect.InvocationTargetException.class, |
| 255 | + () -> constructor.newInstance("example.com", 22, null, "pass", null, null, null, false)); |
| 256 | + |
| 257 | + assertTrue(exception.getCause() instanceof NullPointerException); |
| 258 | + assertTrue(exception.getCause().getMessage().contains("username cannot be null")); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + @DisplayName("Should verify DEFAULT_SESSION_TIMEOUT_MS constant") |
| 263 | + void testConstantSessionTimeout() throws Exception { |
| 264 | + java.lang.reflect.Field sessionTimeout = SftpFileTransferClient.class.getDeclaredField("DEFAULT_SESSION_TIMEOUT_MS"); |
| 265 | + sessionTimeout.setAccessible(true); |
| 266 | + assertEquals(30000, sessionTimeout.get(null)); |
| 267 | + } |
| 268 | + |
| 269 | + @Test |
| 270 | + @DisplayName("Should verify DEFAULT_CHANNEL_TIMEOUT_MS constant") |
| 271 | + void testConstantChannelTimeout() throws Exception { |
| 272 | + java.lang.reflect.Field channelTimeout = SftpFileTransferClient.class.getDeclaredField("DEFAULT_CHANNEL_TIMEOUT_MS"); |
| 273 | + channelTimeout.setAccessible(true); |
| 274 | + assertEquals(10000, channelTimeout.get(null)); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + @DisplayName("Should verify DEFAULT_BUFFER_SIZE constant") |
| 279 | + void testConstantBufferSize() throws Exception { |
| 280 | + java.lang.reflect.Field bufferSize = SftpFileTransferClient.class.getDeclaredField("DEFAULT_BUFFER_SIZE"); |
| 281 | + bufferSize.setAccessible(true); |
| 282 | + assertEquals(8192, bufferSize.get(null)); |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + @DisplayName("Should configure strict host key checking enabled") |
| 287 | + void testStrictHostKeyCheckingEnabled() throws Exception { |
| 288 | + client = SftpFileTransferClient.builder() |
| 289 | + .host("example.com") |
| 290 | + .username("user") |
| 291 | + .password("pass") |
| 292 | + .strictHostKeyChecking(true) |
| 293 | + .build(); |
| 294 | + |
| 295 | + assertNotNull(client); |
| 296 | + } |
| 297 | + |
| 298 | + @Test |
| 299 | + @DisplayName("Should configure strict host key checking disabled") |
| 300 | + void testStrictHostKeyCheckingDisabled() throws Exception { |
| 301 | + client = SftpFileTransferClient.builder() |
| 302 | + .host("example.com") |
| 303 | + .username("user") |
| 304 | + .password("pass") |
| 305 | + .strictHostKeyChecking(false) |
| 306 | + .build(); |
| 307 | + |
| 308 | + assertNotNull(client); |
| 309 | + } |
| 310 | + |
| 311 | + @Test |
| 312 | + @DisplayName("Should configure known hosts file") |
| 313 | + void testKnownHostsFile() throws Exception { |
| 314 | + client = SftpFileTransferClient.builder() |
| 315 | + .host("example.com") |
| 316 | + .username("user") |
| 317 | + .password("pass") |
| 318 | + .knownHostsFile("/path/to/known_hosts") |
| 319 | + .build(); |
| 320 | + |
| 321 | + assertNotNull(client); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + @DisplayName("Should accept valid port 1") |
| 326 | + void testValidPortMinimum() throws Exception { |
| 327 | + client = SftpFileTransferClient.builder() |
| 328 | + .host("example.com") |
| 329 | + .port(1) |
| 330 | + .username("user") |
| 331 | + .password("pass") |
| 332 | + .build(); |
| 333 | + |
| 334 | + assertNotNull(client); |
| 335 | + assertTrue(client.getDescription().contains(":1")); |
| 336 | + } |
| 337 | + |
| 338 | + @Test |
| 339 | + @DisplayName("Should accept valid port 65535") |
| 340 | + void testValidPortMaximum() throws Exception { |
| 341 | + client = SftpFileTransferClient.builder() |
| 342 | + .host("example.com") |
| 343 | + .port(65535) |
| 344 | + .username("user") |
| 345 | + .password("pass") |
| 346 | + .build(); |
| 347 | + |
| 348 | + assertNotNull(client); |
| 349 | + assertTrue(client.getDescription().contains(":65535")); |
| 350 | + } |
| 351 | + |
| 352 | + @Test |
| 353 | + @DisplayName("Should handle null basePath in constructor") |
| 354 | + void testConstructorNullBasePath() throws Exception { |
| 355 | + client = createTestClient("pass", null, null); |
| 356 | + assertNotNull(client); |
| 357 | + // Null basePath should not cause NPE |
| 358 | + String description = client.getDescription(); |
| 359 | + assertTrue(description.contains("SFTP[")); |
| 360 | + } |
| 361 | + |
| 362 | + @Test |
| 363 | + @DisplayName("Should handle both password and privateKey null in constructor") |
| 364 | + void testConstructorBothAuthNull() throws Exception { |
| 365 | + java.lang.reflect.Constructor<SftpFileTransferClient> constructor = |
| 366 | + SftpFileTransferClient.class.getDeclaredConstructor( |
| 367 | + String.class, int.class, String.class, String.class, String.class, |
| 368 | + String.class, String.class, boolean.class); |
| 369 | + constructor.setAccessible(true); |
| 370 | + |
| 371 | + SftpFileTransferClient testClient = constructor.newInstance( |
| 372 | + "example.com", 22, "user", null, null, null, null, false); |
| 373 | + assertNotNull(testClient); |
| 374 | + testClient.close(); |
| 375 | + } |
| 376 | + |
| 377 | + private SftpFileTransferClient createTestClient(String password, String privateKeyPath, String basePath) throws Exception { |
| 378 | + java.lang.reflect.Constructor<SftpFileTransferClient> constructor = |
| 379 | + SftpFileTransferClient.class.getDeclaredConstructor( |
| 380 | + String.class, int.class, String.class, String.class, String.class, |
| 381 | + String.class, String.class, boolean.class); |
| 382 | + constructor.setAccessible(true); |
| 383 | + |
| 384 | + return constructor.newInstance( |
| 385 | + "example.com", 22, "user", password, privateKeyPath, basePath, null, false); |
| 386 | + } |
| 387 | + |
| 388 | + private SftpFileTransferClient createTestClientWithPort(int port) throws Exception { |
| 389 | + java.lang.reflect.Constructor<SftpFileTransferClient> constructor = |
| 390 | + SftpFileTransferClient.class.getDeclaredConstructor( |
| 391 | + String.class, int.class, String.class, String.class, String.class, |
| 392 | + String.class, String.class, boolean.class); |
| 393 | + constructor.setAccessible(true); |
| 394 | + |
| 395 | + return constructor.newInstance( |
| 396 | + "example.com", port, "user", "pass", null, null, null, false); |
| 397 | + } |
| 398 | +} |
0 commit comments