@Test public void shouldRemoveEmail() { OutsiderTeacher teacher = scenarioHelper.createOutsiderTeacher(); assertThat(teacher.getContact().getEmails()).isNotEmpty(); for (Email email : teacher.getContact().getEmails()) { outsiderTeacherManager.removeEmail(email.getId()); } assertThat(outsiderTeacherManager.findOneById(teacher.getId()).getContact().getEmails()) .isEmpty(); assertThat(emailRepository.findAll()).isEmpty(); }
@Test public void shouldAddEmail() { OutsiderTeacher teacher = scenarioHelper.createEmptyOutsiderTeacher(); Email email = EmailTest.createOne(scenarioHelper.createEmailType()); assertThat(outsiderTeacherManager.findOneById(teacher.getId()).getContact().getEmails()) .isEmpty(); Email saved = outsiderTeacherManager.addEmail(teacher.getId(), email); assertThat(emailRepository.findAll()).hasSize(1); assertThat(outsiderTeacherManager.findOneById(teacher.getId()).getContact().getEmails()) .hasSize(1); assertThat(saved.getId()).isNotNull(); }
public Email addEmail(Long teacherId, Email model) { if (teacherId == null) { throw new IllegalArgumentException( "Cannot find a " + Teacher.class.getName() + " with a null id."); } if (model == null) { throw new IllegalArgumentException("Cannot persist a null " + Email.class.getName()); } if (model.getId() != null) { throw new AlreadyDefinedInOnNonPersistedEntity( "Cannot persist a " + Email.class.getName() + " which already has an ID."); } model = emailManager.create(model); Teacher fetched = findOneById(teacherId); fetched.getContact().addEmail(model); teacherRepository.saveAndFlush(new TeacherDomain(fetched)); return model; }
@Test(expected = AlreadyDefinedInOnNonPersistedEntity.class) public void shouldFailAddEmailWithEmailHavingIdAlreadyDefined() { Email model = EmailTest.createOne(); model.setId(12L); outsiderTeacherManager.addEmail(12L, model); }