Project Student: Business Layer
This is part of Project Student. Other posts are Webservice Client With Jersey, Webservice Server with Jersey and Persistence with Spring Data.
The third layer of the RESTful webapp onion is the business layer. This is the guts of the application – well-written persistence and webservice layers are constrained but anything goes in the business layer.
We’re only implementing CRUD methods at this point so these methods are straightforward.
Design Decisions
Spring Data – we will be using Spring Data for the persistence layer so we will want to specify the persistence layer interface with that in mind. As we’ll see in the next blog this greatly simplifies our life.
Limitations
DataAccessException – no attempt is made to throw different exceptions according to the type of DataAccessException we receive. This will be important later – we should treat a constraint violation differently than a lost database connection.
Service Interface
First a reminder of the service interface we identified in the last post.
01 02 03 04 05 06 07 08 09 10 11 12 13 | public interface CourseService { List<Course> findAllCourses(); Course findCourseById(Integer id); Course findCourseByUuid(String uuid); Course createCourse(String name); Course updateCourse(Course course, String name); void deleteCourse(String uuid); } |
Service Implementation
The service implementation goes quickly since it’s basic CRUD operations. Our only concerns are if there’s a problem with the database (DataAccessException in Spring) or if an expected value can’t be found. We need to add a new exception to our API for the former, already have an exception in the API for the latter.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | public class CourseServiceImpl implements CourseService { private static final Logger log = LoggerFactory .getLogger(CourseServiceImpl. class ); @Resource private CourseRepository courseRepository; /** * Default constructor */ public CourseServiceImpl() { } /** * Constructor used in unit tests */ CourseServiceImpl(CourseRepository courseRepository) { this .courseRepository = courseRepository; } /** * @see com.invariantproperties.sandbox.student.business.CourseService# * findAllCourses() */ @Transactional (readOnly = true ) @Override public List<Course> findAllCourses() { List<Course> courses = null ; try { courses = courseRepository.findAll(); } catch (DataAccessException e) { if (!(e instanceof UnitTestException)) { log.info( "error loading list of courses: " + e.getMessage(), e); } throw new PersistenceException( "unable to get list of courses." , e); } return courses; } /** * @see com.invariantproperties.sandbox.student.business.CourseService# * findCourseById(java.lang.Integer) */ @Transactional (readOnly = true ) @Override public Course findCourseById(Integer id) { Course course = null ; try { course = courseRepository.findOne(id); } catch (DataAccessException e) { if (!(e instanceof UnitTestException)) { log.info( "internal error retrieving course: " + id, e); } throw new PersistenceException( "unable to find course by id" , e, id); } if (course == null ) { throw new ObjectNotFoundException(id); } return course; } /** * @see com.invariantproperties.sandbox.student.business.CourseService# * findCourseByUuid(java.lang.String) */ @Transactional (readOnly = true ) @Override public Course findCourseByUuid(String uuid) { Course course = null ; try { course = courseRepository.findCourseByUuid(uuid); } catch (DataAccessException e) { if (!(e instanceof UnitTestException)) { log.info( "internal error retrieving course: " + uuid, e); } throw new PersistenceException( "unable to find course by uuid" , e, uuid); } if (course == null ) { throw new ObjectNotFoundException(uuid); } return course; } /** * @see com.invariantproperties.sandbox.student.business.CourseService# * createCourse(java.lang.String) */ @Transactional @Override public Course createCourse(String name) { final Course course = new Course(); course.setName(name); Course actual = null ; try { actual = courseRepository.saveAndFlush(course); } catch (DataAccessException e) { if (!(e instanceof UnitTestException)) { log.info( "internal error retrieving course: " + name, e); } throw new PersistenceException( "unable to create course" , e); } return actual; } /** * @see com.invariantproperties.sandbox.course.persistence.CourseService# * updateCourse(com.invariantproperties.sandbox.course.domain.Course, * java.lang.String) */ @Transactional @Override public Course updateCourse(Course course, String name) { Course updated = null ; try { final Course actual = courseRepository.findCourseByUuid(course .getUuid()); if (actual == null ) { log.debug( "did not find course: " + course.getUuid()); throw new ObjectNotFoundException(course.getUuid()); } actual.setName(name); updated = courseRepository.saveAndFlush(actual); course.setName(name); } catch (DataAccessException e) { if (!(e instanceof UnitTestException)) { log.info( "internal error deleting course: " + course.getUuid(), e); } throw new PersistenceException( "unable to delete course" , e, course.getUuid()); } return updated; } /** * @see com.invariantproperties.sandbox.student.business.CourseService# * deleteCourse(java.lang.String) */ @Transactional @Override public void deleteCourse(String uuid) { Course course = null ; try { course = courseRepository.findCourseByUuid(uuid); if (course == null ) { log.debug( "did not find course: " + uuid); throw new ObjectNotFoundException(uuid); } courseRepository.delete(course); } catch (DataAccessException e) { if (!(e instanceof UnitTestException)) { log.info( "internal error deleting course: " + uuid, e); } throw new PersistenceException( "unable to delete course" , e, uuid); } } } |
This implementation tells us the required interface for the persistence layer.
Persistence Layer Interface
We will be using Spring Data for our persistence layer and our DAO interface is the same as a Spring Data repository. We only need one nonstandard method.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | public class CourseServiceImplTest { @Test public void testFindAllCourses() { final List<Course> expected = Collections.emptyList(); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findAll()).thenReturn(expected); final CourseService service = new CourseServiceImpl(repository); final List<Course> actual = service.findAllCourses(); assertEquals(expected, actual); } @Test (expected = PersistenceException. class ) public void testFindAllCoursesError() { final List<Course> expected = Collections.emptyList(); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findAll()).thenThrow( new UnitTestException()); final CourseService service = new CourseServiceImpl(repository); final List<Course> actual = service.findAllCourses(); assertEquals(expected, actual); } @Test public void testFindCourseById() { final Course expected = new Course(); expected.setId( 1 ); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findOne(any(Integer. class ))).thenReturn(expected); final CourseService service = new CourseServiceImpl(repository); final Course actual = service.findCourseById(expected.getId()); assertEquals(expected, actual); } @Test (expected = ObjectNotFoundException. class ) public void testFindCourseByIdMissing() { final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findOne(any(Integer. class ))).thenReturn( null ); final CourseService service = new CourseServiceImpl(repository); service.findCourseById( 1 ); } @Test (expected = PersistenceException. class ) public void testFindCourseByIdError() { final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findOne(any(Integer. class ))).thenThrow( new UnitTestException()); final CourseService service = new CourseServiceImpl(repository); service.findCourseById( 1 ); } @Test public void testFindCourseByUuid() { final Course expected = new Course(); expected.setUuid( "[uuid]" ); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenReturn( expected); final CourseService service = new CourseServiceImpl(repository); final Course actual = service.findCourseByUuid(expected.getUuid()); assertEquals(expected, actual); } @Test (expected = ObjectNotFoundException. class ) public void testFindCourseByUuidMissing() { final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenReturn( null ); final CourseService service = new CourseServiceImpl(repository); service.findCourseByUuid( "[uuid]" ); } @Test (expected = PersistenceException. class ) public void testFindCourseByUuidError() { final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenThrow( new UnitTestException()); final CourseService service = new CourseServiceImpl(repository); service.findCourseByUuid( "[uuid]" ); } @Test public void testCreateCourse() { final Course expected = new Course(); expected.setName( "name" ); expected.setUuid( "[uuid]" ); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.saveAndFlush(any(Course. class ))).thenReturn(expected); final CourseService service = new CourseServiceImpl(repository); final Course actual = service.createCourse(expected.getName()); assertEquals(expected, actual); } @Test (expected = PersistenceException. class ) public void testCreateCourseError() { final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.saveAndFlush(any(Course. class ))).thenThrow( new UnitTestException()); final CourseService service = new CourseServiceImpl(repository); service.createCourse( "name" ); } @Test public void testUpdateCourse() { final Course expected = new Course(); expected.setName( "Alice" ); expected.setUuid( "[uuid]" ); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenReturn( expected); when(repository.saveAndFlush(any(Course. class ))).thenReturn(expected); final CourseService service = new CourseServiceImpl(repository); final Course actual = service.updateCourse(expected, "Bob" ); assertEquals( "Bob" , actual.getName()); } @Test (expected = ObjectNotFoundException. class ) public void testUpdateCourseMissing() { final Course expected = new Course(); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenReturn( null ); final CourseService service = new CourseServiceImpl(repository); service.updateCourse(expected, "Bob" ); } @Test (expected = PersistenceException. class ) public void testUpdateCourseError() { final Course expected = new Course(); expected.setUuid( "[uuid]" ); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenReturn( expected); doThrow( new UnitTestException()).when(repository).saveAndFlush( any(Course. class )); final CourseService service = new CourseServiceImpl(repository); service.updateCourse(expected, "Bob" ); } @Test public void testDeleteCourse() { final Course expected = new Course(); expected.setUuid( "[uuid]" ); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenReturn( expected); doNothing().when(repository).delete(any(Course. class )); final CourseService service = new CourseServiceImpl(repository); service.deleteCourse(expected.getUuid()); } @Test (expected = ObjectNotFoundException. class ) public void testDeleteCourseMissing() { final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenReturn( null ); final CourseService service = new CourseServiceImpl(repository); service.deleteCourse( "[uuid]" ); } @Test (expected = PersistenceException. class ) public void testDeleteCourseError() { final Course expected = new Course(); expected.setUuid( "[uuid]" ); final CourseRepository repository = Mockito .mock(CourseRepository. class ); when(repository.findCourseByUuid(any(String. class ))).thenReturn( expected); doThrow( new UnitTestException()).when(repository).delete( any(Course. class )); final CourseService service = new CourseServiceImpl(repository); service.deleteCourse(expected.getUuid()); } } |
Integration Testing
Integration testing has been deferred until the persistence layer is implemented.
Source Code
- The source code is available at http://code.google.com/p/invariant-properties-blog/source/browse/student/student-business.