Remove drools PDP dependency
[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 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 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35
36 import java.util.UUID;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.rest.RestManager;
41 import org.onap.policy.rest.RestManager.Pair;
42 import org.onap.policy.sdnc.SdncManager.SdncCallback;
43 import org.onap.policy.sdnc.util.Serialization;
44
45 public class SdncManagerTest implements SdncCallback {
46     private RestManager   mockedRestManager;
47
48     private Pair<Integer, String> httpResponsePutOk;
49     private Pair<Integer, String> httpResponseGetOk;
50     private Pair<Integer, String> httpResponseBadResponse;
51     private Pair<Integer, String> httpResponseErr;
52
53     private SdncRequest  request;
54     private SdncResponse response;
55
56     @BeforeClass
57     public static void beforeTestSdncManager() {
58     }
59
60     /**
61      * Set up the mocked REST manager.
62      */
63     @Before
64     public void setupMockedRest() {
65         mockedRestManager   = mock(RestManager.class);
66
67         httpResponsePutOk       = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
68         httpResponseGetOk       = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
69         httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
70         httpResponseErr         = mockedRestManager.new Pair<>(200, null);
71     }
72
73     /**
74      * Create the request and response before.
75      */
76     @Before
77     public void createRequestAndResponse() {
78         SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
79         serviceInfo.setServiceInstanceId("E-City");
80
81         SdncHealRequestHeaderInfo additionalParams = new SdncHealRequestHeaderInfo();
82         additionalParams.setSvcAction("Go Home");
83         additionalParams.setSvcRequestId("My Request");
84
85         SdncHealRequest healRequest = new SdncHealRequest();
86         healRequest.setRequestHeaderInfo(additionalParams);
87         healRequest.setServiceInfo(serviceInfo);
88
89         UUID requestId = UUID.randomUUID();
90         request = new SdncRequest();
91         request.setRequestId(requestId);
92         request.setHealRequest(healRequest);
93         request.setNsInstanceId("Dorothy");
94
95         SdncResponseOutput responseDescriptor = new SdncResponseOutput();
96         responseDescriptor.setSvcRequestId("1234");
97         responseDescriptor.setResponseCode("200");
98         responseDescriptor.setAckFinalIndicator("final-indicator-00");
99
100         response = new SdncResponse();
101         response.setRequestId(request.getRequestId().toString());
102         response.setResponseOutput(responseDescriptor);
103     }
104
105     @Test
106     public void testSdncInitiation() throws InterruptedException {
107         try {
108             new SdncManager(null, null, null, null, null);
109             fail("test should throw an exception here");
110         }
111         catch (IllegalArgumentException e) {
112             assertEquals(
113                 "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null",
114                 e.getMessage()
115             );
116         }
117
118         try {
119             new SdncManager(this, null, null, null, null);
120             fail("test should throw an exception here");
121         }
122         catch (IllegalArgumentException e) {
123             assertEquals(
124                 "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null",
125                 e.getMessage()
126             );
127         }
128
129         try {
130             new SdncManager(this, request, null, null, null);
131             fail("test should throw an exception here");
132         }
133         catch (IllegalArgumentException e) {
134             assertEquals(
135                 "the \"url\" parameter on the SdncManager constructor may not be null",
136                 e.getMessage()
137             );
138         }
139
140         new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Toto");
141     }
142
143     @Test
144     public void testSdncExecutionException() throws InterruptedException {
145         SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Exception");
146         manager.setRestManager(mockedRestManager);
147
148         Thread managerThread = new Thread(manager);
149         managerThread.start();
150
151         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Exception"), anyMap(), anyString(), anyString()))
152             .thenThrow(new RuntimeException("OzException"));
153
154
155         managerThread.join(100);
156     }
157
158     @Test
159     public void testSdncExecutionNull() throws InterruptedException {
160         SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Null");
161         manager.setRestManager(mockedRestManager);
162
163         Thread managerThread = new Thread(manager);
164         managerThread.start();
165
166         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
167             .thenReturn(null);
168
169         managerThread.join(100);
170     }
171
172
173     @Test
174     public void testSdncExecutionError0() throws InterruptedException {
175         SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Error0");
176         manager.setRestManager(mockedRestManager);
177
178         Thread managerThread = new Thread(manager);
179         managerThread.start();
180
181         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
182             .thenReturn(httpResponseErr);
183
184         managerThread.join(100);
185     }
186
187     @Test
188     public void testSdncExecutionBadResponse() throws InterruptedException {
189         SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "BadResponse");
190         manager.setRestManager(mockedRestManager);
191
192         Thread managerThread = new Thread(manager);
193         managerThread.start();
194
195         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
196             .thenReturn(httpResponseBadResponse);
197
198         managerThread.join(100);
199     }
200
201     @Test
202     public void testSdncExecutionOk() throws InterruptedException {
203         SdncManager manager = new SdncManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "OOK");
204         manager.setRestManager(mockedRestManager);
205
206         Thread managerThread = new Thread(manager);
207         managerThread.start();
208
209         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
210             .thenReturn(httpResponsePutOk);
211
212         when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
213             .thenReturn(httpResponseGetOk);
214
215
216         managerThread.join(100);
217     }
218
219     @Override
220     public void onCallback(SdncResponse response) {
221         //
222         // Nothing really to do
223         //
224     }
225 }