Merge "Add debugging of REST call"
[policy/drools-applications.git] / controlloop / common / model-impl / sdnc / src / test / java / org / onap / policy / sdnc / SdncManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * sdnc
4  * ================================================================================
5  * Copyright (C) 2018 Huawei. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.sdnc;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27
28 import static org.mockito.ArgumentMatchers.anyMap;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.ArgumentMatchers.endsWith;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.ArgumentMatchers.startsWith;
33
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.UUID;
40
41 import org.drools.core.WorkingMemory;
42 import org.junit.After;
43 import org.junit.AfterClass;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.onap.policy.drools.system.PolicyEngine;
48 import org.onap.policy.rest.RestManager;
49 import org.onap.policy.rest.RestManager.Pair;
50 import org.onap.policy.sdnc.util.Serialization;
51
52 public class SdncManagerTest {
53     private static WorkingMemory mockedWorkingMemory;
54
55     private RestManager   mockedRestManager;
56
57     private Pair<Integer, String> httpResponsePutOk;
58     private Pair<Integer, String> httpResponseGetOk;
59     private Pair<Integer, String> httpResponseBadResponse;
60     private Pair<Integer, String> httpResponseErr;
61
62     private SdncRequest  request;
63     private SdncResponse response;
64
65     @BeforeClass
66     public static void beforeTestSdncManager() {
67         mockedWorkingMemory = mock(WorkingMemory.class);
68     }
69
70     /**
71      * Set up the mocked REST manager.
72      */
73     @Before
74     public void setupMockedRest() {
75         mockedRestManager   = mock(RestManager.class);
76         
77         httpResponsePutOk       = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
78         httpResponseGetOk       = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
79         httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
80         httpResponseErr         = mockedRestManager.new Pair<>(200, null);
81     }
82     
83     /**
84      * Create the request and response before.
85      */
86     @Before
87     public void createRequestAndResponse() {
88         SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
89         serviceInfo.setServiceInstanceId("E-City");
90
91         SdncHealRequestHeaderInfo additionalParams = new SdncHealRequestHeaderInfo();
92         additionalParams.setSvcAction("Go Home");
93         additionalParams.setSvcRequestId("My Request");
94     
95         SdncHealRequest healRequest = new SdncHealRequest();
96         healRequest.setRequestHeaderInfo(additionalParams);
97         healRequest.setServiceInfo(serviceInfo);
98
99         UUID requestId = UUID.randomUUID();
100         request = new SdncRequest();
101         request.setRequestId(requestId);
102         request.setHealRequest(healRequest);
103         request.setNsInstanceId("Dorothy");
104
105         SdncResponseOutput responseDescriptor = new SdncResponseOutput();
106         responseDescriptor.setSvcRequestId("1234");
107         responseDescriptor.setResponseCode("200");
108         responseDescriptor.setAckFinalIndicator("final-indicator-00");
109
110         response = new SdncResponse();
111         response.setRequestId(request.getRequestId().toString());
112         response.setResponseOutput(responseDescriptor);
113     }
114     
115     /**
116      * After Test clean up.
117      */
118     @After
119     public void afterTestSdncManager() throws InterruptedException {
120         PolicyEngine.manager.getEnvironment().remove("sdnc.password");
121         PolicyEngine.manager.getEnvironment().remove("sdnc.username");
122         PolicyEngine.manager.getEnvironment().remove("sdnc.url");
123     }
124
125     @Test
126     public void testSdncInitiation() throws InterruptedException {
127         try {
128             new SdncManager(null, null);
129             fail("test should throw an exception here");
130         }
131         catch (IllegalArgumentException e) {
132             assertEquals(
133                 "the parameters \"wm\" and \"request\" on the SdncManager constructor may not be null", 
134                 e.getMessage()
135             );
136         }
137     
138         try {
139             new SdncManager(mockedWorkingMemory, null);
140             fail("test should throw an exception here");
141         }
142         catch (IllegalArgumentException e) {
143             assertEquals(
144                 "the parameters \"wm\" and \"request\" on the SdncManager constructor may not be null", 
145                 e.getMessage()
146             );
147         }
148         
149         try {
150             new SdncManager(mockedWorkingMemory, request);
151             fail("test should throw an exception here");
152         }
153         catch (IllegalArgumentException e) {
154             assertEquals(
155                 "The value of policy engine manager environment property \"sdnc.url\" may not be null", 
156                 e.getMessage()
157             );
158         }
159         
160         PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
161         try {
162             new SdncManager(mockedWorkingMemory, request);
163             fail("test should throw an exception here");
164         }
165         catch (IllegalArgumentException e) {
166             assertEquals(
167                 "The value of policy engine manager environment property \"sdnc.username\" may not be null", 
168                 e.getMessage()
169             );
170         }
171         
172         PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
173         try {
174             new SdncManager(mockedWorkingMemory, request);
175             fail("test should throw an exception here");
176         }
177         catch (IllegalArgumentException e) {
178             assertEquals(
179                 "The value of policy engine manager environment property \"sdnc.password\" may not be null", 
180                 e.getMessage()
181             );
182         }
183         
184         PolicyEngine.manager.getEnvironment().put("sdnc.password", "Toto");
185         new SdncManager(mockedWorkingMemory, request);
186     }
187
188     @Test
189     public void testSdncExecutionException() throws InterruptedException {
190         PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
191         PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
192         PolicyEngine.manager.getEnvironment().put("sdnc.password", "Exception");
193
194         SdncManager manager = new SdncManager(mockedWorkingMemory, request);
195         manager.setRestManager(mockedRestManager);
196
197         Thread managerThread = new Thread(manager);
198         managerThread.start();
199
200         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Exception"), anyMap(), anyString(), anyString()))
201             .thenThrow(new RuntimeException("OzException"));
202         
203
204         managerThread.join(100);
205     }
206     
207     @Test
208     public void testSdncExecutionNull() throws InterruptedException {
209         PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
210         PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
211         PolicyEngine.manager.getEnvironment().put("sdnc.password", "Null");
212
213         SdncManager manager = new SdncManager(mockedWorkingMemory, request);
214         manager.setRestManager(mockedRestManager);
215
216         Thread managerThread = new Thread(manager);
217         managerThread.start();
218         
219         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
220             .thenReturn(null);
221         
222         managerThread.join(100);
223     }
224
225
226     @Test
227     public void testSdncExecutionError0() throws InterruptedException {
228         PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
229         PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
230         PolicyEngine.manager.getEnvironment().put("sdnc.password", "Error0");
231
232         SdncManager manager = new SdncManager(mockedWorkingMemory, request);
233         manager.setRestManager(mockedRestManager);
234         
235         Thread managerThread = new Thread(manager);
236         managerThread.start();
237         
238         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
239             .thenReturn(httpResponseErr);
240         
241         managerThread.join(100);
242     }
243
244     @Test
245     public void testSdncExecutionBadResponse() throws InterruptedException {
246         PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
247         PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
248         PolicyEngine.manager.getEnvironment().put("sdnc.password", "BadResponse");
249
250         SdncManager manager = new SdncManager(mockedWorkingMemory, request);
251         manager.setRestManager(mockedRestManager);
252         
253         Thread managerThread = new Thread(manager);
254         managerThread.start();
255         
256         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
257             .thenReturn(httpResponseBadResponse);
258         
259         managerThread.join(100);
260     }
261     
262     @Test
263     public void testSdncExecutionOk() throws InterruptedException {
264         PolicyEngine.manager.getEnvironment().put("sdnc.url", "http://somewhere.over.the.rainbow");
265         PolicyEngine.manager.getEnvironment().put("sdnc.username", "Dorothy");
266         PolicyEngine.manager.getEnvironment().put("sdnc.password", "OK");
267         
268         SdncManager manager = new SdncManager(mockedWorkingMemory, request);
269         manager.setRestManager(mockedRestManager);
270         
271         Thread managerThread = new Thread(manager);
272         managerThread.start();
273
274         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
275             .thenReturn(httpResponsePutOk);
276         
277         when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
278             .thenReturn(httpResponseGetOk);
279         
280
281         managerThread.join(100);
282     }
283 }