From 5b416f07f49e719d1e0ad1f0830588c1d19a17dd Mon Sep 17 00:00:00 2001 From: liamfallon Date: Fri, 5 Oct 2018 23:33:00 +0100 Subject: JUnit for apex Admin Protocol listener Added coverage for this package. i Issue-ID: POLICY-1034 Change-Id: I2e4b51983eec245eb9f105587d15f853656ce35a Signed-off-by: liamfallon --- services/services-engine/pom.xml | 5 + .../engine/engdep/EngDepMessageListener.java | 5 +- .../engine/engdep/EngDepMessagingService.java | 24 +- .../apex/service/engine/engdep/BadAction.java | 45 +++ .../apex/service/engine/engdep/BadMessage.java | 41 +++ .../service/engine/engdep/DummyEngineService.java | 327 +++++++++++++++++++++ .../engine/engdep/EngDepMessageListenerTest.java | 168 +++++++++++ .../engine/engdep/EngDepMessagingServiceTest.java | 65 ++++ 8 files changed, 668 insertions(+), 12 deletions(-) create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadAction.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadMessage.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingServiceTest.java (limited to 'services') diff --git a/services/services-engine/pom.xml b/services/services-engine/pom.xml index 2b40baa6a..6b339f96e 100644 --- a/services/services-engine/pom.xml +++ b/services/services-engine/pom.xml @@ -64,6 +64,11 @@ commons-cli commons-cli + + org.mockito + mockito-all + test + diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListener.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListener.java index 261ba41cf..1b84c0a92 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListener.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListener.java @@ -256,11 +256,8 @@ public class EngDepMessageListener implements MessageListener, Runnable handleEngineInfoMessage(message, webSocket); break; - case RESPONSE: - throw new ApexException("RESPONSE action on received message not handled by engine"); - default: - break; + throw new ApexException("action " + enDepAction + " on received message not handled by engine"); } } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingService.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingService.java index 7ebcad830..d46a58d49 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingService.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingService.java @@ -31,25 +31,22 @@ import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; /** - * The Class EngDepMessagingService is used to encapsulate the server side of EngDep communication. - * This class allows users to create and start an EngDep server. + * The Class EngDepMessagingService is used to encapsulate the server side of EngDep communication. This class allows + * users to create and start an EngDep server. * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class EngDepMessagingService { private static final XLogger LOGGER = XLoggerFactory.getXLogger(EngDepMessagingService.class); - // Messaging service is used to transmit and receive messages over a communication protocol - private static MessagingServiceFactory messageServiceFactory = new MessagingServiceFactory<>(); private final MessagingService messageService; // The listener that is listening for messages coming in on the EngDep protocol from clients private final EngDepMessageListener messageListener; /** - * Instantiates a new EngDep messaging service. It creates the message service instance, a - * listener for incoming messages, and starts the message listener thread for handling incoming - * messages. + * Instantiates a new EngDep messaging service. It creates the message service instance, a listener for incoming + * messages, and starts the message listener thread for handling incoming messages. * * @param service the Apex engine service that this EngDep service is running for * @param port the port The port to use for EngDep communication @@ -58,7 +55,7 @@ public class EngDepMessagingService { LOGGER.entry(service); // Create the service and listener and add the listener. - messageService = messageServiceFactory.createServer(new InetSocketAddress(MessagingUtils.checkPort(port))); + messageService = getMessageService(port); messageListener = new EngDepMessageListener(service); messageService.addMessageListener(messageListener); @@ -103,4 +100,15 @@ public class EngDepMessagingService { public boolean isStopped() { return !messageService.isStarted(); } + + /** + * Get a message service instance. This method is protected so that it can be intercepted in unit test. + * @param port the message service port + * @return the message service + */ + protected MessagingService getMessageService(final int port) { + // Messaging service is used to transmit and receive messages over a communication protocol + MessagingServiceFactory factory = new MessagingServiceFactory<>(); + return factory.createServer(new InetSocketAddress(MessagingUtils.checkPort(port))); + } } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadAction.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadAction.java new file mode 100644 index 000000000..72cdb54b3 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadAction.java @@ -0,0 +1,45 @@ +/*- + * ============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.service.engine.engdep; + +import org.apache.commons.lang3.NotImplementedException; +import org.onap.policy.apex.core.protocols.Action; + +/** + * Bad action class. + */ +public class BadAction implements Action { + private static final long serialVersionUID = -6562765120898697138L; + + private String actionString; + + public BadAction(final String actionString) { + this.actionString = actionString; + } + + @Override + public String getActionString() { + if (actionString == "throw exception") { + throw new NotImplementedException("dummy IO excepton"); + } + return actionString; + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadMessage.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadMessage.java new file mode 100644 index 000000000..acbfed83f --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadMessage.java @@ -0,0 +1,41 @@ +/*- + * ============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.service.engine.engdep; + +import org.onap.policy.apex.core.protocols.Action; +import org.onap.policy.apex.core.protocols.Message; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * Bad protocol message. + */ +public class BadMessage extends Message { + private static final long serialVersionUID = 3827403727783909797L; + + /** + * Constructor. + * @param action the message action + * @param targetKey the message target key + */ + public BadMessage(Action action, AxArtifactKey targetKey) { + super(action, targetKey); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java new file mode 100644 index 000000000..aa6eb3bb7 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java @@ -0,0 +1,327 @@ +/*- + * ============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.service.engine.engdep; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.apache.commons.lang3.NotImplementedException; +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.AxEngineState; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.service.engine.runtime.ApexEventListener; +import org.onap.policy.apex.service.engine.runtime.EngineService; +import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface; + +/** + * A dummy engine service class. + */ +public class DummyEngineService implements EngineService { + + private AxArtifactKey startEngineKey; + private AxArtifactKey stopEngineKey; + private long periodicPeriod; + private AxArtifactKey statusKey; + private AxArtifactKey runtimeInfoKey; + private int modelKeyGetCalled; + private AxArtifactKey updateModelKey; + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#registerActionListener(java.lang.String, + * org.onap.policy.apex.service.engine.runtime.ApexEventListener) + */ + @Override + public void registerActionListener(String listenerName, ApexEventListener listener) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#deregisterActionListener(java.lang.String) + */ + @Override + public void deregisterActionListener(String listenerName) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#getEngineServiceEventInterface() + */ + @Override + public EngineServiceEventInterface getEngineServiceEventInterface() { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#getKey() + */ + @Override + public AxArtifactKey getKey() { + return new AxArtifactKey("DummyEngineService:0.0.1"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#getEngineKeys() + */ + @Override + public Collection getEngineKeys() { + List keys = new ArrayList<>(); + keys.add(new AxArtifactKey("DummyEngineService:0.0.1")); + return keys; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#getApexModelKey() + */ + @Override + public AxArtifactKey getApexModelKey() { + modelKeyGetCalled++; + + return new AxArtifactKey("DummyApexModelKey:0.0.1"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#updateModel(org.onap.policy.apex.model.basicmodel. + * concepts.AxArtifactKey, java.lang.String, boolean) + */ + @Override + public void updateModel(AxArtifactKey engineServiceKey, String apexModelString, boolean forceFlag) + throws ApexException { + updateModelKey = engineServiceKey; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#updateModel(org.onap.policy.apex.model.basicmodel. + * concepts.AxArtifactKey, org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel, boolean) + */ + @Override + public void updateModel(AxArtifactKey engineServiceKey, AxPolicyModel apexModel, boolean forceFlag) + throws ApexException { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#getState() + */ + @Override + public AxEngineState getState() { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#startAll() + */ + @Override + public void startAll() throws ApexException { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.service.engine.runtime.EngineService#start(org.onap.policy.apex.model.basicmodel.concepts. + * AxArtifactKey) + */ + @Override + public void start(AxArtifactKey engineKey) throws ApexException { + startEngineKey = engineKey; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#stop() + */ + @Override + public void stop() throws ApexException { + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.service.engine.runtime.EngineService#stop(org.onap.policy.apex.model.basicmodel.concepts. + * AxArtifactKey) + */ + @Override + public void stop(AxArtifactKey engineKey) throws ApexException { + stopEngineKey = engineKey; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#clear() + */ + @Override + public void clear() throws ApexException { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.service.engine.runtime.EngineService#clear(org.onap.policy.apex.model.basicmodel.concepts. + * AxArtifactKey) + */ + @Override + public void clear(AxArtifactKey engineKey) throws ApexException { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#isStarted() + */ + @Override + public boolean isStarted() { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#isStarted(org.onap.policy.apex.model.basicmodel. + * concepts.AxArtifactKey) + */ + @Override + public boolean isStarted(AxArtifactKey engineKey) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#isStopped() + */ + @Override + public boolean isStopped() { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#isStopped(org.onap.policy.apex.model.basicmodel. + * concepts.AxArtifactKey) + */ + @Override + public boolean isStopped(AxArtifactKey engineKey) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#startPeriodicEvents(long) + */ + @Override + public void startPeriodicEvents(long period) throws ApexException { + periodicPeriod = period; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#stopPeriodicEvents() + */ + @Override + public void stopPeriodicEvents() throws ApexException { + periodicPeriod = 0; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.runtime.EngineService#getStatus(org.onap.policy.apex.model.basicmodel. + * concepts.AxArtifactKey) + */ + @Override + public String getStatus(AxArtifactKey engineKey) throws ApexException { + statusKey = engineKey; + return "The Status"; + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.service.engine.runtime.EngineService#getRuntimeInfo(org.onap.policy.apex.model.basicmodel. + * concepts.AxArtifactKey) + */ + @Override + public String getRuntimeInfo(AxArtifactKey engineKey) throws ApexException { + runtimeInfoKey = engineKey; + return "The Runtime Info"; + } + + public AxArtifactKey getStartEngineKey() { + return startEngineKey; + } + + public AxArtifactKey getStopEngineKey() { + return stopEngineKey; + } + + public long getPeriodicPeriod() { + return periodicPeriod; + } + + public AxArtifactKey getStatusKey() { + return statusKey; + } + + public AxArtifactKey getRuntimeInfoKey() { + return runtimeInfoKey; + } + + public int getModelKeyGetCalled() { + return modelKeyGetCalled; + } + + public AxArtifactKey getUpdateModelKey() { + return updateModelKey; + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java new file mode 100644 index 000000000..f7ac43dbc --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java @@ -0,0 +1,168 @@ +/*- + * ============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.service.engine.engdep; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; + +import org.java_websocket.WebSocket; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +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.GetEngineInfo; +import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineServiceInfo; +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.core.protocols.engdep.messages.StartEngine; +import org.onap.policy.apex.core.protocols.engdep.messages.StartPeriodicEvents; +import org.onap.policy.apex.core.protocols.engdep.messages.StopEngine; +import org.onap.policy.apex.core.protocols.engdep.messages.StopPeriodicEvents; +import org.onap.policy.apex.core.protocols.engdep.messages.UpdateModel; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * Test the EngDep messaging Service. + */ +public class EngDepMessageListenerTest { + @Mock + private WebSocket webSocketMock; + + /** + * Set up mocking of the engine service facade. + * + * @throws ApexException on engine service facade setup errors + */ + @Before + public void initializeMocking() throws ApexException { + MockitoAnnotations.initMocks(this); + + Mockito.doReturn(new InetSocketAddress("HostAddress", 123)).when(webSocketMock).getRemoteSocketAddress(); + Mockito.doReturn(true).when(webSocketMock).isOpen(); + } + + @Test + public void testMessageListener() throws ApexException { + DummyEngineService dummyEngineService = new DummyEngineService(); + EngDepMessageListener listener = new EngDepMessageListener(dummyEngineService); + listener.startProcessorThread(); + + try { + listener.onMessage("bad string message"); + fail("test should throw an exception"); + } catch (Exception uoe) { + assertEquals("String messages are not supported on the EngDep protocol", uoe.getMessage()); + } + + List messageList = new ArrayList<>(); + messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals("Start:0.0.1", dummyEngineService.getStartEngineKey().getId()); + + messageList.clear(); + messageList.add(new StopEngine(new AxArtifactKey("Stop:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals("Stop:0.0.1", dummyEngineService.getStopEngineKey().getId()); + + messageList.clear(); + messageList.add(new StartPeriodicEvents(new AxArtifactKey("StartPeriodic:0.0.1"), "12345")); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals(12345, dummyEngineService.getPeriodicPeriod()); + + messageList.clear(); + messageList.add(new StopPeriodicEvents(new AxArtifactKey("StopPeriodic:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals(0, dummyEngineService.getPeriodicPeriod()); + + messageList.clear(); + messageList.add(new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals("EngineInfo:0.0.1", dummyEngineService.getRuntimeInfoKey().getId()); + + messageList.clear(); + messageList.add(new GetEngineStatus(new AxArtifactKey("EngineStatus:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals("EngineStatus:0.0.1", dummyEngineService.getStatusKey().getId()); + + messageList.clear(); + messageList.add(new GetEngineServiceInfo(new AxArtifactKey("EngineServiceInfo:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals(1, dummyEngineService.getModelKeyGetCalled()); + + messageList.clear(); + messageList.add(new UpdateModel(new AxArtifactKey("UpdateModel:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId()); + + try { + messageList.clear(); + messageList.add(new Response(new AxArtifactKey("UpdateModel:0.0.1"), false, + new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1")))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId()); + + messageList.clear(); + Message badMessage0 = new BadMessage(null, null); + messageList.add(badMessage0); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + + messageList.clear(); + Message badMessage1 = new BadMessage(new BadAction(null), null); + messageList.add(badMessage1); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + + messageList.clear(); + Message badMessage2 = new BadMessage(new BadAction("throw exception"), null); + messageList.add(badMessage2); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + + Mockito.doReturn(false).when(webSocketMock).isOpen(); + messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1"))); + listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); + ThreadUtilities.sleep(50); + } catch (Exception e) { + fail("test should not throw exceptions on bad messages"); + } + listener.stopProcessorThreads(); + ThreadUtilities.sleep(50); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingServiceTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingServiceTest.java new file mode 100644 index 000000000..901cbeac4 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessagingServiceTest.java @@ -0,0 +1,65 @@ +/*- + * ============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.service.engine.engdep; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.core.infrastructure.messaging.MessagingService; +import org.onap.policy.apex.core.protocols.Message; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; + +/** + * Test the EngDep messaging Service. + */ +public class EngDepMessagingServiceTest { + @Mock + private MessagingService messageServiceMock; + private EngDepMessagingService edMessagingService; + + /** + * Set up mocking of the engine service facade. + * + * @throws ApexException on engine service facade setup errors + */ + @Before + public void initializeMocking() throws ApexException { + MockitoAnnotations.initMocks(this); + + edMessagingService = Mockito.spy(new EngDepMessagingService(new DummyEngineService(), 12345)); + Mockito.doReturn(messageServiceMock).when(edMessagingService).getMessageService(12345); + } + + @Test + public void testStartStop() throws ApexException { + edMessagingService.start(); + assertTrue(edMessagingService.isStarted()); + assertFalse(edMessagingService.isStopped()); + edMessagingService.stop(); + assertTrue(edMessagingService.isStopped()); + assertFalse(edMessagingService.isStarted()); + } +} -- cgit 1.2.3-korg