77f41cb60241cfbbff72c0164e3d14c487cdd93a
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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.assertThrows;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
37 import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData;
38 import org.onap.policy.clamp.acm.participant.a1pms.utils.ToscaUtils;
39 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
40 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
41 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
42 import org.onap.policy.clamp.models.acm.concepts.DeployState;
43 import org.onap.policy.clamp.models.acm.concepts.LockState;
44 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47
48 class AcElementHandlerTest {
49
50     private final AcA1PmsClient acA1PmsClient = mock(AcA1PmsClient.class);
51
52     private final CommonTestData commonTestData = new CommonTestData();
53
54     private static ToscaServiceTemplate serviceTemplate;
55     private static final String A1_AUTOMATION_COMPOSITION_ELEMENT =
56             "org.onap.domain.database.A1PMSAutomationCompositionElement";
57
58     @BeforeAll
59     static void init() {
60         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
61     }
62
63     @BeforeEach
64     void startMocks() throws A1PolicyServiceException {
65         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.TRUE);
66         doNothing().when(acA1PmsClient).createService(any());
67     }
68
69     @Test
70     void test_automationCompositionElementStateChange() throws A1PolicyServiceException {
71         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
72         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
73         automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
74
75         var automationCompositionId = commonTestData.getAutomationCompositionId();
76         var element = commonTestData.getAutomationCompositionElement();
77         var automationCompositionElementId = element.getId();
78
79         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
80         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
81                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
82         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
83                 automationCompositionElementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
84
85         automationCompositionElementHandler.undeploy(automationCompositionId, automationCompositionElementId);
86         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
87                 automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
88
89         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
90         assertThrows(A1PolicyServiceException.class, () -> automationCompositionElementHandler
91                 .undeploy(automationCompositionId, automationCompositionElementId));
92     }
93
94     @Test
95     void test_AutomationCompositionElementUpdate() throws A1PolicyServiceException {
96         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
97         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
98         automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
99         var element = commonTestData.getAutomationCompositionElement();
100
101         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
102         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
103                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
104         verify(participantIntermediaryApi).updateAutomationCompositionElementState(
105                 commonTestData.getAutomationCompositionId(), element.getId(), DeployState.DEPLOYED, null,
106                 StateChangeResult.NO_ERROR, "Deployed");
107     }
108
109     @Test
110     void test_AutomationCompositionElementUpdateWithUnhealthyA1pms() {
111         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
112         automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class));
113         var element = commonTestData.getAutomationCompositionElement();
114         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
115
116         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
117         assertThrows(A1PolicyServiceException.class,
118                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
119                         nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
120     }
121
122     @Test
123     void test_AutomationCompositionElementUpdateWithInvalidConfiguration() {
124         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
125         automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class));
126         var element = commonTestData.getAutomationCompositionElement();
127         assertThrows(A1PolicyServiceException.class, () -> automationCompositionElementHandler
128                 .deploy(commonTestData.getAutomationCompositionId(), element, Map.of()));
129     }
130
131     @Test
132     void testLock() throws PfModelException {
133         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
134         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
135         automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
136         var automationCompositionId = UUID.randomUUID();
137         var elementId = UUID.randomUUID();
138         automationCompositionElementHandler.lock(automationCompositionId, elementId);
139
140         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
141                 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
142     }
143
144     @Test
145     void testUnlock() throws PfModelException {
146         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
147         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
148         automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
149         var automationCompositionId = UUID.randomUUID();
150         var elementId = UUID.randomUUID();
151         automationCompositionElementHandler.unlock(automationCompositionId, elementId);
152
153         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
154                 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
155     }
156
157     @Test
158     void testUpdate() throws PfModelException {
159         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
160         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
161         automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
162         var automationCompositionId = UUID.randomUUID();
163         var element = commonTestData.getAutomationCompositionElement();
164         automationCompositionElementHandler.update(automationCompositionId, element, Map.of());
165
166         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId,
167                 element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
168     }
169
170     @Test
171     void testDelete() throws PfModelException {
172         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
173         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
174         automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
175         var automationCompositionId = UUID.randomUUID();
176         var elementId = UUID.randomUUID();
177         automationCompositionElementHandler.delete(automationCompositionId, elementId);
178
179         verify(participantIntermediaryApi).updateAutomationCompositionElementState(automationCompositionId, elementId,
180                 DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
181     }
182
183     @Test
184     void testPrime() throws PfModelException {
185         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
186         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
187         automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
188         var compositionId = UUID.randomUUID();
189         automationCompositionElementHandler.prime(compositionId, List.of());
190
191         verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
192                 StateChangeResult.NO_ERROR, "Primed");
193     }
194
195     @Test
196     void testDeprime() throws PfModelException {
197         var automationCompositionElementHandler = new AutomationCompositionElementHandler(acA1PmsClient);
198         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
199         automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
200         var compositionId = UUID.randomUUID();
201         automationCompositionElementHandler.deprime(compositionId);
202
203         verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
204                 StateChangeResult.NO_ERROR, "Deprimed");
205     }
206 }