@@ -569,7 +569,7 @@ public void testGetTopicCategoryListSuccess() throws Exception {
569
569
}
570
570
571
571
/**
572
- * 测试 /api/topic (Post )
572
+ * 测试 /api/topic (POST )
573
573
* - 测试发布话题成功
574
574
* - 需要权限:@LoginAuthorization @AccountActivation
575
575
*/
@@ -619,7 +619,7 @@ public void testReleaseTopicSuccess() throws Exception {
619
619
}
620
620
621
621
/**
622
- * 测试 /api/topic (Post )
622
+ * 测试 /api/topic (POST )
623
623
* - 测试发布话题异常
624
624
* - permission exception
625
625
* - [✔] no login
@@ -655,11 +655,11 @@ public void testReleaseTopicException() throws Exception {
655
655
for (String [] param : params ) {
656
656
if ("" .equals (param [2 ])) {
657
657
//test topic content length > 100000 error
658
- StringBuilder sb = new StringBuilder ();
658
+ StringBuilder strBdr = new StringBuilder ();
659
659
for (int i = 1 ; i <= 100001 ; i ++) {
660
- sb .append ("a" );
660
+ strBdr .append ("a" );
661
661
}
662
- param [2 ] = sb .toString ();
662
+ param [2 ] = strBdr .toString ();
663
663
}
664
664
665
665
//build request-body
@@ -688,16 +688,18 @@ public void testReleaseTopicException() throws Exception {
688
688
}
689
689
690
690
/**
691
- * 【/api/topic/reply (http post)】 test publish topic reply success
691
+ * 测试 /api/topic/reply (POST)
692
+ * - 发布话题回复成功
693
+ * - 需要权限: @LoginAuthorization @AccountActivation
692
694
*/
693
695
@ Test
694
696
@ Transactional
695
- public void testPublishTopicReplySuccess () throws Exception {
697
+ public void testReleaseTopicReplySuccess () throws Exception {
696
698
int topicId = 1 ;
697
699
String content = "new reply content" ;
698
- String requestBodyJson = "{" + util .getJsonField ("topicid" , topicId ) + ","
700
+ String requestBody = "{" + util .getJsonField ("topicid" , topicId ) + ","
699
701
+ util .getJsonField ("content" , content ) + "}" ;
700
- System .out .println ("input request-body: " + requestBodyJson );
702
+ System .out .println ("input request-body: " + requestBody );
701
703
702
704
//before publish reply
703
705
TopicDO beforeTopic = topicDAO .getTopicById (topicId );
@@ -707,50 +709,118 @@ public void testPublishTopicReplySuccess() throws Exception {
707
709
MockMvcRequestBuilders .post ("/api/topic/reply" )
708
710
.cookie (util .getAlreadyLoginUserCookie ())
709
711
.contentType (MediaType .APPLICATION_JSON )
710
- .content (requestBodyJson )
712
+ .content (requestBody )
713
+ .accept (MediaType .APPLICATION_JSON )
711
714
).andExpect (MockMvcResultMatchers .jsonPath ("$.success" ).value (true ))
712
715
.andExpect (MockMvcResultMatchers .jsonPath ("$.message" ).value ("" ))
713
716
.andExpect (MockMvcResultMatchers .jsonPath ("$.model" ).exists ())
714
717
.andReturn ();
715
718
716
719
Map resultMap = (Map ) JSON .parse (result .getResponse ().getContentAsString ());
717
720
718
- //$.model.replyid
721
+ //judge $.model.replyid
719
722
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 );
724
723
724
+ //validate reply information
725
+ TopicReplyDO reply = topicReplyDAO .getTopicReplyById ((Integer ) modelMap .get ("replyid" ));
725
726
Assert .assertEquals (topicId , (int ) reply .getTopicid ());
726
727
Assert .assertEquals (content , reply .getContent ());
727
728
729
+ //validate topic information
728
730
TopicDO afterTopic = topicDAO .getTopicById (reply .getTopicid ());
729
731
Assert .assertNotNull (afterTopic );
730
-
731
- //compare insert reply before and after
732
732
Assert .assertEquals (beforeTopic .getReplies () + 1 , (int ) afterTopic .getReplies ());
733
- Assert .assertNotNull (afterTopic .getLastreplyuserid ());
734
733
Assert .assertNotEquals (beforeTopic .getLastreplytime (), afterTopic .getLastreplytime ());
735
-
736
734
Assert .assertEquals (reply .getCreatetime (), afterTopic .getLastreplytime ());
737
735
738
736
util .printSuccessMessage ();
739
737
}
740
738
741
739
/**
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)
753
751
*/
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
+ }
754
824
755
825
/**
756
826
* 【/api/topic-remove】test remove topic success
0 commit comments