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.getActiveService())
61 .isInstanceOf(AutomationCompositionRuntimeException.class);
63 var elementConfig = new ElementConfig();
64 elementConfig.setElementType(ElementType.STARTER);
66 assertThatThrownBy(() -> messageHandler.update(elementConfig))
67 .isInstanceOf(AutomationCompositionRuntimeException.class);
69 messageHandler.active(elementConfig);
71 var newElementConfig = new ElementConfig();
72 newElementConfig.setElementType(ElementType.BRIDGE);
74 assertThatThrownBy(() -> messageHandler.update(newElementConfig))
75 .isInstanceOf(AutomationCompositionRuntimeException.class);
80 var starter = createMockElementService(ElementType.STARTER);
81 var bridge = createMockElementService(ElementType.BRIDGE);
82 var sink = createMockElementService(ElementType.SINK);
83 var messageHandler = createMessageHandler(List.of(starter, bridge, sink));
85 var elementConfig = new ElementConfig();
86 elementConfig.setElementType(ElementType.STARTER);
88 messageHandler.active(elementConfig);
89 verify(starter).active(elementConfig);
90 assertThat(messageHandler.getActiveService()).isEqualTo(starter);
91 messageHandler.deactivateElement();
96 var starter = createMockElementService(ElementType.STARTER);
97 var bridge = createMockElementService(ElementType.BRIDGE);
98 var sink = createMockElementService(ElementType.SINK);
99 var messageHandler = createMessageHandler(List.of(starter, bridge, sink));
101 var elementConfig = new ElementConfig();
102 elementConfig.setElementType(ElementType.BRIDGE);
103 messageHandler.active(elementConfig);
104 verify(bridge).active(elementConfig);
105 assertThat(messageHandler.getActiveService()).isEqualTo(bridge);
107 messageHandler.update(elementConfig);
108 verify(bridge).update(elementConfig);
110 var message = new ElementStatus();
111 message.setElementId(new ToscaConceptIdentifier(NAME, VERSION));
112 var listener = new MessageListener(messageHandler);
113 listener.onTopicEvent(null, TOPIC, null, message);
114 verify(bridge).handleMessage(message);
115 messageHandler.deactivateElement();
120 var starter = createMockElementService(ElementType.STARTER);
121 var bridge = createMockElementService(ElementType.BRIDGE);
122 var sink = createMockElementService(ElementType.SINK);
123 var messageHandler = createMessageHandler(List.of(starter, bridge, sink));
125 var elementConfig = new ElementConfig();
126 elementConfig.setElementType(ElementType.SINK);
127 messageHandler.active(elementConfig);
128 verify(sink).active(elementConfig);
129 assertThat(messageHandler.getActiveService()).isEqualTo(sink);
131 var message = new ElementStatus();
132 message.setElementId(new ToscaConceptIdentifier(NAME, VERSION));
133 var listener = new MessageListener(messageHandler);
134 listener.onTopicEvent(null, TOPIC, null, message);
135 verify(sink).handleMessage(message);
136 messageHandler.deactivateElement();
139 private MessageHandler createMessageHandler(List<ElementService> elementServices) {
140 var acElement = new AcElement();
141 acElement.setElementId(new ToscaConceptIdentifier(NAME, VERSION));
142 return new MessageHandler(acElement, elementServices);
145 private ElementService createMockElementService(ElementType elementType) {
146 var starter = mock(ElementService.class);
147 when(starter.getType()).thenReturn(elementType);