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 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.loop.components.external.ExternalComponentState;
35 import org.onap.clamp.loop.components.external.PolicyComponent;
38 public class PolicyComponentTest {
41 * Test the computeState method.
42 * oldState newState expectedFinalState
43 * NOT_SENT SENT_AND_DEPLOYED NOT_SENT
44 * NOT_SENT SENT NOT_SENT
45 * NOT_SENT NOT_SENT NOT_SENT
46 * NOT_SENT IN_ERROR IN_ERROR
49 public void computeStateTestOriginalStateNotSent() {
50 Exchange exchange = Mockito.mock(Exchange.class);
51 Message message = Mockito.mock(Message.class);
52 Exchange exchange2 = Mockito.mock(Exchange.class);
53 Mockito.when(exchange.getIn()).thenReturn(message);
54 Mockito.when(message.getExchange()).thenReturn(exchange2);
56 // policy found + deployed
57 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
58 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
60 PolicyComponent policy = new PolicyComponent ();
61 ExternalComponentState state = policy.computeState(exchange);
63 assertThat(state.getStateName()).isEqualTo("NOT_SENT");
65 // policy found + not deployed
66 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
67 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
68 ExternalComponentState state2 = policy.computeState(exchange);
70 assertThat(state2.getStateName()).isEqualTo("NOT_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);
77 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
79 // policy not found + deployed
80 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
81 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
82 ExternalComponentState state3 = policy.computeState(exchange);
84 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
89 * Test the computeState method.
90 * oldState newState expectedFinalState
92 * SENT SENT_AND_DEPLOYED SENT
93 * SENT IN_ERROR IN_ERROR
94 * SENT NOT_SENT NOT_SENT
97 public void computeStateTestOriginalStateSent() throws IOException {
98 Exchange exchange = Mockito.mock(Exchange.class);
99 Message message = Mockito.mock(Message.class);
100 Exchange exchange2 = Mockito.mock(Exchange.class);
101 Mockito.when(exchange.getIn()).thenReturn(message);
102 Mockito.when(message.getExchange()).thenReturn(exchange2);
104 PolicyComponent policy = new PolicyComponent ();
105 ExternalComponentState SENT = new ExternalComponentState("SENT",
106 "The policies defined have been created but NOT deployed on the policy engine", 50);
107 policy.setState(SENT);
109 // new policy state SENT
110 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
111 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
112 ExternalComponentState state = policy.computeState(exchange);
114 assertThat(state.getStateName()).isEqualTo("SENT");
116 // new policy state SENT_AND_DEPLOYED
117 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
118 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
119 ExternalComponentState state2 = policy.computeState(exchange);
121 assertThat(state2.getStateName()).isEqualTo("SENT");
123 // new policy state IN_ERROR
124 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
125 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
126 ExternalComponentState state3 = policy.computeState(exchange);
128 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
130 // new policy state NOT_SENT
131 policy.setState(SENT);
132 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
133 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
134 ExternalComponentState state4 = policy.computeState(exchange);
136 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
140 * Test the computeState method.
141 * oldState newState expectedFinalState
142 * SENT_AND_DEPLOYED SENT_AND_DEPLOYED SENT_AND_DEPLOYED
143 * SENT_AND_DEPLOYED SENT SENT
144 * SENT_AND_DEPLOYED IN_ERROR IN_ERROR
145 * SENT_AND_DEPLOYED NOT_SENT NOT_SENT
148 public void computeStateTestOriginalStateSentAndDeployed() throws IOException {
149 Exchange exchange = Mockito.mock(Exchange.class);
150 Message message = Mockito.mock(Message.class);
151 Exchange exchange2 = Mockito.mock(Exchange.class);
152 Mockito.when(exchange.getIn()).thenReturn(message);
153 Mockito.when(message.getExchange()).thenReturn(exchange2);
155 PolicyComponent policy = new PolicyComponent ();
156 ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED",
157 "The policies defined have been created and deployed on the policy engine", 10);
158 policy.setState(SENT_AND_DEPLOYED);
160 // new policy state SENT_AND_DEPLOYED
161 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
162 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
163 ExternalComponentState state = policy.computeState(exchange);
165 assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
167 // new policy state SENT
168 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
169 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
170 ExternalComponentState state2 = policy.computeState(exchange);
172 assertThat(state2.getStateName()).isEqualTo("SENT");
174 // new policy state IN_ERROR
175 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
176 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
177 ExternalComponentState state3 = policy.computeState(exchange);
179 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
181 // new policy state NOT_SENT
182 policy.setState(SENT_AND_DEPLOYED);
183 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
184 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
185 ExternalComponentState state4 = policy.computeState(exchange);
187 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
191 * Test the computeState method.
192 * oldState newState expectedFinalState
193 * IN_ERROR SENT_AND_DEPLOYED IN_ERROR
194 * IN_ERROR SENT IN_ERROR
195 * IN_ERROR IN_ERROR IN_ERROR
196 * IN_ERROR NOT_SENT IN_ERROR
199 public void computeStateTestOriginalStateInError() throws IOException {
200 Exchange exchange = Mockito.mock(Exchange.class);
201 Message message = Mockito.mock(Message.class);
202 Exchange exchange2 = Mockito.mock(Exchange.class);
203 Mockito.when(exchange.getIn()).thenReturn(message);
204 Mockito.when(message.getExchange()).thenReturn(exchange2);
206 PolicyComponent policy = new PolicyComponent ();
207 ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
208 "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent", 100);
209 policy.setState(IN_ERROR);
211 // new policy state SENT_AND_DEPLOYED
212 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
213 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
214 ExternalComponentState state = policy.computeState(exchange);
216 assertThat(state.getStateName()).isEqualTo("IN_ERROR");
218 // new policy state SENT
219 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
220 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
221 ExternalComponentState state2 = policy.computeState(exchange);
223 assertThat(state2.getStateName()).isEqualTo("IN_ERROR");
225 // new policy state IN_ERROR
226 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
227 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
228 ExternalComponentState state3 = policy.computeState(exchange);
230 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
232 // new policy state NOT_SENT
233 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
234 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
235 ExternalComponentState state4 = policy.computeState(exchange);
237 assertThat(state4.getStateName()).isEqualTo("IN_ERROR");