Remove legacy operational Policy
[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 com.google.gson.Gson;
27 import com.google.gson.JsonObject;
28 import java.io.IOException;
29 import org.apache.camel.Exchange;
30 import org.apache.camel.Message;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.clamp.clds.util.ResourceFileUtils;
34 import org.onap.clamp.loop.components.external.ExternalComponentState;
35 import org.onap.clamp.loop.components.external.PolicyComponent;
36 import org.onap.clamp.loop.template.LoopTemplate;
37 import org.onap.clamp.loop.template.PolicyModel;
38 import org.onap.clamp.policy.microservice.MicroServicePolicy;
39 import org.onap.clamp.policy.operational.OperationalPolicy;
40
41 import static org.assertj.core.api.Assertions.assertThat;
42
43 public class PolicyComponentTest {
44
45     /**
46      * Test the computeState method.
47      * oldState           newState        expectedFinalState
48      * NOT_SENT      SENT_AND_DEPLOYED          NOT_SENT
49      * NOT_SENT             SENT                NOT_SENT
50      * NOT_SENT           NOT_SENT              NOT_SENT
51      * NOT_SENT           IN_ERROR              IN_ERROR
52      */
53     @Test
54     public void computeStateTestOriginalStateUnknown() {
55         Exchange exchange = Mockito.mock(Exchange.class);
56         Message message = Mockito.mock(Message.class);
57         Exchange exchange2 = Mockito.mock(Exchange.class);
58         Mockito.when(exchange.getIn()).thenReturn(message);
59         Mockito.when(message.getExchange()).thenReturn(exchange2);
60         // policy found + deployed
61         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
62         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
63         PolicyComponent policy = new PolicyComponent();
64
65         ExternalComponentState state = policy.computeState(exchange);
66         assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
67         // policy found + not deployed
68         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
69         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
70         ExternalComponentState state2 = policy.computeState(exchange);
71         assertThat(state2.getStateName()).isEqualTo("SENT");
72         // policy not found + not deployed
73         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
74         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
75         ExternalComponentState state4 = policy.computeState(exchange);
76         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
77         // policy not found + deployed
78         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
79         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
80         ExternalComponentState state3 = policy.computeState(exchange);
81         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
82     }
83
84     /**
85      * Test the computeState method.
86      * oldState           newState        expectedFinalState
87      * NOT_SENT      SENT_AND_DEPLOYED          NOT_SENT
88      * NOT_SENT             SENT                NOT_SENT
89      * NOT_SENT           NOT_SENT              NOT_SENT
90      * NOT_SENT           IN_ERROR              IN_ERROR
91      */
92     @Test
93     public void computeStateTestOriginalStateNotSent() {
94         Exchange exchange = Mockito.mock(Exchange.class);
95         Message message = Mockito.mock(Message.class);
96         Exchange exchange2 = Mockito.mock(Exchange.class);
97         Mockito.when(exchange.getIn()).thenReturn(message);
98         Mockito.when(message.getExchange()).thenReturn(exchange2);
99         // policy found + deployed
100         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
101         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
102         PolicyComponent policy = new PolicyComponent();
103         ExternalComponentState notSent = new ExternalComponentState("NOT_SENT",
104                 "The policies defined have NOT yet been created on the policy engine", 90);
105         policy.setState(notSent);
106         ExternalComponentState state = policy.computeState(exchange);
107         assertThat(state.getStateName()).isEqualTo("NOT_SENT");
108         // policy found + not deployed
109         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
110         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
111         ExternalComponentState state2 = policy.computeState(exchange);
112         assertThat(state2.getStateName()).isEqualTo("NOT_SENT");
113         // policy not found + not deployed
114         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
115         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
116         ExternalComponentState state4 = policy.computeState(exchange);
117         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
118         // policy not found + deployed
119         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
120         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
121         ExternalComponentState state3 = policy.computeState(exchange);
122         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
123     }
124
125
126     /**
127      * Test the computeState method.
128      * oldState           newState        expectedFinalState
129      * SENT                 SENT                SENT
130      * SENT          SENT_AND_DEPLOYED          SENT
131      * SENT              IN_ERROR              IN_ERROR
132      * SENT              NOT_SENT              NOT_SENT
133      */
134     @Test
135     public void computeStateTestOriginalStateSent() throws IOException {
136         Exchange exchange = Mockito.mock(Exchange.class);
137         Message message = Mockito.mock(Message.class);
138         Exchange exchange2 = Mockito.mock(Exchange.class);
139         Mockito.when(exchange.getIn()).thenReturn(message);
140         Mockito.when(message.getExchange()).thenReturn(exchange2);
141         PolicyComponent policy = new PolicyComponent();
142         ExternalComponentState sent = new ExternalComponentState("SENT",
143                 "The policies defined have been created but NOT deployed on the policy engine", 50);
144         policy.setState(sent);
145         // new policy state SENT
146         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
147         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
148         ExternalComponentState state = policy.computeState(exchange);
149         assertThat(state.getStateName()).isEqualTo("SENT");
150         // new policy state SENT_AND_DEPLOYED
151         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
152         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
153         ExternalComponentState state2 = policy.computeState(exchange);
154         assertThat(state2.getStateName()).isEqualTo("SENT");
155         // new policy state IN_ERROR
156         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
157         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
158         ExternalComponentState state3 = policy.computeState(exchange);
159         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
160         // new policy state NOT_SENT
161         policy.setState(sent);
162         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
163         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
164         ExternalComponentState state4 = policy.computeState(exchange);
165         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
166     }
167
168     /**
169      * Test the computeState method.
170      * oldState                   newState        expectedFinalState
171      * SENT_AND_DEPLOYED     SENT_AND_DEPLOYED    SENT_AND_DEPLOYED
172      * SENT_AND_DEPLOYED            SENT                SENT
173      * SENT_AND_DEPLOYED          IN_ERROR            IN_ERROR
174      * SENT_AND_DEPLOYED          NOT_SENT            NOT_SENT
175      */
176     @Test
177     public void computeStateTestOriginalStateSentAndDeployed() throws IOException {
178         Exchange exchange = Mockito.mock(Exchange.class);
179         Message message = Mockito.mock(Message.class);
180         Exchange exchange2 = Mockito.mock(Exchange.class);
181         Mockito.when(exchange.getIn()).thenReturn(message);
182         Mockito.when(message.getExchange()).thenReturn(exchange2);
183         PolicyComponent policy = new PolicyComponent();
184         ExternalComponentState sendDeployed = new ExternalComponentState("SENT_AND_DEPLOYED",
185                 "The policies defined have been created and deployed on the policy engine", 10);
186         policy.setState(sendDeployed);
187         // new policy state SENT_AND_DEPLOYED
188         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
189         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
190         ExternalComponentState state = policy.computeState(exchange);
191         assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
192         // new policy state SENT
193         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
194         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
195         ExternalComponentState state2 = policy.computeState(exchange);
196         assertThat(state2.getStateName()).isEqualTo("SENT");
197         // new policy state IN_ERROR
198         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
199         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
200         ExternalComponentState state3 = policy.computeState(exchange);
201         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
202         // new policy state NOT_SENT
203         policy.setState(sendDeployed);
204         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
205         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
206         ExternalComponentState state4 = policy.computeState(exchange);
207         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
208     }
209
210
211     /**
212      * Test the computeState method.
213      * oldState           newState        expectedFinalState
214      * IN_ERROR     SENT_AND_DEPLOYED         IN_ERROR
215      * IN_ERROR            SENT               IN_ERROR
216      * IN_ERROR          IN_ERROR             IN_ERROR
217      * IN_ERROR          NOT_SENT             IN_ERROR
218      */
219     @Test
220     public void computeStateTestOriginalStateInError() throws IOException {
221         Exchange exchange = Mockito.mock(Exchange.class);
222         Message message = Mockito.mock(Message.class);
223         Exchange exchange2 = Mockito.mock(Exchange.class);
224         Mockito.when(exchange.getIn()).thenReturn(message);
225         Mockito.when(message.getExchange()).thenReturn(exchange2);
226         PolicyComponent policy = new PolicyComponent();
227         ExternalComponentState inError = new ExternalComponentState("IN_ERROR",
228                 "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent",
229                 100);
230         policy.setState(inError);
231         // new policy state SENT_AND_DEPLOYED
232         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
233         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
234         ExternalComponentState state = policy.computeState(exchange);
235         assertThat(state.getStateName()).isEqualTo("IN_ERROR");
236         // new policy state SENT
237         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
238         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
239         ExternalComponentState state2 = policy.computeState(exchange);
240         assertThat(state2.getStateName()).isEqualTo("IN_ERROR");
241         // new policy state IN_ERROR
242         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
243         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
244         ExternalComponentState state3 = policy.computeState(exchange);
245         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
246         // new policy state NOT_SENT
247         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
248         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
249         ExternalComponentState state4 = policy.computeState(exchange);
250
251         assertThat(state4.getStateName()).isEqualTo("IN_ERROR");
252     }
253
254     /**
255      * Test the create policies payload PdpGroup test.
256      */
257     @Test
258     public void createPoliciesPayloadPdpGroupTest() throws IOException {
259         Loop loopTest = new Loop("ControlLoopTest");
260         PolicyModel policyModel1 = new PolicyModel("onap.policies.monitoring.test", null, "1.0.0");
261
262         MicroServicePolicy microServicePolicy = new MicroServicePolicy("configPolicyTest", policyModel1, true,
263                 new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), null, "pdpGroup1", "pdpSubgroup1");
264         loopTest.addMicroServicePolicy(microServicePolicy);
265
266         MicroServicePolicy microServicePolicy2 = new MicroServicePolicy("configPolicyTest2", policyModel1, true,
267                 new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), null, "pdpGroup2", "pdpSubgroup1");
268         loopTest.addMicroServicePolicy(microServicePolicy2);
269
270         PolicyModel policyModel2 = new PolicyModel("onap.policies.monitoring.test2", null,
271                 "1.0.0");
272         OperationalPolicy opPolicy =
273                 new OperationalPolicy("opPolicy", new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class),
274                         new Gson().fromJson("{\"jsonschema\":\"schema\"}", JsonObject.class), policyModel2, null,
275                         "pdpGroup2",
276                         "pdpSubgroup2");
277
278         loopTest.addOperationalPolicy(opPolicy);
279         OperationalPolicy opLegacyPolicy =
280                 new OperationalPolicy("opLegacyPolicy", new Gson().fromJson(
281                         "{\"guard_policies\":[{\"policy-id\":\"guard1\"}]}", JsonObject.class),
282                         new Gson().fromJson("{\"jsonschema\":\"schema\"}", JsonObject.class), policyModel2, null,
283                         "pdpGroup2",
284                         "pdpSubgroup2");
285
286         loopTest.addOperationalPolicy(opLegacyPolicy);
287
288         LoopTemplate loopTemplate = new LoopTemplate("test", "yaml", 1, null);
289         loopTemplate.setDcaeBlueprintId("UUID-blueprint");
290         loopTest.setLoopTemplate(loopTemplate);
291
292         String payload = PolicyComponent.createPoliciesPayloadPdpGroup(loopTest, "POST");
293         String expectedRes = ResourceFileUtils.getResourceAsString("tosca/pdp-group-policy-payload.json");
294
295         assertThat(payload).isEqualTo(expectedRes);
296     }
297 }