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