切换到jackson #10
@@ -1,36 +1,42 @@
|
|||||||
package cn.celess.blog.controller;
|
package cn.celess.blog.controller;
|
||||||
|
|
||||||
import cn.celess.blog.BaseTest;
|
import cn.celess.blog.BaseTest;
|
||||||
|
import cn.celess.blog.entity.Response;
|
||||||
import cn.celess.blog.entity.Tag;
|
import cn.celess.blog.entity.Tag;
|
||||||
import cn.celess.blog.entity.model.PageData;
|
import cn.celess.blog.entity.model.PageData;
|
||||||
import cn.celess.blog.entity.model.TagModel;
|
import cn.celess.blog.entity.model.TagModel;
|
||||||
import cn.celess.blog.mapper.TagMapper;
|
import cn.celess.blog.mapper.TagMapper;
|
||||||
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 java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
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 TagControllerTest extends BaseTest {
|
public class TagControllerTest extends BaseTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
TagMapper tagMapper;
|
TagMapper tagMapper;
|
||||||
|
private static final TypeReference<?> TAG_MODEL_TYPE = new TypeReference<Response<TagModel>>() {
|
||||||
|
|
||||||
|
};
|
||||||
|
private static final TypeReference<?> TAG_MODEL_PAGE_TYPE = new TypeReference<Response<PageData<TagModel>>>() {
|
||||||
|
};
|
||||||
|
private static final TypeReference<?> TAG_NAC_LIST_TYPE = new TypeReference<Response<List<Map<String, Object>>>>() {
|
||||||
|
};
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addOne() throws Exception {
|
public void addOne() throws Exception {
|
||||||
String name = randomStr(4);
|
String name = randomStr(4);
|
||||||
mockMvc.perform(post("/admin/tag/create?name=" + name)).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
getMockData(post("/admin/tag/create?name=" + name)).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||||
mockMvc.perform(post("/admin/tag/create?name=" + name).header("authorization", userLogin())).andDo(result -> assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
getMockData(post("/admin/tag/create?name=" + name), userLogin()).andDo(result -> assertEquals(PERMISSION_ERROR.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||||
mockMvc.perform(post("/admin/tag/create?name=" + name).header("authorization", adminLogin())).andDo(result -> {
|
getMockData(post("/admin/tag/create?name=" + name), adminLogin()).andDo(result -> {
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
Response<TagModel> response = getResponse(result, TAG_MODEL_TYPE);
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
JSONObject resJson = object.getJSONObject(Result);
|
TagModel tag = response.getResult();
|
||||||
TagModel tag = (TagModel) JSONObject.toBean(resJson, TagModel.class);
|
|
||||||
assertNotNull(tag.getId());
|
assertNotNull(tag.getId());
|
||||||
assertEquals(name, tag.getName());
|
assertEquals(name, tag.getName());
|
||||||
});
|
});
|
||||||
@@ -42,16 +48,14 @@ public class TagControllerTest extends BaseTest {
|
|||||||
public void delOne() throws Exception {
|
public void delOne() throws Exception {
|
||||||
Tag lastestTag = tagMapper.getLastestTag();
|
Tag lastestTag = tagMapper.getLastestTag();
|
||||||
assertNotNull(lastestTag.getId());
|
assertNotNull(lastestTag.getId());
|
||||||
String token = adminLogin();
|
getMockData(delete("/admin/tag/del?id=" + lastestTag.getId()), adminLogin()).andDo(result -> {
|
||||||
mockMvc.perform(delete("/admin/tag/del?id=" + lastestTag.getId()).header("Authorization", token)).andDo(result -> {
|
Response<Boolean> response = getResponse(result, BOOLEAN_TYPE);
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertTrue(response.getResult());
|
||||||
assertTrue(object.getBoolean(Result));
|
|
||||||
});
|
});
|
||||||
long id = lastestTag.getId() * 2;
|
long id = lastestTag.getId() * 2;
|
||||||
mockMvc.perform(delete("/admin/tag/del?id=" + id).header("Authorization", token)).andDo(result ->
|
getMockData(delete("/admin/tag/del?id=" + id), adminLogin())
|
||||||
assertEquals(TAG_NOT_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))
|
.andDo(result -> assertEquals(TAG_NOT_EXIST.getCode(), getResponse(result, STRING_TYPE).getCode()));
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +63,12 @@ public class TagControllerTest extends BaseTest {
|
|||||||
public void updateOne() throws Exception {
|
public void updateOne() throws Exception {
|
||||||
Tag tag = tagMapper.getLastestTag();
|
Tag tag = tagMapper.getLastestTag();
|
||||||
assertNotNull(tag.getId());
|
assertNotNull(tag.getId());
|
||||||
String name = UUID.randomUUID().toString().substring(0, 4);
|
String name = randomStr(4);
|
||||||
mockMvc.perform(put("/admin/tag/update?id=" + tag.getId() + "&name=" + name).header("Authorization", adminLogin())).andDo(result -> {
|
getMockData(put("/admin/tag/update?id=" + tag.getId() + "&name=" + name), adminLogin()).andDo(result -> {
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
Response<TagModel> response = getResponse(result, TAG_MODEL_TYPE);
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
assertNotNull(object.getJSONObject(Result));
|
assertNotNull(response.getResult());
|
||||||
TagModel t = (TagModel) JSONObject.toBean(object.getJSONObject(Result), TagModel.class);
|
TagModel t = response.getResult();
|
||||||
assertEquals(name, t.getName());
|
assertEquals(name, t.getName());
|
||||||
assertEquals(tag.getId(), t.getId());
|
assertEquals(tag.getId(), t.getId());
|
||||||
});
|
});
|
||||||
@@ -73,23 +77,18 @@ public class TagControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getPage() throws Exception {
|
public void getPage() throws Exception {
|
||||||
mockMvc.perform(get("/tags?page=1&count=5"))
|
getMockData(get("/tags?page=1&count=5")).andDo(result -> {
|
||||||
.andExpect(status().is(200))
|
Response<PageData<TagModel>> response = getResponse(result, TAG_MODEL_PAGE_TYPE);
|
||||||
.andDo(result -> {
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
JSONObject articlesJSON = JSONObject.fromObject(result.getResponse().getContentAsString());
|
|
||||||
// 断言获取数据成功
|
|
||||||
assertEquals(SUCCESS.getCode(), articlesJSON.getInt(Code));
|
|
||||||
// 结果集非空
|
// 结果集非空
|
||||||
assertNotNull(articlesJSON.getJSONObject(Result));
|
assertNotNull(response.getResult());
|
||||||
// 判断pageInfo是否包装完全
|
// 判断pageInfo是否包装完全
|
||||||
JSONObject resultJson = JSONObject.fromObject(articlesJSON.getJSONObject(Result));
|
PageData<TagModel> pageData = response.getResult();
|
||||||
PageData<TagModel> pageData = (PageData<TagModel>) JSONObject.toBean(resultJson, PageData.class);
|
|
||||||
assertNotEquals(0, pageData.getTotal());
|
assertNotEquals(0, pageData.getTotal());
|
||||||
assertEquals(1, pageData.getPageNum());
|
assertEquals(1, pageData.getPageNum());
|
||||||
assertEquals(5, pageData.getPageSize());
|
assertEquals(5, pageData.getPageSize());
|
||||||
// 内容完整
|
// 内容完整
|
||||||
for (Object tag : pageData.getList()) {
|
for (TagModel t : pageData.getList()) {
|
||||||
TagModel t = (TagModel) JSONObject.toBean(JSONObject.fromObject(tag), TagModel.class);
|
|
||||||
assertNotNull(t.getId());
|
assertNotNull(t.getId());
|
||||||
assertNotNull(t.getName());
|
assertNotNull(t.getName());
|
||||||
}
|
}
|
||||||
@@ -98,15 +97,13 @@ public class TagControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTagNameAndCount() throws Exception {
|
public void getTagNameAndCount() throws Exception {
|
||||||
mockMvc.perform(get("/tags/nac")).andDo(result -> {
|
getMockData(get("/tags/nac")).andDo(result -> {
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
Response<List<Map<String, Object>>> response = getResponse(result, TAG_NAC_LIST_TYPE);
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
JSONArray jsonArray = object.getJSONArray(Result);
|
assertNotNull(response.getResult());
|
||||||
assertNotNull(jsonArray);
|
response.getResult().forEach(o -> {
|
||||||
jsonArray.forEach(o -> {
|
assertNotNull(o.get("name"));
|
||||||
JSONObject json = JSONObject.fromObject(o);
|
assertNotNull(o.get("size"));
|
||||||
assertTrue(json.containsKey("size"));
|
|
||||||
assertTrue(json.containsKey("name"));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user