View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2022 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package waffle.servlet;
25  
26  import com.sun.jna.Platform;
27  
28  import java.io.IOException;
29  import java.io.StringReader;
30  
31  import javax.servlet.ServletException;
32  import javax.xml.parsers.DocumentBuilder;
33  import javax.xml.parsers.DocumentBuilderFactory;
34  import javax.xml.parsers.ParserConfigurationException;
35  
36  import org.junit.jupiter.api.Assertions;
37  import org.junit.jupiter.api.Test;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  import org.w3c.dom.Document;
41  import org.w3c.dom.Node;
42  import org.xml.sax.InputSource;
43  import org.xml.sax.SAXException;
44  
45  import waffle.mock.http.SimpleHttpRequest;
46  import waffle.mock.http.SimpleHttpResponse;
47  
48  /**
49   * Test the WaffleInfoServlet.
50   */
51  class WaffleInfoServletTest {
52  
53      /** The Constant LOGGER. */
54      private static final Logger LOGGER = LoggerFactory.getLogger(WaffleInfoServletTest.class);
55  
56      /**
57       * Test get info.
58       *
59       * @throws ParserConfigurationException
60       *             the parser configuration exception
61       * @throws SAXException
62       *             the SAX exception
63       * @throws IOException
64       *             Signals that an I/O exception has occurred.
65       * @throws ServletException
66       *             the servlet exception
67       */
68      @Test
69      void testGetInfo() throws ParserConfigurationException, SAXException, IOException, ServletException {
70          final SimpleHttpRequest request = new SimpleHttpRequest();
71          request.addHeader("hello", "waffle");
72  
73          final SimpleHttpResponse response = new SimpleHttpResponse();
74  
75          final WaffleInfoServlet servlet = new WaffleInfoServlet();
76          servlet.doGet(request, response);
77  
78          final String xml = response.getOutputText();
79          final Document doc = WaffleInfoServletTest.loadXMLFromString(xml);
80  
81          WaffleInfoServletTest.LOGGER.info("GOT: {}", xml);
82  
83          // Make sure JNA Version is properly noted
84          Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
85                  doc.getDocumentElement().getAttribute("jna"));
86  
87          final Node node = doc.getDocumentElement().getFirstChild().getNextSibling() // request
88                  .getFirstChild().getNextSibling() // AuthType
89                  .getNextSibling().getNextSibling();
90  
91          // Make sure the headers were added correctly
92          Assertions.assertEquals("headers", node.getNodeName());
93          final Node child = node.getFirstChild().getNextSibling();
94          Assertions.assertEquals("hello", child.getNodeName());
95      }
96  
97      /**
98       * Load xml from string.
99       *
100      * @param xml
101      *            the xml
102      *
103      * @return the document
104      *
105      * @throws ParserConfigurationException
106      *             the parser configuration exception
107      * @throws SAXException
108      *             the SAX exception
109      * @throws IOException
110      *             Signals that an I/O exception has occurred.
111      */
112     private static Document loadXMLFromString(final String xml)
113             throws ParserConfigurationException, SAXException, IOException {
114         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
115         final DocumentBuilder builder = factory.newDocumentBuilder();
116         final InputSource is = new InputSource(new StringReader(xml));
117         return builder.parse(is);
118     }
119 }