ACS-4032 create category (multiple categories pagination fix) (#1610)

* ACS-4032: Some fixes and refactors after verification. Fixing pagination info on POST multiple entities response.

* ACS-4032: Some fixes and refactors after verification. Fixing pagination info on POST multiple entities response.

* Fixing a test STEP description

Co-authored-by: Tom Page <tpage-alfresco@users.noreply.github.com>

Co-authored-by: Tom Page <tpage-alfresco@users.noreply.github.com>
This commit is contained in:
Maciej Pichura
2022-12-08 17:40:56 +01:00
committed by GitHub
parent f96b3012c4
commit f511436823
5 changed files with 116 additions and 3 deletions

View File

@@ -47,7 +47,7 @@ public class Categories extends ModelRequest<Categories>
}
/**
* 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
*/

View File

@@ -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<RestCategoryModel> 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
*/

View File

@@ -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<RestRuleModel> 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()

View File

@@ -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<NodeRef> 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);

View File

@@ -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
{