编程填空:学生信息处理程序_项目学生:业务层

编程填空:学生信息处理程序

这是Project Student的一部分。 其他职位包括带有Jersey的Webservice Client,带有Jersey的 Webservice Server和带有Spring Data的Persistence 。

RESTful Webapp洋葱的第三层是业务层。 这就是应用程序的精髓所在–编写良好的持久性和Web服务层受到约束,但是任何事情都在业务层中进行。

此时我们仅实现CRUD方法,因此这些方法非常简单。

设计决策

Spring Data –我们将使用Spring Data作为持久层,因此我们需要牢记指定持久层接口。 正如我们将在下一个博客中看到的那样,这大大简化了我们的生活。

局限性

DataAccessException –不会尝试根据我们收到的DataAccessException的类型抛出不同的异常。 稍后这将很重要-我们应该以不同于丢失数据库连接的方式对待约束违反。

服务介面

首先提醒我们在上一篇文章中确定的服务接口。

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);
}

服务实施

由于服务是基本的CRUD操作,因此该服务的实施速度很快。 我们唯一关心的是数据库是否存在问题(Spring中的DataAccessException)或找不到期望值。 我们需要在前者的API中添加新的例外,而后者的API中已经有例外。

public class CourseServiceImpl implements CourseService {private static final Logger log = LoggerFactory.getLogger(CourseServiceImpl.class);@Resourceprivate 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)@Overridepublic 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)@Overridepublic 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)@Overridepublic 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@Overridepublic 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@Overridepublic 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@Overridepublic 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);}}
}

该实现告诉我们持久层所需的接口。

持久层接口

我们将在持久层中使用Spring Data,并且DAO接口与Spring Data存储库相同。 我们只需要一种非标准方法。

public class CourseServiceImplTest {@Testpublic 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);}@Testpublic 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);}@Testpublic 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]");}@Testpublic 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");}@Testpublic 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");}@Testpublic 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());}
}

整合测试

集成测试已推迟到实现持久层。

源代码

  • 可从http://code.google.com/p/invariant-properties-blog/source/browse/student/student-business获得源代码。

参考: 项目学生:来自Invariant Properties博客的JCG合作伙伴 Bear Giles的业务层 。

翻译自: https://www.javacodegeeks.com/2014/01/project-student-business-layer.html

编程填空:学生信息处理程序

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/345595.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

markov chain, MRP MDP

在强化学习中&#xff0c;马尔科夫决策过程&#xff08;Markov decision process, MDP&#xff09;是对完全可观测的环境进行描述的&#xff0c;也就是说观测到的状态内容完整地决定了决策的需要的特征。几乎所有的强化学习问题都可以转化为MDP。本讲是理解强化学习问题的理论基…

【渝粤题库】国家开放大学2021春2717家畜解剖基础题目

试卷代号&#xff1a;2717 2021年春季学期期末统一考试 家畜解剖基础 试题 2021年7月 一、单项选择题&#xff08;每小题3分&#xff0c;共15分&#xff09; 1.与动物站立的地面平行且与动物体长轴平行的切面&#xff0c;称为( )。 A.矢状面 B.横断面 C.额面 D.纵切面 2.下列组…

【渝粤题库】国家开放大学2021春2757宠物饲养题目

试卷代号&#xff1a;2757 2021年春季学期期末统一考试 宠物饲养 试题 2021年7月 一、单项选择题&#xff08;每小题3分&#xff0c;共15分&#xff09; 1.下列选项中的描述不属于母猫发情表现的是( )。 A.外出游荡、大声叫唤 B.食欲旺盛 C.接受公猫交配 D.阴门红肿、湿润、有时…

如何在Java 8中使用filter()方法

Java 8 Stream接口引入了filter()方法&#xff0c;该方法可用于根据特定条件从对象集合中过滤掉某些元素。 应将此条件指定为filter()方法接受为参数的谓词 。 java.util.function.Predicate接口定义了一个名为test()的抽象方法&#xff0c;该方法接受通用类型T的对象并返回一…

【渝粤题库】国家开放大学2021春3620矿井火灾防治题目

试卷代号&#xff1a;3620 2021年春季学期期末统一考试 矿井火灾防治 试题 2021年7月 一、选择题&#xff08;本题共10题&#xff0c;每题3分&#xff0c;共30分&#xff0c;以下各题每题只有一个正确答案&#xff0c;将正确答案的代号填入题中的括号内&#xff09; 1.以下不是…

(网络)流和会话

流:指具有相同五元组(源IP,源端口,目的IP,目的端口,协议)的所有包 会话:指由双向流组成的所有包(源和目的互换)

使用Spring Data R2DBC进行异步RDBMS访问

不久之前&#xff0c;发布了JDBC驱动程序的反应型。 称为R2DBC。 它允许将数据异步流传输到已预订的任何端点。 通过将R2DBC之类的反应性驱动程序与Spring WebFlux结合使用&#xff0c;可以编写一个完整的应用程序&#xff0c;以异步方式处理数据的接收和发送。 在本文中&#…

【渝粤题库】国家开放大学2021春3935理工英语2题目

