SecurityRestApi { @GET @Path("userlist/{searchText}") public Response getUserList(@PathParam("searchText") final String searchText) { final int numUsersToFetch = 5; List<String> usersList = authenticationService.getMatchedUsers(searchText, numUsersToFetch); List<String> rolesList = authenticationService.getMatchedRoles(); List<String> autoSuggestUserList = new ArrayList<>(); List<String> autoSuggestRoleList = new ArrayList<>(); Collections.sort(usersList); Collections.sort(rolesList); Collections.sort( usersList, (o1, o2) -> { if (o1.matches(searchText + "(.*)") && o2.matches(searchText + "(.*)")) { return 0; } else if (o1.matches(searchText + "(.*)")) { return -1; } return 0; }); int maxLength = 0; for (String user : usersList) { if (StringUtils.containsIgnoreCase(user, searchText)) { autoSuggestUserList.add(user); maxLength++; } if (maxLength == numUsersToFetch) { break; } } for (String role : rolesList) { if (StringUtils.containsIgnoreCase(role, searchText)) { autoSuggestRoleList.add(role); } } Map<String, List> returnListMap = new HashMap<>(); returnListMap.put("users", autoSuggestUserList); returnListMap.put("roles", autoSuggestRoleList); return new JsonResponse<>(Response.Status.OK, "", returnListMap).build(); } @Inject  SecurityRestApi(AuthenticationService authenticationService); @GET @Path("ticket") @ZeppelinApi Response ticket(); @GET @Path("userlist/{searchText}") Response getUserList(@PathParam("searchText") final String searchText);  }
@Test public void testGetUserList() throws IOException { GetMethod get = httpGet("/security/userlist/admi", "admin", "password1"); get.addRequestHeader("Origin", "http: Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>(){}.getType()); List<String> userList = (List) ((Map) resp.get("body")).get("users"); collector.checkThat("Search result size", userList.size(), CoreMatchers.equalTo(1)); collector.checkThat("Search result contains admin", userList.contains("admin"), CoreMatchers.equalTo(true)); get.releaseConnection(); GetMethod notUser = httpGet("/security/userlist/randomString", "admin", "password1"); notUser.addRequestHeader("Origin", "http: Map<String, Object> notUserResp = gson.fromJson(notUser.getResponseBodyAsString(), new TypeToken<Map<String, Object>>(){}.getType()); List<String> emptyUserList = (List) ((Map) notUserResp.get("body")).get("users"); collector.checkThat("Search result size", emptyUserList.size(), CoreMatchers.equalTo(0)); notUser.releaseConnection(); }