ce775b616bd74e3a5137b91181179b4b8afdcaff
[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.AutomationCompositionOrderedState;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.springframework.test.context.junit.jupiter.SpringExtension;
46
47 @ExtendWith(SpringExtension.class)
48 class AcElementHandlerTest {
49
50     private final AcA1PmsClient acA1PmsClient = mock(AcA1PmsClient.class);
51
52     @InjectMocks
53     @Spy
54     private AutomationCompositionElementHandler automationCompositionElementHandler =
55             new AutomationCompositionElementHandler(acA1PmsClient);
56
57     private final CommonTestData commonTestData = new CommonTestData();
58
59     private static ToscaServiceTemplate serviceTemplate;
60     private static final String A1_AUTOMATION_COMPOSITION_ELEMENT =
61             "org.onap.domain.database.A1PMSAutomationCompositionElement";
62
63     @BeforeAll
64     static void init() {
65         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
66     }
67
68     @BeforeEach
69     void startMocks() throws A1PolicyServiceException {
70         automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class));
71         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.TRUE);
72         doNothing().when(acA1PmsClient).createService(any());
73     }
74
75     @Test
76     void test_automationCompositionElementStateChange() throws A1PolicyServiceException {
77         var automationCompositionId = commonTestData.getAutomationCompositionId();
78         var element = commonTestData.getAutomationCompositionElement();
79         var automationCompositionElementId = element.getId();
80
81         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
82         automationCompositionElementHandler
83                 .automationCompositionElementUpdate(commonTestData.getAutomationCompositionId(), element,
84                         nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
85
86         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
87                 automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
88                 AutomationCompositionOrderedState.PASSIVE));
89
90         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
91                 automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
92                 AutomationCompositionOrderedState.UNINITIALISED));
93
94         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
95                 automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
96                 AutomationCompositionOrderedState.RUNNING));
97
98         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
99         assertThrows(A1PolicyServiceException.class,
100                 () -> automationCompositionElementHandler.automationCompositionElementStateChange(
101                         automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
102                         AutomationCompositionOrderedState.UNINITIALISED));
103     }
104
105     @Test
106     void test_AutomationCompositionElementUpdate() {
107         var element = commonTestData.getAutomationCompositionElement();
108
109         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
110         assertDoesNotThrow(() -> automationCompositionElementHandler
111                 .automationCompositionElementUpdate(commonTestData.getAutomationCompositionId(), element,
112                         nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
113     }
114
115     @Test
116     void test_AutomationCompositionElementUpdateWithUnhealthyA1pms() {
117         var element = commonTestData.getAutomationCompositionElement();
118         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
119
120         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
121         assertThrows(A1PolicyServiceException.class, () -> automationCompositionElementHandler
122                 .automationCompositionElementUpdate(commonTestData.getAutomationCompositionId(), element,
123                         nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
124     }
125
126     @Test
127     void test_AutomationCompositionElementUpdateWithInvalidConfiguration() {
128         var element = commonTestData.getAutomationCompositionElement();
129         assertThrows(A1PolicyServiceException.class, () -> automationCompositionElementHandler
130                 .automationCompositionElementUpdate(commonTestData.getAutomationCompositionId(), element,
131                         Map.of()));
132     }
133 }