Service层修改 单元测试

This commit is contained in:
禾几海
2020-05-25 23:04:41 +08:00
parent 4e383b6598
commit 73a1d3eadf
4 changed files with 29 additions and 38 deletions

View File

@@ -1,5 +1,6 @@
package cn.celess.blog.service;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.model.WebUpdateModel;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONObject;
@@ -45,7 +46,7 @@ public interface WebUpdateInfoService {
* @param page 数据页
* @return 分页数据
*/
PageInfo<WebUpdateModel> pages(int count, int page);
PageData<WebUpdateModel> pages(int count, int page);
/**
* 获取全部的更新记录

View File

@@ -2,12 +2,14 @@ package cn.celess.blog.service.serviceimpl;
import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.WebUpdate;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.model.WebUpdateModel;
import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.WebUpdateInfoMapper;
import cn.celess.blog.service.WebUpdateInfoService;
import cn.celess.blog.util.DateFormatUtil;
import cn.celess.blog.util.HttpUtil;
import cn.celess.blog.util.ModalTrans;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
@@ -39,11 +41,11 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
if (info == null || info.replaceAll(" ", "").isEmpty()) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
WebUpdate webUpdate = new WebUpdate(info, new Date());
WebUpdate webUpdate = new WebUpdate(info);
if (webUpdateInfoMapper.insert(webUpdate) == 0) {
throw new MyException(ResponseEnum.FAILURE);
}
return trans(webUpdate);
return ModalTrans.webUpdate(webUpdateInfoMapper.findById(webUpdate.getId()));
}
@Override
@@ -65,26 +67,20 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
}
webUpdate.setUpdateInfo(info);
webUpdateInfoMapper.update(id, info);
return trans(webUpdate);
return ModalTrans.webUpdate(webUpdate);
}
@Override
public PageInfo<WebUpdateModel> pages(int count, int page) {
public PageData<WebUpdateModel> pages(int count, int page) {
PageHelper.startPage(page, count);
List<WebUpdate> updateList = webUpdateInfoMapper.findAll();
PageInfo pageInfo = new PageInfo(updateList);
pageInfo.setList(list2List(updateList));
return pageInfo;
return new PageData<WebUpdateModel>(new PageInfo<WebUpdate>(updateList), list2List(updateList));
}
@Override
public List<WebUpdateModel> findAll() {
List<WebUpdate> all = webUpdateInfoMapper.findAll();
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
for (WebUpdate w : all) {
webUpdateModels.add(trans(w));
}
return webUpdateModels;
return list2List(all);
}
@Override
@@ -110,14 +106,7 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {
List<WebUpdateModel> webUpdateModels = new ArrayList<>();
for (WebUpdate w : webUpdates) {
webUpdateModels.add(trans(w));
}
webUpdates.forEach(update -> webUpdateModels.add(ModalTrans.webUpdate(update)));
return webUpdateModels;
}
private WebUpdateModel trans(WebUpdate webUpdate) {
return new WebUpdateModel(webUpdate.getId(), webUpdate.getUpdateInfo(), DateFormatUtil.get(webUpdate.getUpdateTime()));
}
}

View File

@@ -1,13 +1,7 @@
package cn.celess.blog.util;
import cn.celess.blog.entity.Article;
import cn.celess.blog.entity.Category;
import cn.celess.blog.entity.Tag;
import cn.celess.blog.entity.User;
import cn.celess.blog.entity.model.ArticleModel;
import cn.celess.blog.entity.model.CategoryModel;
import cn.celess.blog.entity.model.TagModel;
import cn.celess.blog.entity.model.UserModel;
import cn.celess.blog.entity.*;
import cn.celess.blog.entity.model.*;
import org.springframework.beans.BeanUtils;
/**
@@ -61,4 +55,13 @@ public class ModalTrans {
BeanUtils.copyProperties(tag, model);
return model;
}
public static WebUpdateModel webUpdate(WebUpdate update) {
WebUpdateModel model = new WebUpdateModel();
model.setId(update.getId());
model.setInfo(update.getUpdateInfo());
model.setTime(DateFormatUtil.get(update.getUpdateTime()));
return model;
}
}

View File

@@ -2,9 +2,9 @@ package cn.celess.blog.controller;
import cn.celess.blog.BaseTest;
import cn.celess.blog.entity.WebUpdate;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.model.WebUpdateModel;
import cn.celess.blog.mapper.WebUpdateInfoMapper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Test;
@@ -104,17 +104,15 @@ public class WebUpdateInfoControllerTest extends BaseTest {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
assertNotNull(object.getJSONObject(Result));
PageInfo pageInfo = (PageInfo) JSONObject.toBean(object.getJSONObject(Result), PageInfo.class);
assertNotEquals(0, pageInfo.getEndRow());
assertNotEquals(0, pageInfo.getStartRow());
assertEquals(1, pageInfo.getPageNum());
assertEquals(10, pageInfo.getPageSize());
pageInfo.getList().forEach(o -> {
PageData<WebUpdateModel> pageData = (PageData<WebUpdateModel>) JSONObject.toBean(object.getJSONObject(Result), PageData.class);
assertEquals(1, pageData.getPageNum());
assertEquals(10, pageData.getPageSize());
for (Object o : pageData.getList()) {
WebUpdateModel model = (WebUpdateModel) JSONObject.toBean(JSONObject.fromObject(o), WebUpdateModel.class);
assertNotEquals(0, model.getId());
assertNotNull(model.getTime());
assertNotNull(model.getInfo());
});
}
});
}