Merge "Add support for AAI Named Query error handling"
[policy/drools-applications.git] / controlloop / common / eventmanager / src / test / java / org / onap / policy / controlloop / eventmanager / ControlLoopOperationManagerTest.java
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.aai.util.AAIException;
34 import org.onap.policy.appclcm.LCMRequest;
35 import org.onap.policy.appclcm.LCMRequestWrapper;
36 import org.onap.policy.appclcm.LCMResponse;
37 import org.onap.policy.appclcm.LCMResponseWrapper;
38 import org.onap.policy.controlloop.ControlLoopEventStatus;
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 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class ControlLoopOperationManagerTest {
49         private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
50         private static VirtualControlLoopEvent onset;
51         static {
52                 onset = new VirtualControlLoopEvent();
53                 onset.requestID = UUID.randomUUID();
54                 onset.target = "vserver.selflink";
55                 onset.closedLoopAlarmStart = Instant.now();
56                 onset.AAI = new HashMap<>();
57                 onset.AAI.put("cloud-region.identity-url", "foo");
58                 onset.AAI.put("vserver.selflink", "bar");
59                 onset.AAI.put("vserver.is-closed-loop-disabled", "false");
60                 onset.AAI.put("generic-vnf.vnf-name", "testTriggerSource");
61                 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
62         }
63
64         @Test
65         public void testRetriesFail() {
66                 //
67                 // Load up the policy
68                 //
69                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
70                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
71                 try {
72                         //
73                         // Create a processor
74                         //
75                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
76                         //
77                         // create the manager
78                         //
79                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
80
81                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
82                         logger.debug("{}",manager);
83                         //
84                         //
85                         //
86                         assertFalse(manager.isOperationComplete());
87                         assertFalse(manager.isOperationRunning());
88                         //
89                         // Start
90                         //
91                         Object request = manager.startOperation(onset);
92                         logger.debug("{}",manager);
93                         assertNotNull(request);
94                         assertTrue(request instanceof LCMRequestWrapper);
95                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
96                         LCMRequest appcRequest = dmaapRequest.getBody();
97                         assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
98                         assertFalse(manager.isOperationComplete());
99                         assertTrue(manager.isOperationRunning());
100                         //
101                         // Accept
102                         //
103                         LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
104                         LCMResponse appcResponse = new LCMResponse((LCMRequest) appcRequest);
105                         appcResponse.getStatus().setCode(100);
106                         appcResponse.getStatus().setMessage("ACCEPT");
107                         dmaapResponse.setBody(appcResponse);
108                         //
109                         //
110                         //
111                         PolicyResult result = manager.onResponse(dmaapResponse);
112                         logger.debug("{}",manager);
113                         assertTrue(result == null);
114                         assertFalse(manager.isOperationComplete());
115                         assertTrue(manager.isOperationRunning());
116                         //
117                         // Now we are going to Fail it
118                         //
119                         appcResponse = new LCMResponse(appcRequest);
120                         appcResponse.getStatus().setCode(401);
121                         appcResponse.getStatus().setMessage("AppC failed for some reason");
122                         dmaapResponse.setBody(appcResponse);
123                         result = manager.onResponse(dmaapResponse);
124                         logger.debug("{}",manager);
125                         assertTrue(result.equals(PolicyResult.FAILURE));
126                         assertFalse(manager.isOperationComplete());
127                         assertFalse(manager.isOperationRunning());
128                         //
129                         // Retry it
130                         //
131                         request = manager.startOperation(onset);
132                         logger.debug("{}",manager);
133                         assertNotNull(request);
134                         assertTrue(request instanceof LCMRequestWrapper);
135                         dmaapRequest = (LCMRequestWrapper) request;
136                         appcRequest = dmaapRequest.getBody();
137                         assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
138                         assertFalse(manager.isOperationComplete());
139                         assertTrue(manager.isOperationRunning());
140                         //
141                         // 
142                         //
143                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
144                         logger.debug("{}",manager);
145                         appcResponse.getStatus().setCode(100);
146                         appcResponse.getStatus().setMessage("ACCEPT");
147                         dmaapResponse.setBody(appcResponse);
148                         //
149                         //
150                         //
151                         result = manager.onResponse(dmaapResponse);
152                         logger.debug("{}",manager);
153                         assertTrue(result == null);
154                         assertFalse(manager.isOperationComplete());
155                         assertTrue(manager.isOperationRunning());
156                         //
157                         // Now we are going to Fail it
158                         //
159                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
160                         appcResponse.getStatus().setCode(401);
161                         appcResponse.getStatus().setMessage("AppC failed for some reason");
162                         dmaapResponse.setBody(appcResponse);
163                         result = manager.onResponse(dmaapResponse);
164                         logger.debug("{}",manager);
165                         assertTrue(result.equals(PolicyResult.FAILURE));
166                         //
167                         // Should be complete now
168                         //
169                         assertTrue(manager.isOperationComplete());
170                         assertFalse(manager.isOperationRunning());
171                         assertNotNull(manager.getOperationResult());
172                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
173                         assertTrue(manager.getHistory().size() == 2);
174                 } catch (ControlLoopException | AAIException e) {
175                         fail(e.getMessage());
176                 }
177         }
178
179         @Test
180         public void testTimeout() {
181                 //
182                 // Load up the policy
183                 //
184                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
185                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
186                 try {
187                         //
188                         // Create a processor
189                         //
190                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
191                         //
192                         // create the manager
193                         //
194                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
195
196                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
197                         //
198                         //
199                         //
200                         logger.debug("{}",manager);
201                         assertFalse(manager.isOperationComplete());
202                         assertFalse(manager.isOperationRunning());
203                         //
204                         // Start
205                         //
206                         Object request = manager.startOperation(onset);
207                         logger.debug("{}",manager);
208                         assertNotNull(request);
209                         assertTrue((request) instanceof LCMRequestWrapper);
210                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
211                         LCMRequest appcRequest = dmaapRequest.getBody();
212                         assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
213                         assertFalse(manager.isOperationComplete());
214                         assertTrue(manager.isOperationRunning());
215                         //
216                         // Accept
217                         //
218                         LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
219                         LCMResponse appcResponse = new LCMResponse(appcRequest);
220                 dmaapResponse.setBody(appcResponse);
221                         appcResponse.getStatus().setCode(100);
222                         appcResponse.getStatus().setMessage("ACCEPT");
223                         //
224                         //
225                         //
226                         PolicyResult result = manager.onResponse(dmaapResponse);
227                         logger.debug("{}",manager);
228                         assertTrue(result == null);
229                         assertFalse(manager.isOperationComplete());
230                         assertTrue(manager.isOperationRunning());
231                         //
232                         // Now we are going to simulate Timeout
233                         //
234                         manager.setOperationHasTimedOut();
235                         logger.debug("{}",manager);
236                         assertTrue(manager.isOperationComplete());
237                         assertFalse(manager.isOperationRunning());
238                         assertTrue(manager.getHistory().size() == 1);
239                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
240                         //
241                         // Now we are going to Fail the previous request
242                         //
243                         appcResponse = new LCMResponse(appcRequest);
244                         appcResponse.getStatus().setCode(401);
245                         appcResponse.getStatus().setMessage("AppC failed for some reason");
246                         dmaapResponse.setBody(appcResponse);
247                         result = manager.onResponse(dmaapResponse);
248                         logger.debug("{}",manager);
249                         //
250                         //
251                         //
252                         assertTrue(manager.isOperationComplete());
253                         assertFalse(manager.isOperationRunning());
254                         assertTrue(manager.getHistory().size() == 1);
255                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
256                 } catch (ControlLoopException | AAIException e) {
257                         fail(e.getMessage());
258                 }
259         }
260
261 }