Skip to content

Commit 0769989

Browse files
committed
test: optimize 'api/topic/reply' (post) release reply api success unit test, add exception test
1 parent 0a32889 commit 0769989

File tree

1 file changed

+100
-30
lines changed

1 file changed

+100
-30
lines changed

src/test/test/org/neusoft/neubbs/api/TopicControllerTest.java

Lines changed: 100 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public void testGetTopicCategoryListSuccess() throws Exception {
569569
}
570570

571571
/**
572-
* 测试 /api/topic (Post
572+
* 测试 /api/topic (POST
573573
* - 测试发布话题成功
574574
* - 需要权限:@LoginAuthorization @AccountActivation
575575
*/
@@ -619,7 +619,7 @@ public void testReleaseTopicSuccess() throws Exception {
619619
}
620620

621621
/**
622-
* 测试 /api/topic (Post
622+
* 测试 /api/topic (POST
623623
* - 测试发布话题异常
624624
* - permission exception
625625
* - [✔] no login
@@ -655,11 +655,11 @@ public void testReleaseTopicException() throws Exception {
655655
for (String[] param: params) {
656656
if ("".equals(param[2])) {
657657
//test topic content length > 100000 error
658-
StringBuilder sb = new StringBuilder();
658+
StringBuilder strBdr = new StringBuilder();
659659
for (int i = 1; i <= 100001; i++) {
660-
sb.append("a");
660+
strBdr.append("a");
661661
}
662-
param[2] = sb.toString();
662+
param[2] = strBdr.toString();
663663
}
664664

665665
//build request-body
@@ -688,16 +688,18 @@ public void testReleaseTopicException() throws Exception {
688688
}
689689

690690
/**
691-
* 【/api/topic/reply (http post)】 test publish topic reply success
691+
* 测试 /api/topic/reply (POST)
692+
* - 发布话题回复成功
693+
* - 需要权限: @LoginAuthorization @AccountActivation
692694
*/
693695
@Test
694696
@Transactional
695-
public void testPublishTopicReplySuccess() throws Exception {
697+
public void testReleaseTopicReplySuccess() throws Exception {
696698
int topicId = 1;
697699
String content = "new reply content";
698-
String requestBodyJson = "{" + util.getJsonField("topicid", topicId) + ","
700+
String requestBody = "{" + util.getJsonField("topicid", topicId) + ","
699701
+ util.getJsonField("content", content) + "}";
700-
System.out.println("input request-body: " + requestBodyJson);
702+
System.out.println("input request-body: " + requestBody);
701703

702704
//before publish reply
703705
TopicDO beforeTopic = topicDAO.getTopicById(topicId);
@@ -707,50 +709,118 @@ public void testPublishTopicReplySuccess() throws Exception {
707709
MockMvcRequestBuilders.post("/api/topic/reply")
708710
.cookie(util.getAlreadyLoginUserCookie())
709711
.contentType(MediaType.APPLICATION_JSON)
710-
.content(requestBodyJson)
712+
.content(requestBody)
713+
.accept(MediaType.APPLICATION_JSON)
711714
).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true))
712715
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value(""))
713716
.andExpect(MockMvcResultMatchers.jsonPath("$.model").exists())
714717
.andReturn();
715718

716719
Map resultMap = (Map) JSON.parse(result.getResponse().getContentAsString());
717720

718-
//$.model.replyid
721+
//judge $.model.replyid
719722
Map modelMap = (Map) resultMap.get("model");
720-
int replyId = (Integer) modelMap.get("replyid");
721-
722-
//compare database reply information
723-
TopicReplyDO reply = topicReplyDAO.getTopicReplyById(replyId);
724723

724+
//validate reply information
725+
TopicReplyDO reply = topicReplyDAO.getTopicReplyById((Integer) modelMap.get("replyid"));
725726
Assert.assertEquals(topicId, (int) reply.getTopicid());
726727
Assert.assertEquals(content, reply.getContent());
727728

729+
//validate topic information
728730
TopicDO afterTopic = topicDAO.getTopicById(reply.getTopicid());
729731
Assert.assertNotNull(afterTopic);
730-
731-
//compare insert reply before and after
732732
Assert.assertEquals(beforeTopic.getReplies() + 1, (int) afterTopic.getReplies());
733-
Assert.assertNotNull(afterTopic.getLastreplyuserid());
734733
Assert.assertNotEquals(beforeTopic.getLastreplytime(), afterTopic.getLastreplytime());
735-
736734
Assert.assertEquals(reply.getCreatetime(), afterTopic.getLastreplytime());
737735

