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;
37 public class PolicyComponentTest {
40 * Test the computeState method. oldState newState expectedFinalState NOT_SENT
41 * SENT_AND_DEPLOYED NOT_SENT NOT_SENT SENT NOT_SENT NOT_SENT NOT_SENT NOT_SENT
42 * NOT_SENT IN_ERROR IN_ERROR
45 public void computeStateTestOriginalStateNotSent() {
46 Exchange exchange = Mockito.mock(Exchange.class);
47 Message message = Mockito.mock(Message.class);
48 Exchange exchange2 = Mockito.mock(Exchange.class);
49 Mockito.when(exchange.getIn()).thenReturn(message);
50 Mockito.when(message.getExchange()).thenReturn(exchange2);
52 // policy found + deployed
53 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
54 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
56 PolicyComponent policy = new PolicyComponent();
57 ExternalComponentState state = policy.computeState(exchange);
59 assertThat(state.getStateName()).isEqualTo("NOT_SENT");
61 // policy found + not deployed
62 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
63 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
64 ExternalComponentState state2 = policy.computeState(exchange);
66 assertThat(state2.getStateName()).isEqualTo("NOT_SENT");
68 // policy not found + not deployed
69 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
70 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
71 ExternalComponentState state4 = policy.computeState(exchange);
73 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
75 // policy not found + deployed
76 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
77 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
78 ExternalComponentState state3 = policy.computeState(exchange);
80 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
85 * Test the computeState method. oldState newState expectedFinalState SENT SENT
86 * SENT SENT SENT_AND_DEPLOYED SENT SENT IN_ERROR IN_ERROR SENT NOT_SENT
90 public void computeStateTestOriginalStateSent() throws IOException {
91 Exchange exchange = Mockito.mock(Exchange.class);
92 Message message = Mockito.mock(Message.class);
93 Exchange exchange2 = Mockito.mock(Exchange.class);
94 Mockito.when(exchange.getIn()).thenReturn(message);
95 Mockito.when(message.getExchange()).thenReturn(exchange2);
97 PolicyComponent policy = new PolicyComponent();
98 ExternalComponentState SENT = new ExternalComponentState("SENT",
99 "The policies defined have been created but NOT deployed on the policy engine", 50);
100 policy.setState(SENT);
102 // new policy state SENT
103 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
104 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
105 ExternalComponentState state = policy.computeState(exchange);
107 assertThat(state.getStateName()).isEqualTo("SENT");
109 // new policy state SENT_AND_DEPLOYED
110 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
111 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
112 ExternalComponentState state2 = policy.computeState(exchange);
114 assertThat(state2.getStateName()).isEqualTo("SENT");
116 // new policy state IN_ERROR
117 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
118 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
119 ExternalComponentState state3 = policy.computeState(exchange);
121 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
123 // new policy state NOT_SENT
124 policy.setState(SENT);
125 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
126 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
127 ExternalComponentState state4 = policy.computeState(exchange);
129 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
133 * Test the computeState method. oldState newState expectedFinalState
134 * SENT_AND_DEPLOYED SENT_AND_DEPLOYED SENT_AND_DEPLOYED SENT_AND_DEPLOYED SENT
135 * SENT SENT_AND_DEPLOYED IN_ERROR IN_ERROR SENT_AND_DEPLOYED NOT_SENT NOT_SENT
138 public void computeStateTestOriginalStateSentAndDeployed() throws IOException {
139 Exchange exchange = Mockito.mock(Exchange.class);
140 Message message = Mockito.mock(Message.class);
141 Exchange exchange2 = Mockito.mock(Exchange.class);
142 Mockito.when(exchange.getIn()).thenReturn(message);
143 Mockito.when(message.getExchange()).thenReturn(exchange2);
145 PolicyComponent policy = new PolicyComponent();
146 ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED",
147 "The policies defined have been created and deployed on the policy engine", 10);
148 policy.setState(SENT_AND_DEPLOYED);
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 state = policy.computeState(exchange);
155 assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
157 // new policy state SENT
158 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
159 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
160 ExternalComponentState state2 = policy.computeState(exchange);
162 assertThat(state2.getStateName()).isEqualTo("SENT");
164 // new policy state IN_ERROR
165 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
166 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
167 ExternalComponentState state3 = policy.computeState(exchange);
169 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
171 // new policy state NOT_SENT
172 policy.setState(SENT_AND_DEPLOYED);
173 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
174 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
175 ExternalComponentState state4 = policy.computeState(exchange);
177 assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
181 * Test the computeState method. oldState newState expectedFinalState IN_ERROR
182 * SENT_AND_DEPLOYED IN_ERROR IN_ERROR SENT IN_ERROR IN_ERROR IN_ERROR IN_ERROR
183 * IN_ERROR NOT_SENT IN_ERROR
186 public void computeStateTestOriginalStateInError() throws IOException {
187 Exchange exchange = Mockito.mock(Exchange.class);
188 Message message = Mockito.mock(Message.class);
189 Exchange exchange2 = Mockito.mock(Exchange.class);
190 Mockito.when(exchange.getIn()).thenReturn(message);
191 Mockito.when(message.getExchange()).thenReturn(exchange2);
193 PolicyComponent policy = new PolicyComponent();
194 ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
195 "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent",
197 policy.setState(IN_ERROR);
199 // new policy state SENT_AND_DEPLOYED
200 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
201 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
202 ExternalComponentState state = policy.computeState(exchange);
204 assertThat(state.getStateName()).isEqualTo("IN_ERROR");
206 // new policy state SENT
207 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
208 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
209 ExternalComponentState state2 = policy.computeState(exchange);
211 assertThat(state2.getStateName()).isEqualTo("IN_ERROR");
213 // new policy state IN_ERROR
214 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
215 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
216 ExternalComponentState state3 = policy.computeState(exchange);
218 assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
220 // new policy state NOT_SENT
221 Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
222 Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
223 ExternalComponentState state4 = policy.computeState(exchange);
225 assertThat(state4.getStateName()).isEqualTo("IN_ERROR");