From 6c956f3d80eab6ea14c16654d963cccef17e6bba Mon Sep 17 00:00:00 2001 From: freddyDOTCMS Date: Wed, 26 Jun 2024 12:42:44 -0600 Subject: [PATCH 1/5] #28896 Create Factory method to find categories in all levels --- .../categories/business/CategoryFactory.java | 10 ++ .../business/CategoryFactoryImpl.java | 46 +++++++-- .../business/CategoryFactoryTest.java | 97 +++++++++++++++++++ 3 files changed, 144 insertions(+), 9 deletions(-) diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java index 1018149472a7..41d8d4ed498b 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java @@ -4,6 +4,7 @@ import com.dotmarketing.exception.DotDataException; import com.dotmarketing.portlets.categories.model.Category; +import com.liferay.portal.model.User; /** * @@ -273,5 +274,14 @@ public abstract class CategoryFactory { * @throws DotDataException */ abstract protected List getAllChildren(Categorizable parent) throws DotDataException; + + /** + * Return a list of Categories regardless of their levels. + * + * @param filter Value used to filter the Category by, returning only Categories that contain this value in their key, name, or variable name + * + * @return List of Category filtered + */ + public abstract List findAll(final String filter) throws DotDataException; } diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java index 537ed5d8982e..5623fa47e807 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java @@ -165,15 +165,7 @@ protected List findAll() throws DotDataException { .loadObjectResults(); List categories = convertForCategories(result); - for(final Category category : categories) { - //Updating the cache since we are already loading all the categories - if(catCache.get(category.getInode()) == null) - try { - catCache.put(category); - } catch (DotCacheException e) { - throw new DotDataException(e.getMessage(), e); - } - } + updateCache(categories); return categories; } @@ -843,4 +835,40 @@ protected String suggestVelocityVarName(final String categoryVelVarName) throws throw new DotDataException("Unable to suggest a variable name. Got to:" + var); } + /** + * Default Implementation for {@link CategoryFactory#findAll(String)} + * @param filter Value used to filter the Category by, returning only Categories that contain this value in their key, name, or variable name + * + * @return + */ + public List findAll(final String filter) throws DotDataException { + + final DotConnect dc = new DotConnect() + .setSQL("SELECT * FROM category " + + "WHERE LOWER(category.category_name) LIKE ? OR " + + "LOWER(category.category_key) LIKE ? OR " + + "LOWER(category.category_velocity_var_name) LIKE ?"); + + if ( UtilMethods.isSet(filter) ) { + dc.addObject("%" + filter.toLowerCase() + "%"); + dc.addObject("%" + filter.toLowerCase() + "%"); + dc.addObject("%" + filter.toLowerCase() + "%"); + } + + List categories = convertForCategories(dc.loadObjectResults()); + updateCache(categories); + return categories; + } + + private void updateCache(List categories) throws DotDataException { + for(final Category category : categories) { + if(catCache.get(category.getInode()) == null) + try { + catCache.put(category); + } catch (DotCacheException e) { + throw new DotDataException(e.getMessage(), e); + } + } + } + } diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java index 0a9f6b924ea3..f80b3f32e963 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java @@ -1,5 +1,6 @@ package com.dotmarketing.portlets.categories.business; +import static com.dotcms.util.CollectionsUtils.list; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -7,11 +8,18 @@ import static org.junit.Assert.assertTrue; import com.dotcms.IntegrationTestBase; +import com.dotcms.datagen.CategoryDataGen; +import com.dotcms.util.IntegrationTestInitService; +import com.dotmarketing.business.APILocator; import com.dotmarketing.business.FactoryLocator; import com.dotmarketing.exception.DotDataException; import com.dotmarketing.exception.DotSecurityException; import com.dotmarketing.portlets.categories.model.Category; import java.util.List; +import java.util.stream.Collectors; + +import com.liferay.util.StringUtil; +import net.bytebuddy.utility.RandomString; import org.jetbrains.annotations.NotNull; import org.junit.BeforeClass; import org.junit.Test; @@ -25,6 +33,7 @@ public class CategoryFactoryTest extends IntegrationTestBase { @BeforeClass public static void prepare() throws Exception { + IntegrationTestInitService.getInstance().init(); categoryFactory = FactoryLocator.getCategoryFactory(); } @@ -276,4 +285,92 @@ public void Test_Has_Dependencies() throws DotDataException { assertTrue(categoryFactory.hasDependencies(root)); } + /** + * Method to test: {@link CategoryFactoryImpl#findAll(String)} + * When: Create several Categories some of them with the word 'INCLUDE' on its name. key or variable name + * and call the method with filter equals to 'INCLUDE' + * Should: Return all the Category with the word include on any place + */ + @Test + public void getAllCategoriesFiltered() throws DotDataException { + + final String stringToFilterBy = new RandomString().nextString(); + + final Category topLevelCategory_1 = new CategoryDataGen().setCategoryName("Top Level Category " + stringToFilterBy) + .setKey("top_level_categoria") + .setCategoryVelocityVarName("top_level_categoria") + .nextPersisted(); + + final Category childCategory_1 = new CategoryDataGen().setCategoryName("Child Category 1") + .setKey("child_category_1 " + stringToFilterBy) + .setCategoryVelocityVarName("child_category_1") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category childCategory_2 = new CategoryDataGen().setCategoryName("Child Category 2") + .setKey("child_category_2") + .setCategoryVelocityVarName("child_category_2 " + stringToFilterBy) + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category topLevelCategory_2 = new CategoryDataGen().setCategoryName("Top Level Category " + stringToFilterBy) + .setKey("top_level_categoria") + .setCategoryVelocityVarName("top_level_categoria") + .nextPersisted(); + + final Category childCategory_3 = new CategoryDataGen().setCategoryName("Child Category 3 " + stringToFilterBy) + .setKey("child_category_3") + .setCategoryVelocityVarName("child_category_3") + .parent(topLevelCategory_2) + .nextPersisted(); + + final Category childCategory_4 = new CategoryDataGen().setCategoryName("Child Category 4") + .setKey("child_category_4") + .setCategoryVelocityVarName("child_category_4") + .parent(topLevelCategory_2) + .nextPersisted(); + + final Category topLevelCategory_3 = new CategoryDataGen().setCategoryName("Top Level Category") + .setKey("top_level_categoria") + .setCategoryVelocityVarName("top_level_categoria " + stringToFilterBy) + .nextPersisted(); + + final Category childCategory_5 = new CategoryDataGen().setCategoryName("Child Category 5") + .setKey("child_category_5") + .setCategoryVelocityVarName("child_category_5") + .parent(topLevelCategory_3) + .nextPersisted(); + + final Category topLevelCategory_4 = new CategoryDataGen().setCategoryName("Top Level Category") + .setKey("top_level_categoria") + .setCategoryVelocityVarName("top_level_categoria") + .parent(topLevelCategory_3) + .nextPersisted(); + + final Category childCategory_6 = new CategoryDataGen().setCategoryName("Child Category 6") + .setKey("child_category_6") + .setCategoryVelocityVarName("child_category_6") + .parent(topLevelCategory_3) + .nextPersisted(); + + final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 6 " + stringToFilterBy) + .setKey("grand_child_category_6") + .setCategoryVelocityVarName("grand_child_category_6") + .parent(childCategory_6) + .nextPersisted(); + + List categoriesExpected = list(topLevelCategory_1, childCategory_1, childCategory_2, childCategory_3, + topLevelCategory_3, grandchildCategory_1).stream().map(Category::getInode).collect(Collectors.toList()); + + final List categories_1 = FactoryLocator.getCategoryFactory().findAll(stringToFilterBy.toUpperCase()) + .stream().map(Category::getInode).collect(Collectors.toList()); + assertEquals(categoriesExpected.size(), categories_1.size()); + assertTrue(categories_1.containsAll(categoriesExpected)); + + final List categories_2 = FactoryLocator.getCategoryFactory().findAll(stringToFilterBy.toLowerCase()) + .stream().map(Category::getInode).collect(Collectors.toList()); + assertEquals(categoriesExpected.size(), categories_2.size()); + assertTrue(categories_2.containsAll(categoriesExpected)); + } + } From ecd3c8715e99a673705ef6b5e75429888ffc0fd6 Mon Sep 17 00:00:00 2001 From: freddyDOTCMS Date: Wed, 26 Jun 2024 14:51:12 -0600 Subject: [PATCH 2/5] Fixing test --- .../portlets/categories/business/CategoryFactoryTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java index f80b3f32e963..8705cfb2ccad 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java @@ -359,8 +359,9 @@ public void getAllCategoriesFiltered() throws DotDataException { .parent(childCategory_6) .nextPersisted(); - List categoriesExpected = list(topLevelCategory_1, childCategory_1, childCategory_2, childCategory_3, - topLevelCategory_3, grandchildCategory_1).stream().map(Category::getInode).collect(Collectors.toList()); + List categoriesExpected = list(topLevelCategory_1, topLevelCategory_2, childCategory_1, childCategory_2, + childCategory_3, topLevelCategory_3, grandchildCategory_1).stream() + .map(Category::getInode).collect(Collectors.toList()); final List categories_1 = FactoryLocator.getCategoryFactory().findAll(stringToFilterBy.toUpperCase()) .stream().map(Category::getInode).collect(Collectors.toList()); From 3ceb562f350148eaaee58f07420427dc57ab3686 Mon Sep 17 00:00:00 2001 From: freddyDOTCMS Date: Wed, 26 Jun 2024 15:57:03 -0600 Subject: [PATCH 3/5] #28896 Feedback --- .../categories/business/CategoryFactoryImpl.java | 15 +++++++++------ .../categories/business/CategoryFactoryTest.java | 10 ++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java index 5623fa47e807..b64c6fac8870 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java @@ -839,21 +839,24 @@ protected String suggestVelocityVarName(final String categoryVelVarName) throws * Default Implementation for {@link CategoryFactory#findAll(String)} * @param filter Value used to filter the Category by, returning only Categories that contain this value in their key, name, or variable name * - * @return + * @return A Collection of {@link Category} filtered by 'filter', if filter is null then it return the that + * {@link CategoryFactoryImpl#findAll()} */ public List findAll(final String filter) throws DotDataException { + if ( !UtilMethods.isSet(filter) ) { + return findAll(); + } + final DotConnect dc = new DotConnect() .setSQL("SELECT * FROM category " + "WHERE LOWER(category.category_name) LIKE ? OR " + "LOWER(category.category_key) LIKE ? OR " + "LOWER(category.category_velocity_var_name) LIKE ?"); - if ( UtilMethods.isSet(filter) ) { - dc.addObject("%" + filter.toLowerCase() + "%"); - dc.addObject("%" + filter.toLowerCase() + "%"); - dc.addObject("%" + filter.toLowerCase() + "%"); - } + dc.addObject("%" + filter.toLowerCase() + "%"); + dc.addObject("%" + filter.toLowerCase() + "%"); + dc.addObject("%" + filter.toLowerCase() + "%"); List categories = convertForCategories(dc.loadObjectResults()); updateCache(categories); diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java index 8705cfb2ccad..4abad3532ee5 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java @@ -16,6 +16,7 @@ import com.dotmarketing.exception.DotSecurityException; import com.dotmarketing.portlets.categories.model.Category; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import com.liferay.util.StringUtil; @@ -374,4 +375,13 @@ public void getAllCategoriesFiltered() throws DotDataException { assertTrue(categories_2.containsAll(categoriesExpected)); } + @Test + public void getAllCategoriesWithNullFilter() throws DotDataException { + new CategoryDataGen().nextPersisted(); + + final List categoriesWithFilter = FactoryLocator.getCategoryFactory().findAll(null); + final List categoriesWithoutFilter = FactoryLocator.getCategoryFactory().findAll(); + assertTrue(Objects.deepEquals(categoriesWithoutFilter, categoriesWithFilter)); + } + } From af69b7c222590978ae939d6eb832db6ca2b243b8 Mon Sep 17 00:00:00 2001 From: freddyDOTCMS Date: Mon, 1 Jul 2024 13:56:12 -0600 Subject: [PATCH 4/5] Looking Category starting with a specific parent Category --- .../categories/business/CategoryFactory.java | 66 ++++- .../business/CategoryFactoryImpl.java | 68 +++-- .../business/CategoryFactoryTest.java | 252 +++++++++++++++--- 3 files changed, 324 insertions(+), 62 deletions(-) diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java index 41d8d4ed498b..8d8ab69a6f4c 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java @@ -1,11 +1,17 @@ package com.dotmarketing.portlets.categories.business; +import java.util.Collection; import java.util.List; +import com.dotcms.util.PaginationUtil; +import com.dotcms.util.pagination.OrderDirection; import com.dotmarketing.exception.DotDataException; import com.dotmarketing.portlets.categories.model.Category; import com.liferay.portal.model.User; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.QueryParam; + /** * * @author David Torres @@ -276,12 +282,66 @@ public abstract class CategoryFactory { abstract protected List getAllChildren(Categorizable parent) throws DotDataException; /** - * Return a list of Categories regardless of their levels. + * Return a {@link Category} Collection looking through the entire category tree starting from a specified inode. + * This means the search will begin from the specified inode category and then proceed recursively through its children. * - * @param filter Value used to filter the Category by, returning only Categories that contain this value in their key, name, or variable name + * @param searchCriteria Search Criteria * * @return List of Category filtered */ - public abstract List findAll(final String filter) throws DotDataException; + public abstract Collection findAll(final CategorySearchCriteria searchCriteria) throws DotDataException; + + /** + * Represents Search Criteria for {@link Category} searching, you cans set the follow: + * + * - filter: Value used to filter the Category by, returning only Categories that contain this value in their key, name, or variable name. + * - inode: Entry point on the Category tree to start the searching. + * - orderBy: Field name to order the Category + * - direction: Order by direction, it can be 'ASC' or 'DESC' + */ + public static class CategorySearchCriteria { + final String inode; + final String filter; + final String orderBy; + final OrderDirection direction; + + private CategorySearchCriteria (final Builder builder) { + this.inode = builder.inode; + this.filter = builder.filter; + this.orderBy = builder.orderBy; + this.direction = builder.direction; + } + + public static class Builder { + private String inode; + private String filter; + private String orderBy = "category_name"; + private OrderDirection direction = OrderDirection.ASC; + + public Builder inode(String inode) { + this.inode = inode; + return this; + } + + public Builder filter(String filter) { + this.filter = filter; + return this; + } + + public Builder orderBy(String orderBy) { + this.orderBy = orderBy; + return this; + } + + public Builder direction(OrderDirection direction) { + this.direction = direction; + return this; + } + + public CategorySearchCriteria build() { + return new CategorySearchCriteria(this); + } + } + } } diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java index b64c6fac8870..d8db608401f9 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java @@ -21,17 +21,16 @@ import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; import com.dotmarketing.util.VelocityUtil; +import com.liferay.util.StringPool; + import java.io.Serializable; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; +import java.sql.*; import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * @@ -836,33 +835,62 @@ protected String suggestVelocityVarName(final String categoryVelVarName) throws } /** - * Default Implementation for {@link CategoryFactory#findAll(String)} - * @param filter Value used to filter the Category by, returning only Categories that contain this value in their key, name, or variable name + * Default Implementation for {@link CategoryFactory#findAll(CategorySearchCriteria)} + * @param searchCriteria Search Criteria * - * @return A Collection of {@link Category} filtered by 'filter', if filter is null then it return the that - * {@link CategoryFactoryImpl#findAll()} + * @return + * @throws DotDataException */ - public List findAll(final String filter) throws DotDataException { + public List findAll(final CategorySearchCriteria searchCriteria) + throws DotDataException { - if ( !UtilMethods.isSet(filter) ) { + if (!UtilMethods.isSet(searchCriteria.inode) && !UtilMethods.isSet(searchCriteria.filter)) { return findAll(); } - final DotConnect dc = new DotConnect() - .setSQL("SELECT * FROM category " + - "WHERE LOWER(category.category_name) LIKE ? OR " + - "LOWER(category.category_key) LIKE ? OR " + - "LOWER(category.category_velocity_var_name) LIKE ?"); + final String query = getFindAllSQLQuery(searchCriteria); + + final DotConnect dc = new DotConnect().setSQL(query); - dc.addObject("%" + filter.toLowerCase() + "%"); - dc.addObject("%" + filter.toLowerCase() + "%"); - dc.addObject("%" + filter.toLowerCase() + "%"); + if (UtilMethods.isSet(searchCriteria.inode) ) { + dc.addObject(searchCriteria.inode); + } + + if (UtilMethods.isSet(searchCriteria.filter) ) { + dc.addObject("%" + searchCriteria.filter.toLowerCase() + "%"); + dc.addObject("%" + searchCriteria.filter.toLowerCase() + "%"); + dc.addObject("%" + searchCriteria.filter.toLowerCase() + "%"); + } + + final List categories = convertForCategories(UtilMethods.isSet(searchCriteria.inode) ? + dc.loadObjectResults().stream() + .filter(map -> !map.get("inode").equals(searchCriteria.inode)) + .collect(Collectors.toList()) : dc.loadObjectResults()); - List categories = convertForCategories(dc.loadObjectResults()); updateCache(categories); return categories; } + private static String getFindAllSQLQuery(CategorySearchCriteria searchCriteria) { + final String queryTemplate = "WITH RECURSIVE CategoryHierarchy AS ( " + + "SELECT c.* FROM Category c %s " + + "UNION ALL " + + "SELECT c.* FROM Category c JOIN tree t ON c.inode = t.child JOIN CategoryHierarchy ch ON t.parent = ch.inode " + + ") " + + "SELECT * FROM CategoryHierarchy %s ORDER BY %s %s"; + + final String rootCategoryFilter = UtilMethods.isSet(searchCriteria.inode) ? "WHERE c.inode = ?" : StringPool.BLANK; + + final String filterCategories = UtilMethods.isSet(searchCriteria.filter) ? + "WHERE LOWER(category_name) LIKE ? OR " + + "LOWER(category_key) LIKE ? OR " + + "LOWER(category_velocity_var_name) LIKE ?" : StringPool.BLANK; + + final String query = String.format(queryTemplate, rootCategoryFilter, filterCategories, searchCriteria.orderBy, + searchCriteria.direction.toString()); + return query; + } + private void updateCache(List categories) throws DotDataException { for(final Category category : categories) { if(catCache.get(category.getInode()) == null) diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java index 4abad3532ee5..aca1f7e970c3 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java @@ -10,11 +10,16 @@ import com.dotcms.IntegrationTestBase; import com.dotcms.datagen.CategoryDataGen; import com.dotcms.util.IntegrationTestInitService; +import com.dotcms.util.pagination.OrderDirection; import com.dotmarketing.business.APILocator; import com.dotmarketing.business.FactoryLocator; +import com.dotmarketing.common.db.DotConnect; import com.dotmarketing.exception.DotDataException; import com.dotmarketing.exception.DotSecurityException; import com.dotmarketing.portlets.categories.model.Category; + +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -287,10 +292,22 @@ public void Test_Has_Dependencies() throws DotDataException { } /** - * Method to test: {@link CategoryFactoryImpl#findAll(String)} - * When: Create several Categories some of them with the word 'INCLUDE' on its name. key or variable name - * and call the method with filter equals to 'INCLUDE' - * Should: Return all the Category with the word include on any place + * Method to test: {@link CategoryFactoryImpl#findAll(CategoryFactory.CategorySearchCriteria)} + * When: + * + * - Create a random string to be used as the filter for the test. + * - Create two top-level categories, named topLevelCategory_1 and topLevelCategory_2, and include the filter in their names. + * - For topLevelCategory_1, create four children: + * Include the filter in three of these children: one in the key, one in the name, and one in the variable name. + * The fourth child should not include the filter anywhere. + * - Add a child to the last child of topLevelCategory_1 (the one without the filter) and include the filter in its name. + * Also, create a grandchild and include the filter in its name. + * - Create another child for topLevelCategory_1 and include the filter in the key, name, and variable name. + * - Call the method with the filter in both lowercase and uppercase. + * + * Should: + * + * Return five categories: the three children of topLevelCategory_1 that include the filter and the two grandchildren. */ @Test public void getAllCategoriesFiltered() throws DotDataException { @@ -314,74 +331,231 @@ public void getAllCategoriesFiltered() throws DotDataException { .parent(topLevelCategory_1) .nextPersisted(); - final Category topLevelCategory_2 = new CategoryDataGen().setCategoryName("Top Level Category " + stringToFilterBy) - .setKey("top_level_categoria") - .setCategoryVelocityVarName("top_level_categoria") - .nextPersisted(); - final Category childCategory_3 = new CategoryDataGen().setCategoryName("Child Category 3 " + stringToFilterBy) .setKey("child_category_3") .setCategoryVelocityVarName("child_category_3") - .parent(topLevelCategory_2) + .parent(topLevelCategory_1) .nextPersisted(); final Category childCategory_4 = new CategoryDataGen().setCategoryName("Child Category 4") .setKey("child_category_4") .setCategoryVelocityVarName("child_category_4") - .parent(topLevelCategory_2) + .parent(topLevelCategory_1) .nextPersisted(); - final Category topLevelCategory_3 = new CategoryDataGen().setCategoryName("Top Level Category") - .setKey("top_level_categoria") - .setCategoryVelocityVarName("top_level_categoria " + stringToFilterBy) + final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 1 " + stringToFilterBy) + .setKey("grand_child_category_1") + .setCategoryVelocityVarName("grand_child_category_1") + .parent(childCategory_4) .nextPersisted(); - final Category childCategory_5 = new CategoryDataGen().setCategoryName("Child Category 5") - .setKey("child_category_5") - .setCategoryVelocityVarName("child_category_5") - .parent(topLevelCategory_3) + final Category grandchildCategory_2 = new CategoryDataGen().setCategoryName("Grand Child Category 2 " + stringToFilterBy) + .setKey("grand_child_category_2") + .setCategoryVelocityVarName("grand_child_category_2") + .parent(grandchildCategory_1) .nextPersisted(); - final Category topLevelCategory_4 = new CategoryDataGen().setCategoryName("Top Level Category") - .setKey("top_level_categoria") - .setCategoryVelocityVarName("top_level_categoria") - .parent(topLevelCategory_3) + final Category topLevelCategory_2 = new CategoryDataGen().setCategoryName("Top Level Category " + stringToFilterBy) + .setKey("top_level_category_2") + .setCategoryVelocityVarName("top_level_category_2") .nextPersisted(); - final Category childCategory_6 = new CategoryDataGen().setCategoryName("Child Category 6") - .setKey("child_category_6") - .setCategoryVelocityVarName("child_category_6") - .parent(topLevelCategory_3) + final Category childCategory_5 = new CategoryDataGen().setCategoryName("Child Category 5" + stringToFilterBy) + .setKey("child_category_5 " + stringToFilterBy) + .setCategoryVelocityVarName("child_category_5 " + stringToFilterBy) + .parent(topLevelCategory_2) .nextPersisted(); - final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 6 " + stringToFilterBy) - .setKey("grand_child_category_6") - .setCategoryVelocityVarName("grand_child_category_6") - .parent(childCategory_6) - .nextPersisted(); - List categoriesExpected = list(topLevelCategory_1, topLevelCategory_2, childCategory_1, childCategory_2, - childCategory_3, topLevelCategory_3, grandchildCategory_1).stream() - .map(Category::getInode).collect(Collectors.toList()); + List categoriesExpected = list(childCategory_1, childCategory_2, childCategory_3, grandchildCategory_1, grandchildCategory_2) + .stream().map(Category::getInode).collect(Collectors.toList()); + + final CategoryFactory.CategorySearchCriteria categorySearchCriteria_1 = new CategoryFactory.CategorySearchCriteria.Builder() + .filter(stringToFilterBy.toUpperCase()) + .inode(topLevelCategory_1.getInode()) + .build(); - final List categories_1 = FactoryLocator.getCategoryFactory().findAll(stringToFilterBy.toUpperCase()) + final List categories_1 = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria_1) .stream().map(Category::getInode).collect(Collectors.toList()); assertEquals(categoriesExpected.size(), categories_1.size()); assertTrue(categories_1.containsAll(categoriesExpected)); - final List categories_2 = FactoryLocator.getCategoryFactory().findAll(stringToFilterBy.toLowerCase()) + final CategoryFactory.CategorySearchCriteria categorySearchCriteria_2 = new CategoryFactory.CategorySearchCriteria.Builder() + .filter(stringToFilterBy.toLowerCase()) + .inode(topLevelCategory_1.getInode()) + .build(); + + final List categories_2 = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria_2) .stream().map(Category::getInode).collect(Collectors.toList()); assertEquals(categoriesExpected.size(), categories_2.size()); assertTrue(categories_2.containsAll(categoriesExpected)); } + /** + * Method to test: {@link CategoryFactoryImpl#findAll(CategoryFactory.CategorySearchCriteria)} + * When: call the method with filter and inode equals to null + * Should: return all the Categories + * + * @throws DotDataException + */ @Test - public void getAllCategoriesWithNullFilter() throws DotDataException { - new CategoryDataGen().nextPersisted(); + public void getAllCategoriesWithNullFilterAndInode() throws DotDataException { + final Category topLevelCategory_1 = new CategoryDataGen().setCategoryName("Top Level Category") + .setKey("top_level_categoria") + .setCategoryVelocityVarName("top_level_categoria") + .nextPersisted(); + + final Category childCategory_1 = new CategoryDataGen().setCategoryName("Child Category 1") + .setKey("child_category_1") + .setCategoryVelocityVarName("child_category_1") + .parent(topLevelCategory_1) + .nextPersisted(); - final List categoriesWithFilter = FactoryLocator.getCategoryFactory().findAll(null); + final Category childCategory_2 = new CategoryDataGen().setCategoryName("Child Category 2") + .setKey("child_category_2") + .setCategoryVelocityVarName("child_category_2") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 1") + .setKey("grand_child_category_1") + .setCategoryVelocityVarName("grand_child_category_1") + .parent(childCategory_2) + .nextPersisted(); + + final CategoryFactory.CategorySearchCriteria categorySearchCriteria = new CategoryFactory.CategorySearchCriteria.Builder() + .build(); + + final Collection categoriesWithFilter = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria); final List categoriesWithoutFilter = FactoryLocator.getCategoryFactory().findAll(); + assertTrue(Objects.deepEquals(categoriesWithoutFilter, categoriesWithFilter)); } + /** + * Method to test: {@link CategoryFactoryImpl#findAll(CategoryFactory.CategorySearchCriteria)} + * When: call the method with filter equals to null and inode not null + * Should: return all children Categories + * + * @throws DotDataException + */ + @Test + public void getAllCategoriesWithNullFilter() throws DotDataException { + final Category topLevelCategory_1 = new CategoryDataGen().setCategoryName("Top Level Category 1") + .setKey("top_level_categoria_1") + .setCategoryVelocityVarName("top_level_categoria_1") + .nextPersisted(); + + final Category childCategory_1 = new CategoryDataGen().setCategoryName("Child Category 1") + .setKey("child_category_1") + .setCategoryVelocityVarName("child_category_1") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category childCategory_2 = new CategoryDataGen().setCategoryName("Child Category 2") + .setKey("child_category_2") + .setCategoryVelocityVarName("child_category_2") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category childCategory_3 = new CategoryDataGen().setCategoryName("Child Category 3 ") + .setKey("child_category_3") + .setCategoryVelocityVarName("child_category_3") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category childCategory_4 = new CategoryDataGen().setCategoryName("Child Category 4") + .setKey("child_category_4") + .setCategoryVelocityVarName("child_category_4") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 1") + .setKey("grand_child_category_1") + .setCategoryVelocityVarName("grand_child_category_1") + .parent(childCategory_4) + .nextPersisted(); + + final Category grandchildCategory_2 = new CategoryDataGen().setCategoryName("Grand Child Category 2") + .setKey("grand_child_category_2") + .setCategoryVelocityVarName("grand_child_category_2") + .parent(grandchildCategory_1) + .nextPersisted(); + + final Category topLevelCategory_2 = new CategoryDataGen().setCategoryName("Top Level Category 2") + .setKey("top_level_category_2") + .setCategoryVelocityVarName("top_level_category_2") + .nextPersisted(); + + final Category childCategory_5 = new CategoryDataGen().setCategoryName("Child Category 5") + .setKey("child_category_5") + .setCategoryVelocityVarName("child_category_5") + .parent(topLevelCategory_2) + .nextPersisted(); + + final CategoryFactory.CategorySearchCriteria categorySearchCriteria = new CategoryFactory.CategorySearchCriteria.Builder() + .inode(topLevelCategory_1.getInode()) + .build(); + + List categoriesExpected = list(childCategory_1, childCategory_2, childCategory_3, childCategory_4, + grandchildCategory_1, grandchildCategory_2).stream().map(Category::getInode).collect(Collectors.toList()); + + final List categories = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria) + .stream().map(Category::getInode).collect(Collectors.toList()); + assertEquals(categoriesExpected.size(), categories.size()); + assertTrue(categories.containsAll(categoriesExpected)); + } + + /** + * Method to test: {@link CategoryFactoryImpl#findAll(CategoryFactory.CategorySearchCriteria)} + * When: Create a set of {@link Category} and called the method ordering by key + * Should: return all children Categories ordered + * + * @throws DotDataException + */ + @Test + public void getAllCategoriesFilteredOrdered() throws DotDataException { + final Category topLevelCategory_1 = new CategoryDataGen().setCategoryName("Top Level Category 1") + .setKey("top_level") + .setCategoryVelocityVarName("top_level_categoria_1") + .nextPersisted(); + + final Category childCategory_1 = new CategoryDataGen().setCategoryName("Child Category 1") + .setKey("A") + .setCategoryVelocityVarName("child_category_1") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category childCategory_2 = new CategoryDataGen().setCategoryName("Child Category 2") + .setKey("C") + .setCategoryVelocityVarName("child_category_2") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 1") + .setKey("B") + .setCategoryVelocityVarName("grand_child_category_1") + .parent(childCategory_2) + .nextPersisted(); + + final CategoryFactory.CategorySearchCriteria categorySearchCriteria = new CategoryFactory.CategorySearchCriteria.Builder() + .orderBy("category_key") + .direction(OrderDirection.ASC) + .inode(topLevelCategory_1.getInode()) + .build(); + + final List categoriesInode = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria) + .stream().map(Category::getInode).collect(Collectors.toList()); + + List categoriesExpected = list(childCategory_1, grandchildCategory_1, childCategory_2).stream() + .map(Category::getInode).collect(Collectors.toList()); + + assertEquals(categoriesExpected.size(), categoriesInode.size()); + + for (int i =0; i < categoriesExpected.size(); i++){ + assertEquals(categoriesExpected.get(i), categoriesInode.get(i)); + } + + } + } From 15aa5848cb228fd92ea4d68fe0f1580e8a301065 Mon Sep 17 00:00:00 2001 From: freddyDOTCMS Date: Mon, 1 Jul 2024 15:51:01 -0600 Subject: [PATCH 5/5] Feedback --- .../categories/business/CategoryFactory.java | 10 +- .../business/CategoryFactoryImpl.java | 12 +- .../business/CategoryFactoryTest.java | 103 ++++++++++++------ 3 files changed, 81 insertions(+), 44 deletions(-) diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java index 8d8ab69a6f4c..01d1e34a71af 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactory.java @@ -300,26 +300,26 @@ public abstract class CategoryFactory { * - direction: Order by direction, it can be 'ASC' or 'DESC' */ public static class CategorySearchCriteria { - final String inode; + final String rootInode; final String filter; final String orderBy; final OrderDirection direction; private CategorySearchCriteria (final Builder builder) { - this.inode = builder.inode; + this.rootInode = builder.rootInode; this.filter = builder.filter; this.orderBy = builder.orderBy; this.direction = builder.direction; } public static class Builder { - private String inode; + private String rootInode; private String filter; private String orderBy = "category_name"; private OrderDirection direction = OrderDirection.ASC; - public Builder inode(String inode) { - this.inode = inode; + public Builder rootInode(String rootInode) { + this.rootInode = rootInode; return this; } diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java index d8db608401f9..7c732bd68305 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/categories/business/CategoryFactoryImpl.java @@ -844,7 +844,7 @@ protected String suggestVelocityVarName(final String categoryVelVarName) throws public List findAll(final CategorySearchCriteria searchCriteria) throws DotDataException { - if (!UtilMethods.isSet(searchCriteria.inode) && !UtilMethods.isSet(searchCriteria.filter)) { + if (!UtilMethods.isSet(searchCriteria.rootInode) && !UtilMethods.isSet(searchCriteria.filter)) { return findAll(); } @@ -852,8 +852,8 @@ public List findAll(final CategorySearchCriteria searchCriteria) final DotConnect dc = new DotConnect().setSQL(query); - if (UtilMethods.isSet(searchCriteria.inode) ) { - dc.addObject(searchCriteria.inode); + if (UtilMethods.isSet(searchCriteria.rootInode) ) { + dc.addObject(searchCriteria.rootInode); } if (UtilMethods.isSet(searchCriteria.filter) ) { @@ -862,9 +862,9 @@ public List findAll(final CategorySearchCriteria searchCriteria) dc.addObject("%" + searchCriteria.filter.toLowerCase() + "%"); } - final List categories = convertForCategories(UtilMethods.isSet(searchCriteria.inode) ? + final List categories = convertForCategories(UtilMethods.isSet(searchCriteria.rootInode) ? dc.loadObjectResults().stream() - .filter(map -> !map.get("inode").equals(searchCriteria.inode)) + .filter(map -> !map.get("inode").equals(searchCriteria.rootInode)) .collect(Collectors.toList()) : dc.loadObjectResults()); updateCache(categories); @@ -879,7 +879,7 @@ private static String getFindAllSQLQuery(CategorySearchCriteria searchCriteria) ") " + "SELECT * FROM CategoryHierarchy %s ORDER BY %s %s"; - final String rootCategoryFilter = UtilMethods.isSet(searchCriteria.inode) ? "WHERE c.inode = ?" : StringPool.BLANK; + final String rootCategoryFilter = UtilMethods.isSet(searchCriteria.rootInode) ? "WHERE c.inode = ?" : StringPool.BLANK; final String filterCategories = UtilMethods.isSet(searchCriteria.filter) ? "WHERE LOWER(category_name) LIKE ? OR " + diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java index aca1f7e970c3..adaa4bb53349 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/categories/business/CategoryFactoryTest.java @@ -8,6 +8,8 @@ import static org.junit.Assert.assertTrue; import com.dotcms.IntegrationTestBase; +import com.dotcms.api.vtl.model.DotJSON; +import com.dotcms.cache.DotJSONCacheAddTestCase; import com.dotcms.datagen.CategoryDataGen; import com.dotcms.util.IntegrationTestInitService; import com.dotcms.util.pagination.OrderDirection; @@ -22,17 +24,23 @@ import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.function.Function; import java.util.stream.Collectors; import com.liferay.util.StringUtil; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; import net.bytebuddy.utility.RandomString; import org.jetbrains.annotations.NotNull; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; /*** * Category Factory Test */ +@RunWith(DataProviderRunner.class) public class CategoryFactoryTest extends IntegrationTestBase { private static CategoryFactory categoryFactory; @@ -291,6 +299,19 @@ public void Test_Has_Dependencies() throws DotDataException { assertTrue(categoryFactory.hasDependencies(root)); } + @DataProvider + public static Object[] findCategoriesFilters() { + final String stringToFilterBy = new RandomString().nextString(); + + return new FilterTestCase[] { + new FilterTestCase(stringToFilterBy, String::toLowerCase), + new FilterTestCase(stringToFilterBy, String::toUpperCase), + new FilterTestCase(stringToFilterBy, (filter) -> + filter.substring(1, filter.length()/2).toLowerCase() + + filter.substring(filter.length()/2).toUpperCase()) + }; + } + /** * Method to test: {@link CategoryFactoryImpl#findAll(CategoryFactory.CategorySearchCriteria)} * When: @@ -303,35 +324,36 @@ public void Test_Has_Dependencies() throws DotDataException { * - Add a child to the last child of topLevelCategory_1 (the one without the filter) and include the filter in its name. * Also, create a grandchild and include the filter in its name. * - Create another child for topLevelCategory_1 and include the filter in the key, name, and variable name. - * - Call the method with the filter in both lowercase and uppercase. + * - Call the method with the filter * * Should: * * Return five categories: the three children of topLevelCategory_1 that include the filter and the two grandchildren. */ @Test - public void getAllCategoriesFiltered() throws DotDataException { + @UseDataProvider("findCategoriesFilters") + public void getAllCategoriesFiltered(final FilterTestCase filterTestCase) throws DotDataException { - final String stringToFilterBy = new RandomString().nextString(); + final String stringToFilterBy = filterTestCase.filter; - final Category topLevelCategory_1 = new CategoryDataGen().setCategoryName("Top Level Category " + stringToFilterBy) + final Category topLevelCategory_1 = new CategoryDataGen().setCategoryName("Top Level Category " + filterTestCase.filter) .setKey("top_level_categoria") .setCategoryVelocityVarName("top_level_categoria") .nextPersisted(); final Category childCategory_1 = new CategoryDataGen().setCategoryName("Child Category 1") - .setKey("child_category_1 " + stringToFilterBy) + .setKey("child_category_1 " + filterTestCase.filter) .setCategoryVelocityVarName("child_category_1") .parent(topLevelCategory_1) .nextPersisted(); final Category childCategory_2 = new CategoryDataGen().setCategoryName("Child Category 2") .setKey("child_category_2") - .setCategoryVelocityVarName("child_category_2 " + stringToFilterBy) + .setCategoryVelocityVarName("child_category_2 " + filterTestCase.filter) .parent(topLevelCategory_1) .nextPersisted(); - final Category childCategory_3 = new CategoryDataGen().setCategoryName("Child Category 3 " + stringToFilterBy) + final Category childCategory_3 = new CategoryDataGen().setCategoryName("Child Category 3 " + filterTestCase.filter) .setKey("child_category_3") .setCategoryVelocityVarName("child_category_3") .parent(topLevelCategory_1) @@ -343,52 +365,54 @@ public void getAllCategoriesFiltered() throws DotDataException { .parent(topLevelCategory_1) .nextPersisted(); - final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 1 " + stringToFilterBy) + final Category childCategory_6 = new CategoryDataGen().setCategoryName(filterTestCase.filter + "Child Category 6") + .setKey("child_category_6") + .setCategoryVelocityVarName("child_category_6") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category childCategory_7 = new CategoryDataGen().setCategoryName("Child " + filterTestCase.filter + "Category 7") + .setKey("child_category_7") + .setCategoryVelocityVarName("child_category_7") + .parent(topLevelCategory_1) + .nextPersisted(); + + final Category grandchildCategory_1 = new CategoryDataGen().setCategoryName("Grand Child Category 1 " + filterTestCase.filter) .setKey("grand_child_category_1") .setCategoryVelocityVarName("grand_child_category_1") .parent(childCategory_4) .nextPersisted(); - final Category grandchildCategory_2 = new CategoryDataGen().setCategoryName("Grand Child Category 2 " + stringToFilterBy) + final Category grandchildCategory_2 = new CategoryDataGen().setCategoryName("Grand Child Category 2 " + filterTestCase.filter) .setKey("grand_child_category_2") .setCategoryVelocityVarName("grand_child_category_2") .parent(grandchildCategory_1) .nextPersisted(); - final Category topLevelCategory_2 = new CategoryDataGen().setCategoryName("Top Level Category " + stringToFilterBy) + final Category topLevelCategory_2 = new CategoryDataGen().setCategoryName("Top Level Category " + filterTestCase.filter) .setKey("top_level_category_2") .setCategoryVelocityVarName("top_level_category_2") .nextPersisted(); - final Category childCategory_5 = new CategoryDataGen().setCategoryName("Child Category 5" + stringToFilterBy) - .setKey("child_category_5 " + stringToFilterBy) - .setCategoryVelocityVarName("child_category_5 " + stringToFilterBy) + final Category childCategory_5 = new CategoryDataGen().setCategoryName("Child Category 5" + filterTestCase.filter) + .setKey("child_category_5 " + filterTestCase.filter) + .setCategoryVelocityVarName("child_category_5 " + filterTestCase.filter) .parent(topLevelCategory_2) .nextPersisted(); - List categoriesExpected = list(childCategory_1, childCategory_2, childCategory_3, grandchildCategory_1, grandchildCategory_2) - .stream().map(Category::getInode).collect(Collectors.toList()); + List categoriesExpected = list(childCategory_1, childCategory_2, childCategory_3, grandchildCategory_1, + grandchildCategory_2, childCategory_6, childCategory_7).stream().map(Category::getInode).collect(Collectors.toList()); - final CategoryFactory.CategorySearchCriteria categorySearchCriteria_1 = new CategoryFactory.CategorySearchCriteria.Builder() - .filter(stringToFilterBy.toUpperCase()) - .inode(topLevelCategory_1.getInode()) + final CategoryFactory.CategorySearchCriteria categorySearchCriteria = new CategoryFactory.CategorySearchCriteria.Builder() + .filter(filterTestCase.transformToSearch()) + .rootInode(topLevelCategory_1.getInode()) .build(); - final List categories_1 = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria_1) + final List categories = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria) .stream().map(Category::getInode).collect(Collectors.toList()); - assertEquals(categoriesExpected.size(), categories_1.size()); - assertTrue(categories_1.containsAll(categoriesExpected)); - - final CategoryFactory.CategorySearchCriteria categorySearchCriteria_2 = new CategoryFactory.CategorySearchCriteria.Builder() - .filter(stringToFilterBy.toLowerCase()) - .inode(topLevelCategory_1.getInode()) - .build(); - - final List categories_2 = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria_2) - .stream().map(Category::getInode).collect(Collectors.toList()); - assertEquals(categoriesExpected.size(), categories_2.size()); - assertTrue(categories_2.containsAll(categoriesExpected)); + assertEquals(categoriesExpected.size(), categories.size()); + assertTrue(categories.containsAll(categoriesExpected)); } /** @@ -494,7 +518,7 @@ public void getAllCategoriesWithNullFilter() throws DotDataException { .nextPersisted(); final CategoryFactory.CategorySearchCriteria categorySearchCriteria = new CategoryFactory.CategorySearchCriteria.Builder() - .inode(topLevelCategory_1.getInode()) + .rootInode(topLevelCategory_1.getInode()) .build(); List categoriesExpected = list(childCategory_1, childCategory_2, childCategory_3, childCategory_4, @@ -541,7 +565,7 @@ public void getAllCategoriesFilteredOrdered() throws DotDataException { final CategoryFactory.CategorySearchCriteria categorySearchCriteria = new CategoryFactory.CategorySearchCriteria.Builder() .orderBy("category_key") .direction(OrderDirection.ASC) - .inode(topLevelCategory_1.getInode()) + .rootInode(topLevelCategory_1.getInode()) .build(); final List categoriesInode = FactoryLocator.getCategoryFactory().findAll(categorySearchCriteria) @@ -558,4 +582,17 @@ public void getAllCategoriesFilteredOrdered() throws DotDataException { } + private static class FilterTestCase { + private String filter; + private Function transformToSearch; + + public FilterTestCase(final String filter, final Function transformToSearch) { + this.filter = filter; + this.transformToSearch = transformToSearch; + } + + public String transformToSearch(){ + return transformToSearch.apply(filter); + } + } }