47c279988d6bd8b83bbac6d2ffca555ba71c3916
[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.concepts.LockState;
40 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
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 String ID_NAME = "org.onap.PM_CDS_Blueprint";
51     private static final String ID_VERSION = "1.0.1";
52     private static final UUID automationCompositionElementId = UUID.randomUUID();
53     public static final UUID AC_ID = UUID.randomUUID();
54     private static final ToscaConceptIdentifier DEFINITION = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
55
56     @Test
57     void testHandlerUndeploy() throws PfModelException {
58         var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
59                 mock(PolicyPapHttpClient.class));
60         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
61         handler.setIntermediaryApi(intermediaryApi);
62
63         handler.undeploy(AC_ID, automationCompositionElementId);
64         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
65                 DeployState.UNDEPLOYED, LockState.NONE);
66     }
67
68     private AcElementDeploy getTestingAcElement() {
69         var element = new AcElementDeploy();
70         element.setDefinition(DEFINITION);
71         element.setId(automationCompositionElementId);
72         element.setOrderedState(DeployOrder.DEPLOY);
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         element.setToscaServiceTemplateFragment(template);
78         return element;
79     }
80
81     @Test
82     void testDeploy() throws PfModelException {
83         // Mock success scenario for policy creation and deployment
84         var api = mock(PolicyApiHttpClient.class);
85         doReturn(Response.ok().build()).when(api).createPolicyType(any());
86         doReturn(Response.ok().build()).when(api).createPolicy(any());
87
88         var pap = mock(PolicyPapHttpClient.class);
89         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
90
91         var handler = new AutomationCompositionElementHandler(api, pap);
92         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
93         handler.setIntermediaryApi(intermediaryApi);
94
95         handler.deploy(AC_ID, getTestingAcElement(), Map.of());
96         handler.undeploy(AC_ID, automationCompositionElementId);
97         verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
98                 DeployState.UNDEPLOYED, LockState.NONE);
99     }
100
101     @Test
102     void testApiException() throws PfModelException {
103         var api = mock(PolicyApiHttpClient.class);
104         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
105         doReturn(Response.ok().build()).when(api).createPolicy(any());
106
107         var pap = mock(PolicyPapHttpClient.class);
108         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
109
110         var handler = new AutomationCompositionElementHandler(api, pap);
111         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
112         handler.setIntermediaryApi(intermediaryApi);
113         var element = getTestingAcElement();
114
115         // Mock failure in policy type creation
116         assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
117                 .hasMessageMatching("Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
118     }
119
120     @Test
121     void testDeployPapException() throws PfModelException {
122         var api = mock(PolicyApiHttpClient.class);
123         doReturn(Response.ok().build()).when(api).createPolicyType(any());
124         doReturn(Response.ok().build()).when(api).createPolicy(any());
125
126         var pap = mock(PolicyPapHttpClient.class);
127         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
128
129         var handler = new AutomationCompositionElementHandler(api, pap);
130         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
131         handler.setIntermediaryApi(intermediaryApi);
132         var element = getTestingAcElement();
133         assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
134                 .hasMessageMatching("Deploy of Policy failed.");
135     }
136 }