1 package org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard;
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
7 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.AbstractBaseMBean;
9 import javax.management.MBeanServer;
10 import javax.management.ObjectName;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
14 public class ShardStatsTest {
15 private MBeanServer mbeanServer;
16 private ShardStats shardStats;
17 private ObjectName testMBeanName;
20 public void setUp() throws Exception {
22 shardStats = new ShardStats("shard-1");
23 shardStats.registerMBean();
24 mbeanServer = shardStats.getMBeanServer();
26 AbstractBaseMBean.BASE_JMX_PREFIX + "type=" + shardStats
27 .getMBeanType() + ",Category=" +
28 shardStats.getMBeanCategory() + ",name=" +
29 shardStats.getMBeanName();
30 testMBeanName = new ObjectName(objectName);
34 public void tearDown() throws Exception {
35 shardStats.unregisterMBean();
39 public void testGetShardName() throws Exception {
41 Object attribute = mbeanServer.getAttribute(testMBeanName, "ShardName");
42 Assert.assertEquals((String) attribute, "shard-1");
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();
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);
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));
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)));
79 public void testGetFailedTransactionsCount() throws Exception {
80 //let us increment some transactions count and then check
81 shardStats.incrementFailedTransactionsCount();
82 shardStats.incrementFailedTransactionsCount();
85 //now let us get from MBeanServer what is the transaction count.
87 mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount");
88 Assert.assertEquals((Long) attribute, (Long) 2L);