2 * Copyright (C) 2021 CTC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.server.controller;
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;
43 import jakarta.annotation.Resource;
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;
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.*;
61 @RunWith(MockitoJUnitRunner.class)
62 public class IntentControllerTest {
64 public IntentControllerTest(){}
67 private IntentController intentController;
70 @Resource(name = "IntentService")
71 private IntentServiceImpl intentService;
74 private IntentServiceImpl intentService1;
77 private IntentInstanceService intentInstanceService;
80 private SessionFactory sessionFactory;
82 private Session session;
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);
92 public static void init(){
93 Mockito.mockStatic(UploadFileUtil.class);
94 Mockito.mockStatic(HttpUtil.class,"IntentControllerTest");
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");
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);
125 assertEquals(spy.uploadModel(file, "5gs"), "0");
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);
138 HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
139 Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(), anyString())).thenReturn(mock);
140 assertEquals(intentController.activeModel(modelId), "1");
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");
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);
157 HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
158 when(HttpUtil.sendGetRequest(anyString(), any(Map.class))).thenReturn(mock);
159 when(mock.getResultContent()).thenReturn("{}");
161 assertEquals(spy.deleteModel(modelId), "1");
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);
179 assertEquals(jsonObject.getString("coverageArea"), "Beijing Changping District Chengnan Street");
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");
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);
197 assertEquals(jsonObject.getString("type"), "5gs");
198 assertEquals(jsonObject.getJSONObject("formData").getString("coverageArea"), "Beijing Changping District Chengnan Street");
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");
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);
217 assertEquals(jsonObject.getString("type"), "ccvpn");
218 Assert.assertNotNull(jsonObject.getJSONObject("formData").getJSONObject("accessPointOne").getString("name"));
219 Assert.assertNotNull(jsonObject.getJSONObject("formData").getString("cloudPointName"));
223 public void getInstanceId() {
224 assertEquals(intentController.getInstanceId().containsKey("instanceId"), true);
227 public void getInstanceList() {
228 Map<String, Object> body = new HashMap<>();
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));
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");
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);
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");
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");
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");
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);
293 public void queryAccessNodeInfoTest() throws IOException {
294 Mockito.when(intentInstanceService.queryAccessNodeInfo()).thenReturn("ok");
295 assertEquals(intentController.queryAccessNodeInfo(), "ok");
299 public void getInstanceStatusTest() {
300 Map<String, Object> body = new HashMap<>();
301 List<String> ids = new ArrayList<>();
305 body.put("ids", ids);
306 when(intentInstanceService.getInstanceStatus(any(JSONArray.class))).thenReturn(new JSONObject());
307 assertTrue(intentController.getInstanceStatus(body) instanceof JSONObject);
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");