X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2FTestApplication.java;h=c8065a255264d169726e62a090f31bfc3ea19e3f;hb=176c445bbeb5e47011ff2ca4816c00dd0f6ea779;hp=200b0e2163b32695a68c54d7941ac6f0abcd13c4;hpb=811169825805a7160a6dd28abb281f64a4a56c71;p=aai%2Fbabel.git diff --git a/src/test/java/org/onap/aai/babel/TestApplication.java b/src/test/java/org/onap/aai/babel/TestApplication.java index 200b0e2..c8065a2 100644 --- a/src/test/java/org/onap/aai/babel/TestApplication.java +++ b/src/test/java/org/onap/aai/babel/TestApplication.java @@ -21,76 +21,85 @@ package org.onap.aai.babel; +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.io.IOException; import org.eclipse.jetty.util.security.Password; import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.junit.rules.ExpectedException; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContextException; @SpringBootTest(classes = BabelApplication.class) public class TestApplication { - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - /** * Initialize System Properties. */ - @Before + @BeforeEach public void init() { System.setProperty("APP_HOME", "."); System.setProperty("CONFIG_HOME", "src/test/resources"); System.setProperty("server.ssl.key-store", "src/test/resources/auth/keystore.jks"); } - @Test(expected = Test.None.class /* no exception expected */) + @Test public void testApplicationStarts() { - System.setProperty("KEY_STORE_PASSWORD", "password"); - BabelApplication.main(new String[] {}); - BabelApplication.exit(); + assertDoesNotThrow(() -> { + System.setProperty("KEY_STORE_PASSWORD", "password"); + BabelApplication.main(new String[]{}); + BabelApplication.exit(); + }); } - @Test(expected = Test.None.class /* no exception expected */) + @Test public void testApplicationStartsWithObfuscatedPassword() { - System.setProperty("KEY_STORE_PASSWORD", Password.obfuscate("password")); - BabelApplication.main(new String[] {}); - BabelApplication.exit(); + assertDoesNotThrow(() -> { + System.setProperty("KEY_STORE_PASSWORD", Password.obfuscate("password")); + BabelApplication.main(new String[]{}); + BabelApplication.exit(); + }); } @Test public void testApplicationWithNullArgs() { - System.setProperty("KEY_STORE_PASSWORD", "test"); - expectedEx.expect(IllegalArgumentException.class); - expectedEx.expectMessage("Args must not be null"); - BabelApplication.main(null); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + System.setProperty("KEY_STORE_PASSWORD", "test"); + BabelApplication.main(null); + }); + assertTrue(exception.getMessage().contains("Args must not be null")); } @Test public void testApplicationWithEmptyKeyStorePassword() { - System.setProperty("KEY_STORE_PASSWORD", ""); - expectedEx.expect(IllegalArgumentException.class); - expectedEx.expectMessage("roperty KEY_STORE_PASSWORD not set"); - BabelApplication.main(new String[] {}); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + System.setProperty("KEY_STORE_PASSWORD", ""); + BabelApplication.main(new String[]{}); + }); + assertTrue(exception.getMessage().contains("roperty KEY_STORE_PASSWORD not set")); } @Test public void testApplicationWithNullKeyStorePassword() { - System.clearProperty("KEY_STORE_PASSWORD"); - expectedEx.expect(IllegalArgumentException.class); - expectedEx.expectMessage("roperty KEY_STORE_PASSWORD not set"); - BabelApplication.main(new String[] {}); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + System.clearProperty("KEY_STORE_PASSWORD"); + BabelApplication.main(new String[]{}); + }); + assertTrue(exception.getMessage().contains("roperty KEY_STORE_PASSWORD not set")); } @Test public void testApplicationWithIncorrectKeyStorePassword() { - System.setProperty("KEY_STORE_PASSWORD", "test"); - final CauseMatcher expectedCause = new CauseMatcher(IOException.class, "password was incorrect"); - expectedEx.expectCause(expectedCause); - BabelApplication.main(new String[] {}); + Throwable exception = assertThrows(ApplicationContextException.class, () -> { + System.setProperty("KEY_STORE_PASSWORD", "test"); + BabelApplication.main(new String[]{}); + }); } /** @@ -101,31 +110,10 @@ public class TestApplication { @Test public void testApplicationWithBlankObfuscatedKeyStorePassword() { // Note that "OBF:" is correctly deobfuscated and results in an empty string. - System.setProperty("KEY_STORE_PASSWORD", "OBF:"); - final CauseMatcher expectedCause = new CauseMatcher(IOException.class, "password was incorrect"); - expectedEx.expectCause(expectedCause); - BabelApplication.main(new String[] {}); - } - - private static class CauseMatcher extends TypeSafeMatcher { - - private final Class type; - private final String expectedMessage; - - public CauseMatcher(Class type, String expectedMessage) { - this.type = type; - this.expectedMessage = expectedMessage; - } - - @Override - protected boolean matchesSafely(Throwable item) { - return item.getClass().isAssignableFrom(type) && item.getMessage().contains(expectedMessage); - } - - @Override - public void describeTo(Description description) { - description.appendValue(type).appendText(" and message ").appendValue(expectedMessage); - } + Throwable exception = assertThrows(ApplicationContextException.class, () -> { + System.setProperty("KEY_STORE_PASSWORD", "OBF:"); + BabelApplication.main(new String[]{}); + }); } }