From 3d655ad437c2784ece4b3797cb4f7a2e24473dd8 Mon Sep 17 00:00:00 2001 From: arun chapagain Date: Fri, 21 Aug 2020 15:18:14 +0530 Subject: Junit for ServiceException Issue-ID: VFC-1726 Signed-off-by: arun chapagain Change-Id: I29aa80df17b8c2eacbc130b31732bd95ab4748f0 --- .../common/restclient/ServiceExceptionTest.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/ServiceExceptionTest.java diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/ServiceExceptionTest.java b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/ServiceExceptionTest.java new file mode 100644 index 00000000..b3739703 --- /dev/null +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/ServiceExceptionTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import mockit.integration.junit4.JMockit; +@RunWith(JMockit.class) +public class ServiceExceptionTest { + + @Test + public void test() { + ServiceException serviceException = new ServiceException(); + ServiceException serviceWithThrowable = new ServiceException("id",new Throwable()); + ServiceException serviceWithThrowableMessage = new ServiceException("id","message",new Throwable()); + + ServiceException serviceWithMessage = new ServiceException("message"); + ServiceException serviceWithID = new ServiceException("id","message"); + ServiceException serviceWithHttpCode = new ServiceException("id",200); + ServiceException serviceWithHttpCodeAndMessage = new ServiceException(200,"message"); + ServiceException serviceWithexceptionArgs = new ServiceException("id",200,new ExceptionArgs()); + ServiceException serviceWithMessageThowable =new ServiceException("id","message","args","args"); + ServiceException serviceWithThowableAndArgs =new ServiceException("id","message",new Throwable(),"args"); + assertNotNull(serviceWithThowableAndArgs.getArgs()); + assertNotNull(serviceWithID.getId()); + assertNotNull(serviceWithexceptionArgs.getExceptionArgs()); + assertNotNull(serviceWithThowableAndArgs.getArgs()); + } +} -- cgit 1.2.3-korg From 29bfc15ca573151581e9da09efaab9a847380284 Mon Sep 17 00:00:00 2001 From: arun chapagain Date: Fri, 21 Aug 2020 18:23:38 +0530 Subject: Junit for RegisterConfig and VnfmException Issue-ID: VFC-1726 Signed-off-by: arun chapagain Change-Id: I14a52a0e956a90fdfd1bb3bdcae6bfd227879cf0 --- .../vnfmadapter/common/RegisterConfigInfoTest.java | 45 ++++++++++++++++++++++ .../vnfmadapter/common/VnfmExceptionTest.java | 31 +++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/RegisterConfigInfoTest.java create mode 100644 huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmExceptionTest.java diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/RegisterConfigInfoTest.java b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/RegisterConfigInfoTest.java new file mode 100644 index 00000000..276052f7 --- /dev/null +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/RegisterConfigInfoTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * 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.vfc.nfvo.vnfm.svnfm.vnfmadapter.common; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; +@RunWith(MockitoJUnitRunner.class) +public class RegisterConfigInfoTest { + + @InjectMocks + RegisterConfigInfo registerConfigInfo; + + + + @Test + public void test() { + assertNotNull(registerConfigInfo.getIp()); + assertNotNull(registerConfigInfo.getPort()); + assertNotNull(registerConfigInfo.getProtocol()); + assertNotNull(registerConfigInfo.getServiceName()); + assertNotNull(registerConfigInfo.getTtl()); + assertNotNull(registerConfigInfo.getInstance()); + assertNotNull(registerConfigInfo.getVersion()); + assertNotNull(registerConfigInfo.getUrl()); + } + + +} diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmExceptionTest.java b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmExceptionTest.java new file mode 100644 index 00000000..aeb15ba8 --- /dev/null +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmExceptionTest.java @@ -0,0 +1,31 @@ +/* + * Copyright 2016 Huawei Technologies Co., Ltd. + * + * 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.vfc.nfvo.vnfm.svnfm.vnfmadapter.common; + +import org.junit.Test; + +public class VnfmExceptionTest { + + @Test + public void test() { + VnfmException vnfmException = new VnfmException(); + VnfmException vnfmExceptionWithMessage = new VnfmException("message"); + VnfmException vnfmExceptionWithMessageThrowable = new VnfmException("message", new Throwable()); + VnfmException vnfmExceptionWithThrowable = new VnfmException( new Throwable()); + + } + +} -- cgit 1.2.3-korg From 087408bbb07917024bbfca77f4983b32582e11b4 Mon Sep 17 00:00:00 2001 From: arun chapagain Date: Fri, 21 Aug 2020 20:22:00 +0530 Subject: Junit coverage increased Issue-ID: VFC-1726 Signed-off-by: arun chapagain Change-Id: Ia0e3c2ea3ae429e286e9151f2e6de9e3f639c868 --- .../vnfmadapter/common/ResultRequestUtilTest.java | 35 ++++++++ .../svnfm/vnfmadapter/common/VnfmUtilTest.java | 93 +++++++++++----------- .../common/servicetoken/VnfmRestfulUtilTest.java | 79 ++++++++++-------- 3 files changed, 128 insertions(+), 79 deletions(-) diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/ResultRequestUtilTest.java b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/ResultRequestUtilTest.java index 3e93f2c2..8792f528 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/ResultRequestUtilTest.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/ResultRequestUtilTest.java @@ -16,9 +16,11 @@ package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant; import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect.ConnectMgrVnfm; import mockit.Mock; @@ -36,10 +38,15 @@ public class ResultRequestUtilTest { return 500; } }; + + JSONObject vnfmObject = new JSONObject(); String path = "http://localhost:8080"; String methodName = "get"; String paramsJson = ""; + vnfmObject.put("url", path); + vnfmObject.put(Constant.USERNAME, Constant.USERNAME); + vnfmObject.put(Constant.PASSWORD, Constant.PASSWORD); JSONObject resp = ResultRequestUtil.call(vnfmObject, path, methodName, paramsJson); assertTrue(resp.get("data").equals("connect fail.")); } @@ -91,5 +98,33 @@ public class ResultRequestUtilTest { JSONObject resp = ResultRequestUtil.call(vnfmObject, path, methodName, paramsJson); assertTrue(resp.get("data").equals("get connection error")); } + + + @Test + public void call() { + + JSONObject vnfmObject = new JSONObject(); + vnfmObject.put("url","https://localhost:8080/%s" ); + vnfmObject.put(Constant.USERNAME, Constant.USERNAME); + vnfmObject.put(Constant.PASSWORD, Constant.PASSWORD); + String path = "https://localhost:8080/%s"; + String methodName = "get"; + String paramsJson = ""; + assertNotNull(ResultRequestUtil.call( vnfmObject, path, methodName, paramsJson, + "authModel")); + + } + + @Test + public void callSouth() { + JSONObject vnfmObject = new JSONObject(); + vnfmObject.put("url", "https://localhost:8080/%s"); + vnfmObject.put(Constant.USERNAME, Constant.USERNAME); + vnfmObject.put(Constant.PASSWORD, Constant.PASSWORD); + String path = "https://localhost:8080/%s"; + String methodName = "get"; + String paramsJson = ""; + assertNotNull(ResultRequestUtil.callSouth(vnfmObject, path, methodName, paramsJson, "authModel")); + } } diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmUtilTest.java b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmUtilTest.java index e95e9578..3cd28d7a 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmUtilTest.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/VnfmUtilTest.java @@ -38,14 +38,14 @@ public class VnfmUtilTest { @Test public void getVnfmByIdTestSuccess(){ - new MockUp(){ - @Mock - public RestfulResponse getRemoteResponse(String url, String methodType, String params) { - RestfulResponse resp = new RestfulResponse(); - resp.setStatus(200); - return resp; - } - }; +// new MockUp(){ +// @Mock +// public RestfulResponse getRemoteResponse(String url, String methodType, String params) { +// RestfulResponse resp = new RestfulResponse(); +// resp.setStatus(200); +// return resp; +// } +// }; JSONObject resp = VnfmUtil.getVnfmById("1234"); assertNotNull(resp); } @@ -58,55 +58,56 @@ public class VnfmUtilTest { @Test public void getVnfmIdByIpTestSuccess(){ - new MockUp(){ - @Mock - public RestfulResponse getRemoteResponse(String url, String methodType, String params) { - RestfulResponse resp = new RestfulResponse(); - resp.setStatus(200); - JSONArray respArray = new JSONArray(); - JSONObject obj = new JSONObject(); - obj.put("url", "localhost"); - obj.put("vnfmId", "1234"); - respArray.add(obj); - resp.setResponseJson(respArray.toString()); - return resp; - } - }; +// new MockUp(){ +// @Mock +// public RestfulResponse getRemoteResponse(String url, String methodType, String params) { +// RestfulResponse resp = new RestfulResponse(); +// resp.setStatus(200); +// JSONArray respArray = new JSONArray(); +// JSONObject obj = new JSONObject(); +// obj.put("url", "localhost"); +// obj.put("vnfmId", "1234"); +// respArray.add(obj); +// resp.setResponseJson(respArray.toString()); +// return resp; +// } +// }; String resp = VnfmUtil.getVnfmIdByIp("localhost"); assertTrue("1234".equals(resp)); } @Test public void getVnfmIdByIpTestSuccessInvalidIP(){ - new MockUp(){ - @Mock - public RestfulResponse getRemoteResponse(String url, String methodType, String params) { - RestfulResponse resp = new RestfulResponse(); - resp.setStatus(200); - JSONArray respArray = new JSONArray(); - JSONObject obj = new JSONObject(); - obj.put("url", "127.0.0.1"); - obj.put("vnfmId", "1234"); - respArray.add(obj); - resp.setResponseJson(respArray.toString()); - return resp; - } - }; +// new MockUp(){ +// @Mock +// public RestfulResponse getRemoteResponse(String url, String methodType, String params) { +// RestfulResponse resp = new RestfulResponse(); +// resp.setStatus(200); +// JSONArray respArray = new JSONArray(); +// JSONObject obj = new JSONObject(); +// obj.put("url", "127.0.0.1"); +// obj.put("vnfmId", "1234"); +// respArray.add(obj); +// resp.setResponseJson(respArray.toString()); +// return resp; +// } +// }; String resp = VnfmUtil.getVnfmIdByIp("localhost"); assertTrue("".equals(resp)); } @Test public void getVnfmIdByIpTestSuccessEmptyResp(){ - new MockUp(){ - @Mock - public RestfulResponse getRemoteResponse(String url, String methodType, String params) { - RestfulResponse resp = new RestfulResponse(); - resp.setStatus(200); - JSONArray respArray = new JSONArray(); - resp.setResponseJson(respArray.toString()); - return resp; - } - }; +// new MockUp(){ +// @Mock +// public RestfulResponse getRemoteResponse(String url, String methodType, String params) { +// RestfulResponse resp = new RestfulResponse(); +// resp.setStatus(200); +// JSONArray respArray = new JSONArray(); +// resp.setResponseJson(respArray.toString()); +// return resp; +// } +// }; + VnfmUtil.mockForTest("Vfnid"); String resp = VnfmUtil.getVnfmIdByIp("localhost"); assertTrue("".equals(resp)); } diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/servicetoken/VnfmRestfulUtilTest.java b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/servicetoken/VnfmRestfulUtilTest.java index fec98449..60b5f639 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/servicetoken/VnfmRestfulUtilTest.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/servicetoken/VnfmRestfulUtilTest.java @@ -53,51 +53,51 @@ public class VnfmRestfulUtilTest { @Test public void testSendReqToApp() { - new MockUp() { - - @Mock - public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) { - RestfulResponse restfulResponse = new RestfulResponse(); - restfulResponse.setStatus(Constant.HTTP_OK); - String responseString = "{\"retCode\":1,\"data\":\"success\"}"; - restfulResponse.setResponseJson(responseString); - return restfulResponse; - } - }; +// new MockUp() { +// +// @Mock +// public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) { +// RestfulResponse restfulResponse = new RestfulResponse(); +// restfulResponse.setStatus(Constant.HTTP_OK); +// String responseString = "{\"retCode\":1,\"data\":\"success\"}"; +// restfulResponse.setResponseJson(responseString); +// return restfulResponse; +// } +// }; JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject()); assertEquals(Constant.REST_SUCCESS, result.get("retCode")); } @Test public void testSendReqToAppByErrorMsg() { - new MockUp() { - - @Mock - public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) { - RestfulResponse restfulResponse = new RestfulResponse(); - restfulResponse.setStatus(Constant.HTTP_OK); - String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}"; - restfulResponse.setResponseJson(responseString); - return restfulResponse; - } - }; +// new MockUp() { +// +// @Mock +// public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) { +// RestfulResponse restfulResponse = new RestfulResponse(); +// restfulResponse.setStatus(Constant.HTTP_OK); +// String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}"; +// restfulResponse.setResponseJson(responseString); +// return restfulResponse; +// } +// }; JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject()); assertEquals(Constant.REST_FAIL, result.get("retCode")); } @Test public void testSendReqToAppByError() { - new MockUp() { - - @Mock - public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) { - RestfulResponse restfulResponse = new RestfulResponse(); - restfulResponse.setStatus(Constant.HTTP_OK); - String responseString = "{\"retCode\":-1,\"data\":\"fail\"}"; - restfulResponse.setResponseJson(responseString); - return restfulResponse; - } - }; +// new MockUp() { +// +// @Mock +// public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) { +// RestfulResponse restfulResponse = new RestfulResponse(); +// restfulResponse.setStatus(Constant.HTTP_OK); +// String responseString = "{\"retCode\":-1,\"data\":\"fail\"}"; +// restfulResponse.setResponseJson(responseString); +// return restfulResponse; +// } +// }; JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject()); assertEquals(Constant.REST_FAIL, result.get("retCode")); } @@ -238,4 +238,17 @@ public class VnfmRestfulUtilTest { RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123"); assertNull(resp); } + + @Test + public void getRemoteResponse() { + Map headerMap = new HashMap(); + + VnfmRestfulUtil.getRemoteResponse( "url", "get", headerMap,"params"); + VnfmRestfulUtil.getRemoteResponse( "url", "post", headerMap,"params"); + VnfmRestfulUtil.getRemoteResponse( "url", "put", headerMap,"params"); + assertNull(VnfmRestfulUtil.getRemoteResponse( "url", "delete", headerMap,"params")); + + + + } } -- cgit 1.2.3-korg