Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.73 KB | None | 0 0
  1. package core;
  2.  
  3. import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
  4. import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
  5. import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
  6. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  7. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  8. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  9. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  10. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
  11. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  12. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  13.  
  14. import javax.inject.Inject;
  15.  
  16. import org.junit.Before;
  17. import org.junit.FixMethodOrder;
  18. import org.junit.Rule;
  19. import org.junit.Test;
  20. import org.junit.runner.RunWith;
  21. import org.junit.runners.MethodSorters;
  22. import org.springframework.http.MediaType;
  23. import org.springframework.restdocs.RestDocumentation;
  24. import org.springframework.test.context.ContextConfiguration;
  25. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  26. import org.springframework.test.context.web.WebAppConfiguration;
  27. import org.springframework.test.web.servlet.MockMvc;
  28. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  29. import org.springframework.web.context.WebApplicationContext;
  30. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  31.  
  32. import com.fasterxml.jackson.databind.ObjectMapper;
  33.  
  34. import dbmapping.User;
  35. import security.Encryptor;
  36.  
  37.  
  38. @RunWith(SpringJUnit4ClassRunner.class)
  39. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  40. @EnableWebMvc
  41. @WebAppConfiguration
  42. @ContextConfiguration( locations = {"file:/d:/jdev/manyface/src/main/webapp/WEB-INF/manyface-servlet.xml","file:/d:/jdev/manyface/src/main/webapp/WEB-INF/spring-security-test.xml"})
  43. public class RestTest {
  44.  
  45.  
  46.  
  47. @Rule
  48. public RestDocumentation restDoc = new RestDocumentation("target/generated-snippets");
  49.  
  50. private MockMvc mock;
  51.  
  52.  
  53. @Inject
  54. private WebApplicationContext context;
  55.  
  56.  
  57. @Before
  58. public void setUp() throws Exception {
  59. mock = MockMvcBuilders.webAppContextSetup(this.context)
  60. .apply(springSecurity())
  61. .apply(documentationConfiguration(this.restDoc))
  62. .build();
  63.  
  64. }
  65.  
  66. @Test
  67. public void test01AuthAddTestOk() throws Exception {
  68. String user = "test";
  69. String password = "test";
  70. this.mock.perform(post("/api/v1/auth")
  71. .header("Login", user)
  72. .header("Password", password)
  73. .accept(MediaType.APPLICATION_JSON))
  74. .andExpect(status().isOk())
  75. .andDo(document("AuthAdd/ok"));
  76.  
  77.  
  78. this.mock.perform(post("/api/v1/auth")
  79. .header("Login", "test2")
  80. .header("Password", "test2")
  81. .accept(MediaType.APPLICATION_JSON))
  82. .andExpect(status().isOk());
  83. }
  84.  
  85. @Test
  86. public void test02AuthAddTestMissHeader() throws Exception {
  87.  
  88. this.mock.perform(post("/api/v1/auth")
  89. .accept(MediaType.APPLICATION_JSON))
  90. .andExpect(status().isNotAcceptable())
  91. .andDo(document("AuthAdd/miss"));
  92. }
  93.  
  94. @Test
  95. public void test03AuthAddExist() throws Exception {
  96. this.mock.perform(post("/api/v1/auth")
  97. .header("Login", "test")
  98. .header("Password", "test")
  99. .accept(MediaType.APPLICATION_JSON))
  100. .andExpect(status().isConflict())
  101. .andDo(document("AuthAdd/conflict"));
  102. }
  103.  
  104.  
  105. @Test
  106. public void test04AuthTokenOkTest() throws Exception {
  107. String user = "test";
  108. String password = "test";
  109. this.mock.perform(get("/api/v1/auth")
  110. .header("Login", user)
  111. .header("Password", password)
  112. .accept(MediaType.APPLICATION_JSON))
  113. .andExpect(status().isOk())
  114. .andExpect(header().string("AccessToken", Encryptor.encrypt(user+";"+password) ))
  115. .andDo(document("AuthGet/ok"));
  116. }
  117.  
  118. @Test
  119. public void test05AuthTokenNotAcceptTest() throws Exception {
  120. String user = ";buntu-em;";
  121. String password = ";rabotyagi!?";
  122. this.mock.perform(get("/api/v1/auth")
  123. .header("Login", user)
  124. .header("Password", password)
  125. .accept(MediaType.APPLICATION_JSON))
  126. .andExpect(status().isNotAcceptable())
  127. .andDo(document("AuthGet/error"));
  128. }
  129.  
  130. public String createToken(String user, String password) throws Exception{
  131. return Encryptor.encrypt(user+";"+password);
  132. }
  133.  
  134.  
  135. @Test
  136. public void test06UserGetByIdOkTest() throws Exception{
  137.  
  138. String user = "test";
  139. String password = "test";
  140. this.mock.perform(get("/api/v1/user/1")
  141. .header("AccessToken",createToken(user, password))
  142. .accept(MediaType.APPLICATION_JSON))
  143. .andExpect(status().isOk())
  144. .andDo(document("UserGetById/ok" ))
  145. .andReturn().getResponse();
  146. }
  147.  
  148. @Test
  149. public void test07UserAddOkTest() throws Exception{
  150.  
  151. String login = "test";
  152. String password = "test";
  153. ObjectMapper mapper = new ObjectMapper();
  154. User user = new User();
  155. user.setUsername("testUser");
  156. user.setDescription("test description");
  157.  
  158. this.mock.perform(post("/api/v1/user")
  159. .header("AccessToken",createToken(login, password))
  160. .contentType(MediaType.APPLICATION_JSON)
  161. .content(mapper.writeValueAsString(user))
  162. .accept(MediaType.APPLICATION_JSON))
  163. .andExpect(status().isOk())
  164. .andDo(document("UserAdd/ok" ))
  165. .andReturn().getResponse();
  166.  
  167.  
  168. user.setUsername("newUser");
  169. this.mock.perform(post("/api/v1/user")
  170. .header("AccessToken",createToken("test2", "test2"))
  171. .contentType(MediaType.APPLICATION_JSON)
  172. .content(mapper.writeValueAsString(user))
  173. .accept(MediaType.APPLICATION_JSON))
  174. .andExpect(status().isOk());
  175.  
  176.  
  177. user.setUsername("userExample");
  178. this.mock.perform(post("/api/v1/user")
  179. .header("AccessToken",createToken(login, password))
  180. .contentType(MediaType.APPLICATION_JSON)
  181. .content(mapper.writeValueAsString(user))
  182. .accept(MediaType.APPLICATION_JSON))
  183. .andExpect(status().isOk())
  184. .andDo(print());
  185. }
  186.  
  187. @Test
  188. public void test07UserAddPhoto() throws Exception {
  189.  
  190. this.mock.perform(post("/api/v1/user/1/photo")
  191. .header("AccessToken",createToken("test", "test"))
  192. .header("height",50)
  193. .header("width",50)
  194. .header("format","jpg")
  195. .contentType(MediaType.APPLICATION_JSON)
  196. .content("Some image data here")
  197. .accept(MediaType.APPLICATION_JSON))
  198. .andExpect(status().isOk())
  199. .andDo(document("UserPhotoAdd/ok" ));
  200. }
  201.  
  202.  
  203. @Test
  204. public void test07UserGetPhoto() throws Exception {
  205.  
  206. this.mock.perform(get("/api/v1/user/1/photo")
  207. .header("AccessToken",createToken("test", "test"))
  208. .contentType(MediaType.APPLICATION_JSON)
  209. .accept(MediaType.APPLICATION_JSON))
  210. .andExpect(status().isOk())
  211. .andDo(document("UserPhotoGet/ok" ));
  212. }
  213.  
  214.  
  215. @Test
  216. public void test08UserGetAllOkTest() throws Exception{
  217.  
  218. String user = "test";
  219. String password = "test";
  220. this.mock.perform(get("/api/v1/user")
  221. .header("AccessToken",createToken(user, password))
  222. .accept(MediaType.APPLICATION_JSON))
  223. .andExpect(status().isOk())
  224. .andDo(document("UserGetAll/ok" ))
  225. .andReturn().getResponse();
  226. }
  227.  
  228. @Test
  229. public void test09UserUpdateOkTest() throws Exception{
  230.  
  231. String login = "test";
  232. String password = "test";
  233. ObjectMapper mapper = new ObjectMapper();
  234. User user = new User();
  235. user.setUsername("testUser");
  236. user.setDescription("test description");
  237.  
  238. this.mock.perform(put("/api/v1/user/1")
  239. .header("AccessToken",createToken(login, password))
  240. .contentType(MediaType.APPLICATION_JSON)
  241. .content(mapper.writeValueAsString(user))
  242. .accept(MediaType.APPLICATION_JSON))
  243. .andExpect(status().isOk())
  244. .andDo(document("UserUpdate/ok" ))
  245. .andReturn().getResponse();
  246. }
  247.  
  248. @Test
  249. public void test10ContactsGetOkTest() throws Exception{
  250. String login = "test";
  251. String password = "test";
  252.  
  253. this.mock.perform(get("/api/v1/contact")
  254. .header("AccessToken",createToken(login, password))
  255. .header("user-id",1)
  256. .accept(MediaType.APPLICATION_JSON))
  257. .andExpect(status().isOk())
  258. .andDo(document("Contact/ok" ))
  259. .andReturn().getResponse();
  260.  
  261.  
  262. }
  263.  
  264.  
  265.  
  266.  
  267. @Test
  268. public void test11MessagesSendOkTest() throws Exception{
  269. String login = "test";
  270. String password = "test";
  271.  
  272. this.mock.perform(post("/api/v1/message/2")
  273. .header("AccessToken",createToken(login, password))
  274. .header("user-id",1)
  275. .content("Hello my friend!")
  276. .accept(MediaType.APPLICATION_JSON))
  277. .andExpect(status().isOk())
  278. .andDo(document("MessageSend/ok" ))
  279. .andReturn().getResponse();
  280.  
  281. this.mock.perform(post("/api/v1/message/1")
  282. .header("AccessToken",createToken("test2", "test2"))
  283. .header("user-id",2)
  284. .content("hi!")
  285. .accept(MediaType.APPLICATION_JSON))
  286. .andExpect(status().isOk());
  287.  
  288. this.mock.perform(post("/api/v1/message/2")
  289. .header("AccessToken",createToken(login, password))
  290. .header("user-id",1)
  291. .content("what a nice day to use this api!")
  292. .accept(MediaType.APPLICATION_JSON))
  293. .andExpect(status().isOk());
  294. }
  295.  
  296. @Test
  297. public void test12MessagesGetAllOkTest() throws Exception{
  298. String login = "test";
  299. String password = "test";
  300.  
  301. this.mock.perform(get("/api/v1/message")
  302. .header("AccessToken",createToken(login, password))
  303. .header("user-id",1)
  304. .accept(MediaType.APPLICATION_JSON))
  305. .andExpect(status().isOk())
  306. .andDo(document("MessagesAll/ok" ))
  307. .andReturn().getResponse();
  308.  
  309.  
  310. }
  311. @Test
  312. public void test13MessageGetOkTest() throws Exception{
  313. String login = "test";
  314. String password = "test";
  315.  
  316. this.mock.perform(get("/api/v1/message/2")
  317. .header("AccessToken",createToken(login, password))
  318. .header("user-id",1)
  319. .accept(MediaType.APPLICATION_JSON))
  320. .andExpect(status().isOk())
  321. .andDo(document("MessageGet/ok" ))
  322. .andReturn().getResponse();
  323. }
  324.  
  325. @Test
  326. public void test133MessageAfterDateTest() throws Exception{
  327. String login = "test";
  328. String password = "test";
  329.  
  330. this.mock.perform(get("/api/v1/message/2")
  331. .header("AccessToken",createToken(login, password))
  332. .header("user-id",1)
  333. .header("send_time","2015-01-01 23:59:59")
  334. .header("count",3)
  335. .accept(MediaType.APPLICATION_JSON))
  336. .andExpect(status().isOk())
  337. .andDo(document("MessagesDate/ok" ))
  338. .andReturn().getResponse();
  339.  
  340. }
  341.  
  342.  
  343. @Test
  344. public void test133MessagesBeforeDateTest() throws Exception{
  345. String login = "test";
  346. String password = "test";
  347.  
  348. this.mock.perform(get("/api/v1/message/2")
  349. .header("AccessToken",createToken(login, password))
  350. .header("user-id",1)
  351. .header("send_time","2018-01-01 23:59:59")
  352. .header("count",3)
  353. .header("reversed",true)
  354. .accept(MediaType.APPLICATION_JSON))
  355. .andExpect(status().isOk())
  356. .andDo(document("MessagesDateBefore/ok" ))
  357. .andReturn().getResponse();
  358.  
  359. }
  360.  
  361. @Test
  362. public void test14SearchIdOkTest() throws Exception{
  363. String login = "test";
  364. String password = "test";
  365.  
  366. this.mock.perform(get("/api/v1/search/id/1")
  367. .header("AccessToken",createToken(login, password))
  368. .accept(MediaType.APPLICATION_JSON))
  369. .andExpect(status().isOk())
  370. .andDo(document("SearchID/ok" ))
  371. .andReturn().getResponse();
  372. }
  373.  
  374. @Test
  375. public void test15SearchUsernameOkTest() throws Exception{
  376. String login = "test";
  377. String password = "test";
  378.  
  379. this.mock.perform(get("/api/v1/search/username/user")
  380. .header("AccessToken",createToken(login, password))
  381. .accept(MediaType.APPLICATION_JSON))
  382. .andExpect(status().isOk())
  383. .andDo(document("SearchUsername/ok" ))
  384. .andReturn().getResponse();
  385. }
  386.  
  387.  
  388.  
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement