39db8c3e15acf3d128d62d05c9a9f4c7adc7fe8e
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.element.handler;
22
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;
28
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;
38
39 class MessageHandlerTest {
40
41     private static final String NAME = "name";
42     private static final String VERSION = "1.0.0";
43     private static final String TOPIC = "topic";
44
45     @Test
46     void testAppliesTo() {
47         var messageHandler = createMessageHandler(List.of());
48
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();
52     }
53
54     @Test
55     void testWrongConf() {
56         var starter = createMockElementService(ElementType.STARTER);
57         var bridge = createMockElementService(ElementType.BRIDGE);
58         var messageHandler = createMessageHandler(List.of(starter, bridge));
59
60         assertThatThrownBy(() -> messageHandler.active(null))
61                 .isInstanceOf(NullPointerException.class);
62         assertThatThrownBy(() -> messageHandler.update(null))
63                 .isInstanceOf(NullPointerException.class);
64
65         assertThatThrownBy(messageHandler::getActiveService)
66                 .isInstanceOf(AutomationCompositionRuntimeException.class);
67
68         var elementConfig = new ElementConfig();
69         elementConfig.setElementType(ElementType.STARTER);
70
71         assertThatThrownBy(() -> messageHandler.update(elementConfig))
72                 .isInstanceOf(AutomationCompositionRuntimeException.class);
73
74         messageHandler.active(elementConfig);
75
76         var newElementConfig = new ElementConfig();
77         newElementConfig.setElementType(ElementType.BRIDGE);
78
79         assertThatThrownBy(() -> messageHandler.update(newElementConfig))
80                 .isInstanceOf(AutomationCompositionRuntimeException.class);
81     }
82
83     @Test
84     void testStarter() {
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));
89
90         var elementConfig = new ElementConfig();
91         elementConfig.setElementType(ElementType.STARTER);
92
93         messageHandler.active(elementConfig);
94         verify(starter).active(elementConfig);
95         assertThat(messageHandler.getActiveService()).isEqualTo(starter);
96         messageHandler.deactivateElement();
97     }
98
99     @Test
100     void testBridge() {
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));
105
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);
111
112         messageHandler.update(elementConfig);
113         verify(bridge).update(elementConfig);
114
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();
121     }
122
123     @Test
124     void testSink() {
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));
129
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);
135
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();
142     }
143
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);
148     }
149
150     private ElementService createMockElementService(ElementType elementType) {
151         var starter = mock(ElementService.class);
152         when(starter.getType()).thenReturn(elementType);
153         return starter;
154     }
155 }