diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2022-08-17 09:52:46 +0100 |
---|---|---|
committer | FrancescoFioraEst <francesco.fiora@est.tech> | 2022-08-24 08:53:00 +0100 |
commit | d49e71949f5c5f2b5f70057c9ab6ded2c789fbf1 (patch) | |
tree | 7a431d8cb6e838bd2a2cc8e92501ba2e3db9bde5 /participant/participant-impl/participant-impl-acelement | |
parent | eafce4b03a77e729e1eb7fce7368e206b7fc9dce (diff) |
Create Unit Tests for Message Handler and Activator
Issue-ID: POLICY-4325
Change-Id: I1f91dec71974bd1169e3310d59057a6b96061db9
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'participant/participant-impl/participant-impl-acelement')
5 files changed, 356 insertions, 0 deletions
diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/handler/MessageActivatorTest.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/handler/MessageActivatorTest.java new file mode 100644 index 000000000..06af3f016 --- /dev/null +++ b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/handler/MessageActivatorTest.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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.clamp.acm.element.handler; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +import java.util.List; +import org.junit.jupiter.api.Test; +import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; +import org.onap.policy.common.endpoints.parameters.TopicParameters; + +class MessageActivatorTest { + + @Test + void test() throws Exception { + var listener = mock(MessageListener.class); + var publisher = mock(MessagePublisher.class); + try (var messageActivator = new MessageActivator(listener, publisher)) { + + var topicParameters = new TopicParameters(); + topicParameters.setTopic("topic"); + topicParameters.setServers(List.of("localhost")); + topicParameters.setFetchTimeout(1000); + topicParameters.setTopicCommInfrastructure("dmaap"); + + var parameters = new TopicParameterGroup(); + parameters.setTopicSinks(List.of(topicParameters)); + parameters.setTopicSources(List.of(topicParameters)); + + messageActivator.activate(parameters); + assertThat(messageActivator.isAlive()).isTrue(); + + messageActivator.deactivate(); + assertThat(messageActivator.isAlive()).isFalse(); + } + } + +} diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/handler/MessageHandlerTest.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/handler/MessageHandlerTest.java new file mode 100644 index 000000000..84c6305e2 --- /dev/null +++ b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/handler/MessageHandlerTest.java @@ -0,0 +1,123 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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.clamp.acm.element.handler; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.element.main.parameters.AcElement; +import org.onap.policy.clamp.acm.element.service.ElementService; +import org.onap.policy.clamp.models.acm.messages.dmaap.element.ElementStatus; +import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig; +import org.onap.policy.clamp.models.acm.messages.rest.element.ElementType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +class MessageHandlerTest { + + private static final String NAME = "name"; + private static final String VERSION = "1.0.0"; + + @Test + void testAppliesTo() { + var messageHandler = createMessageHandler(List.of()); + + assertThat(messageHandler.appliesTo(new ToscaConceptIdentifier(NAME, "0.0.2"))).isFalse(); + assertThat(messageHandler.appliesTo(new ToscaConceptIdentifier("different", VERSION))).isFalse(); + assertThat(messageHandler.appliesTo(new ToscaConceptIdentifier(NAME, VERSION))).isTrue(); + } + + @Test + void testStarter() { + var starter = createMockElementService(ElementType.STARTER); + var bridge = createMockElementService(ElementType.BRIDGE); + var sink = createMockElementService(ElementType.SINK); + var messageHandler = createMessageHandler(List.of(starter, bridge, sink)); + + var elementConfig = new ElementConfig(); + elementConfig.setElementType(ElementType.STARTER); + + messageHandler.active(elementConfig); + verify(starter).active(elementConfig); + assertThat(messageHandler.getActiveService()).isEqualTo(starter); + messageHandler.deactivateElement(); + } + + @Test + void testBridge() { + var starter = createMockElementService(ElementType.STARTER); + var bridge = createMockElementService(ElementType.BRIDGE); + var sink = createMockElementService(ElementType.SINK); + var messageHandler = createMessageHandler(List.of(starter, bridge, sink)); + + var elementConfig = new ElementConfig(); + elementConfig.setElementType(ElementType.BRIDGE); + messageHandler.active(elementConfig); + verify(bridge).active(elementConfig); + assertThat(messageHandler.getActiveService()).isEqualTo(bridge); + + messageHandler.update(elementConfig); + verify(bridge).update(elementConfig); + + var message = new ElementStatus(); + messageHandler.handleMessage(message); + verify(bridge).handleMessage(message); + assertThat(messageHandler.getMessages()).hasSize(1); + assertThat(messageHandler.getMessages().get(0)).isEqualTo(message); + messageHandler.deactivateElement(); + } + + @Test + void testSink() { + var starter = createMockElementService(ElementType.STARTER); + var bridge = createMockElementService(ElementType.BRIDGE); + var sink = createMockElementService(ElementType.SINK); + var messageHandler = createMessageHandler(List.of(starter, bridge, sink)); + + var elementConfig = new ElementConfig(); + elementConfig.setElementType(ElementType.SINK); + messageHandler.active(elementConfig); + verify(sink).active(elementConfig); + assertThat(messageHandler.getActiveService()).isEqualTo(sink); + + var message = new ElementStatus(); + messageHandler.handleMessage(message); + verify(sink).handleMessage(message); + assertThat(messageHandler.getMessages()).hasSize(1); + assertThat(messageHandler.getMessages().get(0)).isEqualTo(message); + messageHandler.deactivateElement(); + } + + private MessageHandler createMessageHandler(List<ElementService> elementServices) { + var acElement = new AcElement(); + acElement.setElementId(new ToscaConceptIdentifier(NAME, VERSION)); + return new MessageHandler(acElement, elementServices); + } + + private ElementService createMockElementService(ElementType elementType) { + var starter = mock(ElementService.class); + when(starter.getType()).thenReturn(elementType); + return starter; + } +} diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/BridgeServiceTest.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/BridgeServiceTest.java new file mode 100644 index 000000000..cc62d8d15 --- /dev/null +++ b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/BridgeServiceTest.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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.clamp.acm.element.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.element.handler.MessagePublisher; +import org.onap.policy.clamp.acm.element.main.parameters.AcElement; +import org.onap.policy.clamp.models.acm.messages.dmaap.element.ElementMessage; +import org.onap.policy.clamp.models.acm.messages.dmaap.element.ElementStatus; +import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig; +import org.onap.policy.clamp.models.acm.messages.rest.element.ElementType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +class BridgeServiceTest { + + @Test + void testHandleMessage() { + var acElement = new AcElement(); + acElement.setElementId(new ToscaConceptIdentifier("onap.policy.clamp.ac.element1", "1.0.0")); + + var messagePublisher = mock(MessagePublisher.class); + var bridgeService = new BridgeService(messagePublisher, acElement); + + assertThat(bridgeService.getType()).isEqualTo(ElementType.BRIDGE); + + var elementConfig = new ElementConfig(); + elementConfig.setElementId(new ToscaConceptIdentifier("onap.policy.clamp.ac.element2", "1.0.0")); + bridgeService.active(elementConfig); + + bridgeService.handleMessage(new ElementStatus()); + verify(messagePublisher).publishMsg(any(ElementMessage.class)); + } +} diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/ConfigServiceTest.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/ConfigServiceTest.java new file mode 100644 index 000000000..785673bae --- /dev/null +++ b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/ConfigServiceTest.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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.clamp.acm.element.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.element.handler.MessageActivator; +import org.onap.policy.clamp.acm.element.handler.MessageHandler; +import org.onap.policy.clamp.models.acm.messages.rest.element.DmaapConfig; +import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig; +import org.onap.policy.common.endpoints.parameters.TopicParameterGroup; + +class ConfigServiceTest { + + @Test + void test() { + var elementConfig = new ElementConfig(); + elementConfig.setTopicParameterGroup(new DmaapConfig()); + elementConfig.getTopicParameterGroup().setTopicCommInfrastructure("dmaap"); + elementConfig.getTopicParameterGroup().setTopic("topic"); + elementConfig.getTopicParameterGroup().setServer("localhost"); + elementConfig.getTopicParameterGroup().setFetchTimeout(1000); + + var handler = mock(MessageHandler.class); + var messageActivator = mock(MessageActivator.class); + var configService = new ConfigService(handler, messageActivator); + configService.activateElement(elementConfig); + + verify(messageActivator).activate(any(TopicParameterGroup.class)); + verify(handler).active(elementConfig); + + assertThat(configService.getElementConfig()).isEqualTo(elementConfig); + + configService.deleteConfig(); + verify(messageActivator).deactivate(); + verify(handler).deactivateElement(); + + assertThat(configService.getElementConfig()).isNotEqualTo(elementConfig); + } +} diff --git a/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/StarterServiceTest.java b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/StarterServiceTest.java new file mode 100644 index 000000000..ee58a352c --- /dev/null +++ b/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/service/StarterServiceTest.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * 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.clamp.acm.element.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.element.handler.MessagePublisher; +import org.onap.policy.clamp.acm.element.main.parameters.AcElement; +import org.onap.policy.clamp.models.acm.messages.dmaap.element.ElementMessage; +import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig; +import org.onap.policy.clamp.models.acm.messages.rest.element.ElementType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +class StarterServiceTest { + + @Test + void testActive() throws Exception { + var acElement = new AcElement(); + acElement.setElementId(new ToscaConceptIdentifier("onap.policy.clamp.ac.element1", "1.0.0")); + + var messagePublisher = mock(MessagePublisher.class); + try (var starterService = new StarterService(messagePublisher, acElement)) { + + assertThat(starterService.getType()).isEqualTo(ElementType.STARTER); + + var elementConfig = new ElementConfig(); + elementConfig.setTimerSec(100); + elementConfig.setElementId(new ToscaConceptIdentifier("onap.policy.clamp.ac.element2", "1.0.0")); + starterService.active(elementConfig); + verify(messagePublisher, timeout(200).atLeastOnce()).publishMsg(any(ElementMessage.class)); + starterService.deactivate(); + } + } + +} |