Include JMX Counters and resetTransactionCounters
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / jmx / mbeans / shard / ShardStatsTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard;
9
10 import org.junit.After;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.AbstractBaseMBean;
15
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
20
21 public class ShardStatsTest {
22     private MBeanServer mbeanServer;
23     private ShardStats shardStats;
24     private ObjectName testMBeanName;
25
26     @Before
27     public void setUp() throws Exception {
28
29         shardStats = new ShardStats("shard-1");
30         shardStats.registerMBean();
31         mbeanServer = shardStats.getMBeanServer();
32         String objectName =
33             AbstractBaseMBean.BASE_JMX_PREFIX + "type=" + shardStats
34                 .getMBeanType() + ",Category=" +
35                 shardStats.getMBeanCategory() + ",name=" +
36                 shardStats.getMBeanName();
37         testMBeanName = new ObjectName(objectName);
38     }
39
40     @After
41     public void tearDown() throws Exception {
42         shardStats.unregisterMBean();
43     }
44
45     @Test
46     public void testGetShardName() throws Exception {
47
48         Object attribute = mbeanServer.getAttribute(testMBeanName, "ShardName");
49         Assert.assertEquals((String) attribute, "shard-1");
50
51     }
52
53     @Test
54     public void testGetCommittedTransactionsCount() throws Exception {
55         //let us increment some transactions count and then check
56         shardStats.incrementCommittedTransactionCount();
57         shardStats.incrementCommittedTransactionCount();
58         shardStats.incrementCommittedTransactionCount();
59
60         //now let us get from MBeanServer what is the transaction count.
61         Object attribute = mbeanServer.getAttribute(testMBeanName,
62             "CommittedTransactionsCount");
63         Assert.assertEquals((Long) attribute, (Long) 3L);
64
65
66     }
67
68     @Test
69     public void testGetLastCommittedTransactionTime() throws Exception {
70         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
71         Assert.assertEquals(shardStats.getLastCommittedTransactionTime(),
72             sdf.format(new Date(0L)));
73         long millis = System.currentTimeMillis();
74         shardStats.setLastCommittedTransactionTime(new Date(millis));
75
76         //now let us get from MBeanServer what is the transaction count.
77         Object attribute = mbeanServer.getAttribute(testMBeanName,
78             "LastCommittedTransactionTime");
79         Assert.assertEquals((String) attribute, sdf.format(new Date(millis)));
80         Assert.assertNotEquals((String) attribute,
81             sdf.format(new Date(millis - 1)));
82
83     }
84
85     @Test
86     public void testGetFailedTransactionsCount() throws Exception {
87         //let us increment some transactions count and then check
88         shardStats.incrementFailedTransactionsCount();
89         shardStats.incrementFailedTransactionsCount();
90
91
92         //now let us get from MBeanServer what is the transaction count.
93         Object attribute =
94             mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount");
95         Assert.assertEquals((Long) attribute, (Long) 2L);
96     }
97
98     @Test
99     public void testGetAbortTransactionsCount() throws Exception {
100         //let us increment AbortTransactions count and then check
101         shardStats.incrementAbortTransactionsCount();
102         shardStats.incrementAbortTransactionsCount();
103
104
105         //now let us get from MBeanServer what is the transaction count.
106         Object attribute =
107             mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount");
108         Assert.assertEquals((Long) attribute, (Long) 2L);
109     }
110
111     @Test
112     public void testGetFailedReadTransactionsCount() throws Exception {
113         //let us increment FailedReadTransactions count and then check
114         shardStats.incrementFailedReadTransactionsCount();
115         shardStats.incrementFailedReadTransactionsCount();
116
117
118         //now let us get from MBeanServer what is the transaction count.
119         Object attribute =
120             mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
121         Assert.assertEquals((Long) attribute, (Long) 2L);
122     }
123
124     @Test
125     public void testResetTransactionCounters() throws Exception {
126
127         //let us increment committed transactions count and then check
128         shardStats.incrementCommittedTransactionCount();
129         shardStats.incrementCommittedTransactionCount();
130         shardStats.incrementCommittedTransactionCount();
131
132         //now let us get from MBeanServer what is the transaction count.
133         Object attribute = mbeanServer.getAttribute(testMBeanName,
134             "CommittedTransactionsCount");
135         Assert.assertEquals((Long) attribute, (Long) 3L);
136
137         //let us increment FailedReadTransactions count and then check
138         shardStats.incrementFailedReadTransactionsCount();
139         shardStats.incrementFailedReadTransactionsCount();
140
141
142         //now let us get from MBeanServer what is the transaction count.
143         attribute =
144             mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
145         Assert.assertEquals((Long) attribute, (Long) 2L);
146
147
148         //here we will reset the counters and check the above ones are 0 after reset
149         mbeanServer.invoke(testMBeanName, "resetTransactionCounters", null, null);
150
151         //now let us get from MBeanServer what is the transaction count.
152         attribute = mbeanServer.getAttribute(testMBeanName,
153             "CommittedTransactionsCount");
154         Assert.assertEquals((Long) attribute, (Long) 0L);
155
156         attribute =
157             mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
158         Assert.assertEquals((Long) attribute, (Long) 0L);
159
160
161     }
162 }