PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/components/camel-box/camel-box-component/src/test/java/org/apache/camel/component/box/BoxFoldersManagerIntegrationTest.java

https://github.com/gnodet/camel
Java | 329 lines | 211 code | 60 blank | 58 comment | 5 complexity | 0f93b0cea9778228b930ae494b3352f6 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.box;
  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import com.box.sdk.BoxAPIConnection;
  22. import com.box.sdk.BoxFolder;
  23. import com.box.sdk.BoxItem;
  24. import com.box.sdk.BoxSharedLink;
  25. import org.apache.camel.builder.RouteBuilder;
  26. import org.apache.camel.component.box.api.BoxFoldersManager;
  27. import org.apache.camel.component.box.internal.BoxApiCollection;
  28. import org.apache.camel.component.box.internal.BoxFoldersManagerApiMethod;
  29. import org.junit.jupiter.api.AfterEach;
  30. import org.junit.jupiter.api.BeforeEach;
  31. import org.junit.jupiter.api.Test;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import static org.junit.jupiter.api.Assertions.assertEquals;
  35. import static org.junit.jupiter.api.Assertions.assertFalse;
  36. import static org.junit.jupiter.api.Assertions.assertNotNull;
  37. /**
  38. * Test class for {@link BoxFoldersManager} APIs.
  39. */
  40. public class BoxFoldersManagerIntegrationTest extends AbstractBoxTestSupport {
  41. private static final Logger LOG = LoggerFactory.getLogger(BoxFoldersManagerIntegrationTest.class);
  42. private static final String PATH_PREFIX = BoxApiCollection.getCollection()
  43. .getApiName(BoxFoldersManagerApiMethod.class).getName();
  44. private static final String CAMEL_TEST_FOLDER = "CamelTestFolder";
  45. private static final String CAMEL_TEST_FOLDER_DESCRIPTION = "This is a description of CamelTestFolder";
  46. private static final String CAMEL_TEST_COPY_FOLDER = BoxFoldersManagerIntegrationTest.CAMEL_TEST_FOLDER + "_Copy";
  47. private static final String CAMEL_TEST_MOVE_FOLDER = BoxFoldersManagerIntegrationTest.CAMEL_TEST_FOLDER + "_Move";
  48. private static final String CAMEL_TEST_RENAME_FOLDER = BoxFoldersManagerIntegrationTest.CAMEL_TEST_FOLDER
  49. + "_Rename";
  50. private static final String CAMEL_TEST_ROOT_FOLDER_ID = "0";
  51. private static final String CAMEL_TEST_DESTINATION_FOLDER_ID = "0";
  52. @Test
  53. public void testCreateFolder() throws Exception {
  54. // delete folder created in test setup.
  55. deleteTestFolder();
  56. final Map<String, Object> headers = new HashMap<>();
  57. // parameter type is String
  58. headers.put("CamelBox.parentFolderId", "0");
  59. // parameter type is String
  60. headers.put("CamelBox.folderName", CAMEL_TEST_FOLDER);
  61. testFolder = requestBodyAndHeaders("direct://CREATEFOLDER", null, headers);
  62. assertNotNull(testFolder, "createFolder result");
  63. assertEquals(CAMEL_TEST_FOLDER, testFolder.getInfo().getName(), "createFolder folder name");
  64. LOG.debug("createFolder: " + testFolder);
  65. }
  66. @Test
  67. public void testCreateFolderByPath() throws Exception {
  68. // delete folder created in test setup.
  69. deleteTestFolder();
  70. final Map<String, Object> headers = new HashMap<>();
  71. // parameter type is String
  72. headers.put("CamelBox.parentFolderId", "0");
  73. // parameter type is String[]
  74. headers.put("CamelBox.path", new String[] { CAMEL_TEST_FOLDER });
  75. testFolder = requestBodyAndHeaders("direct://CREATEFOLDER", null, headers);
  76. assertNotNull(testFolder, "createFolder result");
  77. assertEquals(CAMEL_TEST_FOLDER, testFolder.getInfo().getName(), "createFolder folder name");
  78. LOG.debug("createFolder: " + testFolder);
  79. }
  80. @Test
  81. public void testDeleteFolder() throws Exception {
  82. // using String message body for single parameter "folderId"
  83. requestBody("direct://DELETEFOLDER", testFolder.getID());
  84. BoxFolder rootFolder = BoxFolder.getRootFolder(getConnection());
  85. Iterable<BoxItem.Info> it = rootFolder.search("^" + CAMEL_TEST_FOLDER + "$");
  86. int searchResults = sizeOfIterable(it);
  87. assertFalse(searchResults > 0, "deleteFolder exists");
  88. }
  89. @Test
  90. public void testCopyFolder() throws Exception {
  91. com.box.sdk.BoxFolder result = null;
  92. try {
  93. final Map<String, Object> headers = new HashMap<>();
  94. // parameter type is String
  95. headers.put("CamelBox.folderId", testFolder.getID());
  96. // parameter type is String
  97. headers.put("CamelBox.destinationFolderId", CAMEL_TEST_DESTINATION_FOLDER_ID);
  98. // parameter type is String
  99. headers.put("CamelBox.newName", CAMEL_TEST_COPY_FOLDER);
  100. result = requestBodyAndHeaders("direct://COPYFOLDER", null, headers);
  101. assertNotNull(result, "copyFolder result");
  102. assertEquals(CAMEL_TEST_COPY_FOLDER, result.getInfo().getName(), "copyFolder folder name");
  103. LOG.debug("copyFolder: " + result);
  104. } finally {
  105. if (result != null) {
  106. try {
  107. result.delete(true);
  108. } catch (Throwable t) {
  109. }
  110. }
  111. }
  112. }
  113. @Test
  114. public void testCreateSharedLink() throws Exception {
  115. final Map<String, Object> headers = new HashMap<>();
  116. // parameter type is String
  117. headers.put("CamelBox.folderId", testFolder.getID());
  118. // parameter type is com.box.sdk.BoxSharedLink.Access
  119. headers.put("CamelBox.access", BoxSharedLink.Access.COLLABORATORS);
  120. // parameter type is java.util.Date
  121. headers.put("CamelBox.unshareDate", null);
  122. // parameter type is com.box.sdk.BoxSharedLink.Permissions
  123. headers.put("CamelBox.permissions", new BoxSharedLink.Permissions());
  124. final com.box.sdk.BoxSharedLink result = requestBodyAndHeaders("direct://CREATEFOLDERSHAREDLINK", null,
  125. headers);
  126. assertNotNull(result, "createFolderSharedLink result");
  127. LOG.debug("createFolderSharedLink: " + result);
  128. }
  129. @Test
  130. public void testGetFolder() throws Exception {
  131. // using String[] message body for single parameter "path"
  132. final com.box.sdk.BoxFolder result = requestBody("direct://GETFOLDER", new String[] { CAMEL_TEST_FOLDER });
  133. assertNotNull(result, "getFolder result");
  134. assertEquals(testFolder.getID(), result.getID(), "getFolder folder id");
  135. LOG.debug("getFolder: " + result);
  136. }
  137. @Test
  138. public void testGetFolderInfo() throws Exception {
  139. final Map<String, Object> headers = new HashMap<>();
  140. // parameter type is String
  141. headers.put("CamelBox.folderId", testFolder.getID());
  142. // parameter type is String[]
  143. headers.put("CamelBox.fields", new String[] { "name" });
  144. final com.box.sdk.BoxFolder.Info result = requestBodyAndHeaders("direct://GETFOLDERINFO", null, headers);
  145. assertNotNull(result, "getFolderInfo result");
  146. assertNotNull(result.getName(), "getFolderInfo result.getName()");
  147. assertEquals(CAMEL_TEST_FOLDER, result.getName(), "getFolderInfo info name");
  148. LOG.debug("getFolderInfo: " + result);
  149. }
  150. @Test
  151. public void testGetFolderItems() throws Exception {
  152. final Map<String, Object> headers = new HashMap<>();
  153. // parameter type is String
  154. headers.put("CamelBox.folderId", CAMEL_TEST_ROOT_FOLDER_ID);
  155. // parameter type is Long
  156. headers.put("CamelBox.offset", null);
  157. // parameter type is Long
  158. headers.put("CamelBox.limit", null);
  159. // parameter type is String[]
  160. headers.put("CamelBox.fields", null);
  161. @SuppressWarnings("rawtypes")
  162. final java.util.Collection result = requestBodyAndHeaders("direct://GETFOLDERITEMS", null, headers);
  163. assertNotNull(result, "getFolderItems result");
  164. LOG.debug("getFolderItems: " + result);
  165. }
  166. @Test
  167. public void testGetRootFolder() throws Exception {
  168. final com.box.sdk.BoxFolder result = requestBody("direct://GETROOTFOLDER", null);
  169. assertNotNull(result, "getRootFolder result");
  170. LOG.debug("getRootFolder: " + result);
  171. }
  172. @Test
  173. public void testMoveFolder() throws Exception {
  174. final Map<String, Object> headers = new HashMap<>();
  175. // parameter type is String
  176. headers.put("CamelBox.folderId", testFolder.getID());
  177. // parameter type is String
  178. headers.put("CamelBox.destinationFolderId", CAMEL_TEST_DESTINATION_FOLDER_ID);
  179. // parameter type is String
  180. headers.put("CamelBox.newName", CAMEL_TEST_MOVE_FOLDER);
  181. final com.box.sdk.BoxFolder result = requestBodyAndHeaders("direct://MOVEFOLDER", null, headers);
  182. assertNotNull(result, "moveFolder result");
  183. assertEquals(CAMEL_TEST_MOVE_FOLDER, result.getInfo().getName(), "moveFolder folder name");
  184. LOG.debug("moveFolder: " + result);
  185. }
  186. @Test
  187. public void testRenameFolder() throws Exception {
  188. final Map<String, Object> headers = new HashMap<>();
  189. // parameter type is String
  190. headers.put("CamelBox.folderId", testFolder.getID());
  191. // parameter type is String
  192. headers.put("CamelBox.newFolderName", CAMEL_TEST_RENAME_FOLDER);
  193. final com.box.sdk.BoxFolder result = requestBodyAndHeaders("direct://RENAMEFOLDER", null, headers);
  194. assertNotNull(result, "renameFolder result");
  195. assertEquals(CAMEL_TEST_RENAME_FOLDER, result.getInfo().getName(), "moveFolder folder name");
  196. LOG.debug("renameFolder: " + result);
  197. }
  198. @Test
  199. public void testUpdateInfo() throws Exception {
  200. final BoxFolder.Info testFolderInfo = testFolder.getInfo();
  201. final Map<String, Object> headers = new HashMap<>();
  202. // parameter type is String
  203. headers.put("CamelBox.folderId", testFolder.getID());
  204. // parameter type is com.box.sdk.BoxFolder.Info
  205. testFolderInfo.setDescription(CAMEL_TEST_FOLDER_DESCRIPTION);
  206. headers.put("CamelBox.info", testFolderInfo);
  207. final com.box.sdk.BoxFolder result = requestBodyAndHeaders("direct://UPDATEFOLDERINFO", null, headers);
  208. assertNotNull(result, "updateInfo result");
  209. assertEquals(CAMEL_TEST_FOLDER_DESCRIPTION, result.getInfo().getDescription(), "update folder info description");
  210. LOG.debug("updateInfo: " + result);
  211. }
  212. @Override
  213. protected RouteBuilder createRouteBuilder() throws Exception {
  214. return new RouteBuilder() {
  215. public void configure() {
  216. // test route for copyFolder
  217. from("direct://COPYFOLDER").to("box://" + PATH_PREFIX + "/copyFolder");
  218. // test route for createFolder
  219. from("direct://CREATEFOLDER").to("box://" + PATH_PREFIX + "/createFolder");
  220. // test route for createFolderSharedLink
  221. from("direct://CREATEFOLDERSHAREDLINK").to("box://" + PATH_PREFIX + "/createFolderSharedLink");
  222. // test route for deleteFolder
  223. from("direct://DELETEFOLDER").to("box://" + PATH_PREFIX + "/deleteFolder?inBody=folderId");
  224. // test route for getFolder
  225. from("direct://GETFOLDER").to("box://" + PATH_PREFIX + "/getFolder?inBody=path");
  226. // test route for getFolderInfo
  227. from("direct://GETFOLDERINFO").to("box://" + PATH_PREFIX + "/getFolderInfo");
  228. // test route for getFolderItems
  229. from("direct://GETFOLDERITEMS").to("box://" + PATH_PREFIX + "/getFolderItems");
  230. // test route for getRootFolder
  231. from("direct://GETROOTFOLDER").to("box://" + PATH_PREFIX + "/getRootFolder");
  232. // test route for moveFolder
  233. from("direct://MOVEFOLDER").to("box://" + PATH_PREFIX + "/moveFolder");
  234. // test route for renameFolder
  235. from("direct://RENAMEFOLDER").to("box://" + PATH_PREFIX + "/renameFolder");
  236. // test route for updateFolderInfo
  237. from("direct://UPDATEFOLDERINFO").to("box://" + PATH_PREFIX + "/updateFolderInfo");
  238. }
  239. };
  240. }
  241. @BeforeEach
  242. public void setupTest() throws Exception {
  243. createTestFolder();
  244. }
  245. @AfterEach
  246. public void teardownTest() {
  247. deleteTestFolder();
  248. }
  249. public BoxAPIConnection getConnection() {
  250. BoxEndpoint endpoint = (BoxEndpoint) context().getEndpoint("box://" + PATH_PREFIX + "/copyFolder");
  251. return endpoint.getBoxConnection();
  252. }
  253. private void createTestFolder() {
  254. BoxFolder rootFolder = BoxFolder.getRootFolder(getConnection());
  255. testFolder = rootFolder.createFolder(CAMEL_TEST_FOLDER).getResource();
  256. }
  257. private int sizeOfIterable(Iterable<?> it) {
  258. if (it instanceof Collection) {
  259. return ((Collection<?>) it).size();
  260. } else {
  261. int i = 0;
  262. for (@SuppressWarnings("unused")
  263. Object obj : it) {
  264. i++;
  265. }
  266. return i;
  267. }
  268. }
  269. }