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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.policy.main.handler;
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;
30 import jakarta.ws.rs.core.Response;
31 import java.util.List;
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;
52 class AutomationCompositionElementHandlerTest {
54 private static final Coder CODER = new StandardCoder();
56 private static final ToscaConceptIdentifier DEFINITION =
57 new ToscaConceptIdentifier("1.0.1", "org.onap.PM_CDS_Blueprint");
60 void testHandlerUndeployNoPolicy() throws PfModelException {
61 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
62 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
63 mock(PolicyPapHttpClient.class), intermediaryApi);
65 var compositionElement = getCompositionElement();
66 var instanceElement = getInstanceElementWithNullTopology();
68 handler.undeploy(compositionElement, instanceElement);
69 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
70 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
74 private CompositionElementDto getCompositionElement() {
75 return new CompositionElementDto(UUID.randomUUID(), DEFINITION, Map.of(), Map.of());
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());
87 private Map<String, Object> getProperties(ToscaServiceTemplate template) {
89 var json = CODER.encode(template);
90 return CODER.decode(json, Map.class);
91 } catch (CoderException e) {
92 throw new RuntimeException(e);
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());
103 var pap = mock(PolicyPapHttpClient.class);
104 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
106 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
107 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
109 var compositionElement = getCompositionElement();
110 var instanceElement = getInstanceElement();
112 handler.deploy(compositionElement, instanceElement);
113 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
114 instanceElement.elementId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR,
117 clearInvocations(intermediaryApi);
118 handler.undeploy(compositionElement, instanceElement);
119 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
120 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
125 void testDeployNoPolicy() throws PfModelException {
126 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
127 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
128 mock(PolicyPapHttpClient.class), intermediaryApi);
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");
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,
144 clearInvocations(intermediaryApi);
145 handler.undeploy(compositionElement, instanceElement);
146 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
147 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
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());
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());
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());
171 var pap = mock(PolicyPapHttpClient.class);
172 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
174 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
175 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
177 var compositionElement = getCompositionElement();
178 var instanceElement = getInstanceElement();
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.");
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());
193 var pap = mock(PolicyPapHttpClient.class);
194 doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
196 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
197 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
199 var compositionElement = getCompositionElement();
200 var instanceElement = getInstanceElement();
201 assertThatThrownBy(() -> handler.deploy(compositionElement, instanceElement))
202 .hasMessageMatching("Deploy of Policy failed.");