add test class for Intent module 58/121558/1
authorzhaoyehua <zhaoyh6@asiainfo.com>
Thu, 27 May 2021 01:45:07 +0000 (09:45 +0800)
committerzhaoyehua <zhaoyh6@asiainfo.com>
Thu, 27 May 2021 01:45:15 +0000 (09:45 +0800)
Issue-ID: USECASEUI-525
Change-Id: I2b746d0df3935580f59a24e4c2ce7427331da4c0
Signed-off-by: zhaoyehua <zhaoyh6@asiainfo.com>
server/pom.xml
server/src/test/java/org/onap/usecaseui/server/controller/IntentControllerTest.java [new file with mode: 0644]
server/src/test/java/org/onap/usecaseui/server/service/intent/impl/IntentServiceImplTest.java [new file with mode: 0644]

index f167dd7..8ac1e55 100644 (file)
         <dependency>
             <groupId>org.powermock</groupId>
             <artifactId>powermock-api-mockito2</artifactId>
-            <version>2.0.7</version>
+            <version>2.0.2</version>
         </dependency>
         <dependency>
             <groupId>org.mockito</groupId>
         <dependency>
             <groupId>org.powermock</groupId>
             <artifactId>powermock-module-junit4</artifactId>
-            <version>1.6.5</version>
+            <version>2.0.2</version>
             <type>jar</type>
             <scope>compile</scope>
         </dependency>
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 (file)
index 0000000..b970416
--- /dev/null
@@ -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<String,Object> 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 (file)
index 0000000..864f85f
--- /dev/null
@@ -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