1 package org.jumpmind.symmetric.service.impl;
2
3 import java.math.BigDecimal;
4 import java.util.List;
5
6 import javax.management.Notification;
7
8 import org.jumpmind.symmetric.common.Constants;
9 import org.jumpmind.symmetric.model.StatisticAlertThresholds;
10 import org.jumpmind.symmetric.service.IStatisticService;
11 import org.jumpmind.symmetric.statistic.Statistic;
12 import org.jumpmind.symmetric.statistic.StatisticName;
13 import org.jumpmind.symmetric.test.AbstractDatabaseTest;
14 import org.jumpmind.symmetric.util.AppUtils;
15 import org.junit.Before;
16 import org.junit.Test;
17
18 public class StatisticServiceTest extends AbstractDatabaseTest {
19
20 static final String TEST_STAT_NAME = "test";
21
22 IStatisticService statisticService;
23
24 public StatisticServiceTest() throws Exception {
25 super();
26 }
27
28 public StatisticServiceTest(String dbName) {
29 super(dbName);
30 }
31
32 @Before
33 public void setUp() {
34 statisticService = AppUtils.find(Constants.STATISTIC_SERVICE, getSymmetricEngine());
35 }
36
37 @Test
38 public void testSaveAlerts() throws Exception {
39 statisticService.removeStatisticAlertThresholds(TEST_STAT_NAME);
40
41 StatisticAlertThresholds thresholds = new StatisticAlertThresholds(TEST_STAT_NAME, new BigDecimal(10),
42 new Long(2), new BigDecimal(0), new Long(0), new BigDecimal(0), new BigDecimal(0));
43
44 List<StatisticAlertThresholds> list = statisticService.getAlertThresholds();
45 assertFalse(list.contains(thresholds));
46
47 statisticService.saveStatisticAlertThresholds(thresholds);
48
49 list = statisticService.getAlertThresholds();
50 assertTrue(list.contains(thresholds));
51
52 thresholds.setThresholdCountMin(-100l);
53 assertFalse(list.contains(thresholds));
54
55 statisticService.saveStatisticAlertThresholds(thresholds);
56
57 list = statisticService.getAlertThresholds();
58 assertTrue(list.contains(thresholds));
59
60 statisticService.removeStatisticAlertThresholds(TEST_STAT_NAME);
61 }
62
63 @Test
64 public void testSaveAlertsWithNulls() throws Exception {
65 statisticService.removeStatisticAlertThresholds(TEST_STAT_NAME);
66
67 StatisticAlertThresholds thresholds = new StatisticAlertThresholds(TEST_STAT_NAME, new BigDecimal(10),
68 new Long(2), null, new Long(0), null, null);
69
70 List<StatisticAlertThresholds> list = statisticService.getAlertThresholds();
71 assertFalse(list.contains(thresholds));
72
73 statisticService.saveStatisticAlertThresholds(thresholds);
74
75 list = statisticService.getAlertThresholds();
76 assertTrue(list.contains(thresholds));
77
78 thresholds.setThresholdCountMin(-100l);
79 assertFalse(list.contains(thresholds));
80
81 statisticService.saveStatisticAlertThresholds(thresholds);
82
83 list = statisticService.getAlertThresholds();
84 assertTrue(list.contains(thresholds));
85
86 statisticService.removeStatisticAlertThresholds(TEST_STAT_NAME);
87 }
88
89 @Test
90 public void testNotificationCreationForTotals() throws Exception {
91 Statistic stat = new Statistic(StatisticName.INCOMING_BATCH_COUNT, "010101");
92 StatisticAlertThresholds thresholds = new StatisticAlertThresholds(StatisticName.INCOMING_BATCH_COUNT.name(),
93 new BigDecimal(10), null, new BigDecimal(1), null, null, null);
94 assertNotNull(thresholds.outsideOfBoundsNotification(stat));
95 stat.add(new BigDecimal(1));
96 assertNull(thresholds.outsideOfBoundsNotification(stat));
97 stat.add(new BigDecimal(9));
98 assertNull(thresholds.outsideOfBoundsNotification(stat));stat.add(new BigDecimal(1));
99 Notification event = thresholds.outsideOfBoundsNotification(stat);
100 assertNotNull(event);
101 String expectedMsg = stat.getName().name() + ":total=11";
102 assertEquals(event.getMessage(), expectedMsg);
103 }
104 }