Create 1.13.0 maven release
[aai/babel.git] / src / test / java / org / onap / aai / babel / TestApplication.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.babel;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import java.io.IOException;
30 import org.eclipse.jetty.util.security.Password;
31 import org.hamcrest.Description;
32 import org.hamcrest.TypeSafeMatcher;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.rules.ExpectedException;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.context.ApplicationContextException;
38
39 @SpringBootTest(classes = BabelApplication.class)
40 public class TestApplication {
41
42     /**
43      * Initialize System Properties.
44      */
45     @BeforeEach
46     public void init() {
47         System.setProperty("APP_HOME", ".");
48         System.setProperty("CONFIG_HOME", "src/test/resources");
49         System.setProperty("server.ssl.key-store", "src/test/resources/auth/keystore.jks");
50     }
51
52     @Test
53     public void testApplicationStarts() {
54         assertDoesNotThrow(() -> {
55             System.setProperty("KEY_STORE_PASSWORD", "password");
56             BabelApplication.main(new String[]{});
57             BabelApplication.exit();
58         });
59     }
60
61     @Test
62     public void testApplicationStartsWithObfuscatedPassword() {
63         assertDoesNotThrow(() -> {
64             System.setProperty("KEY_STORE_PASSWORD", Password.obfuscate("password"));
65             BabelApplication.main(new String[]{});
66             BabelApplication.exit();
67         });
68     }
69
70     @Test
71     public void testApplicationWithNullArgs() {
72         Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
73             System.setProperty("KEY_STORE_PASSWORD", "test");
74             BabelApplication.main(null);
75         });
76         assertTrue(exception.getMessage().contains("Args must not be null"));
77     }
78
79     @Test
80     public void testApplicationWithEmptyKeyStorePassword() {
81         Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
82             System.setProperty("KEY_STORE_PASSWORD", "");
83             BabelApplication.main(new String[]{});
84         });
85         assertTrue(exception.getMessage().contains("roperty KEY_STORE_PASSWORD not set"));
86     }
87
88     @Test
89     public void testApplicationWithNullKeyStorePassword() {
90         Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
91             System.clearProperty("KEY_STORE_PASSWORD");
92             BabelApplication.main(new String[]{});
93         });
94         assertTrue(exception.getMessage().contains("roperty KEY_STORE_PASSWORD not set"));
95     }
96
97     @Test
98     public void testApplicationWithIncorrectKeyStorePassword() {
99         Throwable exception = assertThrows(ApplicationContextException.class, () -> {
100             System.setProperty("KEY_STORE_PASSWORD", "test");
101             BabelApplication.main(new String[]{});
102         });
103     }
104
105     /**
106      * This test asserts that if the KEY_STORE_PASSWORD System Property is set (and is not empty) then the value is
107      * passed to Jetty, debobfuscated, and used to open the key store, even if the resulting password value is actually
108      * an empty string.
109      */
110     @Test
111     public void testApplicationWithBlankObfuscatedKeyStorePassword() {
112         // Note that "OBF:" is correctly deobfuscated and results in an empty string.
113         Throwable exception = assertThrows(ApplicationContextException.class, () -> {
114             System.setProperty("KEY_STORE_PASSWORD", "OBF:");
115             BabelApplication.main(new String[]{});
116         });
117     }
118
119 }