切换到jackson #10
@@ -1,35 +1,45 @@
|
|||||||
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.WebUpdate;
|
import cn.celess.blog.entity.WebUpdate;
|
||||||
import cn.celess.blog.entity.model.PageData;
|
import cn.celess.blog.entity.model.PageData;
|
||||||
import cn.celess.blog.entity.model.WebUpdateModel;
|
import cn.celess.blog.entity.model.WebUpdateModel;
|
||||||
import cn.celess.blog.mapper.WebUpdateInfoMapper;
|
import cn.celess.blog.mapper.WebUpdateInfoMapper;
|
||||||
import net.sf.json.JSONArray;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import net.sf.json.JSONObject;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
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.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||||
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.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class WebUpdateInfoControllerTest extends BaseTest {
|
public class WebUpdateInfoControllerTest extends BaseTest {
|
||||||
|
|
||||||
|
private final TypeReference<?> MODAL_TYPE = new TypeReference<Response<WebUpdateModel>>() {
|
||||||
|
};
|
||||||
|
private final TypeReference<?> MODAL_LIST_TYPE = new TypeReference<Response<List<WebUpdateModel>>>() {
|
||||||
|
};
|
||||||
|
private final TypeReference<?> MODAL_PAGE_TYPE = new TypeReference<Response<PageData<WebUpdateModel>>>() {
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
WebUpdateInfoMapper mapper;
|
WebUpdateInfoMapper mapper;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void create() throws Exception {
|
public void create() throws Exception {
|
||||||
String info = UUID.randomUUID().toString();
|
String info = randomStr();
|
||||||
mockMvc.perform(post("/admin/webUpdate/create?info=" + info).header("Authorization", adminLogin())).andDo(result -> {
|
getMockData(post("/admin/webUpdate/create?info=" + info), adminLogin()).andDo(result -> {
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
Response<WebUpdateModel> response = getResponse(result, MODAL_TYPE);
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
assertTrue(object.containsKey(Result));
|
assertNotNull(response.getResult());
|
||||||
WebUpdateModel webUpdateModel = (WebUpdateModel) JSONObject.toBean(object.getJSONObject(Result), WebUpdateModel.class);
|
WebUpdateModel webUpdateModel = response.getResult();
|
||||||
assertEquals(info, webUpdateModel.getInfo());
|
assertEquals(info, webUpdateModel.getInfo());
|
||||||
assertNotNull(webUpdateModel.getTime());
|
assertNotNull(webUpdateModel.getTime());
|
||||||
assertNotEquals(0, webUpdateModel.getId());
|
assertNotEquals(0, webUpdateModel.getId());
|
||||||
@@ -40,7 +50,7 @@ public class WebUpdateInfoControllerTest extends BaseTest {
|
|||||||
public void del() throws Exception {
|
public void del() throws Exception {
|
||||||
// 新增数据
|
// 新增数据
|
||||||
WebUpdate webUpdate = new WebUpdate();
|
WebUpdate webUpdate = new WebUpdate();
|
||||||
webUpdate.setUpdateInfo(UUID.randomUUID().toString());
|
webUpdate.setUpdateInfo(randomStr());
|
||||||
webUpdate.setUpdateTime(new Date());
|
webUpdate.setUpdateTime(new Date());
|
||||||
mapper.insert(webUpdate);
|
mapper.insert(webUpdate);
|
||||||
// 接口测试
|
// 接口测试
|
||||||
@@ -49,32 +59,31 @@ public class WebUpdateInfoControllerTest extends BaseTest {
|
|||||||
assertNotEquals(0, update.getId());
|
assertNotEquals(0, update.getId());
|
||||||
|
|
||||||
long id = update.getId();
|
long id = update.getId();
|
||||||
mockMvc.perform(delete("/admin/webUpdate/del/" + id).header("Authorization", adminLogin())).andDo(result -> {
|
getMockData(delete("/admin/webUpdate/del/" + id), adminLogin()).andDo(result -> {
|
||||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
Response<Object> response = getResponse(result);
|
||||||
assertTrue(JSONObject.fromObject(result.getResponse().getContentAsString()).getBoolean(Result));
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
|
assertNotNull(response.getResult());
|
||||||
});
|
});
|
||||||
do {
|
do {
|
||||||
id += 2;
|
id += 2;
|
||||||
} while (mapper.existsById(id));
|
} while (mapper.existsById(id));
|
||||||
System.out.println("准备删除ID=" + id + "的不存在记录");
|
log.debug("准备删除ID={}的不存在记录", id);
|
||||||
mockMvc.perform(delete("/admin/webUpdate/del/" + id).header("Authorization", adminLogin())).andDo(result ->
|
getMockData(delete("/admin/webUpdate/del/" + id), adminLogin()).andDo(result -> assertEquals(DATA_NOT_EXIST.getCode(), getResponse(result).getCode()));
|
||||||
assertEquals(DATA_NOT_EXIST.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void update() throws Exception {
|
public void update() throws Exception {
|
||||||
// 新增数据
|
// 新增数据
|
||||||
WebUpdate webUpdate = new WebUpdate();
|
WebUpdate webUpdate = new WebUpdate();
|
||||||
webUpdate.setUpdateInfo(UUID.randomUUID().toString());
|
webUpdate.setUpdateInfo(randomStr());
|
||||||
webUpdate.setUpdateTime(new Date());
|
webUpdate.setUpdateTime(new Date());
|
||||||
mapper.insert(webUpdate);
|
mapper.insert(webUpdate);
|
||||||
List<WebUpdate> all = mapper.findAll();
|
List<WebUpdate> all = mapper.findAll();
|
||||||
WebUpdate update = all.get(all.size() - 1);
|
WebUpdate update = all.get(all.size() - 1);
|
||||||
assertNotEquals(0, update.getId());
|
assertNotEquals(0, update.getId());
|
||||||
assertNotNull(update.getUpdateInfo());
|
assertNotNull(update.getUpdateInfo());
|
||||||
String info = UUID.randomUUID().toString();
|
String info = randomStr();
|
||||||
mockMvc.perform(put("/admin/webUpdate/update?id=" + update.getId() + "&info=" + info).header("Authorization", adminLogin())).andDo(result -> {
|
getMockData(put("/admin/webUpdate/update?id=" + update.getId() + "&info=" + info), adminLogin()).andDo(result -> {
|
||||||
List<WebUpdate> list = mapper.findAll();
|
List<WebUpdate> list = mapper.findAll();
|
||||||
WebUpdate up = list.get(list.size() - 1);
|
WebUpdate up = list.get(list.size() - 1);
|
||||||
assertEquals(update.getId(), up.getId());
|
assertEquals(update.getId(), up.getId());
|
||||||
@@ -85,12 +94,12 @@ public class WebUpdateInfoControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findAll() throws Exception {
|
public void findAll() throws Exception {
|
||||||
mockMvc.perform(get("/webUpdate")).andDo(result -> {
|
getMockData(get("/webUpdate")).andDo(result -> {
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
Response<List<WebUpdateModel>> response = getResponse(result, MODAL_LIST_TYPE);
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
JSONArray jsonArray = object.getJSONArray(Result);
|
assertNotNull(response.getResult());
|
||||||
jsonArray.forEach(o -> {
|
assertNotEquals(0, response.getResult());
|
||||||
WebUpdateModel webUpdate = (WebUpdateModel) JSONObject.toBean(JSONObject.fromObject(o), WebUpdateModel.class);
|
response.getResult().forEach(webUpdate -> {
|
||||||
assertNotEquals(0, webUpdate.getId());
|
assertNotEquals(0, webUpdate.getId());
|
||||||
assertNotNull(webUpdate.getTime());
|
assertNotNull(webUpdate.getTime());
|
||||||
assertNotNull(webUpdate.getInfo());
|
assertNotNull(webUpdate.getInfo());
|
||||||
@@ -100,15 +109,14 @@ public class WebUpdateInfoControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void page() throws Exception {
|
public void page() throws Exception {
|
||||||
mockMvc.perform(get("/webUpdate/pages?page=1&count=10")).andDo(result -> {
|
getMockData(get("/webUpdate/pages?page=1&count=10")).andDo(result -> {
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
Response<PageData<WebUpdateModel>> response = getResponse(result, MODAL_PAGE_TYPE);
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertEquals(SUCCESS.getCode(), response.getCode());
|
||||||
assertNotNull(object.getJSONObject(Result));
|
assertNotNull(response.getResult());
|
||||||
PageData<WebUpdateModel> pageData = (PageData<WebUpdateModel>) JSONObject.toBean(object.getJSONObject(Result), PageData.class);
|
PageData<WebUpdateModel> pageData = response.getResult();
|
||||||
assertEquals(1, pageData.getPageNum());
|
assertEquals(1, pageData.getPageNum());
|
||||||
assertEquals(10, pageData.getPageSize());
|
assertEquals(10, pageData.getPageSize());
|
||||||
for (Object o : pageData.getList()) {
|
for (WebUpdateModel model : pageData.getList()) {
|
||||||
WebUpdateModel model = (WebUpdateModel) JSONObject.toBean(JSONObject.fromObject(o), WebUpdateModel.class);
|
|
||||||
assertNotEquals(0, model.getId());
|
assertNotEquals(0, model.getId());
|
||||||
assertNotNull(model.getTime());
|
assertNotNull(model.getTime());
|
||||||
assertNotNull(model.getInfo());
|
assertNotNull(model.getInfo());
|
||||||
@@ -118,7 +126,6 @@ public class WebUpdateInfoControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void lastestUpdateTime() throws Exception {
|
public void lastestUpdateTime() throws Exception {
|
||||||
mockMvc.perform(get("/lastestUpdate")).andDo(result ->
|
getMockData(get("/lastestUpdate")).andDo(result -> assertEquals(SUCCESS.getCode(), getResponse(result).getCode()));
|
||||||
assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user