Include lastCommittedTransactionTime in Shard JMX
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / jmx / mbeans / shard / ShardStatsTest.java
1 package org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard;
2
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.AbstractBaseMBean;
8
9 import javax.management.MBeanServer;
10 import javax.management.ObjectName;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13
14 public class ShardStatsTest {
15     private MBeanServer mbeanServer;
16     private ShardStats shardStats;
17     private ObjectName testMBeanName;
18
19     @Before
20     public void setUp() throws Exception {
21
22         shardStats = new ShardStats("shard-1");
23         shardStats.registerMBean();
24         mbeanServer = shardStats.getMBeanServer();
25         String objectName =
26             AbstractBaseMBean.BASE_JMX_PREFIX + "type=" + shardStats
27                 .getMBeanType() + ",Category=" +
28                 shardStats.getMBeanCategory() + ",name=" +
29                 shardStats.getMBeanName();
30         testMBeanName = new ObjectName(objectName);
31     }
32
33     @After
34     public void tearDown() throws Exception {
35         shardStats.unregisterMBean();
36     }
37
38     @Test
39     public void testGetShardName() throws Exception {
40
41         Object attribute = mbeanServer.getAttribute(testMBeanName, "ShardName");
42         Assert.assertEquals((String) attribute, "shard-1");
43
44     }
45
46     @Test
47     public void testGetCommittedTransactionsCount() throws Exception {
48         //let us increment some transactions count and then check
49         shardStats.incrementCommittedTransactionCount();
50         shardStats.incrementCommittedTransactionCount();
51         shardStats.incrementCommittedTransactionCount();
52
53         //now let us get from MBeanServer what is the transaction count.
54         Object attribute = mbeanServer.getAttribute(testMBeanName,
55             "CommittedTransactionsCount");
56         Assert.assertEquals((Long) attribute, (Long) 3L);
57
58
59     }
60
61     @Test
62     public void testGetLastCommittedTransactionTime() throws Exception {
63         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
64         Assert.assertEquals(shardStats.getLastCommittedTransactionTime(),
65             sdf.format(new Date(0L)));
66         long millis = System.currentTimeMillis();
67         shardStats.setLastCommittedTransactionTime(new Date(millis));
68
69         //now let us get from MBeanServer what is the transaction count.
70         Object attribute = mbeanServer.getAttribute(testMBeanName,
71             "LastCommittedTransactionTime");
72         Assert.assertEquals((String) attribute, sdf.format(new Date(millis)));
73         Assert.assertNotEquals((String) attribute,
74             sdf.format(new Date(millis - 1)));
75
76     }
77
78     @Test
79     public void testGetFailedTransactionsCount() throws Exception {
80         //let us increment some transactions count and then check
81         shardStats.incrementFailedTransactionsCount();
82         shardStats.incrementFailedTransactionsCount();
83
84
85         //now let us get from MBeanServer what is the transaction count.
86         Object attribute =
87             mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount");
88         Assert.assertEquals((Long) attribute, (Long) 2L);
89
90
91
92     }
93 }