From fefee35dac8436d91839b5bd86aa9bc282581d36 Mon Sep 17 00:00:00 2001 From: Kristijan Conkas Date: Tue, 25 Oct 2016 16:45:47 +0100 Subject: [PATCH] RM-3964: Refactoring further to API implementation discussion. --- .../rest/model/FilePlanComponent.java | 7 +- .../model/FilePlanComponentsCollection.java | 24 +++ .../rest/requests/FilePlanComponentApi.java | 24 +++ .../RecordCategoryTest.java | 145 +++++++++++++++++- 4 files changed, 192 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/alfresco/rest/model/FilePlanComponentsCollection.java diff --git a/src/main/java/org/alfresco/rest/model/FilePlanComponent.java b/src/main/java/org/alfresco/rest/model/FilePlanComponent.java index 9403411bed..efe28755fc 100644 --- a/src/main/java/org/alfresco/rest/model/FilePlanComponent.java +++ b/src/main/java/org/alfresco/rest/model/FilePlanComponent.java @@ -16,21 +16,16 @@ import static org.alfresco.com.FilePlanComponentFields.PROPERTIES; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Component; - /** * POJO for file plan component * * @author Tuna Aksoy * @since 1.0 */ -@Component -@Scope(value = "prototype") //FIXME: Once the fields have been added the JsonIgnoreProperties annotation should be removed @JsonIgnoreProperties(ignoreUnknown = true) public class FilePlanComponent -{ +{ private String id; private String parentId; diff --git a/src/main/java/org/alfresco/rest/model/FilePlanComponentsCollection.java b/src/main/java/org/alfresco/rest/model/FilePlanComponentsCollection.java new file mode 100644 index 0000000000..8f8f9e1eba --- /dev/null +++ b/src/main/java/org/alfresco/rest/model/FilePlanComponentsCollection.java @@ -0,0 +1,24 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2016 Alfresco Software Limited + * %% + * License rights for this program may be obtained from Alfresco Software, Ltd. + * pursuant to a written agreement and any use of this program without such an + * agreement is prohibited. + * #L% + */ +package org.alfresco.rest.model; + +import org.alfresco.rest.core.RestModels; + +/** + * Handle collection of FilePlanComponents + * @author Kristijan Conkas + * @since 1.0 + */ +public class FilePlanComponentsCollection extends RestModels +{ + +} diff --git a/src/main/java/org/alfresco/rest/requests/FilePlanComponentApi.java b/src/main/java/org/alfresco/rest/requests/FilePlanComponentApi.java index 460e4eb23c..f2764539c6 100644 --- a/src/main/java/org/alfresco/rest/requests/FilePlanComponentApi.java +++ b/src/main/java/org/alfresco/rest/requests/FilePlanComponentApi.java @@ -24,6 +24,7 @@ import com.google.gson.JsonObject; import org.alfresco.rest.core.RestAPI; import org.alfresco.rest.model.FilePlanComponent; +import org.alfresco.rest.model.FilePlanComponentsCollection; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @@ -61,6 +62,29 @@ public class FilePlanComponentApi extends RestAPI )); } + /** + * List child components of a file plan component + * + * @param filePlanComponentId The id of the file plan component of which to get child components + * @return The {@link FilePlanComponent} for the given file plan component id + * @throws Exception for the following cases: + * + */ + public FilePlanComponentsCollection listChildComponents(String filePlanComponentId) throws Exception + { + mandatoryString("filePlanComponentId", filePlanComponentId); + + return usingRestWrapper().processModels(FilePlanComponentsCollection.class, simpleRequest( + GET, + "fileplan-components/{fileplanComponentId}/children", + filePlanComponentId + )); + } + /** * Creates a file plan component with the given properties under the parent node with the given id * diff --git a/src/test/java/org/alfresco/rest/fileplancomponents/RecordCategoryTest.java b/src/test/java/org/alfresco/rest/fileplancomponents/RecordCategoryTest.java index 54dce68633..3fe829770c 100644 --- a/src/test/java/org/alfresco/rest/fileplancomponents/RecordCategoryTest.java +++ b/src/test/java/org/alfresco/rest/fileplancomponents/RecordCategoryTest.java @@ -19,21 +19,29 @@ import static org.alfresco.com.FilePlanComponentFields.NODE_TYPE; import static org.alfresco.com.FilePlanComponentFields.PROPERTIES; import static org.alfresco.com.FilePlanComponentFields.PROPERTIES_TITLE; import static org.alfresco.com.FilePlanComponentType.RECORD_CATEGORY_TYPE; +import static org.alfresco.com.FilePlanComponentType.RECORD_FOLDER_TYPE; import static org.jglue.fluentjson.JsonBuilderFactory.buildObject; import static org.springframework.http.HttpStatus.CREATED; import static org.springframework.http.HttpStatus.NO_CONTENT; import static org.springframework.http.HttpStatus.OK; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; +import java.util.ArrayList; +import java.util.List; + import com.google.gson.JsonObject; +import org.alfresco.com.FilePlanComponentType; import org.alfresco.rest.BaseRestTest; import org.alfresco.rest.core.RestWrapper; import org.alfresco.rest.model.FilePlanComponent; import org.alfresco.rest.model.FilePlanComponentProperties; +import org.alfresco.rest.model.FilePlanComponentsCollection; import org.alfresco.rest.requests.FilePlanComponentApi; import org.alfresco.utility.data.DataUser; +import org.alfresco.utility.data.RandomData; import org.springframework.beans.factory.annotation.Autowired; import org.testng.annotations.Test; @@ -51,7 +59,16 @@ public class RecordCategoryTest extends BaseRestTest @Autowired private DataUser dataUser; + + // for children creation test + private static final int NUMBER_OF_CHILDREN = 10; + /** + *
+     * Given that a file plan exists
+     * When I ask the API to create a root record category
+     * Then it is created as a root record category
+     */
     @Test
     (
         description = "Create category as authorised user"
@@ -88,6 +105,12 @@ public class RecordCategoryTest extends BaseRestTest
         assertEquals(filePlanComponentProperties.getTitle(), categoryTitle);
     }
 
