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.JSON;
19 import com.alibaba.fastjson.JSONObject;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.onap.usecaseui.server.bean.HttpResponseResult;
27 import org.onap.usecaseui.server.bean.intent.IntentInstance;
28 import org.onap.usecaseui.server.bean.intent.IntentModel;
29 import org.onap.usecaseui.server.service.intent.IntentInstanceService;
30 import org.onap.usecaseui.server.service.intent.IntentService;
31 import org.onap.usecaseui.server.util.HttpUtil;
32 import org.powermock.api.mockito.PowerMockito;
33 import org.powermock.api.support.membermodification.MemberModifier;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
37 import javax.annotation.Resource;
39 import java.io.IOException;
40 import java.lang.reflect.InvocationTargetException;
41 import java.lang.reflect.Method;
42 import java.text.ParseException;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
48 import static org.junit.Assert.assertEquals;
49 import static org.mockito.ArgumentMatchers.any;
50 import static org.mockito.ArgumentMatchers.anyString;
51 import static org.powermock.api.mockito.PowerMockito.when;
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({HttpUtil.class})
55 public class IntentControllerTest {
57 public IntentControllerTest(){}
60 private IntentController intentController;
63 @Resource(name = "IntentService")
64 private IntentService intentService;
67 private IntentInstanceService intentInstanceService;
70 public void before() throws IllegalAccessException {
71 MemberModifier.field(IntentController.class, "intentService").set(intentController , intentService);
72 MemberModifier.field(IntentController.class, "intentInstanceService").set(intentController , intentInstanceService);
77 public void activeModelTest() {
78 IntentModel model = new IntentModel();
81 when(intentService.activeModel(anyString())).thenReturn(model);
82 when(intentService.activeModelFile(model)).thenReturn(path);
84 HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
85 PowerMockito.mockStatic(HttpUtil.class);
86 Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock);
87 when(mock.getResultContent()).thenReturn("{'Status':'Success'}");
89 assertEquals(intentController.activeModel(modelId), "1");
93 public void deleteModelTest() throws Exception {
95 IntentModel model = new IntentModel();
96 model.setModelName("filename.zip");
97 when(intentService.getModel(anyString())).thenReturn(model);
98 when(intentService.deleteModel(anyString())).thenReturn("1");
100 File file=PowerMockito.mock(File.class);
101 PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(file);
102 PowerMockito.when(file.exists()).thenReturn(true);
103 PowerMockito.when(file.delete()).thenReturn(true);
105 assertEquals(intentController.deleteModel(modelId), "1");
110 public void predictTest() throws ParseException {
111 Map<String,Object> body = new HashMap<>();
112 body.put("text", "text");
113 String respContent = "";
114 HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class);
115 PowerMockito.mockStatic(HttpUtil.class);
116 Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock);
117 when(mock.getResultContent()).thenReturn("{'Region':'chengnan'}");
118 when(intentService.calcFieldValue(anyString(), anyString())).thenReturn("Beijing Changping District Chengnan Street");
119 String predict = intentController.predict(body);
120 JSONObject jsonObject = JSON.parseObject(predict);
122 assertEquals(jsonObject.getString("coverageArea"), "Beijing Changping District Chengnan Street");
126 public void tranlateFieldNameTest() throws InvocationTargetException, IllegalAccessException {
127 String key = "Region";
128 IntentController spy = PowerMockito.spy(intentController);
129 Method method = PowerMockito.method(IntentController.class, "tranlateFieldName", String.class);
130 Object result = method.invoke(spy, key);
131 assertEquals(result, "coverageArea");
134 public void getInstanceId() {
135 assertEquals(intentController.getInstanceId().containsKey("instanceId"), true);
138 public void getInstanceList() {
139 Map<String, Object> body = new HashMap<>();
141 body.put("currentPage",1);
142 body.put("pageSize",2);
143 Mockito.when(intentInstanceService.queryIntentInstance(null,1,2)).thenReturn(null);
144 assertEquals(intentController.getInstanceList(body), null);
147 public void createIntentInstance() throws IOException {
148 Map<String, Object> body = new HashMap<>();
149 body.put("instanceId","instanceId");
150 body.put("name","name");
151 body.put("lineNum","lineNum");
152 body.put("cloudPointName","cloudPointName");
153 Map<String, Object> accessPointOne = new HashMap<>();
154 accessPointOne.put("name","name");
155 accessPointOne.put("bandwidth","1");
156 body.put("accessPointOne",accessPointOne);
157 Mockito.when(intentInstanceService.createIntentInstance(any())).thenReturn(1);
158 assertEquals(intentController.createIntentInstance(body), "OK");
161 public void getFinishedInstanceInfo() {
162 List<IntentInstance> instanceList = new ArrayList<>();
163 IntentInstance instance = new IntentInstance();
164 instance.setInstanceId("instanceId");
165 instance.setName("name");
166 instanceList.add(instance);
167 Mockito.when(intentInstanceService.getFinishedInstanceInfo()).thenReturn(instanceList);
168 assertEquals(((List)intentController.getFinishedInstanceInfo()).size(), 1);
171 public void deleteIntentInstance() {
172 Map<String, Object> body = new HashMap<>();
173 body.put("instanceId", "instanceId");
174 Mockito.doNothing().when(intentInstanceService).deleteIntentInstance(anyString());
175 assertEquals(intentController.deleteIntentInstance(body), "ok");
178 public void activeIntentInstance() {
179 Map<String, Object> body = new HashMap<>();
180 body.put("instanceId", "instanceId");
181 Mockito.doNothing().when(intentInstanceService).activeIntentInstance(anyString());
182 assertEquals(intentController.activeIntentInstance(body), "ok");
185 public void invalidIntentInstance() {
186 Map<String, Object> body = new HashMap<>();
187 body.put("instanceId", "instanceId");
188 Mockito.doNothing().when(intentInstanceService).invalidIntentInstance(anyString());
189 assertEquals(intentController.invalidIntentInstance(body), "ok");
192 public void queryInstancePerformanceData() {
193 Map<String, Object> body = new HashMap<>();
194 body.put("instanceId", "instanceId");
195 Mockito.when(intentInstanceService.queryInstancePerformanceData(anyString())).thenReturn(body);
196 assertEquals(intentController.queryInstancePerformanceData(body), body);
199 public void queryAccessNodeInfoTest() throws IOException {
200 Mockito.when(intentInstanceService.queryAccessNodeInfo()).thenReturn("ok");
201 assertEquals(intentController.queryAccessNodeInfo(), "ok");