Service,dao层,视图,模型修改 单元测试

This commit is contained in:
禾几海
2020-05-26 12:34:35 +08:00
parent fde9b8511c
commit aa882406d0
11 changed files with 219 additions and 426 deletions

View File

@@ -137,30 +137,42 @@ where article.a_id = article_tag.a_id
and category.is_category = true
and article.a_author_id = user.u_id;
CREATE VIEW commentView
(commentId, pagePath, content, date, status, pid,
fromAuthorId, fromAuthorEmail, fromAuthorDisplayName, fromAuthorAvatar, toAuthorId,
toAuthorEmail, toAuthorDisplayName, toAuthorAvatar, isDelete
)
(commentId, pagePath, content, date, status, pid, toAuthorId, toAuthorEmail, toAuthorDisplayName,
toAuthorAvatar, fromAuthorId, fromAuthorEmail, fromAuthorDisplayName,
fromAuthorAvatar, isDelete)
as
select comment.co_id as commentId,
comment.co_page_path as pagePath,
comment.co_content as content,
comment.co_date as date,
comment.co_status as status,
comment.co_pid as pid,
comment.co_from_author_id as fromAuthorId,
select cuT.co_id as commentId,
cuT.co_page_path as pagePath,
cuT.co_content as content,
cuT.co_date as date,
cuT.co_status as status,
cuT.co_pid as pid,
cuT.co_to_author_id as toAuthorId,
cuT.toEmail as toAuthorEmail,
cuT.toDisplayName as toAuthorDisplayName,
cuT.toAvatar as toAuthorAvatar,
userFrom.u_id as fromAuthorId,
userFrom.u_email as fromAuthorEmail,
userFrom.u_display_name as fromAuthorDisplayName,
userFrom.u_avatar as fromAuthorAvatar,
comment.co_to_author_id as toAuthorId,
userTo.u_email as toAuthorEmail,
userTo.u_display_name as toAuthorDisplayName,
userTo.u_avatar as toAuthorAvatar,
comment.is_delete as isDelete
from comment,
user as userFrom,
user as userTo
where comment.co_from_author_id = userFrom.u_id
and comment.co_to_author_id = userTo.u_id;
cuT.is_delete as isDelete
from (select comment.co_id,
comment.co_page_path,
comment.co_content,
comment.co_date,
comment.co_status,
comment.co_pid,
comment.co_from_author_id,
comment.co_to_author_id,
userTo.u_email as toEmail,
userTo.u_display_name as toDisplayName,
userTo.u_avatar as toAvatar,
comment.is_delete
from comment
left join user userTo on (comment.co_to_author_id = userTo.u_id)
) as cuT,
user as userFrom
where cuT.co_from_author_id = userFrom.u_id;

View File

