From deb798e6479ffd591511a2f5e045efcaae9be4dd Mon Sep 17 00:00:00 2001 From: Hasini Samarathunga Date: Mon, 22 Jul 2024 10:20:47 +0530 Subject: [PATCH] Added unit tests for multiattribute filtering --- .../OrganizationManagementConstants.java | 2 +- .../impl/OrganizationManagementDAOImpl.java | 4 +-- .../service/model/FilterQueryBuilder.java | 4 +-- .../service/OrganizationManagerImplTest.java | 2 ++ .../OrganizationManagementDAOImplTest.java | 35 +++++++++++++++++++ 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/constant/OrganizationManagementConstants.java b/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/constant/OrganizationManagementConstants.java index 1efe9bd9..f86c0411 100644 --- a/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/constant/OrganizationManagementConstants.java +++ b/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/constant/OrganizationManagementConstants.java @@ -422,7 +422,7 @@ public enum ErrorMessages { ERROR_CODE_ERROR_EVALUATING_ADD_ORGANIZATION_AUTHORIZATION("65019", "Unable to create the organization.", "Server encountered an error while evaluating authorization of user to create the " + "organization in parent organization with ID: %s."), - ERROR_CODE_ERROR_BUILDING_PAGINATED_RESPONSE_URL("65020", "Unable to retrieve the organizations.", + ERROR_CODE_ERROR_BUILDING_PAGINATED_RESPONSE_URL("65020", "Unable to retrieve the paginated results.", "Server encountered an error while building paginated response URL."), ERROR_CODE_ERROR_MISSING_SUPER("65021", "Unable to create the organization.", "Server encountered an error while retrieving the super organization."), diff --git a/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/dao/impl/OrganizationManagementDAOImpl.java b/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/dao/impl/OrganizationManagementDAOImpl.java index 0aff13d1..52cf3654 100644 --- a/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/dao/impl/OrganizationManagementDAOImpl.java +++ b/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/dao/impl/OrganizationManagementDAOImpl.java @@ -738,7 +738,7 @@ private List getOrganizationsList(boolean authorizedSubOrgsOnly, b String userID = getUserId(); String sqlStmt = prepareGetOrganizationQuery(authorizedSubOrgsOnly, recursive, sortOrder, applicationAudience, filterQueryBuilder, parentIdFilterQueryBuilder, userID); - boolean isFilteringMetaAttributes = filterQueryBuilder.getMetaAttributeCount() > 1; + boolean isFilteringMetaAttributes = filterQueryBuilder.getMetaAttributeCount() > 0; if (isFilteringMetaAttributes) { sqlStmt = String.format(GET_ALL_UM_ORG_ATTRIBUTES, sqlStmt); @@ -1558,7 +1558,7 @@ private String prepareGetOrganizationQuery(boolean authorizedSubOrgsOnly, boolea String sqlStmt = getOrgSqlStatement(authorizedSubOrgsOnly, applicationAudience); String getOrgSqlStmtTail = getOrgSqlStmtTail(authorizedSubOrgsOnly, applicationAudience); - if (filterQueryBuilder.getMetaAttributeCount() > 1) { + if (filterQueryBuilder.getMetaAttributeCount() > 0) { for (String placeholder : filterQueryBuilder.getMetaAttributePlaceholders()) { sqlStmt = sqlStmt.replace("WHERE", String.format(INNER_JOIN_UM_ORG_ATTRIBUTE, placeholder, placeholder)); diff --git a/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/model/FilterQueryBuilder.java b/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/model/FilterQueryBuilder.java index 0018a593..9ffca585 100644 --- a/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/model/FilterQueryBuilder.java +++ b/components/org.wso2.carbon.identity.organization.management.service/src/main/java/org/wso2/carbon/identity/organization/management/service/model/FilterQueryBuilder.java @@ -35,7 +35,7 @@ public class FilterQueryBuilder { private int count = 1; private String filter; private List metaAttributePlaceholders = new ArrayList<>(); - private int metaAttributeCount = 1; + private int metaAttributeCount; /** * Get filter query builder attributes. @@ -116,9 +116,9 @@ public List getMetaAttributePlaceholders() { */ public String generateMetaAttributePlaceholder() { + metaAttributeCount++; String placeholder = META_ATTRIBUTE_PLACEHOLDER_PREFIX + metaAttributeCount; metaAttributePlaceholders.add(placeholder); - metaAttributeCount++; return placeholder; } diff --git a/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImplTest.java b/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImplTest.java index 50428d03..c3d1f73e 100644 --- a/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImplTest.java +++ b/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/OrganizationManagerImplTest.java @@ -387,6 +387,8 @@ public Object[][] dataForFilterOrganizationsByMetaAttributes() { return new Object[][]{ {"attributes.country co S", false}, + {"attributes.country co S and name eq Greater", false}, + {"attributes.country sw S and attributes.city ew o", false}, {"attributes.country co Z", true}, {"attributes.invalid co S", true} }; diff --git a/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/dao/impl/OrganizationManagementDAOImplTest.java b/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/dao/impl/OrganizationManagementDAOImplTest.java index 3f2389f4..f90dcea3 100644 --- a/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/dao/impl/OrganizationManagementDAOImplTest.java +++ b/components/org.wso2.carbon.identity.organization.management.service/src/test/java/org/wso2/carbon/identity/organization/management/service/dao/impl/OrganizationManagementDAOImplTest.java @@ -59,6 +59,8 @@ public class OrganizationManagementDAOImplTest { private static final String ATTRIBUTE_KEY = "country"; private static final String ATTRIBUTE_KEY_REGION = "region"; private static final String ATTRIBUTE_VALUE = "Sri Lanka"; + private static final String ATTRIBUTE_KEY_CITY = "city"; + private static final String ATTRIBUTE_VALUE_CITY = "Colombo"; private static final String ORG_NAME = "XYZ builders"; private static final String CHILD_ORG_NAME = "ABC builders"; private static final String ORG_DESCRIPTION = "This is a construction company."; @@ -76,6 +78,7 @@ public void setUp() throws Exception { orgId = generateUniqueID(); childOrgId = generateUniqueID(); storeChildOrganization(orgId, ORG_NAME, ORG_DESCRIPTION, SUPER_ORG_ID); + storeOrganizationAttributes(orgId, ATTRIBUTE_KEY_CITY, ATTRIBUTE_VALUE_CITY); storeChildOrganization(childOrgId, CHILD_ORG_NAME, ORG_DESCRIPTION, orgId); storeOrganizationAttributes(childOrgId, ATTRIBUTE_KEY_REGION, ATTRIBUTE_VALUE); } @@ -213,6 +216,38 @@ public void testFilterOrganizationsByMetaAttributes(String attributeValue, Strin SUPER_ORG_ID, "DESC", expressionNodes, new ArrayList<>()); Assert.assertEquals(organizations.get(0).getName(), ORG_NAME); + } + + @DataProvider(name = "dataForFilterOrganizationsByMultipleAttributes") + public Object[][] dataForFilterOrganizationsByMultipleAttributes() { + + return new Object[][]{ + {"attributes.country co S and name co XYZ"}, + {"attributes.country sw S and attributes.country ew a and attributes.city eq Colombo"}, + }; + } + + @Test(dataProvider = "dataForFilterOrganizationsByMultipleAttributes") + public void testFilterOrganizationsByMultipleAttributes(String filter) + throws OrganizationManagementServerException { + + TestUtils.mockCarbonContext(SUPER_ORG_ID); + List expressionNodes = new ArrayList<>(); + String[] filters = filter.split(" and "); + + for (String singleFilter : filters) { + String[] parts = singleFilter.split(" "); + String attributeValue = parts[0]; + String operation = parts[1]; + String value = parts[2]; + + ExpressionNode expressionNode = getExpressionNode(attributeValue, operation, value); + expressionNodes.add(expressionNode); + } + List organizations = organizationManagementDAO.getOrganizationsList(false, 10, + SUPER_ORG_ID, "DESC", expressionNodes, new ArrayList<>()); + + Assert.assertEquals(organizations.get(0).getName(), ORG_NAME); Assert.assertEquals(organizations.get(0).getAttributes().get(0).getKey(), ATTRIBUTE_KEY); Assert.assertEquals(organizations.get(0).getAttributes().get(0).getValue(), ATTRIBUTE_VALUE); }