635b774865c38429544a395966ea1fd86c408149
[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.participant.a1pms.handler;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Map;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Spy;
37 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
38 import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData;
39 import org.onap.policy.clamp.acm.participant.a1pms.utils.ToscaUtils;
40 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
41 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
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     private final AcA1PmsClient acA1PmsClient = mock(AcA1PmsClient.class);
53
54     @InjectMocks
55     @Spy
56     private AutomationCompositionElementHandler automationCompositionElementHandler =
57         new AutomationCompositionElementHandler(acA1PmsClient);
58
59
60     private final CommonTestData commonTestData = new CommonTestData();
61
62     private static ToscaServiceTemplate serviceTemplate;
63     private static final String A1_AUTOMATION_COMPOSITION_ELEMENT =
64         "org.onap.domain.database.A1PMSAutomationCompositionElement";
65
66     @BeforeAll
67     static void init() {
68         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
69     }
70
71     @BeforeEach
72     void startMocks() throws A1PolicyServiceException {
73         automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class));
74         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.TRUE);
75         doNothing().when(acA1PmsClient).createService(any());
76     }
77
78     @Test
79     void test_automationCompositionElementStateChange() throws A1PolicyServiceException {
80         var automationCompositionId = commonTestData.getAutomationCompositionId();
81         var element = commonTestData.getAutomationCompositionElement();
82         var automationCompositionElementId = element.getId();
83
84         Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
85         automationCompositionElementHandler.automationCompositionElementUpdate(
86                 commonTestData.getAutomationCompositionId(), element,
87                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT));
88
89         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
90             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
91             AutomationCompositionOrderedState.PASSIVE));
92
93         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
94             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
95             AutomationCompositionOrderedState.UNINITIALISED));
96
97         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
98             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
99             AutomationCompositionOrderedState.RUNNING));
100
101         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
102         assertThrows(A1PolicyServiceException.class,
103                 () -> automationCompositionElementHandler.automationCompositionElementStateChange(
104                         automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
105                         AutomationCompositionOrderedState.UNINITIALISED));
106     }
107
108     @Test
109     void test_AutomationCompositionElementUpdate() {
110         AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
111
112         Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
113         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementUpdate(
114             commonTestData.getAutomationCompositionId(), element,
115             nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT)));
116     }
117
118     @Test
119     void test_AutomationCompositionElementUpdateWithUnhealthyA1pms() {
120         AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
121         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
122
123         Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
124         assertThrows(A1PolicyServiceException.class,
125                 () -> automationCompositionElementHandler.automationCompositionElementUpdate(
126                         commonTestData.getAutomationCompositionId(), element,
127                         nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT)));
128     }
129
130     @Test
131     void test_AutomationCompositionElementUpdateWithInvalidConfiguration() {
132         AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
133
134         Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
135         ToscaNodeTemplate toscaNodeTemplate = nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT);
136         toscaNodeTemplate.setProperties(Map.of());
137         assertThrows(A1PolicyServiceException.class,
138                 () -> automationCompositionElementHandler.automationCompositionElementUpdate(
139                         commonTestData.getAutomationCompositionId(), element, toscaNodeTemplate));
140     }
141 }