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.mockito.ArgumentMatchers.eq; 022import static org.mockito.Mockito.any; 023import static org.mockito.Mockito.mock; 024import static org.mockito.Mockito.when; 025 026import java.io.PrintWriter; 027import java.io.StringWriter; 028import java.lang.reflect.Type; 029import java.util.HashSet; 030import java.util.Map; 031import java.util.Set; 032import javax.management.MBeanAttributeInfo; 033import javax.management.MBeanInfo; 034import javax.management.MBeanServer; 035import javax.management.ObjectName; 036import org.apache.hadoop.hbase.HBaseClassTestRule; 037import org.apache.hadoop.hbase.testclassification.MiscTests; 038import org.apache.hadoop.hbase.testclassification.SmallTests; 039import org.junit.ClassRule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042 043import org.apache.hbase.thirdparty.com.google.common.reflect.TypeToken; 044import org.apache.hbase.thirdparty.com.google.gson.Gson; 045 046/** 047 * Test {@link JSONBean}. 048 */ 049@Category({ MiscTests.class, SmallTests.class }) 050public class TestJSONBean { 051 @ClassRule 052 public static final HBaseClassTestRule CLASS_RULE = 053 HBaseClassTestRule.forClass(TestJSONBean.class); 054 055 private MBeanServer getMockMBeanServer() throws Exception { 056 MBeanServer mbeanServer = mock(MBeanServer.class); 057 Set<ObjectName> names = new HashSet<>(); 058 names.add(new ObjectName("test1:type=test2")); 059 when(mbeanServer.queryNames(any(), any())).thenReturn(names); 060 MBeanInfo mbeanInfo = mock(MBeanInfo.class); 061 when(mbeanInfo.getClassName()).thenReturn("testClassName"); 062 String[] attributeNames = 063 new String[] { "intAttr", "nanAttr", "infinityAttr", "strAttr", "boolAttr", "test:Attr" }; 064 MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributeNames.length]; 065 for (int i = 0; i < attributeInfos.length; i++) { 066 attributeInfos[i] = new MBeanAttributeInfo(attributeNames[i], null, null, true, false, false); 067 } 068 when(mbeanInfo.getAttributes()).thenReturn(attributeInfos); 069 when(mbeanServer.getMBeanInfo(any())).thenReturn(mbeanInfo); 070 when(mbeanServer.getAttribute(any(), eq("intAttr"))).thenReturn(3); 071 when(mbeanServer.getAttribute(any(), eq("nanAttr"))).thenReturn(Double.NaN); 072 when(mbeanServer.getAttribute(any(), eq("infinityAttr"))).thenReturn(Double.POSITIVE_INFINITY); 073 when(mbeanServer.getAttribute(any(), eq("strAttr"))).thenReturn("aString"); 074 when(mbeanServer.getAttribute(any(), eq("boolAttr"))).thenReturn(true); 075 when(mbeanServer.getAttribute(any(), eq("test:Attr"))).thenReturn("aString"); 076 return mbeanServer; 077 } 078 079 private String getExpectedJSON() { 080 StringWriter sw = new StringWriter(); 081 PrintWriter pw = new PrintWriter(sw); 082 pw.println("{"); 083 pw.println(" \"beans\": ["); 084 pw.println(" {"); 085 pw.println(" \"name\": \"test1:type=test2\","); 086 pw.println(" \"modelerType\": \"testClassName\","); 087 pw.println(" \"intAttr\": 3,"); 088 pw.println(" \"nanAttr\": \"NaN\","); 089 pw.println(" \"infinityAttr\": \"Infinity\","); 090 pw.println(" \"strAttr\": \"aString\","); 091 pw.println(" \"boolAttr\": true,"); 092 pw.println(" \"test:Attr\": aString"); 093 pw.println(" }"); 094 pw.println(" ]"); 095 pw.print("}"); 096 return sw.toString(); 097 } 098 099 @Test 100 public void testJSONBeanValueTypes() throws Exception { 101 JSONBean bean = new JSONBean(); 102 StringWriter stringWriter = new StringWriter(); 103 try (PrintWriter printWriter = new PrintWriter(stringWriter); 104 JSONBean.Writer jsonWriter = bean.open(printWriter)) { 105 jsonWriter.write(getMockMBeanServer(), null, null, false); 106 } 107 108 final Gson gson = GsonUtil.createGson().create(); 109 Type typeOfHashMap = new TypeToken<Map<String, Object>>() { 110 }.getType(); 111 Map<String, Object> expectedJson = gson.fromJson(getExpectedJSON(), typeOfHashMap); 112 Map<String, Object> actualJson = gson.fromJson(stringWriter.toString(), typeOfHashMap); 113 assertEquals(expectedJson, actualJson); 114 } 115}