|
| 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