diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-09-27 22:42:56 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-09-27 22:43:05 +0100 |
commit | d67a4dbff408ebf73a25158c85169fb7165e357e (patch) | |
tree | 8258809f1cf086c02edbc7f321ddd0d9d4218d47 /core/core-deployment/src/test | |
parent | afd3a44e210de3c1aed9f6f7278913b2a2f2f6d2 (diff) |
Unit test for core deployment
This module was not covered by unit test.
Issue-ID: POLICY-1034
Change-Id: I700d43c0bde58c2b236f46994f380e20116418d9
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'core/core-deployment/src/test')
4 files changed, 372 insertions, 0 deletions
diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/ApexDeploymentExceptionTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/ApexDeploymentExceptionTest.java new file mode 100644 index 000000000..d87c6caa5 --- /dev/null +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/ApexDeploymentExceptionTest.java @@ -0,0 +1,46 @@ +/*- + * ============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.core.deployment; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import org.junit.Test; + +/** + * Test the Apex deployment Exception. + * + */ +public class ApexDeploymentExceptionTest { + + @Test + public void testDeploymentException() { + ApexDeploymentException ade0 = new ApexDeploymentException("a message"); + assertNotNull(ade0); + assertEquals("a message", ade0.getMessage()); + + ApexDeploymentException ade1 = new ApexDeploymentException("a message", new IOException()); + assertNotNull(ade1); + assertEquals("a message", ade0.getMessage()); + } +} diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DeploymentClientTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DeploymentClientTest.java new file mode 100644 index 000000000..b12e526d7 --- /dev/null +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DeploymentClientTest.java @@ -0,0 +1,139 @@ +/*- + * ============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.core.deployment; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyObject; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder; +import org.onap.policy.apex.core.infrastructure.messaging.MessageListener; +import org.onap.policy.apex.core.infrastructure.messaging.MessagingService; +import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory; +import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; +import org.onap.policy.apex.core.protocols.Message; +import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus; +import org.onap.policy.apex.core.protocols.engdep.messages.Response; +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * Test the deployment web socket client. + */ +@RunWith(MockitoJUnitRunner.class) +public class DeploymentClientTest { + @Mock + private static MessagingServiceFactory<Message> mockServiceFactory; + + @Mock + private static MessagingService<Message> mockService; + + @SuppressWarnings("rawtypes") + ArgumentCaptor<MessageListener> messageListener = ArgumentCaptor.forClass(MessageListener.class); + + @SuppressWarnings("unchecked") + @Test + public void testDeploymentClientStart() throws Exception { + DeploymentClient deploymentClient = new DeploymentClient("localhost", 51273); + + final Field factoryField = deploymentClient.getClass().getDeclaredField("factory"); + factoryField.setAccessible(true); + factoryField.set(deploymentClient, mockServiceFactory); + + Mockito.doReturn(mockService).when(mockServiceFactory).createClient(anyObject()); + + Mockito.doNothing().when(mockService).addMessageListener(messageListener.capture()); + Mockito.doNothing().when(mockService).startConnection(); + + Mockito.doNothing().when(mockService).send((MessageHolder<Message>) anyObject()); + + Thread clientThread = new Thread(deploymentClient); + clientThread.start(); + + ThreadUtilities.sleep(20); + + assertTrue(deploymentClient.isStarted()); + assertTrue(clientThread.isAlive()); + + AxArtifactKey engineKey = new AxArtifactKey("MyEngine", "0.0.1"); + GetEngineStatus getEngineStatus = new GetEngineStatus(engineKey); + deploymentClient.sendMessage(new GetEngineStatus(engineKey)); + + ThreadUtilities.sleep(20); + Response response = new Response(engineKey, true, getEngineStatus); + List<Message> messageList = new ArrayList<>(); + messageList.add(response); + + MessageBlock<Message> responseBlock = new MessageBlock<>(messageList, null); + messageListener.getValue().onMessage(responseBlock); + + try { + messageListener.getValue().onMessage("StringMessage"); + fail("test should throw an exception here"); + } catch (UnsupportedOperationException use) { + assertEquals("String mesages are not supported on the EngDep protocol", use.getMessage()); + } + + ThreadUtilities.sleep(300); + assertEquals(1, deploymentClient.getMessagesSent()); + assertEquals(2, deploymentClient.getMessagesReceived()); + + deploymentClient.stopClient(); + } + + @Test + public void testDeploymentClientStartException() throws Exception { + DeploymentClient depoymentClient = new DeploymentClient("localhost", 51273); + + final Field factoryField = depoymentClient.getClass().getDeclaredField("factory"); + factoryField.setAccessible(true); + factoryField.set(depoymentClient, mockServiceFactory); + + Mockito.doReturn(mockService).when(mockServiceFactory).createClient(anyObject()); + + Mockito.doNothing().when(mockService).addMessageListener(anyObject()); + Mockito.doThrow(new ApexRuntimeException("connection start failed")).when(mockService).startConnection(); + + Thread clientThread = new Thread(depoymentClient); + clientThread.start(); + + ThreadUtilities.sleep(50); + + assertFalse(depoymentClient.isStarted()); + assertFalse(clientThread.isAlive()); + assertEquals(0, depoymentClient.getReceiveQueue().size()); + + ThreadUtilities.sleep(100); + } +} diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/PeriodicEventManagerTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/PeriodicEventManagerTest.java new file mode 100644 index 000000000..fb204c3a3 --- /dev/null +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/PeriodicEventManagerTest.java @@ -0,0 +1,145 @@ +/*- + * ============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.core.deployment; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +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; + +/** + * Test the periodic event manager utility. + */ +public class PeriodicEventManagerTest { + @Test + public void testPeroidicEventManager() { + try { + final String[] EventArgs = + { "-h" }; + + PeriodicEventManager.main(EventArgs); + } catch (Exception exc) { + fail("test should not throw an exception"); + } + } + + @Test + public void testPeroidicEventManagerNoOptions() { + final String[] EventArgs = new String[] + {}; + + final String outputString = runPeriodicEventManager(EventArgs); + + assertTrue(outputString + .contains("usage: Deployer <server address> <port address> <start/stop> <periods in ms>")); + } + + @Test + public void testPeroidicEventManagerBadOptions() { + final String[] EventArgs = + { "-zabbu" }; + + final String outputString = runPeriodicEventManager(EventArgs); + + assertTrue(outputString + .contains("usage: Deployer <server address> <port address> <start/stop> <periods in ms>")); + } + + @Test + public void testPeroidicEventManagerNonNumeric3() { + final String[] EventArgs = + { "aaa", "bbb", "ccc", "ddd" }; + + try { + runPeriodicEventManager(EventArgs); + } catch (NumberFormatException nfe) { + assertEquals("For input string: \"bbb\"", nfe.getMessage()); + } + } + + @Test + public void testPeroidicEventManagerNonNumeric2() { + final String[] EventArgs = + { "aaa", "12345", "ccc", "1000" }; + + try { + runPeriodicEventManager(EventArgs); + } catch (NumberFormatException nfe) { + assertEquals("For input string: \"ddd\"", nfe.getMessage()); + } + } + + @Test + public void testPeroidicEventManagerStart() { + final String[] EventArgs = + { "localhost", "12345", "start", "1000" }; + + final String outputString = runPeriodicEventManager(EventArgs); + + assertTrue(outputString.contains("\n*** StdErr ***\n\n*** exception ***")); + } + + + @Test + public void testPeroidicEventManagerStop() { + final String[] EventArgs = + { "localhost", "12345", "stop", "1000" }; + + final String outputString = runPeriodicEventManager(EventArgs); + + assertTrue(outputString.contains("\n*** StdErr ***\n\n*** exception ***")); + } + + /** + * Run the application. + * + * @param eventArgs the command arguments + * @return a string containing the command output + */ + private String runPeriodicEventManager(final String[] eventArgs) { + final ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); + final ByteArrayOutputStream baosErr = new ByteArrayOutputStream(); + + PeriodicEventManager peManager = new PeriodicEventManager(eventArgs, new PrintStream(baosOut, true)); + + String exceptionString = ""; + try { + peManager.init(); + } catch (ApexDeploymentException ade) { + exceptionString = ade.getCascadedMessage(); + } + + 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 + "\n*** exception ***\n" + + exceptionString; + } +} diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/TestMessageListener.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/TestMessageListener.java new file mode 100644 index 000000000..8bb1c7e37 --- /dev/null +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/TestMessageListener.java @@ -0,0 +1,42 @@ +/*- + * ============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.core.deployment; + +import static org.junit.Assert.fail; + +import org.onap.policy.apex.core.infrastructure.messaging.MessageListener; +import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock; +import org.onap.policy.apex.core.protocols.Message; + +/** + * A test message listener. + */ +public class TestMessageListener implements MessageListener<Message> { + @Override + public void onMessage(String messageString) { + fail("Message should not be received"); + } + + @Override + public void onMessage(MessageBlock<Message> data) { + fail("Message should not be received"); + } +} |