Acs 4400 multiple same categories can be linked from content or folder (#1675)

ACS-4400: Multiple same categories can be linked from content or folder
This commit is contained in:
krdabrowski
2023-01-16 11:40:37 +01:00
committed by GitHub
parent 2b363fb315
commit c228bfac1f
4 changed files with 106 additions and 0 deletions

View File

@@ -333,6 +333,22 @@ public class ModelAssertion<T>
return (T) model; return (T) model;
} }
public T containsOnce(String value)
{
final String fieldContent = fieldValue.toString();
final int i = fieldContent.indexOf(value);
if (i == -1)
{
Assert.fail(errorMessage("does NOT contain at all the expected value: " + value + ", Current Value: " + fieldValue.toString()));
}
if (i != fieldContent.lastIndexOf(value))
{
Assert.fail(errorMessage("contains more than one expected value: " + value + ", Current Value: " + fieldValue.toString()));
}
return (T) model;
}
public T notContains(String value) public T notContains(String value)
{ {
if (fieldValue.toString().contains(value)) if (fieldValue.toString().contains(value))

View File

@@ -145,6 +145,75 @@ public class LinkToCategoriesTests extends CategoriesRestTest
fileNode.assertThat().field(PROPERTIES_FIELD).contains(secondCategory.getId()); fileNode.assertThat().field(PROPERTIES_FIELD).contains(secondCategory.getId());
} }
/**
* Try to link file to three categories, two of which are the same, and expect two distinct categories in output.
*/
@Test(groups = { TestGroup.REST_API})
public void testLinkContentToCategory_withRepeatedCategory()
{
STEP("Check if file is not linked to any category");
RestNodeModel fileNode = restClient.authenticateUser(user).withCoreAPI().usingNode(file).getNode();
fileNode.assertThat().field(ASPECTS_FIELD).notContains("cm:generalclassifiable");
fileNode.assertThat().field(PROPERTIES_FIELD).notContains("cm:categories");
STEP("Link content to three (one repeated) categories and expect 201");
final RestCategoryModel secondCategory = prepareCategoryUnderRoot();
final List<RestCategoryLinkBodyModel> categoryLinkModels = List.of(
createCategoryLinkModelWithId(category.getId()),
createCategoryLinkModelWithId(secondCategory.getId()),
createCategoryLinkModelWithId(category.getId())
);
final RestCategoryModelsCollection linkedCategories = restClient.authenticateUser(user).withCoreAPI().usingNode(file).linkToCategories(categoryLinkModels);
restClient.assertStatusCodeIs(CREATED);
linkedCategories.assertThat().entriesListCountIs(2);
linkedCategories.getEntries().get(0).onModel().assertThat().isEqualTo(category);
linkedCategories.getEntries().get(1).onModel().assertThat().isEqualTo(secondCategory);
STEP("Verify if repeated category was ignored and only two categories are present in file metadata");
fileNode = restClient.authenticateUser(user).withCoreAPI().usingNode(file).getNode();
fileNode.assertThat().field(PROPERTIES_FIELD).containsOnce(category.getId());
fileNode.assertThat().field(PROPERTIES_FIELD).containsOnce(secondCategory.getId());
}
/**
* Try to link file to already linked category and expect distinct categories in response.
*/
@Test(groups = { TestGroup.REST_API})
public void testLinkContentToCategory_usingAlreadyLinkedCategory()
{
STEP("Create second category under root");
final RestCategoryModel secondCategory = prepareCategoryUnderRoot();
STEP("Link file to one category");
final RestCategoryLinkBodyModel categoryLinkModel = createCategoryLinkModelWithId(category.getId());
restClient.authenticateUser(user).withCoreAPI().usingNode(file).linkToCategory(categoryLinkModel);
RestNodeModel fileNode = restClient.authenticateUser(user).withCoreAPI().usingNode(file).getNode();
fileNode.assertThat().field(PROPERTIES_FIELD).containsOnce(category.getId());
fileNode.assertThat().field(PROPERTIES_FIELD).notContains(secondCategory.getId());
STEP("Link content to two categories using one already linked before to and expect 201");
final List<RestCategoryLinkBodyModel> categoryLinkModels = List.of(
createCategoryLinkModelWithId(category.getId()),
createCategoryLinkModelWithId(secondCategory.getId())
);
final RestCategoryModelsCollection linkedCategories = restClient.authenticateUser(user).withCoreAPI().usingNode(file).linkToCategories(categoryLinkModels);
restClient.assertStatusCodeIs(CREATED);
linkedCategories.assertThat().entriesListCountIs(2);
linkedCategories.getEntries().get(0).onModel().assertThat().isEqualTo(category);
linkedCategories.getEntries().get(1).onModel().assertThat().isEqualTo(secondCategory);
STEP("Verify if repeated category was ignored and only two categories are present in file metadata");
fileNode = restClient.authenticateUser(user).withCoreAPI().usingNode(file).getNode();
fileNode.assertThat().field(PROPERTIES_FIELD).containsOnce(category.getId());
fileNode.assertThat().field(PROPERTIES_FIELD).containsOnce(secondCategory.getId());
}
/** /**
* Link content, which already has some linked category to new ones and verify if all categories are present in node's properties * Link content, which already has some linked category to new ones and verify if all categories are present in node's properties
*/ */

View File

@@ -193,6 +193,7 @@ public class CategoriesImpl implements Categories
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(Category::getId) .map(Category::getId)
.filter(StringUtils::isNotEmpty) .filter(StringUtils::isNotEmpty)
.distinct()
.map(this::getCategoryNodeRef) .map(this::getCategoryNodeRef)
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@@ -1000,6 +1000,26 @@ public class CategoriesImplTest
.hasMessageContaining(NOT_A_VALID_CATEGORY); .hasMessageContaining(NOT_A_VALID_CATEGORY);
} }
@Test
public void testLinkNodeToCategories_withTwoIdenticalCategories()
{
final List<Category> categoryLinks = List.of(CATEGORY, CATEGORY);
final NodeRef categoryParentNodeRef = createNodeRefWithId(PARENT_ID);
final ChildAssociationRef parentAssociation = createAssociationOf(categoryParentNodeRef, CATEGORY_NODE_REF);
given(nodesMock.getNode(any())).willReturn(prepareCategoryNode());
given(nodeServiceMock.getPrimaryParent(any())).willReturn(parentAssociation);
// when
final List<Category> actualLinkedCategories = objectUnderTest.linkNodeToCategories(CONTENT_NODE_ID, categoryLinks);
final Map<QName, Serializable> expectedProperties = Map.of(ContentModel.PROP_CATEGORIES, (Serializable) List.of(CATEGORY_NODE_REF));
then(nodeServiceMock).should().addAspect(CONTENT_NODE_REF, ContentModel.ASPECT_GEN_CLASSIFIABLE, expectedProperties);
final List<Category> expectedLinkedCategories = List.of(CATEGORY);
assertThat(actualLinkedCategories)
.isNotNull().usingRecursiveComparison()
.isEqualTo(expectedLinkedCategories);
}
@Test @Test
public void testListCategoriesForNode() public void testListCategoriesForNode()
{ {