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