View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.certificates;
12  
13  import static org.assertj.core.api.Assertions.assertThat;
14  
15  import com.codebox.bean.JavaBeanTester;
16  
17  import jakarta.inject.Inject;
18  
19  import java.io.File;
20  import java.util.List;
21  
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.api.extension.ExtendWith;
24  import org.springframework.test.context.ContextConfiguration;
25  import org.springframework.test.context.junit.jupiter.SpringExtension;
26  import org.springframework.test.context.web.WebAppConfiguration;
27  import org.springframework.web.context.WebApplicationContext;
28  
29  import psiprobe.ProbeInitializer;
30  import psiprobe.model.certificates.Cert;
31  
32  /**
33   * The Class ListCertificatesControllerTest.
34   */
35  @ExtendWith(SpringExtension.class)
36  @ContextConfiguration(classes = ProbeInitializer.class)
37  @WebAppConfiguration
38  class ListCertificatesControllerTest {
39  
40    /** The ctx. */
41    @Inject
42    private WebApplicationContext ctx;
43  
44    /**
45     * Javabean tester.
46     */
47    @Test
48    void javabeanTester() {
49      JavaBeanTester.builder(ListCertificatesController.class)
50          .skip("applicationContext", "supportedMethods").test();
51    }
52  
53    /**
54     * Test get certificates.
55     *
56     * @throws Exception the exception
57     */
58    @Test
59    void certificatesTest() throws Exception {
60      ListCertificatesController controller = new ListCertificatesController();
61  
62      String storeType = "jks";
63      File storeFile = ctx.getResource("classpath:certs/localhost-truststore.jks").getFile();
64      String storePassword = "123456";
65  
66      List<Cert> certs = controller.getCertificates(storeType, storeFile.toString(), storePassword);
67  
68      assertThat(certs).doesNotContainNull().hasSize(2);
69      assertThat(certs.get(0).getAlias()).isEqualTo("*.google.com");
70      assertThat(certs.get(1).getAlias()).isEqualTo("google_g2_2017");
71    }
72  
73    /**
74     * Test get certificates relative.
75     *
76     * @throws Exception the exception
77     */
78    @Test
79    void certificatesRelativeTest() throws Exception {
80      ListCertificatesController controller = new ListCertificatesController();
81  
82      String storeType = "jks";
83      File certFolder = ctx.getResource("classpath:certs").getFile();
84      System.setProperty("catalina.base", certFolder.getPath());
85  
86      String storePassword = "123456";
87  
88      List<Cert> certs =
89          controller.getCertificates(storeType, "localhost-truststore.jks", storePassword);
90  
91      assertThat(certs).doesNotContainNull().hasSize(2);
92      assertThat(certs.get(0).getAlias()).isEqualTo("*.google.com");
93      assertThat(certs.get(1).getAlias()).isEqualTo("google_g2_2017");
94    }
95  
96    /**
97     * Test get certificates relative uri.
98     *
99     * @throws Exception the exception
100    */
101   @Test
102   void certificatesRelativeUriTest() throws Exception {
103     ListCertificatesController controller = new ListCertificatesController();
104 
105     String storeType = "jks";
106     File storeFile = ctx.getResource("classpath:certs/localhost-truststore.jks").getFile();
107     File certFolder = ctx.getResource("classpath:certs").getFile();
108     System.setProperty("catalina.base", certFolder.getPath());
109 
110     String storePassword = "123456";
111 
112     List<Cert> certs =
113         controller.getCertificates(storeType, storeFile.toURI().toString(), storePassword);
114 
115     assertThat(certs).doesNotContainNull().hasSize(2);
116     assertThat(certs.get(0).getAlias()).isEqualTo("*.google.com");
117     assertThat(certs.get(1).getAlias()).isEqualTo("google_g2_2017");
118   }
119 
120   /**
121    * Test get certificates absolute uri.
122    *
123    * @throws Exception the exception
124    */
125   @Test
126   void certificatesAbsoluteUriTest() throws Exception {
127     ListCertificatesController controller = new ListCertificatesController();
128 
129     String storeType = "jks";
130     File certFolder = ctx.getResource("classpath:certs").getFile();
131     System.setProperty("catalina.base", certFolder.getPath());
132 
133     String storePassword = "123456";
134 
135     List<Cert> certs =
136         controller.getCertificates(storeType, "./localhost-truststore.jks", storePassword);
137 
138     assertThat(certs).doesNotContainNull().hasSize(2);
139     assertThat(certs.get(0).getAlias()).isEqualTo("*.google.com");
140     assertThat(certs.get(1).getAlias()).isEqualTo("google_g2_2017");
141   }
142 
143 }