2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.adapters.vfc;
24 import java.io.FileInputStream;
25 import java.io.IOException;
27 import javax.servlet.http.HttpServletRequest;
29 import org.apache.commons.io.IOUtils;
30 import org.junit.After;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.openecomp.mso.adapters.vfc.constant.CommonConstant;
35 import org.openecomp.mso.adapters.vfc.constant.HttpCode;
36 import org.openecomp.mso.adapters.vfc.model.RestfulResponse;
37 import org.openecomp.mso.adapters.vfc.util.RestfulUtil;
38 import org.openecomp.mso.adapters.vfc.util.ValidateUtil;
39 import org.openecomp.mso.requestsdb.RequestsDatabase;
40 import org.openecomp.mso.requestsdb.ResourceOperationStatus;
52 * @version ONAP Amsterdam Release 2017-08-31
54 public class VfcAdapterTest {
56 private VfcAdapterRest vfcAdapter = new VfcAdapterRest();
61 private static final String FILE_PATH = "src/test/resources/json/";
64 * Mock the request body form a file
68 * @since ONAP Amsterdam Release
70 private void mockRestfulUtil(String fileName) {
71 new MockUp<RestfulUtil>() {
74 * mock get request body
79 * @since ONAP Amsterdam Release
82 public String getRequestBody(HttpServletRequest request) {
83 return getJsonString(fileName);
87 * mock get send method
94 * @since ONAP Amsterdam Release
97 public RestfulResponse send(String url, String methodType, String content) {
98 if(url.equals(CommonConstant.NFVO_CREATE_URL) && methodType.equals(CommonConstant.MethodType.POST)) {
99 return getResponse("createNsRsp.json");
100 } else if(url.contains("instantiate") && methodType.equals(CommonConstant.MethodType.POST)) {
101 return getResponse("instantiateNsRsp.json");
102 } else if(methodType.equals(CommonConstant.MethodType.DELETE)) {
103 return getResponse(null);
104 } else if(url.contains("terminate") && methodType.equals(CommonConstant.MethodType.POST)) {
105 return getResponse("terminateNsRsp.json");
106 } else if(url.contains("/api/nslcm/v1/jobs") && methodType.equals(CommonConstant.MethodType.GET)) {
107 return getResponse("queryJobRsp.json");
117 * Mock the request body form a file
121 * @since ONAP Amsterdam Release
123 private void mockRequestDatabase() {
124 new MockUp<RequestsDatabase>() {
127 * mock get resource operation status
132 * @since ONAP Amsterdam Release
135 public ResourceOperationStatus getResourceOperationStatus(String serviceId, String operationId,
136 String resourceTemplateUUID) {
137 ResourceOperationStatus resStatus = new ResourceOperationStatus();
138 resStatus.setServiceId("111");
139 resStatus.setOperationId("111");
144 * Mock update Res Oper Status
148 * @since ONAP Amsterdam Release
151 public void updateResOperStatus(ResourceOperationStatus operStatus) {
158 * Before executing UT, start mock requst database
161 * @since ONAP Amsterdam Release
164 public void start() {
165 mockRequestDatabase();
169 * After executing UT, close session<br/>
171 * @since ONAP Amsterdam Release
179 public void createTest() {
181 mockRestfulUtil(FILE_PATH + "createNsReq.json");
182 vfcAdapter.createNfvoNs(null);
186 public void deleteTest() {
188 mockRestfulUtil(FILE_PATH + "deleteNsReq.json");
189 vfcAdapter.deleteNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
193 public void instantiateTest() {
195 mockRestfulUtil(FILE_PATH + "instantiateNsReq.json");
196 vfcAdapter.instantiateNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
200 public void terminateTest() {
201 mockRestfulUtil(FILE_PATH + "terminateNsReq.json");
202 vfcAdapter.terminateNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
206 public void queryJobTest() {
207 mockRestfulUtil(FILE_PATH + "queryJobReq.json");
208 vfcAdapter.queryNfvoJobStatus(null, "1");
212 * Get json string from file.<br/>
214 * @param file the path of file
215 * @return json string
216 * @throws IOException when fail to read
217 * @since ONAP Amsterdam Release 2017-9-6
219 @SuppressWarnings("deprecation")
220 private String getJsonString(final String file) {
221 if(ValidateUtil.isStrEmpty(file)) {
227 FileInputStream fileStream = new FileInputStream(new File(file));
228 json = IOUtils.toString(fileStream);
229 } catch(Exception e) {
230 Assert.fail(e.getMessage());
236 * get the response from file
241 * @since ONAP Amsterdam Release
243 private RestfulResponse getResponse(String fileName) {
244 RestfulResponse responseSuccess = new RestfulResponse();
245 responseSuccess.setStatus(HttpCode.RESPOND_OK);
246 if(null != fileName) {
247 String jsonStr = getJsonString(FILE_PATH + fileName);
248 responseSuccess.setResponseContent(jsonStr);
250 return responseSuccess;