aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java
blob: 16530d69fdfd1e0623b12698c844855f4523caf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021-2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.acm.participant.policy.main.handler;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
import org.onap.policy.clamp.models.acm.concepts.DeployState;
import org.onap.policy.clamp.models.acm.concepts.LockState;
import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;

class AutomationCompositionElementHandlerTest {

    private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
    private static final String ID_VERSION = "1.0.1";
    private static final UUID automationCompositionElementId = UUID.randomUUID();
    public static final UUID AC_ID = UUID.randomUUID();
    private static final ToscaConceptIdentifier DEFINITION = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);

    @Test
    void testHandlerUndeployNoPolicy() throws PfModelException {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                mock(PolicyPapHttpClient.class), intermediaryApi);

        handler.undeploy(AC_ID, automationCompositionElementId);
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
    }

    private AcElementDeploy getTestingAcElement() {
        var element = new AcElementDeploy();
        element.setDefinition(DEFINITION);
        element.setId(automationCompositionElementId);
        element.setOrderedState(DeployOrder.DEPLOY);
        var template = new ToscaServiceTemplate();
        template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
        template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
        template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
        element.setToscaServiceTemplateFragment(template);
        return element;
    }

    @Test
    void testDeploy() throws PfModelException {
        // Mock success scenario for policy creation and deployment
        var api = mock(PolicyApiHttpClient.class);
        doReturn(Response.ok().build()).when(api).createPolicyType(any());
        doReturn(Response.ok().build()).when(api).createPolicy(any());

        var pap = mock(PolicyPapHttpClient.class);
        doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());

        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);

        handler.deploy(AC_ID, getTestingAcElement(), Map.of());
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");

        handler.undeploy(AC_ID, automationCompositionElementId);
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
    }

    @Test
    void testDeployNoPolicy() throws PfModelException {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                mock(PolicyPapHttpClient.class), intermediaryApi);

        var acElement = getTestingAcElement();
        acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
        handler.deploy(AC_ID, acElement, Map.of());
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "ToscaTopologyTemplate not defined");
    }

    @Test
    void testApiException() throws PfModelException {
        var api = mock(PolicyApiHttpClient.class);
        doReturn(Response.serverError().build()).when(api).createPolicyType(any());
        doReturn(Response.ok().build()).when(api).createPolicy(any());

        var pap = mock(PolicyPapHttpClient.class);
        doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());

        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);

        var element = getTestingAcElement();

        // Mock failure in policy type creation
        handler.deploy(AC_ID, element, Map.of());
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
                "Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
    }

    @Test
    void testDeployPapException() throws PfModelException {
        var api = mock(PolicyApiHttpClient.class);
        doReturn(Response.ok().build()).when(api).createPolicyType(any());
        doReturn(Response.ok().build()).when(api).createPolicy(any());

        var pap = mock(PolicyPapHttpClient.class);
        doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());

        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);

        var element = getTestingAcElement();
        assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
                .hasMessageMatching("Deploy of Policy failed.");
    }

    @Test
    void testUpdate() throws Exception {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                mock(PolicyPapHttpClient.class), intermediaryApi);

        var acElement = getTestingAcElement();
        acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
        handler.update(AC_ID, acElement, Map.of());
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
    }

    @Test
    void testLock() throws Exception {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                mock(PolicyPapHttpClient.class), intermediaryApi);

        handler.lock(AC_ID, automationCompositionElementId);
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
                LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
    }

    @Test
    void testUnlock() throws Exception {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                mock(PolicyPapHttpClient.class), intermediaryApi);

        handler.unlock(AC_ID, automationCompositionElementId);
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
                LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
    }

    @Test
    void testDelete() throws Exception {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                mock(PolicyPapHttpClient.class), intermediaryApi);

        handler.delete(AC_ID, automationCompositionElementId);
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
    }

    @Test
    void testPrime() throws Exception {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                mock(PolicyPapHttpClient.class), intermediaryApi);

        handler.prime(AC_ID, List.of());
        verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
    }

    @Test
    void testDeprime() throws Exception {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                mock(PolicyPapHttpClient.class), intermediaryApi);

        handler.deprime(AC_ID);
        verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
                "Deprimed");
    }

    @Test
    void testHandleRestartComposition() throws PfModelException {
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var automationCompositionElementHandler =
                new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
                        mock(PolicyPapHttpClient.class), intermediaryApi);

        var compositionId = UUID.randomUUID();
        automationCompositionElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMED);

        verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
                StateChangeResult.NO_ERROR, "Restarted");
    }

    @Test
    void testHandleRestartInstanceDeploying() throws PfModelException {
        // Mock success scenario for policy creation and deployment
        var api = mock(PolicyApiHttpClient.class);
        doReturn(Response.ok().build()).when(api).createPolicyType(any());
        doReturn(Response.ok().build()).when(api).createPolicy(any());

        var pap = mock(PolicyPapHttpClient.class);
        doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());

        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
        var element = getTestingAcElement();

        handler.handleRestartInstance(AC_ID, element, Map.of(), DeployState.DEPLOYING, LockState.NONE);
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");

        handler.handleRestartInstance(AC_ID, element, Map.of(), DeployState.UNDEPLOYING, LockState.LOCKED);
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
    }

    @Test
    void testHandleRestartInstanceDeployed() throws PfModelException {
        var api = mock(PolicyApiHttpClient.class);
        var pap = mock(PolicyPapHttpClient.class);
        var intermediaryApi = mock(ParticipantIntermediaryApi.class);
        var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
        var element = getTestingAcElement();
        handler.handleRestartInstance(AC_ID, element, Map.of(), DeployState.DEPLOYED, LockState.LOCKED);
        verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
                DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted");
    }
}