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