This project has retired. For details please refer to its Attic page.
NavigationServiceTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.inmemory;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.math.BigInteger;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.chemistry.opencmis.commons.PropertyIds;
30  import org.apache.chemistry.opencmis.commons.data.ObjectData;
31  import org.apache.chemistry.opencmis.commons.data.ObjectInFolderContainer;
32  import org.apache.chemistry.opencmis.commons.data.ObjectInFolderData;
33  import org.apache.chemistry.opencmis.commons.data.ObjectInFolderList;
34  import org.apache.chemistry.opencmis.commons.data.Properties;
35  import org.apache.chemistry.opencmis.commons.data.PropertyData;
36  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
37  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
38  import org.apache.chemistry.opencmis.inmemory.types.InMemoryFolderTypeDefinition;
39  import org.apache.chemistry.opencmis.util.repository.ObjectGenerator;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.junit.After;
43  import org.junit.Before;
44  import org.junit.Test;
45  
46  /**
47   * @author Jens
48   */
49  public class NavigationServiceTest extends AbstractServiceTest {
50      private static final Log log = LogFactory.getLog(NavigationServiceTest.class);
51      private static final int NUM_ROOT_FOLDERS = 10;
52      private String fLevel1FolderId;
53  
54      @Override
55      @Before
56      public void setUp() {
57          super.setUp();
58      }
59  
60      @Override
61      @After
62      public void tearDown() {
63          super.tearDown();
64      }
65  
66      @Test
67      public void testGetChildren() {
68          log.info("starting testGetChildren() ...");
69          createLevel1Folders();
70  
71          log.info("test getting all objects with getChildren");
72          BigInteger maxItems = BigInteger.valueOf(NUM_ROOT_FOLDERS * 2);
73          BigInteger skipCount = BigInteger.valueOf(0);
74          ObjectInFolderList result = fNavSvc.getChildren(fRepositoryId, fRootFolderId, "*", null, false,
75                  IncludeRelationships.NONE, null, true, maxItems, skipCount, null);
76          List<ObjectInFolderData> folders = result.getObjects();
77          log.info(" found " + folders.size() + " folders in getChildren()");
78          for (ObjectInFolderData folder : folders) {
79              log.info("   found folder id " + folder.getObject().getId() + " path segment " + folder.getPathSegment());
80          }
81          assertEquals(NUM_ROOT_FOLDERS, folders.size());
82  
83          log.info("test paging with getChildren");
84          maxItems = BigInteger.valueOf(3);
85          skipCount = BigInteger.valueOf(3);
86          result = fNavSvc.getChildren(fRepositoryId, fRootFolderId, "*", null, false, IncludeRelationships.NONE, null,
87                  true, maxItems, skipCount, null);
88          folders = result.getObjects();
89          log.info(" found " + folders.size() + " folders in getChildren()");
90          for (ObjectInFolderData folder : folders) {
91              log.info("   found folder id " + folder.getObject().getId() + " path segment " + folder.getPathSegment());
92          }
93          assertEquals(3, folders.size());
94          assertEquals("Folder 3", folders.get(0).getPathSegment());
95          assertTrue(result.getNumItems().longValue() > 0);
96          log.info("... testGetChildren() finished.");
97      }
98  
99      @Test
100     public void testGetFolderTree() {
101         log.info("starting testGetFolderTree() ...");
102         createFolderHierachy(3, 5);
103 
104         log.info("test getting all objects with getFolderTree");
105         BigInteger depth = BigInteger.valueOf(-1);
106         Boolean includePathSegments = true;
107         String propertyFilter = "*";
108         String renditionFilter = null;
109         Boolean includeAllowableActions = false;
110         String objectId = fRootFolderId;
111 
112         List<ObjectInFolderContainer> tree = fNavSvc.getFolderTree(fRepositoryId, objectId, depth, propertyFilter,
113                 includeAllowableActions, IncludeRelationships.NONE, renditionFilter, includePathSegments, null);
114 
115         log.info("Descendants for object id " + objectId + " are: ");
116         for (ObjectInFolderContainer folder : tree) {
117             logFolderContainer(folder, 0);
118         }
119 
120         log.info("... testGetFolderTree() finished.");
121     }
122 
123     private void logFolderContainer(ObjectInFolderContainer folder, int depth) {
124         StringBuilder prefix = new StringBuilder();
125         for (int i = 0; i < depth; i++) {
126             prefix.append("   ");
127         }
128 
129         log.info(prefix + "name: " + folder.getObject().getPathSegment());
130         List<ObjectInFolderContainer> children = folder.getChildren();
131         if (null != children) {
132             for (ObjectInFolderContainer child : children) {
133                 logFolderContainer(child, depth + 1);
134             }
135         }
136     }
137 
138     @Test
139     public void testGetDescendants() {
140         log.info("starting testGetDescendants() ...");
141         final int numLevels = 3;
142         final int childrenPerLevel = 3;
143         int objCount = createFolderHierachy(numLevels, childrenPerLevel);
144 
145         log.info("test getting all objects with getDescendants");
146         List<ObjectInFolderContainer> result = fNavSvc.getDescendants(fRepositoryId, fRootFolderId, BigInteger
147                 .valueOf(-1), "*", Boolean.TRUE, IncludeRelationships.NONE, null, Boolean.TRUE, null);
148 
149         for (ObjectInFolderContainer obj : result) {
150             log.info("   found folder id " + obj.getObject().getObject().getId() + " path segment "
151                     + obj.getObject().getPathSegment());
152         }
153         int sizeOfDescs = getSizeOfDescendants(result);
154         assertEquals(objCount, sizeOfDescs);
155 
156         log.info("test getting one level with getDescendants");
157         result = fNavSvc.getDescendants(fRepositoryId, fRootFolderId, BigInteger.valueOf(1), "*", Boolean.TRUE,
158                 IncludeRelationships.NONE, null, Boolean.TRUE, null);
159 
160         for (ObjectInFolderContainer obj : result) {
161             log.info("   found folder id " + obj.getObject().getObject().getId() + " path segment "
162                     + obj.getObject().getPathSegment());
163         }
164         sizeOfDescs = getSizeOfDescendants(result);
165         assertEquals(childrenPerLevel, sizeOfDescs);
166 
167         log.info("test getting two levels with getDescendants");
168         result = fNavSvc.getDescendants(fRepositoryId, fRootFolderId, BigInteger.valueOf(2), "*", Boolean.TRUE,
169                 IncludeRelationships.NONE, null, Boolean.TRUE, null);
170 
171         for (ObjectInFolderContainer obj : result) {
172             log.info("   found folder id " + obj.getObject().getObject().getId() + " path segment "
173                     + obj.getObject().getPathSegment());
174         }
175         sizeOfDescs = getSizeOfDescendants(result);
176         assertEquals(childrenPerLevel * childrenPerLevel + childrenPerLevel, sizeOfDescs);
177 
178         log.info("... testGetDescendants() finished.");
179     }
180 
181     @Test
182     public void testGetFolderParent() {
183         log.info("starting testGetFolderParent() ...");
184         createLevel1Folders();
185         String folderId = fLevel1FolderId;
186 
187         ObjectData result = fNavSvc.getFolderParent(fRepositoryId, folderId, null, null);
188         log.info(" found parent for id \'" + folderId + "\' is \'" + result.getId() + "\'");
189         assertEquals(fRootFolderId, result.getId()); // should be root folder
190 
191         folderId = fRootFolderId;
192         try {
193             result = fNavSvc.getFolderParent(fRepositoryId, folderId, null, null);
194             log.info(" found parent for id " + folderId + " is " + result.getId());
195             fail("Should not be possible to get parent for root folder");
196         } catch (Exception e) {
197             assertEquals(CmisInvalidArgumentException.class, e.getClass());
198             log.info(" getParent() for root folder raised expected exception");
199         }
200         log.info("... testGetFolderParent() finished.");
201     }
202 
203     private int getSizeOfDescendants(List<ObjectInFolderContainer> objs) {
204         int sum = 0;
205         if (null != objs) {
206             sum = objs.size();
207             for (ObjectInFolderContainer obj : objs) {
208                 if (null != obj.getChildren()) {
209                     sum += getSizeOfDescendants(obj.getChildren());
210                 }
211             }
212         }
213         return sum;
214     }
215 
216     private void createLevel1Folders() {
217         for (int i = 0; i < NUM_ROOT_FOLDERS; i++) {
218             List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
219             properties.add(fFactory.createPropertyIdData(PropertyIds.NAME, "Folder " + i));
220             properties.add(fFactory.createPropertyIdData(PropertyIds.OBJECT_TYPE_ID, InMemoryFolderTypeDefinition
221                     .getRootFolderType().getId()));
222             Properties props = fFactory.createPropertiesData(properties);
223             String id = fObjSvc.createFolder(fRepositoryId, props, fRootFolderId, null, null, null, null);
224             if (i == 3) {
225                 fLevel1FolderId = id;
226             }
227         }
228     }
229 
230     private int createFolderHierachy(int levels, int childrenPerLevel) {
231 
232         ObjectGenerator gen = new ObjectGenerator(fFactory, fNavSvc, fObjSvc, fRepSvc, fRepositoryId,
233                 ObjectGenerator.CONTENT_KIND.LoremIpsumText);
234         gen.createFolderHierachy(levels, childrenPerLevel, fRootFolderId);
235         int objCount = gen.getObjectsInTotal();
236         return objCount;
237     }
238 
239 }