### Question:
TeacherToCohortValidator extends AbstractContextValidator { @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { Set<String> validIds = new HashSet<String>(); if (!areParametersValid(EntityNames.COHORT, entityType, ids)) { return validIds; } NeutralQuery nq = new NeutralQuery(new NeutralCriteria(ParameterConstants.STAFF_ID, NeutralCriteria.OPERATOR_EQUAL, SecurityUtil .getSLIPrincipal().getEntity().getEntityId())); nq.addCriteria(new NeutralCriteria(ParameterConstants.COHORT_ID, NeutralCriteria.CRITERIA_IN, ids)); Iterable<Entity> entities = getRepo().findAll(EntityNames.STAFF_COHORT_ASSOCIATION, nq); for (Entity entity : entities) { validIds.add((String) entity.getBody().get(ParameterConstants.COHORT_ID)); } validIds.retainAll(ids); return validIds; }  @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> descs = Arrays.asList("Just Cause", "Armaggedon", "Awareness"); Set<String> cohortIds = new HashSet<String>(); for (String desc : descs) { cohortIds.add(this.generateCohortAndAssociate(USER_ID, desc)); } Assert.assertTrue(!val.validate(EntityNames.COHORT, cohortIds).isEmpty()); }
@Test public void testCannotAccessAll() { List<String> descs = Arrays.asList("Just Cause", "Armaggedon", "Awareness"); Set<String> cohortIds = new HashSet<String>(); for (String desc : descs) { cohortIds.add(this.generateCohort(USER_ID, desc)); } Assert.assertFalse(val.validate(EntityNames.COHORT, cohortIds).size() == cohortIds.size()); for (String id : cohortIds) { Assert.assertFalse(val.validate(EntityNames.COHORT, Collections.singleton(id)).size() == 1); } }
@Test public void testHeterogeneousList() { List<String> descs = Arrays.asList("Just Cause", "Armageddon", "Awareness", "Chaos Mastery", "Life Mastery", "Death and Decay", "Node Mastery", "Artificer", "Warlord", "Conjurer"); Set<String> cohortIds = new HashSet<String>(); List<String> successes = new ArrayList<String>(); cohortIds.add(this.generateCohort(USER_ID, "Artifacts Home World")); String idPerm = this.generateCohortAndAssociate(USER_ID, "Large Home World"); cohortIds.add(idPerm); successes.add(idPerm); for (String desc : descs) { if(Math.random()>0.5) { cohortIds.add(this.generateCohort(USER_ID, desc)); } else { String id = this.generateCohortAndAssociate(USER_ID, desc); cohortIds.add(id); successes.add(id); } } Assert.assertFalse(val.validate(EntityNames.COHORT, cohortIds).containsAll(cohortIds)); for (String id : cohortIds) { if(successes.contains(id)) { Assert.assertEquals(val.validate(EntityNames.COHORT, Collections.singleton(id)).size(), 1); } else { Assert.assertFalse(!val.validate(EntityNames.COHORT, Collections.singleton(id)).isEmpty()); } } }