@Test public void testCanGetAccessToCurrentStudentSchoolAssociation() throws Exception { Map<String, Object> goodStudentSchoolAssociation = buildStudentSchoolAssociation("student123", "school123", new DateTime().plusHours(1)); Entity association = new MongoEntity(EntityNames.STUDENT_SCHOOL_ASSOCIATION, goodStudentSchoolAssociation); Mockito.when( mockRepo.findAll(Mockito.eq(EntityNames.STUDENT_SCHOOL_ASSOCIATION), Mockito.any(NeutralQuery.class))) .thenReturn(Arrays.asList(association)); Set<String> associations = new HashSet<String>(); associations.add(association.getEntityId()); studentIds.add("student123"); Mockito.when(teacherToStudentValidator.validate(EntityNames.STUDENT, studentIds)).thenReturn(studentIds); Assert.assertEquals(studentIds.size(), validator.validate(EntityNames.STUDENT_SCHOOL_ASSOCIATION, associations).size()); }
@SuppressWarnings("unchecked") @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { if (!areParametersValid(SUB_ENTITIES_OF_STUDENT, entityType, ids)) { return Collections.emptySet(); } Map<String, Set<String>> students = new HashMap<String, Set<String>>(); NeutralQuery query = new NeutralQuery(new NeutralCriteria(ParameterConstants.ID, NeutralCriteria.CRITERIA_IN, new ArrayList<String>(ids))); Iterable<Entity> entities = repo.findAll(entityType, query); for (Entity entity : entities) { Map<String, Object> body = entity.getBody(); if (body.get(ParameterConstants.STUDENT_ID) instanceof String) { students = putStudents(Arrays.asList((String) body.get(ParameterConstants.STUDENT_ID)), entity.getEntityId(), students); } else if (body.get(ParameterConstants.STUDENT_ID) instanceof List) { students = putStudents((List<String>) body.get(ParameterConstants.STUDENT_ID), entity.getEntityId(), students); } else { LOG.warn("Possible Corrupt Data detected at "+entityType+"/"+entity.getEntityId()); } } if (students.isEmpty()) { return Collections.EMPTY_SET; } Set<String> validStudents = validator.validate(EntityNames.STUDENT, students.keySet()); return getValidIds(validStudents, students); }
TeacherToSubStudentEntityValidator extends AbstractContextValidator { @SuppressWarnings("unchecked") @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { if (!areParametersValid(SUB_ENTITIES_OF_STUDENT, entityType, ids)) { return Collections.emptySet(); } Map<String, Set<String>> students = new HashMap<String, Set<String>>(); NeutralQuery query = new NeutralQuery(new NeutralCriteria(ParameterConstants.ID, NeutralCriteria.CRITERIA_IN, new ArrayList<String>(ids))); Iterable<Entity> entities = repo.findAll(entityType, query); for (Entity entity : entities) { Map<String, Object> body = entity.getBody(); if (body.get(ParameterConstants.STUDENT_ID) instanceof String) { students = putStudents(Arrays.asList((String) body.get(ParameterConstants.STUDENT_ID)), entity.getEntityId(), students); } else if (body.get(ParameterConstants.STUDENT_ID) instanceof List) { students = putStudents((List<String>) body.get(ParameterConstants.STUDENT_ID), entity.getEntityId(), students); } else { LOG.warn("Possible Corrupt Data detected at "+entityType+"/"+entity.getEntityId()); } } if (students.isEmpty()) { return Collections.EMPTY_SET; } Set<String> validStudents = validator.validate(EntityNames.STUDENT, students.keySet()); return getValidIds(validStudents, students); } }
TeacherToSubStudentEntityValidator extends AbstractContextValidator { @SuppressWarnings("unchecked") @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { if (!areParametersValid(SUB_ENTITIES_OF_STUDENT, entityType, ids)) { return Collections.emptySet(); } Map<String, Set<String>> students = new HashMap<String, Set<String>>(); NeutralQuery query = new NeutralQuery(new NeutralCriteria(ParameterConstants.ID, NeutralCriteria.CRITERIA_IN, new ArrayList<String>(ids))); Iterable<Entity> entities = repo.findAll(entityType, query); for (Entity entity : entities) { Map<String, Object> body = entity.getBody(); if (body.get(ParameterConstants.STUDENT_ID) instanceof String) { students = putStudents(Arrays.asList((String) body.get(ParameterConstants.STUDENT_ID)), entity.getEntityId(), students); } else if (body.get(ParameterConstants.STUDENT_ID) instanceof List) { students = putStudents((List<String>) body.get(ParameterConstants.STUDENT_ID), entity.getEntityId(), students); } else { LOG.warn("Possible Corrupt Data detected at "+entityType+"/"+entity.getEntityId()); } } if (students.isEmpty()) { return Collections.EMPTY_SET; } Set<String> validStudents = validator.validate(EntityNames.STUDENT, students.keySet()); return getValidIds(validStudents, students); }  }
TeacherToSubStudentEntityValidator extends AbstractContextValidator { @SuppressWarnings("unchecked") @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { if (!areParametersValid(SUB_ENTITIES_OF_STUDENT, entityType, ids)) { return Collections.emptySet(); } Map<String, Set<String>> students = new HashMap<String, Set<String>>(); NeutralQuery query = new NeutralQuery(new NeutralCriteria(ParameterConstants.ID, NeutralCriteria.CRITERIA_IN, new ArrayList<String>(ids))); Iterable<Entity> entities = repo.findAll(entityType, query); for (Entity entity : entities) { Map<String, Object> body = entity.getBody(); if (body.get(ParameterConstants.STUDENT_ID) instanceof String) { students = putStudents(Arrays.asList((String) body.get(ParameterConstants.STUDENT_ID)), entity.getEntityId(), students); } else if (body.get(ParameterConstants.STUDENT_ID) instanceof List) { students = putStudents((List<String>) body.get(ParameterConstants.STUDENT_ID), entity.getEntityId(), students); } else { LOG.warn("Possible Corrupt Data detected at "+entityType+"/"+entity.getEntityId()); } } if (students.isEmpty()) { return Collections.EMPTY_SET; } Set<String> validStudents = validator.validate(EntityNames.STUDENT, students.keySet()); return getValidIds(validStudents, students); }  @Override boolean canValidate(String entityType, boolean through); @SuppressWarnings("unchecked") @Override Set<String> validate(String entityType, Set<String> ids); void setRepo(PagingRepositoryDelegate<Entity> repo); void setTeacherToStudentValidator(TeacherToStudentValidator teacherToStudentValidator); @Override SecurityUtil.UserContext getContext(); }
TeacherToSubStudentEntityValidator extends AbstractContextValidator { @SuppressWarnings("unchecked") @Override public Set<String> validate(String entityType, Set<String> ids) throws IllegalStateException { if (!areParametersValid(SUB_ENTITIES_OF_STUDENT, entityType, ids)) { return Collections.emptySet(); } Map<String, Set<String>> students = new HashMap<String, Set<String>>(); NeutralQuery query = new NeutralQuery(new NeutralCriteria(ParameterConstants.ID, NeutralCriteria.CRITERIA_IN, new ArrayList<String>(ids))); Iterable<Entity> entities = repo.findAll(entityType, query); for (Entity entity : entities) { Map<String, Object> body = entity.getBody(); if (body.get(ParameterConstants.STUDENT_ID) instanceof String) { students = putStudents(Arrays.asList((String) body.get(ParameterConstants.STUDENT_ID)), entity.getEntityId(), students); } else if (body.get(ParameterConstants.STUDENT_ID) instanceof List) { students = putStudents((List<String>) body.get(ParameterConstants.STUDENT_ID), entity.getEntityId(), students); } else { LOG.warn("Possible Corrupt Data detected at "+entityType+"/"+entity.getEntityId()); } } if (students.isEmpty()) { return Collections.EMPTY_SET; } Set<String> validStudents = validator.validate(EntityNames.STUDENT, students.keySet()); return getValidIds(validStudents, students); }  @Override boolean canValidate(String entityType, boolean through); @SuppressWarnings("unchecked") @Override Set<String> validate(String entityType, Set<String> ids); void setRepo(PagingRepositoryDelegate<Entity> repo); void setTeacherToStudentValidator(TeacherToStudentValidator teacherToStudentValidator); @Override SecurityUtil.UserContext getContext();  }