Test starting application with OBF: password
[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 java.io.IOException;
25 import org.eclipse.jetty.util.security.Password;
26 import org.hamcrest.Description;
27 import org.hamcrest.TypeSafeMatcher;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.springframework.boot.test.context.SpringBootTest;
33
34 @SpringBootTest(classes = BabelApplication.class)
35 public class TestApplication {
36
37     @Rule
38     public ExpectedException expectedEx = ExpectedException.none();
39
40     /**
41      * Initialize System Properties.
42      */
43     @Before
44     public void init() {
45         System.setProperty("APP_HOME", ".");
46         System.setProperty("CONFIG_HOME", "src/test/resources");
47         System.setProperty("server.ssl.key-store", "src/test/resources/auth/keystore.jks");
48     }
49
50     @Test
51     public void testApplicationStarts() {
52         System.setProperty("KEY_STORE_PASSWORD", "password");
53         BabelApplication.main(new String[] {});
54         BabelApplication.exit();
55     }
56
57     @Test
58     public void testApplicationStartsWithObfuscatedPassword() {
59         System.setProperty("KEY_STORE_PASSWORD", Password.obfuscate("password"));
60         BabelApplication.main(new String[] {});
61         BabelApplication.exit();
62     }
63
64     @Test
65     public void testApplicationWithNullArgs() {
66         System.setProperty("KEY_STORE_PASSWORD", "test");
67         expectedEx.expect(IllegalArgumentException.class);
68         expectedEx.expectMessage("Args must not be null");
69         BabelApplication.main(null);
70     }
71
72     @Test
73     public void testApplicationWithEmptyKeyStorePassword() {
74         System.setProperty("KEY_STORE_PASSWORD", "");
75         expectedEx.expect(IllegalArgumentException.class);
76         expectedEx.expectMessage("roperty KEY_STORE_PASSWORD not set");
77         BabelApplication.main(new String[] {});
78     }
79
80     @Test
81     public void testApplicationWithNullKeyStorePassword() {
82         System.clearProperty("KEY_STORE_PASSWORD");
83         expectedEx.expect(IllegalArgumentException.class);
84         expectedEx.expectMessage("roperty KEY_STORE_PASSWORD not set");
85         BabelApplication.main(new String[] {});
86     }
87
88     @Test
89     public void testApplicationWithIncorrectKeyStorePassword() {
90         System.setProperty("KEY_STORE_PASSWORD", "test");
91         final CauseMatcher expectedCause = new CauseMatcher(IOException.class, "password was incorrect");
92         expectedEx.expectCause(expectedCause);
93         BabelApplication.main(new String[] {});
94     }
95
96     private static class CauseMatcher extends TypeSafeMatcher<Throwable> {
97
98         private final Class<? extends Throwable> type;
99         private final String expectedMessage;
100
101         public CauseMatcher(Class<? extends Throwable> type, String expectedMessage) {
102             this.type = type;
103             this.expectedMessage = expectedMessage;
104         }
105
106         @Override
107         protected boolean matchesSafely(Throwable item) {
108             return item.getClass().isAssignableFrom(type) && item.getMessage().contains(expectedMessage);
109         }
110
111         @Override
112         public void describeTo(Description description) {
113             description.appendValue(type).appendText(" and message ").appendValue(expectedMessage);
114         }
115     }
116
117 }