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