b0e05d70964aa273a6a5844331d52ed47fd90374
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-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.http.handler;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyMap;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.clamp.acm.participant.http.main.handler.AutomationCompositionElementHandler;
33 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
34 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
35 import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData;
36 import org.onap.policy.clamp.acm.participant.http.utils.ToscaUtils;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
38 import org.onap.policy.clamp.models.acm.concepts.DeployState;
39 import org.onap.policy.models.base.PfModelException;
40
41 class AcElementHandlerTest {
42
43     private final CommonTestData commonTestData = new CommonTestData();
44     private static final String HTTP_AUTOMATION_COMPOSITION_ELEMENT =
45             "org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement";
46
47     @Test
48     void testUndeploy() throws IOException {
49         var instanceId = commonTestData.getAutomationCompositionId();
50         var element = commonTestData.getAutomationCompositionElement();
51         var acElementId = element.getId();
52
53         try (var automationCompositionElementHandler =
54                 new AutomationCompositionElementHandler(mock(AcHttpClient.class))) {
55             var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
56             automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
57             automationCompositionElementHandler.undeploy(instanceId, acElementId);
58             verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
59                     DeployState.UNDEPLOYED, null, "");
60         }
61     }
62
63     @Test
64     void testDeployConstraintViolations() throws IOException, PfModelException {
65         var instanceId = commonTestData.getAutomationCompositionId();
66         var element = commonTestData.getAutomationCompositionElement();
67         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
68
69         try (var automationCompositionElementHandler =
70                 new AutomationCompositionElementHandler(mock(AcHttpClient.class))) {
71             automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
72             Map<String, Object> map = new HashMap<>();
73             automationCompositionElementHandler.deploy(instanceId, element, map);
74             verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
75                     DeployState.UNDEPLOYED, null, "Constraint violations in the config request");
76         }
77     }
78
79     @Test
80     void testDeployError() throws IOException, PfModelException {
81         var instanceId = commonTestData.getAutomationCompositionId();
82         var element = commonTestData.getAutomationCompositionElement();
83         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
84
85         try (var automationCompositionElementHandler =
86                 new AutomationCompositionElementHandler(mock(AcHttpClient.class))) {
87             automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
88             Map<String, Object> map = new HashMap<>();
89             map.put("httpHeaders", 1);
90             automationCompositionElementHandler.deploy(instanceId, element, map);
91             verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
92                     DeployState.UNDEPLOYED, null, "Error extracting ConfigRequest ");
93         }
94     }
95
96     @Test
97     void testDeploy() throws Exception {
98         var serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
99         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
100         var map = new HashMap<>(nodeTemplatesMap.get(HTTP_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
101         var element = commonTestData.getAutomationCompositionElement();
102         map.putAll(element.getProperties());
103         var instanceId = commonTestData.getAutomationCompositionId();
104         var acHttpClient = mock(AcHttpClient.class);
105         try (var automationCompositionElementHandler = new AutomationCompositionElementHandler(acHttpClient)) {
106             var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
107             automationCompositionElementHandler.setIntermediaryApi(participantIntermediaryApi);
108             automationCompositionElementHandler.deploy(instanceId, element, map);
109             verify(acHttpClient).run(any(ConfigRequest.class), anyMap());
110             verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
111                     DeployState.DEPLOYED, null, "Deployed");
112         }
113     }
114 }