From 4671cbb50f4520924696f0fea3fe8471f6fbe811 Mon Sep 17 00:00:00 2001 From: c00149107 Date: Fri, 15 Sep 2017 16:54:46 +0800 Subject: Add UT for create service Create service UT implement Change-Id: Ie9f1abbe730195b94796ca9cabb5e385c5dd0ea8 Issue-ID:SO-133 Signed-off-by: c00149107 --- .../openecomp/mso/adapters/vfc/VfcAdapterRest.java | 98 ++++++++++++++-------- .../org/openecomp/mso/adapters/vfc/VfcManager.java | 13 +-- .../vfc/exceptions/ApplicationException.java | 45 ++++++++-- .../openecomp/mso/adapters/vfc/util/JsonUtil.java | 19 +++-- .../mso/adapters/vfc/util/ValidateUtil.java | 5 +- 5 files changed, 121 insertions(+), 59 deletions(-) (limited to 'adapters/mso-vfc-adapter/src/main') diff --git a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcAdapterRest.java b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcAdapterRest.java index 1be5bab1c3..438393bc35 100644 --- a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcAdapterRest.java +++ b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcAdapterRest.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.mso.adapters.vfc; import javax.servlet.http.HttpServletRequest; @@ -29,6 +30,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; +import org.openecomp.mso.adapters.vfc.exceptions.ApplicationException; import org.openecomp.mso.adapters.vfc.model.NSResourceInputParameter; import org.openecomp.mso.adapters.vfc.model.NsOperationKey; import org.openecomp.mso.adapters.vfc.model.RestfulResponse; @@ -44,7 +46,7 @@ import org.openecomp.mso.logger.MsoLogger; *

