From 8379a2cec88ba880e0ee472c32f57217b231d348 Mon Sep 17 00:00:00 2001 From: shaoqiu Date: Tue, 27 Aug 2019 01:30:48 +0000 Subject: [PATCH] add unit test for tools-common Issue-ID: POLICY-1992 Change-Id: I6935b24b11ce62a18ae3d45795ff53cb4fa60f8b Signed-off-by: shaoqiu --- .../restclient/TestExecutionPropertyRest.java | 2 +- tools/tools-common/pom.xml | 5 + .../policy/apex/tools/common/CliParserTest.java | 29 +++++- .../onap/policy/apex/tools/common/ConsoleTest.java | 116 +++++++++++++++++++++ 4 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/ConsoleTest.java diff --git a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestExecutionPropertyRest.java b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestExecutionPropertyRest.java index bc5b602e3..6adb3b007 100644 --- a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestExecutionPropertyRest.java +++ b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestExecutionPropertyRest.java @@ -146,7 +146,7 @@ public class TestExecutionPropertyRest { final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json" }; final ApexMain apexMain = new ApexMain(args); - ThreadUtilities.sleep(1000); + ThreadUtilities.sleep(2000); apexMain.shutdown(); diff --git a/tools/tools-common/pom.xml b/tools/tools-common/pom.xml index 872842b68..177dffd13 100644 --- a/tools/tools-common/pom.xml +++ b/tools/tools-common/pom.xml @@ -48,6 +48,11 @@ commons-cli commons-cli + + org.assertj + assertj-core + test + diff --git a/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/CliParserTest.java b/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/CliParserTest.java index 9cd7b71e3..ab14c66c5 100644 --- a/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/CliParserTest.java +++ b/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/CliParserTest.java @@ -20,11 +20,15 @@ package org.onap.policy.apex.tools.common; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.commons.cli.Option; import org.junit.Test; -import org.onap.policy.apex.tools.common.CliParser; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; + /** * Tests for {@link CliParser}. * @@ -40,4 +44,27 @@ public class CliParserTest { final CliParser cli = new CliParser(); LOGGER.info(cli.getAppVersion()); } + + /** + * testAddAndGetOptionException. + */ + @Test + public void testAddAndGetOptionException() { + final CliParser cli = new CliParser(); + assertThatThrownBy(() -> { + cli.addOption(null); + }).isInstanceOf(IllegalStateException.class).hasMessageContaining("CLI parser: given option was null"); + } + + /** + * testParseAndGetCli. + */ + @Test + public void testParseAndGetCli() { + final CliParser cli = new CliParser(); + final Option option = new Option("g", "Good option."); + cli.addOption(option); + cli.parseCli(new String[] {"-g"}); + assertThat(cli.getCommandLine().hasOption("-g")).isTrue(); + } } diff --git a/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/ConsoleTest.java b/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/ConsoleTest.java new file mode 100644 index 000000000..aefa92b9c --- /dev/null +++ b/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/ConsoleTest.java @@ -0,0 +1,116 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.tools.common; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests for {@link Console}. + * + */ + +public class ConsoleTest { + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final PrintStream originalErr = System.err; + + @Before + public void setUpStreams() { + System.setErr(new PrintStream(errContent)); + } + + @After + public void restoreStreams() { + System.setErr(originalErr); + } + + @Test + public void testConsole() { + Console.CONSOLE.setAppName(null); + Console.CONSOLE.info(""); + Console.CONSOLE.error(""); + Console.CONSOLE.debug(""); + Console.CONSOLE.progress(""); + Console.CONSOLE.warn(""); + Console.CONSOLE.trace(""); + Console.CONSOLE.stacktrace(null); + assertThat(errContent.toString().trim()).isEmpty(); + + Console.CONSOLE.setAppName(""); + Console.CONSOLE.set(Console.TYPE_DEBUG, Console.TYPE_ERROR, Console.TYPE_INFO, Console.TYPE_PROGRESS, + Console.TYPE_WARNING, Console.TYPE_TRACE, Console.TYPE_STACKTRACE); + Console.CONSOLE.configure(Console.CONFIG_COLLECT_WARNINGS); + logMessage(); + assertThat(errContent.toString().trim()).contains("debug: debug message.") + .contains("error: error message.").contains("info message.").contains("progress: progress message.") + .contains("warning: warn message.").contains("trace: trace message.") + .contains("exception message: Exception message."); + reset(); + + Console.CONSOLE.setAppName("ConsoleTest"); + Console.CONSOLE.configure(Console.CONFIG_COLLECT_ERRORS); + logMessage(); + assertThat(errContent.toString().trim()) + .contains("ConsoleTest: debug: debug message.").contains("ConsoleTest: error: error message.") + .contains("ConsoleTest: info message.").contains("ConsoleTest: progress: progress message.") + .contains("ConsoleTest: warning: warn message.").contains("ConsoleTest: trace: trace message.") + .contains("ConsoleTest: exception message: Exception message."); + reset(); + + Console.CONSOLE.deActivate(Console.TYPE_DEBUG); + Console.CONSOLE.deActivate(Console.TYPE_ERROR); + Console.CONSOLE.deActivate(Console.TYPE_INFO); + Console.CONSOLE.deActivate(Console.TYPE_PROGRESS); + Console.CONSOLE.deActivate(Console.TYPE_WARNING); + Console.CONSOLE.deActivate(Console.TYPE_TRACE); + logMessage(); + assertThat(errContent.toString().trim()).isEmpty(); + reset(); + + Console.CONSOLE.set(Console.TYPE_STACKTRACE); + Console.CONSOLE.setAppName(null); + Console.CONSOLE.stacktrace(new Exception("Exception message.", new Throwable("test stacktrace!"))); + assertThat(errContent.toString()).contains("exception message: Exception message.") + .contains("exception cause: java.lang.Throwable: test stacktrace!"); + reset(); + } + + private void logMessage() { + Console.CONSOLE.debug("debug message."); + Console.CONSOLE.error("error message."); + Console.CONSOLE.info("info message."); + Console.CONSOLE.progress("progress message."); + Console.CONSOLE.warn("warn message."); + Console.CONSOLE.trace("trace message."); + Console.CONSOLE.stacktrace(new Exception("Exception message.")); + } + + private void reset() { + Console.CONSOLE.resetErrors(); + Console.CONSOLE.resetWarnings(); + errContent.reset(); + } +} -- 2.16.6