2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop;
26 import static org.assertj.core.api.Assertions.assertThat;
28 import com.google.gson.Gson;
29 import com.google.gson.JsonObject;
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;
44 public class PolicyComponentTest {
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
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();
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");
86 * Test the computeState method.
87 * oldState newState expectedFinalState
88 * NOT_SENT SENT_AND_DEPLOYED NOT_SENT
89 * NOT_SENT SENT NOT_SENT
90 * NOT_SENT NOT_SENT NOT_SENT
91 * NOT_SENT IN_ERROR IN_ERROR
94 public void computeStateTestOriginalStateNotSent() {
95 Exchange exchange = Mockito.mock(Exchange.class);
96 Message message = Mockito.mock(Message.class);
97 Exchange exchange2 = Mockito.mock(Exchange.class);
98 Mockito.when(exchange.getIn()).thenReturn(message);
99 Mockito.when(message.getExchange()).thenReturn(exchange2);
100 // policy found + deployed
101 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
102 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
103 PolicyComponent policy = new PolicyComponent();
104 ExternalComponentState notSent = new ExternalComponentState("NOT_SENT",
105 "The policies defined have NOT yet been created on the policy engine", 90);
106 policy.setState(notSent);
107 ExternalComponentState state = policy.computeState(exchange);
108 assertThat(state.getStateName()).isEqualTo("NOT_SENT");
109 // policy found + not deployed
110 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
111 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
112 ExternalComponentState state2 = policy.computeState(exchange);
113 assertThat(state2.getStateName()).isEqualTo("NOT_SENT");
114 // policy not found + not deployed
115 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
116 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
117 ExternalComponentState state4 = policy.computeState(exchange);
118 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
119 // policy not found + deployed
120 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
121 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
122 ExternalComponentState state3 = policy.computeState(exchange);
123 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
128 * Test the computeState method.
129 * oldState newState expectedFinalState
131 * SENT SENT_AND_DEPLOYED SENT
132 * SENT IN_ERROR IN_ERROR
133 * SENT NOT_SENT NOT_SENT
136 public void computeStateTestOriginalStateSent() throws IOException {
137 Exchange exchange = Mockito.mock(Exchange.class);
138 Message message = Mockito.mock(Message.class);
139 Exchange exchange2 = Mockito.mock(Exchange.class);
140 Mockito.when(exchange.getIn()).thenReturn(message);
141 Mockito.when(message.getExchange()).thenReturn(exchange2);
142 PolicyComponent policy = new PolicyComponent();
143 ExternalComponentState sent = new ExternalComponentState("SENT",
144 "The policies defined have been created but NOT deployed on the policy engine", 50);
145 policy.setState(sent);
146 // new policy state SENT
147 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
148 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
149 ExternalComponentState state = policy.computeState(exchange);
150 assertThat(state.getStateName()).isEqualTo("SENT");
151 // new policy state SENT_AND_DEPLOYED
152 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
153 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
154 ExternalComponentState state2 = policy.computeState(exchange);
155 assertThat(state2.getStateName()).isEqualTo("SENT");
156 // new policy state IN_ERROR
157 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
158 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
159 ExternalComponentState state3 = policy.computeState(exchange);
160 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
161 // new policy state NOT_SENT
162 policy.setState(sent);
163 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
164 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
165 ExternalComponentState state4 = policy.computeState(exchange);
166 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
170 * Test the computeState method.
171 * oldState newState expectedFinalState
172 * SENT_AND_DEPLOYED SENT_AND_DEPLOYED SENT_AND_DEPLOYED
173 * SENT_AND_DEPLOYED SENT SENT
174 * SENT_AND_DEPLOYED IN_ERROR IN_ERROR
175 * SENT_AND_DEPLOYED NOT_SENT NOT_SENT
178 public void computeStateTestOriginalStateSentAndDeployed() throws IOException {
179 Exchange exchange = Mockito.mock(Exchange.class);
180 Message message = Mockito.mock(Message.class);
181 Exchange exchange2 = Mockito.mock(Exchange.class);
182 Mockito.when(exchange.getIn()).thenReturn(message);
183 Mockito.when(message.getExchange()).thenReturn(exchange2);
184 PolicyComponent policy = new PolicyComponent();
185 ExternalComponentState sendDeployed = new ExternalComponentState("SENT_AND_DEPLOYED",
186 "The policies defined have been created and deployed on the policy engine", 10);
187 policy.setState(sendDeployed);
188 // new policy state SENT_AND_DEPLOYED
189 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
190 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
191 ExternalComponentState state = policy.computeState(exchange);
192 assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
193 // new policy state SENT
194 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
195 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
196 ExternalComponentState state2 = policy.computeState(exchange);
197 assertThat(state2.getStateName()).isEqualTo("SENT");
198 // new policy state IN_ERROR
199 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
200 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
201 ExternalComponentState state3 = policy.computeState(exchange);
202 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
203 // new policy state NOT_SENT
204 policy.setState(sendDeployed);
205 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
206 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
207 ExternalComponentState state4 = policy.computeState(exchange);
208 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
213 * Test the computeState method.
214 * oldState newState expectedFinalState
215 * IN_ERROR SENT_AND_DEPLOYED IN_ERROR
216 * IN_ERROR SENT IN_ERROR
217 * IN_ERROR IN_ERROR IN_ERROR
218 * IN_ERROR NOT_SENT IN_ERROR
221 public void computeStateTestOriginalStateInError() throws IOException {
222 Exchange exchange = Mockito.mock(Exchange.class);
223 Message message = Mockito.mock(Message.class);
224 Exchange exchange2 = Mockito.mock(Exchange.class);
225 Mockito.when(exchange.getIn()).thenReturn(message);
226 Mockito.when(message.getExchange()).thenReturn(exchange2);
227 PolicyComponent policy = new PolicyComponent();
228 ExternalComponentState inError = new ExternalComponentState("IN_ERROR",
229 "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent",
231 policy.setState(inError);
232 // new policy state SENT_AND_DEPLOYED
233 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
234 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
235 ExternalComponentState state = policy.computeState(exchange);
236 assertThat(state.getStateName()).isEqualTo("IN_ERROR");
237 // new policy state SENT
238 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
239 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
240 ExternalComponentState state2 = policy.computeState(exchange);
241 assertThat(state2.getStateName()).isEqualTo("IN_ERROR");
242 // new policy state IN_ERROR
243 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
244 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
245 ExternalComponentState state3 = policy.computeState(exchange);
246 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
247 // new policy state NOT_SENT
248 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
249 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
250 ExternalComponentState state4 = policy.computeState(exchange);
252 assertThat(state4.getStateName()).isEqualTo("IN_ERROR");
256 * Test the create policies payload PdpGroup test.
259 public void createPoliciesPayloadPdpGroupTest() throws IOException {
260 Loop loopTest = new Loop("ControlLoopTest", "<xml></xml>");
261 PolicyModel policyModel1 = new PolicyModel("onap.policies.monitoring.test", null, "1.0.0");
263 MicroServicePolicy microServicePolicy = new MicroServicePolicy("configPolicyTest", policyModel1, true,
264 new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), null, "pdpGroup1", "pdpSubgroup1");
265 loopTest.addMicroServicePolicy(microServicePolicy);
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, "pdpGroup2",
271 loopTest.addOperationalPolicy(opPolicy);
273 LoopTemplate loopTemplate = new LoopTemplate("test", "yaml", "svg", 1, null);
274 loopTemplate.setDcaeBlueprintId("UUID-blueprint");
275 loopTest.setLoopTemplate(loopTemplate);
277 String payload = PolicyComponent.createPoliciesPayloadPdpGroup(loopTest);
278 String expectedRes = ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json");
280 assertThat(payload).isEqualTo(expectedRes);