39f35e6df2224cb679b562a9d2f92ae1cb2ce9fd
[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.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28
29 import jakarta.ws.rs.core.Response;
30 import java.util.List;
31 import java.util.Map;
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;
47
48 class AutomationCompositionElementHandlerTest {
49
50     private static final ToscaConceptIdentifier DEFINITION =
51             new ToscaConceptIdentifier("1.0.1", "org.onap.PM_CDS_Blueprint");
52
53     @Test
54     void testHandlerUndeployNoPolicy() throws PfModelException {
55         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
56         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
57                 mock(PolicyPapHttpClient.class), intermediaryApi);
58
59         var compositionElement = getCompositionElement();
60         var instanceElement = getInstanceElementWithNullTopology();
61
62         handler.undeploy(compositionElement, instanceElement);
63         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
64                 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
65                 "Undeployed");
66     }
67
68     private CompositionElementDto getCompositionElement() {
69         return new CompositionElementDto(UUID.randomUUID(), DEFINITION, Map.of(), Map.of());
70     }
71
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());
78     }
79
80     @Test
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());
86
87         var pap = mock(PolicyPapHttpClient.class);
88         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
89
90         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
91         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
92
93         var compositionElement = getCompositionElement();
94         var instanceElement = getInstanceElement();
95
96         handler.deploy(compositionElement, instanceElement);
97         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
98                 instanceElement.elementId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR,
99                 "Deployed");
100
101         handler.undeploy(compositionElement, instanceElement);
102         verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
103                 instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
104                 "Undeployed");
105     }
106
107     @Test
108     void testDeployNoPolicy() throws PfModelException {
109         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
110         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
111                 mock(PolicyPapHttpClient.class), intermediaryApi);
112
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");
119     }
120
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());
125     }
126
127     @Test
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());
132
133         var pap = mock(PolicyPapHttpClient.class);
134         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
135
136         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
137         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
138
139         var compositionElement = getCompositionElement();
140         var instanceElement = getInstanceElement();
141
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.");
147     }
148
149     @Test
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());
154
155         var pap = mock(PolicyPapHttpClient.class);
156         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
157
158         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
159         var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
160
161         var compositionElement = getCompositionElement();
162         var instanceElement = getInstanceElement();
163         assertThatThrownBy(() -> handler.deploy(compositionElement, instanceElement))
164                 .hasMessageMatching("Deploy of Policy failed.");
165     }
166 }