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.master.http;
019
020import static org.junit.Assert.assertEquals;
021
022import java.lang.reflect.Method;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.List;
027import java.util.Objects;
028import java.util.Random;
029import java.util.stream.Collectors;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.RegionMetrics;
032import org.apache.hadoop.hbase.RegionMetricsBuilder;
033import org.apache.hadoop.hbase.ServerName;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.client.RegionInfo;
036import org.apache.hadoop.hbase.client.RegionInfoBuilder;
037import org.apache.hadoop.hbase.master.http.RegionVisualizer.RegionDetails;
038import org.apache.hadoop.hbase.testclassification.MasterTests;
039import org.apache.hadoop.hbase.testclassification.SmallTests;
040import org.junit.Assert;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045
046import org.apache.hbase.thirdparty.com.google.gson.Gson;
047import org.apache.hbase.thirdparty.com.google.gson.JsonObject;
048
049@Category({ MasterTests.class, SmallTests.class })
050public class TestRegionVisualizer {
051
052  @ClassRule
053  public static final HBaseClassTestRule testRule =
054    HBaseClassTestRule.forClass(TestRegionVisualizer.class);
055
056  private static final Random rand = new Random();
057  private static List<Method> regionMetricsBuilderLongValueSetters;
058
059  @BeforeClass
060  public static void beforeClass() {
061    regionMetricsBuilderLongValueSetters =
062      Arrays.stream(RegionMetricsBuilder.class.getDeclaredMethods())
063        .filter(method -> method.getName().startsWith("set"))
064        .filter(method -> method.getParameterTypes().length == 1)
065        .filter(method -> Objects.equals(method.getParameterTypes()[0], long.class))
066        .collect(Collectors.toList());
067  }
068
069  @Test
070  public void testRegionDetailsJsonSerialization() throws Exception {
071    final ServerName serverName =
072      ServerName.valueOf("example.org", 1234, System.currentTimeMillis());
073    final TableName tableName = TableName.valueOf("foo", "bar");
074    final RegionDetails regionDetails =
075      new RegionDetails(serverName, tableName, buildRegionMetrics(tableName));
076
077    final Gson gson = RegionVisualizer.buildGson();
078    final JsonObject result = gson.fromJson(gson.toJson(regionDetails), JsonObject.class);
079    Assert.assertNotNull(result);
080    assertEquals(serverName.toShortString(), result.get("server_name").getAsString());
081    assertEquals(tableName.getNameAsString(), result.get("table_name").getAsString());
082  }
083
084  /**
085   * Build a {@link RegionMetrics} object for {@code tableName}. Populates a couple random fields
086   * with non-empty values.
087   */
088  final RegionMetrics buildRegionMetrics(final TableName tableName) throws Exception {
089    final List<Method> setters = new ArrayList<>(regionMetricsBuilderLongValueSetters);
090    Collections.shuffle(setters, rand);
091
092    final RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build();
093    final RegionMetricsBuilder builder =
094      RegionMetricsBuilder.newBuilder(regionInfo.getRegionName());
095    for (final Method setter : setters.subList(0, 3)) {
096      setter.invoke(builder, rand.nextLong());
097    }
098    return builder.build();
099  }
100}