diff --git a/packaging/tests/tas-restapi/src/main/java/org/alfresco/rest/requests/Categories.java b/packaging/tests/tas-restapi/src/main/java/org/alfresco/rest/requests/Categories.java index 460ae87a3f..e5a3843201 100644 --- a/packaging/tests/tas-restapi/src/main/java/org/alfresco/rest/requests/Categories.java +++ b/packaging/tests/tas-restapi/src/main/java/org/alfresco/rest/requests/Categories.java @@ -47,7 +47,7 @@ public class Categories extends ModelRequest } /** - * Retrieves a category with ID using GET call on using GET call on "/tags/{tagId}" + * Retrieves a category with ID using GET call on "/categories/{categoryId}" * * @return RestCategoryModel */ diff --git a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/categories/CreateCategoriesTests.java b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/categories/CreateCategoriesTests.java index edfc95d36f..74200018f0 100644 --- a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/categories/CreateCategoriesTests.java +++ b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/categories/CreateCategoriesTests.java @@ -44,6 +44,7 @@ import org.alfresco.utility.model.FolderModel; import org.alfresco.utility.model.SiteModel; import org.alfresco.utility.model.TestGroup; import org.alfresco.utility.model.UserModel; +import org.apache.commons.lang3.StringUtils; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -84,6 +85,24 @@ public class CreateCategoriesTests extends RestTest createdCategory.assertThat().field(FIELD_HAS_CHILDREN).is(false); } + /** + * Check we get 400 error when attempting to create a category with empty name + */ + @Test(groups = {TestGroup.REST_API}) + public void testCreateCategoryWithoutName_andFail() + { + STEP("Create a category under root category (as admin)"); + final RestCategoryModel rootCategory = new RestCategoryModel(); + rootCategory.setId("-root-"); + final RestCategoryModel aCategory = new RestCategoryModel(); + aCategory.setName(StringUtils.EMPTY); + restClient.authenticateUser(dataUser.getAdminUser()) + .withCoreAPI() + .usingCategory(rootCategory) + .createSingleCategory(aCategory); + restClient.assertStatusCodeIs(BAD_REQUEST).assertLastError().containsSummary("Category name must not be null or empty"); + } + /** * Check we can create several categories as children of a created category */ @@ -134,6 +153,54 @@ public class CreateCategoriesTests extends RestTest parentCategoryFromGet.assertThat().field(FIELD_HAS_CHILDREN).is(true); } + /** + * Check we can create over 100 categories as children of a created category and pagination information is proper. + */ + @Test(groups = {TestGroup.REST_API}) + public void testCreateOver100SubCategories() + { + STEP("Create a category under root category (as admin)"); + final RestCategoryModel rootCategory = new RestCategoryModel(); + rootCategory.setId("-root-"); + final RestCategoryModel aCategory = new RestCategoryModel(); + aCategory.setName(RandomData.getRandomName("Category")); + final RestCategoryModel createdCategory = restClient.authenticateUser(dataUser.getAdminUser()) + .withCoreAPI() + .usingCategory(rootCategory) + .createSingleCategory(aCategory); + restClient.assertStatusCodeIs(CREATED); + + createdCategory.assertThat().field(FIELD_NAME).is(aCategory.getName()) + .assertThat().field(FIELD_PARENT_ID).is(rootCategory.getId()) + .assertThat().field(FIELD_HAS_CHILDREN).is(false) + .assertThat().field(FIELD_ID).isNotEmpty(); + + STEP("Create more than a hundred categories under the previously created (as admin)"); + final int categoriesNumber = 120; + final List categoriesToCreate = getCategoriesToCreate(categoriesNumber); + final RestCategoryModelsCollection createdSubCategories = restClient.authenticateUser(dataUser.getAdminUser()) + .withCoreAPI() + .usingCategory(createdCategory) + .createCategoriesList(categoriesToCreate); + restClient.assertStatusCodeIs(CREATED); + + createdSubCategories.assertThat() + .entriesListCountIs(categoriesToCreate.size()); + IntStream.range(0, categoriesNumber) + .forEach(i -> createdSubCategories.getEntries().get(i).onModel() + .assertThat().field(FIELD_NAME).is(categoriesToCreate.get(i).getName()) + .assertThat().field(FIELD_PARENT_ID).is(createdCategory.getId()) + .assertThat().field(FIELD_HAS_CHILDREN).is(false) + .assertThat().field(FIELD_ID).isNotEmpty() + ); + createdSubCategories.getPagination().assertThat().field("count").is(categoriesNumber) + .assertThat().field("totalItems").is(categoriesNumber) + .assertThat().field("maxItems").is(categoriesNumber) + .assertThat().field("skipCount").is(0) + .assertThat().field("hasMoreItems").is(false); + + } + /** * Check we cannot create a category as direct child of root category as non-admin user */ diff --git a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/CreateRulesTests.java b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/CreateRulesTests.java index cf4888952b..8b195e3ebd 100644 --- a/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/CreateRulesTests.java +++ b/packaging/tests/tas-restapi/src/test/java/org/alfresco/rest/rules/CreateRulesTests.java @@ -56,6 +56,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import java.util.stream.IntStream; import javax.json.Json; import javax.json.JsonObject; @@ -63,6 +64,7 @@ import javax.json.JsonObject; import org.alfresco.rest.model.RestActionBodyExecTemplateModel; import org.alfresco.rest.model.RestActionConstraintModel; import org.alfresco.rest.model.RestCompositeConditionDefinitionModel; +import org.alfresco.rest.model.RestPaginationModel; import org.alfresco.rest.model.RestRuleModel; import org.alfresco.rest.model.RestRuleModelsCollection; import org.alfresco.utility.constants.UserRole; @@ -267,6 +269,38 @@ public class CreateRulesTests extends RulesRestTest .assertThat().field("name").is(ruleNames.get(i))); } + /** Check we can create over 100 rules and get them all back in response. */ + @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) + public void createOver100Rules() + { + STEP("Create a list of 120 rules in one POST request"); + final int ruleCount = 120; + final String ruleNamePrefix = "multiRule"; + final List ruleModels = IntStream.rangeClosed(1, ruleCount) + .mapToObj(i -> rulesUtils.createRuleModel(ruleNamePrefix + i)) + .collect(Collectors.toList()); + + final FolderModel aFolder = dataContent.usingUser(user).usingSite(site).createFolder(); + final RestRuleModelsCollection rules = restClient.authenticateUser(user).withPrivateAPI().usingNode(aFolder).usingDefaultRuleSet() + .createListOfRules(ruleModels); + + restClient.assertStatusCodeIs(CREATED); + + assertEquals("Unexpected number of rules received in response.", ruleCount, rules.getEntries().size()); + IntStream.range(0, ruleModels.size()).forEach(i -> + rules.getEntries().get(i).onModel() + .assertThat().field("id").isNotNull() + .assertThat().field("name").is(ruleNamePrefix + (i + 1))); + + rules.getPagination() + .assertThat().field("count").is(ruleCount) + .assertThat().field("totalItems").is(ruleCount) + .assertThat().field("maxItems").is(ruleCount) + .assertThat().field("skipCount").is(0) + .assertThat().field("hasMoreItems").is(false); + + } + /** Try to create several rules with an error in one of them. */ @Test (groups = { TestGroup.REST_API, TestGroup.RULES }) public void createRulesWithOneError() diff --git a/remote-api/src/main/java/org/alfresco/rest/api/impl/CategoriesImpl.java b/remote-api/src/main/java/org/alfresco/rest/api/impl/CategoriesImpl.java index 28f452ea31..8db2796b16 100644 --- a/remote-api/src/main/java/org/alfresco/rest/api/impl/CategoriesImpl.java +++ b/remote-api/src/main/java/org/alfresco/rest/api/impl/CategoriesImpl.java @@ -49,12 +49,14 @@ import org.alfresco.service.cmr.search.CategoryService; import org.alfresco.service.cmr.security.AuthorityService; import org.alfresco.service.namespace.RegexQNamePattern; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; @Experimental public class CategoriesImpl implements Categories { static final String NOT_A_VALID_CATEGORY = "Node id does not refer to a valid category"; static final String NO_PERMISSION_TO_CREATE_A_CATEGORY = "Current user does not have permission to create a category"; + private static final String NOT_NULL_OR_EMPTY = "Category name must not be null or empty"; private final AuthorityService authorityService; private final CategoryService categoryService; @@ -97,13 +99,21 @@ public class CategoriesImpl implements Categories throw new InvalidArgumentException(NOT_A_VALID_CATEGORY, new String[]{parentCategoryId}); } final List categoryNodeRefs = categories.stream() - .map(c -> categoryService.createCategory(parentNodeRef, c.getName())) + .map(c -> createCategoryNodeRef(parentNodeRef, c)) .collect(Collectors.toList()); return categoryNodeRefs.stream() .map(this::mapToCategory) .collect(Collectors.toList()); } + private NodeRef createCategoryNodeRef(NodeRef parentNodeRef, Category c) + { + if (StringUtils.isEmpty(c.getName())) { + throw new InvalidArgumentException(NOT_NULL_OR_EMPTY); + } + return categoryService.createCategory(parentNodeRef, c.getName()); + } + private boolean isNotACategory(NodeRef nodeRef) { return !nodes.isSubClass(nodeRef, ContentModel.TYPE_CATEGORY, false); diff --git a/remote-api/src/main/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java b/remote-api/src/main/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java index 5fcd23b332..1d3500a596 100644 --- a/remote-api/src/main/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java +++ b/remote-api/src/main/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java @@ -44,6 +44,7 @@ import org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResource import org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction; import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; +import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.rest.framework.resource.parameters.Params; import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams; import org.alfresco.rest.framework.tools.RecognizedParamsExtractor; @@ -377,7 +378,8 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements { if (created !=null && created.size() > 1) { - return CollectionWithPagingInfo.asPagedCollection(created.toArray()); + final Paging pagingAll = Paging.valueOf(0, created.size()); + return CollectionWithPagingInfo.asPaged(pagingAll, created); } else {