4cf5a58471c763aeef73a48d1e7a85563aa70543
[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.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 java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import javax.ws.rs.core.Response;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
35 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
36 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
37 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
38 import org.onap.policy.clamp.models.acm.concepts.DeployState;
39 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
46
47 class AutomationCompositionElementHandlerTest {
48
49     private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
50     private static final String ID_VERSION = "1.0.1";
51     private static final UUID automationCompositionElementId = UUID.randomUUID();
52     public static final UUID AC_ID = UUID.randomUUID();
53     private static final ToscaConceptIdentifier DEFINITION = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
54
55     @Test
56     void testHandlerUndeployNoPolicy() throws PfModelException {
57         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
58                 mock(PolicyPapHttpClient.class));
59         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
60         handler.setIntermediaryApi(intermediaryApi);
61
62         handler.undeploy(AC_ID, automationCompositionElementId);
63         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
64                 DeployState.UNDEPLOYED, null, "Undeployed");
65     }
66
67     private AcElementDeploy getTestingAcElement() {
68         var element = new AcElementDeploy();
69         element.setDefinition(DEFINITION);
70         element.setId(automationCompositionElementId);
71         element.setOrderedState(DeployOrder.DEPLOY);
72         var template = new ToscaServiceTemplate();
73         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
74         template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
75         template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
76         element.setToscaServiceTemplateFragment(template);
77         return element;
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 handler = new AutomationCompositionElementHandler(api, pap);
91         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
92         handler.setIntermediaryApi(intermediaryApi);
93
94         handler.deploy(AC_ID, getTestingAcElement(), Map.of());
95         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
96                 DeployState.DEPLOYED, null, "Deployed");
97
98         handler.undeploy(AC_ID, automationCompositionElementId);
99         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
100                 DeployState.UNDEPLOYED, null, "Undeployed");
101     }
102
103     @Test
104     void testDeployNoPolicy() throws PfModelException {
105         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
106                 mock(PolicyPapHttpClient.class));
107         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
108         handler.setIntermediaryApi(intermediaryApi);
109
110         var acElement = getTestingAcElement();
111         acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
112         handler.deploy(AC_ID, acElement, Map.of());
113         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
114                 DeployState.UNDEPLOYED, null, "ToscaTopologyTemplate not defined");
115     }
116
117     @Test
118     void testApiException() throws PfModelException {
119         var api = mock(PolicyApiHttpClient.class);
120         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
121         doReturn(Response.ok().build()).when(api).createPolicy(any());
122
123         var pap = mock(PolicyPapHttpClient.class);
124         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
125
126         var handler = new AutomationCompositionElementHandler(api, pap);
127         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
128         handler.setIntermediaryApi(intermediaryApi);
129         var element = getTestingAcElement();
130
131         // Mock failure in policy type creation
132         handler.deploy(AC_ID, element, Map.of());
133         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
134                 DeployState.UNDEPLOYED, null,
135                 "Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
136     }
137
138     @Test
139     void testDeployPapException() throws PfModelException {
140         var api = mock(PolicyApiHttpClient.class);
141         doReturn(Response.ok().build()).when(api).createPolicyType(any());
142         doReturn(Response.ok().build()).when(api).createPolicy(any());
143
144         var pap = mock(PolicyPapHttpClient.class);
145         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
146
147         var handler = new AutomationCompositionElementHandler(api, pap);
148         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
149         handler.setIntermediaryApi(intermediaryApi);
150         var element = getTestingAcElement();
151         assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
152                 .hasMessageMatching("Deploy of Policy failed.");
153     }
154 }