|
5 | 5 | import me.chanjar.weixin.common.error.WxErrorException; |
6 | 6 | import me.chanjar.weixin.cp.api.WxCpService; |
7 | 7 | import me.chanjar.weixin.cp.bean.WxCpBaseResp; |
8 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocData; |
9 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocGetDataRequest; |
10 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocAdminListResult; |
11 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocAdminRequest; |
12 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocImageUploadResult; |
13 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocModifyRequest; |
14 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocSmartSheetAuth; |
15 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocSmartSheetAuthRequest; |
16 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocSmartSheetModifyAuthRequest; |
17 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocSmartSheetRequest; |
18 | | -import me.chanjar.weixin.cp.bean.oa.doc.WxCpDocSmartSheetResult; |
| 8 | +import me.chanjar.weixin.cp.bean.oa.doc.*; |
19 | 9 | import me.chanjar.weixin.cp.config.WxCpConfigStorage; |
20 | 10 | import org.mockito.ArgumentCaptor; |
21 | 11 | import org.testng.annotations.Test; |
22 | 12 |
|
23 | 13 | import java.io.File; |
24 | 14 |
|
| 15 | +import static java.util.Collections.singletonList; |
25 | 16 | import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Oa.*; |
26 | 17 | import static org.assertj.core.api.Assertions.assertThat; |
27 | 18 | import static org.mockito.ArgumentMatchers.any; |
|
37 | 28 | public class WxCpOaWeDocServiceImplTest { |
38 | 29 |
|
39 | 30 | @Test |
40 | | - public void testNewApisUseExpectedPaths() throws WxErrorException { |
| 31 | + public void testLegacyApisUseExpectedPaths() throws WxErrorException { |
| 32 | + WxCpService cpService = mock(WxCpService.class); |
| 33 | + WxCpConfigStorage configStorage = mock(WxCpConfigStorage.class); |
| 34 | + when(cpService.getWxCpConfigStorage()).thenReturn(configStorage); |
| 35 | + when(configStorage.getApiUrl(WEDOC_CREATE_DOC)).thenReturn("https://api.test/create_doc"); |
| 36 | + when(configStorage.getApiUrl(WEDOC_RENAME_DOC)).thenReturn("https://api.test/rename_doc"); |
| 37 | + when(configStorage.getApiUrl(WEDOC_DEL_DOC)).thenReturn("https://api.test/del_doc"); |
| 38 | + when(configStorage.getApiUrl(WEDOC_GET_DOC_BASE_INFO)).thenReturn("https://api.test/get_doc_base_info"); |
| 39 | + when(configStorage.getApiUrl(WEDOC_DOC_SHARE)).thenReturn("https://api.test/doc_share"); |
| 40 | + when(configStorage.getApiUrl(WEDOC_DOC_GET_AUTH)).thenReturn("https://api.test/doc_get_auth"); |
| 41 | + when(configStorage.getApiUrl(WEDOC_MOD_DOC_JOIN_RULE)).thenReturn("https://api.test/mod_doc_join_rule"); |
| 42 | + when(configStorage.getApiUrl(WEDOC_MOD_DOC_MEMBER)).thenReturn("https://api.test/mod_doc_member"); |
| 43 | + when(configStorage.getApiUrl(WEDOC_MOD_DOC_SAFTY_SETTING)).thenReturn("https://api.test/mod_doc_safty_setting"); |
| 44 | + when(configStorage.getApiUrl(WEDOC_SPREADSHEET_BATCH_UPDATE)).thenReturn("https://api.test/spreadsheet/batch_update"); |
| 45 | + when(configStorage.getApiUrl(WEDOC_SPREADSHEET_GET_SHEET_PROPERTIES)).thenReturn("https://api.test/spreadsheet/get_sheet_properties"); |
| 46 | + when(configStorage.getApiUrl(WEDOC_SPREADSHEET_GET_SHEET_RANGE_DATA)).thenReturn("https://api.test/spreadsheet/get_sheet_range_data"); |
| 47 | + when(configStorage.getApiUrl(WEDOC_CREATE_FORM)).thenReturn("https://api.test/create_form"); |
| 48 | + when(configStorage.getApiUrl(WEDOC_MODIFY_FORM)).thenReturn("https://api.test/modify_form"); |
| 49 | + when(configStorage.getApiUrl(WEDOC_GET_FORM_INFO)).thenReturn("https://api.test/get_form_info"); |
| 50 | + when(configStorage.getApiUrl(WEDOC_GET_FORM_STATISTIC)).thenReturn("https://api.test/get_form_statistic"); |
| 51 | + when(configStorage.getApiUrl(WEDOC_GET_FORM_ANSWER)).thenReturn("https://api.test/get_form_answer"); |
| 52 | + |
| 53 | + when(cpService.post(eq("https://api.test/create_doc"), anyString())) |
| 54 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"url\":\"https://wedoc.test/doc/1\",\"docid\":\"doc1\"}"); |
| 55 | + when(cpService.post(eq("https://api.test/rename_doc"), anyString())) |
| 56 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}"); |
| 57 | + when(cpService.post(eq("https://api.test/del_doc"), anyString())) |
| 58 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}"); |
| 59 | + when(cpService.post(eq("https://api.test/get_doc_base_info"), anyString())) |
| 60 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"doc_base_info\":{\"docid\":\"doc1\",\"doc_name\":\"日报\",\"doc_type\":3}}"); |
| 61 | + when(cpService.post(eq("https://api.test/doc_share"), anyString())) |
| 62 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"share_url\":\"https://wedoc.test/share/1\"}"); |
| 63 | + when(cpService.post(eq("https://api.test/doc_get_auth"), anyString())) |
| 64 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"access_rule\":{\"enable_corp_internal\":true,\"corp_internal_auth\":1}}"); |
| 65 | + when(cpService.post(eq("https://api.test/mod_doc_join_rule"), anyString())) |
| 66 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}"); |
| 67 | + when(cpService.post(eq("https://api.test/mod_doc_member"), anyString())) |
| 68 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}"); |
| 69 | + when(cpService.post(eq("https://api.test/mod_doc_safty_setting"), anyString())) |
| 70 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}"); |
| 71 | + when(cpService.post(eq("https://api.test/spreadsheet/batch_update"), anyString())) |
| 72 | + .thenReturn("{\"add_sheet_response\":{\"properties\":{\"sheet_id\":\"sheet2\",\"title\":\"Sheet A\",\"row_count\":20,\"column_count\":5}},\"update_range_response\":{\"updated_cells\":2}}"); |
| 73 | + when(cpService.post(eq("https://api.test/spreadsheet/get_sheet_properties"), anyString())) |
| 74 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"properties\":[{\"sheet_id\":\"sheet1\",\"title\":\"Sheet A\",\"row_count\":20,\"column_count\":5}]}"); |
| 75 | + when(cpService.post(eq("https://api.test/spreadsheet/get_sheet_range_data"), anyString())) |
| 76 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"grid_data\":{\"start_row\":0,\"start_column\":0,\"rows\":[{\"values\":[{\"cell_value\":{\"text\":\"hello\"}}]}]}}"); |
| 77 | + when(cpService.post(eq("https://api.test/create_form"), anyString())) |
| 78 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"formid\":\"FORMID1\",\"url\":\"https://wedoc.test/form/1\"}"); |
| 79 | + when(cpService.post(eq("https://api.test/modify_form"), anyString())) |
| 80 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}"); |
| 81 | + when(cpService.post(eq("https://api.test/get_form_info"), anyString())) |
| 82 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"form_info\":{\"formid\":\"FORMID1\",\"form_title\":\"日报\"}}"); |
| 83 | + when(cpService.post(eq("https://api.test/get_form_statistic"), anyString())) |
| 84 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"fill_cnt\":3,\"submit_users\":[{\"userid\":\"zhangsan\",\"answer_id\":1}]}"); |
| 85 | + when(cpService.post(eq("https://api.test/get_form_answer"), anyString())) |
| 86 | + .thenReturn("{\"errcode\":0,\"errmsg\":\"ok\",\"answer\":{\"answer_list\":[{\"answer_id\":1,\"userid\":\"zhangsan\",\"reply\":{\"items\":[{\"question_id\":1,\"text_reply\":\"ok\"}]}}]}}"); |
| 87 | + |
| 88 | + WxCpOaWeDocServiceImpl service = new WxCpOaWeDocServiceImpl(cpService); |
| 89 | + |
| 90 | + WxCpDocCreateRequest createRequest = WxCpDocCreateRequest.builder() |
| 91 | + .spaceId("space1") |
| 92 | + .fatherId("father1") |
| 93 | + .docType(3) |
| 94 | + .docName("日报") |
| 95 | + .build(); |
| 96 | + WxCpDocCreateData createData = service.docCreate(createRequest); |
| 97 | + assertThat(createData.getDocId()).isEqualTo("doc1"); |
| 98 | + ArgumentCaptor<String> createBodyCaptor = ArgumentCaptor.forClass(String.class); |
| 99 | + verify(cpService).post(eq("https://api.test/create_doc"), createBodyCaptor.capture()); |
| 100 | + assertThat(createBodyCaptor.getValue()).contains("\"spaceid\":\"space1\""); |
| 101 | + assertThat(createBodyCaptor.getValue()).contains("\"doc_type\":3"); |
| 102 | + |
| 103 | + WxCpBaseResp renameResp = service.docRename(WxCpDocRenameRequest.builder() |
| 104 | + .docId("doc1") |
| 105 | + .newName("周报") |
| 106 | + .build()); |
| 107 | + assertThat(renameResp.getErrcode()).isZero(); |
| 108 | + ArgumentCaptor<String> renameBodyCaptor = ArgumentCaptor.forClass(String.class); |
| 109 | + verify(cpService).post(eq("https://api.test/rename_doc"), renameBodyCaptor.capture()); |
| 110 | + assertThat(renameBodyCaptor.getValue()).contains("\"new_name\":\"周报\""); |
| 111 | + |
| 112 | + WxCpBaseResp deleteResp = service.docDelete("doc1", null); |
| 113 | + assertThat(deleteResp.getErrcode()).isZero(); |
| 114 | + ArgumentCaptor<String> deleteBodyCaptor = ArgumentCaptor.forClass(String.class); |
| 115 | + verify(cpService).post(eq("https://api.test/del_doc"), deleteBodyCaptor.capture()); |
| 116 | + assertThat(deleteBodyCaptor.getValue()).contains("\"docid\":\"doc1\""); |
| 117 | + |
| 118 | + WxCpDocInfo docInfo = service.docInfo("doc1"); |
| 119 | + assertThat(docInfo.getDocBaseInfo().getDocName()).isEqualTo("日报"); |
| 120 | + verify(cpService).post(eq("https://api.test/get_doc_base_info"), anyString()); |
| 121 | + |
| 122 | + WxCpDocShare docShare = service.docShare("doc1"); |
| 123 | + assertThat(docShare.getShareUrl()).isEqualTo("https://wedoc.test/share/1"); |
| 124 | + verify(cpService).post(eq("https://api.test/doc_share"), anyString()); |
| 125 | + |
| 126 | + WxCpDocAuthInfo docAuthInfo = service.docGetAuth("doc1"); |
| 127 | + assertThat(docAuthInfo.getAccessRule().getEnableCorpInternal()).isTrue(); |
| 128 | + verify(cpService).post(eq("https://api.test/doc_get_auth"), anyString()); |
| 129 | + |
| 130 | + WxCpBaseResp joinRuleResp = service.docModifyJoinRule(WxCpDocModifyJoinRuleRequest.builder() |
| 131 | + .docId("doc1") |
| 132 | + .enableCorpInternal(true) |
| 133 | + .corpInternalAuth(1) |
| 134 | + .build()); |
| 135 | + assertThat(joinRuleResp.getErrcode()).isZero(); |
| 136 | + verify(cpService).post(eq("https://api.test/mod_doc_join_rule"), anyString()); |
| 137 | + |
| 138 | + WxCpDocAuthInfo.DocMember docMember = new WxCpDocAuthInfo.DocMember(); |
| 139 | + docMember.setType(1); |
| 140 | + docMember.setUserId("zhangsan"); |
| 141 | + docMember.setAuth(7); |
| 142 | + WxCpBaseResp modifyMemberResp = service.docModifyMember(WxCpDocModifyMemberRequest.builder() |
| 143 | + .docId("doc1") |
| 144 | + .updateFileMemberList(singletonList(docMember)) |
| 145 | + .build()); |
| 146 | + assertThat(modifyMemberResp.getErrcode()).isZero(); |
| 147 | + verify(cpService).post(eq("https://api.test/mod_doc_member"), anyString()); |
| 148 | + |
| 149 | + WxCpDocAuthInfo.Watermark watermark = new WxCpDocAuthInfo.Watermark(); |
| 150 | + watermark.setMarginType(2); |
| 151 | + watermark.setShowText(true); |
| 152 | + watermark.setText("wm"); |
| 153 | + WxCpBaseResp safetyResp = service.docModifySaftySetting(WxCpDocModifySaftySettingRequest.builder() |
| 154 | + .docId("doc1") |
| 155 | + .enableReadonlyCopy(true) |
| 156 | + .watermark(watermark) |
| 157 | + .build()); |
| 158 | + assertThat(safetyResp.getErrcode()).isZero(); |
| 159 | + verify(cpService).post(eq("https://api.test/mod_doc_safty_setting"), anyString()); |
| 160 | + |
| 161 | + WxCpDocSheetBatchUpdateRequest.Request.AddSheetRequest addSheetRequest = |
| 162 | + new WxCpDocSheetBatchUpdateRequest.Request.AddSheetRequest(); |
| 163 | + addSheetRequest.setTitle("Sheet A"); |
| 164 | + addSheetRequest.setRowCount(20); |
| 165 | + addSheetRequest.setColumnCount(5); |
| 166 | + WxCpDocSheetBatchUpdateRequest.Request batchRequest = new WxCpDocSheetBatchUpdateRequest.Request(); |
| 167 | + batchRequest.setAddSheetRequest(addSheetRequest); |
| 168 | + WxCpDocSheetBatchUpdateResponse batchUpdateResponse = service.docBatchUpdate(WxCpDocSheetBatchUpdateRequest.builder() |
| 169 | + .docId("doc1") |
| 170 | + .requests(singletonList(batchRequest)) |
| 171 | + .build()); |
| 172 | + assertThat(batchUpdateResponse.getAddSheetResponse().getProperties().getSheetId()).isEqualTo("sheet2"); |
| 173 | + verify(cpService).post(eq("https://api.test/spreadsheet/batch_update"), anyString()); |
| 174 | + |
| 175 | + WxCpDocSheetProperties sheetProperties = service.getSheetProperties("doc1"); |
| 176 | + assertThat(sheetProperties.getProperties()).hasSize(1); |
| 177 | + assertThat(sheetProperties.getProperties().get(0).getSheetId()).isEqualTo("sheet1"); |
| 178 | + ArgumentCaptor<String> sheetPropertiesBodyCaptor = ArgumentCaptor.forClass(String.class); |
| 179 | + verify(cpService).post(eq("https://api.test/spreadsheet/get_sheet_properties"), sheetPropertiesBodyCaptor.capture()); |
| 180 | + assertThat(sheetPropertiesBodyCaptor.getValue()).isEqualTo("{\"docid\":\"doc1\"}"); |
| 181 | + |
| 182 | + WxCpDocSheetData sheetData = service.getSheetRangeData(WxCpDocSheetGetDataRequest.builder() |
| 183 | + .docId("doc1") |
| 184 | + .sheetId("sheet1") |
| 185 | + .range("A1:B2") |
| 186 | + .build()); |
| 187 | + assertThat(sheetData.getGridData().getRows()).hasSize(1); |
| 188 | + verify(cpService).post(eq("https://api.test/spreadsheet/get_sheet_range_data"), anyString()); |
| 189 | + |
| 190 | + WxCpFormInfo formInfo = new WxCpFormInfo(); |
| 191 | + formInfo.setFormTitle("日报"); |
| 192 | + WxCpFormCreateResult formCreateResult = service.formCreate(WxCpFormCreateRequest.builder() |
| 193 | + .spaceId("space1") |
| 194 | + .fatherId("father1") |
| 195 | + .formInfo(formInfo) |
| 196 | + .build()); |
| 197 | + assertThat(formCreateResult.getFormId()).isEqualTo("FORMID1"); |
| 198 | + verify(cpService).post(eq("https://api.test/create_form"), anyString()); |
| 199 | + |
| 200 | + WxCpBaseResp formModifyResp = service.formModify(WxCpFormModifyRequest.builder() |
| 201 | + .oper(1) |
| 202 | + .formId("FORMID1") |
| 203 | + .formInfo(formInfo) |
| 204 | + .build()); |
| 205 | + assertThat(formModifyResp.getErrcode()).isZero(); |
| 206 | + verify(cpService).post(eq("https://api.test/modify_form"), anyString()); |
| 207 | + |
| 208 | + WxCpFormInfoResult formInfoResult = service.formInfo("FORMID1"); |
| 209 | + assertThat(formInfoResult.getFormInfo().getFormId()).isEqualTo("FORMID1"); |
| 210 | + ArgumentCaptor<String> formInfoBodyCaptor = ArgumentCaptor.forClass(String.class); |
| 211 | + verify(cpService).post(eq("https://api.test/get_form_info"), formInfoBodyCaptor.capture()); |
| 212 | + assertThat(formInfoBodyCaptor.getValue()).isEqualTo("{\"formid\":\"FORMID1\"}"); |
| 213 | + |
| 214 | + WxCpFormStatistic formStatistic = service.formStatistic(WxCpFormStatisticRequest.builder() |
| 215 | + .repeatedId("repeat-1") |
| 216 | + .build()); |
| 217 | + assertThat(formStatistic.getFillCnt()).isEqualTo(3); |
| 218 | + verify(cpService).post(eq("https://api.test/get_form_statistic"), anyString()); |
| 219 | + |
| 220 | + WxCpFormAnswer formAnswer = service.formAnswer(WxCpFormAnswerRequest.builder() |
| 221 | + .repeatedId("repeat-1") |
| 222 | + .answerIds(singletonList(1L)) |
| 223 | + .build()); |
| 224 | + assertThat(formAnswer.getAnswer().getAnswerList()).hasSize(1); |
| 225 | + verify(cpService).post(eq("https://api.test/get_form_answer"), anyString()); |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + public void testNewApisUseExpectedPaths() throws Exception { |
41 | 230 | WxCpService cpService = mock(WxCpService.class); |
42 | 231 | WxCpConfigStorage configStorage = mock(WxCpConfigStorage.class); |
43 | 232 | when(cpService.getWxCpConfigStorage()).thenReturn(configStorage); |
@@ -139,7 +328,9 @@ public void testNewApisUseExpectedPaths() throws WxErrorException { |
139 | 328 | assertThat(modifyResp.getErrcode()).isZero(); |
140 | 329 | verify(cpService).post(eq("https://api.test/mod_doc"), anyString()); |
141 | 330 |
|
142 | | - WxCpDocImageUploadResult uploadResult = service.docUploadImage(new File("demo.png")); |
| 331 | + File uploadFile = File.createTempFile("wedoc-upload-", ".png"); |
| 332 | + uploadFile.deleteOnExit(); |
| 333 | + WxCpDocImageUploadResult uploadResult = service.docUploadImage(uploadFile); |
143 | 334 | assertThat(uploadResult.getEffectiveUrl()).isEqualTo("https://img.test/a.png"); |
144 | 335 | assertThat(uploadResult.getMediaId()).isEqualTo("media-1"); |
145 | 336 | verify(cpService).upload(eq("https://api.test/upload_doc_image"), any()); |
|
0 commit comments