diff options
Diffstat (limited to 'client/client-full/src')
5 files changed, 532 insertions, 2 deletions
diff --git a/client/client-full/src/main/java/org/onap/policy/apex/client/full/rest/ParameterCheck.java b/client/client-full/src/main/java/org/onap/policy/apex/client/full/rest/ParameterCheck.java index 9832f4317..1fb01c86e 100644 --- a/client/client-full/src/main/java/org/onap/policy/apex/client/full/rest/ParameterCheck.java +++ b/client/client-full/src/main/java/org/onap/policy/apex/client/full/rest/ParameterCheck.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * 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. @@ -21,7 +22,6 @@ package org.onap.policy.apex.client.full.rest; import java.util.Map; - import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -69,6 +69,10 @@ public final class ParameterCheck { * @return the host name */ public static String getHostName(final Map<String, String[]> parameterMap) { + if (parameterMap == null) { + return null; + } + if (!parameterMap.containsKey(HOSTNAME_PAR)) { LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND); return null; @@ -76,6 +80,10 @@ public final class ParameterCheck { final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR); + if (hostNameValue == null) { + return null; + } + if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) { LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND); return null; @@ -91,6 +99,10 @@ public final class ParameterCheck { * @return the port */ public static int getPort(final Map<String, String[]> parameterMap) { + if (parameterMap == null) { + return -1; + } + if (!parameterMap.containsKey(PORT_PAR)) { LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND); return -1; @@ -127,6 +139,10 @@ public final class ParameterCheck { * @return the engine key */ public static AxArtifactKey getEngineKey(final Map<String, String[]> parameterMap) { + if (parameterMap == null) { + return null; + } + String artifactKeyParameter = null; for (final String parameter : parameterMap.keySet()) { // Check for an AxArtifactKey parameter @@ -147,7 +163,12 @@ public final class ParameterCheck { return null; } - return new AxArtifactKey(axArtifactKeyArray[1]); + try { + return new AxArtifactKey(axArtifactKeyArray[1]); + } catch (Exception apEx) { + LOGGER.trace("invalid artifact key ID {}", axArtifactKeyArray[1], apEx); + return null; + } } /** @@ -159,6 +180,10 @@ public final class ParameterCheck { */ public static ParameterCheck.StartStop getStartStop(final Map<String, String[]> parameterMap, final AxArtifactKey engineKey) { + if (parameterMap == null || engineKey == null) { + return null; + } + final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId(); if (!parameterMap.containsKey(startStopPar)) { LOGGER.warn("parameter \"{}\" not found", startStopPar); @@ -167,6 +192,10 @@ public final class ParameterCheck { final String[] startStopValue = parameterMap.get(startStopPar); + if (startStopValue == null) { + return null; + } + if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) { LOGGER.warn("value of parameter \"{}\" not found", startStopPar); return null; @@ -193,6 +222,10 @@ public final class ParameterCheck { * @return The long value */ public static long getLong(final Map<String, String[]> parameterMap, final String longName) { + if (parameterMap == null || longName == null) { + return -1; + } + if (!parameterMap.containsKey(longName)) { LOGGER.warn("parameter \"{}\" not found", longName); return -1; @@ -200,6 +233,10 @@ public final class ParameterCheck { final String[] longValue = parameterMap.get(longName); + if (longValue == null) { + return -1; + } + if (longValue.length == 0 || longValue[0].trim().length() == 0) { LOGGER.warn("value of parameter \"{}\" not found", longName); return -1; 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(); + } +} |