SunControllerTest.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.book.server.controller;
  2. import com.book.dao.utils.TimeUtil;
  3. import com.book.server.utils.HttpUtils;
  4. import com.book.server.utils.JsonUtils;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  10. import org.springframework.boot.test.context.SpringBootTest;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.test.context.junit4.SpringRunner;
  13. import org.springframework.test.web.servlet.MockMvc;
  14. import org.springframework.test.web.servlet.MvcResult;
  15. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  16. import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
  17. import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
  18. import java.nio.charset.StandardCharsets;
  19. import java.util.Calendar;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  23. /**
  24. * created in 2021/9/28
  25. * Project: book-store
  26. *
  27. * @author win7
  28. */
  29. @Slf4j
  30. @RunWith(SpringRunner.class)
  31. @SpringBootTest
  32. @AutoConfigureMockMvc
  33. public class SunControllerTest {
  34. @Autowired
  35. private MockMvc mockMvc;
  36. @Autowired
  37. private SunDataController sunDataController;
  38. @Test
  39. public void testAdd() throws Exception {
  40. MockMvc mockMvc1 = mockMvc;
  41. MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/api/sun/secret/deciphering")
  42. .accept(MediaType.APPLICATION_JSON)
  43. .param("start_time", "2021-09-27 00:00:00")
  44. .param("end_time", "2021-09-28 00:00:00"))
  45. .andExpect(MockMvcResultMatchers.status().isOk())
  46. .andDo(MockMvcResultHandlers.print())
  47. .andReturn();
  48. log.info(mvcResult.getResponse().getContentAsString());
  49. }
  50. public void loadData(String from,String to) throws Exception{
  51. Calendar start = Calendar.getInstance();
  52. start.setTime(TimeUtil.dateFromStr(from,TimeUtil.YYYY_MM_DD_HH_MM_SS));
  53. Calendar end = Calendar.getInstance();
  54. end.setTime(TimeUtil.dateFromStr(to,TimeUtil.YYYY_MM_DD_HH_MM_SS));
  55. Map<String,String> map = new HashMap<>();
  56. map.put("start_time",from);
  57. map.put("end_time",to);
  58. String jsonStr = JsonUtils.toJsonStr(map);
  59. System.out.println(jsonStr);
  60. Map he = new HashMap();
  61. he.put("Content-Type","application/json");
  62. HttpUtils.sendPost("http://api.griacxv.cn/api/sun/getData/user_collect",he,jsonStr.getBytes(StandardCharsets.UTF_8));
  63. start.add(Calendar.HOUR_OF_DAY,1);
  64. end.add(Calendar.HOUR_OF_DAY,1);
  65. if ("2021-09-30 12:00:00".equals(TimeUtil.dateToStr(start.getTime(),TimeUtil.YYYY_MM_DD_HH_MM_SS))){
  66. return;
  67. }
  68. Thread.sleep(1000);
  69. loadData(TimeUtil.dateToStr(start.getTime(),TimeUtil.YYYY_MM_DD_HH_MM_SS),TimeUtil.dateToStr(end.getTime(),TimeUtil.YYYY_MM_DD_HH_MM_SS));
  70. }
  71. @Test
  72. public void load() throws Exception {
  73. loadData("2021-09-25 00:00:00","2021-09-25 01:00:00");
  74. }
  75. }