From 126a9c78005bc2d5785330f6d091f32a20848122 Mon Sep 17 00:00:00 2001 From: zhaoyehua Date: Thu, 27 May 2021 09:45:07 +0800 Subject: add test class for Intent module Issue-ID: USECASEUI-525 Change-Id: I2b746d0df3935580f59a24e4c2ce7427331da4c0 Signed-off-by: zhaoyehua --- server/pom.xml | 4 +- .../server/controller/IntentControllerTest.java | 117 ++++++++++++++++++ .../service/intent/impl/IntentServiceImplTest.java | 134 +++++++++++++++++++++ 3 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java create mode 100644 server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java diff --git a/server/pom.xml b/server/pom.xml index f167dd7e..8ac1e555 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -323,7 +323,7 @@ org.powermock powermock-api-mockito2 - 2.0.7 + 2.0.2 org.mockito @@ -333,7 +333,7 @@ org.powermock powermock-module-junit4 - 1.6.5 + 2.0.2 jar compile diff --git a/server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java b/server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java new file mode 100644 index 00000000..b970416a --- /dev/null +++ b/server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2017 CTC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.usecaseui.server.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.ParseException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.usecaseui.server.bean.HttpResponseResult; +import org.onap.usecaseui.server.bean.intent.IntentModel; +import org.onap.usecaseui.server.service.intent.IntentService; +import org.onap.usecaseui.server.util.HttpUtil; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.powermock.api.mockito.PowerMockito.when; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({IntentController.class, HttpUtil.class}) +class IntentControllerTest { + + public IntentControllerTest(){} + + @InjectMocks + private IntentController intentController; + + @Mock + private IntentService intentService; + + @Test + public void activeModelTest() { + IntentModel model = new IntentModel(); + String path = "path"; + String modelId = "1"; + when(intentService.activeModel(anyString())).thenReturn(model); + when(intentService.activeModelFile(model)).thenReturn(path); + + HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class); + PowerMockito.mockStatic(HttpUtil.class); + Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock); + when(mock.getResultContent()).thenReturn("{'Status':'Success'}"); + + assertEquals(intentController.activeModel(modelId), "1"); + } + + @Test + public void deleteModelTest() throws Exception { + String modelId = "1"; + IntentModel model = new IntentModel(); + model.setModelName("filename.zip"); + when(intentService.getModel(anyString())).thenReturn(model); + when(intentService.deleteModel(anyString())).thenReturn("1"); + + File file=PowerMockito.mock(File.class); + PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(file); + PowerMockito.when(file.exists()).thenReturn(true); + PowerMockito.when(file.delete()).thenReturn(true); + + assertEquals(intentController.deleteModel(modelId), "1"); + + } + + @Test + public void predictTest() throws ParseException { + Map body = new HashMap<>(); + body.put("text", "text"); + String respContent = ""; + HttpResponseResult mock = PowerMockito.mock(HttpResponseResult.class); + PowerMockito.mockStatic(HttpUtil.class); + Mockito.when(HttpUtil.sendPostRequestByJson(anyString(), any(Map.class), anyString())).thenReturn(mock); + when(mock.getResultContent()).thenReturn("{'Region':'chengnan'}"); + when(intentService.calcFieldValue(anyString(), anyString())).thenReturn("Beijing Changping District Chengnan Street"); + String predict = intentController.predict(body); + JSONObject jsonObject = JSON.parseObject(predict); + + assertEquals(jsonObject.getString("coverageArea"), "Beijing Changping District Chengnan Street"); + } + + @Test + public void tranlateFieldNameTest() throws InvocationTargetException, IllegalAccessException { + String key = "Region"; + IntentController spy = PowerMockito.spy(intentController); + Method method = PowerMockito.method(IntentController.class, "tranlateFieldName", String.class);//如果多个参数,逗号分隔,然后写参数类型.class + Object result = method.invoke(spy, key); + assertEquals(result, "coverageArea"); + } +} \ No newline at end of file diff --git a/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java b/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java new file mode 100644 index 00000000..864f85fa --- /dev/null +++ b/server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2017 CTC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.usecaseui.server.service.intent.impl; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.onap.usecaseui.server.bean.intent.IntentModel; +import org.onap.usecaseui.server.util.ZipUtil; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ZipUtil.class,IntentServiceImpl.class}) +class IntentServiceImplTest { + public IntentServiceImplTest(){} + + + @InjectMocks + private IntentServiceImpl intentService; + + @Before + public void before() throws Exception { + //doReturn(session).when(sessionFactory,"openSession"); + } + + + //public String addModel(IntentModel model) + /*@Test + public void addModelTest() throws Exception { + IntentModel model = new IntentModel(); + model.setId(1); + Transaction tx = Mockito.mock(Transaction.class); + doReturn(tx).when(session,"beginTransaction"); + Serializable save = Mockito.mock(Serializable.class); + Mockito.when(session.save(model)).thenReturn(save); + + }*/ + @Test + public void activeModelFileTest() throws Exception { + IntentModel model = new IntentModel(); + String filePath = "filePath.zip"; + String parentPath = "parentPath"; + String unzipPath = "filePath"; + model.setFilePath(filePath); + + File file=PowerMockito.mock(File.class); + PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(file); + PowerMockito.when(file.exists()).thenReturn(true); + PowerMockito.when(file.getParent()).thenReturn(model.getFilePath()); + + assertThat(intentService.activeModelFile(model), is(unzipPath)); + } + @Test + public void activeModelFileModelIsNullTest() throws Exception { + assertEquals(intentService.activeModelFile(null), null); + } + @Test + public void activeModelFileFilePathIsNullTest() throws Exception { + IntentModel model = new IntentModel(); + assertEquals(intentService.activeModelFile(model), null); + } + + + @Test + public void calcFieldValueValueIsNullTest() { + assertEquals(intentService.calcFieldValue(null, null), ""); + } + @Test + public void calcFieldValueKeyIsResourceSharingLevelTest() { + assertEquals(intentService.calcFieldValue("resourceSharingLevel", "shared"), "shared"); + } + @Test + public void calcFieldValueKeyIsUEMobilityLevelNomadicTest() { + assertEquals(intentService.calcFieldValue("uEMobilityLevel", "Nomadic"), "nomadic"); + } + @Test + public void calcFieldValueKeyIsUEMobilityLevelRestrictedTest() { + assertEquals(intentService.calcFieldValue("uEMobilityLevel", "restricted"), "Spatially Restricted Mobility"); + } + @Test + public void calcFieldValueKeyIsUEMobilityLevelFullyTest() { + assertEquals(intentService.calcFieldValue("uEMobilityLevel", "fully"), "Fully Mobility"); + } + @Test + public void calcFieldValueKeyIsCoverageAreaTest() { + assertEquals(intentService.calcFieldValue("coverageArea", "zhongguancun"), "Beijing Haidian District Zhongguancun"); + } + @Test + public void calcFieldValueKeyIsMaxNumberofUEsTest() { + assertEquals(intentService.calcFieldValue("maxNumberofUEs", "5"), "5"); + } + @Test + public void calcFieldValueKeyIsExpDataRateDLTest() { + assertEquals(intentService.calcFieldValue("expDataRateDL", "1gb"), "1000"); + } + @Test + public void calcFieldValueKeyIsLatencyTest() { + assertEquals(intentService.calcFieldValue("latency", "1s"), "200"); + } + + + @Test + public void formatValueForResourcesSharingLevelTest() throws InvocationTargetException, IllegalAccessException { + String value = "shared"; + IntentServiceImpl spy = PowerMockito.spy(intentService); + Method method = PowerMockito.method(IntentServiceImpl.class, "formatValueForResourcesSharingLevel", String.class);//如果多个参数,逗号分隔,然后写参数类型.class + Object result = method.invoke(spy, value); + assertEquals(result, "shared"); + } +} \ No newline at end of file -- cgit 1.2.3-korg