ACS-4966 Correct path in unit tests, cover child category case

This commit is contained in:
MichalKinas
2023-04-13 09:19:05 +02:00
parent 0612d74c68
commit 3dec621b15
3 changed files with 93 additions and 31 deletions

View File

@@ -53,8 +53,7 @@ public class CategoriesPathTests extends CategoriesRestTest
@BeforeClass(alwaysRun = true) @BeforeClass(alwaysRun = true)
public void dataPreparation() throws Exception public void dataPreparation() throws Exception
{ {
STEP("Create user and site"); STEP("Create site");
user = dataUser.createRandomTestUser();
SiteModel site = dataSite.usingUser(user).createPublicRandomSite(); SiteModel site = dataSite.usingUser(user).createPublicRandomSite();
STEP("Create a folder, file in it and a category"); STEP("Create a folder, file in it and a category");
@@ -112,6 +111,27 @@ public class CategoriesPathTests extends CategoriesRestTest
.allMatch(cat -> cat.getPath().equals("/categories/General"))); .allMatch(cat -> cat.getPath().equals("/categories/General")));
} }
/**
* Verify path for child category.
*/
@Test(groups = { TestGroup.REST_API })
public void testGetChildCategory_includePath()
{
STEP("Create parent and child categories and verify if child path contains parent name");
final RestCategoryModel parentCategory = createCategoryModelWithId(ROOT_CATEGORY_ID);
final RestCategoryModel childCategory = prepareCategoriesUnder(parentCategory, 2);
final RestCategoryModelsCollection actualCategories = restClient.authenticateUser(user)
.withCoreAPI()
.usingCategory(parentCategory)
.include(INCLUDE_PATH_PARAM)
.getCategoryChildren();
restClient.assertStatusCodeIs(OK);
assertTrue(actualCategories.getEntries().stream()
.map(RestCategoryModel::onModel)
.allMatch(cat -> cat.getPath().equals("/categories/General/" + parentCategory.getName())));
}
/** /**
* Create category and verify that it has a path. * Create category and verify that it has a path.
*/ */

View File

