@Test public void searchTest6() throws QueryException, ComponentLookupException { String input = "a"; JSONArray resultsArray = new JSONArray(); Query q = mock(Query.class); QueryManager qm = this.mocker.getInstance(QueryManager.class); when(q.bindValue("input", "%%" + input + "%%")).thenReturn(q); when(q.<String>execute()).thenThrow(new QueryException("failed", q, null)); when(qm.createQuery(usersQueryString, Query.XWQL)).thenReturn(q); JSONObject expectedResult = new JSONObject(); expectedResult.put("matched", resultsArray); Assert.assertEquals(expectedResult.toString(), this.mocker.getComponentUnderTest().search(input, 10, true, false, true)); Assert.assertEquals(expectedResult.toString(), this.mocker.getComponentUnderTest().search(input, 10, false, false, true)); }
public String search(String input, int maxResults, boolean searchUsers, boolean searchGroups, boolean returnAsJSON) { String formattedInput = String.format(UsersAndGroups.INPUT_FORMAT, input.toLowerCase()); JSONArray resultArray = new JSONArray(); try { if (searchUsers) { runUsersQuery(resultArray, UsersAndGroups.usersQueryString, formattedInput, maxResults); } if (searchGroups && resultArray.length() < maxResults) { runGroupsQuery(resultArray, UsersAndGroups.groupsQueryString, formattedInput, maxResults - resultArray.length()); } } catch (Exception ex) { this.logger.error("Error in search ({})", input, ex.getMessage()); } if (returnAsJSON) { JSONObject result = new JSONObject(); result.put("matched", resultArray); return result.toString(); } else { StringBuilder xmlResult = new StringBuilder(); xmlResult.append("<results>"); for (Object object : resultArray) { JSONObject entityJson = (JSONObject) object; String escapedId = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.ID_KEY)); String escapedInfo = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.INFO_KEY)); String escapedValue = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.VALUE_KEY)); String escapedDescription = XMLUtils.escapeElementContent(entityJson.optString(UsersAndGroups.DESC_KEY)); xmlResult.append("<rs id=\"").append(escapedId).append("\" "); xmlResult.append("icon=\"").append(entityJson.optString(UsersAndGroups.ICON_KEY)).append("\" "); xmlResult.append("value=\"").append(escapedValue).append("\" "); xmlResult.append("info=\"").append(escapedInfo).append("\">"); xmlResult.append(escapedDescription); xmlResult.append("</rs>"); } xmlResult.append("</results>"); return xmlResult.toString(); } }
UsersAndGroups implements Initializable { public String search(String input, int maxResults, boolean searchUsers, boolean searchGroups, boolean returnAsJSON) { String formattedInput = String.format(UsersAndGroups.INPUT_FORMAT, input.toLowerCase()); JSONArray resultArray = new JSONArray(); try { if (searchUsers) { runUsersQuery(resultArray, UsersAndGroups.usersQueryString, formattedInput, maxResults); } if (searchGroups && resultArray.length() < maxResults) { runGroupsQuery(resultArray, UsersAndGroups.groupsQueryString, formattedInput, maxResults - resultArray.length()); } } catch (Exception ex) { this.logger.error("Error in search ({})", input, ex.getMessage()); } if (returnAsJSON) { JSONObject result = new JSONObject(); result.put("matched", resultArray); return result.toString(); } else { StringBuilder xmlResult = new StringBuilder(); xmlResult.append("<results>"); for (Object object : resultArray) { JSONObject entityJson = (JSONObject) object; String escapedId = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.ID_KEY)); String escapedInfo = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.INFO_KEY)); String escapedValue = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.VALUE_KEY)); String escapedDescription = XMLUtils.escapeElementContent(entityJson.optString(UsersAndGroups.DESC_KEY)); xmlResult.append("<rs id=\"").append(escapedId).append("\" "); xmlResult.append("icon=\"").append(entityJson.optString(UsersAndGroups.ICON_KEY)).append("\" "); xmlResult.append("value=\"").append(escapedValue).append("\" "); xmlResult.append("info=\"").append(escapedInfo).append("\">"); xmlResult.append(escapedDescription); xmlResult.append("</rs>"); } xmlResult.append("</results>"); return xmlResult.toString(); } } }
UsersAndGroups implements Initializable { public String search(String input, int maxResults, boolean searchUsers, boolean searchGroups, boolean returnAsJSON) { String formattedInput = String.format(UsersAndGroups.INPUT_FORMAT, input.toLowerCase()); JSONArray resultArray = new JSONArray(); try { if (searchUsers) { runUsersQuery(resultArray, UsersAndGroups.usersQueryString, formattedInput, maxResults); } if (searchGroups && resultArray.length() < maxResults) { runGroupsQuery(resultArray, UsersAndGroups.groupsQueryString, formattedInput, maxResults - resultArray.length()); } } catch (Exception ex) { this.logger.error("Error in search ({})", input, ex.getMessage()); } if (returnAsJSON) { JSONObject result = new JSONObject(); result.put("matched", resultArray); return result.toString(); } else { StringBuilder xmlResult = new StringBuilder(); xmlResult.append("<results>"); for (Object object : resultArray) { JSONObject entityJson = (JSONObject) object; String escapedId = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.ID_KEY)); String escapedInfo = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.INFO_KEY)); String escapedValue = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.VALUE_KEY)); String escapedDescription = XMLUtils.escapeElementContent(entityJson.optString(UsersAndGroups.DESC_KEY)); xmlResult.append("<rs id=\"").append(escapedId).append("\" "); xmlResult.append("icon=\"").append(entityJson.optString(UsersAndGroups.ICON_KEY)).append("\" "); xmlResult.append("value=\"").append(escapedValue).append("\" "); xmlResult.append("info=\"").append(escapedInfo).append("\">"); xmlResult.append(escapedDescription); xmlResult.append("</rs>"); } xmlResult.append("</results>"); return xmlResult.toString(); } }  }
UsersAndGroups implements Initializable { public String search(String input, int maxResults, boolean searchUsers, boolean searchGroups, boolean returnAsJSON) { String formattedInput = String.format(UsersAndGroups.INPUT_FORMAT, input.toLowerCase()); JSONArray resultArray = new JSONArray(); try { if (searchUsers) { runUsersQuery(resultArray, UsersAndGroups.usersQueryString, formattedInput, maxResults); } if (searchGroups && resultArray.length() < maxResults) { runGroupsQuery(resultArray, UsersAndGroups.groupsQueryString, formattedInput, maxResults - resultArray.length()); } } catch (Exception ex) { this.logger.error("Error in search ({})", input, ex.getMessage()); } if (returnAsJSON) { JSONObject result = new JSONObject(); result.put("matched", resultArray); return result.toString(); } else { StringBuilder xmlResult = new StringBuilder(); xmlResult.append("<results>"); for (Object object : resultArray) { JSONObject entityJson = (JSONObject) object; String escapedId = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.ID_KEY)); String escapedInfo = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.INFO_KEY)); String escapedValue = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.VALUE_KEY)); String escapedDescription = XMLUtils.escapeElementContent(entityJson.optString(UsersAndGroups.DESC_KEY)); xmlResult.append("<rs id=\"").append(escapedId).append("\" "); xmlResult.append("icon=\"").append(entityJson.optString(UsersAndGroups.ICON_KEY)).append("\" "); xmlResult.append("value=\"").append(escapedValue).append("\" "); xmlResult.append("info=\"").append(escapedInfo).append("\">"); xmlResult.append(escapedDescription); xmlResult.append("</rs>"); } xmlResult.append("</results>"); return xmlResult.toString(); } }  @Override void initialize(); boolean isUser(EntityReference userOrGroup); boolean isGroup(EntityReference userOrGroup); String search(String input, int maxResults, boolean searchUsers, boolean searchGroups,
        boolean returnAsJSON); JSONObject getAll(); }
