Update UI to define Pdp Group
[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
30 import java.io.IOException;
31 import java.util.HashSet;
32 import org.apache.camel.Exchange;
33 import org.apache.camel.Message;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.onap.clamp.clds.util.ResourceFileUtil;
37 import org.onap.clamp.loop.components.external.ExternalComponentState;
38 import org.onap.clamp.loop.components.external.PolicyComponent;
39 import org.onap.clamp.loop.template.LoopTemplate;
40 import org.onap.clamp.loop.template.PolicyModel;
41 import org.onap.clamp.policy.microservice.MicroServicePolicy;
42 import org.onap.clamp.policy.operational.OperationalPolicy;
43
44 public class PolicyComponentTest {
45
46     /**
47     * Test the computeState method. 
48     * oldState           newState        expectedFinalState
49     * NOT_SENT      SENT_AND_DEPLOYED          NOT_SENT
50     * NOT_SENT             SENT                NOT_SENT
51     * NOT_SENT           NOT_SENT              NOT_SENT
52     * NOT_SENT           IN_ERROR              IN_ERROR
53     */
54     @Test
55     public void computeStateTestOriginalStateUnknown() {
56         Exchange exchange = Mockito.mock(Exchange.class);
57         Message message = Mockito.mock(Message.class);
58         Exchange exchange2 = Mockito.mock(Exchange.class);
59         Mockito.when(exchange.getIn()).thenReturn(message);
60         Mockito.when(message.getExchange()).thenReturn(exchange2);
61         // policy found + deployed
62         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
63         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
64         PolicyComponent policy = new PolicyComponent();
65
66         ExternalComponentState state = policy.computeState(exchange);
67         assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
68         // policy found + not deployed
69         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
70         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
71         ExternalComponentState state2 = policy.computeState(exchange);
72         assertThat(state2.getStateName()).isEqualTo("SENT");
73         // policy not found + not deployed
74         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
75         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
76         ExternalComponentState state4 = policy.computeState(exchange);
77         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
78         // policy not found + deployed
79         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
80         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
81         ExternalComponentState state3 = policy.computeState(exchange);
82         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
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", "<xml></xml>");
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         PolicyModel policyModel2 = new PolicyModel("onap.policies.controlloop.Operational", null, "1.0.0");
267         OperationalPolicy opPolicy = new OperationalPolicy("opPolicy", loopTest,
268                 new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), policyModel2, null, "pdpGroup2", "pdpSubgroup2");
269         loopTest.addOperationalPolicy(opPolicy);
270
271         LoopTemplate loopTemplate = new LoopTemplate("test", "yaml", "svg", 1, null);
272         loopTemplate.setDcaeBlueprintId("UUID-blueprint");
273         loopTest.setLoopTemplate(loopTemplate);
274
275         String payload = PolicyComponent.createPoliciesPayloadPdpGroup(loopTest);
276         String expectedRes = ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json");
277
278         assertThat(payload).isEqualTo(expectedRes);
279     }
280 }