2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022,2024 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.element.handler;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import java.util.List;
30 import org.junit.jupiter.api.Test;
31 import org.onap.policy.clamp.acm.element.main.parameters.AcElement;
32 import org.onap.policy.clamp.acm.element.service.ElementService;
33 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
34 import org.onap.policy.clamp.models.acm.messages.kafka.element.ElementStatus;
35 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
36 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementType;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 class MessageHandlerTest {
41 private static final String NAME = "name";
42 private static final String VERSION = "1.0.0";
43 private static final String TOPIC = "topic";
46 void testAppliesTo() {
47 var messageHandler = createMessageHandler(List.of());
49 assertThat(messageHandler.appliesTo(new ToscaConceptIdentifier(NAME, "0.0.2"))).isFalse();
50 assertThat(messageHandler.appliesTo(new ToscaConceptIdentifier("different", VERSION))).isFalse();
51 assertThat(messageHandler.appliesTo(new ToscaConceptIdentifier(NAME, VERSION))).isTrue();
55 void testWrongConf() {
56 var starter = createMockElementService(ElementType.STARTER);
57 var bridge = createMockElementService(ElementType.BRIDGE);
58 var messageHandler = createMessageHandler(List.of(starter, bridge));
60 assertThatThrownBy(() -> messageHandler.active(null))
61 .isInstanceOf(NullPointerException.class);
62 assertThatThrownBy(() -> messageHandler.update(null))
63 .isInstanceOf(NullPointerException.class);
65 assertThatThrownBy(messageHandler::getActiveService)
66 .isInstanceOf(AutomationCompositionRuntimeException.class);
68 var elementConfig = new ElementConfig();
69 elementConfig.setElementType(ElementType.STARTER);
71 assertThatThrownBy(() -> messageHandler.update(elementConfig))
72 .isInstanceOf(AutomationCompositionRuntimeException.class);
74 messageHandler.active(elementConfig);
76 var newElementConfig = new ElementConfig();
77 newElementConfig.setElementType(ElementType.BRIDGE);
79 assertThatThrownBy(() -> messageHandler.update(newElementConfig))
80 .isInstanceOf(AutomationCompositionRuntimeException.class);
85 var starter = createMockElementService(ElementType.STARTER);
86 var bridge = createMockElementService(ElementType.BRIDGE);
87 var sink = createMockElementService(ElementType.SINK);
88 var messageHandler = createMessageHandler(List.of(starter, bridge, sink));
90 var elementConfig = new ElementConfig();
91 elementConfig.setElementType(ElementType.STARTER);
93 messageHandler.active(elementConfig);
94 verify(starter).active(elementConfig);
95 assertThat(messageHandler.getActiveService()).isEqualTo(starter);
96 messageHandler.deactivateElement();
101 var starter = createMockElementService(ElementType.STARTER);
102 var bridge = createMockElementService(ElementType.BRIDGE);
103 var sink = createMockElementService(ElementType.SINK);
104 var messageHandler = createMessageHandler(List.of(starter, bridge, sink));
106 var elementConfig = new ElementConfig();
107 elementConfig.setElementType(ElementType.BRIDGE);
108 messageHandler.active(elementConfig);
109 verify(bridge).active(elementConfig);
110 assertThat(messageHandler.getActiveService()).isEqualTo(bridge);
112 messageHandler.update(elementConfig);
113 verify(bridge).update(elementConfig);
115 var message = new ElementStatus();
116 message.setElementId(new ToscaConceptIdentifier(NAME, VERSION));
117 var listener = new MessageListener(messageHandler);
118 listener.onTopicEvent(null, TOPIC, null, message);
119 verify(bridge).handleMessage(message);
120 messageHandler.deactivateElement();
125 var starter = createMockElementService(ElementType.STARTER);
126 var bridge = createMockElementService(ElementType.BRIDGE);
127 var sink = createMockElementService(ElementType.SINK);
128 var messageHandler = createMessageHandler(List.of(starter, bridge, sink));
130 var elementConfig = new ElementConfig();
131 elementConfig.setElementType(ElementType.SINK);
132 messageHandler.active(elementConfig);
133 verify(sink).active(elementConfig);
134 assertThat(messageHandler.getActiveService()).isEqualTo(sink);
136 var message = new ElementStatus();
137 message.setElementId(new ToscaConceptIdentifier(NAME, VERSION));
138 var listener = new MessageListener(messageHandler);
139 listener.onTopicEvent(null, TOPIC, null, message);
140 verify(sink).handleMessage(message);
141 messageHandler.deactivateElement();
144 private MessageHandler createMessageHandler(List<ElementService> elementServices) {
145 var acElement = new AcElement();
146 acElement.setElementId(new ToscaConceptIdentifier(NAME, VERSION));
147 return new MessageHandler(acElement, elementServices);
150 private ElementService createMockElementService(ElementType elementType) {
151 var starter = mock(ElementService.class);
152 when(starter.getType()).thenReturn(elementType);