DeviceAlarmServiceImplTest.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.gb28181.bean.DeviceAlarm;
  3. import com.genersoft.iot.vmp.service.IDeviceAlarmService;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. import javax.annotation.Resource;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. @SpringBootTest
  11. @RunWith(SpringRunner.class)
  12. class DeviceAlarmServiceImplTest {
  13. @Resource
  14. private IDeviceAlarmService deviceAlarmService;
  15. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  16. @org.junit.jupiter.api.Test
  17. void getAllAlarm() {
  18. // deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111",null,null,null, null, null);
  19. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, null, null, null, null,
  20. // null, null).getSize());
  21. //
  22. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", null, null, null,
  23. // null, null).getSize());
  24. //
  25. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "1", null, null,
  26. // null, null).getSize());
  27. //
  28. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "2", null, null,
  29. // null, null).getSize());
  30. //
  31. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "3", null, null,
  32. // null, null).getSize());
  33. //
  34. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "4", null, null,
  35. // null, null).getSize());
  36. //
  37. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", "5", null, null,
  38. // null, null).getSize());
  39. //
  40. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", null, "1", null,
  41. // null, null).getSize());
  42. // System.out.println(deviceAlarmService.getAllAlarm(0, 10000, "11111111111111111111", null, "1", null,
  43. // null, null).getSize());
  44. }
  45. @org.junit.jupiter.api.Test
  46. void add() {
  47. for (int i = 0; i < 1000; i++) {
  48. DeviceAlarm deviceAlarm = new DeviceAlarm();
  49. deviceAlarm.setDeviceId("11111111111111111111");
  50. deviceAlarm.setAlarmDescription("test_" + i);
  51. /**
  52. * 报警方式 , 1为电话报警, 2为设备报警, 3为短信报警, 4为 GPS报警, 5为视频报警, 6为设备故障报警,
  53. * * 7其他报警;可以为直接组合如12为电话报警或 设备报警-
  54. */
  55. deviceAlarm.setAlarmMethod((int)(Math.random()*7 + 1) + "");
  56. Date date = randomDate("2021-01-01 00:00:00", "2021-06-01 00:00:00");
  57. deviceAlarm.setAlarmTime(format.format(date));
  58. /**
  59. * 报警级别, 1为一级警情, 2为二级警情, 3为三级警情, 4为四级 警情-
  60. */
  61. deviceAlarm.setAlarmPriority((int)(Math.random()*4 + 1) + "");
  62. deviceAlarm.setLongitude(116.325);
  63. deviceAlarm.setLatitude(39.562);
  64. deviceAlarmService.add(deviceAlarm);
  65. }
  66. }
  67. @org.junit.jupiter.api.Test
  68. void clearAlarmBeforeTime() {
  69. deviceAlarmService.clearAlarmBeforeTime(null,null, null);
  70. }
  71. private Date randomDate(String beginDate, String endDate) {
  72. try {
  73. Date start = format.parse(beginDate);//构造开始日期
  74. Date end = format.parse(endDate);//构造结束日期
  75. //getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
  76. if (start.getTime() >= end.getTime()) {
  77. return null;
  78. }
  79. long date = random(start.getTime(), end.getTime());
  80. return new Date(date);
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. return null;
  85. }
  86. private static long random(long begin, long end) {
  87. long rtn = begin + (long) (Math.random() * (end - begin));
  88. //如果返回的是开始时间和结束时间,则递归调用本函数查找随机值
  89. if (rtn == begin || rtn == end) {
  90. return random(begin, end);
  91. }
  92. return rtn;
  93. }
  94. }