diff options
Diffstat (limited to 'service')
14 files changed, 3907 insertions, 0 deletions
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<JSONObject>() { + + @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<JSONObject>() { + + @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<JSONObject>() { + + @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<JSONObject>() { + + 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<JsonUtil>() { + + @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<JsonUtil>() { + + @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<JsonUtil>() { + + @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<JSONObject>() { + + @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<JSONObject>() { + + @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<RestfulUtil>() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt, + String type) { + return "{\"ResponseContent\":\"123\"}"; + } + }; + } + + private void mockGetResponseContentReturnNull() { + new MockUp<RestfulUtil>() { + + @Mock + public String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt, + String type) { + return null; + } + }; + } + + @Test + public void testGetResponseObjWithThreeParams() { + new MockUp<RestfulUtil>() { + + @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<RestfulUtil>() { + + @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<RestfulUtil>() { + + @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<RestfulUtil>() { + + @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<RestfulUtil>() { + + @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<RestfulUtil>() { + + @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<StringUtil> constructor = StringUtil.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/request/RequestUtilTest.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/request/RequestUtilTest.java new file mode 100644 index 0000000..2c8b6c9 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/request/RequestUtilTest.java @@ -0,0 +1,261 @@ +/* + * 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.request; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.io.IOUtils; +import org.apache.cxf.jaxrs.impl.HttpServletRequestFilter; +import org.junit.Test; +import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulParametes; +import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.ServiceException; + +import javassist.Modifier; +import mockit.Mock; +import mockit.MockUp; +import mockit.Mocked; +import net.sf.json.JSONException; +import net.sf.json.JSONObject; + +public class RequestUtilTest { + + @Test + public void testGetStringRequestBody() { + HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() { + + @Mocked + ServletInputStream input; + + @Mock + public ServletInputStream getInputStream() throws IOException { + return input; + } + }.getMockInstance(); + new MockUp<IOUtils>() { + + String data = "{\"NETWORK\":{\"id\": \"123\"}}"; + + @Mock + public String toString(InputStream input) throws IOException { + return data; + } + }; + String result = RequestUtil.getStringRequestBody(context); + String expectedResult = "{\"NETWORK\":{\"id\": \"123\"}}"; + assertEquals(expectedResult, result); + } + + @Test + public void testGetStringRequestBodyException() { + HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() { + + @Mock + public ServletInputStream getInputStream() throws IOException { + throw new IOException(); + } + }.getMockInstance(); + String result = RequestUtil.getStringRequestBody(context); + String expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testGetJsonRequestBody() { + HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() { + + @Mocked + ServletInputStream input; + + @Mock + public ServletInputStream getInputStream() throws IOException { + return input; + } + }.getMockInstance(); + new MockUp<IOUtils>() { + + String data = "{\"NETWORK\":{\"id\": \"123\"}}"; + + @Mock + public String toString(InputStream input) throws IOException { + return data; + } + }; + JSONObject result = RequestUtil.getJsonRequestBody(context); + String data1 = "{\"NETWORK\":{\"id\": \"123\"}}"; + JSONObject expectedResult = JSONObject.fromObject(data1); + assertEquals(expectedResult, result); + } + + @Test + public void testGetJsonRequestBody1() { + new MockUp<RequestUtil>() { + + String data1 = "{\"NETWORK\":{\"id\": \"123\"}}"; + + @Mock + public String getStringRequestBody(HttpServletRequest context) { + return data1; + } + }; + JSONObject result = RequestUtil.getJsonRequestBody(null); + String data1 = "{\"NETWORK\":{\"id\": \"123\"}}"; + JSONObject expectedResult = JSONObject.fromObject(data1); + assertEquals(expectedResult, result); + } + + @Test + public void testGetJsonRequestBodyException() { + HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() { + + @Mocked + ServletInputStream input; + + @Mock + public ServletInputStream getInputStream() throws JSONException { + throw new JSONException(); + } + }.getMockInstance(); + JSONObject result = RequestUtil.getJsonRequestBody(context); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @SuppressWarnings("rawtypes") + @Test + public void testGetAllJsonRequestBodyRequestBodyIsNull() { + HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() { + + @Mocked + ServletInputStream input; + + @Mock + public ServletInputStream getInputStream() throws IOException { + return input; + } + + @Mock + public Enumeration getHeaderNames() { + return new Enumeration() { + + List<String> a = Arrays.asList(new String[] { "1", "2" }); + + @Override + public boolean hasMoreElements() { + return false; + } + + @Override + public Object nextElement() { + return null; + } + + }; + } + + }.getMockInstance(); + new MockUp<RequestUtil>() { + + @Mock + public JSONObject getJsonRequestBody(HttpServletRequest context) { + return null; + } + }; + JSONObject result = RequestUtil.getAllJsonRequestBody(context); + JSONObject expectedResult = new JSONObject(); + expectedResult.put("header", new HashMap<String, String>()); + assertEquals(expectedResult, result); + } + + @SuppressWarnings("rawtypes") + @Test + public void testGetContextHeader() { + HttpServletRequestFilter context = new MockUp<HttpServletRequestFilter>() { + + @Mock + public String getHeader(String name) { + return "1"; + } + + @Mock + public Enumeration getHeaderNames() { + return new Enumeration() { + + List<String> a = Arrays.asList(new String[] { "1", "2" }); + + int count = 1; + + @Override + public boolean hasMoreElements() { + if (count == 1) { + count += 1; + return true; + } else + return false; + } + + @Override + public Object nextElement() { + return "1"; + } + + }; + } + + }.getMockInstance(); + new MockUp<RequestUtil>() { + + @Mock + public JSONObject getJsonRequestBody(HttpServletRequest context) { + return null; + } + }; + JSONObject result = RequestUtil.getAllJsonRequestBody(context); + JSONObject expectedResult = new JSONObject(); + Map<String, String> map = new HashMap<String, String>(); + map.put("1", "1"); + expectedResult.put("header", map); + assertEquals(expectedResult, result); + } + @Test + public void testPrivateConstructor() throws Exception { + Constructor constructor = RequestUtil.class.getDeclaredConstructor(); + assertTrue("Constructor is private", Modifier.isPrivate(constructor.getModifiers())); + + constructor.setAccessible(true); + constructor.newInstance(); + } + + @Test + public void testGetAllAAIHeaders() throws ServiceException { + RequestUtil.getAAIHeaderMap(); + RequestUtil.encodeParams(new RestfulParametes()); + } + +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/response/ResponseUtilTest.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/response/ResponseUtilTest.java new file mode 100644 index 0000000..ce3ce81 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/response/ResponseUtilTest.java @@ -0,0 +1,91 @@ +/* + * 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.response; + +import static org.junit.Assert.*; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.onap.vfc.nfvo.multivimproxy.common.util.response.ResponseUtil; + +import net.sf.json.JSONObject; + +public class ResponseUtilTest { + + @Test + public void TestGenHttpResponseWithTwoParam() { + int retCode1 = -1; + String msg1 = "123"; + JSONObject result = ResponseUtil.genHttpResponse(retCode1, msg1); + JSONObject expectedResult = new JSONObject(); + expectedResult.put("msg", "123"); + assertEquals(result.toString(), expectedResult.toString()); + ; + } + + @Test + public void TestGenHttpResponseWithThreeParam() { + int retCode1 = -1; + String msg1 = "123"; + JSONObject result = ResponseUtil.genHttpResponse(retCode1, msg1, null); + JSONObject expectedResult = new JSONObject(); + expectedResult.put("msg", "123"); + assertEquals(result.toString(), expectedResult.toString()); + ; + } + + @Test + public void TestGenHttpResponseWithFourParam1() { + int httpStatusCode = -1; + int retCode1 = -1; + String msg1 = "123"; + JSONObject result = ResponseUtil.genHttpResponse(httpStatusCode, retCode1, msg1); + JSONObject expectedResult = new JSONObject(); + expectedResult.put("msg", "123"); + assertEquals(result.toString(), expectedResult.toString()); + ; + } + + @Test + public void TestGenHttpResponseWithFourParam2() { + Map<String, Integer> codeMap = new HashMap<String, Integer>(5); + codeMap.put("httpStatusCode", -1); + codeMap.put("retCode", 1); + Map<String, Object> map = new HashMap<String, Object>(5); + map.put("a", -1); + map.put("b", 1); + String msg1 = "123"; + JSONObject result = ResponseUtil.genHttpResponse(codeMap, msg1, map); + JSONObject expectedResult = new JSONObject(); + expectedResult.put("msg", "123"); + expectedResult.put("a", "-1"); + assertEquals(result.toString(), expectedResult.toString()); + ; + } + @Test + public void testPrivateConstructor() throws Exception { + Constructor constructor = ResponseUtil.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/response/RoaResponseUtilTest.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/response/RoaResponseUtilTest.java new file mode 100644 index 0000000..3ca08c9 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/response/RoaResponseUtilTest.java @@ -0,0 +1,148 @@ +/* + * 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.response; + +import static org.junit.Assert.*; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; +import java.util.Map; + +import org.junit.Test; +import org.onap.vfc.nfvo.multivimproxy.common.util.response.ResponseUtil; +import org.onap.vfc.nfvo.multivimproxy.common.util.response.RoaResponseUtil; + +import mockit.Mock; +import mockit.MockUp; +import net.sf.json.JSONObject; + +public class RoaResponseUtilTest { + + @Test + public void testGet() { + new MockUp<ResponseUtil>() { + + @Mock + public JSONObject genHttpResponse(int retCode, String msg, Map<String, Object> map) { + return null; + } + }; + JSONObject result = RoaResponseUtil.get(null); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testAdd() { + int a = 0; + new MockUp<ResponseUtil>() { + + @Mock + public JSONObject genHttpResponse(int retCode, String msg) { + return null; + } + }; + JSONObject result = RoaResponseUtil.add(a); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testAdd1() { + int a = 2; + new MockUp<ResponseUtil>() { + + @Mock + public JSONObject genHttpResponse(int retCode, String msg) { + return null; + } + }; + JSONObject result = RoaResponseUtil.add(a); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testUpdate() { + int a = 0; + new MockUp<ResponseUtil>() { + + @Mock + public JSONObject genHttpResponse(int retCode, String msg) { + return null; + } + }; + JSONObject result = RoaResponseUtil.update(a); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testUpdate1() { + int a = 2; + new MockUp<ResponseUtil>() { + + @Mock + public JSONObject genHttpResponse(int retCode, String msg) { + return null; + } + }; + JSONObject result = RoaResponseUtil.update(a); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testDelete() { + int a = -1; + new MockUp<ResponseUtil>() { + + @Mock + public JSONObject genHttpResponse(int retCode, String msg) { + return null; + } + }; + JSONObject result = RoaResponseUtil.delete(a); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testDelete1() { + int a = 0; + new MockUp<ResponseUtil>() { + + @Mock + public JSONObject genHttpResponse(int retCode, String msg) { + return null; + } + }; + JSONObject result = RoaResponseUtil.delete(a); + JSONObject expectedResult = null; + assertEquals(expectedResult, result); + } + + @Test + public void testPrivateConstructor() throws Exception { + Constructor constructor = RoaResponseUtil.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/restclient/ReaderHelperTest.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/ReaderHelperTest.java new file mode 100644 index 0000000..223e5bb --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/ReaderHelperTest.java @@ -0,0 +1,117 @@ +/* + * 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.restclient; + +import static org.junit.Assert.assertEquals; + +import java.io.Reader; +import java.io.StringReader; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class ReaderHelperTest { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link org.openo.baseservice.util.inf.ReaderHelper#getLine()}. + */ + @Test + public void testGetLine() { + final String message = "hello.. how are you?"; + final Reader reader = new StringReader(message); + final ReaderHelper helper = new ReaderHelper(reader); + final String actual = helper.getLine(); + assertEquals(message, actual); + } + + /** + * Test method for {@link org.openo.baseservice.util.inf.ReaderHelper#getLine()}. + */ + @Test + public void testGetLineMultiLine() { + final String line1 = "hello.. how are you?"; + final String line2 = "I am fine."; + final Reader reader = new StringReader(line1 + System.lineSeparator() + line2); + final ReaderHelper helper = new ReaderHelper(reader); + String actual = helper.getLine(); + assertEquals(line1, actual); + actual = helper.getLine(); + assertEquals(line2, actual); + actual = helper.getLine(); + assertEquals(null, actual); + } + + @Test + public void testGetLineNull() { + final ReaderHelper helper = new ReaderHelper(null); + final String actual = helper.getLine(); + assertEquals(null, actual); + + } + +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestHttpRest.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestHttpRest.java new file mode 100644 index 0000000..bbcb675 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestHttpRest.java @@ -0,0 +1,958 @@ +/* + * 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.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.lang.reflect.Field; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.HttpExchange; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import mockit.Mock; +import mockit.MockUp; +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +@RunWith(JMockit.class) +public class TestHttpRest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testInitHttpRest() throws Exception { + final RestfulOptions options = new RestfulOptions(); + new MockUp<HttpClient>() { + + @Mock + public void doStart() { + } + }; + final HttpRest httpRest = new HttpRest(); + httpRest.initHttpRest(options); + final Field httpClient = HttpBaseRest.class.getDeclaredField("client"); + httpClient.setAccessible(true); + Assert.assertNotNull(httpClient.get(httpRest)); + } + + /** + * <br/> + * + * @throws NoSuchFieldException + * @throws Exception + * @since + */ + @Test + public void testCreateRestHttpContentExchange() throws NoSuchFieldException, Exception { + final HttpBaseRest httpRest = new HttpRest(); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + final RestHttpContentExchange exchange = httpRest.createRestHttpContentExchange(callback); + assertNotNull(exchange); + final Field callbackField = RestHttpContentExchange.class.getDeclaredField("callback"); + assertNotNull(callbackField); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testGetStringRestfulParametes() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", "application/json"); + parametes.putHttpContextHeader("Accept-Encoding", "*/*"); + final RestfulResponse response = httpRest.get("path/to/service", parametes); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testGetStringRestfulParametesRestfulOptions() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulResponse response = httpRest.get("path/to/service", new RestfulParametes(), options); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testHeadStringRestfulParametes() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", ""); + parametes.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.head("path/to/service", parametes); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testHeadStringRestfulParametesRestfulOptions() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", ""); + parametes.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.head("path/to/service", parametes, options); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @param options + * @return + * @throws ServiceException + * @since + */ + private HttpRest getHttpRest(final RestfulOptions options) throws ServiceException { + final HttpRest httpRest = new HttpRest(); + { + new MockUp<HttpClient>() { + + @Mock + public void doStart() { + } + + @Mock + public void send(final HttpExchange exchange) throws IOException { + } + }; + httpRest.initHttpRest(options); + + } + return httpRest; + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testAsyncGetStringRestfulParametesRestfulAsyncCallback() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncGet("path/to/service", new RestfulParametes(), callback); + httpRest.asyncGet("path/to/service", new RestfulParametes(), null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncGetStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), callback); + httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testPutStringRestfulParametes() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", ""); + parametes.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.put("path/to/service", parametes); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testPutStringRestfulParametesRestfulOptions() throws ServiceException { + + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", ""); + parametes.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.put("path/to/service", parametes, null); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncPutStringRestfulParametesRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPut("path/to/service", new RestfulParametes(), callback); + httpRest.asyncPut("path/to/service", new RestfulParametes(), null); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testAsyncPutStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), callback); + httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), null); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testAsyncPostStringRestfulParametesRestfulAsyncCallback() throws Exception { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return 99; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_EXCEPTED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback); + httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncPostStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback); + httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testDeleteStringRestfulParametes() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpBaseRest httpRest = getHttpRest(options); + + final RestfulResponse response = httpRest.delete("path/to/service", null); + assertEquals(-1, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testDeleteStringRestfulParametesRestfulOptions() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parameters = new RestfulParametes(); + parameters.put("id", "1234"); + parameters.put("name", "some-name"); + parameters.put("address", null); + parameters.setRawData("{ \"data\"=\"sample JSON data\""); + parameters.putHttpContextHeader("Content-Type", ""); + parameters.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.delete("path/to/service", parameters, options); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncDeleteStringRestfulParametesRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncDelete("path/to/service", new RestfulParametes(), callback); + httpRest.asyncDelete("path/to/service", new RestfulParametes(), null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncDeleteStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, callback); + httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testPatchStringRestfulParametes() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parameters = new RestfulParametes(); + parameters.put("id", "1234"); + parameters.put("name", "some-name"); + parameters.put("address", null); + parameters.setRawData("{ \"data\"=\"sample JSON data\""); + parameters.putHttpContextHeader("Content-Type", ""); + parameters.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.patch("path/to/service", parameters); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testPatchStringRestfulParametesRestfulOptions() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parameters = new RestfulParametes(); + parameters.put("id", "1234"); + parameters.put("name", "some-name"); + parameters.put("address", null); + parameters.setRawData("{ \"data\"=\"sample JSON data\""); + parameters.putHttpContextHeader("Content-Type", ""); + parameters.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.patch("path/to/service", parameters, options); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncPatchStringRestfulParametesRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPatch("path/to/service", new RestfulParametes(), callback); + httpRest.asyncPatch("path/to/service", new RestfulParametes(), null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncPatchStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, callback); + httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, null); + } + +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestHttpContentExchange.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestHttpContentExchange.java new file mode 100644 index 0000000..d407081 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestHttpContentExchange.java @@ -0,0 +1,584 @@ +/* + * 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.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.zip.GZIPInputStream; + +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.eclipse.jetty.client.Address; +import org.eclipse.jetty.client.CachedExchange; +import org.eclipse.jetty.client.HttpDestination; +import org.eclipse.jetty.client.HttpExchange; +import org.eclipse.jetty.http.HttpFields; +import org.eclipse.jetty.http.HttpHeaders; +import org.eclipse.jetty.io.Buffer; +import org.eclipse.jetty.io.ByteArrayBuffer; +import org.eclipse.jetty.util.StringUtil; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import mockit.Mock; +import mockit.MockUp; +import mockit.Mocked; +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +@RunWith(JMockit.class) +public class TestRestHttpContentExchange { + + @Mocked + HttpDestination mockedDest; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.ERROR); + } + + /** + * <br/> + * + * @throws IOException + * @since + */ + @Test + public void testOnRequestCommitted() throws IOException { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onRequestCommitted(); + + LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG); + exchange.onRequestCommitted(); + } + + /** + * <br/> + * + * @throws IOException + * @since + */ + @Test + public void testOnRequestComplete() throws IOException { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onRequestComplete(); + + LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG); + exchange.onRequestComplete(); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testOnResponseComplete() throws Exception { + RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onResponseComplete(); + + LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG); + exchange.onResponseComplete(); + + final AtomicInteger isCallback = new AtomicInteger(0); + final AtomicInteger isException = new AtomicInteger(0); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + isCallback.set(1); + } + + @Override + public void handleExcepion(final Throwable e) { + isException.set(1); + } + + }; + + final Field statusField = HttpExchange.class.getDeclaredField("_status"); + statusField.setAccessible(true); + exchange = new RestHttpContentExchange(false, callback); + statusField.set(exchange, new AtomicInteger(200)); + exchange.setAddress(new Address("localhost", 9999)); + exchange.setRequestURI("/the/request/uri"); + exchange.onResponseComplete(); + assertEquals(1, isCallback.get()); + assertEquals(0, isException.get()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + @Ignore + public void testDecompressGzipToStr() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz"); + final byte[] binaryData = new byte[1024]; + stream.read(binaryData); + final String expected = "sample data."; + + final String actual = exchange.decompressGzipToStr(binaryData); + + assertEquals(actual, expected); + + new MockUp<ByteArrayInputStream>() { + + @Mock + public int read() throws Exception { + throw new IOException(); + } + + @Mock + public int read(final byte abyte0[], final int i, final int j) { + + return -1; + } + + }; + + thrown.expect(IOException.class); + exchange.decompressGzipToStr(binaryData); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + @Ignore + public void testDecompressGzipToStrException() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz"); + final byte[] binaryData = new byte[1024]; + stream.read(binaryData); + final String expected = "sample data."; + + new MockUp<GZIPInputStream>() { + + @Mock + public void close() throws IOException { + throw new IOException(); + } + + }; + + new MockUp<InputStreamReader>() { + + @Mock + public void close() throws IOException { + throw new IOException(); + } + + }; + + new MockUp<ByteArrayInputStream>() { + + @Mock + public void close() throws IOException { + throw new IOException(); + } + + }; + + final String actual = exchange.decompressGzipToStr(binaryData); + assertEquals(actual, expected); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testDecompressGzipToStrNull() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + final String expected = ""; + final String actual = exchange.decompressGzipToStr(null); + + assertEquals(actual, expected); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testOnResponseHeaderBufferBuffer() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final Buffer name = new ByteArrayBuffer("key"); + final Buffer value = new ByteArrayBuffer("value"); + exchange.onResponseHeader(name, value); + + new MockUp<HttpHeaders>() { + + @Mock + public int getOrdinal(final Buffer buffer) { + return HttpHeaders.CONTENT_ENCODING_ORDINAL; + } + + }; + exchange.onResponseHeader(name, value); + + new MockUp<StringUtil>() { + + @Mock + public String asciiToLowerCase(final String s) { + return "gzip"; + } + + }; + exchange.onResponseHeader(name, value); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testOnExceptionThrowable() { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onException(new Exception()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testOnExceptionThrowableWithCallback() { + final AtomicInteger isCallback = new AtomicInteger(0); + final AtomicInteger isException = new AtomicInteger(0); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + isCallback.set(1); + } + + @Override + public void handleExcepion(final Throwable e) { + isException.set(1); + } + + }; + final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onException(new Exception()); + assertEquals(0, isCallback.get()); + assertEquals(1, isException.get()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testOnConnectionFailedThrowable() { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onConnectionFailed(new Exception()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testOnConnectionFailedThrowableException() { + final AtomicInteger isCallback = new AtomicInteger(0); + final AtomicInteger isException = new AtomicInteger(0); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + isCallback.set(1); + } + + @Override + public void handleExcepion(final Throwable e) { + isException.set(1); + } + + }; + final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onConnectionFailed(new Exception()); + assertEquals(0, isCallback.get()); + assertEquals(1, isException.get()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testExpireHttpDestination() { + final RestHttpContentExchange exchange = new RestHttpContentExchange(true, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.expire(mockedDest); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testExpireHttpDestinationException() throws Exception { + final AtomicInteger isCallback = new AtomicInteger(0); + final AtomicInteger isException = new AtomicInteger(0); + final List<Throwable> thrSet = new ArrayList<Throwable>(); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + isCallback.set(1); + } + + @Override + public void handleExcepion(final Throwable e) { + isException.set(1); + thrSet.add(e); + } + + }; + final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.expire(mockedDest); + assertEquals(0, isCallback.get()); + assertEquals(1, isException.get()); + assertEquals(1, thrSet.size()); + final Throwable t = thrSet.get(0); + assertEquals(ServiceException.class, t.getClass()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testIsGzip() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final Buffer name = new ByteArrayBuffer("key"); + final Buffer value = new ByteArrayBuffer("value"); + + new MockUp<HttpHeaders>() { + + @Mock + public int getOrdinal(final Buffer buffer) { + return HttpHeaders.CONTENT_ENCODING_ORDINAL; + } + + }; + exchange.onResponseHeader(name, value); + assertFalse(exchange.isGzip()); + + new MockUp<StringUtil>() { + + @Mock + public String asciiToLowerCase(final String s) { + return "gzip"; + } + + }; + exchange.onResponseHeader(name, value); + assertTrue(exchange.isGzip()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testGetResponse() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final Field statusField = HttpExchange.class.getDeclaredField("_status"); + statusField.setAccessible(true); + statusField.set(exchange, new AtomicInteger(200)); + + RestfulResponse response = exchange.getResponse(); + assertEquals(0, response.getStatus()); + + final HttpFields fields = new HttpFields(); + final Field headerFields = CachedExchange.class.getDeclaredField("_responseFields"); + headerFields.setAccessible(true); + headerFields.set(exchange, fields); + response = exchange.getResponse(); + assertEquals(0, response.getStatus()); + fields.add("Content-Type", "application/json"); + fields.add("Content-Encode", "UTF-8"); + response = exchange.getResponse(); + assertEquals(0, response.getStatus()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testGetResponseGzip() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + new MockUp<RestHttpContentExchange>() { + + @Mock + public boolean isGzip() { + return true; + } + }; + final Field statusField = HttpExchange.class.getDeclaredField("_status"); + statusField.setAccessible(true); + statusField.set(exchange, new AtomicInteger(200)); + + final RestfulResponse response = exchange.getResponse(); + assertEquals(0, response.getStatus()); + } +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulFactory.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulFactory.java new file mode 100644 index 0000000..acbd1ae --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulFactory.java @@ -0,0 +1,106 @@ +/* + * 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.restclient; + +import static org.junit.Assert.assertNotNull; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import mockit.Mock; +import mockit.MockUp; +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +@RunWith(JMockit.class) +public class TestRestfulFactory { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRestInstance() { + Restful instance = RestfulFactory.getRestInstance("https"); + + new MockUp<HttpRest>() { + + @Mock + public void initHttpRest(final RestfulOptions option) throws ServiceException { + throw new ServiceException(); + } + + }; + instance = RestfulFactory.getRestInstance("http"); + assertNotNull(instance); + + instance = RestfulFactory.getRestInstance("http"); + assertNotNull(instance); + } +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulOptions.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulOptions.java new file mode 100644 index 0000000..58baa49 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulOptions.java @@ -0,0 +1,288 @@ +/* + * 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.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +@RunWith(JMockit.class) +public class TestRestfulOptions { + + @Rule + final public ExpectedException thrown = ExpectedException.none(); + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetPort() { + final RestfulOptions options = new RestfulOptions(); + final int port = 9091; + assertEquals(0, options.getPort()); + options.setPort(port); + assertEquals(port, options.getPort()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetPort() { + final RestfulOptions options = new RestfulOptions(); + final int port = 9091; + assertTrue(options.setPort(port)); + assertEquals(port, options.getPort()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetHost() { + final RestfulOptions options = new RestfulOptions(); + final String host = "localhost"; + assertEquals("", options.getHost()); + options.setHost(host); + assertEquals(host, options.getHost()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetHost() { + final RestfulOptions options = new RestfulOptions(); + final String host = "localhost"; + assertTrue(options.setHost(host)); + assertEquals(host, options.getHost()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetRestTimeout() { + final RestfulOptions options = new RestfulOptions(); + int timeout = 0; + assertFalse(options.setRestTimeout(timeout)); + assertEquals(0, options.getRestTimeout()); + + timeout = 1; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + + timeout = 10; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + + timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT - 1; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + + timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + + timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT + 1; + assertFalse(options.setRestTimeout(timeout)); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRestTimeout() { + final RestfulOptions options = new RestfulOptions(); + int timeout = 0; + assertEquals(0, options.getRestTimeout()); + + timeout = 1; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetOption() { + final RestfulOptions options = new RestfulOptions(); + assertNull(options.getOption("invalid")); + + options.setHost("localhost"); + Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME); + assertNotNull(obj); + assertTrue(obj instanceof String); + assertEquals("localhost", obj); + + final List<String> list = new ArrayList<String>(); + list.add("data"); + options.setOption("list", list); + obj = options.getOption("list"); + assertNotNull(obj); + assertTrue(obj instanceof List); + assertSame(list, obj); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetIntOption() { + final RestfulOptions options = new RestfulOptions(); + + assertEquals(0, options.getIntOption("count")); + + options.setOption("count", 1); + assertEquals(1, options.getIntOption("count")); + + thrown.expect(RuntimeException.class); + + options.setOption("string-count", "two"); + final int value = options.getIntOption("string-count"); + assertEquals(2, value); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetStringOption() { + final RestfulOptions options = new RestfulOptions(); + + assertEquals("", options.getStringOption("count")); + + options.setOption("string-count", "one"); + assertEquals("one", options.getStringOption("string-count")); + + thrown.expect(RuntimeException.class); + + options.setOption("count", 2); + final String value = options.getStringOption("count"); + assertEquals(2, value); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetOption() { + final RestfulOptions options = new RestfulOptions(); + assertNull(options.getOption("invalid")); + + options.setHost("localhost"); + Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME); + assertNotNull(obj); + assertTrue(obj instanceof String); + assertEquals("localhost", obj); + + final List<String> list = new ArrayList<String>(); + list.add("data"); + options.setOption("list", list); + obj = options.getOption("list"); + assertNotNull(obj); + assertTrue(obj instanceof List); + assertSame(list, obj); + } +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulParametes.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulParametes.java new file mode 100644 index 0000000..0aeaeb6 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulParametes.java @@ -0,0 +1,229 @@ +/* + * 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.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class TestRestfulParametes { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGet() { + final RestfulParametes params = new RestfulParametes(); + assertNull(params.get("param")); + final Map<String, String> paramMap = new HashMap<String, String>(); + params.setParamMap(paramMap); + paramMap.put("param", "value"); + assertEquals("value", params.get("param")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetRawData() { + final RestfulParametes params = new RestfulParametes(); + final String data = "Sample data."; + params.setRawData(data); + assertEquals(data, params.getRawData()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRawData() { + final RestfulParametes params = new RestfulParametes(); + assertNull(params.getRawData()); + final String data = "Sample data."; + params.setRawData(data); + assertEquals(data, params.getRawData()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testPut() { + final RestfulParametes params = new RestfulParametes(); + params.put("somekey", "somevalue"); + params.put("otherkey", "othervalue"); + assertEquals("somevalue", params.get("somekey")); + assertEquals("othervalue", params.get("otherkey")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testPutHttpContextHeaderStringString() { + final RestfulParametes params = new RestfulParametes(); + params.putHttpContextHeader("Context-Encoding", "UTF-8"); + assertEquals("UTF-8", params.getHttpContextHeader("Context-Encoding")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testPutHttpContextHeaderStringInt() { + final RestfulParametes params = new RestfulParametes(); + params.putHttpContextHeader("Expire-At", 1000); + assertEquals("1000", params.getHttpContextHeader("Expire-At")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetHttpContextHeader() { + final RestfulParametes params = new RestfulParametes(); + params.putHttpContextHeader("Expire-At", 1000); + params.putHttpContextHeader("Context-Encoding", "UTF-8"); + assertEquals("1000", params.getHttpContextHeader("Expire-At")); + assertEquals("UTF-8", params.getHttpContextHeader("Context-Encoding")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetParamMap() { + final RestfulParametes params = new RestfulParametes(); + params.put("key", "value"); + final Map<String, String> map = params.getParamMap(); + assertEquals("value", map.get("key")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetParamMap() { + final RestfulParametes params = new RestfulParametes(); + final Map<String, String> map = new HashMap<String, String>(); + map.put("key", "value"); + params.setParamMap(map); + assertEquals("value", params.get("key")); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetHeaderMap() { + final RestfulParametes params = new RestfulParametes(); + params.putHttpContextHeader("key", "value"); + final Map<String, String> map = params.getHeaderMap(); + assertEquals("value", map.get("key")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetHeaderMap() { + final RestfulParametes params = new RestfulParametes(); + final Map<String, String> map = new HashMap<String, String>(); + map.put("key", "value"); + params.setHeaderMap(map); + assertEquals("value", params.getHttpContextHeader("key")); + } +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulResponse.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulResponse.java new file mode 100644 index 0000000..23114e9 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestRestfulResponse.java @@ -0,0 +1,273 @@ +/* + * 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.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class TestRestfulResponse { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetStatus() { + final RestfulResponse response = new RestfulResponse(); + int actual = response.getStatus(); + int expected = -1; + + assertEquals(expected, actual); + expected = 202; + response.setStatus(expected); + actual = response.getStatus(); + assertEquals(expected, actual); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetStatus() { + final RestfulResponse response = new RestfulResponse(); + final int expected = 10; + response.setStatus(expected); + final int actual = response.getStatus(); + assertEquals(expected, actual); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRespHeaderMap() { + final RestfulResponse response = new RestfulResponse(); + Map<String, String> expected = response.getRespHeaderMap(); + assertNull(expected); + expected = new HashMap<String, String>(); + expected.put("key", "value"); + response.setRespHeaderMap(expected); + final Map<String, String> actual = response.getRespHeaderMap(); + assertNotNull(actual); + assertSame(actual, expected); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetRespHeaderMap() { + final RestfulResponse response = new RestfulResponse(); + response.setRespHeaderMap(null); + Map<String, String> expected = response.getRespHeaderMap(); + assertNull(expected); + expected = new HashMap<String, String>(); + expected.put("key", "value"); + response.setRespHeaderMap(expected); + final Map<String, String> actual = response.getRespHeaderMap(); + assertNotNull(actual); + assertSame(actual, expected); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRespHeaderInt() { + final RestfulResponse response = new RestfulResponse(); + response.setRespHeaderMap(null); + int actual = response.getRespHeaderInt("somekey"); + assertEquals(-1, actual); + final Map<String, String> headers = new HashMap<String, String>(); + headers.put("key", "value"); + headers.put("count", "1"); + response.setRespHeaderMap(headers); + actual = response.getRespHeaderInt("somekey"); + assertEquals(-1, actual); + + actual = response.getRespHeaderInt("count"); + assertEquals(1, actual); + + thrown.expect(RuntimeException.class); + actual = response.getRespHeaderInt("key"); + assertEquals(1, actual); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRespHeaderLong() { + final RestfulResponse response = new RestfulResponse(); + response.setRespHeaderMap(null); + long actual = response.getRespHeaderLong("somekey"); + assertEquals(-1, actual); + final Map<String, String> headers = new HashMap<String, String>(); + headers.put("key", "value"); + headers.put("count", "1"); + headers.put("max", "" + Long.MAX_VALUE); + headers.put("max++", Long.MAX_VALUE + 1 + ""); + response.setRespHeaderMap(headers); + actual = response.getRespHeaderLong("somekey"); + assertEquals(-1, actual); + + actual = response.getRespHeaderLong("count"); + assertEquals(1, actual); + + actual = response.getRespHeaderLong("max"); + assertEquals(Long.MAX_VALUE, actual); + + actual = response.getRespHeaderLong("max++"); + assertTrue(actual < 0); + + thrown.expect(RuntimeException.class); + actual = response.getRespHeaderLong("key"); + assertEquals(1, actual); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRespHeaderStr() { + final RestfulResponse response = new RestfulResponse(); + response.setRespHeaderMap(null); + String actual = response.getRespHeaderStr("somekey"); + assertEquals(null, actual); + final Map<String, String> headers = new HashMap<String, String>(); + headers.put("key", "value"); + headers.put("count", "1"); + headers.put("max", "" + Long.MAX_VALUE); + response.setRespHeaderMap(headers); + actual = response.getRespHeaderStr("somekey"); + assertEquals(null, actual); + + actual = response.getRespHeaderStr("key"); + assertEquals("value", actual); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetResponseContent() { + final RestfulResponse response = new RestfulResponse(); + assertEquals(null, response.getResponseContent()); + + final String content = "{ \"content\" = \"The response content\" }"; + response.setResponseJson(content); + assertEquals(content, response.getResponseContent()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetResponseJson() { + final RestfulResponse response = new RestfulResponse(); + assertEquals(null, response.getResponseContent()); + + final String content = "{ \"content\" = \"The response content\" }"; + response.setResponseJson(content); + assertEquals(content, response.getResponseContent()); + } +} diff --git a/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestServiceUtil.java b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestServiceUtil.java new file mode 100644 index 0000000..3dcca58 --- /dev/null +++ b/service/src/test/java/org/onap/vfc/nfvo/multivimproxy/common/util/restclient/TestServiceUtil.java @@ -0,0 +1,92 @@ +/* + * 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.restclient; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class TestServiceUtil { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetServiceHost() { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetServicePort() { + } +} |