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.http.conf;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.StringReader;
025import java.io.StringWriter;
026import java.util.HashSet;
027import java.util.Map;
028import java.util.Set;
029import javax.xml.parsers.DocumentBuilder;
030import javax.xml.parsers.DocumentBuilderFactory;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.hbase.testclassification.MiscTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036import org.w3c.dom.Document;
037import org.w3c.dom.Element;
038import org.w3c.dom.Node;
039import org.w3c.dom.NodeList;
040import org.xml.sax.InputSource;
041
042import org.apache.hbase.thirdparty.org.eclipse.jetty.util.ajax.JSON;
043
044/**
045 * Basic test case that the ConfServlet can write configuration to its output in XML and JSON
046 * format.
047 */
048@Tag(MiscTests.TAG)
049@Tag(SmallTests.TAG)
050public class TestConfServlet {
051
052  private static final String TEST_KEY = "testconfservlet.key";
053  private static final String TEST_VAL = "testval";
054
055  private Configuration getTestConf() {
056    Configuration testConf = new Configuration();
057    testConf.set(TEST_KEY, TEST_VAL);
058    return testConf;
059  }
060
061  @Test
062  @SuppressWarnings("unchecked")
063  public void testWriteJson() throws Exception {
064    StringWriter sw = new StringWriter();
065    ConfServlet.writeResponse(getTestConf(), sw, "json");
066    String json = sw.toString();
067    boolean foundSetting = false;
068    Set<String> programSet = new HashSet<>();
069    programSet.add("programatically");
070    programSet.add("programmatically");
071    Object parsed = new JSON().fromJSON(json);
072    Object[] properties = ((Map<String, Object[]>) parsed).get("properties");
073    for (Object o : properties) {
074      Map<String, Object> propertyInfo = (Map<String, Object>) o;
075      String key = (String) propertyInfo.get("key");
076      String val = (String) propertyInfo.get("value");
077      String resource = (String) propertyInfo.get("resource");
078      System.err.println("k: " + key + " v: " + val + " r: " + resource);
079      if (TEST_KEY.equals(key) && TEST_VAL.equals(val) && programSet.contains(resource)) {
080        foundSetting = true;
081      }
082    }
083    assertTrue(foundSetting);
084  }
085
086  @Test
087  public void testWriteXml() throws Exception {
088    StringWriter sw = new StringWriter();
089    ConfServlet.writeResponse(getTestConf(), sw, "xml");
090    String xml = sw.toString();
091
092    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
093    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
094    Document doc = builder.parse(new InputSource(new StringReader(xml)));
095    NodeList nameNodes = doc.getElementsByTagName("name");
096    boolean foundSetting = false;
097    for (int i = 0; i < nameNodes.getLength(); i++) {
098      Node nameNode = nameNodes.item(i);
099      String key = nameNode.getTextContent();
100      System.err.println("xml key: " + key);
101      if (TEST_KEY.equals(key)) {
102        foundSetting = true;
103        Element propertyElem = (Element) nameNode.getParentNode();
104        String val = propertyElem.getElementsByTagName("value").item(0).getTextContent();
105        assertEquals(TEST_VAL, val);
106      }
107    }
108    assertTrue(foundSetting);
109  }
110
111  @Test
112  public void testMask() {
113    final String passwordKey = "hbase.rpc.tls.keystore.password";
114    Configuration conf = getTestConf();
115    conf.set(passwordKey, "MyPassword");
116    Configuration maskedConf = ConfServlet.mask(conf);
117    assertEquals(ConfServlet.MASKED, maskedConf.get(passwordKey));
118  }
119
120  @Test
121  public void testBadFormat() throws Exception {
122    StringWriter sw = new StringWriter();
123    assertThrows(ConfServlet.BadFormatException.class,
124      () -> ConfServlet.writeResponse(getTestConf(), sw, "not a format"));
125    assertEquals("", sw.toString());
126  }
127}