测试类编写
This commit is contained in:
parent
ca65b5e8fc
commit
0c074c046f
@ -91,7 +91,7 @@ public class MonitorItemBeanManager implements ApplicationListener<ContextRefres
|
||||
CheckResult checkResult = item.check();
|
||||
if (checkResult.isTriggered()) {
|
||||
logger.info("定时任务[{}]触发成功,发送通知:[{}]", taskId, checkResult.getNotifyContent());
|
||||
notifyService.notify(checkResult.getNotifyContent(), NotifyType.MAIL_TYPE);
|
||||
notifyService.notify(checkResult.getNotifyContent(), NotifyType.TYPE_MAIL);
|
||||
}
|
||||
}, new CronTrigger(cron)));
|
||||
}
|
||||
|
@ -277,18 +277,16 @@ public interface HttpService {
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @return 统一返回报文
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
Msg<String> doDelete(String url) throws Exception;
|
||||
Msg<String> doDelete(String url);
|
||||
|
||||
/**
|
||||
* 发送delete请求;不带请求参数
|
||||
*
|
||||
* @param url 请求地址
|
||||
* @return 统一返回报文
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
Msg<HttpEntity> doDeleteWithEntity(String url) throws Exception;
|
||||
Msg<HttpEntity> doDeleteWithEntity(String url);
|
||||
|
||||
/**
|
||||
* 发送delete请求;带请求参数
|
||||
@ -296,9 +294,8 @@ public interface HttpService {
|
||||
* @param url 请求地址
|
||||
* @param params 请求参数
|
||||
* @return 统一返回报文
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
Msg<String> doDelete(String url, Map<String, String> params) throws Exception;
|
||||
Msg<String> doDelete(String url, Map<String, String> params);
|
||||
|
||||
|
||||
/**
|
||||
@ -307,9 +304,8 @@ public interface HttpService {
|
||||
* @param url 请求地址
|
||||
* @param params 请求参数
|
||||
* @return 统一返回报文
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params) throws Exception;
|
||||
Msg<HttpEntity> doDeleteWithEntity(String url, Map<String, String> params);
|
||||
|
||||
interface NetWorkListener<T> {
|
||||
/**
|
||||
|
@ -12,8 +12,9 @@ public enum NotifyType {
|
||||
/**
|
||||
* 邮件类型
|
||||
*/
|
||||
MAIL_TYPE("邮件"),
|
||||
SMS_TYPE("短信");
|
||||
TYPE_LOG("日志输出"),
|
||||
TYPE_MAIL("邮件"),
|
||||
TYPE_MSG("短信");
|
||||
|
||||
private final String typeName;
|
||||
|
||||
|
@ -6,7 +6,6 @@ import com.hxuanyu.notify.service.MailService;
|
||||
import com.hxuanyu.notify.service.NotifyService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@ -26,10 +25,12 @@ public class NotifyServiceImpl implements NotifyService {
|
||||
|
||||
@Override
|
||||
public void notify(Object content, NotifyType notifyType) {
|
||||
if (NotifyType.MAIL_TYPE.equals(notifyType)) {
|
||||
if (NotifyType.TYPE_MAIL.equals(notifyType)) {
|
||||
sendMail(content);
|
||||
} else if (NotifyType.SMS_TYPE.equals(notifyType)) {
|
||||
} else if (NotifyType.TYPE_MSG.equals(notifyType)) {
|
||||
sendSms(content);
|
||||
} else if (NotifyType.TYPE_LOG.equals(notifyType)) {
|
||||
logger.info("新通知:{}", content);
|
||||
} else {
|
||||
logger.info("未匹配到通知类型:[{}]", content);
|
||||
}
|
||||
|
23
test/pom.xml
23
test/pom.xml
@ -12,6 +12,15 @@
|
||||
<artifactId>test</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>notify-spring-boot-starter</artifactId>
|
||||
@ -20,13 +29,19 @@
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>common-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>monitor-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<groupId>com.hxuanyu</groupId>
|
||||
<artifactId>network-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -4,11 +4,10 @@ import com.hxuanyu.common.message.Msg;
|
||||
import com.hxuanyu.notify.model.Mail;
|
||||
import com.hxuanyu.notify.service.MailService;
|
||||
import com.hxuanyu.notify.service.NotifyService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.Mapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@ -22,12 +21,12 @@ import javax.annotation.Resource;
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(TestController.class);
|
||||
@Resource
|
||||
MailService mailService;
|
||||
|
||||
@RequestMapping("/testMail")
|
||||
public Msg<Mail> testMail(){
|
||||
public Msg<Mail> testMail() {
|
||||
Mail mail = new Mail();
|
||||
mail.setContent("邮件内容");
|
||||
mail.setSubject("邮件主题");
|
||||
@ -41,4 +40,33 @@ public class TestController {
|
||||
|
||||
return Msg.success("发送邮件成功", mail);
|
||||
}
|
||||
|
||||
|
||||
@ResponseBody
|
||||
@GetMapping("/")
|
||||
public Msg<String> testGet() {
|
||||
logger.info("收到GET请求");
|
||||
return Msg.success("GET 测试");
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@PostMapping("/")
|
||||
public Msg<String> testPost() {
|
||||
logger.info("收到POST请求");
|
||||
return Msg.success("POST 测试");
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@PutMapping("/")
|
||||
public Msg<String> testPut() {
|
||||
logger.info("收到PUT请求");
|
||||
return Msg.success("PUT 测试");
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@DeleteMapping("/")
|
||||
public Msg<String> testDelete() {
|
||||
logger.info("收到DELETE请求");
|
||||
return Msg.success("DELETE 测试");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.hxuanyu.test.monitor;
|
||||
|
||||
import com.hxuanyu.monitor.annotation.MonitorItem;
|
||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||
import com.hxuanyu.monitor.common.CheckResult;
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
|
||||
/**
|
||||
* 定时任务监控测试
|
||||
*
|
||||
* @author hxuanyu
|
||||
*/
|
||||
@MonitorItem(cron = "0/5 * * * * *")
|
||||
public class TestMonitorItem extends BaseMonitorItem {
|
||||
|
||||
@Override
|
||||
public CheckResult check() {
|
||||
return CheckResult.triggered("Hello", NotifyType.TYPE_LOG);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.hxuanyu.starter.test.common;
|
||||
|
||||
|
||||
import com.hxuanyu.common.message.Msg;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MsgTest {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MsgTest.class);
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Msg<String> msg = Msg.success("测试内容", "测试体");
|
||||
logger.info(msg.toString());
|
||||
assert msg.isSuccess();
|
||||
msg = Msg.failed("失败消息");
|
||||
logger.info(msg.toString());
|
||||
assert msg.isFailed();
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.hxuanyu.starter.test.monitor;
|
||||
|
||||
import com.hxuanyu.monitor.base.BaseMonitorItem;
|
||||
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
|
||||
import com.hxuanyu.test.MainApplication;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class MonitorTest {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MonitorTest.class);
|
||||
@Autowired
|
||||
MonitorItemBeanManager monitorItemBeanManager;
|
||||
|
||||
@Test
|
||||
public void testMonitorItemScan() {
|
||||
BaseMonitorItem testMonitorItem = monitorItemBeanManager.getMonitorItemMap().get("ScheduledTask-TestMonitorItem");
|
||||
assert testMonitorItem.getMonitorItemName() != null;
|
||||
logger.info("获取到的Bean:{}", testMonitorItem.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.hxuanyu.starter.test.network;
|
||||
|
||||
import com.hxuanyu.common.message.Msg;
|
||||
import com.hxuanyu.network.service.HttpService;
|
||||
import com.hxuanyu.test.MainApplication;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class NetworkTest {
|
||||
|
||||
@Autowired
|
||||
HttpService httpService;
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(NetworkTest.class);
|
||||
|
||||
@Test
|
||||
@Timeout(3000)
|
||||
public void testGet() throws InterruptedException {
|
||||
Msg<String> msg = httpService.doGet("https://baidu.com");
|
||||
logger.info("GET测试:{}", msg);
|
||||
assert msg.isSuccess();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(3000)
|
||||
public void testPost() {
|
||||
Msg<String> msg = httpService.doPost("https://baidu.com");
|
||||
logger.info("POST测试:{}", msg);
|
||||
assert msg.isSuccess();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(3000)
|
||||
public void testDelete() {
|
||||
Msg<String> msg = httpService.doDelete("https://baidu.com");
|
||||
logger.info("DELETE测试:{}", msg);
|
||||
assert msg.isSuccess();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(3000)
|
||||
public void testPut() {
|
||||
Msg<String> msg = httpService.doPut("https://baidu.com");
|
||||
logger.info("PUT测试:{}", msg);
|
||||
assert msg.isSuccess();
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.hxuanyu.starter.test.notify;
|
||||
|
||||
import com.hxuanyu.notify.enums.NotifyType;
|
||||
import com.hxuanyu.notify.model.Mail;
|
||||
import com.hxuanyu.notify.service.NotifyService;
|
||||
import com.hxuanyu.test.MainApplication;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class NotifyTest {
|
||||
|
||||
@Autowired
|
||||
NotifyService notifyService;
|
||||
|
||||
@Test
|
||||
public void testNotify() throws InterruptedException {
|
||||
notifyService.notify(new Mail("2252193204@qq.com", "test subject", "test success"), NotifyType.TYPE_MAIL);
|
||||
sleep(2000);
|
||||
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
|
||||
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user