summaryrefslogtreecommitdiffstats
path: root/adapters
diff options
context:
space:
mode:
authorc00149107 <chenchuanyu@huawei.com>2017-09-15 16:54:46 +0800
committerChuanyu Chen <chenchuanyu@huawei.com>2017-09-15 09:06:04 +0000
commit4671cbb50f4520924696f0fea3fe8471f6fbe811 (patch)
treefabc536d453ef1d6ae16d06a54d3b2c7fca052ca /adapters
parent3dea986a2280e2e4c1108c897a2839606b74a174 (diff)
Add UT for create service
Create service UT implement Change-Id: Ie9f1abbe730195b94796ca9cabb5e385c5dd0ea8 Issue-ID:SO-133 Signed-off-by: c00149107 <chenchuanyu@huawei.com>
Diffstat (limited to 'adapters')
-rw-r--r--adapters/mso-vfc-adapter/pom.xml24
-rw-r--r--adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcAdapterRest.java98
-rw-r--r--adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/VfcManager.java13
-rw-r--r--adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/exceptions/ApplicationException.java45
-rw-r--r--adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/JsonUtil.java19
-rw-r--r--adapters/mso-vfc-adapter/src/main/java/org/openecomp/mso/adapters/vfc/util/ValidateUtil.java5
-rw-r--r--adapters/mso-vfc-adapter/src/test/java/org/openecomp/mso/adapters/vfc/VfcAdapterTest.java225
7 files changed, 301 insertions, 128 deletions
diff --git a/adapters/mso-vfc-adapter/pom.xml b/adapters/mso-vfc-adapter/pom.xml
index 4f049c350f..7cb8eeacec 100644
--- a/adapters/mso-vfc-adapter/pom.xml
+++ b/adapters/mso-vfc-adapter/pom.xml
@@ -33,13 +33,18 @@
</build>
<dependencies>
- <dependency>
+ <!--<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
- </dependency>
+ </dependency>-->
<dependency>
+ <groupId>javax.ws.rs</groupId>
+ <artifactId>javax.ws.rs-api</artifactId>
+ <version>2.0</version>
+</dependency>
+ <dependency>
<groupId>org.openecomp.so.adapters</groupId>
<artifactId>mso-adapter-utils</artifactId>
<version>${project.version}</version>
@@ -83,6 +88,10 @@
<artifactId>mso-requests-db</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
@@ -101,5 +110,16 @@
<version>1.19</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.glassfish.jersey.core</groupId>
+ <artifactId>jersey-common</artifactId>
+ <version>2.22.2</version>
+ <scope>test</scope>
+</dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ <version>3.1.0</version>
+ </dependency>
</dependencies>
</project>
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;
* </p>
*
* @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
* <br>
@@ -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 {
* <br>
*
* @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
* <br>
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
* <br>
@@ -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<br/>
* <p>
@@ -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
+ * <br>
+ *
+ * @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.<br/>
* <p>
@@ -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> T unMarshal(String jsonstr, Class<T> type) {
+ public static <T> T unMarshal(String jsonstr, Class<T> 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> T unMarshal(String jsonstr, TypeReference<T> type) {
+ public static <T> T unMarshal(String jsonstr, TypeReference<T> 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.");
diff --git a/adapters/mso-vfc-adapter/src/test/java/org/openecomp/mso/adapters/vfc/VfcAdapterTest.java b/adapters/mso-vfc-adapter/src/test/java/org/openecomp/mso/adapters/vfc/VfcAdapterTest.java
index 46ab134bce..4c316f3196 100644
--- a/adapters/mso-vfc-adapter/src/test/java/org/openecomp/mso/adapters/vfc/VfcAdapterTest.java
+++ b/adapters/mso-vfc-adapter/src/test/java/org/openecomp/mso/adapters/vfc/VfcAdapterTest.java
@@ -17,17 +17,29 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
-package org.openecomp.mso.adapters.vfc;
+package org.openecomp.mso.adapters.vfc;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
-import org.mockito.Mock;
+import org.openecomp.mso.adapters.vfc.constant.CommonConstant;
+import org.openecomp.mso.adapters.vfc.constant.HttpCode;
+import org.openecomp.mso.adapters.vfc.model.RestfulResponse;
import org.openecomp.mso.adapters.vfc.util.RestfulUtil;
import org.openecomp.mso.adapters.vfc.util.ValidateUtil;
-import org.openecomp.mso.db.catalog.CatalogDatabase;
+import org.openecomp.mso.requestsdb.RequestsDatabase;
+import org.openecomp.mso.requestsdb.ResourceOperationStatus;
+import mockit.Mock;
import mockit.MockUp;
/**
@@ -37,89 +49,150 @@ import mockit.MockUp;
* </p>
*
* @author
- * @version ONAP Amsterdam Release 2017-08-31
+ * @version ONAP Amsterdam Release 2017-08-31
*/
public class VfcAdapterTest {
- @Mock
- private static CatalogDatabase db;
+ private VfcAdapterRest vfcAdapter = new VfcAdapterRest();
/**
* File path
*/
private static final String FILE_PATH = "src/test/resources/json/";
- @Test
- public void createTest () {
-// // get request
-// mockGetRequestBody(FILE_PATH + "createNfvoNsReq.json");
-// // get service template
-// ServiceTemplate svcTmpl = new ServiceTemplate();
-// svcTmpl.setId("id");
-// svcTmpl.setServiceTemplateId("svcTmplId");
-// new MockUp<CatalogProxyImpl>() {
-// @Mock
-// public ServiceTemplate getSvcTmplByNodeType(String nodeType, String domainHost){
-// return svcTmpl;
-// }
-// };
-// // get response
-// RestfulResponse restRsp = new RestfulResponse();
-// restRsp.setStatus(HttpStatus.SC_OK);
-// restRsp.setResponseJson(getJsonString(FILE_PATH + "createNfvoNsRsp.json"));
-// mockGetRestfulRsp(restRsp);
-// // insert data
-// new MockUp<ServiceSegmentDaoImpl>() {
-// @Mock
-// public void insertSegment(ServiceSegmentModel serviceSegment) {
-// // do nothing
-// }
-// @Mock
-// public void insertSegmentOper(ServiceSegmentOperation svcSegmentOper) {
-// // do nothing
-// }
-// };
-// Response rsp = impl.createNfvoNs(servletReq);
-// JSONObject obj = JSONObject.fromObject(rsp.getEntity());
-// Assert.assertEquals(null, "1", obj.getString("nsInstanceId"));
+ /**
+ * Mock the request body form a file
+ * <br>
+ *
+ * @param fileName
+ * @since ONAP Amsterdam Release
+ */
+ private void mockRestfulUtil(String fileName) {
+ new MockUp<RestfulUtil>() {
+
+ /**
+ * mock get request body
+ * <br>
+ *
+ * @param request
+ * @return
+ * @since ONAP Amsterdam Release
+ */
+ @Mock
+ public String getRequestBody(HttpServletRequest request) {
+ return getJsonString(fileName);
+ }
+
+ /**
+ * mock get send method
+ * <br>
+ *
+ * @param url
+ * @param methodType
+ * @param content
+ * @return
+ * @since ONAP Amsterdam Release
+ */
+ @Mock
+ public RestfulResponse send(String url, String methodType, String content) {
+ if(url.contains(CommonConstant.NFVO_CREATE_URL) && methodType.equals(CommonConstant.MethodType.POST)) {
+ return getResponse("createNsRsp.json");
+ } else {
+ return null;
+ }
+ }
+ };
+ }
+
+ /**
+ * Mock the request body form a file
+ * <br>
+ *
+ * @param fileName
+ * @since ONAP Amsterdam Release
+ */
+ private void mockRequestDatabase() {
+ new MockUp<RequestsDatabase>() {
+
+ /**
+ * mock get resource operation status
+ * <br>
+ *
+ * @param request
+ * @return
+ * @since ONAP Amsterdam Release
+ */
+ @Mock
+ public ResourceOperationStatus getResourceOperationStatus(String serviceId, String operationId,
+ String resourceTemplateUUID) {
+ ResourceOperationStatus resStatus = new ResourceOperationStatus();
+ resStatus.setServiceId("111");
+ resStatus.setOperationId("111");
+ return resStatus;
+ }
+
+ /**
+ * Mock update Res Oper Status
+ * <br>
+ *
+ * @param operStatus
+ * @since ONAP Amsterdam Release
+ */
+ @Mock
+ public void updateResOperStatus(ResourceOperationStatus operStatus) {
+
+ }
+ };
+ }
+
+ /**
+ * Before executing UT, start mock requst database
+ * <br>
+ *
+ * @since ONAP Amsterdam Release
+ */
+ @Before
+ public void start() {
+ mockRequestDatabase();
+ }
+
+ /**
+ * After executing UT, close session<br/>
+ *
+ * @since ONAP Amsterdam Release
+ */
+ @After
+ public void stop() {
+
}
@Test
- public void deleteTest () {
-
+ public void createTest() {
+ // get request
+ mockRestfulUtil(FILE_PATH + "createNsReq.json");
+ vfcAdapter.createNfvoNs(null);
}
@Test
- public void instantiateTest () {
+ public void deleteTest() {
}
@Test
- public void terminateTest () {
-
+ public void instantiateTest() {
+
}
@Test
- public void queryJobTest () {
+ public void terminateTest() {
}
-
- /**
- * Mock to get request body.<br/>
- *
- * @param file json file path.
- * @since ONAP Amsterdam Release 2017-9-6
- */
- private void mockGetRequestBody(final String file) {
- new MockUp<RestfulUtil>() {
-// @Mock
-// public String getRequestBody(HttpServletRequest request) {
-// return getJsonString(file);
-// }
- };
+ @Test
+ public void queryJobTest() {
+
}
-
+
/**
* Get json string from file.<br/>
*
@@ -128,19 +201,37 @@ public class VfcAdapterTest {
* @throws IOException when fail to read
* @since ONAP Amsterdam Release 2017-9-6
*/
+ @SuppressWarnings("deprecation")
private String getJsonString(final String file) {
if(ValidateUtil.isStrEmpty(file)) {
return "";
}
String json = null;
-// try {
-// FileInputStream fileStream = new FileInputStream(new File(file));
-// json = IOUtils.toString(fileStream);
-// } catch(Exception e) {
-// Assert.fail(e.getMessage());
-// }
-
+ try {
+ FileInputStream fileStream = new FileInputStream(new File(file));
+ json = IOUtils.toString(fileStream);
+ } catch(Exception e) {
+ Assert.fail(e.getMessage());
+ }
return json;
}
+
+ /**
+ * get the response from file
+ * <br>
+ *
+ * @param fileName
+ * @return
+ * @since ONAP Amsterdam Release
+ */
+ private RestfulResponse getResponse(String fileName) {
+ RestfulResponse responseSuccess = new RestfulResponse();
+ responseSuccess.setStatus(HttpCode.RESPOND_OK);
+ if(null != fileName) {
+ String jsonStr = getJsonString(FILE_PATH + fileName);
+ responseSuccess.setResponseContent(jsonStr);
+ }
+ return responseSuccess;
+ }
}