52e10d2db30d0338b7ea13f8b0ceda6268e554f4
[clamp.git] / src / test / java / org / onap / clamp / loop / PolicyComponentTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import com.google.gson.Gson;
28 import com.google.gson.JsonObject;
29 import java.io.IOException;
30 import org.apache.camel.Exchange;
31 import org.apache.camel.Message;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.onap.clamp.clds.util.ResourceFileUtil;
35 import org.onap.clamp.loop.components.external.ExternalComponentState;
36 import org.onap.clamp.loop.components.external.PolicyComponent;
37 import org.onap.clamp.loop.template.LoopTemplate;
38 import org.onap.clamp.loop.template.PolicyModel;
39 import org.onap.clamp.policy.microservice.MicroServicePolicy;
40 import org.onap.clamp.policy.operational.OperationalPolicy;
41
42 public class PolicyComponentTest {
43
44     /**
45     * Test the computeState method. 
46     * oldState           newState        expectedFinalState
47     * NOT_SENT      SENT_AND_DEPLOYED          NOT_SENT
48     * NOT_SENT             SENT                NOT_SENT
49     * NOT_SENT           NOT_SENT              NOT_SENT
50     * NOT_SENT           IN_ERROR              IN_ERROR
51     */
52     @Test
53     public void computeStateTestOriginalStateUnknown() {
54         Exchange exchange = Mockito.mock(Exchange.class);
55         Message message = Mockito.mock(Message.class);
56         Exchange exchange2 = Mockito.mock(Exchange.class);
57         Mockito.when(exchange.getIn()).thenReturn(message);
58         Mockito.when(message.getExchange()).thenReturn(exchange2);
59         // policy found + deployed
60         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
61         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
62         PolicyComponent policy = new PolicyComponent();
63
64         ExternalComponentState state = policy.computeState(exchange);
65         assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
66         // policy found + not deployed
67         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
68         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
69         ExternalComponentState state2 = policy.computeState(exchange);
70         assertThat(state2.getStateName()).isEqualTo("SENT");
71         // policy not found + not deployed
72         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
73         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
74         ExternalComponentState state4 = policy.computeState(exchange);
75         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
76         // policy not found + deployed
77         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
78         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
79         ExternalComponentState state3 = policy.computeState(exchange);
80         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
81     }
82     /**
83     * Test the computeState method. 
84     * oldState           newState        expectedFinalState
85     * NOT_SENT      SENT_AND_DEPLOYED          NOT_SENT
86     * NOT_SENT             SENT                NOT_SENT
87     * NOT_SENT           NOT_SENT              NOT_SENT
88     * NOT_SENT           IN_ERROR              IN_ERROR
89     */
90     @Test
91     public void computeStateTestOriginalStateNotSent() {
92         Exchange exchange = Mockito.mock(Exchange.class);
93         Message message = Mockito.mock(Message.class);
94         Exchange exchange2 = Mockito.mock(Exchange.class);
95         Mockito.when(exchange.getIn()).thenReturn(message);
96         Mockito.when(message.getExchange()).thenReturn(exchange2);
97         // policy found + deployed
98         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
99         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
100         PolicyComponent policy = new PolicyComponent();
101         ExternalComponentState notSent = new ExternalComponentState("NOT_SENT",
102                 "The policies defined have NOT yet been created on the policy engine", 90);
103         policy.setState(notSent);
104         ExternalComponentState state = policy.computeState(exchange);
105         assertThat(state.getStateName()).isEqualTo("NOT_SENT");
106         // policy found + not deployed
107         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
108         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
109         ExternalComponentState state2 = policy.computeState(exchange);
110         assertThat(state2.getStateName()).isEqualTo("NOT_SENT");
111         // policy not found + not deployed
112         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
113         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
114         ExternalComponentState state4 = policy.computeState(exchange);
115         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
116         // policy not found + deployed
117         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
118         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
119         ExternalComponentState state3 = policy.computeState(exchange);
120         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
121     }
122
123
124     /**
125     * Test the computeState method. 
126     * oldState           newState        expectedFinalState
127     * SENT                 SENT                SENT
128     * SENT          SENT_AND_DEPLOYED          SENT
129     * SENT              IN_ERROR              IN_ERROR
130     * SENT              NOT_SENT              NOT_SENT
131     */
132     @Test
133     public void computeStateTestOriginalStateSent() throws IOException {
134         Exchange exchange = Mockito.mock(Exchange.class);
135         Message message = Mockito.mock(Message.class);
136         Exchange exchange2 = Mockito.mock(Exchange.class);
137         Mockito.when(exchange.getIn()).thenReturn(message);
138         Mockito.when(message.getExchange()).thenReturn(exchange2);
139         PolicyComponent policy = new PolicyComponent();
140         ExternalComponentState sent = new ExternalComponentState("SENT",
141                 "The policies defined have been created but NOT deployed on the policy engine", 50);
142         policy.setState(sent);
143         // new policy state SENT
144         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
145         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
146         ExternalComponentState state = policy.computeState(exchange);
147         assertThat(state.getStateName()).isEqualTo("SENT");
148         // new policy state SENT_AND_DEPLOYED
149         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
150         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
151         ExternalComponentState state2 = policy.computeState(exchange);
152         assertThat(state2.getStateName()).isEqualTo("SENT");
153         // new policy state IN_ERROR
154         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
155         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
156         ExternalComponentState state3 = policy.computeState(exchange);
157         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
158         // new policy state NOT_SENT
159         policy.setState(sent);
160         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
161         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
162         ExternalComponentState state4 = policy.computeState(exchange);
163         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
164     }
165
166     /**
167     * Test the computeState method. 
168     * oldState                   newState        expectedFinalState
169     * SENT_AND_DEPLOYED     SENT_AND_DEPLOYED    SENT_AND_DEPLOYED
170     * SENT_AND_DEPLOYED            SENT                SENT
171     * SENT_AND_DEPLOYED          IN_ERROR            IN_ERROR
172     * SENT_AND_DEPLOYED          NOT_SENT            NOT_SENT
173     */
174     @Test
175     public void computeStateTestOriginalStateSentAndDeployed() throws IOException {
176         Exchange exchange = Mockito.mock(Exchange.class);
177         Message message = Mockito.mock(Message.class);
178         Exchange exchange2 = Mockito.mock(Exchange.class);
179         Mockito.when(exchange.getIn()).thenReturn(message);
180         Mockito.when(message.getExchange()).thenReturn(exchange2);
181         PolicyComponent policy = new PolicyComponent();
182         ExternalComponentState sendDeployed = new ExternalComponentState("SENT_AND_DEPLOYED",
183                 "The policies defined have been created and deployed on the policy engine", 10);
184         policy.setState(sendDeployed);
185         // new policy state SENT_AND_DEPLOYED
186         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
187         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
188         ExternalComponentState state = policy.computeState(exchange);
189         assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
190         // new policy state SENT
191         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
192         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
193         ExternalComponentState state2 = policy.computeState(exchange);
194         assertThat(state2.getStateName()).isEqualTo("SENT");
195         // new policy state IN_ERROR
196         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
197         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
198         ExternalComponentState state3 = policy.computeState(exchange);
199         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
200         // new policy state NOT_SENT
201         policy.setState(sendDeployed);
202         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
203         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
204         ExternalComponentState state4 = policy.computeState(exchange);
205         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
206     }
207
208
209     /**
210     * Test the computeState method. 
211     * oldState           newState        expectedFinalState
212     * IN_ERROR     SENT_AND_DEPLOYED         IN_ERROR
213     * IN_ERROR            SENT               IN_ERROR
214     * IN_ERROR          IN_ERROR             IN_ERROR
215     * IN_ERROR          NOT_SENT             IN_ERROR
216     */
217     @Test
218     public void computeStateTestOriginalStateInError() throws IOException {
219         Exchange exchange = Mockito.mock(Exchange.class);
220         Message message = Mockito.mock(Message.class);
221         Exchange exchange2 = Mockito.mock(Exchange.class);
222         Mockito.when(exchange.getIn()).thenReturn(message);
223         Mockito.when(message.getExchange()).thenReturn(exchange2);
224         PolicyComponent policy = new PolicyComponent();
225         ExternalComponentState inError = new ExternalComponentState("IN_ERROR",
226                 "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent",
227                 100);
228         policy.setState(inError);
229         // new policy state SENT_AND_DEPLOYED
230         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
231         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
232         ExternalComponentState state = policy.computeState(exchange);
233         assertThat(state.getStateName()).isEqualTo("IN_ERROR");
234         // new policy state SENT
235         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
236         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
237         ExternalComponentState state2 = policy.computeState(exchange);
238         assertThat(state2.getStateName()).isEqualTo("IN_ERROR");
239         // new policy state IN_ERROR
240         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
241         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
242         ExternalComponentState state3 = policy.computeState(exchange);
243         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
244         // new policy state NOT_SENT
245         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
246         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
247         ExternalComponentState state4 = policy.computeState(exchange);
248
249         assertThat(state4.getStateName()).isEqualTo("IN_ERROR");
250     }
251
252     /**
253      * Test the create policies payload PdpGroup test.
254      */
255     @Test
256     public void createPoliciesPayloadPdpGroupTest() throws IOException {
257         Loop loopTest = new Loop("ControlLoopTest", "<xml></xml>");
258         PolicyModel policyModel1 = new PolicyModel("onap.policies.monitoring.test", null, "1.0.0");
259
260         MicroServicePolicy microServicePolicy = new MicroServicePolicy("configPolicyTest", policyModel1, true,
261                 new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), null);
262         microServicePolicy.setPdpGroup("pdpGroup1");
263         microServicePolicy.setPdpSubGroup("pdpSubgroup1");
264
265         loopTest.addMicroServicePolicy(microServicePolicy);
266
267         PolicyModel policyModel2 = new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0");
268         OperationalPolicy opPolicy = new OperationalPolicy("opPolicy", loopTest,
269                 new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), policyModel2, null);
270         opPolicy.setPdpGroup("pdpGroup2");
271         opPolicy.setPdpSubGroup("pdpSubgroup2");
272
273         loopTest.addOperationalPolicy(opPolicy);
274
275         LoopTemplate loopTemplate = new LoopTemplate("test", "yaml", "svg", 1, null);
276         loopTemplate.setDcaeBlueprintId("UUID-blueprint");
277         loopTest.setLoopTemplate(loopTemplate);
278
279         String payload = PolicyComponent.createPoliciesPayloadPdpGroup(loopTest);
280         String expectedRes = ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json");
281         assertThat(payload).isEqualTo(expectedRes);
282     }
283 }