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