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