738736
util.printSuccessMessage();
739737
}
740738

741739
/**
742-
* 【undone】
743-
* 【/api/topic/reply (http post)】 test publish topic reply throw exception
744-
* - permission exception
745-
* - [ ] no login
746-
* - [ ] account no activate
747-
* - request param error, no norm
748-
* - [ ] null
749-
* - [ ] param norm
750-
* - database exception
751-
* - [ ] no topic
752-
* - [ ] no user (get user from cookie)
740+
* 测试 /api/topic/reply
741+
* - 测试发布话题回复异常
742+
* - no permission
743+
* - [] no login
744+
* - [✔] the account not activated
745+
* - request param error, no norm
746+
* - [] null
747+
* - [] param norm
748+
* - service exception
749+
* - [ ] no topic
750+
* - [ ] no user (get user from cookie)
753751
*/
752+
@Test
753+
@Transactional
754+
public void testReleaseTopicTopicReply() throws Exception {
755+
//no login
756+
util.testApiThrowNoPermissionException("/api/topic/reply", RequestMethod.POST, null);
757+
758+
//the account not activated
759+
util.testApiThrowNoPermissionException("/api/topic/reply", RequestMethod.POST, util.getNoActivatedUserDO());
760+
761+
//request param error
762+
String[][] params = {{null, null}, {null, "new topic content"}, {"1", null}};
763+
764+
for (String[] param: params) {
765+
String requestBody = "{"
766+
+ util.getJsonField("topicid", (param[0] == null ? null : Integer.parseInt(param[0]))) + ","
767+
+ util.getJsonField("content", param[1]) + "}";
768+
System.out.println("input request-body: " + requestBody);
769+
770+
try {
771+
mockMvc.perform(
772+
MockMvcRequestBuilders.post("/api/topic/reply")
773+
.cookie(util.getAlreadyLoginUserCookie())
774+
.contentType(MediaType.APPLICATION_JSON)
775+
.content(requestBody)
776+
.accept(MediaType.APPLICATION_JSON)
777+
).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(false))
778+
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value(ApiMessage.PARAM_ERROR))
779+
.andExpect(MockMvcResultMatchers.jsonPath("$.model").value(CoreMatchers.notNullValue()));
780+
781+
} catch (NestedServletException ne) {
782+
Assert.assertTrue(ne.getRootCause() instanceof ParamsErrorException);
783+
Assert.assertEquals(ApiMessage.PARAM_ERROR, ne.getRootCause().getMessage());
784+
}
785+
}
786+
787+
//request param 'topicId' no number type
788+
try {
789+
mockMvc.perform(
790+
MockMvcRequestBuilders.post("/api/topic/reply")
791+
.cookie(util.getAlreadyLoginUserCookie())
792+
.contentType(MediaType.APPLICATION_JSON)
793+
.content("{" + util.getJsonField("topicid", "1") + "," + util.getJsonField("content", "content") + "}")
794+
).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(false))
795+
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value(ApiMessage.UNKNOWN_ERROR))
796+
.andExpect(MockMvcResultMatchers.jsonPath("$.model").value(CoreMatchers.notNullValue()));
797+
798+
} catch (NestedServletException ne) {
799+
Assert.assertTrue(ne.getRootCause() instanceof ClassCastException);
800+
}
801+
802+
//request param 'content' String length
803+
StringBuilder strBdr = new StringBuilder();
804+
for (int i = 1; i <= 151; i ++) {
805+
strBdr.append("a");
806+
}
807+
try {
808+
mockMvc.perform(
809+
MockMvcRequestBuilders.post("/api/topic/reply")
810+
.cookie(util.getAlreadyLoginUserCookie())
811+
.contentType(MediaType.APPLICATION_JSON)
812+
.content("{" + util.getJsonField("topicid", 1) + "," + util.getJsonField("content", strBdr.toString()) + "}")
813+
).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(false))
814+
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value(ApiMessage.PARAM_ERROR))
815+
.andExpect(MockMvcResultMatchers.jsonPath("$.model").value(CoreMatchers.notNullValue()));
816+
817+
} catch (NestedServletException ne) {
818+
Assert.assertTrue(ne.getRootCause() instanceof ParamsErrorException);
819+
Assert.assertEquals(ApiMessage.PARAM_ERROR, ne.getRootCause().getMessage());
820+
}
821+
822+
util.printSuccessMessage();
823+
}
754824

755825
/**
756826
* 【/api/topic-remove】test remove topic success

0 commit comments

Comments
 (0)