08f008ef385269c2e2794291f18c1b4c2d8fe425
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-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.controlloop.participant.http.handler;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doNothing;
26
27 import java.io.IOException;
28 import java.util.Map;
29 import java.util.concurrent.ExecutionException;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.Spy;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
40 import org.onap.policy.clamp.controlloop.participant.http.main.handler.ControlLoopElementHandler;
41 import org.onap.policy.clamp.controlloop.participant.http.main.models.ConfigRequest;
42 import org.onap.policy.clamp.controlloop.participant.http.utils.CommonTestData;
43 import org.onap.policy.clamp.controlloop.participant.http.utils.ToscaUtils;
44 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.springframework.test.context.junit.jupiter.SpringExtension;
49
50
51 @ExtendWith(SpringExtension.class)
52 class ClElementHandlerTest {
53
54     @InjectMocks
55     @Spy
56     private ControlLoopElementHandler controlLoopElementHandler = new ControlLoopElementHandler();
57
58     @Mock
59     private ParticipantIntermediaryApi participantIntermediaryApi;
60
61     private CommonTestData commonTestData = new CommonTestData();
62
63     private static ToscaServiceTemplate serviceTemplate;
64     private static final String HTTP_CONTROL_LOOP_ELEMENT =
65             "org.onap.domain.database.Http_PMSHMicroserviceControlLoopElement";
66
67     @BeforeAll
68     static void init() throws CoderException {
69         serviceTemplate = ToscaUtils.readControlLoopFromTosca();
70     }
71
72     @Test
73     void test_controlLoopElementeStateChange() throws IOException {
74         var controlLoopId = commonTestData.getControlLoopId();
75         var element = commonTestData.getControlLoopElement();
76         var controlLoopElementId = element.getId();
77
78         var config = Mockito.mock(ConfigRequest.class);
79         assertDoesNotThrow(() -> controlLoopElementHandler.invokeHttpClient(config));
80
81         assertDoesNotThrow(() -> controlLoopElementHandler
82                 .controlLoopElementStateChange(controlLoopId,
83                         controlLoopElementId, ControlLoopState.PASSIVE, ControlLoopOrderedState.PASSIVE));
84
85         assertDoesNotThrow(() -> controlLoopElementHandler
86                 .controlLoopElementStateChange(controlLoopId,
87                         controlLoopElementId, ControlLoopState.PASSIVE, ControlLoopOrderedState.UNINITIALISED));
88
89         assertDoesNotThrow(() -> controlLoopElementHandler
90                 .controlLoopElementStateChange(controlLoopId,
91                         controlLoopElementId, ControlLoopState.PASSIVE, ControlLoopOrderedState.RUNNING));
92
93         controlLoopElementHandler.close();
94     }
95
96     @Test
97     void test_ControlLoopElementUpdate() throws ExecutionException, InterruptedException {
98         doNothing().when(controlLoopElementHandler).invokeHttpClient(any());
99         ControlLoopElement element = commonTestData.getControlLoopElement();
100
101         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
102             serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
103
104         assertDoesNotThrow(() -> controlLoopElementHandler
105             .controlLoopElementUpdate(commonTestData.getControlLoopId(), element,
106                 nodeTemplatesMap.get(HTTP_CONTROL_LOOP_ELEMENT)));
107     }
108 }