Bug 1446: Add JMX stats for clustered data store
[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.md.sal.common.util.jmx.AbstractMXBean;
15
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
18
19 import java.lang.management.ManagementFactory;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22
23 public class ShardStatsTest {
24     private MBeanServer mbeanServer;
25     private ShardStats shardStats;
26     private ObjectName testMBeanName;
27
28     @Before
29     public void setUp() throws Exception {
30
31         shardStats = new ShardStats("shard-1", "DataStore");
32         shardStats.registerMBean();
33         mbeanServer = ManagementFactory.getPlatformMBeanServer();
34         String objectName =
35             AbstractMXBean.BASE_JMX_PREFIX + "type=" + shardStats
36                 .getMBeanType() + ",Category=" +
37                 shardStats.getMBeanCategory() + ",name=" +
38                 shardStats.getMBeanName();
39         testMBeanName = new ObjectName(objectName);
40     }
41
42     @After
43     public void tearDown() throws Exception {
44         shardStats.unregisterMBean();
45     }
46
47     @Test
48     public void testGetShardName() throws Exception {
49
50         Object attribute = mbeanServer.getAttribute(testMBeanName, "ShardName");
51         Assert.assertEquals(attribute, "shard-1");
52
53     }
54
55     @Test
56     public void testGetCommittedTransactionsCount() throws Exception {
57         //let us increment some transactions count and then check
58         shardStats.incrementCommittedTransactionCount();
59         shardStats.incrementCommittedTransactionCount();
60         shardStats.incrementCommittedTransactionCount();
61
62         //now let us get from MBeanServer what is the transaction count.
63         Object attribute = mbeanServer.getAttribute(testMBeanName,
64             "CommittedTransactionsCount");
65         Assert.assertEquals(attribute, 3L);
66
67
68     }
69
70     @Test
71     public void testGetLastCommittedTransactionTime() throws Exception {
72         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
73         Assert.assertEquals(shardStats.getLastCommittedTransactionTime(),
74             sdf.format(new Date(0L)));
75         long millis = System.currentTimeMillis();
76         shardStats.setLastCommittedTransactionTime(millis);
77
78         //now let us get from MBeanServer what is the transaction count.
79         Object attribute = mbeanServer.getAttribute(testMBeanName,
80             "LastCommittedTransactionTime");
81         Assert.assertEquals(attribute, sdf.format(new Date(millis)));
82         Assert.assertNotEquals(attribute,
83             sdf.format(new Date(millis - 1)));
84
85     }
86
87     @Test
88     public void testGetFailedTransactionsCount() throws Exception {
89         //let us increment some transactions count and then check
90         shardStats.incrementFailedTransactionsCount();
91         shardStats.incrementFailedTransactionsCount();
92
93
94         //now let us get from MBeanServer what is the transaction count.
95         Object attribute =
96             mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount");
97         Assert.assertEquals(attribute, 2L);
98     }
99
100     @Test
101     public void testGetAbortTransactionsCount() throws Exception {
102         //let us increment AbortTransactions count and then check
103         shardStats.incrementAbortTransactionsCount();
104         shardStats.incrementAbortTransactionsCount();
105
106
107         //now let us get from MBeanServer what is the transaction count.
108         Object attribute =
109             mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount");
110         Assert.assertEquals(attribute, 2L);
111     }
112
113     @Test
114     public void testGetFailedReadTransactionsCount() throws Exception {
115         //let us increment FailedReadTransactions count and then check
116         shardStats.incrementFailedReadTransactionsCount();
117         shardStats.incrementFailedReadTransactionsCount();
118
119
120         //now let us get from MBeanServer what is the transaction count.
121         Object attribute =
122             mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
123         Assert.assertEquals(attribute, 2L);
124     }
125
126     @Test
127     public void testResetTransactionCounters() throws Exception {
128
129         //let us increment committed transactions count and then check
130         shardStats.incrementCommittedTransactionCount();
131         shardStats.incrementCommittedTransactionCount();
132         shardStats.incrementCommittedTransactionCount();
133
134         //now let us get from MBeanServer what is the transaction count.
135         Object attribute = mbeanServer.getAttribute(testMBeanName,
136             "CommittedTransactionsCount");
137         Assert.assertEquals(attribute, 3L);
138
139         //let us increment FailedReadTransactions count and then check
140         shardStats.incrementFailedReadTransactionsCount();
141         shardStats.incrementFailedReadTransactionsCount();
142
143
144         //now let us get from MBeanServer what is the transaction count.
145         attribute =
146             mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
147         Assert.assertEquals(attribute, 2L);
148
149
150         //here we will reset the counters and check the above ones are 0 after reset
151         mbeanServer.invoke(testMBeanName, "resetTransactionCounters", null, null);
152
153         //now let us get from MBeanServer what is the transaction count.
154         attribute = mbeanServer.getAttribute(testMBeanName,
155             "CommittedTransactionsCount");
156         Assert.assertEquals(attribute, 0L);
157
158         attribute =
159             mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
160         Assert.assertEquals(attribute, 0L);
161
162
163     }
164 }