View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 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.xml.parsers.DocumentBuilder;
32  import javax.xml.parsers.DocumentBuilderFactory;
33  import javax.xml.parsers.ParserConfigurationException;
34  
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.Test;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  import org.w3c.dom.Document;
40  import org.w3c.dom.Node;
41  import org.xml.sax.InputSource;
42  import org.xml.sax.SAXException;
43  
44  import waffle.mock.http.SimpleHttpRequest;
45  import waffle.mock.http.SimpleHttpResponse;
46  
47  /**
48   * Test the WaffleInfoServlet.
49   */
50  class WaffleInfoServletTest {
51  
52      /** The Constant LOGGER. */
53      private static final Logger LOGGER = LoggerFactory.getLogger(WaffleInfoServletTest.class);
54  
55      /**
56       * Test get info.
57       *
58       * @throws ParserConfigurationException
59       *             the parser configuration exception
60       * @throws SAXException
61       *             the SAX exception
62       * @throws IOException
63       *             Signals that an I/O exception has occurred.
64       */
65      @Test
66      void testGetInfo() throws ParserConfigurationException, SAXException, IOException {
67          final SimpleHttpRequest request = new SimpleHttpRequest();
68          request.addHeader("hello", "waffle");
69  
70          final SimpleHttpResponse response = new SimpleHttpResponse();
71  
72          final WaffleInfoServlet servlet = new WaffleInfoServlet();
73          servlet.doGet(request, response);
74  
75          final String xml = response.getOutputText();
76          final Document doc = WaffleInfoServletTest.loadXMLFromString(xml);
77  
78          WaffleInfoServletTest.LOGGER.info("GOT: {}", xml);
79  
80          // Make sure JNA Version is properly noted
81          Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
82                  doc.getDocumentElement().getAttribute("jna"));
83  
84          final Node node = doc.getDocumentElement().getFirstChild().getNextSibling() // request
85                  .getFirstChild().getNextSibling() // AuthType
86                  .getNextSibling().getNextSibling();
87  
88          // Make sure the headers were added correctly
89          Assertions.assertEquals("headers", node.getNodeName());
90          final Node child = node.getFirstChild().getNextSibling();
91          Assertions.assertEquals("hello", child.getNodeName());
92      }
93  
94      /**
95       * Load xml from string.
96       *
97       * @param xml
98       *            the xml
99       *
100      * @return the document
101      *
102      * @throws ParserConfigurationException
103      *             the parser configuration exception
104      * @throws SAXException
105      *             the SAX exception
106      * @throws IOException
107      *             Signals that an I/O exception has occurred.
108      */
109     private static Document loadXMLFromString(final String xml)
110             throws ParserConfigurationException, SAXException, IOException {
111         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
112         final DocumentBuilder builder = factory.newDocumentBuilder();
113         final InputSource is = new InputSource(new StringReader(xml));
114         return builder.parse(is);
115     }
116 }