### Question:
TeacherToStudentProgramAssociationValidator extends AbstractContextValidator { @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { if (!areParametersValid(EntityNames.STUDENT_PROGRAM_ASSOCIATION, entityType, ids)) { return Collections.EMPTY_SET; } NeutralQuery query = new NeutralQuery( new NeutralCriteria(ParameterConstants.ID, NeutralCriteria.CRITERIA_IN, ids)); query.setIncludeFields(Arrays.asList(ParameterConstants.PROGRAM_ID)); Map<String, Set<String>> programIdsToSPA = new HashMap<String, Set<String>>(); Iterable<Entity> spas = getRepo().findAll(EntityNames.STUDENT_PROGRAM_ASSOCIATION, query); for (Entity spa : spas) { String programId = (String) spa.getBody().get(ParameterConstants.PROGRAM_ID); if (!programIdsToSPA.containsKey(programId)) { programIdsToSPA.put(programId, new HashSet<String>()); } programIdsToSPA.get(programId).add(spa.getEntityId()); } String teacherId = SecurityUtil.getSLIPrincipal().getEntity().getEntityId(); NeutralQuery nq = new NeutralQuery(new NeutralCriteria(ParameterConstants.PROGRAM_ID, NeutralCriteria.CRITERIA_IN, programIdsToSPA.keySet())); nq.addCriteria(new NeutralCriteria(ParameterConstants.STAFF_ID, NeutralCriteria.OPERATOR_EQUAL, teacherId)); nq.addCriteria(new NeutralCriteria(ParameterConstants.STUDENT_RECORD_ACCESS, NeutralCriteria.OPERATOR_EQUAL, true)); Iterable<Entity> entities = getRepo().findAll(EntityNames.STAFF_PROGRAM_ASSOCIATION, nq); Set<String> validProgramIds = new HashSet<String>(); for (Entity entity : entities) { String expireDate = (String) entity.getBody().get(ParameterConstants.END_DATE); if (expireDate == null || isLhsBeforeRhs(getNowMinusGracePeriod(), getDateTime(expireDate))) { validProgramIds.add((String) entity.getBody().get(ParameterConstants.PROGRAM_ID)); } } return getValidIds(validProgramIds, programIdsToSPA); }  @Override boolean canValidate(String entityType, boolean isTransitive); @Override Set<String> validate(String entityType, Set<String> ids); @Override SecurityUtil.UserContext getContext();  }### Answer:
@Test public void testValidAccessTeacher() { Set<String> ids = new HashSet<String>(Arrays.asList(studentProgramAssoc1.getEntityId())); Assert.assertTrue(validator.validate(EntityNames.STUDENT_PROGRAM_ASSOCIATION, ids).equals(ids)); }
@Test public void testInvalidAccessTeacher1() { Set<String> ids = new HashSet<String>(Arrays.asList(studentProgramAssoc2.getEntityId())); Assert.assertFalse(validator.validate(EntityNames.STUDENT_PROGRAM_ASSOCIATION, ids).equals(ids)); ids = new HashSet<String>(Arrays.asList(studentProgramAssoc3.getEntityId())); Assert.assertFalse(validator.validate(EntityNames.STUDENT_PROGRAM_ASSOCIATION, ids).equals(ids)); ids = new HashSet<String>(Arrays.asList(studentProgramAssoc4.getEntityId())); Assert.assertFalse(validator.validate(EntityNames.STUDENT_PROGRAM_ASSOCIATION, ids).equals(ids)); ids = new HashSet<String>(Arrays.asList(studentProgramAssoc5.getEntityId())); Assert.assertFalse(validator.validate(EntityNames.STUDENT_PROGRAM_ASSOCIATION, ids).equals(ids)); ids = new HashSet<String>( Arrays.asList(studentProgramAssoc1.getEntityId(), studentProgramAssoc3.getEntityId())); Assert.assertFalse(validator.validate(EntityNames.STUDENT_PROGRAM_ASSOCIATION, ids).equals(ids)); }