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
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 java.util.List;
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;
47 class AutomationCompositionElementHandlerTest {
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);
56 void testHandlerUndeploy() throws PfModelException {
57 var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
58 mock(PolicyPapHttpClient.class));
59 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
60 handler.setIntermediaryApi(intermediaryApi);
62 handler.undeploy(AC_ID, automationCompositionElementId);
63 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
64 DeployState.UNDEPLOYED, null, "Undeployed");
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);
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 handler = new AutomationCompositionElementHandler(api, pap);
91 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
92 handler.setIntermediaryApi(intermediaryApi);
94 handler.deploy(AC_ID, getTestingAcElement(), Map.of());
95 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
96 DeployState.DEPLOYED, null, "Deployed");
100 void testApiException() throws PfModelException {
101 var api = mock(PolicyApiHttpClient.class);
102 doReturn(Response.serverError().build()).when(api).createPolicyType(any());
103 doReturn(Response.ok().build()).when(api).createPolicy(any());
105 var pap = mock(PolicyPapHttpClient.class);
106 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
108 var handler = new AutomationCompositionElementHandler(api, pap);
109 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
110 handler.setIntermediaryApi(intermediaryApi);
111 var element = getTestingAcElement();
113 // Mock failure in policy type creation
114 assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
115 .hasMessageMatching("Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
119 void testDeployPapException() throws PfModelException {
120 var api = mock(PolicyApiHttpClient.class);
121 doReturn(Response.ok().build()).when(api).createPolicyType(any());
122 doReturn(Response.ok().build()).when(api).createPolicy(any());
124 var pap = mock(PolicyPapHttpClient.class);
125 doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
127 var handler = new AutomationCompositionElementHandler(api, pap);
128 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
129 handler.setIntermediaryApi(intermediaryApi);
130 var element = getTestingAcElement();
131 assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
132 .hasMessageMatching("Deploy of Policy failed.");