完善章节3单元测试

This commit is contained in:
bwh
2019-04-12 02:08:09 +08:00
parent cd6fa32da8
commit 5b902de8c1
7 changed files with 299 additions and 73 deletions

View File

@@ -60,4 +60,23 @@ public class Book implements Serializable {
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public Book(Long id, String name, String writer, String introduction) {
this.id = id;
this.name = name;
this.writer = writer;
this.introduction = introduction;
}
public Book(Long id, String name) {
this.id = id;
this.name = name;
}
public Book(String name) {
this.name = name;
}
public Book() {
}
}

View File

@@ -42,4 +42,18 @@ public interface BookService {
* @param id 编号
*/
Book findById(Long id);
/**
* 查找书是否存在
* @param book
* @return
*/
boolean exists(Book book);
/**
* 根据书名获取书籍
* @param name
* @return
*/
Book findByName(String name);
}

View File

@@ -2,24 +2,40 @@ package demo.springboot.service.impl;
import demo.springboot.domain.Book;
import demo.springboot.service.BookService;
import demo.springboot.web.BookController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* Book 业务层实现
*
* <p>
* Created by bysocket on 27/09/2017.
*/
@Service
public class BookServiceImpl implements BookService {
private static final AtomicLong counter = new AtomicLong();
/**
* 使用集合模拟数据库
*/
private static List<Book> books = new ArrayList<>(
Arrays.asList(
new Book(counter.incrementAndGet(), "book")));
// 模拟数据库,存储 Book 信息
// 第五章《数据存储》会替换成 MySQL 存储
private static Map<Long, Book> BOOK_DB = new HashMap<>();
private static Map<String, Book> BOOK_DB = new HashMap<>();
@Override
public List<Book> findAll() {
@@ -29,23 +45,40 @@ public class BookServiceImpl implements BookService {
@Override
public Book insertByBook(Book book) {
book.setId(BOOK_DB.size() + 1L);
BOOK_DB.put(book.getId(), book);
BOOK_DB.put(book.getId().toString(), book);
return book;
}
@Override
public Book update(Book book) {
BOOK_DB.put(book.getId(), book);
BOOK_DB.put(book.getId().toString(), book);
return book;
}
@Override
public Book delete(Long id) {
return BOOK_DB.remove(id);
return BOOK_DB.remove(id.toString());
}
@Override
public Book findById(Long id) {
return BOOK_DB.get(id);
return BOOK_DB.get(id.toString());
}
@Override
public boolean exists(Book book) {
return findByName(book.getName()) != null;
}
@Override
public Book findByName(String name) {
for (Book book : books) {
if (book.getName().equals(name)) {
return book;
}
}
return null;
}
}

View File

@@ -2,8 +2,14 @@ package demo.springboot.web;
import demo.springboot.domain.Book;
import demo.springboot.service.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.List;
@@ -16,6 +22,10 @@ import java.util.List;
@RequestMapping(value = "/book")
public class BookController {
private final Logger LOG = LoggerFactory.getLogger(BookController.class);
@Autowired
BookService bookService;
@@ -43,8 +53,20 @@ public class BookController {
* 通过 @RequestBody 绑定实体参数,也通过 @RequestParam 传递参数
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
public Book postBook(@RequestBody Book book) {
return bookService.insertByBook(book);
public ResponseEntity<Void> postBook(@RequestBody Book book, UriComponentsBuilder ucBuilder) {
LOG.info("creating new book: {}", book);
if (book.getName().equals("conflict")){
LOG.info("a book with name " + book.getName() + " already exists");
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
bookService.insertByBook(book);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/book/{id}").buildAndExpand(book.getId()).toUri());
return new ResponseEntity<>(headers, HttpStatus.CREATED);
}
/**