+    /**
+     * 
+     * Given that a record category exists
+     * When I ask the API to update the details of the record category
+     * Then the details of the record category are updated
+     */
     @Test
     (
         description = "Rename category as authorised user"
@@ -113,7 +136,6 @@ public class RecordCategoryTest extends BaseRestTest
         // Create the record category
         FilePlanComponent filePlanComponent = filePlanComponentApi.createFilePlanComponent(recordCategoryProperties, FILE_PLAN_ALIAS.toString());
 
-
         String newCategoryName = "Rename " + categoryName;
 
         // Build the properties which will be updated
@@ -131,9 +153,15 @@ public class RecordCategoryTest extends BaseRestTest
         assertEquals(renamedFilePlanComponent.getName(), newCategoryName);
     }
 
+    /**
+     * 
+     * Given that a record category exists
+     * When I ask the API to delete the record category
+     * Then the record category and all its contents is deleted
+     */
     @Test
     (
-        description = "Rename category as authorised user"
+        description = "Delete category as authorised user"
     )
     public void deleteCategoryAsAuthorisedUser() throws Exception
     {
@@ -162,4 +190,117 @@ public class RecordCategoryTest extends BaseRestTest
         // Verify the status code
         restWrapper.assertStatusCodeIs(NO_CONTENT);
     }
