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.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
29 import jakarta.ws.rs.core.Response;
30 import java.util.List;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
35 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
36 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
37 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
38 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
48 class AutomationCompositionElementHandlerTest {
50 private static final ToscaConceptIdentifier DEFINITION =
51 new ToscaConceptIdentifier("1.0.1", "org.onap.PM_CDS_Blueprint");
54 void testHandlerUndeployNoPolicy() throws PfModelException {
55 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
56 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
57 mock(PolicyPapHttpClient.class), intermediaryApi);
59 var compositionElement = getCompositionElement();
60 var instanceElement = getInstanceElementWithNullTopology();
62 handler.undeploy(compositionElement, instanceElement);
63 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
64 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
68 private CompositionElementDto getCompositionElement() {
69 return new CompositionElementDto(UUID.randomUUID(), DEFINITION, Map.of(), Map.of());
72 private InstanceElementDto getInstanceElement() {
73 var template = new ToscaServiceTemplate();
74 template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
75 template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
76 template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
77 return new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), template, Map.of(), Map.of());
81 void testDeploy() throws PfModelException {
82 // Mock success scenario for policy creation and deployment
83 var api = mock(PolicyApiHttpClient.class);
84 doReturn(Response.ok().build()).when(api).createPolicyType(any());
85 doReturn(Response.ok().build()).when(api).createPolicy(any());
87 var pap = mock(PolicyPapHttpClient.class);
88 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
90 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
91 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
93 var compositionElement = getCompositionElement();
94 var instanceElement = getInstanceElement();
96 handler.deploy(compositionElement, instanceElement);
97 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
98 instanceElement.elementId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR,
101 handler.undeploy(compositionElement, instanceElement);
102 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
103 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
108 void testDeployNoPolicy() throws PfModelException {
109 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
110 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
111 mock(PolicyPapHttpClient.class), intermediaryApi);
113 var compositionElement = getCompositionElement();
114 var instanceElement = getInstanceElementWithNullTopology();
115 handler.deploy(compositionElement, instanceElement);
116 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
117 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
118 "ToscaTopologyTemplate not defined");
121 private InstanceElementDto getInstanceElementWithNullTopology() {
122 var template = new ToscaServiceTemplate();
123 template.setToscaTopologyTemplate(null);
124 return new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), template, Map.of(), Map.of());
128 void testApiException() throws PfModelException {
129 var api = mock(PolicyApiHttpClient.class);
130 doReturn(Response.serverError().build()).when(api).createPolicyType(any());
131 doReturn(Response.ok().build()).when(api).createPolicy(any());
133 var pap = mock(PolicyPapHttpClient.class);
134 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
136 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
137 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
139 var compositionElement = getCompositionElement();
140 var instanceElement = getInstanceElement();
142 // Mock failure in policy type creation
143 handler.deploy(compositionElement, instanceElement);
144 verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
145 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
146 "Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
150 void testDeployPapException() {
151 var api = mock(PolicyApiHttpClient.class);
152 doReturn(Response.ok().build()).when(api).createPolicyType(any());
153 doReturn(Response.ok().build()).when(api).createPolicy(any());
155 var pap = mock(PolicyPapHttpClient.class);
156 doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
158 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
159 var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
161 var compositionElement = getCompositionElement();
162 var instanceElement = getInstanceElement();
163 assertThatThrownBy(() -> handler.deploy(compositionElement, instanceElement))
164 .hasMessageMatching("Deploy of Policy failed.");