2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel;
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;
34 @SpringBootTest(classes = BabelApplication.class)
35 public class TestApplication {
38 public ExpectedException expectedEx = ExpectedException.none();
41 * Initialize System Properties.
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");
50 @Test(expected = Test.None.class /* no exception expected */)
51 public void testApplicationStarts() {
52 System.setProperty("KEY_STORE_PASSWORD", "password");
53 BabelApplication.main(new String[] {});
54 BabelApplication.exit();
57 @Test(expected = Test.None.class /* no exception expected */)
58 public void testApplicationStartsWithObfuscatedPassword() {
59 System.setProperty("KEY_STORE_PASSWORD", Password.obfuscate("password"));
60 BabelApplication.main(new String[] {});
61 BabelApplication.exit();
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);
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[] {});
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[] {});
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[] {});
97 * This test asserts that if the KEY_STORE_PASSWORD System Property is set (and is not empty) then the value is
98 * passed to Jetty, debobfuscated, and used to open the key store, even if the resulting password value is actually
102 public void testApplicationWithBlankObfuscatedKeyStorePassword() {
103 // Note that "OBF:" is correctly deobfuscated and results in an empty string.
104 System.setProperty("KEY_STORE_PASSWORD", "OBF:");
105 final CauseMatcher expectedCause = new CauseMatcher(IOException.class, "password was incorrect");
106 expectedEx.expectCause(expectedCause);
107 BabelApplication.main(new String[] {});
110 private static class CauseMatcher extends TypeSafeMatcher<Throwable> {
112 private final Class<? extends Throwable> type;
113 private final String expectedMessage;
115 public CauseMatcher(Class<? extends Throwable> type, String expectedMessage) {
117 this.expectedMessage = expectedMessage;
121 protected boolean matchesSafely(Throwable item) {
122 return item.getClass().isAssignableFrom(type) && item.getMessage().contains(expectedMessage);
126 public void describeTo(Description description) {
127 description.appendValue(type).appendText(" and message ").appendValue(expectedMessage);