fbb68944880fa26f408ba2428bd27c97d291b09b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
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.common.utils.coder.CoderException;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.springframework.test.context.junit.jupiter.SpringExtension;
48
49 @ExtendWith(SpringExtension.class)
50 class AcElementHandlerTest {
51
52     @InjectMocks
53     @Spy
54     private AutomationCompositionElementHandler automationCompositionElementHandler =
55         new AutomationCompositionElementHandler();
56
57     @Mock
58     private ParticipantIntermediaryApi participantIntermediaryApi;
59
60     private CommonTestData commonTestData = new CommonTestData();
61
62     private static ToscaServiceTemplate serviceTemplate;
63     private static final String HTTP_AUTOMATION_COMPOSITION_ELEMENT =
64         "org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement";
65
66     @BeforeAll
67     static void init() throws CoderException {
68         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
69     }
70
71     @Test
72     void test_automationCompositionElementeStateChange() throws IOException {
73         var automationCompositionId = commonTestData.getAutomationCompositionId();
74         var element = commonTestData.getAutomationCompositionElement();
75         var automationCompositionElementId = element.getId();
76
77         var config = Mockito.mock(ConfigRequest.class);
78         assertDoesNotThrow(() -> automationCompositionElementHandler.invokeHttpClient(config));
79
80         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
81             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
82             AutomationCompositionOrderedState.PASSIVE));
83
84         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
85             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
86             AutomationCompositionOrderedState.UNINITIALISED));
87
88         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
89             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
90             AutomationCompositionOrderedState.RUNNING));
91
92         automationCompositionElementHandler.close();
93     }
94
95     @Test
96     void test_AutomationCompositionElementUpdate() throws Exception {
97         doNothing().when(automationCompositionElementHandler).invokeHttpClient(any());
98         AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
99
100         Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
101
102         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementUpdate(
103             commonTestData.getAutomationCompositionId(), element,
104             nodeTemplatesMap.get(HTTP_AUTOMATION_COMPOSITION_ELEMENT)));
105     }
106 }