From e9209d18524c9617997ff300d1fd511bb27f6636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Fri, 24 Jul 2020 20:24:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4CommentController=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CommentControllerTest.java | 82 ++++++++----------- 1 file changed, 34 insertions(+), 48 deletions(-) diff --git a/src/test/java/cn/celess/blog/controller/CommentControllerTest.java b/src/test/java/cn/celess/blog/controller/CommentControllerTest.java index 19df58b..eed6588 100644 --- a/src/test/java/cn/celess/blog/controller/CommentControllerTest.java +++ b/src/test/java/cn/celess/blog/controller/CommentControllerTest.java @@ -3,21 +3,23 @@ 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.Response; 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.mapper.ArticleMapper; import cn.celess.blog.mapper.CommentMapper; import cn.celess.blog.mapper.UserMapper; -import net.sf.json.JSONObject; +import com.fasterxml.jackson.core.type.TypeReference; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; 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 cn.celess.blog.enmu.ResponseEnum.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; public class CommentControllerTest extends BaseTest { @@ -27,6 +29,10 @@ public class CommentControllerTest extends BaseTest { UserMapper userMapper; @Autowired CommentMapper commentMapper; + private static final TypeReference COMMENT_MODEL_TYPE = new TypeReference>() { + }; + private static final TypeReference COMMENT_MODEL_PAGE_TYPE = new TypeReference>>() { + }; @Test public void addOne() throws Exception { @@ -36,15 +42,10 @@ public class CommentControllerTest extends BaseTest { commentReq.setContent(UUID.randomUUID().toString()); commentReq.setPid(-1L); commentReq.setToUserId(-1L); - String token = userLogin(); - mockMvc.perform(post("/user/comment/create") - .contentType(MediaType.APPLICATION_JSON_UTF8) - .content(JSONObject.fromObject(commentReq).toString()) - .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); + getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> { + Response response = getResponse(result, COMMENT_MODEL_TYPE); + assertEquals(SUCCESS.getCode(), response.getCode()); + CommentModel model = response.getResult(); assertNotEquals(0, model.getId()); assertEquals(commentReq.getPid(), model.getPid().longValue()); assertEquals(-1, model.getPid().longValue()); @@ -59,14 +60,10 @@ public class CommentControllerTest extends BaseTest { commentReq.setContent(UUID.randomUUID().toString()); commentReq.setPid(-1L); commentReq.setToUserId(2); - mockMvc.perform(post("/user/comment/create") - .contentType(MediaType.APPLICATION_JSON_UTF8) - .content(JSONObject.fromObject(commentReq).toString()) - .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); + getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> { + Response response = getResponse(result, COMMENT_MODEL_TYPE); + assertEquals(SUCCESS.getCode(), response.getCode()); + CommentModel model = response.getResult(); // 响应数据的完整性 assertNotEquals(0, model.getId()); 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.setContent(UUID.randomUUID().toString()); - commentReq.setPid(lastestComment.getId()); - mockMvc.perform(post("/user/comment/create") - .contentType(MediaType.APPLICATION_JSON_UTF8) - .content(JSONObject.fromObject(commentReq).toString()) - .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); + commentReq.setPid(latestComment.getId()); + getMockData(post("/user/comment/create"), userLogin(), commentReq).andDo(result -> { + Response response = getResponse(result, COMMENT_MODEL_TYPE); + assertEquals(SUCCESS.getCode(), response.getCode()); + CommentModel model = response.getResult(); // 重新获取父评论信息 - Comment pCommon = commentMapper.findCommentById(lastestComment.getId()); + Comment pCommon = commentMapper.findCommentById(latestComment.getId()); assertEquals(pCommon.getId(), model.getPid()); }); } @@ -116,16 +109,13 @@ public class CommentControllerTest extends BaseTest { // 接口测试 long id = comment.getId(); assertNotEquals(0, id); - String token = userLogin(); - mockMvc.perform(delete("/user/comment/del?id=" + id).header("Authorization", token)).andDo(result -> { - JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); - assertEquals(SUCCESS.getCode(), object.getInt(Code)); - 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 -> { + Response response = getResponse(result, BOOLEAN_TYPE); + assertEquals(SUCCESS.getCode(), response.getCode()); + assertTrue(response.getResult()); }); + getMockData(delete("/user/comment/del?id=" + id), userLogin()) + .andDo(result -> assertEquals(DATA_IS_DELETED.getCode(), getResponse(result, COMMENT_MODEL_TYPE).getCode())); } @Test @@ -135,14 +125,10 @@ public class CommentControllerTest extends BaseTest { commentReq.setId(comment.getId()); commentReq.setContent(UUID.randomUUID().toString()); // 不合法数据 setResponseId - mockMvc.perform(put("/user/comment/update") - .content(JSONObject.fromObject(commentReq).toString()) - .contentType(MediaType.APPLICATION_JSON_UTF8) - .header("Authorization", userLogin()) - ).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); + getMockData(put("/user/comment/update"), userLogin(), commentReq).andDo(result -> { + Response response = getResponse(result, COMMENT_MODEL_TYPE); + assertEquals(SUCCESS.getCode(), response.getCode()); + CommentModel c = response.getResult(); assertEquals(commentReq.getContent(), c.getContent()); }); }