1cb8b5a5d1488471b7c82808179c7ae2d4a8fd0b
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.eventmanager;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
31
32 import org.junit.Test;
33 import org.onap.policy.appc.Request;
34 import org.onap.policy.appc.Response;
35 import org.onap.policy.appc.ResponseCode;
36 import org.onap.policy.appc.ResponseValue;
37 import org.onap.policy.controlloop.ControlLoopEventStatus;
38
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.ControlLoopException;
41 import org.onap.policy.controlloop.Util;
42 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
43 import org.onap.policy.controlloop.policy.PolicyResult;
44 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
45
46 public class ControlLoopOperationManagerTest {
47         
48         private static VirtualControlLoopEvent onset;
49         static {
50                 onset = new VirtualControlLoopEvent();
51                 onset.requestID = UUID.randomUUID();
52                 onset.target = "vserver.selflink";
53                 onset.closedLoopAlarmStart = Instant.now();
54                 onset.AAI = new HashMap<String, String>();
55                 onset.AAI.put("cloud-region.identity-url", "foo");
56                 onset.AAI.put("vserver.selflink", "bar");
57                 onset.AAI.put("vserver.is-closed-loop-disabled", "false");
58                 onset.AAI.put("generic-vnf.vnf-name", "testTriggerSource");
59                 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
60         }
61
62         @Test
63         public void testRetriesFail() {
64                 //
65                 // Load up the policy
66                 //
67                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
68                 onset.closedLoopControlName = pair.a.controlLoop.controlLoopName;
69                 try {
70                         //
71                         // Create a processor
72                         //
73                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
74                         //
75                         // create the manager
76                         //
77                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
78
79                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
80                         System.out.println(manager);
81                         //
82                         //
83                         //
84                         assertFalse(manager.isOperationComplete());
85                         assertFalse(manager.isOperationRunning());
86                         //
87                         // Start
88                         //
89                         Object request = manager.startOperation(onset);
90                         System.out.println(manager);
91                         assertNotNull(request);
92                         assertTrue(request instanceof Request);
93                         assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("1"));
94                         assertFalse(manager.isOperationComplete());
95                         assertTrue(manager.isOperationRunning());
96                         //
97                         // Accept
98                         //
99                         Response response = new Response((Request) request);
100                         response.Status.Code = ResponseCode.ACCEPT.getValue();
101                         response.Status.Value = ResponseValue.ACCEPT.toString();
102                         //
103                         //
104                         //
105                         PolicyResult result = manager.onResponse(response);
106                         System.out.println(manager);
107                         assertTrue(result == null);
108                         assertFalse(manager.isOperationComplete());
109                         assertTrue(manager.isOperationRunning());
110                         //
111                         // Now we are going to Fail it
112                         //
113                         response = new Response((Request) request);
114                         response.Status.Code = ResponseCode.FAILURE.getValue();
115                         response.Status.Value = ResponseValue.FAILURE.toString();
116                         response.Status.Description = "AppC failed for some reason";
117                         result = manager.onResponse(response);
118                         System.out.println(manager);
119                         assertTrue(result.equals(PolicyResult.FAILURE));
120                         assertFalse(manager.isOperationComplete());
121                         assertFalse(manager.isOperationRunning());
122                         //
123                         // Retry it
124                         //
125                         request = manager.startOperation(onset);
126                         System.out.println(manager);
127                         assertNotNull(request);
128                         assertTrue(request instanceof Request);
129                         assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("2"));
130                         assertFalse(manager.isOperationComplete());
131                         assertTrue(manager.isOperationRunning());
132                         //
133                         // 
134                         //
135                         response = new Response((Request) request);
136                         System.out.println(manager);
137                         response.Status.Code = ResponseCode.ACCEPT.getValue();
138                         response.Status.Value = ResponseValue.ACCEPT.toString();
139                         //
140                         //
141                         //
142                         result = manager.onResponse(response);
143                         System.out.println(manager);
144                         assertTrue(result == null);
145                         assertFalse(manager.isOperationComplete());
146                         assertTrue(manager.isOperationRunning());
147                         //
148                         // Now we are going to Fail it
149                         //
150                         response = new Response((Request) request);
151                         response.Status.Code = ResponseCode.FAILURE.getValue();
152                         response.Status.Value = ResponseValue.FAILURE.toString();
153                         response.Status.Description = "AppC failed for some reason";
154                         result = manager.onResponse(response);
155                         System.out.println(manager);
156                         assertTrue(result.equals(PolicyResult.FAILURE));
157                         //
158                         // Should be complete now
159                         //
160                         assertTrue(manager.isOperationComplete());
161                         assertFalse(manager.isOperationRunning());
162                         assertNotNull(manager.getOperationResult());
163                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
164                         assertTrue(manager.getHistory().size() == 2);
165                 } catch (ControlLoopException e) {
166                         fail(e.getMessage());
167                 }
168         }
169
170         @Test
171         public void testTimeout() {
172                 //
173                 // Load up the policy
174                 //
175                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
176                 onset.closedLoopControlName = pair.a.controlLoop.controlLoopName;
177                 try {
178                         //
179                         // Create a processor
180                         //
181                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
182                         //
183                         // create the manager
184                         //
185                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
186
187                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
188                         //
189                         //
190                         //
191                         System.out.println(manager);
192                         assertFalse(manager.isOperationComplete());
193                         assertFalse(manager.isOperationRunning());
194                         //
195                         // Start
196                         //
197                         Object request = manager.startOperation(onset);
198                         System.out.println(manager);
199                         assertNotNull(request);
200                         assertTrue(request instanceof Request);
201                         assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("1"));
202                         assertFalse(manager.isOperationComplete());
203                         assertTrue(manager.isOperationRunning());
204                         //
205                         // Accept
206                         //
207                         Response response = new Response((Request) request);
208                         response.Status.Code = ResponseCode.ACCEPT.getValue();
209                         response.Status.Value = ResponseValue.ACCEPT.toString();
210                         //
211                         //
212                         //
213                         PolicyResult result = manager.onResponse(response);
214                         System.out.println(manager);
215                         assertTrue(result == null);
216                         assertFalse(manager.isOperationComplete());
217                         assertTrue(manager.isOperationRunning());
218                         //
219                         // Now we are going to simulate Timeout
220                         //
221                         manager.setOperationHasTimedOut();
222                         System.out.println(manager);
223                         assertTrue(manager.isOperationComplete());
224                         assertFalse(manager.isOperationRunning());
225                         assertTrue(manager.getHistory().size() == 1);
226                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
227                         //
228                         // Now we are going to Fail the previous request
229                         //
230                         response = new Response((Request) request);
231                         response.Status.Code = ResponseCode.FAILURE.getValue();
232                         response.Status.Value = ResponseValue.FAILURE.toString();
233                         response.Status.Description = "AppC failed for some reason";
234                         result = manager.onResponse(response);
235                         System.out.println(manager);
236                         //
237                         //
238                         //
239                         assertTrue(manager.isOperationComplete());
240                         assertFalse(manager.isOperationRunning());
241                         assertTrue(manager.getHistory().size() == 1);
242                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
243                 } catch (ControlLoopException e) {
244                         fail(e.getMessage());
245                 }
246         }
247
248 }