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