+    
+    /**
+     * 
+     * Given that a record category exists
+     * When I ask the API to create a record category
+     * Then it is created within the record category
+     */
+    @Test
+    (
+        description = "Create child category"
+    )
+    public void createSubcategory() throws Exception
+    {
+        // create root level category
+        FilePlanComponent rootCategory = createCategory(FILE_PLAN_ALIAS.toString(), RandomData.getRandomAlphanumeric());
+        assertNotNull(rootCategory.getId());
+        
+        // create subcategory as a child of rootCategory
+        FilePlanComponent childCategory = createCategory(rootCategory.getId(), RandomData.getRandomAlphanumeric());
+        // child category created?
+        assertNotNull(childCategory.getId());
+    }
+    
+    /**
+     * 
+     * Given that a record category exists
+     * And contains a number of record categories and record folders
+     * When I ask the APi to get me the children of the record category
+     * Then I am returned the contained record categories and record folders
+     * And their details
+     */
+    @Test
+    (
+        description = "List children of a category"
+    )
+    public void listChildren() throws Exception
+    {
+        // create root level category
+        FilePlanComponent rootCategory = createCategory(FILE_PLAN_ALIAS.toString(), RandomData.getRandomAlphanumeric());
+        assertNotNull(rootCategory.getId());
+        
+        // add child categories/folders
+        ArrayList children = new ArrayList();
+        for(int i=0; i < NUMBER_OF_CHILDREN; i++)
+        {
+            // create a child
+            FilePlanComponent child = createComponent(rootCategory.getId(), 
+                RandomData.getRandomAlphanumeric(),
+                // half of the children should be subcategories, the other subfolders
+                (i <= NUMBER_OF_CHILDREN / 2) ? RECORD_CATEGORY_TYPE : RECORD_FOLDER_TYPE);
+            assertNotNull(child.getId());
+            children.add(child);
+        }
+        
+        // list children from API
+        RestWrapper restWrapper = filePlanComponentApi.usingRestWrapper().authenticateUser(dataUser.getAdminUser());
+        FilePlanComponentsCollection apiChildren = filePlanComponentApi.listChildComponents(rootCategory.getId());
+        restWrapper.assertStatusCodeIs(OK);
+        
+        List childrenApi = apiChildren.getEntries();
+        childrenApi.forEach(c -> {
+            //assertNotNull(c.getId());
+            
+            logger.info(c + " id=" + c.getId() + " name=" + c.getName() + " properties=" + c.getProperties());
+        });
+    }
+    
+    /**
+     * Helper method to create child category
+     * @param parentCategoryId
+     * @param categoryName
+     * @throws Exception on unsuccessful component creation
+     */
+    private FilePlanComponent createCategory(String parentCategoryId, String categoryName) throws Exception
+    {
+        return createComponent(parentCategoryId, categoryName, RECORD_CATEGORY_TYPE);
+    }
+    
+    /**
+     * Helper method to create child folder
+     * @param parentComponentId parent category or folder id
+     * @param folderName new folder name
+     * @throws Exception on unsuccessful folder creation
+     */
+    private FilePlanComponent createFolder(String parentComponentId, String folderName) throws Exception
+    {
+        return createComponent(parentComponentId, folderName, RECORD_FOLDER_TYPE);
+    }
+    
+    /**
+     * Helper method to create generic child component
+     * @param parentComponentId
+     * @param componentName
+     * @param componentType
+     * @return
+     * @throws Exception
+     */
+    private FilePlanComponent createComponent(String parentComponentId, String componentName, FilePlanComponentType componentType) throws Exception
+    {
+        RestWrapper restWrapper = filePlanComponentApi.usingRestWrapper().authenticateUser(dataUser.getAdminUser());
+
+        JsonObject componentProperties = buildObject().
+            add(NAME, componentName).
+            add(NODE_TYPE, componentType.toString()).
+            addObject(PROPERTIES).
+                add(PROPERTIES_TITLE, "Title for " + componentName).
+                end().
+            getJson();
+
+        FilePlanComponent fpc = filePlanComponentApi.createFilePlanComponent(componentProperties, parentComponentId);
+        restWrapper.assertStatusCodeIs(CREATED);
+        return fpc;
+    }
 }