@Test public void testApplyOptionalField() { List<EntityBody> entities = new ArrayList<EntityBody>(); entities.add(new EntityBody(createTestStudentEntityWithSectionAssociation(STUDENT_ID, SECTION_ID))); entities = studentGradebookOptionalFieldAppender.applyOptionalField(entities, null); assertEquals("Should be 1", 1, entities.size()); List<EntityBody> studentGradebookAssociations = (List<EntityBody>) entities.get(0).get("studentGradebookEntries"); assertEquals("Should match", 2, studentGradebookAssociations.size()); assertEquals("Should match", STUDENT_ID, studentGradebookAssociations.get(0).get("studentId")); assertEquals("Should match", SECTION_ID, studentGradebookAssociations.get(0).get("sectionId")); EntityBody body = (EntityBody) ((List<EntityBody>) entities.get(0).get("studentGradebookEntries")).get(0); EntityBody gradebookEntry = (EntityBody) body.get("gradebookEntries"); assertNotNull("Should not be null", gradebookEntry); assertEquals("Should match", "Unit Tests", gradebookEntry.get("gradebookEntryType")); assertEquals("", gradebookEntry.get("id"), body.get("gradebookEntryId")); }
@Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> sectionIds = new ArrayList<String>(optionalFieldAppenderHelper.getSectionIds(entities)); List<EntityBody> studentGradebookEntries; if (sectionIds.size() != 0) { studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.SECTION_ID, sectionIds); } else { List<String> studentIds = new ArrayList<String>(optionalFieldAppenderHelper.getIdList(entities, "id")); studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.STUDENT_ID, studentIds); } List<String> gradebookEntryIds = optionalFieldAppenderHelper.getIdList(studentGradebookEntries, ParameterConstants.GRADEBOOK_ENTRY_ID); List<EntityBody> gradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.GRADEBOOK_ENTRIES, "_id", gradebookEntryIds); for (EntityBody student : entities) { List<EntityBody> studentGradebookEntriesForStudent = optionalFieldAppenderHelper.getEntitySubList(studentGradebookEntries, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentGradebookEntry : studentGradebookEntriesForStudent) { EntityBody gradebookEntry = optionalFieldAppenderHelper.getEntityFromList(gradebookEntries, "id", (String) studentGradebookEntry.get(ParameterConstants.GRADEBOOK_ENTRY_ID)); if (gradebookEntry != null) { studentGradebookEntry.put(PathConstants.GRADEBOOK_ENTRIES, gradebookEntry); } } student.put(PathConstants.STUDENT_GRADEBOOK_ENTRIES, studentGradebookEntriesForStudent); } return entities; }
StudentGradebookOptionalFieldAppender implements OptionalFieldAppender { @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> sectionIds = new ArrayList<String>(optionalFieldAppenderHelper.getSectionIds(entities)); List<EntityBody> studentGradebookEntries; if (sectionIds.size() != 0) { studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.SECTION_ID, sectionIds); } else { List<String> studentIds = new ArrayList<String>(optionalFieldAppenderHelper.getIdList(entities, "id")); studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.STUDENT_ID, studentIds); } List<String> gradebookEntryIds = optionalFieldAppenderHelper.getIdList(studentGradebookEntries, ParameterConstants.GRADEBOOK_ENTRY_ID); List<EntityBody> gradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.GRADEBOOK_ENTRIES, "_id", gradebookEntryIds); for (EntityBody student : entities) { List<EntityBody> studentGradebookEntriesForStudent = optionalFieldAppenderHelper.getEntitySubList(studentGradebookEntries, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentGradebookEntry : studentGradebookEntriesForStudent) { EntityBody gradebookEntry = optionalFieldAppenderHelper.getEntityFromList(gradebookEntries, "id", (String) studentGradebookEntry.get(ParameterConstants.GRADEBOOK_ENTRY_ID)); if (gradebookEntry != null) { studentGradebookEntry.put(PathConstants.GRADEBOOK_ENTRIES, gradebookEntry); } } student.put(PathConstants.STUDENT_GRADEBOOK_ENTRIES, studentGradebookEntriesForStudent); } return entities; } }
StudentGradebookOptionalFieldAppender implements OptionalFieldAppender { @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> sectionIds = new ArrayList<String>(optionalFieldAppenderHelper.getSectionIds(entities)); List<EntityBody> studentGradebookEntries; if (sectionIds.size() != 0) { studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.SECTION_ID, sectionIds); } else { List<String> studentIds = new ArrayList<String>(optionalFieldAppenderHelper.getIdList(entities, "id")); studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.STUDENT_ID, studentIds); } List<String> gradebookEntryIds = optionalFieldAppenderHelper.getIdList(studentGradebookEntries, ParameterConstants.GRADEBOOK_ENTRY_ID); List<EntityBody> gradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.GRADEBOOK_ENTRIES, "_id", gradebookEntryIds); for (EntityBody student : entities) { List<EntityBody> studentGradebookEntriesForStudent = optionalFieldAppenderHelper.getEntitySubList(studentGradebookEntries, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentGradebookEntry : studentGradebookEntriesForStudent) { EntityBody gradebookEntry = optionalFieldAppenderHelper.getEntityFromList(gradebookEntries, "id", (String) studentGradebookEntry.get(ParameterConstants.GRADEBOOK_ENTRY_ID)); if (gradebookEntry != null) { studentGradebookEntry.put(PathConstants.GRADEBOOK_ENTRIES, gradebookEntry); } } student.put(PathConstants.STUDENT_GRADEBOOK_ENTRIES, studentGradebookEntriesForStudent); } return entities; }  }
StudentGradebookOptionalFieldAppender implements OptionalFieldAppender { @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> sectionIds = new ArrayList<String>(optionalFieldAppenderHelper.getSectionIds(entities)); List<EntityBody> studentGradebookEntries; if (sectionIds.size() != 0) { studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.SECTION_ID, sectionIds); } else { List<String> studentIds = new ArrayList<String>(optionalFieldAppenderHelper.getIdList(entities, "id")); studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.STUDENT_ID, studentIds); } List<String> gradebookEntryIds = optionalFieldAppenderHelper.getIdList(studentGradebookEntries, ParameterConstants.GRADEBOOK_ENTRY_ID); List<EntityBody> gradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.GRADEBOOK_ENTRIES, "_id", gradebookEntryIds); for (EntityBody student : entities) { List<EntityBody> studentGradebookEntriesForStudent = optionalFieldAppenderHelper.getEntitySubList(studentGradebookEntries, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentGradebookEntry : studentGradebookEntriesForStudent) { EntityBody gradebookEntry = optionalFieldAppenderHelper.getEntityFromList(gradebookEntries, "id", (String) studentGradebookEntry.get(ParameterConstants.GRADEBOOK_ENTRY_ID)); if (gradebookEntry != null) { studentGradebookEntry.put(PathConstants.GRADEBOOK_ENTRIES, gradebookEntry); } } student.put(PathConstants.STUDENT_GRADEBOOK_ENTRIES, studentGradebookEntriesForStudent); } return entities; }  @Override List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters); }
StudentGradebookOptionalFieldAppender implements OptionalFieldAppender { @Override public List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters) { List<String> sectionIds = new ArrayList<String>(optionalFieldAppenderHelper.getSectionIds(entities)); List<EntityBody> studentGradebookEntries; if (sectionIds.size() != 0) { studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.SECTION_ID, sectionIds); } else { List<String> studentIds = new ArrayList<String>(optionalFieldAppenderHelper.getIdList(entities, "id")); studentGradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.STUDENT_GRADEBOOK_ENTRIES, ParameterConstants.STUDENT_ID, studentIds); } List<String> gradebookEntryIds = optionalFieldAppenderHelper.getIdList(studentGradebookEntries, ParameterConstants.GRADEBOOK_ENTRY_ID); List<EntityBody> gradebookEntries = optionalFieldAppenderHelper.queryEntities(ResourceNames.GRADEBOOK_ENTRIES, "_id", gradebookEntryIds); for (EntityBody student : entities) { List<EntityBody> studentGradebookEntriesForStudent = optionalFieldAppenderHelper.getEntitySubList(studentGradebookEntries, ParameterConstants.STUDENT_ID, (String) student.get("id")); for (EntityBody studentGradebookEntry : studentGradebookEntriesForStudent) { EntityBody gradebookEntry = optionalFieldAppenderHelper.getEntityFromList(gradebookEntries, "id", (String) studentGradebookEntry.get(ParameterConstants.GRADEBOOK_ENTRY_ID)); if (gradebookEntry != null) { studentGradebookEntry.put(PathConstants.GRADEBOOK_ENTRIES, gradebookEntry); } } student.put(PathConstants.STUDENT_GRADEBOOK_ENTRIES, studentGradebookEntriesForStudent); } return entities; }  @Override List<EntityBody> applyOptionalField(List<EntityBody> entities, String parameters);  }