* * @author - * @version ONAP Amsterdam Release 2017-08-28 + * @version ONAP Amsterdam Release 2017-08-28 */ @Path("/v1/vfcdrivers") public class VfcAdapterRest { @@ -53,6 +55,10 @@ public class VfcAdapterRest { private final VfcManager driverMgr = new VfcManager(); + public VfcAdapterRest() { + + } + /** * Create a NS *
@@ -67,12 +73,16 @@ public class VfcAdapterRest { @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response createNfvoNs(HttpServletRequest servletReq) { // Step 1: get parameters from request for current node - String body = RestfulUtil.getRequestBody(servletReq); - ValidateUtil.assertObjectNotNull(body); - LOGGER.debug("body from request is {}" + body); - NSResourceInputParameter nsInput = JsonUtil.unMarshal(body, NSResourceInputParameter.class); - RestfulResponse rsp = driverMgr.createNs(nsInput); - return buildResponse(rsp); + try { + String body = RestfulUtil.getRequestBody(servletReq); + ValidateUtil.assertObjectNotNull(body); + LOGGER.debug("body from request is {}" + body); + NSResourceInputParameter nsInput = JsonUtil.unMarshal(body, NSResourceInputParameter.class); + RestfulResponse rsp = driverMgr.createNs(nsInput); + return buildResponse(rsp); + } catch(ApplicationException e) { + return e.buildErrorResponse(); + } } /** @@ -87,14 +97,17 @@ public class VfcAdapterRest { @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response deleteNfvoNs(HttpServletRequest servletReq, @PathParam("nsInstanceId") String nsInstanceId) { - // Step 1: get parameters from request for current node - String body = RestfulUtil.getRequestBody(servletReq); - ValidateUtil.assertObjectNotNull(body); - LOGGER.debug("body from request is {}" + body); - NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class); - - RestfulResponse rsp = driverMgr.deleteNs(nsOperationKey, nsInstanceId); - return buildResponse(rsp); + try { + // Step 1: get parameters from request for current node + String body = RestfulUtil.getRequestBody(servletReq); + ValidateUtil.assertObjectNotNull(body); + LOGGER.debug("body from request is {}" + body); + NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class); + RestfulResponse rsp = driverMgr.deleteNs(nsOperationKey, nsInstanceId); + return buildResponse(rsp); + } catch(ApplicationException e) { + return e.buildErrorResponse(); + } } /** @@ -102,7 +115,7 @@ public class VfcAdapterRest { *
* * @param servletReq The Http Request - * @param jobId The job id + * @param jobId The job id * @return * @since ONAP Amsterdam Release */ @@ -111,15 +124,19 @@ public class VfcAdapterRest { @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response queryNfvoJobStatus(HttpServletRequest servletReq, @PathParam("jobId") String jobId) { - ValidateUtil.assertObjectNotNull(jobId); - String body = RestfulUtil.getRequestBody(servletReq); - ValidateUtil.assertObjectNotNull(body); - LOGGER.debug("body from request is {}" + body); - NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class); - RestfulResponse rsp = driverMgr.getNsProgress(nsOperationKey, jobId); - return buildResponse(rsp); - } + try { + ValidateUtil.assertObjectNotNull(jobId); + String body = RestfulUtil.getRequestBody(servletReq); + ValidateUtil.assertObjectNotNull(body); + LOGGER.debug("body from request is {}" + body); + NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class); + RestfulResponse rsp = driverMgr.getNsProgress(nsOperationKey, jobId); + return buildResponse(rsp); + } catch(ApplicationException e) { + return e.buildErrorResponse(); + } + } /** * Instantiate NS instance @@ -136,11 +153,15 @@ public class VfcAdapterRest { @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response instantiateNfvoNs(HttpServletRequest servletReq, @PathParam("nsInstanceId") String nsInstanceId) { String body = RestfulUtil.getRequestBody(servletReq); - ValidateUtil.assertObjectNotNull(body); - LOGGER.debug("body from request is {}" + body); - NSResourceInputParameter nsInput = JsonUtil.unMarshal(body, NSResourceInputParameter.class); - RestfulResponse rsp = driverMgr.instantiateNs(nsInstanceId, nsInput); - return buildResponse(rsp); + try { + ValidateUtil.assertObjectNotNull(body); + LOGGER.debug("body from request is {}" + body); + NSResourceInputParameter nsInput = JsonUtil.unMarshal(body, NSResourceInputParameter.class); + RestfulResponse rsp = driverMgr.instantiateNs(nsInstanceId, nsInput); + return buildResponse(rsp); + } catch(ApplicationException e) { + return e.buildErrorResponse(); + } } /** @@ -157,16 +178,19 @@ public class VfcAdapterRest { @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response terminateNfvoNs(HttpServletRequest servletReq, @PathParam("nsInstanceId") String nsInstanceId) { - ValidateUtil.assertObjectNotNull(nsInstanceId); - String body = RestfulUtil.getRequestBody(servletReq); - ValidateUtil.assertObjectNotNull(body); - LOGGER.debug("body from request is {}" + body); - NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class); - RestfulResponse rsp = driverMgr.terminateNs(nsOperationKey, nsInstanceId); - return buildResponse(rsp); + try { + ValidateUtil.assertObjectNotNull(nsInstanceId); + String body = RestfulUtil.getRequestBody(servletReq); + ValidateUtil.assertObjectNotNull(body); + LOGGER.debug("body from request is {}" + body); + NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class); + RestfulResponse rsp = driverMgr.terminateNs(nsOperationKey, nsInstanceId); + return buildResponse(rsp); + } catch(ApplicationException e) { + return e.buildErrorResponse(); + } } - /** * build response from restful response *
diff --git a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcManager.java b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcManager.java index 171949aa8e..f463f5e344 100644 --- a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcManager.java +++ b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcManager.java @@ -71,6 +71,9 @@ public class VfcManager { nfvoUrlMap.put(Step.QUERY, CommonConstant.NFVO_QUERY_URL); } + public VfcManager(){ + + } /** * create network service *
@@ -79,7 +82,7 @@ public class VfcManager { * @return * @since ONAP Amsterdam Release */ - public RestfulResponse createNs(NSResourceInputParameter segInput) { + public RestfulResponse createNs(NSResourceInputParameter segInput) throws ApplicationException { // Step1: get service template by node type String nsdId = segInput.getNsOperationKey().getNodeTemplateId(); @@ -143,7 +146,7 @@ public class VfcManager { * @return * @since ONAP Amsterdam Release */ - public RestfulResponse deleteNs(NsOperationKey nsOperationKey, String nsInstanceId) { + public RestfulResponse deleteNs(NsOperationKey nsOperationKey, String nsInstanceId) throws ApplicationException{ LOGGER.info("delete ns -> begin"); // Step1: prepare url and methodType String url = getUrl(nsInstanceId, CommonConstant.Step.DELETE); @@ -190,7 +193,7 @@ public class VfcManager { * @return * @since ONAP Amsterdam Release */ - public RestfulResponse instantiateNs(String nsInstanceId, NSResourceInputParameter segInput) { + public RestfulResponse instantiateNs(String nsInstanceId, NSResourceInputParameter segInput) throws ApplicationException{ // Call the NFVO or SDNO service to instantiate service LOGGER.info("instantiate ns -> begin"); @@ -254,7 +257,7 @@ public class VfcManager { * @return * @since ONAP Amsterdam Release */ - public RestfulResponse terminateNs(NsOperationKey nsOperationKey, String nsInstanceId) { + public RestfulResponse terminateNs(NsOperationKey nsOperationKey, String nsInstanceId) throws ApplicationException{ // Step1: save segment operation info for delete process LOGGER.info("save segment operation for delete process"); ResourceOperationStatus nsOperInfo = (RequestsDatabase.getInstance()).getResourceOperationStatus(nsOperationKey.getServiceId(), @@ -319,7 +322,7 @@ public class VfcManager { * @return * @since ONAP Amsterdam Release */ - public RestfulResponse getNsProgress(NsOperationKey nsOperationKey, String jobId) { + public RestfulResponse getNsProgress(NsOperationKey nsOperationKey, String jobId) throws ApplicationException{ ValidateUtil.assertObjectNotNull(jobId); // Step 1: query the current resource operation status diff --git a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/exceptions/ApplicationException.java b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/exceptions/ApplicationException.java index 6a78d2e73e..61966d07f2 100644 --- a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/exceptions/ApplicationException.java +++ b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/exceptions/ApplicationException.java @@ -17,19 +17,22 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.mso.adapters.vfc.exceptions; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response;; -public class ApplicationException extends WebApplicationException { +public class ApplicationException extends Exception { /** * Serial number. */ private static final long serialVersionUID = 1L; + private int errorCode; + + private String errorMsg; + /** * Constructor
*

@@ -37,9 +40,37 @@ public class ApplicationException extends WebApplicationException { * * @param errorCode error status * @param errorDetail error detail - * @since ONAP Amsterdam Release 2017-9-6 + * @since ONAP Amsterdam Release 2017-9-6 + */ + public ApplicationException(int errorCode, String errorMsg) { + this.errorCode = errorCode; + this.errorMsg = errorMsg; + } + + public int getErrorCode() { + return errorCode; + } + + public void setErrorCode(int errorCode) { + this.errorCode = errorCode; + } + + public String getErrorMsg() { + return errorMsg; + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } + + /** + * build error Response + *
+ * + * @return + * @since ONAP Amsterdam Release */ - public ApplicationException(int errorCode, Object errorDetail) { - super(Response.status(errorCode).entity(errorDetail).type(MediaType.APPLICATION_JSON).build()); + public Response buildErrorResponse() { + return Response.status(errorCode).entity(errorMsg).build(); } } diff --git a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/JsonUtil.java b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/JsonUtil.java index 34beb02c13..a9546d5c4c 100644 --- a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/JsonUtil.java +++ b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/JsonUtil.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.mso.adapters.vfc.util; import java.io.IOException; @@ -29,7 +30,6 @@ import org.openecomp.mso.adapters.vfc.exceptions.ApplicationException; import org.openecomp.mso.logger.MessageEnum; import org.openecomp.mso.logger.MsoLogger; - /** * Interface for json analyzing.
*

@@ -43,7 +43,7 @@ public class JsonUtil { /** * Log service */ - private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA); + private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA); /** * Mapper. @@ -75,11 +75,12 @@ public class JsonUtil { * @return model object * @since ONAP Amsterdam Release 2017-9-6 */ - public static T unMarshal(String jsonstr, Class type) { + public static T unMarshal(String jsonstr, Class type) throws ApplicationException { try { return MAPPER.readValue(jsonstr, type); } catch(IOException e) { - LOGGER.error(MessageEnum.RA_NS_EXC, "","", MsoLogger.ErrorCode.BusinessProcesssError, "fail to unMarshal json", e); + LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError, + "fail to unMarshal json", e); throw new ApplicationException(HttpCode.BAD_REQUEST, "fail to unMarshal json"); } } @@ -92,11 +93,12 @@ public class JsonUtil { * @return model object * @since ONAP Amsterdam Release 2017-9-6 */ - public static T unMarshal(String jsonstr, TypeReference type) { + public static T unMarshal(String jsonstr, TypeReference type) throws ApplicationException { try { return MAPPER.readValue(jsonstr, type); } catch(IOException e) { - LOGGER.error(MessageEnum.RA_NS_EXC, "","", MsoLogger.ErrorCode.BusinessProcesssError, "fail to unMarshal json", e); + LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError, + "fail to unMarshal json", e); throw new ApplicationException(HttpCode.BAD_REQUEST, "fail to unMarshal json"); } } @@ -108,11 +110,12 @@ public class JsonUtil { * @return json string * @since ONAP Amsterdam Release 2017-9-6 */ - public static String marshal(Object srcObj) { + public static String marshal(Object srcObj) throws ApplicationException { try { return MAPPER.writeValueAsString(srcObj); } catch(IOException e) { - LOGGER.error(MessageEnum.RA_NS_EXC, "","", MsoLogger.ErrorCode.BusinessProcesssError, "fail to marshal json", e); + LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError, + "fail to marshal json", e); throw new ApplicationException(HttpCode.BAD_REQUEST, "srcObj marshal failed!"); } } diff --git a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/ValidateUtil.java b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/ValidateUtil.java index 37228c805c..a6fa2d29e2 100644 --- a/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/ValidateUtil.java +++ b/adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/ValidateUtil.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.mso.adapters.vfc.util; import org.openecomp.mso.adapters.vfc.constant.HttpCode; @@ -49,7 +50,7 @@ public class ValidateUtil { * @param name of parameter * @since ONAP Amsterdam Release 2017-9-6 */ - public static void assertStringNotNull(String paramValue, String paramName) { + public static void assertStringNotNull(String paramValue, String paramName) throws ApplicationException { if(null != paramValue && !paramValue.isEmpty()) { return; } @@ -64,7 +65,7 @@ public class ValidateUtil { * @param object data object * @since ONAP Amsterdam Release 2017-9-6 */ - public static void assertObjectNotNull(Object object) { + public static void assertObjectNotNull(Object object) throws ApplicationException { if(null == object) { LOGGER.error("Object is null."); throw new ApplicationException(HttpCode.BAD_REQUEST, "Object is null."); -- cgit 1.2.3-korg