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