1313import org .springframework .test .context .junit4 .SpringRunner ;
1414import org .springframework .test .web .servlet .MockMvc ;
1515
16+ import java .io .Serializable ;
1617import java .sql .Timestamp ;
1718import java .text .SimpleDateFormat ;
1819import java .util .TimeZone ;
@@ -34,21 +35,12 @@ public class ReviewControllerTests {
3435 @ MockBean
3536 private KafkaTemplate <String , String > kafkaTemplate ;
3637
37- public static String asJsonString (final Object obj ) {
38- try {
39- return new ObjectMapper ().writeValueAsString (obj );
40- } catch (Exception e ) {
41- throw new RuntimeException (e );
42- }
43- }
44-
4538 @ Test
4639 public void createReviewShouldReturnTheGivenReviewBack () throws Exception {
4740 String userId = "3" ;
48- Timestamp t = new Timestamp (10 );
49- String time = getTimeStringFormatted (t );
50- Review review = new Review (userId , "5" , 3.5F , t );
51- given (kafkaTemplate .sendDefault ("3,5,3.5,10" )).willReturn (null );
41+ Timestamp timestamp = new Timestamp (10 );
42+ String time = getTimeStringFormatted (timestamp );
43+ Review review = new Review (userId , "5" , 3.5F , timestamp );
5244 this .mockMvc .perform (post ("/users/" + userId + "/reviews" )
5345 .content (asJsonString (review ))
5446 .contentType (MediaType .APPLICATION_JSON_UTF8 )
@@ -64,6 +56,59 @@ public void createReviewShouldReturnTheGivenReviewBack() throws Exception {
6456 ));
6557 }
6658
59+ @ Test
60+ public void createReviewShouldReturnBadRequestSinceScoreIsNotValid () throws Exception {
61+ String userId = "3" ;
62+ String productId = "5" ;
63+ float score = 6F ;
64+ Timestamp timestamp = new Timestamp (10 );
65+ Review review = new Review (userId , productId , score , timestamp );
66+ this .mockMvc .perform (post ("/users/" + userId + "/reviews" )
67+ .content (asJsonString (review ))
68+ .contentType (MediaType .APPLICATION_JSON_UTF8 )
69+ .accept (MediaType .APPLICATION_JSON_UTF8 ))
70+ .andDo (print ())
71+ .andExpect (status ().isBadRequest ());
72+ }
73+
74+ @ Test
75+ public void createReviewShouldReturnNotFoundSinceUserIdIsEmpty () throws Exception {
76+ String userId = "" ;
77+ Timestamp timestamp = new Timestamp (10 );
78+ String time = getTimeStringFormatted (timestamp );
79+ Review review = new Review (userId , "5" , 3.5F , timestamp );
80+ this .mockMvc .perform (post ("/users/" + userId + "/reviews" )
81+ .content (asJsonString (review ))
82+ .contentType (MediaType .APPLICATION_JSON_UTF8 )
83+ .accept (MediaType .APPLICATION_JSON_UTF8 ))
84+ .andDo (print ())
85+ .andExpect (status ().isNotFound ());
86+ }
87+
88+ @ Test
89+ public void createReviewShouldReturnBadRequestSinceAllFieldsDoNotExist () throws Exception {
90+ String uId = "3" ;
91+ Object review = new Object () {
92+ public final String userId = uId ;
93+ public final String productId = "5" ;
94+ public final Timestamp timestamp = new Timestamp (10 );
95+ };
96+ this .mockMvc .perform (post ("/users/" + uId + "/reviews" )
97+ .content (asJsonString (review ))
98+ .contentType (MediaType .APPLICATION_JSON_UTF8 )
99+ .accept (MediaType .APPLICATION_JSON_UTF8 ))
100+ .andDo (print ())
101+ .andExpect (status ().isBadRequest ());
102+ }
103+
104+ public static String asJsonString (final Object obj ) {
105+ try {
106+ return new ObjectMapper ().writeValueAsString (obj );
107+ } catch (Exception e ) {
108+ throw new RuntimeException (e );
109+ }
110+ }
111+
67112 private String getTimeStringFormatted (Timestamp time ) {
68113 String timeStringFormatted ;
69114 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" );
0 commit comments