切换到jackson #10
@@ -3,21 +3,23 @@ package cn.celess.blog.controller;
|
|||||||
import cn.celess.blog.BaseTest;
|
import cn.celess.blog.BaseTest;
|
||||||
import cn.celess.blog.entity.Article;
|
import cn.celess.blog.entity.Article;
|
||||||
import cn.celess.blog.entity.Comment;
|
import cn.celess.blog.entity.Comment;
|
||||||
|
import cn.celess.blog.entity.Response;
|
||||||
import cn.celess.blog.entity.User;
|
import cn.celess.blog.entity.User;
|
||||||
import cn.celess.blog.entity.model.CommentModel;
|
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.entity.request.CommentReq;
|
||||||
import cn.celess.blog.mapper.ArticleMapper;
|
import cn.celess.blog.mapper.ArticleMapper;
|
||||||
import cn.celess.blog.mapper.CommentMapper;
|
import cn.celess.blog.mapper.CommentMapper;
|
||||||
import cn.celess.blog.mapper.UserMapper;
|
import cn.celess.blog.mapper.UserMapper;
|
||||||
import net.sf.json.JSONObject;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static cn.celess.blog.enmu.ResponseEnum.DATA_IS_DELETED;
|
||||||
|
import static cn.celess.blog.enmu.ResponseEnum.SUCCESS;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
|
|
||||||
public class CommentControllerTest extends BaseTest {
|
public class CommentControllerTest extends BaseTest {
|
||||||
@@ -27,6 +29,10 @@ public class CommentControllerTest extends BaseTest {
|
|||||||
UserMapper userMapper;
|
UserMapper userMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
CommentMapper commentMapper;
|
CommentMapper commentMapper;
|
||||||
|
private static final TypeReference<?> COMMENT_MODEL_TYPE = new TypeReference<Response<CommentModel>>() {
|
||||||
|
};
|
||||||
|
private static final TypeReference<?> COMMENT_MODEL_PAGE_TYPE = new TypeReference<Response<PageData<CommentModel>>>() {
|
||||||
|
};
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addOne() throws Exception {
|
public void addOne() throws Exception {
|
||||||
@@ -36,15 +42,10 @@ public class CommentControllerTest extends BaseTest {
|
|||||||
commentReq.setContent(UUID.randomUUID().toString());
|
commentReq.setContent(UUID.randomUUID().toString());
|
||||||
commentReq.setPid(-1L);
|
commentReq.setPid(-1L);
|
||||||
commentReq.setToUserId(-1L);
|
commentReq.setToUserId(-1L);
|
||||||
String token = userLogin();
|
getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> {
|
||||||
mockMvc.perform(post("/user/comment/create")
|
Response<CommentModel> response = getResponse(result, COMMENT_MODEL_TYPE);
|
||||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
.content(JSONObject.fromObject(commentReq).toString())
|
CommentModel model = response.getResult();
|
||||||
.header("Authorization", token)
|
|
||||||
).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);
|
|
||||||
assertNotEquals(0, model.getId());
|
assertNotEquals(0, model.getId());
|
||||||
assertEquals(commentReq.getPid(), model.getPid().longValue());
|
assertEquals(commentReq.getPid(), model.getPid().longValue());
|
||||||
assertEquals(-1, model.getPid().longValue());
|
assertEquals(-1, model.getPid().longValue());
|
||||||
@@ -59,14 +60,10 @@ public class CommentControllerTest extends BaseTest {
|
|||||||
commentReq.setContent(UUID.randomUUID().toString());
|
commentReq.setContent(UUID.randomUUID().toString());
|
||||||
commentReq.setPid(-1L);
|
commentReq.setPid(-1L);
|
||||||
commentReq.setToUserId(2);
|
commentReq.setToUserId(2);
|
||||||
mockMvc.perform(post("/user/comment/create")
|
getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> {
|
||||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
Response<CommentModel> response = getResponse(result, COMMENT_MODEL_TYPE);
|
||||||
.content(JSONObject.fromObject(commentReq).toString())
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
.header("Authorization", token)
|
CommentModel model = response.getResult();
|
||||||
).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);
|
|
||||||
// 响应数据的完整性
|
// 响应数据的完整性
|
||||||
assertNotEquals(0, model.getId());
|
assertNotEquals(0, model.getId());
|
||||||
assertEquals(commentReq.getPid(), model.getPid().longValue());
|
assertEquals(commentReq.getPid(), model.getPid().longValue());
|
||||||
@@ -79,20 +76,16 @@ public class CommentControllerTest extends BaseTest {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 测试二级回复
|
// 测试二级回复
|
||||||
Comment lastestComment = commentMapper.getLastestComment();
|
Comment latestComment = commentMapper.getLastestComment();
|
||||||
commentReq.setPagePath("/article/" + article.getId());
|
commentReq.setPagePath("/article/" + article.getId());
|
||||||
commentReq.setContent(UUID.randomUUID().toString());
|
commentReq.setContent(UUID.randomUUID().toString());
|
||||||
commentReq.setPid(lastestComment.getId());
|
commentReq.setPid(latestComment.getId());
|
||||||
mockMvc.perform(post("/user/comment/create")
|
getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> {
|
||||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
Response<CommentModel> response = getResponse(result, COMMENT_MODEL_TYPE);
|
||||||
.content(JSONObject.fromObject(commentReq).toString())
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
.header("Authorization", token)
|
CommentModel model = response.getResult();
|
||||||
).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());
|
Comment pCommon = commentMapper.findCommentById(latestComment.getId());
|
||||||
assertEquals(pCommon.getId(), model.getPid());
|
assertEquals(pCommon.getId(), model.getPid());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -116,16 +109,13 @@ public class CommentControllerTest extends BaseTest {
|
|||||||
// 接口测试
|
// 接口测试
|
||||||
long id = comment.getId();
|
long id = comment.getId();
|
||||||
assertNotEquals(0, id);
|
assertNotEquals(0, id);
|
||||||
String token = userLogin();
|
getMockData(delete("/user/comment/del?id=" + id), userLogin()).andDo(result -> {
|
||||||
mockMvc.perform(delete("/user/comment/del?id=" + id).header("Authorization", token)).andDo(result -> {
|
Response<Boolean> response = getResponse(result, BOOLEAN_TYPE);
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertTrue(response.getResult());
|
||||||
assertTrue(object.getBoolean(Result));
|
|
||||||
});
|
|
||||||
mockMvc.perform(delete("/user/comment/del?id=" + id).header("Authorization", token)).andDo(result -> {
|
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
|
||||||
assertEquals(DATA_IS_DELETED.getCode(), object.getInt(Code));
|
|
||||||
});
|
});
|
||||||
|
getMockData(delete("/user/comment/del?id=" + id), userLogin())
|
||||||
|
.andDo(result -> assertEquals(DATA_IS_DELETED.getCode(), getResponse(result, COMMENT_MODEL_TYPE).getCode()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -135,14 +125,10 @@ public class CommentControllerTest extends BaseTest {
|
|||||||
commentReq.setId(comment.getId());
|
commentReq.setId(comment.getId());
|
||||||
commentReq.setContent(UUID.randomUUID().toString());
|
commentReq.setContent(UUID.randomUUID().toString());
|
||||||
// 不合法数据 setResponseId
|
// 不合法数据 setResponseId
|
||||||
mockMvc.perform(put("/user/comment/update")
|
getMockData(put("/user/comment/update"), userLogin(), commentReq).andDo(result -> {
|
||||||
.content(JSONObject.fromObject(commentReq).toString())
|
Response<CommentModel> response = getResponse(result, COMMENT_MODEL_TYPE);
|
||||||
.contentType(MediaType.APPLICATION_JSON_UTF8)
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
.header("Authorization", userLogin())
|
CommentModel c = response.getResult();
|
||||||
).andDo(result -> {
|
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
|
||||||
CommentModel c = (CommentModel) JSONObject.toBean(object.getJSONObject(Result), CommentModel.class);
|
|
||||||
assertEquals(commentReq.getContent(), c.getContent());
|
assertEquals(commentReq.getContent(), c.getContent());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user