@@ -69,7 +69,6 @@ import org.apache.commons.lang3.StringUtils;
public class CategoriesImpl implements Categories public class CategoriesImpl implements Categories
{ {
static final String INCLUDE_COUNT_PARAM = "count"; static final String INCLUDE_COUNT_PARAM = "count";
static final String INCLUDE_PATH_PARAM = "path";
static final String NOT_A_VALID_CATEGORY = "Node id does not refer to a valid category"; static final String NOT_A_VALID_CATEGORY = "Node id does not refer to a valid category";
static final String NO_PERMISSION_TO_MANAGE_A_CATEGORY = "Current user does not have permission to manage a category"; static final String NO_PERMISSION_TO_MANAGE_A_CATEGORY = "Current user does not have permission to manage a category";
static final String NO_PERMISSION_TO_READ_CONTENT = "Current user does not have read permission to content"; static final String NO_PERMISSION_TO_READ_CONTENT = "Current user does not have read permission to content";
@@ -112,9 +111,9 @@ public class CategoriesImpl implements Categories
category.setCount(categoriesCount.getOrDefault(category.getId(), 0)); category.setCount(categoriesCount.getOrDefault(category.getId(), 0));
} }
if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) if (parameters.getInclude().contains(Nodes.PARAM_INCLUDE_PATH))
{ {
category.setPath(nodeService.getPath(nodeRef).toDisplayPath(nodeService, permissionService)); category.setPath(getCategoryPath(category));
} }
return category; return category;
@@ -134,9 +133,9 @@ public class CategoriesImpl implements Categories
{ {
category.setCount(0); category.setCount(0);
} }
if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) if (parameters.getInclude().contains(Nodes.PARAM_INCLUDE_PATH))
{ {
category.setPath(nodeService.getPath(nodes.getNode(category.getId()).getNodeRef()).toDisplayPath(nodeService, permissionService)); category.setPath(getCategoryPath(category));
} }
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
@@ -152,9 +151,9 @@ public class CategoriesImpl implements Categories
.map(ChildAssociationRef::getChildRef) .map(ChildAssociationRef::getChildRef)
.map(this::mapToCategory) .map(this::mapToCategory)
.peek(category -> { .peek(category -> {
if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) if (parameters.getInclude().contains(Nodes.PARAM_INCLUDE_PATH))
{ {
category.setPath(nodeService.getPath(nodes.getNode(category.getId()).getNodeRef()).toDisplayPath(nodeService, permissionService)); category.setPath(getCategoryPath(category));
} }
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
@@ -187,9 +186,9 @@ public class CategoriesImpl implements Categories
category.setCount(categoriesCount.getOrDefault(category.getId(), 0)); category.setCount(categoriesCount.getOrDefault(category.getId(), 0));
} }
if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) if (parameters.getInclude().contains(Nodes.PARAM_INCLUDE_PATH))
{ {
category.setPath(nodeService.getPath(categoryNodeRef).toDisplayPath(nodeService, permissionService)); category.setPath(getCategoryPath(category));
} }
return category; return category;
@@ -226,9 +225,9 @@ public class CategoriesImpl implements Categories
.stream() .stream()
.map(this::mapToCategory) .map(this::mapToCategory)
.peek(category -> { .peek(category -> {
if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) if (parameters.getInclude().contains(Nodes.PARAM_INCLUDE_PATH))
{ {
category.setPath(nodeService.getPath(nodes.getNode(category.getId()).getNodeRef()).toDisplayPath(nodeService, permissionService)); category.setPath(getCategoryPath(category));
} }
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
@@ -265,9 +264,9 @@ public class CategoriesImpl implements Categories
.stream() .stream()
.map(this::mapToCategory) .map(this::mapToCategory)
.peek(category -> { .peek(category -> {
if (parameters.getInclude().contains(INCLUDE_PATH_PARAM)) if (parameters.getInclude().contains(Nodes.PARAM_INCLUDE_PATH))
{ {
category.setPath(nodeService.getPath(nodes.getNode(category.getId()).getNodeRef()).toDisplayPath(nodeService, permissionService)); category.setPath(getCategoryPath(category));
} }
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
@@ -515,4 +514,16 @@ public class CategoriesImpl implements Categories
.stream() .stream()
.collect(Collectors.toMap(pair -> pair.getFirst().toString().replace(idPrefix, StringUtils.EMPTY), Pair::getSecond)); .collect(Collectors.toMap(pair -> pair.getFirst().toString().replace(idPrefix, StringUtils.EMPTY), Pair::getSecond));
} }
/**
* Get path for a given category in human-readable form.
*
* @param category Category to provide path for.
* @return Path for a category in human-readable form.
*/
private String getCategoryPath(final Category category)
{
final NodeRef categoryNodeRef = nodes.getNode(category.getId()).getNodeRef();
return nodeService.getPath(categoryNodeRef).toDisplayPath(nodeService, permissionService);
}
} }

View File

@@ -28,7 +28,6 @@ package org.alfresco.rest.api.impl;
import static org.alfresco.rest.api.Nodes.PATH_ROOT; import static org.alfresco.rest.api.Nodes.PATH_ROOT;
import static org.alfresco.rest.api.impl.CategoriesImpl.INCLUDE_COUNT_PARAM; import static org.alfresco.rest.api.impl.CategoriesImpl.INCLUDE_COUNT_PARAM;
import static org.alfresco.rest.api.impl.CategoriesImpl.INCLUDE_PATH_PARAM;
import static org.alfresco.rest.api.impl.CategoriesImpl.INVALID_NODE_TYPE; import static org.alfresco.rest.api.impl.CategoriesImpl.INVALID_NODE_TYPE;
import static org.alfresco.rest.api.impl.CategoriesImpl.NOT_A_VALID_CATEGORY; import static org.alfresco.rest.api.impl.CategoriesImpl.NOT_A_VALID_CATEGORY;
import static org.alfresco.rest.api.impl.CategoriesImpl.NOT_NULL_OR_EMPTY; import static org.alfresco.rest.api.impl.CategoriesImpl.NOT_NULL_OR_EMPTY;
@@ -61,6 +60,7 @@ import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.transfer.PathHelper;
import org.alfresco.rest.api.Nodes; import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Category; import org.alfresco.rest.api.model.Category;
import org.alfresco.rest.api.model.Node; import org.alfresco.rest.api.model.Node;
@@ -260,11 +260,16 @@ public class CategoriesImplTest
final QName categoryQName = createCmQNameOf(CATEGORY_NAME); final QName categoryQName = createCmQNameOf(CATEGORY_NAME);
final NodeRef parentCategoryNodeRef = createNodeRefWithId(PARENT_ID); final NodeRef parentCategoryNodeRef = createNodeRefWithId(PARENT_ID);
final ChildAssociationRef parentAssociation = createAssociationOf(parentCategoryNodeRef, CATEGORY_NODE_REF, categoryQName); final ChildAssociationRef parentAssociation = createAssociationOf(parentCategoryNodeRef, CATEGORY_NODE_REF, categoryQName);
Path mockPath = new Path(); final Path mockPath = new Path();
final String mockRootLevel = "/{mockRootLevel}";
final String mockChildLevel = "/{mockChild}";
mockPath.append(PathHelper.stringToPath(mockRootLevel));
mockPath.append(PathHelper.stringToPath(mockChildLevel));
mockPath.append(PathHelper.stringToPath("/"));
given(nodesMock.getNode(any())).willReturn(createNode()); given(nodesMock.getNode(any())).willReturn(createNode());
given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation); given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation);
given(parametersMock.getInclude()).willReturn(List.of(INCLUDE_PATH_PARAM)); given(parametersMock.getInclude()).willReturn(List.of(Nodes.PARAM_INCLUDE_PATH));
given(nodeServiceMock.getPath(CATEGORY_NODE_REF)).willReturn(mockPath); given(nodeServiceMock.getPath(any())).willReturn(mockPath);
// when // when
final Category actualCategory = objectUnderTest.getCategoryById(CATEGORY_ID, parametersMock); final Category actualCategory = objectUnderTest.getCategoryById(CATEGORY_ID, parametersMock);
@@ -273,7 +278,7 @@ public class CategoriesImplTest
.isNotNull() .isNotNull()
.extracting(Category::getPath) .extracting(Category::getPath)
.isNotNull() .isNotNull()
.isEqualTo(""); .isEqualTo("//" + mockRootLevel + "//" + mockChildLevel);
} }
@Test @Test
@@ -511,11 +516,16 @@ public class CategoriesImplTest
final NodeRef parentCategoryNodeRef = createNodeRefWithId(PARENT_ID); final NodeRef parentCategoryNodeRef = createNodeRefWithId(PARENT_ID);
final ChildAssociationRef parentAssociation = createAssociationOf(parentCategoryNodeRef, CATEGORY_NODE_REF, categoryQName); final ChildAssociationRef parentAssociation = createAssociationOf(parentCategoryNodeRef, CATEGORY_NODE_REF, categoryQName);
final Path mockPath = new Path(); final Path mockPath = new Path();
final String mockRootLevel = "/{mockRootLevel}";
final String mockChildLevel = "/{mockChild}";
mockPath.append(PathHelper.stringToPath(mockRootLevel));
mockPath.append(PathHelper.stringToPath(mockChildLevel));
mockPath.append(PathHelper.stringToPath("/"));
given(nodesMock.validateNode(PARENT_ID)).willReturn(parentCategoryNodeRef); given(nodesMock.validateNode(PARENT_ID)).willReturn(parentCategoryNodeRef);
given(categoryServiceMock.createCategory(parentCategoryNodeRef, CATEGORY_NAME)).willReturn(categoryNodeRef); given(categoryServiceMock.createCategory(parentCategoryNodeRef, CATEGORY_NAME)).willReturn(categoryNodeRef);
given(nodesMock.getNode(any())).willReturn(createNode()); given(nodesMock.getNode(any())).willReturn(createNode());
given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation); given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation);
given(parametersMock.getInclude()).willReturn(List.of(INCLUDE_PATH_PARAM)); given(parametersMock.getInclude()).willReturn(List.of(Nodes.PARAM_INCLUDE_PATH));
given(nodeServiceMock.getPath(any())).willReturn(mockPath); given(nodeServiceMock.getPath(any())).willReturn(mockPath);
final List<Category> categoryModels = new ArrayList<>(prepareCategories()); final List<Category> categoryModels = new ArrayList<>(prepareCategories());
@@ -531,7 +541,7 @@ public class CategoriesImplTest
.element(0) .element(0)
.extracting(Category::getPath) .extracting(Category::getPath)
.isNotNull() .isNotNull()
.isEqualTo(""); .isEqualTo("//" + mockRootLevel + "//" + mockChildLevel);
} }
@Test @Test
@@ -692,9 +702,15 @@ public class CategoriesImplTest
final int childrenCount = 3; final int childrenCount = 3;
final List<ChildAssociationRef> childAssociationRefMocks = prepareChildAssocMocks(childrenCount, parentCategoryNodeRef); final List<ChildAssociationRef> childAssociationRefMocks = prepareChildAssocMocks(childrenCount, parentCategoryNodeRef);
final Path mockPath = new Path(); final Path mockPath = new Path();
final String mockRootLevel = "/{mockRootLevel}";
final String mockChildLevel = "/{mockChild}";
mockPath.append(PathHelper.stringToPath(mockRootLevel));
mockPath.append(PathHelper.stringToPath(mockChildLevel));
mockPath.append(PathHelper.stringToPath("/"));
final String resultPath = "//" + mockRootLevel + "//" + mockChildLevel;
given(nodeServiceMock.getChildAssocs(parentCategoryNodeRef)).willReturn(childAssociationRefMocks); given(nodeServiceMock.getChildAssocs(parentCategoryNodeRef)).willReturn(childAssociationRefMocks);
childAssociationRefMocks.forEach(this::prepareCategoryNodeMocks); childAssociationRefMocks.forEach(this::prepareCategoryNodeMocks);
given(parametersMock.getInclude()).willReturn(List.of(INCLUDE_PATH_PARAM)); given(parametersMock.getInclude()).willReturn(List.of(Nodes.PARAM_INCLUDE_PATH));
given(nodeServiceMock.getPath(any())).willReturn(mockPath); given(nodeServiceMock.getPath(any())).willReturn(mockPath);
// when // when
@@ -705,7 +721,7 @@ public class CategoriesImplTest
.hasSize(3) .hasSize(3)
.extracting(Category::getPath) .extracting(Category::getPath)
.isNotNull() .isNotNull()
.isEqualTo(List.of("", "", "")); .isEqualTo(List.of(resultPath, resultPath, resultPath));
} }
@Test @Test
@@ -841,11 +857,16 @@ public class CategoriesImplTest
final NodeRef parentCategoryNodeRef = createNodeRefWithId(PARENT_ID); final NodeRef parentCategoryNodeRef = createNodeRefWithId(PARENT_ID);
final ChildAssociationRef parentAssociation = createAssociationOf(parentCategoryNodeRef, CATEGORY_NODE_REF, categoryQName); final ChildAssociationRef parentAssociation = createAssociationOf(parentCategoryNodeRef, CATEGORY_NODE_REF, categoryQName);
final Path mockPath = new Path(); final Path mockPath = new Path();
final String mockRootLevel = "/{mockRootLevel}";
final String mockChildLevel = "/{mockChild}";
mockPath.append(PathHelper.stringToPath(mockRootLevel));
mockPath.append(PathHelper.stringToPath(mockChildLevel));
mockPath.append(PathHelper.stringToPath("/"));
given(nodesMock.getNode(any())).willReturn(createNode(categoryNewName)); given(nodesMock.getNode(any())).willReturn(createNode(categoryNewName));
given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation); given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation);
given(nodeServiceMock.moveNode(any(), any(), any(), any())).willReturn(createAssociationOf(parentCategoryNodeRef, CATEGORY_NODE_REF, createCmQNameOf(categoryNewName))); given(nodeServiceMock.moveNode(any(), any(), any(), any())).willReturn(createAssociationOf(parentCategoryNodeRef, CATEGORY_NODE_REF, createCmQNameOf(categoryNewName)));
given(parametersMock.getInclude()).willReturn(List.of(INCLUDE_PATH_PARAM)); given(parametersMock.getInclude()).willReturn(List.of(Nodes.PARAM_INCLUDE_PATH));
given(nodeServiceMock.getPath(CATEGORY_NODE_REF)).willReturn(mockPath); given(nodeServiceMock.getPath(any())).willReturn(mockPath);
// when // when
final Category actualCategory = objectUnderTest.updateCategoryById(CATEGORY_ID, fixedCategory, parametersMock); final Category actualCategory = objectUnderTest.updateCategoryById(CATEGORY_ID, fixedCategory, parametersMock);
@@ -854,7 +875,7 @@ public class CategoriesImplTest
.isNotNull() .isNotNull()
.extracting(Category::getPath) .extracting(Category::getPath)
.isNotNull() .isNotNull()
.isEqualTo(""); .isEqualTo("//" + mockRootLevel + "//" + mockChildLevel);
} }
@Test @Test
@@ -1097,10 +1118,15 @@ public class CategoriesImplTest
final NodeRef categoryParentNodeRef = createNodeRefWithId(PARENT_ID); final NodeRef categoryParentNodeRef = createNodeRefWithId(PARENT_ID);
final ChildAssociationRef parentAssociation = createAssociationOf(categoryParentNodeRef, CATEGORY_NODE_REF); final ChildAssociationRef parentAssociation = createAssociationOf(categoryParentNodeRef, CATEGORY_NODE_REF);
final Path mockPath = new Path(); final Path mockPath = new Path();
final String mockRootLevel = "/{mockRootLevel}";
final String mockChildLevel = "/{mockChild}";
mockPath.append(PathHelper.stringToPath(mockRootLevel));
mockPath.append(PathHelper.stringToPath(mockChildLevel));
mockPath.append(PathHelper.stringToPath("/"));
given(nodesMock.getNode(any())).willReturn(createNode()); given(nodesMock.getNode(any())).willReturn(createNode());
given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation); given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation);
given(nodeServiceMock.hasAspect(any(), any())).willReturn(true); given(nodeServiceMock.hasAspect(any(), any())).willReturn(true);
given(parametersMock.getInclude()).willReturn(List.of(INCLUDE_PATH_PARAM)); given(parametersMock.getInclude()).willReturn(List.of(Nodes.PARAM_INCLUDE_PATH));
given(nodeServiceMock.getPath(any())).willReturn(mockPath); given(nodeServiceMock.getPath(any())).willReturn(mockPath);
// when // when
@@ -1116,7 +1142,7 @@ public class CategoriesImplTest
then(nodeServiceMock).should().setProperty(CONTENT_NODE_REF, ContentModel.PROP_CATEGORIES, expectedCategories); then(nodeServiceMock).should().setProperty(CONTENT_NODE_REF, ContentModel.PROP_CATEGORIES, expectedCategories);
then(nodeServiceMock).should().getParentAssocs(categoryParentNodeRef); then(nodeServiceMock).should().getParentAssocs(categoryParentNodeRef);
final List<Category> expectedLinkedCategories = List.of(CATEGORY); final List<Category> expectedLinkedCategories = List.of(CATEGORY);
expectedLinkedCategories.get(0).setPath(""); expectedLinkedCategories.get(0).setPath("//" + mockRootLevel + "//" + mockChildLevel);
assertThat(actualLinkedCategories) assertThat(actualLinkedCategories)
.isNotNull().usingRecursiveComparison() .isNotNull().usingRecursiveComparison()
.isEqualTo(expectedLinkedCategories); .isEqualTo(expectedLinkedCategories);
@@ -1318,10 +1344,15 @@ public class CategoriesImplTest
final NodeRef categoryParentNodeRef = createNodeRefWithId(PARENT_ID); final NodeRef categoryParentNodeRef = createNodeRefWithId(PARENT_ID);
final ChildAssociationRef parentAssociation = createAssociationOf(categoryParentNodeRef, CATEGORY_NODE_REF); final ChildAssociationRef parentAssociation = createAssociationOf(categoryParentNodeRef, CATEGORY_NODE_REF);
final Path mockPath = new Path(); final Path mockPath = new Path();
final String mockRootLevel = "/{mockRootLevel}";
final String mockChildLevel = "/{mockChild}";
mockPath.append(PathHelper.stringToPath(mockRootLevel));
mockPath.append(PathHelper.stringToPath(mockChildLevel));
mockPath.append(PathHelper.stringToPath("/"));
given(nodeServiceMock.getProperty(any(), eq(ContentModel.PROP_CATEGORIES))).willReturn((Serializable) List.of(CATEGORY_NODE_REF)); given(nodeServiceMock.getProperty(any(), eq(ContentModel.PROP_CATEGORIES))).willReturn((Serializable) List.of(CATEGORY_NODE_REF));
given(nodesMock.getNode(any())).willReturn(createNode()); given(nodesMock.getNode(any())).willReturn(createNode());
given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation); given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation);
given(parametersMock.getInclude()).willReturn(List.of(INCLUDE_PATH_PARAM)); given(parametersMock.getInclude()).willReturn(List.of(Nodes.PARAM_INCLUDE_PATH));
given(nodeServiceMock.getPath(any())).willReturn(mockPath); given(nodeServiceMock.getPath(any())).willReturn(mockPath);
// when // when
@@ -1341,7 +1372,7 @@ public class CategoriesImplTest
then(nodeServiceMock).should().getPath(any()); then(nodeServiceMock).should().getPath(any());
then(nodeServiceMock).shouldHaveNoMoreInteractions(); then(nodeServiceMock).shouldHaveNoMoreInteractions();
final List<Category> expectedCategories = List.of(CATEGORY); final List<Category> expectedCategories = List.of(CATEGORY);
expectedCategories.get(0).setPath(""); expectedCategories.get(0).setPath("//" + mockRootLevel + "//" + mockChildLevel);
assertThat(actualCategories) assertThat(actualCategories)
.isNotNull().usingRecursiveComparison() .isNotNull().usingRecursiveComparison()
.isEqualTo(expectedCategories); .isEqualTo(expectedCategories);