Skip to content

Commit 2e1c95f

Browse files
committed
Test for ReviewController is added.
1 parent 6b28b6f commit 2e1c95f

File tree

4 files changed

+136
-4
lines changed

4 files changed

+136
-4
lines changed

src/main/java/com/trendyol/recomengine/webservice/controllers/ReviewController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.trendyol.recomengine.webservice.controllers;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.trendyol.recomengine.webservice.engine.Producer;
45
import com.trendyol.recomengine.webservice.resource.Review;
56
import com.trendyol.recomengine.webservice.resource.ReviewWithoutUserId;

src/main/java/com/trendyol/recomengine/webservice/resource/Review.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.trendyol.recomengine.webservice.resource;
22

33
import javax.validation.constraints.NotBlank;
4+
import java.sql.Timestamp;
45
import java.util.Date;
56

67
/**
@@ -17,7 +18,7 @@ public class Review extends ReviewWithoutUserId {
1718
* @param score Reviewed score
1819
* @param time Review timestamp
1920
*/
20-
public Review(String userId, String productId, float score, Date time) {
21+
public Review(String userId, String productId, float score, Timestamp time) {
2122
super(productId, score, time);
2223
this.userId = userId;
2324
}

src/main/java/com/trendyol/recomengine/webservice/resource/ReviewWithoutUserId.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import javax.validation.constraints.Max;
55
import javax.validation.constraints.NotBlank;
66
import javax.validation.constraints.NotNull;
7+
import java.sql.Timestamp;
78
import java.util.Date;
89

910
/**
@@ -19,14 +20,14 @@ public class ReviewWithoutUserId {
1920
private final float score;
2021

2122
@NotNull
22-
private final Date timestamp;
23+
private final Timestamp timestamp;
2324

2425
/**
2526
* @param productId Reviewed product's id
2627
* @param score Reviewed score
2728
* @param timestamp Review timestamp
2829
*/
29-
ReviewWithoutUserId(String productId, float score, Date timestamp) {
30+
ReviewWithoutUserId(String productId, float score, Timestamp timestamp) {
3031
this.productId = productId;
3132
this.score = score;
3233
this.timestamp = timestamp;
@@ -49,7 +50,7 @@ public float getScore() {
4950
/**
5051
* @return Review timestamp in seconds
5152
*/
52-
public Date getTimestamp() {
53+
public Timestamp getTimestamp() {
5354
return timestamp;
5455
}
5556

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.trendyol.recomengine.webservice.controllers;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.trendyol.recomengine.webservice.resource.Review;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.Mockito;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.boot.test.mock.mockito.MockBean;
13+
import org.springframework.http.MediaType;
14+
import org.springframework.kafka.core.KafkaTemplate;
15+
import org.springframework.kafka.support.SendResult;
16+
import org.springframework.test.context.junit4.SpringRunner;
17+
import org.springframework.test.web.servlet.MockMvc;
18+
import org.springframework.util.concurrent.FailureCallback;
19+
import org.springframework.util.concurrent.ListenableFuture;
20+
import org.springframework.util.concurrent.ListenableFutureCallback;
21+
import org.springframework.util.concurrent.SuccessCallback;
22+
23+
import java.sql.Timestamp;
24+
import java.text.SimpleDateFormat;
25+
import java.time.ZoneId;
26+
import java.util.TimeZone;
27+
import java.util.concurrent.ExecutionException;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.TimeoutException;
30+
31+
import static org.springframework.test.web.servlet.ResultMatcher.matchAll;
32+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
33+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
34+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
35+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
36+
37+
@RunWith(SpringRunner.class)
38+
@SpringBootTest
39+
@AutoConfigureMockMvc
40+
public class ReviewControllerTests {
41+
42+
@Autowired
43+
private MockMvc mockMvc;
44+
45+
@MockBean
46+
private KafkaTemplate<String, String> kafkaTemplate;
47+
48+
@Test
49+
public void createReviewShouldReturnTheGivenReviewBack() throws Exception {
50+
String userId = "3";
51+
Timestamp t = new Timestamp(10);
52+
String time = getTimeStringFormatted(t);
53+
Review review = new Review(userId, "5", 3.5F, t);
54+
55+
this.mockMvc.perform(post("/users/"+userId+"/reviews")
56+
.content(asJsonString(review))
57+
.contentType(MediaType.APPLICATION_JSON_UTF8)
58+
.accept(MediaType.APPLICATION_JSON_UTF8))
59+
.andDo(print())
60+
.andExpect(matchAll(
61+
status().isOk(),
62+
content().contentType(MediaType.APPLICATION_JSON_UTF8),
63+
jsonPath("$.userId").value(userId),
64+
jsonPath("$.productId").value(review.getProductId()),
65+
jsonPath("$.score").value(review.getScore()),
66+
jsonPath("$.timestamp").value(time)
67+
));
68+
}
69+
70+
private String getTimeStringFormatted(Timestamp time) {
71+
String timeStringFormatted;
72+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
73+
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
74+
System.out.println("GMT time: " + sdf.format(time));
75+
timeStringFormatted = sdf.format(time);
76+
return timeStringFormatted;
77+
}
78+
79+
public static String asJsonString(final Object obj) {
80+
try {
81+
return new ObjectMapper().writeValueAsString(obj);
82+
} catch (Exception e) {
83+
throw new RuntimeException(e);
84+
}
85+
}
86+
87+
@Before
88+
public void setUp() {
89+
Mockito.when(kafkaTemplate.sendDefault("3,5,3.5,10")).thenReturn(new ListenableFuture<SendResult<String, String>>() {
90+
@Override
91+
public void addCallback(ListenableFutureCallback<? super SendResult<String, String>> callback) {
92+
93+
}
94+
95+
@Override
96+
public void addCallback(SuccessCallback<? super SendResult<String, String>> successCallback, FailureCallback failureCallback) {
97+
98+
}
99+
100+
@Override
101+
public boolean cancel(boolean b) {
102+
return false;
103+
}
104+
105+
@Override
106+
public boolean isCancelled() {
107+
return false;
108+
}
109+
110+
@Override
111+
public boolean isDone() {
112+
return false;
113+
}
114+
115+
@Override
116+
public SendResult<String, String> get() throws InterruptedException, ExecutionException {
117+
return null;
118+
}
119+
120+
@Override
121+
public SendResult<String, String> get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
122+
return null;
123+
}
124+
});/*.then((Answer<String>) invocation -> {
125+
System.out.println("3,5,3.5,10 -- printed");
126+
return "Do nothing.";
127+
});*/
128+
}
129+
}

0 commit comments

Comments
 (0)