Skip to content

Commit

Permalink
Added unit tests for multiattribute filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
HasiniSama committed Jul 22, 2024
1 parent 82faa29 commit 863d649
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ private List<Organization> 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);
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class FilterQueryBuilder {
private int count = 1;
private String filter;
private List<String> metaAttributePlaceholders = new ArrayList<>();
private int metaAttributeCount = 1;
private int metaAttributeCount;

/**
* Get filter query builder attributes.
Expand Down Expand Up @@ -116,9 +116,9 @@ public List<String> getMetaAttributePlaceholders() {
*/
public String generateMetaAttributePlaceholder() {

metaAttributeCount++;
String placeholder = META_ATTRIBUTE_PLACEHOLDER_PREFIX + metaAttributeCount;
metaAttributePlaceholders.add(placeholder);
metaAttributeCount++;
return placeholder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<ExpressionNode> 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<Organization> 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);
}
Expand Down

0 comments on commit 863d649

Please sign in to comment.