Merge "Improve unit test coverage of loop/log"
[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 public class PolicyComponentTest {
38
39     /**
40     * Test the computeState method. 
41     * oldState           newState        expectedFinalState
42     * NOT_SENT      SENT_AND_DEPLOYED          NOT_SENT
43     * NOT_SENT             SENT                NOT_SENT
44     * NOT_SENT           NOT_SENT              NOT_SENT
45     * NOT_SENT           IN_ERROR              IN_ERROR
46     */
47     @Test
48     public void computeStateTestOriginalStateUnknown() {
49         Exchange exchange = Mockito.mock(Exchange.class);
50         Message message = Mockito.mock(Message.class);
51         Exchange exchange2 = Mockito.mock(Exchange.class);
52         Mockito.when(exchange.getIn()).thenReturn(message);
53         Mockito.when(message.getExchange()).thenReturn(exchange2);
54         // policy found + deployed
55         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
56         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
57         PolicyComponent policy = new PolicyComponent();
58
59         ExternalComponentState state = policy.computeState(exchange);
60         assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
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);
65         assertThat(state2.getStateName()).isEqualTo("SENT");
66         // policy not found + not deployed
67         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
68         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
69         ExternalComponentState state4 = policy.computeState(exchange);
70         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
71         // policy not found + deployed
72         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
73         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
74         ExternalComponentState state3 = policy.computeState(exchange);
75         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
76     }
77     /**
78     * Test the computeState method. 
79     * oldState           newState        expectedFinalState
80     * NOT_SENT      SENT_AND_DEPLOYED          NOT_SENT
81     * NOT_SENT             SENT                NOT_SENT
82     * NOT_SENT           NOT_SENT              NOT_SENT
83     * NOT_SENT           IN_ERROR              IN_ERROR
84     */
85     @Test
86     public void computeStateTestOriginalStateNotSent() {
87         Exchange exchange = Mockito.mock(Exchange.class);
88         Message message = Mockito.mock(Message.class);
89         Exchange exchange2 = Mockito.mock(Exchange.class);
90         Mockito.when(exchange.getIn()).thenReturn(message);
91         Mockito.when(message.getExchange()).thenReturn(exchange2);
92         // policy found + deployed
93         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
94         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
95         PolicyComponent policy = new PolicyComponent();
96         ExternalComponentState notSent = new ExternalComponentState("NOT_SENT",
97                 "The policies defined have NOT yet been created on the policy engine", 90);
98         policy.setState(notSent);
99         ExternalComponentState state = policy.computeState(exchange);
100         assertThat(state.getStateName()).isEqualTo("NOT_SENT");
101         // policy found + not deployed
102         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
103         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
104         ExternalComponentState state2 = policy.computeState(exchange);
105         assertThat(state2.getStateName()).isEqualTo("NOT_SENT");
106         // policy not found + not deployed
107         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
108         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
109         ExternalComponentState state4 = policy.computeState(exchange);
110         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
111         // policy not found + deployed
112         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
113         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
114         ExternalComponentState state3 = policy.computeState(exchange);
115         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
116     }
117
118
119     /**
120     * Test the computeState method. 
121     * oldState           newState        expectedFinalState
122     * SENT                 SENT                SENT
123     * SENT          SENT_AND_DEPLOYED          SENT
124     * SENT              IN_ERROR              IN_ERROR
125     * SENT              NOT_SENT              NOT_SENT
126     */
127     @Test
128     public void computeStateTestOriginalStateSent() throws IOException {
129         Exchange exchange = Mockito.mock(Exchange.class);
130         Message message = Mockito.mock(Message.class);
131         Exchange exchange2 = Mockito.mock(Exchange.class);
132         Mockito.when(exchange.getIn()).thenReturn(message);
133         Mockito.when(message.getExchange()).thenReturn(exchange2);
134         PolicyComponent policy = new PolicyComponent();
135         ExternalComponentState sent = new ExternalComponentState("SENT",
136                 "The policies defined have been created but NOT deployed on the policy engine", 50);
137         policy.setState(sent);
138         // new policy state SENT
139         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
140         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
141         ExternalComponentState state = policy.computeState(exchange);
142         assertThat(state.getStateName()).isEqualTo("SENT");
143         // new policy state SENT_AND_DEPLOYED
144         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
145         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
146         ExternalComponentState state2 = policy.computeState(exchange);
147         assertThat(state2.getStateName()).isEqualTo("SENT");
148         // new policy state IN_ERROR
149         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
150         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
151         ExternalComponentState state3 = policy.computeState(exchange);
152         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
153         // new policy state NOT_SENT
154         policy.setState(sent);
155         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
156         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
157         ExternalComponentState state4 = policy.computeState(exchange);
158         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
159     }
160
161     /**
162     * Test the computeState method. 
163     * oldState                   newState        expectedFinalState
164     * SENT_AND_DEPLOYED     SENT_AND_DEPLOYED    SENT_AND_DEPLOYED
165     * SENT_AND_DEPLOYED            SENT                SENT
166     * SENT_AND_DEPLOYED          IN_ERROR            IN_ERROR
167     * SENT_AND_DEPLOYED          NOT_SENT            NOT_SENT
168     */
169     @Test
170     public void computeStateTestOriginalStateSentAndDeployed() throws IOException {
171         Exchange exchange = Mockito.mock(Exchange.class);
172         Message message = Mockito.mock(Message.class);
173         Exchange exchange2 = Mockito.mock(Exchange.class);
174         Mockito.when(exchange.getIn()).thenReturn(message);
175         Mockito.when(message.getExchange()).thenReturn(exchange2);
176         PolicyComponent policy = new PolicyComponent();
177         ExternalComponentState sendDeployed = new ExternalComponentState("SENT_AND_DEPLOYED",
178                 "The policies defined have been created and deployed on the policy engine", 10);
179         policy.setState(sendDeployed);
180         // new policy state SENT_AND_DEPLOYED
181         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
182         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
183         ExternalComponentState state = policy.computeState(exchange);
184         assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
185         // new policy state SENT
186         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
187         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
188         ExternalComponentState state2 = policy.computeState(exchange);
189         assertThat(state2.getStateName()).isEqualTo("SENT");
190         // new policy state IN_ERROR
191         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
192         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
193         ExternalComponentState state3 = policy.computeState(exchange);
194         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
195         // new policy state NOT_SENT
196         policy.setState(sendDeployed);
197         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
198         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
199         ExternalComponentState state4 = policy.computeState(exchange);
200         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
201     }
202
203
204     /**
205     * Test the computeState method. 
206     * oldState           newState        expectedFinalState
207     * IN_ERROR     SENT_AND_DEPLOYED         IN_ERROR
208     * IN_ERROR            SENT               IN_ERROR
209     * IN_ERROR          IN_ERROR             IN_ERROR
210     * IN_ERROR          NOT_SENT             IN_ERROR
211     */
212     @Test
213     public void computeStateTestOriginalStateInError() throws IOException {
214         Exchange exchange = Mockito.mock(Exchange.class);
215         Message message = Mockito.mock(Message.class);
216         Exchange exchange2 = Mockito.mock(Exchange.class);
217         Mockito.when(exchange.getIn()).thenReturn(message);
218         Mockito.when(message.getExchange()).thenReturn(exchange2);
219         PolicyComponent policy = new PolicyComponent();
220         ExternalComponentState inError = new ExternalComponentState("IN_ERROR",
221                 "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent",
222                 100);
223         policy.setState(inError);
224         // new policy state SENT_AND_DEPLOYED
225         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
226         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
227         ExternalComponentState state = policy.computeState(exchange);
228         assertThat(state.getStateName()).isEqualTo("IN_ERROR");
229         // new policy state SENT
230         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
231         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
232         ExternalComponentState state2 = policy.computeState(exchange);
233         assertThat(state2.getStateName()).isEqualTo("IN_ERROR");
234         // new policy state IN_ERROR
235         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
236         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
237         ExternalComponentState state3 = policy.computeState(exchange);
238         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
239         // new policy state NOT_SENT
240         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(false);
241         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(false);
242         ExternalComponentState state4 = policy.computeState(exchange);
243
244         assertThat(state4.getStateName()).isEqualTo("IN_ERROR");
245     }
246 }