2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard;
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;
16 import javax.management.MBeanServer;
17 import javax.management.ObjectName;
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
21 public class ShardStatsTest {
22 private MBeanServer mbeanServer;
23 private ShardStats shardStats;
24 private ObjectName testMBeanName;
27 public void setUp() throws Exception {
29 shardStats = new ShardStats("shard-1");
30 shardStats.registerMBean();
31 mbeanServer = shardStats.getMBeanServer();
33 AbstractBaseMBean.BASE_JMX_PREFIX + "type=" + shardStats
34 .getMBeanType() + ",Category=" +
35 shardStats.getMBeanCategory() + ",name=" +
36 shardStats.getMBeanName();
37 testMBeanName = new ObjectName(objectName);
41 public void tearDown() throws Exception {
42 shardStats.unregisterMBean();
46 public void testGetShardName() throws Exception {
48 Object attribute = mbeanServer.getAttribute(testMBeanName, "ShardName");
49 Assert.assertEquals((String) attribute, "shard-1");
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();
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);
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));
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)));
86 public void testGetFailedTransactionsCount() throws Exception {
87 //let us increment some transactions count and then check
88 shardStats.incrementFailedTransactionsCount();
89 shardStats.incrementFailedTransactionsCount();
92 //now let us get from MBeanServer what is the transaction count.
94 mbeanServer.getAttribute(testMBeanName, "FailedTransactionsCount");
95 Assert.assertEquals((Long) attribute, (Long) 2L);
99 public void testGetAbortTransactionsCount() throws Exception {
100 //let us increment AbortTransactions count and then check
101 shardStats.incrementAbortTransactionsCount();
102 shardStats.incrementAbortTransactionsCount();
105 //now let us get from MBeanServer what is the transaction count.
107 mbeanServer.getAttribute(testMBeanName, "AbortTransactionsCount");
108 Assert.assertEquals((Long) attribute, (Long) 2L);
112 public void testGetFailedReadTransactionsCount() throws Exception {
113 //let us increment FailedReadTransactions count and then check
114 shardStats.incrementFailedReadTransactionsCount();
115 shardStats.incrementFailedReadTransactionsCount();
118 //now let us get from MBeanServer what is the transaction count.
120 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
121 Assert.assertEquals((Long) attribute, (Long) 2L);
125 public void testResetTransactionCounters() throws Exception {
127 //let us increment committed transactions count and then check
128 shardStats.incrementCommittedTransactionCount();
129 shardStats.incrementCommittedTransactionCount();
130 shardStats.incrementCommittedTransactionCount();
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);
137 //let us increment FailedReadTransactions count and then check
138 shardStats.incrementFailedReadTransactionsCount();
139 shardStats.incrementFailedReadTransactionsCount();
142 //now let us get from MBeanServer what is the transaction count.
144 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
145 Assert.assertEquals((Long) attribute, (Long) 2L);
148 //here we will reset the counters and check the above ones are 0 after reset
149 mbeanServer.invoke(testMBeanName, "resetTransactionCounters", null, null);
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);
157 mbeanServer.getAttribute(testMBeanName, "FailedReadTransactionsCount");
158 Assert.assertEquals((Long) attribute, (Long) 0L);