diff options
author | ning.xi <ning.xi@est.tech> | 2019-08-13 05:20:20 +0000 |
---|---|---|
committer | ning.xi <ning.xi@est.tech> | 2019-08-13 05:20:20 +0000 |
commit | c678c3a22f7761928f4be8e4f82f2f3c7ea417db (patch) | |
tree | 15679e12025af9a056208ff9879250b7ce458d25 /client/client-full/src/test | |
parent | 6203d19345c38dbfb6136ea6754bb11ca09fa90b (diff) |
add JUnit test in client deployment and client full
Issue-ID: POLICY-1962
Change-Id: Ic10b0edc9364624161f71935f51f4411fe67c882
Signed-off-by: ning.xi <ning.xi@est.tech>
Diffstat (limited to 'client/client-full/src/test')
4 files changed, 493 insertions, 0 deletions
diff --git a/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ParameterCheckTest.java b/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ParameterCheckTest.java new file mode 100644 index 000000000..0be0ac91c --- /dev/null +++ b/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ParameterCheckTest.java @@ -0,0 +1,188 @@ +/*- + * ============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.client.full.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import java.util.LinkedHashMap; +import java.util.Map; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * Test the parameter check class. + * + */ +public class ParameterCheckTest { + + @Test + public void testStartStop() { + assertEquals("START", ParameterCheck.StartStop.START.name()); + assertEquals("STOP", ParameterCheck.StartStop.STOP.name()); + } + + @Test + public void testHostName() { + assertNull(ParameterCheck.getHostName(null)); + + Map<String, String[]> parameterMap = new LinkedHashMap<>(); + assertNull(ParameterCheck.getHostName(parameterMap)); + parameterMap.put("hostname", null); + assertNull(ParameterCheck.getHostName(parameterMap)); + + String[] hostnameBlankValue0 = {"", ""}; + parameterMap.put("hostname", hostnameBlankValue0); + assertNull(ParameterCheck.getHostName(parameterMap)); + + String[] hostnameBlankValue1 = {" ", " "}; + parameterMap.put("hostname", hostnameBlankValue1); + assertNull(ParameterCheck.getHostName(parameterMap)); + + String[] hostnameValue = {"hostname0", "hostname1"}; + parameterMap.put("hostname", hostnameValue); + assertEquals("hostname0", ParameterCheck.getHostName(parameterMap)); + } + + @Test + public void testPort() { + assertEquals(-1, ParameterCheck.getPort(null)); + + Map<String, String[]> parameterMap = new LinkedHashMap<>(); + assertEquals(-1, ParameterCheck.getPort(parameterMap)); + + String[] portBlankValue0 = {"", ""}; + parameterMap.put("port", portBlankValue0); + assertEquals(-1, ParameterCheck.getPort(parameterMap)); + + String[] portBlankValue1 = {" ", " "}; + parameterMap.put("port", portBlankValue1); + assertEquals(-1, ParameterCheck.getPort(parameterMap)); + + String[] portValueBad = {"port", "value"}; + parameterMap.put("port", portValueBad); + assertEquals(-1, ParameterCheck.getPort(parameterMap)); + + String[] portValueRange0 = {"-1", "-1"}; + parameterMap.put("port", portValueRange0); + assertEquals(-1, ParameterCheck.getPort(parameterMap)); + + String[] portValueRange1 = {"65536", "65536"}; + parameterMap.put("port", portValueRange1); + assertEquals(-1, ParameterCheck.getPort(parameterMap)); + + String[] portValue = {"12344", "23221"}; + parameterMap.put("port", portValue); + assertEquals(12344, ParameterCheck.getPort(parameterMap)); + } + + @Test + public void testEngineKey() { + assertEquals(null, ParameterCheck.getEngineKey(null)); + + Map<String, String[]> parameterMap = new LinkedHashMap<>(); + parameterMap.put("Zooby", null); + assertEquals(null, ParameterCheck.getEngineKey(parameterMap)); + + parameterMap.put("AxArtifactKey", null); + assertEquals(null, ParameterCheck.getEngineKey(parameterMap)); + parameterMap.remove("AxArtifactKey"); + + parameterMap.put("AxArtifactKey#zooby", null); + assertEquals(null, ParameterCheck.getEngineKey(parameterMap)); + parameterMap.remove("AxArtifactKey#zooby"); + + parameterMap.put("AxArtifactKey#zooby#looby", null); + assertEquals(null, ParameterCheck.getEngineKey(parameterMap)); + parameterMap.remove("AxArtifactKey#zooby#looby"); + + parameterMap.put("AxArtifactKey#Name:0.0.1", null); + assertEquals(new AxArtifactKey("Name", "0.0.1"), ParameterCheck.getEngineKey(parameterMap)); + } + + @Test + public void testStartStopValue() { + assertEquals(null, ParameterCheck.getStartStop(null, null)); + + Map<String, String[]> parameterMap = new LinkedHashMap<>(); + assertEquals(null, ParameterCheck.getStartStop(parameterMap, null)); + + parameterMap.put("Zooby", null); + assertEquals(null, ParameterCheck.getStartStop(parameterMap, null)); + + AxArtifactKey engineKey = new AxArtifactKey("Engine", "0.0.1"); + + parameterMap.put("Zooby", null); + assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey)); + + String key = "AxArtifactKey#" + engineKey.getId(); + + parameterMap.put(key, null); + assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey)); + + String[] startStopBlankValue0 = {"", ""}; + parameterMap.put(key, startStopBlankValue0); + assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey)); + + String[] startStopBlankValue1 = {" ", " "}; + parameterMap.put(key, startStopBlankValue1); + assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey)); + + String[] startStopValueBad = {key, "value"}; + parameterMap.put(key, startStopValueBad); + assertEquals(null, ParameterCheck.getStartStop(parameterMap, engineKey)); + + String[] startValue = {"START", "STOP"}; + parameterMap.put(key, startValue); + assertEquals(ParameterCheck.StartStop.START, ParameterCheck.getStartStop(parameterMap, engineKey)); + + String[] stopValue = {"STOP", "START"}; + parameterMap.put(key, stopValue); + assertEquals(ParameterCheck.StartStop.STOP, ParameterCheck.getStartStop(parameterMap, engineKey)); + } + + @Test + public void testLong() { + assertEquals(-1, ParameterCheck.getLong(null, null)); + + Map<String, String[]> parameterMap = new LinkedHashMap<>(); + assertEquals(-1, ParameterCheck.getLong(parameterMap, null)); + + parameterMap.put("long0", null); + assertEquals(-1, ParameterCheck.getLong(parameterMap, "longx")); + assertEquals(-1, ParameterCheck.getLong(parameterMap, "long0")); + + String[] longBlankValue0 = {"", ""}; + parameterMap.put("long1", longBlankValue0); + assertEquals(-1, ParameterCheck.getLong(parameterMap, "long1")); + + String[] longBlankValue1 = {" ", " "}; + parameterMap.put("long2", longBlankValue1); + assertEquals(-1, ParameterCheck.getLong(parameterMap, "long2")); + + String[] longValueBad = {"long", "value"}; + parameterMap.put("long3", longValueBad); + assertEquals(-1, ParameterCheck.getLong(parameterMap, "long3")); + + String[] longValue = {"12345", "6789"}; + parameterMap.put("long4", longValue); + assertEquals(12345, ParameterCheck.getLong(parameterMap, "long4")); + } +} diff --git a/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesExceptionTest.java b/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesExceptionTest.java new file mode 100644 index 000000000..7ae8e7143 --- /dev/null +++ b/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesExceptionTest.java @@ -0,0 +1,37 @@ +/*- + * ============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.client.full.rest; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +/** + * Test the REST Services exception. + * + */ +public class ServicesExceptionTest { + + @Test + public void test() { + ApexServicesRestParameterException ame = new ApexServicesRestParameterException("a message"); + assertEquals("a message", ame.getMessage()); + } +} diff --git a/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesRestMainTest.java b/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesRestMainTest.java new file mode 100644 index 000000000..f6b3373ce --- /dev/null +++ b/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesRestMainTest.java @@ -0,0 +1,221 @@ +/*- + * ============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.client.full.rest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.catchThrowable; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Test; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; + +/** + * Test the periodic event manager utility. + */ +public class ServicesRestMainTest { + private static InputStream systemInStream = System.in; + + @Test + public void testServicesMainClientOk() { + final String[] eventArgs = {"-t", "1", "-p", "1256"}; + assertThatCode(() -> ApexServicesRestMain.main(eventArgs)).doesNotThrowAnyException(); + } + + @Test + public void testServicesClientNoOptions() { + final String[] eventArgs = new String[] + {}; + + assertThat(testApexServicesRestMainConstructor(eventArgs)).isEqualTo("*** StdOut ***\n\n*** StdErr ***\n"); + + + } + + @Test + public void testServicesClientBadOptions() { + final String[] eventArgs = + { "-zabbu" }; + Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out)); + + assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining( + "Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) " + + "parameter error, invalid command line arguments specified " + + ": Unrecognized option: -zabbu"); + } + + @Test + public void testServicesClientHelp() { + final String[] eventArgs = + { "-h" }; + Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out)); + + assertThat(thrown).isInstanceOf(Exception.class) + .hasMessageContaining("usage: org.onap.policy.apex.client.full.rest.ApexServicesRestMain [options...]"); + } + + @Test + public void testServicesClientPortBad() { + final String[] eventArgs = + { "-p", "hello" }; + + Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out)); + + assertThat(thrown).isInstanceOf(Exception.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) " + + "parameter error, error parsing argument \"port\" :For input string: \"hello\""); + + } + + @Test + public void testServicesClientPortNegative() { + final String[] eventArgs = + { "-p", "-1" }; + + Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out)); + + assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining( + "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: " + + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, " + + "port must be greater than 1023 and less than 65536"); + + } + + @Test + public void testServicesClientTtlTooSmall() { + final String[] eventArgs = + { "-t", "-2" }; + + Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out)); + + assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining( + "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: " + + "URI=http://localhost:18989/apexservices/, TTL=-2sec], State=STOPPED) parameters invalid, " + + "time to live must be greater than -1 (set to -1 to wait forever)"); + } + + @Test + public void testServicesClientTooManyPars() { + final String[] eventArgs = + { "-t", "10", "-p", "12344", "aaa", "bbb" }; + Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out)); + + assertThat(thrown).isInstanceOf(Exception.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) " + + "parameter error, too many command line arguments specified : [aaa, bbb]"); + } + + + @Test + public void testServicesClientTtlNotNumber() { + final String[] eventArgs = + { "-t", "timetolive" }; + Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out)); + + assertThat(thrown).isInstanceOf(Exception.class) + .hasMessageContaining("Apex Editor REST endpoint (ApexServicesRestMain: Config=[null], State=STOPPED) " + + "parameter error, error parsing argument \"time-to-live\" :" + + "For input string: \"timetolive\""); + } + + @Test + public void testServicesClientTtlSetValue() { + final String[] eventArgs = {"-t", "3", "-p", "1257"}; + assertThatCode(() -> { + ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out); + monRestMain.init(); + monRestMain.shutdown(); + }).doesNotThrowAnyException(); + + } + + @Test + public void testServicesClientPortTooBig() { + final String[] eventArgs = + { "-p", "65536" }; + Throwable thrown = catchThrowable(() -> new ApexServicesRestMain(eventArgs, System.out)); + + assertThat(thrown).isInstanceOf(Exception.class).hasMessageContaining( + "Apex Editor REST endpoint (ApexServicesRestMain: Config=[ApexServicesRestParameters: " + + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, " + + "port must be greater than 1023 and less than 65536"); + } + + @Test + public void testServicesOneSecStart() { + final String[] eventArgs = {"-t", "1", "-p", "1258"}; + + assertThatCode(() -> { + ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out); + monRestMain.init(); + monRestMain.shutdown(); + }).doesNotThrowAnyException(); + } + + @Test + public void testServicesForeverStart() { + final String[] eventArgs = {"-t", "-1", "-p", "1259"}; + + ApexServicesRestMain monRestMain = new ApexServicesRestMain(eventArgs, System.out); + + Thread monThread = new Thread() { + @Override + public void run() { + monRestMain.init(); + } + }; + + assertThatCode(() -> { + monThread.start(); + ThreadUtilities.sleep(2000); + monRestMain.shutdown(); + }).doesNotThrowAnyException(); + } + + @After + public void cleanUpStreamSetting() { + System.setIn(systemInStream); + } + + /** + * Run the application. + * + * @param eventArgs the command arguments + * @return a string containing the command output + */ + private String testApexServicesRestMainConstructor(final String[] eventArgs) { + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + final ByteArrayOutputStream baosErr = new ByteArrayOutputStream(); + + new ApexServicesRestMain(eventArgs, new PrintStream(baosOut, true)); + + InputStream testInput = new ByteArrayInputStream("Test Data for Input to WS".getBytes()); + System.setIn(testInput); + + String outString = baosOut.toString(); + String errString = baosErr.toString(); + + return "*** StdOut ***\n" + outString + "\n*** StdErr ***\n" + errString; + } +} diff --git a/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesRestParameterTest.java b/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesRestParameterTest.java new file mode 100644 index 000000000..777a9c170 --- /dev/null +++ b/client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesRestParameterTest.java @@ -0,0 +1,47 @@ +/*- + * ============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.client.full.rest; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +/** + * Extra Services rest tests. + * + */ +public class ServicesRestParameterTest { + + @Test + public void test() { + ApexServicesRestParameters parameters = new ApexServicesRestParameters(); + parameters.setRestPort(12345); + assertEquals(12345, parameters.getRestPort()); + } + + @Test + public void testMainDefaultParameter() { + assertThatCode(() -> { + ApexServicesRest monRest = new ApexServicesRest(); + monRest.shutdown(); + }).doesNotThrowAnyException(); + } +} |