|
| 1 | +package com.bhyoo.onedrive; |
| 2 | + |
| 3 | +import com.bhyoo.onedrive.client.Client; |
| 4 | +import com.bhyoo.onedrive.container.AsyncJobMonitor; |
| 5 | +import com.bhyoo.onedrive.container.AsyncJobStatus; |
| 6 | +import com.bhyoo.onedrive.container.items.DriveItem; |
| 7 | +import com.bhyoo.onedrive.container.items.FileItem; |
| 8 | +import com.bhyoo.onedrive.container.items.FolderItem; |
| 9 | +import com.bhyoo.onedrive.container.items.pointer.PathPointer; |
| 10 | +import com.bhyoo.onedrive.exceptions.ErrorResponseException; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.junit.jupiter.api.AfterAll; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.net.URISyntaxException; |
| 17 | +import java.net.URL; |
| 18 | +import java.nio.file.Path; |
| 19 | +import java.nio.file.Paths; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.*; |
| 22 | + |
| 23 | +class TestCases { |
| 24 | + private static final String TEST_DIR_NAME = "___for_test"; |
| 25 | + private static Client client; |
| 26 | + |
| 27 | + @BeforeAll |
| 28 | + static void getClient() { |
| 29 | + assertNull(client); |
| 30 | + |
| 31 | + final String clientId = "0c4b69a8-adac-4ec1-b310-6c28ff9fa263"; |
| 32 | + final String[] scope = {"files.readwrite.all", "offline_access"}; |
| 33 | + final String redirectURL = "http://localhost:8080/"; |
| 34 | + final String clientSecret = "iqggDGYW25$;#$otqVUH024"; |
| 35 | + |
| 36 | + client = new Client(clientId, scope, redirectURL, clientSecret); |
| 37 | + |
| 38 | + |
| 39 | + assertNotNull(client); |
| 40 | + assertTrue(client.isLogin()); |
| 41 | + assertFalse(client.isExpired()); |
| 42 | + |
| 43 | + System.out.println(client.getFullToken()); |
| 44 | + |
| 45 | + assertNotNull(client.getAccessToken()); |
| 46 | + assertNotNull(client.getAuthCode()); |
| 47 | + assertNotNull(client.getClientId()); |
| 48 | + assertNotNull(client.getClientSecret()); |
| 49 | + assertNotNull(client.getFullToken()); |
| 50 | + assertNotNull(client.getRedirectURL()); |
| 51 | + assertNotNull(client.getRefreshToken()); |
| 52 | + assertNotNull(client.getTokenType()); |
| 53 | + assertArrayEquals(client.getScopes(), scope); |
| 54 | + assertNotEquals(client.getExpirationTime(), 0L); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterAll |
| 58 | + static void logout() throws ErrorResponseException { |
| 59 | + assertNotNull(client); |
| 60 | + assertTrue(client.isLogin()); |
| 61 | + |
| 62 | + client.deleteItem(PathPointer.root.resolve(TEST_DIR_NAME)); |
| 63 | + |
| 64 | + client.logout(); |
| 65 | + |
| 66 | + assertFalse(client.isLogin()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Scenario: |
| 71 | + * <p> |
| 72 | + * 1. create a directory on root dir |
| 73 | + * 2. upload an audio file to the dir |
| 74 | + * 3. rename the file |
| 75 | + * 4. copy the file to same directory with another name |
| 76 | + */ |
| 77 | + @Test |
| 78 | + void createDirAndUpload() throws ErrorResponseException, URISyntaxException { |
| 79 | + // Create directory '___for_test' in root directory of default drive of current account |
| 80 | + FolderItem testDir = client.createFolder(PathPointer.root, TEST_DIR_NAME); |
| 81 | + |
| 82 | + assertEquals(TEST_DIR_NAME, testDir.getName()); |
| 83 | + assertEquals( |
| 84 | + new PathPointer("/" + TEST_DIR_NAME, testDir.getDriveId()).toASCIIApi(), |
| 85 | + testDir.getPathPointer().toASCIIApi()); |
| 86 | + assertEquals(0L, testDir.getSize().longValue()); |
| 87 | + assertFalse(testDir.isDeleted()); |
| 88 | + assertFalse(testDir.isRoot()); |
| 89 | + assertFalse(testDir.isSpecial()); |
| 90 | + assertArrayEquals(new DriveItem[0], testDir.allChildren()); |
| 91 | + |
| 92 | + URL resource = ClassLoader.getSystemClassLoader().getResource("SampleAudio_0.7mb.mp3"); |
| 93 | + assertNotNull(resource); |
| 94 | + |
| 95 | + Path path = Paths.get(resource.toURI()); |
| 96 | + // Upload local file to '___for_test' asynchronously |
| 97 | + FileItem uploaded = (FileItem) testDir.simpleUploadFileAsync(path).awaitUninterruptibly().getNow(); |
| 98 | + |
| 99 | + assertNotNull(uploaded); |
| 100 | + assertEquals("SampleAudio_0.7mb.mp3", uploaded.getName()); |
| 101 | + assertEquals(TEST_DIR_NAME, uploaded.getParentReference().getPathPointer().getName()); |
| 102 | + |
| 103 | + // Rename the uploaded file to 'modified.mp3' |
| 104 | + uploaded.rename("modified.mp3"); |
| 105 | + |
| 106 | + assertNotNull(uploaded); |
| 107 | + assertEquals("modified.mp3", uploaded.getName()); |
| 108 | + |
| 109 | + // Copy the uploaded file to same directory with another name 'copied.mp3' |
| 110 | + @NotNull AsyncJobMonitor monitor = uploaded.copyTo(testDir, "copied.mp3"); |
| 111 | + |
| 112 | + System.out.println(monitor.getPercentageComplete()); |
| 113 | + // Wait until copy job is done |
| 114 | + while (monitor.getStatus() != AsyncJobStatus.COMPLETED) { |
| 115 | + monitor.update(); |
| 116 | + System.out.println(monitor.getPercentageComplete()); |
| 117 | + } |
| 118 | + |
| 119 | + assertTrue(testDir.isChildrenFetched()); |
| 120 | + assertEquals(0, testDir.childCount()); |
| 121 | + |
| 122 | + // Update children info `testDir` |
| 123 | + testDir.fetchChildren(); |
| 124 | + |
| 125 | + assertTrue(testDir.isChildrenFetched()); |
| 126 | + assertEquals(2, testDir.allChildren().length); |
| 127 | + for (DriveItem item : testDir.allChildren()) { |
| 128 | + assertTrue(item.getName().equals("copied.mp3") || item.getName().equals("modified.mp3")); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments