diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-10-05 23:33:00 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-10-05 23:33:47 +0100 |
commit | 5b416f07f49e719d1e0ad1f0830588c1d19a17dd (patch) | |
tree | c3021c20cceee3523c4ae70b3c02b3e5934e6b84 /services/services-engine/src/test/java/org | |
parent | 9cedbd41c0ee3bade89de5d2e1e822cc4e9a36e7 (diff) |
JUnit for apex Admin Protocol listener
Added coverage for this package.
i
Issue-ID: POLICY-1034
Change-Id: I2e4b51983eec245eb9f105587d15f853656ce35a
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'services/services-engine/src/test/java/org')
5 files changed, 646 insertions, 0 deletions
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<AxArtifactKey> getEngineKeys() { + List<AxArtifactKey> 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<Message> 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<Message> 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()); + } +} |