@Test public void testInsertNewStudent() { Student student = new Student(1234, "John Doe", LocalDate.of(2011, Month.JANUARY, 1)); try { Student insertedStudent = studentService.insertStudent(student); assertEquals(student, insertedStudent); } catch (ResourceAlreadyExistsException e) { e.printStackTrace(); } }
public Student insertStudent(Student student) throws ResourceAlreadyExistsException { Optional<Student> optionalStudent = studentRepository.findById(student.getId()); if (optionalStudent.isPresent()) { throw new ResourceAlreadyExistsException(student.getId() + ""); } else { return studentRepository.save(student); } }
StudentService { public Student insertStudent(Student student) throws ResourceAlreadyExistsException { Optional<Student> optionalStudent = studentRepository.findById(student.getId()); if (optionalStudent.isPresent()) { throw new ResourceAlreadyExistsException(student.getId() + ""); } else { return studentRepository.save(student); } } }
StudentService { public Student insertStudent(Student student) throws ResourceAlreadyExistsException { Optional<Student> optionalStudent = studentRepository.findById(student.getId()); if (optionalStudent.isPresent()) { throw new ResourceAlreadyExistsException(student.getId() + ""); } else { return studentRepository.save(student); } } StudentService(StudentRepository studentRepository); }
StudentService { public Student insertStudent(Student student) throws ResourceAlreadyExistsException { Optional<Student> optionalStudent = studentRepository.findById(student.getId()); if (optionalStudent.isPresent()) { throw new ResourceAlreadyExistsException(student.getId() + ""); } else { return studentRepository.save(student); } } StudentService(StudentRepository studentRepository); Student findById(long id); List<Student> findAll(); boolean deleteById(long id); Student insertStudent(Student student); Student updateStudent(long id, Student student); }
StudentService { public Student insertStudent(Student student) throws ResourceAlreadyExistsException { Optional<Student> optionalStudent = studentRepository.findById(student.getId()); if (optionalStudent.isPresent()) { throw new ResourceAlreadyExistsException(student.getId() + ""); } else { return studentRepository.save(student); } } StudentService(StudentRepository studentRepository); Student findById(long id); List<Student> findAll(); boolean deleteById(long id); Student insertStudent(Student student); Student updateStudent(long id, Student student);  }