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.apache;
25  
26  import static org.assertj.core.api.Assertions.assertThat;
27  
28  import java.io.ByteArrayInputStream;
29  import java.io.ByteArrayOutputStream;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  
39  import waffle.mock.MockWindowsAccount;
40  import waffle.windows.auth.WindowsAccount;
41  
42  /**
43   * Windows Account Test.
44   */
45  class WindowsAccountTest {
46  
47      /** The mock windows account. */
48      private final MockWindowsAccount mockWindowsAccount = new MockWindowsAccount("localhost\\Administrator");
49  
50      /** The windows account. */
51      private WindowsAccount windowsAccount;
52  
53      /**
54       * Sets the up.
55       */
56      @BeforeEach
57      void setUp() {
58          this.windowsAccount = new WindowsAccount(this.mockWindowsAccount);
59      }
60  
61      /**
62       * Test equals.
63       */
64      @Test
65      void testEquals() {
66          Assertions.assertEquals(this.windowsAccount, new WindowsAccount(this.mockWindowsAccount));
67          final MockWindowsAccount mockWindowsAccount2 = new MockWindowsAccount("localhost\\Administrator2");
68          Assertions.assertNotEquals(this.windowsAccount, new WindowsAccount(mockWindowsAccount2));
69      }
70  
71      /**
72       * Test is serializable.
73       *
74       * @throws IOException
75       *             Signals that an I/O exception has occurred.
76       * @throws ClassNotFoundException
77       *             the class not found exception
78       */
79      @Test
80      void testIsSerializable() throws IOException, ClassNotFoundException {
81          // serialize
82          final ByteArrayOutputStream out = new ByteArrayOutputStream();
83          try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
84              oos.writeObject(this.windowsAccount);
85          }
86          assertThat(out.toByteArray()).isNotEmpty();
87          // deserialize
88          final InputStream in = new ByteArrayInputStream(out.toByteArray());
89          final ObjectInputStream ois = new ObjectInputStream(in);
90          final WindowsAccount copy = (WindowsAccount) ois.readObject();
91          // test
92          Assertions.assertEquals(this.windowsAccount, copy);
93          Assertions.assertEquals(this.windowsAccount.getDomain(), copy.getDomain());
94          Assertions.assertEquals(this.windowsAccount.getFqn(), copy.getFqn());
95          Assertions.assertEquals(this.windowsAccount.getName(), copy.getName());
96          Assertions.assertEquals(this.windowsAccount.getSidString(), copy.getSidString());
97      }
98  
99      /**
100      * Test properties.
101      */
102     @Test
103     void testProperties() {
104         Assertions.assertEquals("localhost", this.windowsAccount.getDomain());
105         Assertions.assertEquals("localhost\\Administrator", this.windowsAccount.getFqn());
106         Assertions.assertEquals("Administrator", this.windowsAccount.getName());
107         Assertions.assertTrue(this.windowsAccount.getSidString().startsWith("S-"));
108     }
109 
110 }