View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2020 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.util;
25  
26  import com.sun.jna.Platform;
27  
28  import javax.xml.parsers.ParserConfigurationException;
29  
30  import org.junit.jupiter.api.Assertions;
31  import org.junit.jupiter.api.Test;
32  import org.w3c.dom.Document;
33  import org.w3c.dom.Element;
34  import org.w3c.dom.Node;
35  import org.w3c.dom.NodeList;
36  
37  import waffle.windows.auth.IWindowsAccount;
38  import waffle.windows.auth.IWindowsAuthProvider;
39  import waffle.windows.auth.IWindowsComputer;
40  import waffle.windows.auth.impl.WindowsAccountImpl;
41  import waffle.windows.auth.impl.WindowsAuthProviderImpl;
42  
43  /**
44   * Build an info document and check that it has the right values.
45   */
46  class WaffleInfoTest {
47  
48      /**
49       * Test waffle info.
50       *
51       * @throws ParserConfigurationException
52       *             the parser configuration exception
53       */
54      @Test
55      void testWaffleInfo() throws ParserConfigurationException {
56          final WaffleInfo helper = new WaffleInfo();
57          final Document info = helper.getWaffleInfo();
58  
59          // Make sure JNA Version is properly noted
60          Assertions.assertEquals(Platform.class.getPackage().getImplementationVersion(),
61                  info.getDocumentElement().getAttribute("jna"));
62  
63          // waffle auth currentUser computer
64          final Node node = info.getDocumentElement().getFirstChild().getFirstChild().getNextSibling();
65  
66          Assertions.assertEquals("computer", node.getNodeName());
67  
68          final IWindowsAuthProvider auth = new WindowsAuthProviderImpl();
69          final IWindowsComputer computer = auth.getCurrentComputer();
70  
71          final NodeList nodes = node.getChildNodes();
72          Assertions.assertEquals(computer.getComputerName(), nodes.item(0).getTextContent());
73          Assertions.assertEquals(computer.getMemberOf(), nodes.item(1).getTextContent());
74          Assertions.assertEquals(computer.getJoinStatus(), nodes.item(2).getTextContent());
75  
76          // Add Lookup Info for Various accounts
77          String lookup = WindowsAccountImpl.getCurrentUsername();
78          final IWindowsAccount account = new WindowsAccountImpl(lookup);
79          Element elem = helper.getLookupInfo(info, lookup);
80          Assertions.assertEquals(lookup, elem.getAttribute("name"));
81          Assertions.assertEquals(account.getName(), elem.getFirstChild().getTextContent());
82  
83          // Report an error when unknown name
84          lookup = "__UNKNOWN_ACCOUNT_NAME___";
85          elem = helper.getLookupInfo(info, lookup);
86          Assertions.assertEquals(lookup, elem.getAttribute("name"));
87          Assertions.assertEquals("exception", elem.getFirstChild().getNodeName());
88      }
89  }