切换到jackson #10
@@ -2,51 +2,34 @@ package cn.celess.blog.controller;
|
|||||||
|
|
||||||
import cn.celess.blog.BaseTest;
|
import cn.celess.blog.BaseTest;
|
||||||
import cn.celess.blog.entity.Category;
|
import cn.celess.blog.entity.Category;
|
||||||
|
import cn.celess.blog.entity.Response;
|
||||||
import cn.celess.blog.entity.model.CategoryModel;
|
import cn.celess.blog.entity.model.CategoryModel;
|
||||||
|
import cn.celess.blog.entity.model.PageData;
|
||||||
import cn.celess.blog.mapper.CategoryMapper;
|
import cn.celess.blog.mapper.CategoryMapper;
|
||||||
import net.sf.json.JSONArray;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import net.sf.json.JSONObject;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||||
|
|
||||||
public class CategoryControllerTest extends BaseTest {
|
public class CategoryControllerTest extends BaseTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
CategoryMapper categoryMapper;
|
CategoryMapper categoryMapper;
|
||||||
|
private static final TypeReference<?> CATEGORY_MODEL_TYPE = new TypeReference<Response<CategoryModel>>() {
|
||||||
|
};
|
||||||
|
private static final TypeReference<?> CATEGORY_MODEL_PAGE_TYPE = new TypeReference<Response<PageData<CategoryModel>>>() {
|
||||||
|
};
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addOne() throws Exception {
|
public void addOne() throws Exception {
|
||||||
String categoryName = randomStr(4);
|
String categoryName = randomStr(4);
|
||||||
System.out.println("categoryName: ==> " + categoryName);
|
getMockData(post("/admin/category/create?name=" + categoryName), adminLogin()).andDo(result -> {
|
||||||
// 未登录
|
Response<CategoryModel> response = getResponse(result, CATEGORY_MODEL_TYPE);
|
||||||
mockMvc.perform(post("/admin/category/create?name=" + categoryName)).andExpect(status().isOk())
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
.andDo(result -> {
|
CategoryModel category = response.getResult();
|
||||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
|
||||||
});
|
|
||||||
// User权限
|
|
||||||
String token = userLogin();
|
|
||||||
mockMvc.perform(post("/admin/category/create?name=" + categoryName)
|
|
||||||
.header("Authorization", token))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andDo(result -> {
|
|
||||||
assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
|
||||||
});
|
|
||||||
// Admin权限
|
|
||||||
token = adminLogin();
|
|
||||||
mockMvc.perform(post("/admin/category/create?name=" + categoryName)
|
|
||||||
.header("Authorization", token))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andDo(result -> {
|
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
|
||||||
CategoryModel category = (CategoryModel) JSONObject.toBean(object.getJSONObject(Result), CategoryModel.class);
|
|
||||||
assertEquals(categoryName, category.getName());
|
assertEquals(categoryName, category.getName());
|
||||||
assertNotNull(category.getId());
|
assertNotNull(category.getId());
|
||||||
assertNotEquals(0, category.getArticles());
|
assertNotEquals(0, category.getArticles());
|
||||||
@@ -56,28 +39,10 @@ public class CategoryControllerTest extends BaseTest {
|
|||||||
@Test
|
@Test
|
||||||
public void deleteOne() throws Exception {
|
public void deleteOne() throws Exception {
|
||||||
Category category = categoryMapper.getLastestCategory();
|
Category category = categoryMapper.getLastestCategory();
|
||||||
// 未登录
|
getMockData(delete("/admin/category/del?id=" + category.getId()), adminLogin()).andDo(result -> {
|
||||||
mockMvc.perform(delete("/admin/category/del?id=" + category.getId())).andExpect(status().isOk())
|
Response<Boolean> response = getResponse(result, BOOLEAN_TYPE);
|
||||||
.andDo(result -> {
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
assertTrue(response.getResult());
|
||||||
});
|
|
||||||
// User权限
|
|
||||||
String token = userLogin();
|
|
||||||
mockMvc.perform(delete("/admin/category/del?id=" + category.getId())
|
|
||||||
.header("Authorization", token))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andDo(result -> {
|
|
||||||
assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
|
||||||
});
|
|
||||||
// Admin权限
|
|
||||||
token = adminLogin();
|
|
||||||
mockMvc.perform(delete("/admin/category/del?id=" + category.getId())
|
|
||||||
.header("Authorization", token))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andDo(result -> {
|
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
|
||||||
assertTrue(object.getBoolean(Result));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,28 +50,11 @@ public class CategoryControllerTest extends BaseTest {
|
|||||||
public void updateOne() throws Exception {
|
public void updateOne() throws Exception {
|
||||||
Category category = categoryMapper.getLastestCategory();
|
Category category = categoryMapper.getLastestCategory();
|
||||||
String name = randomStr(4);
|
String name = randomStr(4);
|
||||||
// 未登录
|
getMockData(put("/admin/category/update?id=" + category.getId() + "&name=" + name), adminLogin()).andDo(result -> {
|
||||||
mockMvc.perform(put("/admin/category/update?id=" + category.getId() + "&name=" + name)).andExpect(status().isOk())
|
// Response<CategoryModel> response = mapper.readValue(result.getResponse().getContentAsString(), new ResponseType<Response<CategoryModel>>());
|
||||||
.andDo(result -> {
|
Response<CategoryModel> response = getResponse(result, CATEGORY_MODEL_TYPE);
|
||||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
});
|
CategoryModel c = response.getResult();
|
||||||
// User权限
|
|
||||||
String token = userLogin();
|
|
||||||
mockMvc.perform(put("/admin/category/update?id=" + category.getId() + "&name=" + name)
|
|
||||||
.header("Authorization", token))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andDo(result -> {
|
|
||||||
assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
|
||||||
});
|
|
||||||
// Admin权限
|
|
||||||
token = adminLogin();
|
|
||||||
mockMvc.perform(put("/admin/category/update?id=" + category.getId() + "&name=" + name)
|
|
||||||
.header("Authorization", token))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andDo(result -> {
|
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
|
||||||
CategoryModel c = (CategoryModel) JSONObject.toBean(object.getJSONObject(Result), CategoryModel.class);
|
|
||||||
assertEquals(name, c.getName());
|
assertEquals(name, c.getName());
|
||||||
assertNotEquals(0, c.getArticles());
|
assertNotEquals(0, c.getArticles());
|
||||||
assertNotNull(c.getId());
|
assertNotNull(c.getId());
|
||||||
@@ -115,14 +63,11 @@ public class CategoryControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getPage() throws Exception {
|
public void getPage() throws Exception {
|
||||||
mockMvc.perform(get("/categories")).andExpect(status().isOk())
|
getMockData(get("/categories")).andDo(result -> {
|
||||||
.andDo(result -> {
|
Response<PageData<CategoryModel>> response = getResponse(result, CATEGORY_MODEL_PAGE_TYPE);
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertNotNull(response.getResult());
|
||||||
JSONArray jsonArray = object.getJSONObject(Result).getJSONArray("list");
|
response.getResult().getList().forEach(c -> {
|
||||||
assertNotNull(jsonArray);
|
|
||||||
jsonArray.forEach(o -> {
|
|
||||||
CategoryModel c = (CategoryModel) JSONObject.toBean(JSONObject.fromObject(o), CategoryModel.class);
|
|
||||||
assertNotNull(c.getName());
|
assertNotNull(c.getName());
|
||||||
assertNotNull(c.getId());
|
assertNotNull(c.getId());
|
||||||
assertNotEquals(0, c.getArticles());
|
assertNotEquals(0, c.getArticles());
|
||||||
|
|||||||
Reference in New Issue
Block a user