e0b4a69c28443fb008021662dab4627cf45af2e2
[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.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30 import javax.ws.rs.core.Response;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.Mockito;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
34 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
35 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
37 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
44
45 class AutomationCompositionElementHandlerTest {
46
47     private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
48     private static final String ID_VERSION = "1.0.1";
49     private static final UUID automationCompositionElementId = UUID.randomUUID();
50     public static final UUID AC_ID = UUID.randomUUID();
51     private static final ToscaConceptIdentifier DEFINITION = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
52
53     private PolicyApiHttpClient api = Mockito.mock(PolicyApiHttpClient.class);
54     private PolicyPapHttpClient pap = Mockito.mock(PolicyPapHttpClient.class);
55
56     @Test
57     void testHandlerDoesNotThrowExceptions() {
58         var handler = getTestingHandler();
59
60         assertDoesNotThrow(() -> handler.undeploy(AC_ID, automationCompositionElementId));
61     }
62
63     private AutomationCompositionElementHandler getTestingHandler() {
64         var handler = new AutomationCompositionElementHandler(api, pap);
65         var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class);
66         handler.setIntermediaryApi(intermediaryApi);
67         return handler;
68     }
69
70     private AcElementDeploy getTestingAcElement() {
71         var element = new AcElementDeploy();
72         element.setDefinition(DEFINITION);
73         element.setId(automationCompositionElementId);
74         element.setOrderedState(DeployOrder.DEPLOY);
75         var template = new ToscaServiceTemplate();
76         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
77         template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
78         template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
79         element.setToscaServiceTemplateFragment(template);
80         return element;
81     }
82
83     @Test
84     void testAcElementUpdate() throws PfModelException {
85         // Mock success scenario for policy creation and deployment
86         doReturn(Response.ok().build()).when(api).createPolicyType(any());
87         doReturn(Response.ok().build()).when(api).createPolicy(any());
88         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
89
90         var handler = getTestingHandler();
91         var element = getTestingAcElement();
92
93         assertDoesNotThrow(() -> handler.deploy(AC_ID, element, Map.of()));
94
95         assertDoesNotThrow(() -> handler.undeploy(AC_ID, automationCompositionElementId));
96
97         // Mock failure in policy deployment
98         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
99         assertDoesNotThrow(() -> handler.deploy(AC_ID, element, Map.of()));
100
101         // Mock failure in policy type creation
102         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
103         assertDoesNotThrow(() -> handler.deploy(AC_ID, element, Map.of()));
104     }
105 }