From 87db9c00fe96b7fec8cee0d000727c27cca514fa Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Thu, 29 Mar 2018 10:39:23 +0530 Subject: Verify multivimproxy utils Issue-ID: VFC-644 Change-Id: Ie347ded9e821d908c44d78d11729dbba11885ce5 Signed-off-by: Kanagaraj Manickam k00365106 --- .../multivimproxy/common/util/JsonUtilTest.java | 435 +++++++++++++++++++++ .../multivimproxy/common/util/RestfulUtilTest.java | 181 +++++++++ .../multivimproxy/common/util/StringUtilTest.java | 144 +++++++ 3 files changed, 760 insertions(+) create mode 100644 service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtilTest.java create mode 100644 service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/RestfulUtilTest.java create mode 100644 service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/StringUtilTest.java diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtilTest.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtilTest.java new file mode 100644 index 0000000..301405f --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtilTest.java @@ -0,0 +1,435 @@ +/* + * Copyright 2018 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.multivimproxy.common.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import org.junit.Test; + +import mockit.Mock; +import mockit.MockUp; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +public class JsonUtilTest { + + @Test + public void testGetJsonFieldStr() { + JSONObject jsonObj = new JSONObject(); + String fieldName = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + String result = JsonUtil.getJsonFieldStr(jsonObj, fieldName); + String expectedResult = "1"; + assertEquals(expectedResult, result); + + } + + @Test + public void testGetJsonFieldInt() { + JSONObject jsonObj = new JSONObject(); + String fieldName = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + int result = JsonUtil.getJsonFieldInt(jsonObj, fieldName); + int expectedResult = 1; + assertEquals(expectedResult, result); + + } + + @Test + public void testGetJsonFieldArr() { + JSONObject jsonObj = new JSONObject(); + String fieldName = "a"; + jsonObj.put("a", new JSONArray()); + jsonObj.put("b", "2"); + JSONArray result = JsonUtil.getJsonFieldArr(jsonObj, fieldName); + JSONArray expectedResult = new JSONArray(); + assertEquals(expectedResult, result); + + } + + @Test + public void testGetJsonFieldJson() { + JSONObject jsonObj = new JSONObject(); + String fieldName = "a"; + jsonObj.put("a", new JSONObject()); + jsonObj.put("b", "2"); + JSONObject result = JsonUtil.getJsonFieldJson(jsonObj, fieldName); + JSONObject expectedResult = new JSONObject(); + assertEquals(expectedResult, result); + + } + + @Test + public void testGetJsonFieldLong() { + JSONObject jsonObj = new JSONObject(); + String fieldName = "a"; + jsonObj.put("a", 1); + jsonObj.put("b", 2); + Long result = JsonUtil.getJsonFieldLong(jsonObj, fieldName); + Long expectedResult = new Long(1); + assertEquals(expectedResult, result); + + } + + @Test + public void testGetJsonFieldObjectException() { + JSONObject jsonObj = new JSONObject(); + String fieldName = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + JSONObject result = JsonUtil.getJsonFieldJson(jsonObj, fieldName); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + + } + + @Test + public void testIsNullJson1() { + JSONObject jsonObj = new JSONObject(); + assertTrue(JsonUtil.isNullJson(jsonObj)); + } + + @Test + public void testIsNullJson2() { + assertTrue(JsonUtil.isNullJson(null)); + } + + @Test + public void testIsNullJson3() { + JSONObject jsonObj = new JSONObject(); + jsonObj.put("a", "1"); + assertFalse(JsonUtil.isNullJson(jsonObj)); + } + + @Test + public void testGetStrValueByjsonNULL() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + String result = JsonUtil.getStrValueByjson(jsonObj, key); + String expectedResult = null; + assertEquals(expectedResult, result); + + } + + @Test + public void testGetStrValueByjson() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + new MockUp() { + + @SuppressWarnings("static-access") + @Mock + public JSONObject optJSONObject(String key) { + return new JSONObject().fromObject("{\"a\":\"1\"}"); + } + + @Mock + public JSONObject getJSONObject(String key) { + return new JSONObject(); + } + }; + String result = JsonUtil.getStrValueByjson(jsonObj, key); + String expectedResult = "1"; + assertEquals(expectedResult, result); + + } + + @Test + public void testGetStrValueByjson1() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + new MockUp() { + + @Mock + public JSONObject optJSONObject(String key) { + return null; + } + + @SuppressWarnings("static-access") + @Mock + public JSONArray optJSONArray(String key) { + return new JSONArray().fromObject("[\"a\",\"1\"]"); + } + + @Mock + public JSONArray getJSONArray(String key) { + return new JSONArray(); + } + }; + String result = JsonUtil.getStrValueByjson(jsonObj, key); + String expectedResult = "1"; + assertEquals(expectedResult, result); + + } + + @Test + public void testGetStrValueByjson2() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + String result = JsonUtil.getStrValueByjson(jsonObj, key); + String expectedResult = "1"; + assertEquals(expectedResult, result); + + } + + @Test + public void testGetStrValueByJArray() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + new MockUp() { + + @Mock + public JSONObject optJSONObject(String key) { + return null; + } + + @SuppressWarnings("static-access") + @Mock + public JSONArray optJSONArray(String key) { + return new JSONArray().fromObject("[\"a\",\"1\"]"); + } + + @SuppressWarnings("static-access") + @Mock + public JSONArray getJSONArray(String key) { + return new JSONArray().fromObject("[\"a\",\"1\"]"); + } + + + }; + + String result = JsonUtil.getStrValueByjson(jsonObj, key); + + } + + @Test + public void testGetStrValueByJArray1() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + new MockUp() { + + int count = 1; + + @SuppressWarnings("static-access") + @Mock + public JSONObject optJSONObject(String key) { + if (count == 1) { + count += 1; + return null; + } else + return new JSONObject().fromObject("{\"a\":\"1\"}"); + } + + @SuppressWarnings("static-access") + @Mock + public JSONArray optJSONArray(String key) { + return new JSONArray().fromObject("[\"a\",\"1\"]"); + } + + @SuppressWarnings("static-access") + @Mock + public JSONArray getJSONArray(String key) { + return new JSONArray().fromObject("[\"a\",\"1\"]"); + } + }; + String result = JsonUtil.getStrValueByjson(jsonObj, key); + String expectedResult = "1"; + assertEquals(expectedResult, result); + + } + + @Test + public void testGetJsonValueByjson() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + String result = JsonUtil.getJsonValueByjson(jsonObj, key).toString(); + String expectedResult = "{\"a\":\"1\"}"; + assertEquals(expectedResult, result); + } + + @Test + public void testGetJsonValueByjsonResultIsNull() { + JSONObject jsonObj = new JSONObject(); + String key = "c"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + JSONObject result = JsonUtil.getJsonValueByjson(jsonObj, key); + String expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetStrValueByJsonParentKeyIsNull() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + String parentKey = ""; + String result = JsonUtil.getStrValueByParentJson(jsonObj, parentKey, key); + String expectedResult = "1"; + assertEquals(expectedResult, result); + } + + @Test + public void testGetStrValueByJsonParentJsonIsNull() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + String parentKey = "b"; + new MockUp() { + + @Mock + public JSONObject getJsonValueByjson(JSONObject json, String key) { + return new JSONObject(); + } + }; + String result = JsonUtil.getStrValueByParentJson(jsonObj, parentKey, key); + String expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetStrValueByJson() { + JSONObject jsonObj = new JSONObject(); + String key = "a"; + jsonObj.put("a", "1"); + jsonObj.put("b", "2"); + String parentKey = "b"; + String result = JsonUtil.getStrValueByParentJson(jsonObj, parentKey, key); + String expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetResponseDataRetcodeError1() { + new MockUp() { + + @Mock + public Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) { + return null; + } + }; + JSONObject result = JsonUtil.getResponseData(null); + String expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetResponseDataRetcodeError2() { + new MockUp() { + + @Mock + public Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) { + return -1; + } + }; + JSONObject result = JsonUtil.getResponseData(null); + String expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetResponseDataResultIsEmpty() { + JSONObject obj = new JSONObject(); + obj.put("data", "1"); + obj.put("retCode", "1"); + JSONObject result = JsonUtil.getResponseData(obj); + String expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetResponseData() { + JSONObject obj = new JSONObject(); + obj.put("data", new JSONObject()); + obj.put("retCode", "1"); + new MockUp() { + + @SuppressWarnings("static-access") + @Mock + public JSONObject optJSONObject(String key) { + return new JSONObject().fromObject("{\"a\":\"1\"}"); + } + }; + JSONObject result = JsonUtil.getResponseData(obj); + String expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetResponseData1() { + JSONObject obj = new JSONObject(); + obj.put("data", JSONArray.fromObject("[{\"a\":\"1\"},\"1\"]")); + obj.put("retCode", "1"); + new MockUp() { + + @Mock + public JSONObject optJSONObject(String key) { + return null; + } + + @SuppressWarnings("static-access") + @Mock + public JSONArray optJSONArray(String key) { + return new JSONArray().fromObject("[\"a\",\"1\"]"); + } + }; + JSONObject result = JsonUtil.getResponseData(obj); + String expectedResult = "{\"a\":\"1\"}"; + assertEquals(expectedResult, result.toString()); + } + + @Test + public void testGetResponseData2() { + JSONObject obj = new JSONObject(); + JSONObject json = new JSONObject(); + json.put("retCode", "1"); + obj.put("data", json); + obj.put("retCode", "1"); + JSONObject result = JsonUtil.getResponseData(obj); + String expectedResult = null; + assertEquals(expectedResult, result); + } + @Test + public void testPrivateConstructor() throws Exception { + Constructor constructor = JsonUtil.class.getDeclaredConstructor(); + assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers())); + + constructor.setAccessible(true); + constructor.newInstance(); + } +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/RestfulUtilTest.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/RestfulUtilTest.java new file mode 100644 index 0000000..2f9c00d --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/RestfulUtilTest.java @@ -0,0 +1,181 @@ +/* + * Copyright 2018 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.multivimproxy.common.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulOptions; +import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulParametes; +import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulResponse; +import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.ServiceException; + +import mockit.Mock; +import mockit.MockUp; +import net.sf.json.JSONArray; +import net.sf.json.JSONException; +import net.sf.json.JSONObject; + +public class RestfulUtilTest { + + @Test + public void testGetResponseObjWithTwoParams() { + mockGetResponseContent(); + JSONObject result = RestfulUtil.getResponseObj(null, null); + JSONObject expectedResult = new JSONObject().fromObject("{\"ResponseContent\":\"123\"}"); + assertEquals(expectedResult, result); + } + + private void mockGetResponseContent() { + new MockUp() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt, + String type) { + return "{\"ResponseContent\":\"123\"}"; + } + }; + } + + private void mockGetResponseContentReturnNull() { + new MockUp() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt, + String type) { + return null; + } + }; + } + + @Test + public void testGetResponseObjWithThreeParams() { + new MockUp() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt, + String type) { + return "{\"ResponseContent\":\"123\"}"; + } + }; + JSONObject result = RestfulUtil.getResponseObj(null, null, null); + @SuppressWarnings("static-access") + JSONObject expectedResult = new JSONObject().fromObject("{\"ResponseContent\":\"123\"}"); + assertEquals(expectedResult, result); + } + + @Test + public void testGetResponseObjExpections() { + new MockUp() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt, + String type) { + throw new JSONException(); + } + }; + JSONObject result = RestfulUtil.getResponseObj(null, null, null); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetRestResObjectsIsNull() { + mockGetResponseContentReturnNull(); + RestfulResponse result = RestfulUtil.getRestRes(null, null); + RestfulResponse expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetRestResReflectiveOperationException() { + RestfulResponse result = RestfulUtil.getRestRes("123", "get"); + RestfulResponse expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetRestRes() { + RestfulResponse result = RestfulUtil.getRestRes("async123", new RestfulResponse()); + RestfulResponse expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetResponse() throws ServiceException { + new MockUp() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, String type) { + return "{\"ResponseContent\":\"123\",\"data\":[\"datas\"]}"; + } + }; + JSONArray result = RestfulUtil.getResponseRes(null, null); + JSONArray expectedResult = JSONArray.fromObject("[\"datas\"]"); + assertEquals(expectedResult, result); + } + + @Test + public void testGetResponseExceptions() throws ServiceException { + new MockUp() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, String type) { + return "{\"ResponseContent\":\"123\",}"; + } + }; + try { + RestfulUtil.getResponseRes(null, null); + } catch(ServiceException e) { + assertTrue(true); + } + } + + @Test + public void testGgetResponseRes() throws ServiceException { + new MockUp() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, String type) { + return "{\"ResponseContent\":\"123\",}"; + } + }; + try { + RestfulUtil.getResponseRes(null, null, null); + } catch(ServiceException e) { + assertTrue(true); + } + } + + @Test + public void testGgetResponseResException() throws ServiceException { + new MockUp() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, String type) { + return null; + } + }; + try { + RestfulUtil.getResponseRes(null, null, null); + } catch(ServiceException e) { + assertTrue(true); + } + } + +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/StringUtilTest.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/StringUtilTest.java new file mode 100644 index 0000000..cb3b811 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/StringUtilTest.java @@ -0,0 +1,144 @@ +/* + * Copyright 2018 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.multivimproxy.common.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import org.junit.Test; +import org.onap.vfc.nfvo.multivimproxy.common.util.StringUtil; + +public class StringUtilTest { + + @Test + public void testisValidString() { + assertTrue(StringUtil.isValidString("abc")); + } + + @Test + public void testisValidString1() { + assertFalse(StringUtil.isValidString(null)); + } + + @Test + public void testisValidString2() { + assertFalse(StringUtil.isValidString("")); + } + + @Test + public void testIsAnyLargeThanZero() { + assertFalse(StringUtil.isAnyLargeThanZero("")); + } + + @Test + public void testIsAnyLargeThanZero1() { + assertTrue(StringUtil.isAnyLargeThanZero("123")); + } + + @Test + public void testIsIntegerExceptions() { + assertFalse(StringUtil.isInteger("asd")); + } + + @Test + public void testIsInteger() { + assertTrue(StringUtil.isInteger("123")); + } + + @Test + public void testIsInteger1() { + assertFalse(StringUtil.isInteger("-1")); + } + + @Test + public void testIsNumericExceptions() { + assertFalse(StringUtil.isNumeric("abc")); + } + + @Test + public void testIsNumeric() { + assertTrue(StringUtil.isNumeric("1.456")); + } + + @Test + public void testIsNumeric1() { + assertFalse(StringUtil.isNumeric("-1.456")); + } + + @Test + public void testCompareZeroByFloat() { + assertTrue(StringUtil.compareZeroByFloat("3.0", "1.0", "2.0")); + } + + @Test + public void testCompareZeroByFloat1() { + assertFalse(StringUtil.compareZeroByFloat("3.0", "1.2", "2.5")); + } + + @Test + public void testCompareZeroByInteger() { + assertTrue(StringUtil.compareZeroByInteger("3", "1", "2")); + } + + @Test + public void testCompareZeroByInteger1() { + assertFalse(StringUtil.compareZeroByInteger("3", "1", "3")); + } + + @Test + public void testNumFormatDataIsNull() { + String result = StringUtil.numFormat(null); + assertEquals(null, result); + } + + @Test + public void testNumFormatDataIsEmpty() { + String result = StringUtil.numFormat(""); + assertEquals(null, result); + } + + @Test + public void testNumFormatInteger() { + String result = StringUtil.numFormat("12"); + String expectedResult = "12"; + assertEquals(expectedResult, result); + } + + @Test + public void testNumFormatFloat() { + String result = StringUtil.numFormat("12.5"); + String expectedResult = "12.5"; + assertEquals(expectedResult, result); + } + + @Test + public void testCheckXss() { + assertTrue(StringUtil.checkXss("123")); + } + @Test + public void testPrivateConstructor() throws Exception { + Constructor constructor = StringUtil.class.getDeclaredConstructor(); + assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers())); + + constructor.setAccessible(true); + constructor.newInstance(); + } +} -- cgit 1.2.3-korg