### Question:
TeacherToStaffCohortAssociationValidator extends AbstractContextValidator { @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { if (!areParametersValid(EntityNames.STAFF_COHORT_ASSOCIATION, entityType, ids)) { return Collections.EMPTY_SET; } NeutralQuery nq = new NeutralQuery(new NeutralCriteria(ParameterConstants.STAFF_ID, NeutralCriteria.OPERATOR_EQUAL, SecurityUtil.getSLIPrincipal().getEntity().getEntityId())); nq.addCriteria(new NeutralCriteria(ParameterConstants.ID, NeutralCriteria.CRITERIA_IN, ids)); Iterable<String> result = getRepo().findAllIds(EntityNames.STAFF_COHORT_ASSOCIATION, nq); return Sets.newHashSet(result); }  @Override boolean canValidate(String entityType, boolean isTransitive); @Override Set<String> validate(String entityType, Set<String> ids); @Override SecurityUtil.UserContext getContext();  }### Answer:
@Test(expected = IllegalArgumentException.class) public void testValidateWrongType() { val.validate(EntityNames.ASSESSMENT, new HashSet<String>(Arrays.asList("Jomolungma"))); }
@Test public void testCanAccessAll() { List<String> ids = Arrays.asList("Just Cause", "Armaggedon", "Awareness"); Set<String> sca = new HashSet<String>(); for (String id : ids) { sca.add(this.vth.generateStaffCohort(USER_ID, id, false, true).getEntityId()); } Assert.assertTrue(val.validate(EntityNames.STAFF_COHORT_ASSOCIATION, sca).equals(sca)); }
@Test public void testCannotAccessAll() { List<String> ids = Arrays.asList("Just Cause", "Armaggedon", "Awareness"); Set<String> sca = new HashSet<String>(); for (String id : ids) { sca.add(this.vth.generateStaffCohort("Sky Drake", id, false, true).getEntityId()); } Assert.assertFalse(val.validate(EntityNames.STAFF_COHORT_ASSOCIATION, sca).equals(sca)); for (String id : sca) { Set<String> single = Collections.singleton(id); Assert.assertFalse(val.validate(EntityNames.STAFF_COHORT_ASSOCIATION, single).equals(single)); } }
@Test public void testHeterogeneousList() { List<String> ids = Arrays.asList("Just Cause", "Armaggedon", "Awareness", "Chaos Mastery", "Life Mastery", "Death and Decay", "Node Mastery", "Artificer", "Warlord", "Conjurer"); Set<String> sca = new HashSet<String>(); List<String> successes = new ArrayList<String>(); for (String id : ids) { if (Math.random() > 0.33) { sca.add(this.vth.generateStaffCohort("Sky Drake", id, false, true).getEntityId()); } else if (Math.random() > 0.5) { String id2 = this.vth.generateStaffCohort(USER_ID, id, false, true).getEntityId(); sca.add(id2); successes.add(id2); } else { sca.add("Earth Elemental"); } } Assert.assertFalse(val.validate(EntityNames.STAFF_COHORT_ASSOCIATION, sca).equals(sca)); for (String id : sca) { Set<String> single = Collections.singleton(id); if (successes.contains(id)) { Assert.assertTrue(val.validate(EntityNames.STAFF_COHORT_ASSOCIATION, single).equals(single)); } else { Assert.assertFalse(val.validate(EntityNames.STAFF_COHORT_ASSOCIATION, single).equals(single)); } } }