4fc10b828975bd02ee02a61b9d85b799eebfc3ed
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.acm.participant.policy.main.parameters.CommonTestData;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
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;
46
47 class AutomationCompositionElementHandlerTest {
48
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);
54
55     private PolicyApiHttpClient api = Mockito.mock(PolicyApiHttpClient.class);
56     private PolicyPapHttpClient pap = Mockito.mock(PolicyPapHttpClient.class);
57
58     @Test
59     void testHandlerDoesNotThrowExceptions() {
60         var handler = getTestingHandler();
61
62         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
63                 AutomationCompositionState.UNINITIALISED, AutomationCompositionOrderedState.PASSIVE));
64
65         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
66                 AutomationCompositionState.RUNNING, AutomationCompositionOrderedState.UNINITIALISED));
67
68         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
69                 AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.RUNNING));
70     }
71
72     private AutomationCompositionElementHandler getTestingHandler() {
73         var handler = new AutomationCompositionElementHandler(api, pap);
74         var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class);
75         handler.setIntermediaryApi(intermediaryApi);
76         return handler;
77     }
78
79     private AutomationCompositionElement getTestingAcElement() {
80         var element = new AutomationCompositionElement();
81         element.setDefinition(DEFINITION);
82         element.setDescription("Description");
83         element.setId(automationCompositionElementId);
84         element.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
85         element.setParticipantId(CommonTestData.getParticipantId());
86         element.setState(AutomationCompositionState.UNINITIALISED);
87         var template = new ToscaServiceTemplate();
88         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
89         template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
90         template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
91         element.setToscaServiceTemplateFragment(template);
92         return element;
93     }
94
95     @Test
96     void testAcElementUpdate() throws PfModelException {
97         // Mock success scenario for policy creation and deployment
98         doReturn(Response.ok().build()).when(api).createPolicyType(any());
99         doReturn(Response.ok().build()).when(api).createPolicy(any());
100         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
101
102         var handler = getTestingHandler();
103         var element = getTestingAcElement();
104
105         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));
106
107         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
108                 AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED));
109
110         // Mock failure in policy deployment
111         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
112         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));
113
114         // Mock failure in policy type creation
115         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
116         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));
117     }
118 }