04cb3426938ad1a7d42b006a512a512f2d09db02
[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.acm.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 org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mockito;
35 import org.mockito.Spy;
36 import org.onap.policy.clamp.acm.participant.http.main.handler.AutomationCompositionElementHandler;
37 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
38 import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData;
39 import org.onap.policy.clamp.acm.participant.http.utils.ToscaUtils;
40 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
47
48 @ExtendWith(SpringExtension.class)
49 class AcElementHandlerTest {
50
51     @InjectMocks
52     @Spy
53     private AutomationCompositionElementHandler automationCompositionElementHandler =
54         new AutomationCompositionElementHandler();
55
56     private final CommonTestData commonTestData = new CommonTestData();
57
58     private static ToscaServiceTemplate serviceTemplate;
59     private static final String HTTP_AUTOMATION_COMPOSITION_ELEMENT =
60         "org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement";
61
62     @BeforeAll
63     static void init() {
64         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
65     }
66
67     @BeforeEach
68     void startMocks() {
69         automationCompositionElementHandler.setIntermediaryApi(Mockito.mock(ParticipantIntermediaryApi.class));
70     }
71
72     @Test
73     void test_automationCompositionElementStateChange() throws IOException {
74         var automationCompositionId = commonTestData.getAutomationCompositionId();
75         var element = commonTestData.getAutomationCompositionElement();
76         var automationCompositionElementId = element.getId();
77
78         var config = Mockito.mock(ConfigRequest.class);
79         assertDoesNotThrow(() -> automationCompositionElementHandler.invokeHttpClient(config));
80
81         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
82             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
83             AutomationCompositionOrderedState.PASSIVE));
84
85         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
86             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
87             AutomationCompositionOrderedState.UNINITIALISED));
88
89         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
90             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
91             AutomationCompositionOrderedState.RUNNING));
92
93         automationCompositionElementHandler.close();
94     }
95
96     @Test
97     void test_AutomationCompositionElementUpdate() throws Exception {
98         doNothing().when(automationCompositionElementHandler).invokeHttpClient(any());
99         AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
100
101         Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
102
103         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementUpdate(
104             commonTestData.getAutomationCompositionId(), element,
105             nodeTemplatesMap.get(HTTP_AUTOMATION_COMPOSITION_ELEMENT)));
106     }
107 }