· 6 years ago · Jun 03, 2019, 01:32 PM
1package com.fosspowered.peist.controller;
2
3import com.fasterxml.jackson.databind.ObjectMapper;
4import com.fosspowered.peist.model.json.PasteRequest;
5import org.junit.jupiter.api.Test;
6import org.junit.runner.RunWith;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
9import org.springframework.boot.test.context.SpringBootTest;
10import org.springframework.http.MediaType;
11import org.springframework.mock.web.MockHttpServletResponse;
12import org.springframework.test.context.junit4.SpringRunner;
13import org.springframework.test.web.servlet.MockMvc;
14import org.springframework.test.web.servlet.MvcResult;
15import org.springframework.test.web.servlet.ResultActions;
16
17import static org.hamcrest.Matchers.*;
18import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
20import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
21
22
23@RunWith(SpringRunner.class)
24@SpringBootTest
25@AutoConfigureMockMvc
26class PasteControllerTest {
27 private static final String TITLE = "";
28 private static final String LANGUAGE = "";
29 private static final String AUTHOR = "";
30 private static final String SECRET_KEY = "";
31 private static final String PASTE_DATA = "";
32
33 @Autowired
34 private MockMvc mvc;
35
36 @Autowired
37 private ObjectMapper objectMapper;
38
39 @Test
40 void ping() throws Exception {
41 this.mvc.perform(get("/paste/ping")).andExpect(status().isOk())
42 .andExpect(content().string("pong"));
43 }
44
45 @Test
46 void add() throws Exception {
47 String pasteData = "std::cout<<\"Hello, world.\"";
48 String title = "C++ print statement";
49 String language = "C++";
50 String author = "The Jackass";
51 PasteRequest pasteRequest = PasteRequest.builder()
52 .title(title)
53 .language(language)
54 .author(author)
55 .isVisible(true)
56 .secretKey(null)
57 .pasteData(pasteData)
58 .build();
59 this.mvc.perform(post("/paste")
60 .contentType(MediaType.APPLICATION_JSON)
61 .content(objectMapper.writeValueAsString(pasteRequest)))
62 .andExpect(status().isCreated())
63 .andExpect(jsonPath(".urlId", not(isEmptyOrNullString())))
64 .andExpect(jsonPath("title", is(title)))
65 .andExpect(jsonPath("author", is(author)))
66 .andExpect(jsonPath("language", is(language)))
67 .andExpect(jsonPath("creationDate", is(notNullValue())))
68 .andExpect(jsonPath("pasteData", is(pasteData)));
69 }
70
71 @Test
72 void getPaste() {
73 String pasteData = "import antigravity";
74 String title = "I am flying";
75 String language = "Python";
76 String author = "The Zen";
77 PasteRequest pasteRequest = PasteRequest.builder()
78 .title(title)
79 .language(language)
80 .author(author)
81 .isVisible(true)
82 .secretKey(null)
83 .pasteData(pasteData)
84 .build();
85 ResultActions resultActions = this.mvc.perform(post("/paste");
86
87 }
88}