cc8bb0ac1b9fb870c30d0229655cb6572edad7db
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 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.policy.main.handler;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.clearInvocations;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29
30 import jakarta.ws.rs.core.Response;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
36 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
38 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
39 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
42 import org.onap.policy.common.utils.coder.Coder;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
51
52 class AutomationCompositionElementHandlerTest {
53
54     private static final Coder CODER = new StandardCoder();
55
56     private static final ToscaConceptIdentifier DEFINITION =
57             new ToscaConceptIdentifier("1.0.1", "org.onap.PM_CDS_Blueprint");
58
59     @Test
60     void testHandlerUndeployNoPolicy() throws PfModelException {
61         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
62         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
63                 mock(PolicyPapHttpClient.class), intermediaryApi);
64
65         var compositionElement = getCompositionElement();
66         var instanceElement = getInstanceElementWithNullTopology();
67
68         handler.undeploy(compositionElement, instanceElement);
69         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
70                 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
71                 "Undeployed");
72     }
73
74     private CompositionElementDto getCompositionElement() {
75         return new CompositionElementDto(UUID.randomUUID(), DEFINITION, Map.of(), Map.of());
76     }
77
78     private InstanceElementDto getInstanceElement() {
79         var template = new ToscaServiceTemplate();
80         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
81         template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
82         template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
83         var inProperties = getProperties(template);
84         return new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), inProperties, Map.of());
85     }
86
87     private Map<String, Object> getProperties(ToscaServiceTemplate template) {
88         try {
89             var json = CODER.encode(template);
90             return CODER.decode(json, Map.class);
91         } catch (CoderException e) {
92             throw new RuntimeException(e);
93         }
94     }
95
96     @Test
97     void testDeploy() throws PfModelException {
98         // Mock success scenario for policy creation and deployment
99         var api = mock(PolicyApiHttpClient.class);
100         doReturn(Response.ok().build()).when(api).createPolicyType(any());
101         doReturn(Response.ok().build()).when(api).createPolicy(any());
102
103         var pap = mock(PolicyPapHttpClient.class);
104         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
105
106         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
107         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
108
109         var compositionElement = getCompositionElement();
110         var instanceElement = getInstanceElement();
111
112         handler.deploy(compositionElement, instanceElement);
113         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
114                 instanceElement.elementId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR,
115                 "Deployed");
116
117         clearInvocations(intermediaryApi);
118         handler.undeploy(compositionElement, instanceElement);
119         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
120                 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
121                 "Undeployed");
122     }
123
124     @Test
125     void testDeployNoPolicy() throws PfModelException {
126         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
127         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
128                 mock(PolicyPapHttpClient.class), intermediaryApi);
129
130         var compositionElement = getCompositionElement();
131         var instanceElement = getInstanceElementWithNullTopology();
132         handler.deploy(compositionElement, instanceElement);
133         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
134                 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
135                 "ToscaTopologyTemplate not defined");
136
137         clearInvocations(intermediaryApi);
138         instanceElement = getInstanceElementWithNoPolicy();
139         handler.deploy(compositionElement, instanceElement);
140         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
141                 instanceElement.elementId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR,
142                 "Deployed");
143
144         clearInvocations(intermediaryApi);
145         handler.undeploy(compositionElement, instanceElement);
146         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
147                 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
148                 "Undeployed");
149     }
150
151     private InstanceElementDto getInstanceElementWithNullTopology() {
152         var template = new ToscaServiceTemplate();
153         template.setToscaTopologyTemplate(null);
154         var inProperties = getProperties(template);
155         return new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), inProperties, Map.of());
156     }
157
158     private InstanceElementDto getInstanceElementWithNoPolicy() {
159         var template = new ToscaServiceTemplate();
160         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
161         var inProperties = getProperties(template);
162         return new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), inProperties, Map.of());
163     }
164
165     @Test
166     void testApiException() throws PfModelException {
167         var api = mock(PolicyApiHttpClient.class);
168         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
169         doReturn(Response.ok().build()).when(api).createPolicy(any());
170
171         var pap = mock(PolicyPapHttpClient.class);
172         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
173
174         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
175         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
176
177         var compositionElement = getCompositionElement();
178         var instanceElement = getInstanceElement();
179
180         // Mock failure in policy type creation
181         handler.deploy(compositionElement, instanceElement);
182         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
183                 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
184                 "Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
185     }
186
187     @Test
188     void testDeployPapException() {
189         var api = mock(PolicyApiHttpClient.class);
190         doReturn(Response.ok().build()).when(api).createPolicyType(any());
191         doReturn(Response.ok().build()).when(api).createPolicy(any());
192
193         var pap = mock(PolicyPapHttpClient.class);
194         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
195
196         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
197         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
198
199         var compositionElement = getCompositionElement();
200         var instanceElement = getInstanceElement();
201         assertThatThrownBy(() -> handler.deploy(compositionElement, instanceElement))
202                 .hasMessageMatching("Deploy of Policy failed.");
203     }
204 }