UsersAndGroups implements Initializable { public String search(String input, int maxResults, boolean searchUsers, boolean searchGroups, boolean returnAsJSON) { String formattedInput = String.format(UsersAndGroups.INPUT_FORMAT, input.toLowerCase()); JSONArray resultArray = new JSONArray(); try { if (searchUsers) { runUsersQuery(resultArray, UsersAndGroups.usersQueryString, formattedInput, maxResults); } if (searchGroups && resultArray.length() < maxResults) { runGroupsQuery(resultArray, UsersAndGroups.groupsQueryString, formattedInput, maxResults - resultArray.length()); } } catch (Exception ex) { this.logger.error("Error in search ({})", input, ex.getMessage()); } if (returnAsJSON) { JSONObject result = new JSONObject(); result.put("matched", resultArray); return result.toString(); } else { StringBuilder xmlResult = new StringBuilder(); xmlResult.append("<results>"); for (Object object : resultArray) { JSONObject entityJson = (JSONObject) object; String escapedId = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.ID_KEY)); String escapedInfo = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.INFO_KEY)); String escapedValue = XMLUtils.escapeAttributeValue(entityJson.optString(UsersAndGroups.VALUE_KEY)); String escapedDescription = XMLUtils.escapeElementContent(entityJson.optString(UsersAndGroups.DESC_KEY)); xmlResult.append("<rs id=\"").append(escapedId).append("\" "); xmlResult.append("icon=\"").append(entityJson.optString(UsersAndGroups.ICON_KEY)).append("\" "); xmlResult.append("value=\"").append(escapedValue).append("\" "); xmlResult.append("info=\"").append(escapedInfo).append("\">"); xmlResult.append(escapedDescription); xmlResult.append("</rs>"); } xmlResult.append("</results>"); return xmlResult.toString(); } }  @Override void initialize(); boolean isUser(EntityReference userOrGroup); boolean isGroup(EntityReference userOrGroup); String search(String input, int maxResults, boolean searchUsers, boolean searchGroups,
        boolean returnAsJSON); JSONObject getAll();  }