@@ -40,58 +40,40 @@ public class CommentController {
}
/**
* 获取所有的一级评论
* 获取所有的评论
*
* @param articleId 文章id
* @param pagePath pagePath
* @param count 单页数据量
* @param page 页码
* @return Response
*/
@GetMapping("/comments")
public Response commentsOfArticle(@RequestParam("articleId") long articleId,
public Response commentsOfArticle(@RequestParam("pagePath") String pagePath,
@RequestParam(value = "pid", required = false, defaultValue = "-1") long pid,
@RequestParam(value = "count", required = false, defaultValue = "10") int count,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
return ResponseUtil.success(commentService.retrievePageByArticle(articleId, -1, page, count));
return ResponseUtil.success(commentService.retrievePageByPageAndPid(pagePath, pid, page, count));
}
/**
* 通过pid获取数据
*
* @param pid
* @return
* @param pagePath pagePath
* @param count count
* @param page page
* @return Response
*/
@GetMapping("/comment/pid/{pid}")
public Response retrievePage(@PathVariable("pid") long pid) {
return ResponseUtil.success(commentService.retrievePageByPid(pid));
}
/**
* 获取所以的一级留言
*
* @param count
* @param page
* @return
*/
@GetMapping("/leaveMsg")
public Response retrievePageOfLeaveMsg(@RequestParam(value = "count", required = false, defaultValue = "10") int count,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
return ResponseUtil.success(commentService.retrievePageByTypeAndPid(false, -1, page, count));
}
@GetMapping("/admin/comment/type/{type}")
public Response retrievePageAdmin(
@PathVariable("type") int isComment,
@GetMapping("/comment/pagePath/{pagePath}")
public Response retrievePage(@PathVariable("pagePath") String pagePath,
@RequestParam(value = "count", required = false, defaultValue = "10") int count,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
return ResponseUtil.success(commentService.retrievePageByType(1 == isComment, page, count));
return ResponseUtil.success(commentService.retrievePage(pagePath, page, count));
}
@GetMapping("/user/comment/type/{type}")
public Response retrievePageByAuthor(
@PathVariable(value = "type") int isComment,
@GetMapping("/user/comment/pagePath/{pagePath}")
public Response userComment(@PathVariable("pagePath") String pagePath,
@RequestParam(value = "count", required = false, defaultValue = "10") int count,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
return ResponseUtil.success(commentService.retrievePageByAuthor(1 == isComment, page, count));
return ResponseUtil.success(commentService.retrievePageByAuthor(pagePath, page, count));
}
}

View File

@@ -11,6 +11,8 @@ public enum ResponseEnum {
FAILURE(-1, "失败"),
ERROR(-2, "错误"),
DATA_IS_DELETED(1000, "数据已被删除"),
//文章类
ARTICLE_NOT_EXIST(2010, "文章不存在"),
ARTICLE_HAS_EXIST(2020, "文章已存在"),
@@ -68,7 +70,6 @@ public enum ResponseEnum {
PARAMETERS_QQ_ERROR(8540, "QQ格式错误"),
PARAMETERS_PWD_ERROR(8550, "密码格式错误"),
VERIFY_OUT(8400, "已经验证过了");
private int code;
private String msg;

View File

@@ -1,5 +1,6 @@
package cn.celess.blog.entity.model;
import cn.celess.blog.entity.User;
import lombok.Getter;
import lombok.Setter;
@@ -14,46 +15,31 @@ import java.util.List;
public class CommentModel {
private long id;
/**
* 是评论还是留言 0:评论 其他1留言
*/
private boolean isComment;
private UserModel fromUser;
private String authorName;
private String authorAvatarImgUrl;
private UserModel toUser;
/**
* 内容
*/
private String content;
/**
* 文章ID
*/
private long articleID;
/**
* 文章标题
*/
private String articleTitle;
private String pagePath;
/**
* 发布日期
*/
private String date;
/**
* 回应着ID 默认为顶级回复
*/
private String responseId = "";
/**
* 评论的父ID
*/
private long pid = -1;
private Long pid;
private List<CommentModel> respComment;
private int status;
}

View File

@@ -9,9 +9,8 @@ import lombok.Data;
@Data
public class CommentReq {
private Long id;
private Boolean comment;
private String content;
private Long pid;
private Long articleID;
private String responseId;
private long pid = -1;
private String pagePath;
private long toUserId = -1;
}

View File

@@ -34,6 +34,8 @@ public interface CommentMapper {
List<Comment> findAllByPagePath(String pagePath);
List<Comment> findAllByPagePathAndFromUser(String pagePath,long userId);
List<Comment> findAllByPagePathAndPid(String pagePath, long pid);
long countByPagePath(String pagePath);

View File

@@ -1,6 +1,7 @@
package cn.celess.blog.service;
import cn.celess.blog.entity.model.CommentModel;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.request.CommentReq;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
@@ -40,12 +41,12 @@ public interface CommentService {
/**
* 分页获取数据
*
* @param isComment true评论 false留言
* @param pagePath pagePath
* @param count 单页数据量
* @param page 数据页
* @return 分页数据
*/
PageInfo<CommentModel> retrievePage(Boolean isComment, int page, int count);
PageData<CommentModel> retrievePage(String pagePath, int page, int count);
/**
* 通过pid获取数据
@@ -59,43 +60,33 @@ public interface CommentService {
/**
* 根据评论者获取数据
*
* @param isComment true评论 false留言
* @param pagePath pagePath
* @param count 单页数据量
* @param page 数据页
* @return 分页数据
*/
PageInfo<CommentModel> retrievePageByAuthor(Boolean isComment, int page, int count);
PageData<CommentModel> retrievePageByAuthor(String pagePath, int page, int count);
/**
* 根据文章获取数据
*
* @param articleID 文章id
* @param pid 父id
* @param count 单页数据量
* @param page 数据页
* @return 分页数据
*/
PageInfo<CommentModel> retrievePageByArticle(long articleID, long pid, int page, int count);
/**
* 根据数据的type和pid获取数据
*
* @param isComment true评论 false留言
* @param pagePath pagePath
* @param pid 父id
* @param count 单页数据量
* @param page 数据页
* @return 分页数据
*/
PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count);
PageData<CommentModel> retrievePageByPageAndPid(String pagePath, long pid, int page, int count);
/**
* 根据type获取数据
*
* @param isComment true评论 false留言
* @param pagePath pagePath
* @param count 单页数据量
* @param page 数据页
* @return 分页数据
*/
PageInfo<CommentModel> retrievePageByType(Boolean isComment, int page, int count);
PageData<CommentModel> retrievePageByPage(String pagePath, int page, int count);
}

View File

@@ -2,18 +2,22 @@ package cn.celess.blog.service.serviceimpl;
import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.Comment;
import cn.celess.blog.entity.User;
import cn.celess.blog.entity.model.CommentModel;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.request.CommentReq;
import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.ArticleMapper;
import cn.celess.blog.mapper.CommentMapper;
import cn.celess.blog.mapper.UserMapper;
import cn.celess.blog.service.CommentService;
import cn.celess.blog.service.UserService;
import cn.celess.blog.util.DateFormatUtil;
import cn.celess.blog.util.ModalTrans;
import cn.celess.blog.util.RedisUserUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -31,7 +35,7 @@ public class CommentServiceImpl implements CommentService {
@Autowired
CommentMapper commentMapper;
@Autowired
UserService userService;
UserMapper userMapper;
@Autowired
ArticleMapper articleMapper;
@Autowired
@@ -41,52 +45,43 @@ public class CommentServiceImpl implements CommentService {
@Override
public CommentModel create(CommentReq reqBody) {
if (reqBody == null) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
long authorID = redisUserUtil.get().getId();
User user = redisUserUtil.get();
Comment pComment = null;
if (reqBody.getPid() != null && reqBody.getPid() != -1) {
if (reqBody.getPid() != -1) {
pComment = commentMapper.findCommentById(reqBody.getPid());
}
if (reqBody.getPid() == null) {
reqBody.setPid(-1L);
}
//不是一级评论
if (reqBody.getPid() != -1 && pComment == null) {
//父评论不存在
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
Comment comment = new Comment();
comment.setAuthorID(authorID);
comment.setType(reqBody.getComment());
if (reqBody.getComment()) {
//若为评论
if (reqBody.getArticleID() <= 0) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
comment.setFromUser(user);
User userTo = new User();
userTo.setId(-1L);
if (reqBody.getToUserId() != -1) {
userTo = userMapper.findById(reqBody.getToUserId());
comment.setToUser(userTo);
}
comment.setArticleID(reqBody.getArticleID());
} else {
comment.setArticleID(-1L);
}
comment.setContent(reqBody.getContent());
comment.setPid(reqBody.getPid());
comment.setDate(new Date());
comment.setResponseId("");
comment.setToUser(userTo);
userMapper.findById(reqBody.getToUserId());
BeanUtils.copyProperties(reqBody, comment);
commentMapper.insert(comment);
if (reqBody.getPid() != -1) {
commentMapper.updateResponder(pComment.getResponseId() + comment.getId() + ",", reqBody.getPid());
}
return trans(comment);
return ModalTrans.comment(commentMapper.findCommentById(comment.getId()));
}
@Override
public boolean delete(long id) {
boolean b = commentMapper.existsById(id);
if (!b) {
Comment b = commentMapper.findCommentById(id);
if (b == null ) {
throw new MyException(ResponseEnum.COMMENT_NOT_EXIST);
}
if(b.isDelete()){
throw new MyException(ResponseEnum.DATA_IS_DELETED);
}
commentMapper.delete(id);
return true;
}
@@ -101,86 +96,54 @@ public class CommentServiceImpl implements CommentService {
commentMapper.updateContent(reqBody.getContent(), reqBody.getId());
comment.setContent(reqBody.getContent());
}
if (!comment.getResponseId().equals(reqBody.getResponseId())) {
commentMapper.updateResponder(reqBody.getResponseId(), reqBody.getId());
comment.setResponseId(reqBody.getResponseId());
return ModalTrans.comment(comment);
}
return trans(comment);
}
@Override
public PageInfo<CommentModel> retrievePage(Boolean isComment, int page, int count) {
public PageData<CommentModel> retrievePage(String pagePath, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByType(isComment);
return pageTrans(commentList);
List<Comment> list = commentMapper.findAllByPagePath(pagePath);
return pageTrans(list);
}
@Override
public List<CommentModel> retrievePageByPid(long pid) {
List<Comment> commentList = commentMapper.findAllByPId(pid);
List<Comment> allByPagePath = commentMapper.findAllByPid(pid);
List<CommentModel> commentModels = new ArrayList<>();
allByPagePath.forEach(comment -> commentModels.add(ModalTrans.comment(comment)));
return commentModels;
}
@Override
public PageData<CommentModel> retrievePageByAuthor(String pagePath, int page, int count) {
User user = redisUserUtil.get();
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndFromUser(pagePath, user.getId());
return pageTrans(list);
}
@Override
public PageData<CommentModel> retrievePageByPageAndPid(String pagePath, long pid, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndPid(pagePath, pid);
return pageTrans(list);
}
@Override
public PageData<CommentModel> retrievePageByPage(String pagePath, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> list = commentMapper.findAllByPagePathAndPid(pagePath, -1);
return pageTrans(list);
}
private PageData<CommentModel> pageTrans(List<Comment> commentList) {
PageInfo<Comment> p = PageInfo.of(commentList);
List<CommentModel> modelList = new ArrayList<>();
commentList.forEach(m -> modelList.add(trans(m)));
return modelList;
}
@Override
public PageInfo<CommentModel> retrievePageByArticle(long articleID, long pid, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByArticleIDAndPId(articleID, pid);
return pageTrans(commentList);
}
@Override
public PageInfo<CommentModel> retrievePageByTypeAndPid(Boolean isComment, int pid, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findCommentsByTypeAndPId(isComment, pid);
return pageTrans(commentList);
}
@Override
public PageInfo<CommentModel> retrievePageByAuthor(Boolean isComment, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByAuthorIDAndType(redisUserUtil.get().getId(), isComment);
return pageTrans(commentList);
}
@Override
public PageInfo<CommentModel> retrievePageByType(Boolean isComment, int page, int count) {
PageHelper.startPage(page, count);
List<Comment> commentList = commentMapper.findAllByType(isComment);
return pageTrans(commentList);
}
private CommentModel trans(Comment comment) {
CommentModel commentModel = new CommentModel();
commentModel.setId(comment.getId());
commentModel.setComment(comment.getType());
commentModel.setContent(comment.getContent());
commentModel.setArticleID(comment.getArticleID());
commentModel.setDate(DateFormatUtil.get(comment.getDate()));
commentModel.setResponseId(comment.getResponseId());
commentModel.setPid(comment.getPid());
commentModel.setAuthorName(userService.getNameById(comment.getAuthorID()));
commentModel.setAuthorAvatarImgUrl("http://cdn.celess.cn/" + userService.getAvatarImg(comment.getAuthorID()));
if (comment.getType() && commentModel.getArticleID() > 0) {
commentModel.setArticleTitle(articleMapper.getTitleById(comment.getArticleID()));
}
return commentModel;
}
private PageInfo<CommentModel> pageTrans(List<Comment> commentList) {
PageInfo p = new PageInfo(commentList);
List<CommentModel> modelList = new ArrayList<>();
commentList.forEach(l -> {
CommentModel model = trans(l);
CommentModel model = ModalTrans.comment(l);
model.setRespComment(this.retrievePageByPid(model.getId()));
modelList.add(model);
});
p.setList(modelList);
return p;
return new PageData<CommentModel>(p, modelList);
}
}

View File

@@ -38,6 +38,9 @@ public class ModalTrans {
public static UserModel user(User user) {
if (user == null || user.getId() == -1) {
return null;
}
UserModel userModel = new UserModel();
BeanUtils.copyProperties(user, userModel);
userModel.setAvatarImgUrl(user.getAvatarImgUrl() == null || user.getAvatarImgUrl().length() == 0 ?
@@ -49,6 +52,9 @@ public class ModalTrans {
}
public static CategoryModel category(Category category) {
if (category == null) {
return null;
}
CategoryModel model = new CategoryModel();
BeanUtils.copyProperties(category, model);
return model;
@@ -56,6 +62,9 @@ public class ModalTrans {
public static TagModel tag(Tag tag) {
if (tag == null) {
return null;
}
TagModel model = new TagModel();
BeanUtils.copyProperties(tag, model);
return model;
@@ -63,10 +72,25 @@ public class ModalTrans {
public static WebUpdateModel webUpdate(WebUpdate update) {
if (update == null) {
return null;
}
WebUpdateModel model = new WebUpdateModel();
model.setId(update.getId());
model.setInfo(update.getUpdateInfo());
model.setTime(DateFormatUtil.get(update.getUpdateTime()));
return model;
}
public static CommentModel comment(Comment comment) {
if (comment == null) {
return null;
}
CommentModel model = new CommentModel();
BeanUtils.copyProperties(comment, model);
model.setFromUser(user(comment.getFromUser()));
model.setToUser(user(comment.getToUser()));
model.setDate(DateFormatUtil.get(comment.getDate()));
return model;
}
}

View File

@@ -68,7 +68,7 @@
<select id="findCommentById" resultMap="commentViewResultMap">
select *
from commentView
where commentId = #{id}
where commentId = #{sqid}
</select>
<select id="findAllByFromUser" resultMap="commentViewResultMap">
@@ -89,6 +89,13 @@
where pagePath = #{pagePath}
</select>
<select id="findAllByPagePathAndFromUser" resultMap="commentViewResultMap">
select *
from commentView
where pagePath = #{pagePath}
and fromAuthorId = #{userId}
</select>
<select id="findAllByPagePathAndPid" resultMap="commentViewResultMap">
select *
from commentView
@@ -110,7 +117,8 @@
<select id="count" resultType="long">
select count(*)
from article where is_delete = false;
from article
where is_delete = false;
</select>
</mapper>

View File

@@ -3,24 +3,18 @@ package cn.celess.blog.controller;
import cn.celess.blog.BaseTest;
import cn.celess.blog.entity.Article;
import cn.celess.blog.entity.Comment;
import cn.celess.blog.entity.User;
import cn.celess.blog.entity.model.CommentModel;
import cn.celess.blog.entity.request.CommentReq;
import cn.celess.blog.mapper.ArticleMapper;
import cn.celess.blog.mapper.CommentMapper;
import cn.celess.blog.mapper.UserMapper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.*;
import static cn.celess.blog.enmu.ResponseEnum.*;
@@ -30,19 +24,19 @@ public class CommentControllerTest extends BaseTest {
@Autowired
ArticleMapper articleMapper;
@Autowired
UserMapper userMapper;
@Autowired
CommentMapper commentMapper;
@Test
public void addOne() throws Exception {
Article article = articleMapper.getLastestArticle();
CommentReq commentReq = new CommentReq();
// 测试留言
commentReq.setArticleID(null);
commentReq.setComment(false);
commentReq.setPagePath("/article/" + article.getId());
commentReq.setContent(UUID.randomUUID().toString());
commentReq.setPid(-1L);
commentReq.setResponseId(null);
commentReq.setToUserId(-1L);
String token = userLogin();
CommentModel PC = null;
mockMvc.perform(post("/user/comment/create")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSONObject.fromObject(commentReq).toString())
@@ -52,22 +46,19 @@ public class CommentControllerTest extends BaseTest {
assertEquals(SUCCESS.getCode(), object.getInt(Code));
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
assertNotEquals(0, model.getId());
assertEquals(commentReq.getPid().longValue(), model.getPid());
assertEquals(-1, model.getPid());
assertEquals(commentReq.getComment(), model.isComment());
assertEquals(commentReq.getPid(), model.getPid().longValue());
assertEquals(-1, model.getPid().longValue());
assertEquals(commentReq.getContent(), model.getContent());
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getFromUser());
assertNull(model.getToUser());
});
Article article = articleMapper.getLastestArticle();
// 测试评论
commentReq.setArticleID(article.getId());
commentReq.setComment(true);
commentReq.setPagePath("/article/" + article.getId());
commentReq.setContent(UUID.randomUUID().toString());
commentReq.setPid(-1L);
commentReq.setResponseId(null);
commentReq.setToUserId(2);
mockMvc.perform(post("/user/comment/create")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSONObject.fromObject(commentReq).toString())
@@ -78,58 +69,50 @@ public class CommentControllerTest extends BaseTest {
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
// 响应数据的完整性
assertNotEquals(0, model.getId());
assertEquals(commentReq.getPid().longValue(), model.getPid());
assertEquals(-1, model.getPid());
assertEquals(commentReq.getComment(), model.isComment());
assertEquals(commentReq.getPid(), model.getPid().longValue());
assertEquals(-1, model.getPid().longValue());
assertEquals(commentReq.getContent(), model.getContent());
assertEquals(commentReq.getArticleID().longValue(), model.getArticleID());
assertEquals(commentReq.getPagePath(), "/article/" + article.getId());
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getFromUser());
assertNotNull(model.getToUser());
});
// 测试二级回复
Comment lastestComment = commentMapper.getLastestComment();
commentReq.setArticleID(lastestComment.getArticleID());
commentReq.setComment(lastestComment.getType());
commentReq.setPagePath("/article/" + article.getId());
commentReq.setContent(UUID.randomUUID().toString());
commentReq.setPid(lastestComment.getId());
commentReq.setResponseId(null);
mockMvc.perform(post("/user/comment/create")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSONObject.fromObject(commentReq).toString())
.header("Authorization", token)
).andDo(MockMvcResultHandlers.print()).andDo(result -> {
).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
CommentModel model = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
// 重新获取父评论信息
Comment pCommon = commentMapper.findCommentById(lastestComment.getId());
assertEquals(pCommon.getId().longValue(), model.getPid());
// 判断父评论中是否有写入当前新增的评论的id
String[] ids = pCommon.getResponseId().split(",");
boolean contain = false;
for (String id : ids) {
if (!id.isEmpty() && Long.parseLong(id) == model.getId()) {
contain = true;
break;
}
}
assertTrue(contain);
assertEquals(pCommon.getId(), model.getPid());
});
}
@Test
public void deleteTest() throws Exception {
// 准备数据
Comment c = new Comment();
c.setArticleID(-1L);
c.setType(true);
c.setAuthorID(2L);
c.setDate(new Date());
c.setPid(-1L);
commentMapper.insert(c);
Comment comment = commentMapper.getLastestComment();
User from = userMapper.findByEmail("zh56462271@qq.com");
assertNotNull(from);
User to = userMapper.findByEmail("a@celess.cn");
assertNotNull(to);
Comment comment = new Comment();
comment.setContent(randomStr(8));
comment.setFromUser(from);
comment.setToUser(to);
comment.setPagePath("/tags");
comment.setPid(-1L);
commentMapper.insert(comment);
comment = commentMapper.findCommentById(comment.getId());
// 接口测试
long id = comment.getId();
assertNotEquals(0, id);
@@ -141,7 +124,7 @@ public class CommentControllerTest extends BaseTest {
});
mockMvc.perform(delete("/user/comment/del?id=" + id).header("Authorization", token)).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(COMMENT_NOT_EXIST.getCode(), object.getInt(Code));
assertEquals(DATA_IS_DELETED.getCode(), object.getInt(Code));
});
}
@@ -150,12 +133,8 @@ public class CommentControllerTest extends BaseTest {
Comment comment = commentMapper.getLastestComment();
CommentReq commentReq = new CommentReq();
commentReq.setId(comment.getId());
commentReq.setPid(comment.getPid());
commentReq.setContent(UUID.randomUUID().toString());
commentReq.setArticleID(comment.getArticleID());
// 不合法数据 setResponseId
commentReq.setResponseId("xxxx");
commentReq.setComment(comment.getType());
mockMvc.perform(put("/user/comment/update")
.content(JSONObject.fromObject(commentReq).toString())
.contentType(MediaType.APPLICATION_JSON_UTF8)
@@ -165,161 +144,7 @@ public class CommentControllerTest extends BaseTest {
assertEquals(SUCCESS.getCode(), object.getInt(Code));
CommentModel c = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
assertEquals(commentReq.getContent(), c.getContent());
assertEquals(commentReq.getResponseId(), c.getResponseId());
assertNotNull(c.getAuthorAvatarImgUrl());
assertNotNull(c.getAuthorName());
assertNotNull(c.getDate());
assertNotEquals(0, c.getPid());
assertNotEquals(0, c.getArticleID());
});
}
@Test
public void commentsOfArticle() throws Exception {
mockMvc.perform(get("/comments?articleId=3&page=1&count=10")).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
assertNotEquals(0, pageInfo.getStartRow());
assertNotEquals(0, pageInfo.getEndRow());
assertEquals(1, pageInfo.getPageNum());
assertEquals(10, pageInfo.getPageSize());
pageInfo.getList().forEach(o -> {
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
assertEquals(3, model.getArticleID());
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getArticleTitle());
assertNotNull(model.getContent());
assertNotNull(model.getResponseId());
});
});
}
@Test
public void retrievePage() throws Exception {
long pid = -1;
mockMvc.perform(get("/comment/pid/" + pid)).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
JSONArray jsonArray = object.getJSONArray(Result);
assertNotEquals(0, jsonArray.size());
jsonArray.forEach(o -> {
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getContent());
assertNotNull(model.getResponseId());
});
});
}
@Test
public void retrievePageOfLeaveMsg() throws Exception {
mockMvc.perform(get("/leaveMsg?page=1&count=10")).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
assertNotEquals(0, pageInfo.getStartRow());
assertNotEquals(0, pageInfo.getEndRow());
assertEquals(1, pageInfo.getPageNum());
assertEquals(10, pageInfo.getPageSize());
pageInfo.getList().forEach(o -> {
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
assertEquals(-1, model.getArticleID());
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getContent());
assertNotNull(model.getResponseId());
assertFalse(model.isComment());
});
});
}
@Test
public void retrievePageAdmin() throws Exception {
mockMvc.perform(get("/admin/comment/type/1?page=1&count=10").header("Authorization", adminLogin())).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
assertNotEquals(0, pageInfo.getStartRow());
assertNotEquals(0, pageInfo.getEndRow());
assertEquals(1, pageInfo.getPageNum());
assertEquals(10, pageInfo.getPageSize());
pageInfo.getList().forEach(o -> {
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
assertNotEquals(-1, model.getArticleID());
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getContent());
assertNotNull(model.getResponseId());
assertTrue(model.isComment());
});
});
mockMvc.perform(get("/admin/comment/type/0?page=1&count=10").header("Authorization", adminLogin())).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
assertNotEquals(0, pageInfo.getStartRow());
assertNotEquals(0, pageInfo.getEndRow());
assertEquals(1, pageInfo.getPageNum());
assertEquals(10, pageInfo.getPageSize());
pageInfo.getList().forEach(o -> {
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
assertEquals(-1, model.getArticleID());
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getContent());
assertNotNull(model.getResponseId());
assertFalse(model.isComment());
});
});
}
@Test
public void retrievePageByAuthor() throws Exception {
mockMvc.perform(get("/user/comment/type/1?page=1&count=10").header("Authorization", userLogin())).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
assertNotEquals(0, pageInfo.getStartRow());
assertNotEquals(0, pageInfo.getEndRow());
assertEquals(1, pageInfo.getPageNum());
assertEquals(10, pageInfo.getPageSize());
pageInfo.getList().forEach(o -> {
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
assertNotEquals(-1, model.getArticleID());
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getContent());
assertNotNull(model.getResponseId());
assertTrue(model.isComment());
});
});
mockMvc.perform(get("/user/comment/type/0?page=1&count=10").header("Authorization", userLogin())).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
assertNotEquals(0, pageInfo.getStartRow());
assertNotEquals(0, pageInfo.getEndRow());
assertEquals(1, pageInfo.getPageNum());
assertEquals(10, pageInfo.getPageSize());
pageInfo.getList().forEach(o -> {
CommentModel model = (CommentModel) JSONObject.toBean(JSONObject.fromObject(o), CommentModel.class);
assertEquals(-1, model.getArticleID());
assertNotNull(model.getDate());
assertNotNull(model.getAuthorName());
assertNotNull(model.getAuthorAvatarImgUrl());
assertNotNull(model.getContent());
assertNotNull(model.getResponseId());
assertFalse(model.isComment());
});
});
}
}