diff options
Diffstat (limited to 'runtime-controlloop/src/test/java')
4 files changed, 114 insertions, 55 deletions
diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java index 82f7e66d5..ae1c59851 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java @@ -23,6 +23,9 @@ package org.onap.policy.clamp.controlloop.runtime.instantiation; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import java.io.IOException; import java.util.ArrayList; @@ -34,6 +37,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider; @@ -63,7 +67,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; * */ class ControlLoopInstantiationProviderTest { - + private static final String ID_NAME = "PMSH_Instance1"; + private static final String ID_VERSION = "1.2.3"; private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json"; private static final String CL_INSTANTIATION_UPDATE_JSON = "src/test/resources/rest/controlloops/ControlLoopsUpdate.json"; @@ -133,7 +138,9 @@ class ControlLoopInstantiationProviderTest { var participantRegisterAckPublisher = Mockito.mock(ParticipantRegisterAckPublisher.class); var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class); var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class); - supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, + var modelsProvider = Mockito.mock(PolicyModelsProvider.class); + + supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, modelsProvider, controlLoopUpdatePublisher, controlLoopStateChangePublisher, participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher); } @@ -155,6 +162,24 @@ class ControlLoopInstantiationProviderTest { } @Test + void testIntanceResponses() throws PfModelException { + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); + var instancePropertyList = instantiationProvider.createInstanceProperties(serviceTemplate); + assertNull(instancePropertyList.getErrorDetails()); + var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION); + assertEquals(id, instancePropertyList.getAffectedInstanceProperties().get(0)); + var instanceOrderState = instantiationProvider.getInstantiationOrderState(ID_NAME, ID_VERSION); + assertEquals(ControlLoopOrderedState.UNINITIALISED, instanceOrderState.getOrderedState()); + assertEquals(ID_NAME, instanceOrderState.getControlLoopIdentifierList().get(0).getName()); + + assertNotNull(clProvider.getControlLoop(id)); + var instanceResponse = instantiationProvider.deleteInstanceProperties(ID_NAME, ID_VERSION); + assertEquals(ID_NAME, instanceResponse.getAffectedControlLoops().get(0).getName()); + assertNull(clProvider.getControlLoop(id)); + } + + @Test void testInstantiationCrud() throws Exception { participantProvider.createParticipants(CommonTestData.createParticipants()); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandlerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandlerTest.java index 0ad8aad43..bbc4deec1 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandlerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandlerTest.java @@ -23,7 +23,6 @@ package org.onap.policy.clamp.controlloop.runtime.supervision; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyList; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -32,6 +31,7 @@ import java.util.List; import java.util.Map; import java.util.UUID; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; @@ -56,9 +56,12 @@ import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantReg import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class SupervisionHandlerTest { + private static final String TOSCA_TEMPLATE_YAML = + "src/test/resources/rest/servicetemplates/tosca-for-smoke-testing.yaml"; private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json"; private static final ToscaConceptIdentifier identifier = new ToscaConceptIdentifier("PMSHInstance0Crud", "1.0.1"); private static final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("ParticipantId", "1.0.0"); @@ -133,7 +136,7 @@ class SupervisionHandlerTest { participant.setParticipantType(participantType); var participantProvider = mock(ParticipantProvider.class); - when(participantProvider.getParticipants(eq(participantId.getName()), eq(participantId.getVersion()))) + when(participantProvider.getParticipants(participantId.getName(), participantId.getVersion())) .thenReturn(List.of(participant)); var participantDeregisterMessage = new ParticipantDeregister(); @@ -149,7 +152,7 @@ class SupervisionHandlerTest { handler.handleParticipantMessage(participantDeregisterMessage); verify(participantProvider).updateParticipants(anyList()); - verify(participantDeregisterAckPublisher).send(eq(participantDeregisterMessage.getMessageId())); + verify(participantDeregisterAckPublisher).send(participantDeregisterMessage.getMessageId()); } @Test @@ -173,8 +176,8 @@ class SupervisionHandlerTest { handler.handleParticipantMessage(participantRegisterMessage); verify(participantProvider).createParticipants(anyList()); - verify(participantRegisterAckPublisher).send(eq(participantRegisterMessage.getMessageId()), eq(participantId), - eq(participantType)); + verify(participantRegisterAckPublisher).send(participantRegisterMessage.getMessageId(), participantId, + participantType); } @Test @@ -185,7 +188,7 @@ class SupervisionHandlerTest { participant.setParticipantType(participantType); var participantProvider = mock(ParticipantProvider.class); - when(participantProvider.getParticipants(eq(participantId.getName()), eq(participantId.getVersion()))) + when(participantProvider.getParticipants(participantId.getName(), participantId.getVersion())) .thenReturn(List.of(participant)); var participantUpdateAckMessage = new ParticipantUpdateAck(); @@ -231,8 +234,8 @@ class SupervisionHandlerTest { participantUpdatePublisher); handler.handleSendCommissionMessage(participantId.getName(), participantId.getVersion()); - verify(participantUpdatePublisher).sendComissioningBroadcast(eq(participantId.getName()), - eq(participantId.getVersion())); + verify(participantUpdatePublisher).sendComissioningBroadcast(participantId.getName(), + participantId.getVersion()); } @Test @@ -260,9 +263,13 @@ class SupervisionHandlerTest { var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); - when(controlLoopProvider.getControlLoop(eq(identifier))).thenReturn(controlLoop); + when(controlLoopProvider.getControlLoop(identifier)).thenReturn(controlLoop); - return new SupervisionHandler(controlLoopProvider, participantProvider, monitoringProvider, + var modelsProvider = Mockito.mock(PolicyModelsProvider.class); + when(modelsProvider.getServiceTemplateList(any(), any())) + .thenReturn(List.of(InstantiationUtils.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML))); + + return new SupervisionHandler(controlLoopProvider, participantProvider, monitoringProvider, modelsProvider, controlLoopUpdatePublisher, controlLoopStateChangePublisher, participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java index 47cc6f1dc..f884e0129 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java @@ -29,11 +29,9 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.List; -import java.util.Map; -import java.util.UUID; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; @@ -47,6 +45,7 @@ import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpd import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusReqPublisher; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @@ -56,11 +55,19 @@ class SupervisionScannerTest { private static final String TOSCA_SERVICE_TEMPLATE_YAML = "src/test/resources/rest/servicetemplates/tosca-for-smoke-testing.yaml"; + private static final String CONTROLLOOP_JSON = "src/test/resources/rest/controlloops/ControlLoopsSmoke.json"; + + private static PolicyModelsProvider modelsProvider = mock(PolicyModelsProvider.class); + + @BeforeAll + public static void setUpBeforeAll() throws Exception { + ToscaServiceTemplate serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML); + when(modelsProvider.getServiceTemplateList(null, null)).thenReturn(List.of(serviceTemplate)); + } @Test - void testScannerOrderedStateEqualsToState() throws PfModelException { + void testScannerOrderedStateEqualsToState() throws PfModelException, CoderException { var controlLoopProvider = mock(ControlLoopProvider.class); - var modelsProvider = mock(PolicyModelsProvider.class); var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class); var participantProvider = mock(ParticipantProvider.class); @@ -68,8 +75,9 @@ class SupervisionScannerTest { var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner"); - var controlLoop = new ControlLoop(); - when(controlLoopProvider.getControlLoops(null, null)).thenReturn(List.of(controlLoop)); + var controlLoops = + InstantiationUtils.getControlLoopsFromResource(CONTROLLOOP_JSON, "Crud").getControlLoopList(); + when(controlLoopProvider.getControlLoops(null, null)).thenReturn(controlLoops); var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, @@ -80,13 +88,13 @@ class SupervisionScannerTest { } @Test - void testScannerOrderedStateDifferentToState() throws PfModelException { - var controlLoop = new ControlLoop(); - controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE); - controlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED); - controlLoop.setElements(Map.of(UUID.randomUUID(), new ControlLoopElement())); + void testScannerOrderedStateDifferentToState() throws PfModelException, CoderException { + var controlLoops = + InstantiationUtils.getControlLoopsFromResource(CONTROLLOOP_JSON, "Crud").getControlLoopList(); + controlLoops.get(0).setState(ControlLoopState.UNINITIALISED2PASSIVE); + controlLoops.get(0).setOrderedState(ControlLoopOrderedState.UNINITIALISED); var controlLoopProvider = mock(ControlLoopProvider.class); - when(controlLoopProvider.getControlLoops(null, null)).thenReturn(List.of(controlLoop)); + when(controlLoopProvider.getControlLoops(null, null)).thenReturn(controlLoops); var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class); var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); @@ -95,9 +103,6 @@ class SupervisionScannerTest { var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner"); - var modelsProvider = mock(PolicyModelsProvider.class); - when(modelsProvider.getServiceTemplateList(null, null)).thenReturn(List.of(new ToscaServiceTemplate())); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); @@ -118,7 +123,6 @@ class SupervisionScannerTest { participant.setVersion("1.0.0"); when(participantProvider.getParticipants(null, null)).thenReturn(List.of(participant)); - var modelsProvider = mock(PolicyModelsProvider.class); var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class); var participantStatusReqPublisher = mock(ParticipantStatusReqPublisher.class); var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); @@ -136,19 +140,24 @@ class SupervisionScannerTest { } @Test - void testSendControlLoopMsgUpdate() throws PfModelException { - var controlLoop = new ControlLoop(); - controlLoop.setState(ControlLoopState.UNINITIALISED2PASSIVE); - controlLoop.setOrderedState(ControlLoopOrderedState.PASSIVE); - controlLoop.setElements(Map.of(UUID.randomUUID(), - createHttpElement(ControlLoopState.UNINITIALISED, ControlLoopOrderedState.PASSIVE))); + void testSendControlLoopMsgUpdate() throws PfModelException, CoderException { + var controlLoops = + InstantiationUtils.getControlLoopsFromResource(CONTROLLOOP_JSON, "Crud").getControlLoopList(); + controlLoops.get(0).setState(ControlLoopState.UNINITIALISED2PASSIVE); + controlLoops.get(0).setOrderedState(ControlLoopOrderedState.PASSIVE); + for (var element : controlLoops.get(0).getElements().values()) { + if ("org.onap.domain.database.Http_PMSHMicroserviceControlLoopElement" + .equals(element.getDefinition().getName())) { + element.setOrderedState(ControlLoopOrderedState.PASSIVE); + element.setState(ControlLoopState.UNINITIALISED); + } else { + element.setOrderedState(ControlLoopOrderedState.PASSIVE); + element.setState(ControlLoopState.PASSIVE); + } + } var controlLoopProvider = mock(ControlLoopProvider.class); - when(controlLoopProvider.getControlLoops(null, null)).thenReturn(List.of(controlLoop)); - - ToscaServiceTemplate serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML); - var modelsProvider = mock(PolicyModelsProvider.class); - when(modelsProvider.getServiceTemplateList(null, null)).thenReturn(List.of(serviceTemplate)); + when(controlLoopProvider.getControlLoops(null, null)).thenReturn(controlLoops); var participantProvider = mock(ParticipantProvider.class); var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class); @@ -166,19 +175,6 @@ class SupervisionScannerTest { verify(controlLoopUpdatePublisher).send(any(ControlLoop.class), anyInt()); } - private ControlLoopElement createHttpElement(ControlLoopState state, ControlLoopOrderedState orderedState) { - var element = new ControlLoopElement(); - element.setDefinition(new ToscaConceptIdentifier( - "org.onap.domain.database.Http_PMSHMicroserviceControlLoopElement", "1.2.3")); - element.setState(state); - element.setOrderedState(orderedState); - element.setParticipantId(new ToscaConceptIdentifier("HttpParticipant0", "1.0.0")); - element.setParticipantType( - new ToscaConceptIdentifier("org.onap.k8s.controlloop.HttpControlLoopParticipant", "2.3.4")); - - return element; - } - @Test void testScanParticipant() throws PfModelException { var controlLoopProvider = mock(ControlLoopProvider.class); @@ -199,7 +195,6 @@ class SupervisionScannerTest { var participantProvider = new ParticipantProvider(clRuntimeParameterGroup.getDatabaseProviderParameters()); participantProvider.updateParticipants(List.of(participant)); - var modelsProvider = mock(PolicyModelsProvider.class); var controlLoopUpdatePublisher = mock(ControlLoopUpdatePublisher.class); var participantStatusReqPublisher = mock(ParticipantStatusReqPublisher.class); var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java index 70d115e47..4dfb8a9fd 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java @@ -22,14 +22,21 @@ package org.onap.policy.clamp.controlloop.runtime.supervision.comm; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import java.time.Instant; import java.util.Collections; +import java.util.List; import java.util.UUID; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; @@ -51,6 +58,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class SupervisionMessagesTest extends CommonRestController { + private static final String NOT_ACTIVE = "Not Active!"; private static final Object lockit = new Object(); private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; @@ -77,12 +85,13 @@ class SupervisionMessagesTest extends CommonRestController { var monitoringProvider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider); var participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); + var modelsProvider = Mockito.mock(PolicyModelsProvider.class); var controlLoopUpdatePublisher = Mockito.mock(ControlLoopUpdatePublisher.class); var controlLoopStateChangePublisher = Mockito.mock(ControlLoopStateChangePublisher.class); var participantRegisterAckPublisher = Mockito.mock(ParticipantRegisterAckPublisher.class); var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class); var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class); - supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, + supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, modelsProvider, controlLoopUpdatePublisher, controlLoopStateChangePublisher, participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher); } @@ -154,6 +163,29 @@ class SupervisionMessagesTest extends CommonRestController { } } + @Test + void testSendControlLoopStateChangePublisherNotActive() { + var publisher = new ControlLoopStateChangePublisher(); + assertThatThrownBy(() -> publisher.send(getControlLoop(), 0)).hasMessage(NOT_ACTIVE); + } + + @Test + void testSendControlLoopStateChangePublisher() { + var publisher = new ControlLoopStateChangePublisher(); + var topicSink = mock(TopicSink.class); + publisher.active(List.of(topicSink)); + publisher.send(getControlLoop(), 0); + verify(topicSink).send(anyString()); + } + + private ControlLoop getControlLoop() { + var controlLoop = new ControlLoop(); + controlLoop.setName("NAME"); + controlLoop.setVersion("0.0.1"); + controlLoop.setState(ControlLoopState.UNINITIALISED); + return controlLoop; + } + private ToscaConceptIdentifier getParticipantId() { return new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0"); } |