73bd7f9b0afa25cdaaa689fd523e3da25cfebf2d
[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
28 import java.io.IOException;
29
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;
36
37
38 public class PolicyComponentTest {
39
40      /**
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
47      */
48     @Test
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);
55
56          // policy found + deployed 
57          Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
58          Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
59
60          PolicyComponent policy = new PolicyComponent ();
61          ExternalComponentState state = policy.computeState(exchange);
62
63          assertThat(state.getStateName()).isEqualTo("NOT_SENT");
64
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);
69
70          assertThat(state2.getStateName()).isEqualTo("NOT_SENT");
71
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
77          assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
78
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);
83
84          assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
85
86     }
87
88     /**
89     * Test the computeState method. 
90     * oldState           newState        expectedFinalState
91     * SENT                 SENT                SENT
92     * SENT          SENT_AND_DEPLOYED          SENT
93     * SENT              IN_ERROR              IN_ERROR
94     * SENT              NOT_SENT              NOT_SENT
95     */
96     @Test
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);
103
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);
108
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);
113
114          assertThat(state.getStateName()).isEqualTo("SENT");
115
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);
120
121          assertThat(state2.getStateName()).isEqualTo("SENT");
122
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);
127
128          assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
129
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);
135
136          assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
137     }
138
139     /**
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
146     */
147     @Test
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);
154
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);
159
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);
164
165          assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
166
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);
171
172          assertThat(state2.getStateName()).isEqualTo("SENT");
173
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);
178
179          assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
180
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);
186
187          assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
188     }
189
190     /**
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
197     */
198     @Test
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);
205
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);
210
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);
215
216          assertThat(state.getStateName()).isEqualTo("IN_ERROR");
217
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);
222
223          assertThat(state2.getStateName()).isEqualTo("IN_ERROR");
224
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);
229
230          assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
231
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);
236
237          assertThat(state4.getStateName()).isEqualTo("IN_ERROR");
238     }
239 }