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.gson;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.util.Map;
023import java.util.TreeMap;
024import org.apache.hadoop.hbase.Size;
025import org.apache.hadoop.hbase.testclassification.MasterTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.jupiter.api.BeforeAll;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032import org.apache.hbase.thirdparty.com.google.gson.Gson;
033
034@Tag(MasterTests.TAG)
035@Tag(SmallTests.TAG)
036public class GsonFactoryTest {
037
038  private static Gson gson;
039
040  @BeforeAll
041  public static void beforeClass() {
042    gson = GsonFactory.buildGson();
043  }
044
045  @Test
046  public void testSerializeToLowerCaseUnderscores() {
047    final SomeBean input = new SomeBean(false, 57, "hello\n");
048    final String actual = gson.toJson(input);
049    final String expected = "{\"a_boolean\":false,\"an_int\":57,\"a_string\":\"hello\\n\"}";
050    assertEquals(expected, actual);
051  }
052
053  @Test
054  public void testSerializeMapWithSizeKeys() {
055    final Map<Size, String> input = new TreeMap<>();
056    input.put(new Size(10, Size.Unit.KILOBYTE), "10kb");
057    input.put(new Size(5, Size.Unit.MEGABYTE), "5mb");
058    final String actual = gson.toJson(input);
059    final String expected = "{\"10240.0\":\"10kb\",\"5242880.0\":\"5mb\"}";
060    assertEquals(expected, actual);
061  }
062
063  @Test
064  public void testSerializeNonPrintableByteArrays() {
065    final Map<byte[], byte[]> input = new TreeMap<>(Bytes.BYTES_COMPARATOR);
066    input.put(Bytes.toBytes("this is printable"), new byte[] { 0, 1, 2, 3, 4, 5 });
067    input.put(new byte[] { -127, -63, 0, 63, 127 }, Bytes.toBytes("test"));
068    final String actual = gson.toJson(input);
069    final String expected =
070      "{" + "\"this is printable\":\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\","
071        + "\"��\\u0000?\u007F\":\"test\"}";
072    assertEquals(expected, actual);
073  }
074
075  private static final class SomeBean {
076    private final boolean aBoolean;
077    private final int anInt;
078    private final String aString;
079
080    public SomeBean(final boolean aBoolean, final int anInt, final String aString) {
081      this.aBoolean = aBoolean;
082      this.anInt = anInt;
083      this.aString = aString;
084    }
085
086    public boolean isaBoolean() {
087      return aBoolean;
088    }
089
090    public int getAnInt() {
091      return anInt;
092    }
093
094    public String getaString() {
095      return aString;
096    }
097  }
098}