summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorshaoqiu <tim.huang@est.tech>2019-08-27 01:30:48 +0000
committershaoqiu <tim.huang@est.tech>2019-08-27 01:30:48 +0000
commit8379a2cec88ba880e0ee472c32f57217b231d348 (patch)
treefa055bc0f2f8b455669d80711ca4a099da6ea052 /tools
parent4883d8e231f765d00dccae504d65071dc8cd11a5 (diff)
add unit test for tools-common
Issue-ID: POLICY-1992 Change-Id: I6935b24b11ce62a18ae3d45795ff53cb4fa60f8b Signed-off-by: shaoqiu <tim.huang@est.tech>
Diffstat (limited to 'tools')
-rw-r--r--tools/tools-common/pom.xml5
-rw-r--r--tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/CliParserTest.java29
-rw-r--r--tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/ConsoleTest.java116
3 files changed, 149 insertions, 1 deletions
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 @@
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
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();
+ }
+}