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;
46 * VF-C adapter UT <br>
51 * @version ONAP Amsterdam Release 2017-08-31
53 public class VfcAdapterTest {
55 private VfcAdapterRest vfcAdapter = new VfcAdapterRest();
60 private static final String FILE_PATH = "src/test/resources/json/";
63 * Mock the request body form a file <br>
66 * @since ONAP Amsterdam Release
68 private void mockRestfulUtil(String fileName) {
69 new MockUp<RestfulUtil>() {
72 * mock get request body <br>
76 * @since ONAP Amsterdam Release
79 public String getRequestBody(HttpServletRequest request) {
80 return getJsonString(fileName);
84 * mock get send method <br>
90 * @since ONAP Amsterdam Release
93 public RestfulResponse send(String url, String methodType, String content) {
94 if (url.equals(CommonConstant.NFVO_CREATE_URL)
95 && methodType.equals(CommonConstant.MethodType.POST)) {
96 return getResponse("createNsRsp.json");
97 } else if (url.contains("instantiate")
98 && methodType.equals(CommonConstant.MethodType.POST)) {
99 return getResponse("instantiateNsRsp.json");
100 } else if (methodType.equals(CommonConstant.MethodType.DELETE)) {
101 return getResponse(null);
102 } else if (url.contains("terminate") && methodType.equals(CommonConstant.MethodType.POST)) {
103 return getResponse("terminateNsRsp.json");
104 } else if (url.contains("/api/nslcm/v1/jobs")
105 && methodType.equals(CommonConstant.MethodType.GET)) {
106 return getResponse("queryJobRsp.json");
115 * Mock the request body form a file <br>
118 * @since ONAP Amsterdam Release
120 private void mockRequestDatabase() {
121 new MockUp<RequestsDatabase>() {
124 * mock get resource operation status <br>
128 * @since ONAP Amsterdam Release
131 public ResourceOperationStatus getResourceOperationStatus(String serviceId,
132 String operationId, String resourceTemplateUUID) {
133 ResourceOperationStatus resStatus = new ResourceOperationStatus();
134 resStatus.setServiceId("111");
135 resStatus.setOperationId("111");
140 * Mock update Res Oper Status <br>
143 * @since ONAP Amsterdam Release
146 public void updateResOperStatus(ResourceOperationStatus operStatus) {
153 * Before executing UT, start mock requst database <br>
155 * @since ONAP Amsterdam Release
158 public void start() {
159 mockRequestDatabase();
163 * After executing UT, close session<br/>
165 * @since ONAP Amsterdam Release
173 public void createTest() {
175 mockRestfulUtil(FILE_PATH + "createNsReq.json");
176 vfcAdapter.createNfvoNs(null);
180 public void deleteTest() {
182 mockRestfulUtil(FILE_PATH + "deleteNsReq.json");
183 vfcAdapter.deleteNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
187 public void instantiateTest() {
189 mockRestfulUtil(FILE_PATH + "instantiateNsReq.json");
190 vfcAdapter.instantiateNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
194 public void terminateTest() {
195 mockRestfulUtil(FILE_PATH + "terminateNsReq.json");
196 vfcAdapter.terminateNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
200 public void queryJobTest() {
201 mockRestfulUtil(FILE_PATH + "queryJobReq.json");
202 vfcAdapter.queryNfvoJobStatus(null, "1");
206 * Get json string from file.<br/>
208 * @param file the path of file
209 * @return json string
210 * @throws IOException when fail to read
211 * @since ONAP Amsterdam Release 2017-9-6
213 @SuppressWarnings("deprecation")
214 private String getJsonString(final String file) {
215 if (ValidateUtil.isStrEmpty(file)) {
221 FileInputStream fileStream = new FileInputStream(new File(file));
222 json = IOUtils.toString(fileStream);
223 } catch (Exception e) {
224 Assert.fail(e.getMessage());
230 * get the response from file <br>
234 * @since ONAP Amsterdam Release
236 private RestfulResponse getResponse(String fileName) {
237 RestfulResponse responseSuccess = new RestfulResponse();
238 responseSuccess.setStatus(HttpCode.RESPOND_OK);
239 if (null != fileName) {
240 String jsonStr = getJsonString(FILE_PATH + fileName);
241 responseSuccess.setResponseContent(jsonStr);
243 return responseSuccess;