This commit is contained in:
xiangwbs
2018-06-19 22:17:03 +08:00
parent 2ddba378d7
commit 0907e01a13
31 changed files with 893 additions and 241 deletions
@@ -1,16 +0,0 @@
package com.xwbing;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {
@Test
public void contextLoads() {
}
}
@@ -0,0 +1,29 @@
package com.xwbing;
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.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
/**
* 说明:
* 项目名称: boot-module-demo
* 创建时间: 2018/5/10 16:36
* 作者: xiangwb
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@Rollback(false)
@WebAppConfiguration
public class BaseTest {
@Autowired
protected MockMvc mvc;
}
@@ -0,0 +1,68 @@
package com.xwbing.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.BaseTest;
import com.xwbing.domain.entity.sys.SysRole;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.text.MessageFormat;
/**
* 说明:
* 项目名称: boot-module-demo
* 创建时间: 2018/5/10 16:36
* 作者: xiangwb
*/
@Slf4j
public class SysRoleControlTest extends BaseTest {
@Test
public void all() throws Exception {
//save
SysRole sysRole = new SysRole();
sysRole.setCode("apiTest");
sysRole.setEnable("Y");
sysRole.setName("apiTest");
sysRole.setRemark("apiTest");
String saveStr = mvc.perform(MockMvcRequestBuilders
.post("/role/save")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSONObject.toJSONString(sysRole)))
.andReturn().getResponse().getContentAsString();
log.info(saveStr);
//getById
JSONObject response = JSONObject.parseObject(saveStr);
String getByIdStr = mvc.perform(MockMvcRequestBuilders
.get("/role/getById")
.param("id", response.getString("id")))
.andReturn().getResponse().getContentAsString();
log.info(getByIdStr);
response = JSONObject.parseObject(getByIdStr);
sysRole = JSON.toJavaObject(response.getJSONObject("data"), SysRole.class);
//listByEnable
String listByEnableString = mvc.perform(MockMvcRequestBuilders
.get("/role/listByEnable")
.param("enable", "Y"))
.andReturn().getResponse().getContentAsString();
log.info(listByEnableString);
//update
sysRole.setName("updateTest");
String updateStr = mvc.perform(MockMvcRequestBuilders
.put("/role/update")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(JSONObject.toJSONString(sysRole)))
.andReturn().getResponse().getContentAsString();
log.info(updateStr);
//removeById
String removeByIdStr = mvc.perform(MockMvcRequestBuilders
.delete(MessageFormat.format("/role/removeById/{0}", sysRole.getId())))
.andReturn().getResponse().getContentAsString();
log.info(removeByIdStr);
// mvc.perform(MockMvcRequestBuilders
// .delete(MessageFormat.format("/role/removeById/{0}", sysRole.getId())))
// .andExpect(MockMvcResultMatchers.status().isOk());
}
}
@@ -0,0 +1,128 @@
package com.xwbing.service;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.BaseTest;
import com.xwbing.domain.entity.ExpressInfo;
import com.xwbing.domain.entity.sys.SysRole;
import com.xwbing.domain.entity.vo.ExpressInfoVo;
import com.xwbing.domain.mapper.sys.SysRoleMapper;
import com.xwbing.rabbit.Sender;
import com.xwbing.redis.RedisService;
import com.xwbing.service.rest.ExpressDeliveryService;
import com.xwbing.service.rest.QRCodeZipService;
import com.xwbing.service.sys.SysRoleService;
import com.xwbing.util.RSAUtil;
import com.xwbing.util.RestMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* 项目名称: boot-module-demo
* 创建时间: 2018/6/1 13:20
* 作者: xiangwb
* 说明: 服务层功能测试
*/
@Slf4j
public class ServiceTest extends BaseTest {
@Resource
private RedisService redisService;
@Resource
private ExpressDeliveryService expressDeliveryService;
@Resource
private QRCodeZipService qrCodeZipService;
@Resource
private Sender sender;
@Resource
private SysRoleService sysRoleService;
@Resource
private SysRoleMapper sysRoleMapper;
@Transactional("jpaTransactionManager")
@Test
public void jpaTransactionTest() {
log.info("jpa数据源事务回滚");
SysRole sysRole = new SysRole();
sysRole.setName("serviceTest");
sysRole.setCode("serviceTest");
sysRole.setEnable("Y");
sysRole.setRemark("serviceTest");
Assert.assertTrue(sysRoleService.save(sysRole).isSuccess());
}
@Transactional
@Test
public void mybatisTransactionTest() {
log.info("mybatis数据源事务回滚");
SysRole sysRole = new SysRole();
sysRole.setName("serviceTest");
sysRole.setCode("serviceTest");
sysRole.setEnable("Y");
sysRole.setRemark("serviceTest");
int insert = sysRoleMapper.insert(sysRole);
Assert.assertEquals(1, insert);
}
@Test
public void listShipperCodeTest() {
log.debug("获取快递列表");
List<JSONObject> list = expressDeliveryService.listShipperCode();
Assert.assertSame(12, list.size());
}
@Test
public void expressInfoTest() {
log.debug("快递查询");
ExpressInfo info = new ExpressInfo();
info.setLogisticCode("1179751819114");
info.setShipperCode("EMS");
if (StringUtils.isEmpty(info.getLogisticCode()) || StringUtils.isEmpty(info.getShipperCode())) {
log.error("快递公司或物流单号不能为空");
}
ExpressInfoVo infoVo = expressDeliveryService.queryOrderTraces(info);
Assert.assertTrue(infoVo.isSuccess());
}
@Test
public void createQRCodeTest() {
RestMessage qrCode = qrCodeZipService.createQRCode("test", "test");
Assert.assertTrue(qrCode.isSuccess());
}
@Test
public void decodeQRCodeTest() throws IOException {
ClassPathResource pic = new ClassPathResource("pic");
String path = pic.getFile().getAbsolutePath();
File file = new File(path + File.separator + "QRCode.png");
RestMessage decode = qrCodeZipService.decode(file);
Assert.assertTrue(decode.isSuccess());
}
@Test
public void rsaTest() {
String en = RSAUtil.encrypt("123456");
String de = RSAUtil.decrypt(en);
Assert.assertEquals("123456", de);
}
@Test
public void sendTest() {
sender.sendMessage(new String[]{"mq测试"});
}
@Test
public void redisTest() {
redisService.set("redis", "xwbing");
String s = redisService.get("redis");
Assert.assertEquals("xwbing", s);
}
}
@@ -0,0 +1,49 @@
package com.xwbing.service;
import com.xwbing.BaseTest;
import com.xwbing.domain.entity.sys.SysRole;
import com.xwbing.domain.mapper.sys.SysRoleMapper;
import com.xwbing.service.sys.SysRoleService;
import com.xwbing.util.RestMessage;
import org.junit.Assert;
import org.junit.Test;
import javax.annotation.Resource;
/**
* 项目名称: boot-module-demo
* 创建时间: 2018/6/1 13:20
* 作者: xiangwb
* 说明: 权限服务层测试
*/
public class SysRoleServiceTest extends BaseTest {
@Resource
private SysRoleService sysRoleService;
@Resource
private SysRoleMapper sysRoleMapper;
@Test
public void all() {
//save
SysRole sysRole = new SysRole();
sysRole.setCode("serviceTest");
sysRole.setEnable("Y");
sysRole.setName("serviceTest");
sysRole.setRemark("serviceTest");
RestMessage rest = sysRoleService.save(sysRole);
Assert.assertTrue(rest.getMessage(), rest.isSuccess());
//getById
sysRole = sysRoleService.getById(rest.getId());
Assert.assertNotNull(sysRole);
//update
sysRole.setName("updateTest");
rest = sysRoleService.update(sysRole);
Assert.assertTrue(rest.getMessage(), rest.isSuccess());
Assert.assertNotSame("修改失败", sysRole, sysRoleService.getById(rest.getId()));
//listAllByEnable
Assert.assertNotNull(sysRoleService.listAllByEnable("Y"));
//removeById
Assert.assertTrue("删除失败", sysRoleService.removeById(rest.getId()).isSuccess());
//............................
}
}