From 8a11194b8fdee332d9d70fd99f1dbe6ace8611a2 Mon Sep 17 00:00:00 2001 From: "1986113@126.com" Date: Thu, 10 Aug 2017 14:19:50 +0800 Subject: [PATCH] no message --- .classpath | 10 +--- .gitignore | 1 + .settings/org.eclipse.core.resources.prefs | 5 ++ .settings/org.eclipse.jdt.core.prefs | 5 ++ .settings/org.eclipse.m2e.core.prefs | 4 ++ .../junit/controller/MessageController.java | 47 +++++++--------- .../junit/service/impl/VoiceServiceImpl.java | 56 +++++++++++++++++++ src/{test => main}/resources/sql/ddl.sql | 37 ++++++------ src/main/resources/sql/dml.sql | 1 + .../controller/MessageControllerTest.java | 31 +++++++--- .../outstanding/junit/dao/MybaitsTest.java | 23 +++++--- .../spring/init-data-mybatis-junit-Beans.xml | 8 ++- src/test/resources/sql/{dml.sql => test.sql} | 9 +-- 13 files changed, 163 insertions(+), 74 deletions(-) create mode 100644 .gitignore create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 src/main/java/com/outstanding/junit/service/impl/VoiceServiceImpl.java rename src/{test => main}/resources/sql/ddl.sql (86%) create mode 100644 src/main/resources/sql/dml.sql rename src/test/resources/sql/{dml.sql => test.sql} (90%) diff --git a/.classpath b/.classpath index 434d2af..a1a8aaf 100644 --- a/.classpath +++ b/.classpath @@ -6,11 +6,7 @@ - - - - - + @@ -29,9 +25,9 @@ - + - + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..cdfe4f1 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..abec6ca --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/src/main/java/com/outstanding/junit/controller/MessageController.java b/src/main/java/com/outstanding/junit/controller/MessageController.java index 6358b9b..39f7bad 100644 --- a/src/main/java/com/outstanding/junit/controller/MessageController.java +++ b/src/main/java/com/outstanding/junit/controller/MessageController.java @@ -8,8 +8,9 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import com.outstanding.framework.core.MultiPendingException; +import com.outstanding.framework.core.OutStandingException; import com.outstanding.framework.core.PendingException; -import com.outstanding.framework.core.SysCode; import com.outstanding.junit.code.ResCode; import com.outstanding.junit.constant.SessionConstant; import com.outstanding.junit.dto.voice.VoiceReqDto; @@ -30,35 +31,27 @@ public class MessageController { private VoiceService voiceService; @RequestMapping(value = "/sendVoiceCode.do") - public @ResponseBody VoiceResDto sendVoiceCode(@RequestBody VoiceReqDto dto,HttpSession session) { + public @ResponseBody VoiceResDto sendVoiceCode(@RequestBody VoiceReqDto dto,HttpSession session) throws OutStandingException { VoiceResDto res = new VoiceResDto(); - try { - - String auth = (String) session.getAttribute(SessionConstant.USER_LOGIN_AUTH); - - SystemUtils.outPrint("session值", auth); - - if (!auth.equals("true")) { - throw new PendingException(ResCode.userNotLogin.getCode(), ResCode.userNotLogin.getName()); - } - - if (!dto.validate()) { - throw new PendingException(ResCode.paramError.getCode(), ResCode.paramError.getName()); - } - - if (!voiceService.sendVoice(dto)) { - throw new PendingException(ResCode.voiceSendFail.getCode(), ResCode.voiceSendFail.getName()); - } - - res.setRspCd(ResCode.success.getCode()); - - } catch (PendingException e) { - res.setRspCd(e.getCode()); - } catch (Exception e) { - res.setRspCd(SysCode.SYS_FAIL); - } + String auth = (String) session.getAttribute(SessionConstant.USER_LOGIN_AUTH); + + SystemUtils.outPrint("session值", auth); + + if (!auth.equals("true")) { + throw new PendingException(ResCode.userNotLogin.getCode(), ResCode.userNotLogin.getName()); + } + + if (!dto.validate()) { + throw new PendingException(ResCode.paramError.getCode(), ResCode.paramError.getName()); + } + + if (!voiceService.sendVoice(dto)) { + throw new PendingException(ResCode.voiceSendFail.getCode(), ResCode.voiceSendFail.getName()); + } + + res.setRspCd(ResCode.success.getCode()); return res; } diff --git a/src/main/java/com/outstanding/junit/service/impl/VoiceServiceImpl.java b/src/main/java/com/outstanding/junit/service/impl/VoiceServiceImpl.java new file mode 100644 index 0000000..e096b4b --- /dev/null +++ b/src/main/java/com/outstanding/junit/service/impl/VoiceServiceImpl.java @@ -0,0 +1,56 @@ +package com.outstanding.junit.service.impl; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.outstanding.framework.core.PendingException; +import com.outstanding.junit.bo.gateway.GatewayBo; +import com.outstanding.junit.code.ResCode; +import com.outstanding.junit.dao.GatewayDao; +import com.outstanding.junit.dto.voice.VoiceReqDto; +import com.outstanding.junit.service.VoiceService; +import com.outstanding.junit.utils.SystemUtils; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@Service("voiceService") +public class VoiceServiceImpl implements VoiceService { + + @Autowired + private GatewayDao gatewayDao; + + public boolean sendVoice(VoiceReqDto dto) throws PendingException{ + + if (dto.getMobile().matches("^[1][3,4,5,7,8][0-9]{9}$")) { + throw new PendingException(ResCode.paramError.getCode(),ResCode.paramError.getName()); + } + + return true; + } + + public boolean send(VoiceReqDto dto) throws PendingException { + + //参数输出 + SystemUtils.outPrint("请求参数",dto.toString()); + + if (StringUtils.isEmpty(dto.getMobile())) { + throw new PendingException(ResCode.paramError.getCode(),ResCode.paramError.getName()); + } + + GatewayBo gate = new GatewayBo(); + gate.setGatewayNo("alidayu"); + GatewayBo gateBo = gatewayDao.getGateway(gate); + + gatewayDao.queryGatewayList(new GatewayBo()); + + if (gateBo==null) { + return false; + } + + return true; + } +} diff --git a/src/test/resources/sql/ddl.sql b/src/main/resources/sql/ddl.sql similarity index 86% rename from src/test/resources/sql/ddl.sql rename to src/main/resources/sql/ddl.sql index 6ddacb9..eb5899d 100644 --- a/src/test/resources/sql/ddl.sql +++ b/src/main/resources/sql/ddl.sql @@ -1,19 +1,20 @@ -CREATE TABLE `gateway_config` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `gateway_name` varchar(50) NOT NULL, - `gateway_no` varchar(50) NOT NULL, - `gateway_key` varchar(100) DEFAULT NULL, - `gateway_secret` varchar(100) DEFAULT NULL, - `gateway_url` varchar(500) NOT NULL, - `gateway_type` varchar(1) NOT NULL DEFAULT '1', - `gateway_state` varchar(1) NOT NULL DEFAULT '1', - `gateway_weight` varchar(10) DEFAULT NULL, - `is_delete` int(1) NOT NULL DEFAULT '1', - `create_time` datetime, - `update_time` datetime, - `gateway_batch_url` varchar(500) DEFAULT NULL, - `is_batch` varchar(1) NOT NULL DEFAULT '1', - `batch_count` int(11) DEFAULT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `gateway_No_Type` (`gateway_no`,`gateway_type`) +/**创建数据库表sql文件,此注释不能为空,防止读取sql文件报空错误。此sql文件只能增量修改*/ +CREATE TABLE `gateway_config` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `gateway_name` varchar(50) NOT NULL, + `gateway_no` varchar(50) NOT NULL, + `gateway_key` varchar(100) DEFAULT NULL, + `gateway_secret` varchar(100) DEFAULT NULL, + `gateway_url` varchar(500) NOT NULL, + `gateway_type` varchar(1) NOT NULL DEFAULT '1', + `gateway_state` varchar(1) NOT NULL DEFAULT '1', + `gateway_weight` varchar(10) DEFAULT NULL, + `is_delete` int(1) NOT NULL DEFAULT '1', + `create_time` datetime, + `update_time` datetime, + `gateway_batch_url` varchar(500) DEFAULT NULL, + `is_batch` varchar(1) NOT NULL DEFAULT '1', + `batch_count` int(11) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `gateway_No_Type` (`gateway_no`,`gateway_type`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/src/main/resources/sql/dml.sql b/src/main/resources/sql/dml.sql new file mode 100644 index 0000000..2c19fab --- /dev/null +++ b/src/main/resources/sql/dml.sql @@ -0,0 +1 @@ +/**初始化基础sql文件,此注释不能为空,防止读取sql文件报空错误。此sql文件只能增量修改*/ \ No newline at end of file diff --git a/src/test/java/com/outstanding/junit/controller/MessageControllerTest.java b/src/test/java/com/outstanding/junit/controller/MessageControllerTest.java index 644138e..7a1cfad 100644 --- a/src/test/java/com/outstanding/junit/controller/MessageControllerTest.java +++ b/src/test/java/com/outstanding/junit/controller/MessageControllerTest.java @@ -29,6 +29,9 @@ public class MessageControllerTest { private MessageController messageController; + /** + * HttpSession模拟 + */ private MockHttpSession session; @Before @@ -46,8 +49,11 @@ public class MessageControllerTest { //测试失败-参数效验失败 session.setAttribute(SessionConstant.USER_LOGIN_AUTH, "true"); - VoiceResDto result1 = messageController.sendVoiceCode(new VoiceReqDto(),session); - Assert.assertEquals(ResCode.paramError.getCode(), result1.getRspCd()); + try { + messageController.sendVoiceCode(new VoiceReqDto(),session); + } catch (PendingException e) { + Assert.assertEquals(ResCode.paramError.getCode(), e.getCode()); + } //测试成功-发送成功 VoiceReqDto req1 = new VoiceReqDto(); @@ -62,24 +68,33 @@ public class MessageControllerTest { req2.setCode("133323"); req2.setMobile("13500000000"); session.setAttribute(SessionConstant.USER_LOGIN_AUTH, "true"); - VoiceResDto result3 = messageController.sendVoiceCode(req2,session); - Assert.assertEquals(ResCode.voiceSendFail.getCode(), result3.getRspCd()); + try { + messageController.sendVoiceCode(req2,session); + } catch (PendingException e) { + Assert.assertEquals(ResCode.voiceSendFail.getCode(), e.getCode()); + } //测试失败-异常 VoiceReqDto req3 = new VoiceReqDto(); req3.setCode("123456"); req3.setMobile("33500000000"); session.setAttribute(SessionConstant.USER_LOGIN_AUTH, "true"); - VoiceResDto result4 = messageController.sendVoiceCode(req3,session); - Assert.assertEquals(SysCode.SYS_FAIL, result4.getRspCd()); + try { + messageController.sendVoiceCode(req3,session); + } catch (PendingException e) { + Assert.assertEquals(SysCode.SYS_FAIL, e.getCode()); + } //测试失败-session VoiceReqDto req4 = new VoiceReqDto(); req4.setCode("123456"); req4.setMobile("33500000000"); session.setAttribute(SessionConstant.USER_LOGIN_AUTH, "false"); - VoiceResDto result5 = messageController.sendVoiceCode(req4,session); - Assert.assertEquals(ResCode.userNotLogin.getCode(), result5.getRspCd()); + try { + messageController.sendVoiceCode(req4,session); + } catch (PendingException e) { + Assert.assertEquals(ResCode.userNotLogin.getCode(), e.getCode()); + } // 确认执行顺序、次数与传入参数 verify(voiceService, times(3)).sendVoice(any(VoiceReqDto.class)); diff --git a/src/test/java/com/outstanding/junit/dao/MybaitsTest.java b/src/test/java/com/outstanding/junit/dao/MybaitsTest.java index dff1e03..9196430 100644 --- a/src/test/java/com/outstanding/junit/dao/MybaitsTest.java +++ b/src/test/java/com/outstanding/junit/dao/MybaitsTest.java @@ -2,26 +2,23 @@ package com.outstanding.junit.dao; import org.junit.Assert; import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import com.outstanding.junit.base.MybatisBaseTest; import com.outstanding.junit.bo.gateway.GatewayBo; import com.outstanding.junit.enums.EnableDisableType; import com.outstanding.junit.enums.IsBatch; import com.outstanding.junit.enums.IsDelete; import com.outstanding.junit.enums.MessageType; +import com.outstanding.junit.utils.SystemUtils; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = {"/spring/init-data-mybatis-junit-Beans.xml"}) -public class MybaitsTest { +public class MybaitsTest extends MybatisBaseTest{ @Autowired private GatewayDao noteDao; @Test - public void addGateway(){ + public void addGateway() throws Exception{ GatewayBo gateway = new GatewayBo(); gateway.setGatewayNo("alidayu-test"); gateway.setGatewayBatchUrl("www.qq.com"); @@ -34,7 +31,15 @@ public class MybaitsTest { gateway.setIsBatch(IsBatch.YES); gateway.setGatewayState(EnableDisableType.ENABLE); int i = noteDao.addGateway(gateway); - Assert.assertTrue(i > 0); + + + GatewayBo newGateway = new GatewayBo(); + newGateway.setGatewayNo("alidayu-test"); + newGateway.setIsDelete(IsDelete.NORMAL); + GatewayBo newGatewayBo = noteDao.getGateway(newGateway); + SystemUtils.outPrint("获取网关", newGatewayBo.toString()); + + Assert.assertTrue(i == 1); } @Test @@ -43,7 +48,7 @@ public class MybaitsTest { gateway.setGatewayNo("alidayu"); gateway.setIsDelete(IsDelete.NORMAL); GatewayBo gatewayBo = noteDao.getGateway(gateway); - System.out.println("========"+gatewayBo.toString()); + SystemUtils.outPrint("获取网关", gatewayBo.toString()); Assert.assertTrue(gatewayBo.getId() > 0); } } diff --git a/src/test/resources/spring/init-data-mybatis-junit-Beans.xml b/src/test/resources/spring/init-data-mybatis-junit-Beans.xml index 31d5044..d3e19a7 100644 --- a/src/test/resources/spring/init-data-mybatis-junit-Beans.xml +++ b/src/test/resources/spring/init-data-mybatis-junit-Beans.xml @@ -8,11 +8,17 @@ http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd"> + + + - + + + + \ No newline at end of file diff --git a/src/test/resources/sql/dml.sql b/src/test/resources/sql/test.sql similarity index 90% rename from src/test/resources/sql/dml.sql rename to src/test/resources/sql/test.sql index c2afadf..aae41bd 100644 --- a/src/test/resources/sql/dml.sql +++ b/src/test/resources/sql/test.sql @@ -1,5 +1,6 @@ -INSERT INTO `gateway_config` VALUES (9, '阿里大于', 'alidayu', '23560825', '78c50cc1dc2c513e2ca62e3b491e62b9', 'http://gw.api.taobao.com/router/rest', '1', '1', '5', 1, '2016-12-8 15:57:00', '2016-12-19 13:37:45', '', '2', NULL); -INSERT INTO `gateway_config` VALUES (10, '创蓝', 'chuanglan', 'vip_lejia', 'Tch617798', 'http://222.73.117.158/msg/index.jsp', '2', '1', '2', 1, '2016-12-8 16:03:49', '2016-12-19 13:37:45', NULL, '2', NULL); -INSERT INTO `gateway_config` VALUES (11, '12580', 'g12580', 'ljyxxjs', 'w2t6h7n9', 'http://sms.weiyingjia.cn:8080/dog3/httpUTF8SMS.jsp', '1', '1', '3', 1, '2016-12-8 16:08:23', '2016-12-19 14:08:52', '', '1', NULL); -INSERT INTO `gateway_config` VALUES (12, '云片', 'yunpian', NULL, '859743956d4a786144dde886f8cbcea4', 'https://sms.yunpian.com/v2/sms/single_send.json', '1', '1', '4', 1, '2016-12-8 16:11:56', '2016-12-19 13:37:45', 'https://sms.yunpian.com/v2/sms/batch_send.json', '1', NULL); +/**初始化测试sql文件,此注释不能为空,防止读取sql文件报空错误。此sql文件只能增量修改*/ +INSERT INTO `gateway_config` VALUES (9, '阿里大于', 'alidayu', '23560825', '78c50cc1dc2c513e2ca62e3b491e62b9', 'http://gw.api.taobao.com/router/rest', '1', '1', '5', 1, '2016-12-8 15:57:00', '2016-12-19 13:37:45', '', '2', NULL); +INSERT INTO `gateway_config` VALUES (10, '创蓝', 'chuanglan', 'vip_lejia', 'Tch617798', 'http://222.73.117.158/msg/index.jsp', '2', '1', '2', 1, '2016-12-8 16:03:49', '2016-12-19 13:37:45', NULL, '2', NULL); +INSERT INTO `gateway_config` VALUES (11, '12580', 'g12580', 'ljyxxjs', 'w2t6h7n9', 'http://sms.weiyingjia.cn:8080/dog3/httpUTF8SMS.jsp', '1', '1', '3', 1, '2016-12-8 16:08:23', '2016-12-19 14:08:52', '', '1', NULL); +INSERT INTO `gateway_config` VALUES (12, '云片', 'yunpian', NULL, '859743956d4a786144dde886f8cbcea4', 'https://sms.yunpian.com/v2/sms/single_send.json', '1', '1', '4', 1, '2016-12-8 16:11:56', '2016-12-19 13:37:45', 'https://sms.yunpian.com/v2/sms/batch_send.json', '1', NULL); INSERT INTO `gateway_config` VALUES (13, '阿里百川', 'alibaichuan', '23249025', '4feaef5ed2fe11f0207aae6a73ae65a5', 'http://gw.api.taobao.com/router/rest', '1', '1', '1', 1, '2016-12-8 16:14:29', '2016-12-19 13:36:56', NULL, '2', NULL); \ No newline at end of file -- Gitee