001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertNull;
023
024import com.fasterxml.jackson.core.JsonProcessingException;
025import com.fasterxml.jackson.databind.JsonNode;
026import java.io.IOException;
027import java.lang.management.GarbageCollectorMXBean;
028import java.lang.management.ManagementFactory;
029import java.util.Hashtable;
030import java.util.List;
031import java.util.Map;
032import javax.management.MalformedObjectNameException;
033import javax.management.ObjectName;
034import javax.management.openmbean.CompositeData;
035import org.apache.hadoop.hbase.HBaseClassTestRule;
036import org.apache.hadoop.hbase.testclassification.MiscTests;
037import org.apache.hadoop.hbase.testclassification.SmallTests;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044@Category({MiscTests.class, SmallTests.class})
045public class TestJSONMetricUtil {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestJSONMetricUtil.class);
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestJSONMetricUtil.class);
052
053  @Test
054  public void testBuildHashtable() {
055    String[] keys = {"type", "name"};
056    String[] emptyKey = {};
057    String[] values = {"MemoryPool", "Par Eden Space"};
058    String[] values2 = {"MemoryPool", "Par Eden Space", "Test"};
059    String[] emptyValue = {};
060    Map<String, String> properties = JSONMetricUtil.buldKeyValueTable(keys, values);
061    assertEquals(values[0], properties.get("type"));
062    assertEquals(values[1], properties.get("name"));
063
064    assertNull(JSONMetricUtil.buldKeyValueTable(keys, values2));
065    assertNull(JSONMetricUtil.buldKeyValueTable(keys, emptyValue));
066    assertNull(JSONMetricUtil.buldKeyValueTable(emptyKey, values2));
067    assertNull(JSONMetricUtil.buldKeyValueTable(emptyKey, emptyValue));
068  }
069
070  @Test
071  public void testSearchJson() throws JsonProcessingException, IOException {
072    String jsonString = "{\"test\":[{\"data1\":100,\"data2\":\"hello\",\"data3\": [1 , 2 , 3]}, "
073        + "{\"data4\":0}]}";
074    JsonNode  node = JSONMetricUtil.mappStringToJsonNode(jsonString);
075    JsonNode r1 = JSONMetricUtil.searchJson(node, "data1");
076    JsonNode r2 = JSONMetricUtil.searchJson(node, "data2");
077    JsonNode r3 = JSONMetricUtil.searchJson(node, "data3");
078    JsonNode r4 = JSONMetricUtil.searchJson(node, "data4");
079    assertEquals(100, r1.intValue());
080    assertEquals("hello", r2.textValue());
081    assertEquals(1, r3.get(0).intValue());
082    assertEquals(0, r4.intValue());
083  }
084
085  @Test
086  public void testBuildObjectName() throws MalformedObjectNameException {
087    String[] keys = {"type", "name"};
088    String[] values = {"MemoryPool", "Par Eden Space"};
089    Hashtable<String, String> properties = JSONMetricUtil.buldKeyValueTable(keys, values);
090    ObjectName testObject = JSONMetricUtil.buildObjectName(JSONMetricUtil.JAVA_LANG_DOMAIN,
091      properties);
092    assertEquals(JSONMetricUtil.JAVA_LANG_DOMAIN, testObject.getDomain());
093    assertEquals(testObject.getKeyPropertyList(), properties);
094  }
095
096  @Test
097  public void testGetLastGCInfo() {
098    List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
099    for(GarbageCollectorMXBean bean:gcBeans) {
100      ObjectName on = bean.getObjectName();
101      Object value = JSONMetricUtil.getValueFromMBean(on, "LastGcInfo");
102      LOG.info("Collector Info: "+ value);
103      if (value != null && value instanceof CompositeData) {
104        CompositeData cds = (CompositeData)value;
105        assertNotNull(cds.get("duration"));
106      }
107    }
108  }
109}