From 10bf323945c80e9f240fdb497e27a57f82d00239 Mon Sep 17 00:00:00 2001 From: luxin Date: Tue, 20 Mar 2018 14:59:09 +0800 Subject: Add test case for VnfRoa.java Change-Id: Iad4d2abdc588c7433f092dc63abedbacd2f6cf0b Issue-ID: VFC-833 Signed-off-by: luxin --- .../jujuvnfmadapter/service/rest/VnfRoaTest.java | 256 +++++++++++++++++---- 1 file changed, 209 insertions(+), 47 deletions(-) diff --git a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/service/rest/VnfRoaTest.java b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/service/rest/VnfRoaTest.java index 31d8bcd..9ba8f8a 100644 --- a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/service/rest/VnfRoaTest.java +++ b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/service/rest/VnfRoaTest.java @@ -13,22 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.rest; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.PathParam; -import javax.ws.rs.core.Context; import org.junit.Before; import org.junit.Test; import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.StringUtil; +import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.restclient.ServiceException; import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.entity.JujuVnfmInfo; import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.process.VnfMgr; -import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.rest.VnfRoa; -import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.restclient.ServiceException; import org.springframework.mock.web.MockHttpServletResponse; import mockit.Mock; @@ -36,16 +34,18 @@ import mockit.MockUp; import net.sf.json.JSONObject; public class VnfRoaTest { - + VnfRoa roa = new VnfRoa(); + @Before - public void setUp(){ + public void setUp() { roa.setVnfMgr(new VnfMgr()); } - + @Test - public void addVnfTestNull() throws ServiceException{ - new MockUp(){ + public void addVnfTestNull() throws ServiceException { + new MockUp() { + @Mock public T getJsonFromContexts(HttpServletRequest vnfReq) { return null; @@ -57,33 +57,75 @@ public class VnfRoaTest { String res = roa.addVnf(context, resp, vnfmId); assertNotNull(res); } - + + @Test + public void addVnfTest() throws ServiceException { + new MockUp() { + + @Mock + public T getJsonFromContexts(HttpServletRequest vnfReq) { + String vnfJsonStr = "{}"; + return (T)JSONObject.fromObject(vnfJsonStr); + } + }; + new MockUp() { + + @Mock + public JSONObject addVnf(JSONObject vnfObject, String vnfmId) { + JSONObject result = new JSONObject(); + result.put("retCode", -1); + return result; + } + }; + HttpServletRequest context = null; + HttpServletResponse resp = new MockHttpServletResponse(); + String vnfmId = "1234"; + String res = roa.addVnf(context, resp, vnfmId); + assertNotNull(res); + } + @Test - public void addVnfTest() throws ServiceException{ - new MockUp(){ + public void addVnfTestSucce() throws ServiceException { + new MockUp() { + @Mock public T getJsonFromContexts(HttpServletRequest vnfReq) { String vnfJsonStr = "{}"; return (T)JSONObject.fromObject(vnfJsonStr); } }; + new MockUp() { + + @Mock + public JSONObject addVnf(JSONObject vnfObject, String vnfmId) { + JSONObject result = new JSONObject(); + result.put("retCode", 1); + JSONObject data = new JSONObject(); + data.put("result", "success"); + result.put("data", data); + return result; + } + }; HttpServletRequest context = null; HttpServletResponse resp = new MockHttpServletResponse(); String vnfmId = "1234"; String res = roa.addVnf(context, resp, vnfmId); assertNotNull(res); } + @Test - public void delVnfTestNull() throws ServiceException{ - new MockUp(){ + public void delVnfTestNull() throws ServiceException { + new MockUp() { + @Mock public T getJsonFromContexts(HttpServletRequest vnfReq) { return null; } }; - new MockUp(){ + new MockUp() { + @Mock - private JujuVnfmInfo findByVnfId(String vnfId){ + private JujuVnfmInfo findByVnfId(String vnfId) { JujuVnfmInfo info = new JujuVnfmInfo(); info.setVnfmId("1234"); return info; @@ -92,48 +134,108 @@ public class VnfRoaTest { HttpServletRequest context = null; HttpServletResponse resp = new MockHttpServletResponse(); String vnfmId = "1234"; - String res = roa.delVnf("vnfmId", resp, "vnfInstanceId",context); + String res = roa.delVnf("vnfmId", resp, "vnfInstanceId", context); + assertNotNull(res); + } + + @Test + public void delVnf2TestNull() throws ServiceException { + new MockUp() { + + @Mock + public T getJsonFromContexts(HttpServletRequest vnfReq) { + return null; + } + }; + HttpServletRequest context = null; + HttpServletResponse resp = new MockHttpServletResponse(); + String vnfmId = "1234"; + String res = roa.delVnf(null, resp, "vnfInstanceId", context); + assertNotNull(res); + } + + @Test + public void delVnf3TestNull() throws ServiceException { + new MockUp() { + + @Mock + public T getJsonFromContexts(HttpServletRequest vnfReq) { + return null; + } + }; + HttpServletRequest context = null; + HttpServletResponse resp = new MockHttpServletResponse(); + String vnfmId = "1234"; + String res = roa.delVnf(vnfmId, resp, null, context); assertNotNull(res); } + @Test - public void delVnf2TestNull() throws ServiceException{ - new MockUp(){ + public void delVnfSuccess() throws ServiceException { + new MockUp() { + @Mock public T getJsonFromContexts(HttpServletRequest vnfReq) { return null; } }; + new MockUp() { + + @Mock + public JSONObject deleteVnf(String vnfId, String vnfmId, JSONObject vnfObject) { + JSONObject result = new JSONObject(); + result.put("retCode", 1); + JSONObject data = new JSONObject(); + data.put("result", "success"); + result.put("data", data); + return result; + } + }; HttpServletRequest context = null; HttpServletResponse resp = new MockHttpServletResponse(); String vnfmId = "1234"; - String res = roa.delVnf(null, resp, "vnfInstanceId",context); + String res = roa.delVnf(vnfmId, resp, "vnfInstanceId", context); assertNotNull(res); } + @Test - public void delVnf3TestNull() throws ServiceException{ - new MockUp(){ + public void delVnfFail() throws ServiceException { + new MockUp() { + @Mock public T getJsonFromContexts(HttpServletRequest vnfReq) { return null; } }; + new MockUp() { + + @Mock + public JSONObject deleteVnf(String vnfId, String vnfmId, JSONObject vnfObject) { + JSONObject result = new JSONObject(); + result.put("retCode", -1); + return result; + } + }; HttpServletRequest context = null; HttpServletResponse resp = new MockHttpServletResponse(); String vnfmId = "1234"; - String res = roa.delVnf(vnfmId, resp, null,context); + String res = roa.delVnf(null, resp, "vnfInstanceId", context); assertNotNull(res); } + @Test - public void getVnf1TestNull() throws ServiceException{ - new MockUp(){ + public void getVnf1TestNull() throws ServiceException { + new MockUp() { + @Mock public T getJsonFromContexts(HttpServletRequest vnfReq) { return null; } }; - new MockUp(){ + new MockUp() { + @Mock - private JujuVnfmInfo findByVnfId(String vnfId){ + private JujuVnfmInfo findByVnfId(String vnfId) { JujuVnfmInfo info = new JujuVnfmInfo(); info.setVnfmId("1234"); return info; @@ -142,12 +244,67 @@ public class VnfRoaTest { HttpServletRequest context = null; HttpServletResponse resp = new MockHttpServletResponse(); String vnfmId = "1234"; - String res = roa.getVnf(vnfmId, resp, "vnfInstanceId",context); + String res = roa.getVnf(vnfmId, resp, "vnfInstanceId", context); + assertNotNull(res); + } + + @Test + public void getVnfSuccess() throws ServiceException { + new MockUp() { + + @Mock + public T getJsonFromContexts(HttpServletRequest vnfReq) { + return null; + } + }; + new MockUp() { + + @Mock + public JSONObject getVnf(String vnfId, String vnfmId) { + JSONObject result = new JSONObject(); + result.put("retCode", 1); + JSONObject data = new JSONObject(); + data.put("result", "success"); + result.put("data", data); + return result; + } + }; + HttpServletRequest context = null; + HttpServletResponse resp = new MockHttpServletResponse(); + String vnfmId = "1234"; + String res = roa.getVnf(vnfmId, resp, "vnfInstanceId", context); + assertNotNull(res); + } + + @Test + public void getVnfFail() throws ServiceException { + new MockUp() { + + @Mock + public T getJsonFromContexts(HttpServletRequest vnfReq) { + return null; + } + }; + new MockUp() { + + @Mock + public JSONObject getVnf(String vnfId, String vnfmId) { + JSONObject result = new JSONObject(); + result.put("retCode", -1); + return result; + } + }; + HttpServletRequest context = null; + HttpServletResponse resp = new MockHttpServletResponse(); + String vnfmId = "1234"; + String res = roa.getVnf(vnfmId, resp, "vnfInstanceId", context); assertNotNull(res); } + @Test - public void getVnf2TestNull() throws ServiceException{ - new MockUp(){ + public void getVnf2TestNull() throws ServiceException { + new MockUp() { + @Mock public T getJsonFromContexts(HttpServletRequest vnfReq) { return null; @@ -156,12 +313,14 @@ public class VnfRoaTest { HttpServletRequest context = null; HttpServletResponse resp = new MockHttpServletResponse(); String vnfmId = "1234"; - String res = roa.getVnf(null, resp, "vnfInstanceId",context); + String res = roa.getVnf(null, resp, "vnfInstanceId", context); assertNotNull(res); } + @Test - public void getVnf3TestNull() throws ServiceException{ - new MockUp(){ + public void getVnf3TestNull() throws ServiceException { + new MockUp() { + @Mock public T getJsonFromContexts(HttpServletRequest vnfReq) { return null; @@ -170,31 +329,35 @@ public class VnfRoaTest { HttpServletRequest context = null; HttpServletResponse resp = new MockHttpServletResponse(); String vnfmId = "1234"; - String res = roa.getVnf(vnfmId, resp, null,context); + String res = roa.getVnf(vnfmId, resp, null, context); assertNotNull(res); } + @Test - public void getJobTestNull() throws ServiceException{ + public void getJobTestNull() throws ServiceException { HttpServletResponse resp = new MockHttpServletResponse(); - String res = roa.getJob("jobId", "vnfmId", resp,"responseId"); + String res = roa.getJob("jobId", "vnfmId", resp, "responseId"); assertNotNull(res); } + @Test - public void getJobTest2Null() throws ServiceException{ + public void getJobTest2Null() throws ServiceException { HttpServletResponse resp = new MockHttpServletResponse(); - String res = roa.getJob(null, "vnfmId", resp,"responseId"); + String res = roa.getJob(null, "vnfmId", resp, "responseId"); assertNotNull(res); } + @Test - public void getJobTest3Null() throws ServiceException{ + public void getJobTest3Null() throws ServiceException { HttpServletResponse resp = new MockHttpServletResponse(); - String res = roa.getJob("jobId", null, resp,"responseId"); + String res = roa.getJob("jobId", null, resp, "responseId"); assertNotNull(res); } - + @Test - public void getJobTestNormal() throws ServiceException{ - new MockUp(){ + public void getJobTestNormal() throws ServiceException { + new MockUp() { + @Mock public JSONObject getJob(String jobId, String vnfmId) { JSONObject obj = new JSONObject(); @@ -205,12 +368,11 @@ public class VnfRoaTest { obj.put("retCode", 1); return obj; } - + }; HttpServletResponse resp = new MockHttpServletResponse(); - String res = roa.getJob("jobId", "vnfmId", resp,"responseId"); + String res = roa.getJob("jobId", "vnfmId", resp, "responseId"); assertNotNull(res); } - } -- cgit 1.2.3-korg