Add policy model id in tosca parser
[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. 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
43      */
44     @Test
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);
51
52         // policy found + deployed
53         Mockito.when(exchange2.getProperty("policyFound")).thenReturn(true);
54         Mockito.when(exchange2.getProperty("policyDeployed")).thenReturn(true);
55
56         PolicyComponent policy = new PolicyComponent();
57         ExternalComponentState state = policy.computeState(exchange);
58
59         assertThat(state.getStateName()).isEqualTo("NOT_SENT");
60
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
66         assertThat(state2.getStateName()).isEqualTo("NOT_SENT");
67
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);
72
73         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
74
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);
79
80         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
81
82     }
83
84     /**
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
87      * NOT_SENT
88      */
89     @Test
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);
96
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);
101
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);
106
107         assertThat(state.getStateName()).isEqualTo("SENT");
108
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);
113
114         assertThat(state2.getStateName()).isEqualTo("SENT");
115
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);
120
121         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
122
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);
128
129         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
130     }
131
132     /**
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
136      */
137     @Test
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);
144
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);
149
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);
154
155         assertThat(state.getStateName()).isEqualTo("SENT_AND_DEPLOYED");
156
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);
161
162         assertThat(state2.getStateName()).isEqualTo("SENT");
163
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);
168
169         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
170
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);
176
177         assertThat(state4.getStateName()).isEqualTo("NOT_SENT");
178     }
179
180     /**
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
184      */
185     @Test
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);
192
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",
196                 100);
197         policy.setState(IN_ERROR);
198
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);
203
204         assertThat(state.getStateName()).isEqualTo("IN_ERROR");
205
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);
210
211         assertThat(state2.getStateName()).isEqualTo("IN_ERROR");
212
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);
217
218         assertThat(state3.getStateName()).isEqualTo("IN_ERROR");
219
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);
224
225         assertThat(state4.getStateName()).isEqualTo("IN_ERROR");
226     }
227 }