|
28 | 28 | import static org.mockito.ArgumentMatchers.anyLong; |
29 | 29 | import static org.mockito.ArgumentMatchers.anyMap; |
30 | 30 | import static org.mockito.ArgumentMatchers.anyString; |
| 31 | +import static org.mockito.ArgumentMatchers.eq; |
| 32 | +import static org.mockito.Mockito.doNothing; |
31 | 33 | import static org.mockito.Mockito.doReturn; |
32 | 34 | import static org.mockito.Mockito.mock; |
33 | 35 | import static org.mockito.Mockito.when; |
|
36 | 38 | import java.io.IOException; |
37 | 39 | import java.nio.file.Files; |
38 | 40 | import java.nio.file.Path; |
| 41 | +import java.nio.file.Paths; |
| 42 | +import java.nio.file.StandardOpenOption; |
39 | 43 | import java.util.ArrayList; |
40 | 44 | import java.util.HashMap; |
41 | 45 | import java.util.List; |
@@ -622,4 +626,53 @@ public void deleteExtensionPayloadFile_DoesNothing_WhenActionIsNotTrivial() { |
622 | 626 | Mockito.verify(logger, Mockito.never()).trace(anyString(), any(), any(), any(), any()); |
623 | 627 | } |
624 | 628 | } |
| 629 | + |
| 630 | + @Test |
| 631 | + public void prepareExternalPayloadCreatesFileInNewDirectory() throws IOException { |
| 632 | + String extensionName = "test-extension"; |
| 633 | + Map<String, Object> details = Map.of("key", "value"); |
| 634 | + String result = provisioner.prepareExternalPayload(extensionName, details); |
| 635 | + assertNotNull(result); |
| 636 | + Path payloadFile = Paths.get(result); |
| 637 | + assertTrue(Files.exists(payloadFile)); |
| 638 | + assertTrue(Files.isRegularFile(payloadFile)); |
| 639 | + assertEquals("{\"key\":\"value\"}", Files.readString(payloadFile)); |
| 640 | + } |
| 641 | + |
| 642 | + @Test |
| 643 | + public void prepareExternalPayloadCreatesFileInExistingDirectory() throws IOException { |
| 644 | + String extensionName = "test-extension"; |
| 645 | + Map<String, Object> details = Map.of("key", "value"); |
| 646 | + Path existingDir = Paths.get(tempDataDir.getAbsolutePath(), extensionName); |
| 647 | + Files.createDirectories(existingDir); |
| 648 | + doNothing().when(provisioner).scheduleExtensionPayloadDirectoryCleanup(extensionName); |
| 649 | + String result = provisioner.prepareExternalPayload(extensionName, details); |
| 650 | + assertNotNull(result); |
| 651 | + Path payloadFile = Paths.get(result); |
| 652 | + assertTrue(Files.exists(payloadFile)); |
| 653 | + assertTrue(Files.isRegularFile(payloadFile)); |
| 654 | + assertEquals("{\"key\":\"value\"}", Files.readString(payloadFile)); |
| 655 | + } |
| 656 | + |
| 657 | + @Test |
| 658 | + public void prepareExternalPayloadSchedulesCleanupForExistingDirectory() throws IOException { |
| 659 | + String extensionName = "test-extension"; |
| 660 | + Map<String, Object> details = Map.of("key", "value"); |
| 661 | + Path existingDir = Paths.get(tempDataDir.getAbsolutePath(), extensionName); |
| 662 | + Files.createDirectories(existingDir); |
| 663 | + doNothing().when(provisioner).scheduleExtensionPayloadDirectoryCleanup(extensionName); |
| 664 | + provisioner.prepareExternalPayload(extensionName, details); |
| 665 | + } |
| 666 | + |
| 667 | + @Test(expected = IOException.class) |
| 668 | + public void prepareExternalPayloadThrowsExceptionWhenFileCannotBeWritten() throws IOException { |
| 669 | + String extensionName = "test-extension"; |
| 670 | + Map<String, Object> details = Map.of("key", "value"); |
| 671 | + Path existingDir = Paths.get(tempDataDir.getAbsolutePath(), extensionName); |
| 672 | + Files.createDirectories(existingDir); |
| 673 | + try (MockedStatic<Files> filesMock = Mockito.mockStatic(Files.class)) { |
| 674 | + filesMock.when(() -> Files.writeString(any(Path.class), anyString(), eq(StandardOpenOption.CREATE_NEW))).thenThrow(IOException.class); |
| 675 | + provisioner.prepareExternalPayload(extensionName, details); |
| 676 | + } |
| 677 | + } |
625 | 678 | } |
0 commit comments