试卷代号&#xff1a;3935 2021年春季学期期末统一考试 理工英语2 试题 2021年7月注 意 事 项 一、将你的学号、姓名及分校&#xff08;工作站&#xff09;名称填写雀答题纸的规定栏内。考试结束后&#xff0c;把试卷和答题纸放在桌上。试卷和答题纸均不得带出考场。监考人…

Filtration, σ-algebras

1. Filtration filtration在钱敏平老师和龚光鲁老师的《随机过程论》中直接称其为非降的KaTeX parse error: Undefined control sequence: \sigmma at position 1: \̲s̲i̲g̲m̲m̲a̲代数族。如图。 一般叫σ\sigmaσ-代数流或σ\sigmaσ-域流 在鞅论中的花体FtF_tFt​&…

【渝粤题库】国家开放大学2021春4988电子政务概论题目

试卷代号&#xff1a;4988 2021年春季学期期末统一考试 电子政务概论 试题&#xff08;开卷&#xff09; 2021年7月 注意事项 一、将你的学号、姓名及分校&#xff08;工作站&#xff09;名称填写在答题纸的规定栏内。考试 结束后&#xff0c;把试卷和答题纸放在桌上。试卷和答…

gradle 命令行_Gradle命令行便利

gradle 命令行在我的《用Gradle构建Java的gradle tasks 》一文中&#xff0c;我简要提到了使用Gradle的“ gradle tasks ”命令来查看特定Gradle构建的可用任务。 在这篇文章中&#xff0c;我将对这一简短提及进行更多的扩展&#xff0c;并查看一些相关的Gradle命令行便利。 Gr…

【渝粤题库】广东开放大学 java web开发技术 形成性考核

题库查询系统 选择题 题目&#xff1a;当多个用户请求同一个JSP页面时&#xff0c;Tomcat服务器为每个客户启动一个_____。 题目&#xff1a;以下_____不是JSP运行所必须的条件。 题目&#xff1a;Tomcat服务器的默认端口为_____。 题目&#xff1a;如果想在tomcat服务器启动时…

【渝粤题库】广东开放大学 实用文写作 形成性考核

选择题 题目&#xff1a; 《关于厂进口SD6型自动车床的请示》&#xff0c;作者应是&#xff1a;( ) 选择一项&#xff1a; 题目&#xff1a; 请示可以一文多事。 选择一项&#xff1a; 题目&#xff1a; 除批转法规性文件外&#xff0c;通知的标题中一般不含书名号。&#x…

怎样更好地理解并记忆泰勒展开式

本段的核心思想是仿造。当我们想要仿造一个东西的时候&#xff0c;无形之中都会按照上文提到的思路&#xff0c;即先保证大体上相似&#xff0c;再保证局部相似&#xff0c;再保证细节相似&#xff0c;再保证更细微的地方相似……不断地细化下去&#xff0c;无穷次细化以后&…

【渝粤题库】广东开放大学 标准法律法规 形成性考核

​&#x1f449;关注我,看答案&#x1f448; 选择题 题目&#xff1a;狭义的立法程序仅指国家最高( )在制定、修改、补充、废止规范性文件过程中的工作方法、步骤和次序。 题目&#xff1a;行政法规是国务院制定、修改的有关&#xff08; &#xff09;和管理行政事务事项的规范…

新的DMN编辑器预览

Workbench 7.13.0.Final于10月16日星期二发布&#xff0c;此版本带来了许多有趣的功能和重要的修复程序。 亮点之一是作为技术预览功能的新DMN编辑器&#xff0c;该功能仍在开发中&#xff0c;但您可以开始使用。 在本文中&#xff0c;您将学习如何启用DMN编辑器预览&#xff…

【渝粤题库】广东开放大学 面向对象方法精粹 形成性考核

选择题 题目&#xff1a; 单选 可行性分析研究的主要目的是 题目&#xff1a; &#xff08;&#xff09;指的是一个软件结构内&#xff0c;不同模块之间互联的紧密程度。 题目&#xff1a; 4 、&#xff08;&#xff09;衡量一个模块内各个元素彼此结合的紧密程度。 题目&#…

junit junit_JUnit理论简介

junit junit您读过数学理论吗&#xff1f; 它通常读取如下内容&#xff1a; 对于所有a&#xff0c;b> 0满足以下条件&#xff1a;a b> a和a b> b 通常&#xff0c;这些语句更难以理解。 这种陈述有一些有趣的地方&#xff1a;它适用于相当大&#xff08;在这种情…

指数矩阵(exponential matrix)

类似于指数ex……e^x……ex……的本质是一种近似&#xff0c;eAt……e^{At}……eAt……是同样原理。 http://www.mashangxue123.com/%E7%BA%BF%E6%80%A7%E4%BB%A3%E6%95%B0/1756604500.html

【渝粤题库】陕西师范大学151210 成本会计作业 (专升本)

《成本会计【专升本】》作业 一、单选题 1.从现行企业会计制度的有关规定出发&#xff0c;成本会计的对象是&#xff08; &#xff09;。 A各项期间费用的支出及归集过程 B产品生产成本的形成过程 C诸会计要素的增减变动 D企业生产经营过程中发生的生产经营业务成本和期间费用 …