diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-09-28 22:48:30 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-09-30 22:17:06 +0100 |
commit | acb59c7b0ccbb86f4c38a94b72e7bab73b2acf8f (patch) | |
tree | e2f4a368b3a04572babc1df900369f6954fcc6da /client/client-monitoring/src | |
parent | 4ee8953bc55f2b905dee845728b3287eb4e8e795 (diff) |
Unit test for client monitoring
Coverage for the client monitoring module.
Issue-ID: POLICY-1034
Change-Id: I6bb6dab26ce0572923c3d451a3b5f4745d40d088
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'client/client-monitoring/src')
7 files changed, 737 insertions, 3 deletions
diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java index 6b7f12061..07e2efd13 100644 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java +++ b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java @@ -213,7 +213,7 @@ public class ApexMonitoringRestResource { } /** - * Start/Stop and Apex engine. + * Start/Stop Apex engine Periodic Events. * * @param hostName the host name of the engine service to connect to. * @param port the port number of the engine service to connect to. @@ -332,6 +332,41 @@ public class ApexMonitoringRestResource { return super.add(elm); } + private ApexMonitoringRestResource getOuterType() { + return ApexMonitoringRestResource.this; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + getOuterType().hashCode(); + result = prime * result + maxEntries; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (!super.equals(obj)) { + return false; + } + + if (getClass() != obj.getClass()) { + return false; + } + + @SuppressWarnings("unchecked") + SlidingWindowList<V> other = (SlidingWindowList<V>) obj; + if (!getOuterType().equals(other.getOuterType())) { + return false; + } + + return maxEntries == other.maxEntries; + } } /** diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheck.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheck.java index acaee5fdb..34bacb974 100644 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheck.java +++ b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheck.java @@ -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,13 +99,21 @@ 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; } final String[] portValue = parameterMap.get(PORT_PAR); - + + if (portValue == null) { + return -1; + } + if (portValue.length == 0 || portValue[0].trim().length() == 0) { LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND); return -1; @@ -127,6 +143,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 +167,13 @@ 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 +185,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); @@ -166,6 +196,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); @@ -193,6 +227,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 +238,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-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringExceptionTest.java b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringExceptionTest.java new file mode 100644 index 000000000..3b5ecd100 --- /dev/null +++ b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringExceptionTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.monitoring.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +/** + * Test the REST monitoring exception. + * + */ +public class MonitoringExceptionTest { + + @Test + public void test() { + ApexMonitoringRestParameterException ame = new ApexMonitoringRestParameterException("a message"); + assertNotNull(ame); + assertEquals("a message", ame.getMessage()); + } +} diff --git a/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestExtraTest.java b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestExtraTest.java new file mode 100644 index 000000000..277b2ed17 --- /dev/null +++ b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestExtraTest.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.monitoring.rest; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Extra monitoring rest tests. + * + */ +public class MonitoringRestExtraTest { + + @Test + public void test() { + ApexMonitoringRestParameters parameters = new ApexMonitoringRestParameters(); + parameters.setRestPort(12345); + assertEquals(12345, parameters.getRestPort()); + } +} diff --git a/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestMainTest.java b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestMainTest.java new file mode 100644 index 000000000..0c929f534 --- /dev/null +++ b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestMainTest.java @@ -0,0 +1,261 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.monitoring.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; + +import org.junit.Test; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; + +/** + * Test the periodic event manager utility. + */ +public class MonitoringRestMainTest { + @Test + public void testMonitoringClientBad() { + try { + final String[] eventArgs = + { "-z" }; + + ApexMonitoringRestMain.main(eventArgs); + } catch (Exception exc) { + fail("test should not throw an exception"); + } + } + + @Test + public void testMonitoringClientOk() { + try { + final String[] eventArgs = + { "-t", "1" }; + + ApexMonitoringRestMain.main(eventArgs); + } catch (Exception exc) { + fail("test should not throw an exception"); + } + } + + @Test + public void testMonitoringClientNoOptions() { + final String[] eventArgs = new String[] + {}; + + final String outputString = testApexMonitoringRestMainConstructor(eventArgs); + + System.err.println(outputString); + assertEquals("*** StdOut ***\n\n*** StdErr ***\n", outputString); + } + + @Test + public void testMonitoringClientBadOptions() { + final String[] eventArgs = + { "-zabbu" }; + + try { + new ApexMonitoringRestMain(eventArgs, System.out); + fail("test should throw an exception"); + } catch (Exception ex) { + assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " + + "parameter error, invalid command line arguments specified " + + ": Unrecognized option: -zabbu", ex.getMessage().substring(0, 170)); + } + } + + @Test + public void testMonitoringClientHelp() { + final String[] eventArgs = + { "-h" }; + + try { + new ApexMonitoringRestMain(eventArgs, System.out); + fail("test should throw an exception"); + } catch (Exception ex) { + assertEquals("usage: org.onap.policy.apex.client.monitoring.rest.ApexMonitoringRestMain [options...]", + ex.getMessage().substring(0, 86)); + } + } + + @Test + public void testMonitoringClientPortBad() { + final String[] eventArgs = + { "-p", "hello" }; + + try { + new ApexMonitoringRestMain(eventArgs, System.out); + fail("test should throw an exception"); + } catch (Exception ex) { + assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " + + "parameter error, error parsing argument \"port\" :For input string: \"hello\"", + ex.getMessage().substring(0, 156)); + } + } + + @Test + public void testMonitoringClientPortNegative() { + final String[] eventArgs = + { "-p", "-1" }; + + try { + new ApexMonitoringRestMain(eventArgs, System.out); + fail("test should throw an exception"); + } catch (Exception ex) { + assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: " + + "URI=http://localhost:-1/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, " + + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 227)); + } + } + + @Test + public void testMonitoringClientTtlTooSmall() { + final String[] eventArgs = + { "-t", "-2" }; + + try { + new ApexMonitoringRestMain(eventArgs, System.out); + fail("test should throw an exception"); + } catch (Exception ex) { + assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: " + + "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)", + ex.getMessage().substring(0, 244)); + } + } + + @Test + public void testMonitoringClientTooManyPars() { + final String[] eventArgs = + { "-t", "10", "-p", "12344", "aaa", "bbb" }; + + try { + new ApexMonitoringRestMain(eventArgs, System.out); + fail("test should throw an exception"); + } catch (Exception ex) { + assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " + + "parameter error, too many command line arguments specified : [aaa, bbb]", + ex.getMessage().substring(0, 154)); + } + } + + @Test + public void testMonitoringClientTtlNotNumber() { + final String[] eventArgs = + { "-t", "timetolive" }; + + try { + new ApexMonitoringRestMain(eventArgs, System.out); + fail("test should throw an exception"); + } catch (Exception ex) { + assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[null], State=STOPPED) " + + "parameter error, error parsing argument \"time-to-live\" :" + + "For input string: \"timetolive\"", ex.getMessage().substring(0, 169)); + } + } + + @Test + public void testMonitoringClientPortTooBig() { + final String[] eventArgs = + { "-p", "65536" }; + + try { + new ApexMonitoringRestMain(eventArgs, System.out); + fail("test should throw an exception"); + } catch (Exception ex) { + assertEquals("Apex Services REST endpoint (ApexMonitoringRestMain: Config=[ApexMonitoringRestParameters: " + + "URI=http://localhost:65536/apexservices/, TTL=-1sec], State=STOPPED) parameters invalid, " + + "port must be greater than 1023 and less than 65536", ex.getMessage().substring(0, 230)); + } + } + + @Test + public void testMonitoringClientDefaultPars() { + try { + ApexMonitoringRest monRest = new ApexMonitoringRest(); + monRest.shutdown(); + + } catch (Exception ex) { + fail("test should not throw an exception"); + } + } + + @Test + public void testMonitoringOneSecStart() { + final String[] eventArgs = + { "-t", "1" }; + + try { + ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out); + monRestMain.init(); + monRestMain.shutdown(); + + } catch (Exception ex) { + fail("test should not throw an exception"); + } + } + + @Test + public void testMonitoringForeverStart() { + final String[] eventArgs = + { "-t", "-1" }; + + ApexMonitoringRestMain monRestMain = new ApexMonitoringRestMain(eventArgs, System.out); + + Thread monThread = new Thread() { + public void run() { + monRestMain.init(); + } + }; + + try { + monThread.start(); + ThreadUtilities.sleep(2000); + monRestMain.shutdown(); + } catch (Exception ex) { + fail("test should not throw an exception"); + } + } + + /** + * Run the application. + * + * @param eventArgs the command arguments + * @return a string containing the command output + */ + private String testApexMonitoringRestMainConstructor(final String[] eventArgs) { + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + final ByteArrayOutputStream baosErr = new ByteArrayOutputStream(); + + new ApexMonitoringRestMain(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-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheckTest.java b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheckTest.java new file mode 100644 index 000000000..026f4d3dd --- /dev/null +++ b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheckTest.java @@ -0,0 +1,192 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.monitoring.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)); + parameterMap.put("port", null); + 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-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/RestResourceTest.java b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/RestResourceTest.java new file mode 100644 index 000000000..d63f6bd8c --- /dev/null +++ b/client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/RestResourceTest.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.monitoring.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import javax.ws.rs.core.Response; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.core.deployment.EngineServiceFacade; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +/** + * Test the monitoring rest resource. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(ApexMonitoringRestResource.class) +public class RestResourceTest { + @Mock + EngineServiceFacade engineServiceFacadeMock; + + /** + * Set up the mocking for this test. + * + * @throws Exception on mock setup failures + */ + @Before + public void setupFacade() throws Exception { + MockitoAnnotations.initMocks(this); + PowerMockito.whenNew(EngineServiceFacade.class).withAnyArguments().thenReturn(engineServiceFacadeMock); + } + + @Test + public void testRestResourceCreateSession() throws ApexException { + final AxArtifactKey engineServiceKey = new AxArtifactKey("EngineServiceKey", "0.0.1"); + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + final AxArtifactKey[] engineServiceKeyArray = + { engineKey }; + final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]); + + Mockito.when(engineServiceFacadeMock.getKey()).thenReturn(engineServiceKey); + Mockito.when(engineServiceFacadeMock.getEngineKeyArray()).thenReturn(engineServiceKeyArray); + Mockito.when(engineServiceFacadeMock.getEngineStatus(engineKey)).thenReturn(engineModel); + + ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); + Response response = restResource.createSession("apexServer", 12345); + assertEquals(200, response.getStatus()); + assertTrue(((String) response.getEntity()).contains(engineKey.getId())); + } + + @Test + public void testRestResourceStartEngine() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + + ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); + Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start"); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceStopEngine() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + + ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); + Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Stop"); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceStartPeriodicEvents() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + + ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); + Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Start", 1000); + assertEquals(200, response.getStatus()); + } + + @Test + public void testRestResourceStopEPeriodicEvents() throws ApexException { + final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1"); + + ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); + Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000); + assertEquals(200, response.getStatus()); + } + + @Test + public void testCounter() { + ApexMonitoringRestResource restResource = new ApexMonitoringRestResource(); + + ApexMonitoringRestResource.Counter counter = restResource.new Counter(1538338576, 1538338592); + + assertEquals(1538338576, counter.getTimestamp()); + assertEquals(1538338592, counter.getValue()); + } +} |