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