dd651decc8da7eea4558238d553c6d5ecd50b3ee
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 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.dmaap.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.getActiveService())
61                 .isInstanceOf(AutomationCompositionRuntimeException.class);
62
63         var elementConfig = new ElementConfig();
64         elementConfig.setElementType(ElementType.STARTER);
65
66         assertThatThrownBy(() -> messageHandler.update(elementConfig))
67                 .isInstanceOf(AutomationCompositionRuntimeException.class);
68
69         messageHandler.active(elementConfig);
70
71         var newElementConfig = new ElementConfig();
72         newElementConfig.setElementType(ElementType.BRIDGE);
73
74         assertThatThrownBy(() -> messageHandler.update(newElementConfig))
75                 .isInstanceOf(AutomationCompositionRuntimeException.class);
76     }
77
78     @Test
79     void testStarter() {
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));
84
85         var elementConfig = new ElementConfig();
86         elementConfig.setElementType(ElementType.STARTER);
87
88         messageHandler.active(elementConfig);
89         verify(starter).active(elementConfig);
90         assertThat(messageHandler.getActiveService()).isEqualTo(starter);
91         messageHandler.deactivateElement();
92     }
93
94     @Test
95     void testBridge() {
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));
100
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);
106
107         messageHandler.update(elementConfig);
108         verify(bridge).update(elementConfig);
109
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();
116     }
117
118     @Test
119     void testSink() {
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));
124
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);
130
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();
137     }
138
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);
143     }
144
145     private ElementService createMockElementService(ElementType elementType) {
146         var starter = mock(ElementService.class);
147         when(starter.getType()).thenReturn(elementType);
148         return starter;
149     }
150 }