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.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;
48 class AutomationCompositionElementHandlerTest {
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);
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);
63 handler.undeploy(AC_ID, automationCompositionElementId);
64 verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
65 DeployState.UNDEPLOYED, LockState.NONE);
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);
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());
88 var pap = mock(PolicyPapHttpClient.class);
89 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
91 var handler = new AutomationCompositionElementHandler(api, pap);
92 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
93 handler.setIntermediaryApi(intermediaryApi);
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);
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());
107 var pap = mock(PolicyPapHttpClient.class);
108 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
110 var handler = new AutomationCompositionElementHandler(api, pap);
111 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
112 handler.setIntermediaryApi(intermediaryApi);
113 var element = getTestingAcElement();
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.");
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());
126 var pap = mock(PolicyPapHttpClient.class);
127 doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
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.");