View Javadoc
1   package io.apicurio.registry.maven;
2   
3   import io.apicurio.registry.client.common.RegistryClientOptions;
4   import org.apache.maven.plugin.MojoExecutionException;
5   import org.apache.maven.plugin.MojoFailureException;
6   import org.junit.jupiter.api.BeforeEach;
7   import org.junit.jupiter.api.Test;
8   
9   import java.util.concurrent.ExecutionException;
10  
11  import static org.junit.jupiter.api.Assertions.*;
12  
13  /**
14   * Tests for TLS configuration functionality in AbstractRegistryMojo.
15   */
16  public class TlsConfigurationTest {
17  
18      private TestableRegistryMojo mojo;
19  
20      @BeforeEach
21      void setup() {
22          mojo = new TestableRegistryMojo();
23          mojo.setRegistryUrl("http://localhost:8080/apis/registry/v3");
24      }
25  
26      // ========================================================================
27      // Tests for detectStoreType
28      // ========================================================================
29  
30      @Test
31      void testDetectStoreType_JksExtension() {
32          assertEquals("JKS", mojo.detectStoreType("/path/to/truststore.jks", null));
33          assertEquals("JKS", mojo.detectStoreType("/path/to/truststore.JKS", null));
34      }
35  
36      @Test
37      void testDetectStoreType_Pkcs12Extension() {
38          assertEquals("PKCS12", mojo.detectStoreType("/path/to/truststore.p12", null));
39          assertEquals("PKCS12", mojo.detectStoreType("/path/to/truststore.P12", null));
40          assertEquals("PKCS12", mojo.detectStoreType("/path/to/truststore.pfx", null));
41          assertEquals("PKCS12", mojo.detectStoreType("/path/to/truststore.PFX", null));
42      }
43  
44      @Test
45      void testDetectStoreType_PemExtension() {
46          assertEquals("PEM", mojo.detectStoreType("/path/to/ca-cert.pem", null));
47          assertEquals("PEM", mojo.detectStoreType("/path/to/ca-cert.PEM", null));
48          assertEquals("PEM", mojo.detectStoreType("/path/to/ca-cert.crt", null));
49          assertEquals("PEM", mojo.detectStoreType("/path/to/ca-cert.CRT", null));
50          assertEquals("PEM", mojo.detectStoreType("/path/to/ca-cert.cer", null));
51          assertEquals("PEM", mojo.detectStoreType("/path/to/ca-cert.CER", null));
52      }
53  
54      @Test
55      void testDetectStoreType_ExplicitTypeOverridesExtension() {
56          assertEquals("JKS", mojo.detectStoreType("/path/to/truststore.p12", "JKS"));
57          assertEquals("PEM", mojo.detectStoreType("/path/to/truststore.jks", "PEM"));
58          assertEquals("PKCS12", mojo.detectStoreType("/path/to/cert.pem", "PKCS12"));
59      }
60  
61      @Test
62      void testDetectStoreType_UnknownExtensionDefaultsToPkcs12() {
63          assertEquals("PKCS12", mojo.detectStoreType("/path/to/truststore.unknown", null));
64          assertEquals("PKCS12", mojo.detectStoreType("/path/to/truststore", null));
65          assertEquals("PKCS12", mojo.detectStoreType("/path/to/truststore.keystore", null));
66      }
67  
68      @Test
69      void testDetectStoreType_EmptyExplicitTypeUsesExtension() {
70          assertEquals("JKS", mojo.detectStoreType("/path/to/truststore.jks", ""));
71          assertEquals("PKCS12", mojo.detectStoreType("/path/to/truststore.p12", ""));
72      }
73  
74      // ========================================================================
75      // Tests for TLS setter methods
76      // ========================================================================
77  
78      @Test
79      void testSetTrustStorePath() {
80          mojo.setTrustStorePath("/path/to/truststore.p12");
81          assertEquals("/path/to/truststore.p12", mojo.trustStorePath);
82      }
83  
84      @Test
85      void testSetTrustStorePassword() {
86          mojo.setTrustStorePassword("secret");
87          assertEquals("secret", mojo.trustStorePassword);
88      }
89  
90      @Test
91      void testSetTrustStoreType() {
92          mojo.setTrustStoreType("JKS");
93          assertEquals("JKS", mojo.trustStoreType);
94      }
95  
96      @Test
97      void testSetKeyStorePath() {
98          mojo.setKeyStorePath("/path/to/keystore.p12");
99          assertEquals("/path/to/keystore.p12", mojo.keyStorePath);
100     }
101 
102     @Test
103     void testSetKeyStorePassword() {
104         mojo.setKeyStorePassword("secret");
105         assertEquals("secret", mojo.keyStorePassword);
106     }
107 
108     @Test
109     void testSetKeyStoreType() {
110         mojo.setKeyStoreType("PKCS12");
111         assertEquals("PKCS12", mojo.keyStoreType);
112     }
113 
114     @Test
115     void testSetKeyStorePemKeyPath() {
116         mojo.setKeyStorePemKeyPath("/path/to/client-key.pem");
117         assertEquals("/path/to/client-key.pem", mojo.keyStorePemKeyPath);
118     }
119 
120     @Test
121     void testSetTrustAll() {
122         assertFalse(mojo.trustAll);
123         mojo.setTrustAll(true);
124         assertTrue(mojo.trustAll);
125     }
126 
127     @Test
128     void testSetVerifyHostname() {
129         // Note: Java default for boolean is false; Maven @Parameter defaultValue only applies during Maven execution
130         mojo.setVerifyHostname(true);
131         assertTrue(mojo.verifyHostname);
132         mojo.setVerifyHostname(false);
133         assertFalse(mojo.verifyHostname);
134     }
135 
136     // ========================================================================
137     // Tests for configureTrustStore
138     // ========================================================================
139 
140     @Test
141     void testConfigureTrustStore_NullPath_DoesNothing() {
142         mojo.setTrustStorePath(null);
143         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
144 
145         // Should not throw and should not modify options
146         assertDoesNotThrow(() -> mojo.configureTrustStore(options));
147         assertEquals(RegistryClientOptions.TrustStoreType.NONE, options.getTrustStoreType());
148     }
149 
150     @Test
151     void testConfigureTrustStore_EmptyPath_DoesNothing() {
152         mojo.setTrustStorePath("");
153         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
154 
155         assertDoesNotThrow(() -> mojo.configureTrustStore(options));
156         assertEquals(RegistryClientOptions.TrustStoreType.NONE, options.getTrustStoreType());
157     }
158 
159     @Test
160     void testConfigureTrustStore_JksFormat() {
161         mojo.setTrustStorePath("/path/to/truststore.jks");
162         mojo.setTrustStorePassword("password");
163         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
164 
165         mojo.configureTrustStore(options);
166 
167         assertEquals(RegistryClientOptions.TrustStoreType.JKS, options.getTrustStoreType());
168         assertEquals("/path/to/truststore.jks", options.getTrustStorePath());
169         assertEquals("password", options.getTrustStorePassword());
170     }
171 
172     @Test
173     void testConfigureTrustStore_Pkcs12Format() {
174         mojo.setTrustStorePath("/path/to/truststore.p12");
175         mojo.setTrustStorePassword("password");
176         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
177 
178         mojo.configureTrustStore(options);
179 
180         assertEquals(RegistryClientOptions.TrustStoreType.PKCS12, options.getTrustStoreType());
181         assertEquals("/path/to/truststore.p12", options.getTrustStorePath());
182         assertEquals("password", options.getTrustStorePassword());
183     }
184 
185     @Test
186     void testConfigureTrustStore_PemFormat() {
187         mojo.setTrustStorePath("/path/to/ca-cert.pem");
188         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
189 
190         mojo.configureTrustStore(options);
191 
192         assertEquals(RegistryClientOptions.TrustStoreType.PEM, options.getTrustStoreType());
193         assertNotNull(options.getPemCertPaths());
194         assertEquals(1, options.getPemCertPaths().length);
195         assertEquals("/path/to/ca-cert.pem", options.getPemCertPaths()[0]);
196     }
197 
198     @Test
199     void testConfigureTrustStore_ExplicitType() {
200         mojo.setTrustStorePath("/path/to/truststore.unknown");
201         mojo.setTrustStoreType("JKS");
202         mojo.setTrustStorePassword("password");
203         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
204 
205         mojo.configureTrustStore(options);
206 
207         assertEquals(RegistryClientOptions.TrustStoreType.JKS, options.getTrustStoreType());
208     }
209 
210     // ========================================================================
211     // Tests for configureKeyStore
212     // ========================================================================
213 
214     @Test
215     void testConfigureKeyStore_NullPath_DoesNothing() {
216         mojo.setKeyStorePath(null);
217         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
218 
219         assertDoesNotThrow(() -> mojo.configureKeyStore(options));
220         assertEquals(RegistryClientOptions.KeyStoreType.NONE, options.getKeyStoreType());
221     }
222 
223     @Test
224     void testConfigureKeyStore_EmptyPath_DoesNothing() {
225         mojo.setKeyStorePath("");
226         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
227 
228         assertDoesNotThrow(() -> mojo.configureKeyStore(options));
229         assertEquals(RegistryClientOptions.KeyStoreType.NONE, options.getKeyStoreType());
230     }
231 
232     @Test
233     void testConfigureKeyStore_JksFormat() {
234         mojo.setKeyStorePath("/path/to/keystore.jks");
235         mojo.setKeyStorePassword("password");
236         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
237 
238         mojo.configureKeyStore(options);
239 
240         assertEquals(RegistryClientOptions.KeyStoreType.JKS, options.getKeyStoreType());
241         assertEquals("/path/to/keystore.jks", options.getKeyStorePath());
242         assertEquals("password", options.getKeyStorePassword());
243     }
244 
245     @Test
246     void testConfigureKeyStore_Pkcs12Format() {
247         mojo.setKeyStorePath("/path/to/keystore.p12");
248         mojo.setKeyStorePassword("password");
249         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
250 
251         mojo.configureKeyStore(options);
252 
253         assertEquals(RegistryClientOptions.KeyStoreType.PKCS12, options.getKeyStoreType());
254         assertEquals("/path/to/keystore.p12", options.getKeyStorePath());
255         assertEquals("password", options.getKeyStorePassword());
256     }
257 
258     @Test
259     void testConfigureKeyStore_PemFormat_WithKeyPath() {
260         mojo.setKeyStorePath("/path/to/client-cert.pem");
261         mojo.setKeyStorePemKeyPath("/path/to/client-key.pem");
262         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
263 
264         mojo.configureKeyStore(options);
265 
266         assertEquals(RegistryClientOptions.KeyStoreType.PEM, options.getKeyStoreType());
267         assertEquals("/path/to/client-cert.pem", options.getPemClientCertPath());
268         assertEquals("/path/to/client-key.pem", options.getPemClientKeyPath());
269     }
270 
271     @Test
272     void testConfigureKeyStore_PemFormat_WithoutKeyPath_ThrowsException() {
273         mojo.setKeyStorePath("/path/to/client-cert.pem");
274         mojo.setKeyStorePemKeyPath(null);
275         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
276 
277         IllegalArgumentException exception = assertThrows(
278                 IllegalArgumentException.class,
279                 () -> mojo.configureKeyStore(options)
280         );
281 
282         assertTrue(exception.getMessage().contains("keyStorePemKeyPath is required"));
283     }
284 
285     @Test
286     void testConfigureKeyStore_PemFormat_EmptyKeyPath_ThrowsException() {
287         mojo.setKeyStorePath("/path/to/client-cert.pem");
288         mojo.setKeyStorePemKeyPath("");
289         RegistryClientOptions options = RegistryClientOptions.create("http://localhost:8080");
290 
291         IllegalArgumentException exception = assertThrows(
292                 IllegalArgumentException.class,
293                 () -> mojo.configureKeyStore(options)
294         );
295 
296         assertTrue(exception.getMessage().contains("keyStorePemKeyPath is required"));
297     }
298 
299     // ========================================================================
300     // Tests for default values
301     // ========================================================================
302 
303     @Test
304     void testDefaultValues() {
305         TestableRegistryMojo freshMojo = new TestableRegistryMojo();
306 
307         // String fields default to null
308         assertNull(freshMojo.trustStorePath);
309         assertNull(freshMojo.trustStorePassword);
310         assertNull(freshMojo.trustStoreType);
311         assertNull(freshMojo.keyStorePath);
312         assertNull(freshMojo.keyStorePassword);
313         assertNull(freshMojo.keyStoreType);
314         assertNull(freshMojo.keyStorePemKeyPath);
315 
316         // Boolean fields default to Java defaults (false)
317         // Note: Maven @Parameter defaultValue only applies during Maven execution
318         assertFalse(freshMojo.trustAll);
319         assertFalse(freshMojo.verifyHostname);
320     }
321 
322     // ========================================================================
323     // Concrete implementation of AbstractRegistryMojo for testing
324     // ========================================================================
325 
326     private static class TestableRegistryMojo extends AbstractRegistryMojo {
327         @Override
328         protected void executeInternal()
329                 throws MojoExecutionException, MojoFailureException, ExecutionException, InterruptedException {
330             // No-op for testing
331         }
332     }
333 }