View Javadoc
1   /*
2    * Copyright 2008-2016 Brian Thomas Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.maven.plugins.ldap;
18  
19  import com.btmatthews.maven.plugins.ldap.AbstractLDAPServer;
20  import com.btmatthews.maven.plugins.ldap.LDAPServer;
21  import com.btmatthews.utils.monitor.Logger;
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.mockito.Mock;
25  
26  import java.io.File;
27  import java.net.MalformedURLException;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertSame;
31  import static org.mockito.Mockito.*;
32  import static org.mockito.MockitoAnnotations.initMocks;
33  
34  /**
35   * Unit test the abstract base class for the directory server implementations.
36   *
37   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
38   * @since 1.2.0
39   */
40  public class TestAbstractLDAPServer {
41  
42      /**
43       * Mock the logger object so that we can verify behaviour.
44       */
45      @Mock
46      private Logger logger;
47  
48      /**
49       * The test class.
50       */
51      private LDAPServer server;
52  
53      /**
54       * Prepare for unit test execution.
55       */
56      @Before
57      public void setUp() {
58          initMocks(this);
59          server = new AbstractLDAPServer() {
60          };
61      }
62  
63      /**
64       * Verify the configuration of the root DN.
65       */
66      @Test
67      public void configureRoot() {
68          server.configure("root", "dc=btmatthews,dc=com", logger);
69          verify(logger).logInfo("Configured root DN for directory server: dc=btmatthews,dc=com");
70          assertEquals("dc=btmatthews,dc=com", server.getRoot());
71      }
72  
73      /**
74       * Verify the configuration of the authentication identifier.
75       */
76      @Test
77      public void configureAuthDN() {
78          server.configure("authDn", "uid=admin,ou=system", logger);
79          verify(logger).logInfo("Configured admin identity for directory server: uid=admin,ou=system");
80          assertEquals("uid=admin,ou=system", server.getAuthDn());
81      }
82  
83      /**
84       * Verify the configuration of the authentication credentials.
85       */
86      @Test
87      public void configurePassword() {
88          server.configure("passwd", "secret", logger);
89          verify(logger).logInfo("Configured admin credentials for directory server: secret");
90          assertEquals("secret", server.getPasswd());
91      }
92  
93      /**
94       * Verify the configuration of the working directory.
95       */
96      @Test
97      public void configureWorkingDirectory() {
98          final File workDir = mock(File.class);
99          when(workDir.toString()).thenReturn("tmp");
100         server.configure("workingDirectory", workDir, logger);
101         verify(logger).logInfo("Configured working directory for directory server: tmp");
102         assertSame(workDir, server.getWorkingDirectory());
103     }
104 
105     /**
106      * Verify the configuration of the LDIF file containing seed data.
107      *
108      * @throws MalformedURLException If the URL is invalid.
109      */
110     @Test
111     public void configureLDIFFile() throws MalformedURLException {
112         final File ldifFile = new File("initial.ldif");
113         server.configure("ldifFile", ldifFile, logger);
114         verify(logger).logInfo("Configured LDIF seed data source for directory server: initial.ldif");
115         assertSame(ldifFile, server.getLdifFile());
116     }
117 
118     /**
119      * Verify the configuration of the LDAP port.
120      */
121     @Test
122     public void configureLDAPPort() {
123         server.configure("ldapPort", 10389, logger);
124         verify(logger).logInfo("Configured TCP port for directory server: 10389");
125         assertEquals(10389, server.getServerPort());
126     }
127 }