Modify Unit Tests
[usecase-ui/server.git] / server / src / test / java / org / onap / usecaseui / server / controller / IntentControllerTest.java
1 /*
2  * Copyright (C) 2021 CTC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.usecaseui.server.controller;
17
18 import com.alibaba.fastjson.JSONArray;
19 import com.alibaba.fastjson.JSONObject;
20 import org.hibernate.Session;
21 import org.hibernate.SessionFactory;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.onap.usecaseui.server.bean.HttpResponseResult;
32 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
33 import org.onap.usecaseui.server.bean.intent.IntentModel;
34 import org.onap.usecaseui.server.service.intent.IntentInstanceService;
35 import org.onap.usecaseui.server.service.intent.impl.IntentServiceImpl;
36 import org.onap.usecaseui.server.util.HttpUtil;
37 import org.onap.usecaseui.server.util.Page;
38 import org.onap.usecaseui.server.util.UploadFileUtil;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.api.support.membermodification.MemberModifier;
41 import org.springframework.web.multipart.MultipartFile;
42
43 import jakarta.annotation.Resource;
44
45 import java.io.File;
46 import java.io.IOException;
47 import java.text.ParseException;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertTrue;
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.ArgumentMatchers.anyString;
57 import static org.mockito.ArgumentMatchers.eq;
58 import static org.mockito.Mockito.when;
59 import static org.powermock.api.mockito.PowerMockito.*;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class IntentControllerTest {
63
64     public IntentControllerTest(){}
65
66     @InjectMocks
67     private IntentController intentController;
68
69     @Mock
70     @Resource(name = "IntentService")
71     private IntentServiceImpl intentService;
72
73     @InjectMocks
74     private IntentServiceImpl intentService1;
75
76     @Mock
77     private IntentInstanceService intentInstanceService;
78
79     @Mock
80     private SessionFactory sessionFactory;
81     @Mock
82     private Session session;
83
84     @Before
85     public void before() throws IllegalAccessException {
86         MemberModifier.field(IntentController.class, "intentService").set(intentController , intentService);
87         MemberModifier.field(IntentController.class, "intentInstanceService").set(intentController , intentInstanceService);
88         MemberModifier.field(IntentServiceImpl.class, "sessionFactory").set(intentService1 , sessionFactory);
89     }
90
91     @BeforeClass
92     public static void init(){
93         Mockito.mockStatic(UploadFileUtil.class);
94         Mockito.mockStatic(HttpUtil.class,"IntentControllerTest");
95     }
96
97     @Test
98     public void uploadModelTest() throws Exception {
99         MultipartFile file=PowerMockito.mock(MultipartFile.class);
100         PowerMockito.when(file.getOriginalFilename()).thenReturn("filename.zip");
101         IntentController spy = PowerMockito.spy(intentController);
102         File dest=PowerMockito.mock(File.class);
103         when(spy.newFile(anyString())).thenReturn(dest);
104         File parent=PowerMockito.mock(File.class);
105         when(dest.getParentFile()).thenReturn(parent);
106         when(parent.mkdirs()).thenReturn(true);
107         doNothing().when(file).transferTo(dest);
108         when(dest.length()).thenReturn(1024L);
109         when(UploadFileUtil.formUpload(anyString(), any(Map.class), any(Map.class),anyString())).thenReturn("ok");
110         when(intentService.addModel(any(IntentModel.class))).thenReturn("1");
111         assertEquals(spy.uploadModel(file, "5gs"), "1");
112     }
113     @Test
114     public void uploadModelTestThrowError() throws Exception {
115         MultipartFile file=PowerMockito.mock(MultipartFile.class);
116         PowerMockito.when(file.getOriginalFilename()).thenReturn("filename.zip");
117         IntentController spy = PowerMockito.spy(intentController);
118         File dest=PowerMockito.mock(File.class);
119         when(spy.newFile(anyString())).thenReturn(dest);
120         File parent=PowerMockito.mock(File.class);
121         when(dest.getParentFile()).thenReturn(parent);
122         when(parent.mkdirs()).thenReturn(true);
123         doThrow(new RuntimeException()).when(file).transferTo(dest);
124
125         assertEquals(spy.uploadModel(file, "5gs"), "0");
126
127     }
128
129
130     @Test
131     public void activeModelTest() {
132         IntentModel model = new IntentModel();
133         String path = "path";
134         String modelId = "1";
135         when(intentService.activeModel(anyString())).thenReturn(model);
136         when(intentService.activeModelFile(model)).thenReturn(path);
137
138         HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
139         Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(), anyString())).thenReturn(mock);
140         assertEquals(intentController.activeModel(modelId), "1");
141     }
142
143     @Test
144     public void deleteModelTest() throws Exception {
145         String modelId = "1";
146         IntentModel model = new IntentModel();
147         model.setModelName("filename.zip");
148         when(intentService.getModel(eq(modelId))).thenReturn(model);
149         when(intentService.deleteModel(anyString())).thenReturn("1");
150
151         File file=PowerMockito.mock(File.class);
152         IntentController spy = PowerMockito.spy(intentController);
153         when(spy.newFile(anyString())).thenReturn(file);
154         PowerMockito.when(file.exists()).thenReturn(true);
155         PowerMockito.when(file.delete()).thenReturn(true);
156
157         HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
158         when(HttpUtil.sendGetRequest(anyString(), any(Map.class))).thenReturn(mock);
159         when(mock.getResultContent()).thenReturn("{}");
160
161         assertEquals(spy.deleteModel(modelId), "1");
162
163     }
164
165     @Test
166     public void predictTest() throws ParseException {
167         Map<String,Object> body = new HashMap<>();
168         body.put("text", "text");
169         body.put("modelType", "5gs");
170         String respContent = "";
171         HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
172         Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock);
173         when(mock.getResultContent()).thenReturn("{'Area':'chengnan'}");
174         when(intentService.calcFieldValue(anyString(), anyString())).thenReturn("Beijing Changping District Chengnan Street");
175         when(intentService.getActiveModelType()).thenReturn("5gs");
176         Map<String, Object> predict = intentController.predict(body);
177         JSONObject jsonObject = new JSONObject(predict);
178
179         assertEquals(jsonObject.getString("coverageArea"), "Beijing Changping District Chengnan Street");
180     }
181
182     @Test
183     public void unifyPredict_5gs_Test() throws ParseException {
184         Map<String,Object> body = new HashMap<>();
185         body.put("text", "Service");
186         String respContent = "";
187         when(intentService.getModelTypeByIntentText(anyString())).thenReturn("5gs");
188         when(intentService.getActiveModelType()).thenReturn("5gs");
189
190         HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
191         Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock);
192         when(mock.getResultContent()).thenReturn("{'Area':'chengnan'}");
193         when(intentService.calcFieldValue(anyString(), anyString())).thenReturn("Beijing Changping District Chengnan Street");
194         Map<String, Object> predict = intentController.unifyPredict(body);
195         JSONObject jsonObject = new JSONObject(predict);
196
197         assertEquals(jsonObject.getString("type"), "5gs");
198         assertEquals(jsonObject.getJSONObject("formData").getString("coverageArea"), "Beijing Changping District Chengnan Street");
199     }
200     @Test
201     public void unifyPredict_ccvpn_Test() throws ParseException {
202         Map<String,Object> body = new HashMap<>();
203         body.put("text", "I need create a Cloud Leased Line, I need a line from Access two to Cloud one, 20Gbps");
204         String respContent = "";
205         when(intentService.getModelTypeByIntentText(anyString())).thenReturn("ccvpn");
206         when(intentService.getActiveModelType()).thenReturn("ccvpn");
207
208         HttpResponseResult mock = Mockito.mock(HttpResponseResult.class);
209         Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock);
210         when(mock.getResultContent()).thenReturn("{'access point':'','cloud point':'','bandwidth':''}");
211         when(intentInstanceService.formatAccessPoint(anyString())).thenReturn("");
212         when(intentInstanceService.formatCloudPoint(anyString())).thenReturn("");
213         Map<String, Object> predict = intentController.unifyPredict(body);
214         JSONObject jsonObject = new JSONObject(predict);
215
216
217         assertEquals(jsonObject.getString("type"), "ccvpn");
218         Assert.assertNotNull(jsonObject.getJSONObject("formData").getJSONObject("accessPointOne").getString("name"));
219         Assert.assertNotNull(jsonObject.getJSONObject("formData").getString("cloudPointName"));
220     }
221
222     @Test
223     public void getInstanceId() {
224         assertEquals(intentController.getInstanceId().containsKey("instanceId"), true);
225     }
226     @Test
227     public void getInstanceList() {
228         Map<String, Object> body = new HashMap<>();
229
230         body.put("currentPage",1);
231         body.put("pageSize",2);
232         Page<CCVPNInstance> page = new Page<>();
233         CCVPNInstance ccvpnInstance = new CCVPNInstance();
234         ccvpnInstance.setAccessPointOneName("xx");
235         ccvpnInstance.setCloudPointName("bb");
236         page.setList(List.of(ccvpnInstance));
237         Mockito.when(intentInstanceService.queryIntentInstance(null,1,2)).thenReturn(page);
238         Assert.assertNotNull(intentController.getInstanceList(body));
239     }
240     @Test
241     public void createIntentInstance() throws IOException {
242         Map<String, Object> body = new HashMap<>();
243         body.put("instanceId","instanceId");
244         body.put("name","name");
245         body.put("lineNum","lineNum");
246         body.put("cloudPointName","cloudPointName");
247         Map<String, Object> accessPointOne = new HashMap<>();
248         accessPointOne.put("name","name");
249         accessPointOne.put("bandwidth","1");
250         body.put("accessPointOne",accessPointOne);
251         Mockito.when(intentInstanceService.createCCVPNInstance(any())).thenReturn(1);
252         assertEquals(intentController.createCCVPNInstance(body), "OK");
253     }
254     @Test
255     public void getFinishedInstanceInfo() {
256         List<CCVPNInstance> instanceList = new ArrayList<>();
257         CCVPNInstance instance = new CCVPNInstance();
258         instance.setInstanceId("instanceId");
259         instance.setName("name");
260         instanceList.add(instance);
261         Mockito.when(intentInstanceService.getFinishedInstanceInfo()).thenReturn(instanceList);
262         assertEquals(((List)intentController.getFinishedInstanceInfo()).size(), 1);
263     }
264     @Test
265     public void deleteIntentInstance() {
266         Map<String, Object> body = new HashMap<>();
267         body.put("instanceId", "instanceId");
268         Mockito.doNothing().when(intentInstanceService).deleteIntentInstance(anyString());
269         assertEquals(intentController.deleteIntentInstance("instanceId"), "ok");
270     }
271     @Test
272     public void activeIntentInstance() {
273         Map<String, Object> body = new HashMap<>();
274         body.put("instanceId", "instanceId");
275         Mockito.doNothing().when(intentInstanceService).activeIntentInstance(anyString());
276         assertEquals(intentController.activeIntentInstance(body), "ok");
277     }
278     @Test
279     public void invalidIntentInstance() {
280         Map<String, Object> body = new HashMap<>();
281         body.put("instanceId", "instanceId");
282         Mockito.doNothing().when(intentInstanceService).invalidIntentInstance(anyString());
283         assertEquals(intentController.invalidIntentInstance(body), "ok");
284     }
285     @Test
286     public void queryInstancePerformanceData() {
287         Map<String, Object> body = new HashMap<>();
288         body.put("instanceId", "instanceId");
289         Mockito.when(intentInstanceService.queryInstancePerformanceData(anyString())).thenReturn(body);
290         assertEquals(intentController.queryInstancePerformanceData(body), body);
291     }
292     @Test
293     public void queryAccessNodeInfoTest() throws IOException {
294         Mockito.when(intentInstanceService.queryAccessNodeInfo()).thenReturn("ok");
295         assertEquals(intentController.queryAccessNodeInfo(), "ok");
296     }
297
298     @Test
299     public void getInstanceStatusTest() {
300         Map<String, Object> body = new HashMap<>();
301         List<String> ids = new ArrayList<>();
302         ids.add("1");
303         ids.add("2");
304         ids.add("3");
305         body.put("ids", ids);
306         when(intentInstanceService.getInstanceStatus(any(JSONArray.class))).thenReturn(new JSONObject());
307         assertTrue(intentController.getInstanceStatus(body) instanceof JSONObject);
308     }
309     @Test
310     public void loadTest() {
311         HttpResponseResult result = Mockito.mock(HttpResponseResult.class);
312         PowerMockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(), anyString())).thenReturn(result);
313         PowerMockito.when(result.getResultContent()).thenReturn("{\"Status\":\"OK\"}");
314         assertEquals(intentService1.load("filename"), "OK");
315     }
316 }