### Question:
StaffToStudentCohortProgramAssociationValidator extends AbstractContextValidator { @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { if (!areParametersValid(STUDENT_ASSOCIATIONS, entityType, ids)) { return Collections.EMPTY_SET; } Map<String, Set<String>> associations = new HashMap<String, Set<String>>(); NeutralQuery basicQuery = new NeutralQuery(new NeutralCriteria(ParameterConstants.ID, NeutralCriteria.CRITERIA_IN, ids)); Iterable<Entity> assocs = getRepo().findAll(entityType, basicQuery); for (Entity assoc : assocs) { String studentId = (String) assoc.getBody().get(ParameterConstants.STUDENT_ID); if (!isFieldExpired(assoc.getBody(), ParameterConstants.END_DATE, true)) { if (!associations.containsKey(studentId)) { associations.put(studentId, new HashSet<String>()); } associations.get(studentId).add(assoc.getEntityId()); } } Set<String> validStudents = studentValidator.validate(EntityNames.STUDENT, associations.keySet()); return getValidIds(validStudents, associations); }  @Override boolean canValidate(String entityType, boolean isTransitive); @Override Set<String> validate(String entityType, Set<String> ids); @Override SecurityUtil.UserContext getContext();  }### Answer:
@Test public void testCanValidateValidCohortAssociation() { Mockito.when(mockStudentValidator.validate(Mockito.eq(EntityNames.STUDENT), Mockito.any(Set.class))) .thenReturn(new HashSet<String>(Arrays.asList("Boop"))); for (int i = 0; i < 10; ++i) { cohortIds.add(helper.generateStudentCohort("Boop", "" + i, false).getEntityId()); } assertTrue(validator.validate(EntityNames.STUDENT_COHORT_ASSOCIATION, cohortIds).equals(cohortIds)); }
@Test public void testCanValidateValidProgramAssociation() { Mockito.when(mockStudentValidator.validate(Mockito.eq(EntityNames.STUDENT), Mockito.any(Set.class))) .thenReturn(new HashSet<String>(Arrays.asList("Boop"))); for (int i = 0; i < 10; ++i) { programIds.add(helper.generateStudentProgram("Boop", "" + i, false).getEntityId()); } assertTrue(validator.validate(EntityNames.STUDENT_PROGRAM_ASSOCIATION, programIds).equals(programIds)); }
@Test public void testCanNotValidExpiredAssociation() { Mockito.when(mockStudentValidator.validate(Mockito.eq(EntityNames.STUDENT), Mockito.any(Set.class))) .thenReturn(new HashSet<String>(Arrays.asList("Boop"))); cohortIds.add(helper.generateStudentCohort("Boop", "Beep", true).getEntityId()); assertFalse(validator.validate(EntityNames.STUDENT_COHORT_ASSOCIATION, cohortIds).equals(cohortIds)); }
@Test public void testCanNotValidateAssociationWithoutStudentAccess() { Mockito.when(mockStudentValidator.validate(Mockito.eq(EntityNames.STUDENT), Mockito.any(Set.class))) .thenReturn(new HashSet<String>(Arrays.asList("dummy"))); for (int i = 0; i < 10; ++i) { cohortIds.add(helper.generateStudentCohort("Boop", "" + i, false).getEntityId()); } assertFalse(validator.validate(EntityNames.STUDENT_COHORT_ASSOCIATION, cohortIds).equals(cohortIds)); }