ab181d476909fed018bbb5cb9887bcdfe8b8f945
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.participant.dcae.main.handler;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.UUID;
32 import org.json.JSONException;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
38 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ClampHttpClient;
39 import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ConsulDcaeHttpClient;
40 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
41 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.springframework.test.context.junit.jupiter.SpringExtension;
49
50 /**
51  * Class to perform unit test of {@link ControlLoopElementHandler}.
52  *
53  */
54 @ExtendWith(SpringExtension.class)
55 class ControlLoopElementHandlerTest {
56
57     private static final String LOOP = "pmsh_loop";
58     private static final String TEMPLATE = "LOOP_TEMPLATE_k8s_pmsh";
59     private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED";
60     private static final String MICROSERVICE_INSTALLED_SUCCESSFULLY = "MICROSERVICE_INSTALLED_SUCCESSFULLY";
61
62     public static final Coder CODER = new StandardCoder();
63     private CommonTestData commonTestData = new CommonTestData();
64
65     @Test
66     void test_ControlLoopElementStateChange() {
67         ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
68         ConsulDcaeHttpClient consulClient = mock(ConsulDcaeHttpClient.class);
69         ControlLoopElementHandler controlLoopElementHandler =
70                 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
71
72         when(clampClient.getstatus(eq(LOOP))).thenReturn(new Loop());
73
74         ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
75         controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
76
77         controlLoopElementHandler.controlLoopElementStateChange(UUID.randomUUID(), ControlLoopState.PASSIVE,
78                 ControlLoopOrderedState.UNINITIALISED);
79
80         verify(clampClient).undeploy(eq(LOOP));
81     }
82
83     @Test
84     void testCreate_ControlLoopElementUpdate() throws PfModelException, JSONException, CoderException {
85         ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
86         Loop loopDeployed = CODER.convert(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED), Loop.class);
87         when(clampClient.create(eq(LOOP), eq(TEMPLATE))).thenReturn(loopDeployed);
88         when(clampClient.deploy(eq(LOOP))).thenReturn(true);
89
90         Loop loopInstalled =
91                 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
92         when(clampClient.getstatus(eq(LOOP))).thenReturn(null, loopInstalled);
93
94         ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
95         when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
96
97         ControlLoopElementHandler controlLoopElementHandler =
98                 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
99
100         ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
101         controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
102
103         ControlLoopElement element = new ControlLoopElement();
104         element.setId(UUID.randomUUID());
105         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
106
107         final ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
108         controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition);
109
110         verify(clampClient).create(eq(LOOP), eq(TEMPLATE));
111         verify(consulClient).deploy(any(String.class), any(String.class));
112         verify(clampClient).deploy(eq(LOOP));
113     }
114
115     @Test
116     void test_ControlLoopElementUpdate() throws PfModelException, JSONException, CoderException {
117         ClampHttpClient clampClient = spy(mock(ClampHttpClient.class));
118         Loop loopDeployed = CODER.convert(CommonTestData.createJsonStatus(BLUEPRINT_DEPLOYED), Loop.class);
119         Loop loopInstalled =
120                 CODER.convert(CommonTestData.createJsonStatus(MICROSERVICE_INSTALLED_SUCCESSFULLY), Loop.class);
121         when(clampClient.getstatus(eq(LOOP))).thenReturn(loopDeployed, loopInstalled);
122         when(clampClient.deploy(eq(LOOP))).thenReturn(true);
123
124         ConsulDcaeHttpClient consulClient = spy(mock(ConsulDcaeHttpClient.class));
125         when(consulClient.deploy(any(String.class), any(String.class))).thenReturn(true);
126
127         ControlLoopElementHandler controlLoopElementHandler =
128                 new ControlLoopElementHandler(clampClient, consulClient, commonTestData.getParticipantDcaeParameters());
129
130         ParticipantIntermediaryApi intermediaryApi = mock(ParticipantIntermediaryApi.class);
131         controlLoopElementHandler.setIntermediaryApi(intermediaryApi);
132
133         ControlLoopElement element = new ControlLoopElement();
134         element.setId(UUID.randomUUID());
135         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
136
137         ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate();
138         controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition);
139
140         verify(clampClient, times(0)).create(eq(LOOP), eq(TEMPLATE));
141         verify(consulClient).deploy(any(String.class), any(String.class));
142         verify(clampClient).deploy(eq(LOOP));
143     }
144 }