@Test public void testApplyOptionalField() { List<EntityBody> entities = new ArrayList<EntityBody>(); entities.add(new EntityBody(createTestStudentEntity(STUDENT_ID))); entities = studentAssessmentOptionalFieldAppender.applyOptionalField(entities, null); assertEquals("Should be 1", 1, entities.size()); List<EntityBody> studentAssessments = (List<EntityBody>) entities.get(0).get("studentAssessments"); assertEquals("Should match", 2, studentAssessments.size()); assertEquals("Should match", STUDENT_ID, studentAssessments.get(0).get("studentId")); EntityBody body = (EntityBody) ((List<EntityBody>) entities.get(0).get("studentAssessments")).get(0); EntityBody assessment = (EntityBody) body.get("assessments"); assertNotNull("Should not be null", assessment); assertEquals("Should match", "Reading", assessment.get("academicSubject")); assertEquals("", assessment.get("id"), body.get("assessmentId")); }
@Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); List<EntityBody> studentAssessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_ASSESSMENTS, ParameterConstants.STUDENT_ID, studentIds); List<String> assessmentIds = optionalFieldAppenderHelper.getIdList(studentAssessments, ParameterConstants.ASSESSMENT_ID); List<EntityBody> assessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.ASSESSMENTS, "_id", assessmentIds); for (EntityBody student : entities) { List<EntityBody> studentAssessmentsForStudent = optionalFieldAppenderHelper.getEntitySubList(studentAssessments, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentAssessment : studentAssessmentsForStudent) { EntityBody assessment = optionalFieldAppenderHelper.getEntityFromList(assessments, "id", (String) studentAssessment.get(ParameterConstants.ASSESSMENT_ID)); studentAssessment.put(PathConstants.ASSESSMENTS, assessment); } student.put(PathConstants.STUDENT_ASSESSMENTS, studentAssessmentsForStudent); } return entities; }
StudentAssessmentOptionalFieldAppender implements OptionalFieldAppender { @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); List<EntityBody> studentAssessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_ASSESSMENTS, ParameterConstants.STUDENT_ID, studentIds); List<String> assessmentIds = optionalFieldAppenderHelper.getIdList(studentAssessments, ParameterConstants.ASSESSMENT_ID); List<EntityBody> assessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.ASSESSMENTS, "_id", assessmentIds); for (EntityBody student : entities) { List<EntityBody> studentAssessmentsForStudent = optionalFieldAppenderHelper.getEntitySubList(studentAssessments, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentAssessment : studentAssessmentsForStudent) { EntityBody assessment = optionalFieldAppenderHelper.getEntityFromList(assessments, "id", (String) studentAssessment.get(ParameterConstants.ASSESSMENT_ID)); studentAssessment.put(PathConstants.ASSESSMENTS, assessment); } student.put(PathConstants.STUDENT_ASSESSMENTS, studentAssessmentsForStudent); } return entities; } }
StudentAssessmentOptionalFieldAppender implements OptionalFieldAppender { @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); List<EntityBody> studentAssessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_ASSESSMENTS, ParameterConstants.STUDENT_ID, studentIds); List<String> assessmentIds = optionalFieldAppenderHelper.getIdList(studentAssessments, ParameterConstants.ASSESSMENT_ID); List<EntityBody> assessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.ASSESSMENTS, "_id", assessmentIds); for (EntityBody student : entities) { List<EntityBody> studentAssessmentsForStudent = optionalFieldAppenderHelper.getEntitySubList(studentAssessments, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentAssessment : studentAssessmentsForStudent) { EntityBody assessment = optionalFieldAppenderHelper.getEntityFromList(assessments, "id", (String) studentAssessment.get(ParameterConstants.ASSESSMENT_ID)); studentAssessment.put(PathConstants.ASSESSMENTS, assessment); } student.put(PathConstants.STUDENT_ASSESSMENTS, studentAssessmentsForStudent); } return entities; }  }
StudentAssessmentOptionalFieldAppender implements OptionalFieldAppender { @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); List<EntityBody> studentAssessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_ASSESSMENTS, ParameterConstants.STUDENT_ID, studentIds); List<String> assessmentIds = optionalFieldAppenderHelper.getIdList(studentAssessments, ParameterConstants.ASSESSMENT_ID); List<EntityBody> assessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.ASSESSMENTS, "_id", assessmentIds); for (EntityBody student : entities) { List<EntityBody> studentAssessmentsForStudent = optionalFieldAppenderHelper.getEntitySubList(studentAssessments, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentAssessment : studentAssessmentsForStudent) { EntityBody assessment = optionalFieldAppenderHelper.getEntityFromList(assessments, "id", (String) studentAssessment.get(ParameterConstants.ASSESSMENT_ID)); studentAssessment.put(PathConstants.ASSESSMENTS, assessment); } student.put(PathConstants.STUDENT_ASSESSMENTS, studentAssessmentsForStudent); } return entities; }  @Override List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters); }
StudentAssessmentOptionalFieldAppender implements OptionalFieldAppender { @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> studentIds = optionalFieldAppenderHelper.getIdList(entities, "id"); List<EntityBody> studentAssessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_ASSESSMENTS, ParameterConstants.STUDENT_ID, studentIds); List<String> assessmentIds = optionalFieldAppenderHelper.getIdList(studentAssessments, ParameterConstants.ASSESSMENT_ID); List<EntityBody> assessments = optionalFieldAppenderHelper.queryEntities(ResourceNames.ASSESSMENTS, "_id", assessmentIds); for (EntityBody student : entities) { List<EntityBody> studentAssessmentsForStudent = optionalFieldAppenderHelper.getEntitySubList(studentAssessments, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentAssessment : studentAssessmentsForStudent) { EntityBody assessment = optionalFieldAppenderHelper.getEntityFromList(assessments, "id", (String) studentAssessment.get(ParameterConstants.ASSESSMENT_ID)); studentAssessment.put(PathConstants.ASSESSMENTS, assessment); } student.put(PathConstants.STUDENT_ASSESSMENTS, studentAssessmentsForStudent); } return entities; }  @Override List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters);  }