|
19 | 19 | package cn.vika.client.api; |
20 | 20 |
|
21 | 21 | import java.io.File; |
| 22 | +import java.net.URL; |
22 | 23 |
|
| 24 | +import cn.vika.client.api.http.ApiCredential; |
23 | 25 | import cn.vika.client.api.model.Attachment; |
24 | 26 | import cn.vika.core.http.ClassPathResourceLoader; |
25 | 27 | import cn.vika.core.http.FileResourceLoader; |
26 | 28 | import cn.vika.core.http.UrlResourceLoader; |
27 | 29 | import cn.vika.core.utils.UrlUtil; |
28 | 30 | import com.fasterxml.jackson.core.JsonProcessingException; |
29 | | -import org.junit.jupiter.api.BeforeAll; |
30 | 31 | import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
31 | 32 | import org.junit.jupiter.api.Order; |
32 | 33 | import org.junit.jupiter.api.Test; |
33 | 34 | import org.junit.jupiter.api.TestMethodOrder; |
34 | 35 |
|
| 36 | +import static cn.vika.client.api.ConstantKey.TEST_API_KEY; |
35 | 37 | import static cn.vika.client.api.ConstantKey.TEST_DATASHEET_ID; |
| 38 | +import static cn.vika.client.api.ConstantKey.TEST_HOST_URL; |
36 | 39 | import static org.assertj.core.api.Assertions.assertThat; |
37 | 40 |
|
38 | 41 | /** |
39 | | - * attachment test |
40 | | - * |
41 | | - * @author Zoe Zheng |
42 | | - * @date 2020-12-17 18:49:24 |
| 42 | + * Attachment Api Test |
| 43 | + * @author Shawn Deng |
| 44 | + * @date 2021-02-24 16:33:58 |
43 | 45 | */ |
44 | 46 | @TestMethodOrder(OrderAnnotation.class) |
45 | | -public class AttachmentTest extends BaseTest { |
| 47 | +public class AttachmentTest { |
46 | 48 |
|
47 | | - private static VikaApiClient vikaApiClient; |
| 49 | + private final String DATASHEET_ID = TEST_DATASHEET_ID.get(); |
48 | 50 |
|
49 | | - @BeforeAll |
50 | | - public static void setup() { |
51 | | - vikaApiClient = testInitApiClient(); |
52 | | - } |
| 51 | + private final String HOST_URL = TEST_HOST_URL.get(); |
| 52 | + |
| 53 | + private final String API_KEY = TEST_API_KEY.get(); |
| 54 | + |
| 55 | + private final VikaApiClient vikaApiClient = new VikaApiClient(HOST_URL, new ApiCredential(API_KEY)); |
53 | 56 |
|
54 | 57 | @Test |
55 | 58 | @Order(1) |
56 | | - public void testUploadWithClassPathResource() throws JsonProcessingException { |
57 | | - Attachment attachment = vikaApiClient.getAttachmentApi().upload(TEST_DATASHEET_ID.get(), new ClassPathResourceLoader("test.txt")); |
| 59 | + void testUploadWithClassPathResource() throws JsonProcessingException, InterruptedException { |
| 60 | + Attachment attachment = vikaApiClient.getAttachmentApi().upload(DATASHEET_ID, new ClassPathResourceLoader("test.txt")); |
58 | 61 | assertThat(attachment).isNotNull(); |
59 | 62 | System.out.println(JacksonJsonUtil.toJson(attachment, true)); |
| 63 | + Thread.sleep(1000); |
60 | 64 | } |
61 | 65 |
|
62 | 66 | @Test |
63 | 67 | @Order(2) |
64 | | - public void testUploadWithUrlResource() throws JsonProcessingException { |
65 | | - Attachment attachment = vikaApiClient.getAttachmentApi().upload(TEST_DATASHEET_ID.get(), new UrlResourceLoader(UrlUtil.url("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3483500324,2196746779&fm=26&gp=0.jpg"))); |
| 68 | + void testUploadWithUrlResource() throws JsonProcessingException, InterruptedException { |
| 69 | + Attachment attachment = vikaApiClient.getAttachmentApi().upload(DATASHEET_ID, new UrlResourceLoader(UrlUtil.url("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3483500324,2196746779&fm=26&gp=0.jpg"))); |
66 | 70 | assertThat(attachment).isNotNull(); |
67 | 71 | System.out.println(JacksonJsonUtil.toJson(attachment, true)); |
| 72 | + Thread.sleep(1000); |
68 | 73 | } |
69 | 74 |
|
70 | 75 | @Test |
71 | 76 | @Order(3) |
72 | | - public void testUploadWithFileResource() throws JsonProcessingException { |
73 | | - File file = new File("/Users/shawndeng/Documents/Project/vika.java/client/src/test/resources/test.docx"); |
74 | | - Attachment attachment = vikaApiClient.getAttachmentApi().upload(TEST_DATASHEET_ID.get(), new FileResourceLoader(file)); |
| 77 | + void testUploadWithFileResource() throws JsonProcessingException, InterruptedException { |
| 78 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 79 | + URL url = classLoader.getResource("test.txt"); |
| 80 | + assertThat(url).isNotNull(); |
| 81 | + File file = new File(url.getFile()); |
| 82 | + assertThat(file).isNotNull(); |
| 83 | + Attachment attachment = vikaApiClient.getAttachmentApi().upload(DATASHEET_ID, new FileResourceLoader(file)); |
75 | 84 | assertThat(attachment).isNotNull(); |
76 | 85 | System.out.println(JacksonJsonUtil.toJson(attachment, true)); |
| 86 | + Thread.sleep(1000); |
77 | 87 | } |
78 | 88 |
|
79 | 89 | @Test |
80 | 90 | @Order(4) |
81 | | - public void testUploadWithFile() throws JsonProcessingException { |
82 | | - File file = new File("/Users/shawndeng/Documents/Project/vika.java/client/src/test/resources/test.docx"); |
83 | | - Attachment attachment = vikaApiClient.getAttachmentApi().upload(TEST_DATASHEET_ID.get(), file); |
| 91 | + void testUploadWithFile() throws JsonProcessingException { |
| 92 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 93 | + URL url = classLoader.getResource("test.xlsx"); |
| 94 | + assertThat(url).isNotNull(); |
| 95 | + File file = new File(url.getFile()); |
| 96 | + assertThat(file).isNotNull(); |
| 97 | + Attachment attachment = vikaApiClient.getAttachmentApi().upload(DATASHEET_ID, file); |
84 | 98 | assertThat(attachment).isNotNull(); |
85 | 99 | System.out.println(JacksonJsonUtil.toJson(attachment, true)); |
86 | 100 | } |
87 | | - |
88 | 101 | } |
0 commit comments