From 223ecf2c81b3edd890f26f11e308a8fcda935078 Mon Sep 17 00:00:00 2001 From: yidao620 Date: Sun, 4 Mar 2018 16:40:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84springboot-restful=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-restful/pom.xml | 11 ++ .../pos/controller/LoginController.java | 40 ------ .../com/xncoding/pos/model/BaseResponse.java | 2 +- .../com/xncoding/pos/model/LoginParam.java | 59 -------- .../java/com/xncoding/pos/model/User.java | 47 ++++++ .../com/xncoding/pos/util/JacksonUtil.java | 45 ++++++ .../com/xncoding/pos/ApplicationTests.java | 134 ++++++++++++++++++ 7 files changed, 238 insertions(+), 100 deletions(-) delete mode 100644 springboot-restful/src/main/java/com/xncoding/pos/controller/LoginController.java delete mode 100644 springboot-restful/src/main/java/com/xncoding/pos/model/LoginParam.java create mode 100644 springboot-restful/src/main/java/com/xncoding/pos/model/User.java create mode 100644 springboot-restful/src/main/java/com/xncoding/pos/util/JacksonUtil.java create mode 100644 springboot-restful/src/test/java/com/xncoding/pos/ApplicationTests.java diff --git a/springboot-restful/pom.xml b/springboot-restful/pom.xml index 794f25b..406cb03 100644 --- a/springboot-restful/pom.xml +++ b/springboot-restful/pom.xml @@ -40,6 +40,17 @@ org.springframework.boot spring-boot-starter-jetty + + org.springframework.boot + spring-boot-starter-test + test + + + org.hamcrest + hamcrest-all + 1.3 + test + diff --git a/springboot-restful/src/main/java/com/xncoding/pos/controller/LoginController.java b/springboot-restful/src/main/java/com/xncoding/pos/controller/LoginController.java deleted file mode 100644 index 7ec4e60..0000000 --- a/springboot-restful/src/main/java/com/xncoding/pos/controller/LoginController.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.xncoding.pos.controller; - -import com.xncoding.pos.model.LoginParam; -import com.xncoding.pos.model.UnbindParam; -import com.xncoding.pos.model.BaseResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RestController; - -/** - * 登录接口类 - */ -@RestController -public class LoginController { - - private static final Logger _logger = LoggerFactory.getLogger(LoginController.class); - - @PostMapping("/login") - public BaseResponse login(@RequestHeader(name = "Content-Type", defaultValue = "application/json") String contentType, - @RequestBody LoginParam loginParam) { - _logger.info("用户请求登录获取Token"); - String username = loginParam.getUsername(); - String password = loginParam.getPassword(); - return new BaseResponse<>(true, "Login success", username + password); - } - - @PostMapping("/unbind") - public BaseResponse unbind(@RequestHeader(name = "Content-Type", defaultValue = "application/json") String contentType, - @RequestHeader(name = "Authorization", defaultValue = "token") String token, - @RequestBody UnbindParam unbindParam) { - _logger.info("解绑通知接口start"); - String imei = unbindParam.getImei(); - String location = unbindParam.getLocation(); - return new BaseResponse<>(true, "解绑通知发送成功", "unbind"); - } - -} diff --git a/springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java b/springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java index b7c3e5e..4a66a03 100644 --- a/springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java +++ b/springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java @@ -1,7 +1,7 @@ package com.xncoding.pos.model; /** - * Controller的基础返回类 + * 基础返回类 * * @author XiongNeng * @version 1.0 diff --git a/springboot-restful/src/main/java/com/xncoding/pos/model/LoginParam.java b/springboot-restful/src/main/java/com/xncoding/pos/model/LoginParam.java deleted file mode 100644 index 466e8e3..0000000 --- a/springboot-restful/src/main/java/com/xncoding/pos/model/LoginParam.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.xncoding.pos.model; - -/** - * 登录认证接口参数 - * - * @author XiongNeng - * @version 1.0 - * @since 2018/1/9 - */ -public class LoginParam { - /** - * 用户名 - */ - private String username; - /** - * 密码 - */ - private String password; - /** - * Application ID - */ - private String appid; - /** - * IMEI码 - */ - private String imei; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getAppid() { - return appid; - } - - public void setAppid(String appid) { - this.appid = appid; - } - - public String getImei() { - return imei; - } - - public void setImei(String imei) { - this.imei = imei; - } -} diff --git a/springboot-restful/src/main/java/com/xncoding/pos/model/User.java b/springboot-restful/src/main/java/com/xncoding/pos/model/User.java new file mode 100644 index 0000000..f758c3d --- /dev/null +++ b/springboot-restful/src/main/java/com/xncoding/pos/model/User.java @@ -0,0 +1,47 @@ +package com.xncoding.pos.model; + +/** + * 用户 + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/4 + */ +public class User { + private Long id; + private String name; + private Integer age; + + public User() { + } + + public User(Long id, String name, Integer age) { + this.id = id; + this.name = name; + this.age = age; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } +} diff --git a/springboot-restful/src/main/java/com/xncoding/pos/util/JacksonUtil.java b/springboot-restful/src/main/java/com/xncoding/pos/util/JacksonUtil.java new file mode 100644 index 0000000..89b7e30 --- /dev/null +++ b/springboot-restful/src/main/java/com/xncoding/pos/util/JacksonUtil.java @@ -0,0 +1,45 @@ +package com.xncoding.pos.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; + +/** + * JacksonUtil + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/4 + */ +public class JacksonUtil { + private static ObjectMapper mapper = new ObjectMapper(); + + public static String bean2Json(Object obj) { + try { + return mapper.writeValueAsString(obj); + } catch (JsonProcessingException e) { + e.printStackTrace(); + return null; + } + } + +// public static T json2Bean(String jsonStr, Class objClass) { +// try { +// return mapper.readValue(jsonStr, objClass); +// } catch (IOException e) { +// e.printStackTrace(); +// return null; +// } +// } + + public static T json2Bean(String jsonStr, TypeReference typeReference) { + try { + return mapper.readValue(jsonStr, typeReference); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/springboot-restful/src/test/java/com/xncoding/pos/ApplicationTests.java b/springboot-restful/src/test/java/com/xncoding/pos/ApplicationTests.java new file mode 100644 index 0000000..a1d41aa --- /dev/null +++ b/springboot-restful/src/test/java/com/xncoding/pos/ApplicationTests.java @@ -0,0 +1,134 @@ +package com.xncoding.pos; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.xncoding.pos.model.BaseResponse; +import com.xncoding.pos.model.User; +import com.xncoding.pos.util.JacksonUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.RequestBuilder; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.Matchers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * ApplicationTests + * + * @author XiongNeng + * @version 1.0 + * @since 2018/3/4 + */ + +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class ApplicationTests { + @Autowired + private MockMvc mvc; + + @Test + public void testUserController() throws Exception { + // 测试UserController + RequestBuilder request; + + // 1、get查一下user列表,应该为空 + request = get("/users/"); + MvcResult result = mvc.perform(request) + .andExpect(status().isOk()) + .andReturn(); + String content = result.getResponse().getContentAsString(); + BaseResponse> response = JacksonUtil.json2Bean(content, new TypeReference>>() {}); + assertThat(response.isSuccess(), is(true)); + assertThat(response.getMsg(), is("查询列表成功")); + assertThat(((List) response.getData()).size(), is(0)); + + // 2、post提交一个user + request = post("/users/") + .param("id", "1") + .param("name", "测试大师") + .param("age", "20"); + result = mvc.perform(request) + .andExpect(status().isOk()) + .andReturn(); + content = result.getResponse().getContentAsString(); + BaseResponse response1 = JacksonUtil.json2Bean(content, new TypeReference>() {}); + assertThat(response1.isSuccess(), is(true)); + assertThat(response1.getMsg(), is("新增成功")); + + // 3、get获取user列表,应该有刚才插入的数据 + request = get("/users/"); + result = mvc.perform(request) + .andExpect(status().isOk()) + .andReturn(); + content = result.getResponse().getContentAsString(); + BaseResponse> response2 = JacksonUtil.json2Bean(content, new TypeReference>>() {}); + assertThat(response2.isSuccess(), is(true)); + assertThat(response2.getMsg(), is("查询列表成功")); + assertThat((response2.getData()).size(), is(1)); + + // 4、put修改id为1的user + request = put("/users/1") + .param("name", "测试终极大师") + .param("age", "30"); + result = mvc.perform(request) + .andExpect(status().isOk()) + .andReturn(); + content = result.getResponse().getContentAsString(); + BaseResponse response3 = JacksonUtil.json2Bean(content, new TypeReference>() {}); + assertThat(response3.isSuccess(), is(true)); + assertThat(response3.getMsg(), is("更新成功")); + + // 5、get一个id为1的user + request = get("/users/1"); + result = mvc.perform(request) + .andExpect(status().isOk()) + .andReturn(); + content = result.getResponse().getContentAsString(); + BaseResponse response4 = JacksonUtil.json2Bean(content, new TypeReference>() {}); + assertThat(response4.isSuccess(), is(true)); + assertThat(response4.getMsg(), is("查询成功")); + User user = response4.getData(); + assertThat(user.getId(), is(1L)); + assertThat(user.getName(), is("测试终极大师")); + + // 6、del删除id为1的user + request = delete("/users/1"); + result = mvc.perform(request) + .andExpect(status().isOk()) + .andReturn(); + content = result.getResponse().getContentAsString(); + BaseResponse response5 = JacksonUtil.json2Bean(content, new TypeReference>() {}); + assertThat(response5.isSuccess(), is(true)); + assertThat(response5.getMsg(), is("删除成功")); + + // 7、get查一下user列表,应该为空 + request = get("/users/"); + result = mvc.perform(request) + .andExpect(status().isOk()) + .andReturn(); + content = result.getResponse().getContentAsString(); + BaseResponse> response6 = JacksonUtil.json2Bean(content, new TypeReference>>() {}); + assertThat(response6.isSuccess(), is(true)); + assertThat(response6.getMsg(), is("查询列表成功")); + assertThat((response6.getData()).size(), is(0)); + } + +}