### Question:
AccessRestrictionUtil { public void add(StringBuilder query, AccessGroupSet principals) throws AccessRestrictionException { if (disablePermissionFiltering) { return; } if (principals == null || principals.size() == 0) { throw new AccessRestrictionException("No access groups were provided."); } if (globalPermissionEvaluator.hasGlobalPrincipal(principals)) { return; } boolean allowPatronAccess = searchSettings.getAllowPatronAccess(); String joinedGroups = principals.joinAccessGroups(" OR ", null, true); if (allowPatronAccess) { query.append(" AND (").append("readGroup:(").append(joinedGroups).append(')') .append(" OR adminGroup:(").append(joinedGroups).append("))"); } else { query.append(" AND adminGroup:(").append(joinedGroups).append(')'); } }  void add(StringBuilder query, AccessGroupSet principals); void setSearchSettings(SearchSettings searchSettings); void setDisablePermissionFiltering(boolean disablePermissionFiltering); void setGlobalPermissionEvaluator(GlobalPermissionEvaluator globalPermissionEvaluator);  }### Answer:
@Test(expected = AccessRestrictionException.class) public void addAccessNoGroupsTest() { accessGroups = new AccessGroupSet(); StringBuilder query = new StringBuilder(BASE_QUERY); restrictionUtil.add(query, accessGroups); }
@Test public void addAccessGlobalPermissionsTest() { when(globalPermissionEvaluator.hasGlobalPrincipal(anySetOf(String.class))).thenReturn(true); StringBuilder query = new StringBuilder(BASE_QUERY); restrictionUtil.add(query, accessGroups); assertEquals(BASE_QUERY, query.toString()); }
@Test public void addAccessAllowPatronAccessTest() { when(searchSettings.getAllowPatronAccess()).thenReturn(true); StringBuilder query = new StringBuilder(BASE_QUERY); restrictionUtil.add(query, accessGroups); assertEquals(BASE_QUERY + " AND (readGroup:(" + ACCESS_GROUP + ") OR adminGroup:(" + ACCESS_GROUP + "))", query.toString()); }
@Test public void addAccessAdminOnlyTest() { when(searchSettings.getAllowPatronAccess()).thenReturn(false); StringBuilder query = new StringBuilder(BASE_QUERY); restrictionUtil.add(query, accessGroups); assertEquals(BASE_QUERY + " AND adminGroup:(" + ACCESS_GROUP + ")", query.toString()); }