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 org.apache.commons.io.IOUtils;
28 import org.junit.After;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.openecomp.mso.adapters.vfc.constant.CommonConstant;
33 import org.openecomp.mso.adapters.vfc.constant.HttpCode;
34 import org.openecomp.mso.adapters.vfc.model.RestfulResponse;
35 import org.openecomp.mso.adapters.vfc.util.RestfulUtil;
36 import org.openecomp.mso.adapters.vfc.util.ValidateUtil;
37 import org.openecomp.mso.requestsdb.RequestsDatabase;
38 import org.openecomp.mso.requestsdb.ResourceOperationStatus;
44 * VF-C adapter UT <br>
49 * @version ONAP Amsterdam Release 2017-08-31
51 public class VfcAdapterTest {
53 private VfcAdapterRest vfcAdapter = new VfcAdapterRest();
58 private static final String FILE_PATH = "src/test/resources/json/";
61 * mock get request body <br>
65 * @since ONAP Amsterdam Release
67 public String getRequestBody(String fileName) {
68 return getJsonString(fileName);
72 * Mock the request body form a file <br>
74 * @since ONAP Amsterdam Release
76 private void mockRestfulUtil() {
77 new MockUp<RestfulUtil>() {
80 * mock get send method <br>
86 * @since ONAP Amsterdam Release
89 public RestfulResponse send(String url, String methodType, String content) {
90 if(url.equals(CommonConstant.NFVO_CREATE_URL) && methodType.equals(CommonConstant.MethodType.POST)) {
91 return getResponse("createNsRsp.json");
92 } else if(url.contains("instantiate") && methodType.equals(CommonConstant.MethodType.POST)) {
93 return getResponse("instantiateNsRsp.json");
94 } else if(methodType.equals(CommonConstant.MethodType.DELETE)) {
95 return getResponse(null);
96 } else if(url.contains("terminate") && methodType.equals(CommonConstant.MethodType.POST)) {
97 return getResponse("terminateNsRsp.json");
98 } else if(url.contains("/api/nslcm/v1/jobs") && methodType.equals(CommonConstant.MethodType.GET)) {
99 return getResponse("queryJobRsp.json");
108 * Mock the request body form a file <br>
111 * @since ONAP Amsterdam Release
113 private void mockRequestDatabase() {
114 new MockUp<RequestsDatabase>() {
117 * mock get resource operation status <br>
121 * @since ONAP Amsterdam Release
124 public ResourceOperationStatus getResourceOperationStatus(String serviceId, String operationId,
125 String resourceTemplateUUID) {
126 ResourceOperationStatus resStatus = new ResourceOperationStatus();
127 resStatus.setServiceId("111");
128 resStatus.setOperationId("111");
133 * Mock update Res Oper Status <br>
136 * @since ONAP Amsterdam Release
139 public void updateResOperStatus(ResourceOperationStatus operStatus) {
146 * Before executing UT, start mock requst database <br>
148 * @since ONAP Amsterdam Release
151 public void start() {
152 mockRequestDatabase();
157 * After executing UT, close session<br/>
159 * @since ONAP Amsterdam Release
167 public void createTest() {
169 String createReq = getRequestBody(FILE_PATH + "createNsReq.json");
170 vfcAdapter.createNfvoNs(createReq);
174 public void deleteTest() {
176 String req = getRequestBody(FILE_PATH + "deleteNsReq.json");
177 vfcAdapter.deleteNfvoNs(req, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
181 public void instantiateTest() {
183 String req = getRequestBody(FILE_PATH + "instantiateNsReq.json");
184 vfcAdapter.instantiateNfvoNs(req, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
188 public void terminateTest() {
189 String req = getRequestBody(FILE_PATH + "terminateNsReq.json");
190 vfcAdapter.deleteNfvoNs(req, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
194 public void queryJobTest() {
195 String req = getRequestBody(FILE_PATH + "queryJobReq.json");
196 vfcAdapter.queryNfvoJobStatus(req, "1");
200 * Get json string from file.<br/>
202 * @param file the path of file
203 * @return json string
204 * @throws IOException when fail to read
205 * @since ONAP Amsterdam Release 2017-9-6
207 @SuppressWarnings("deprecation")
208 private String getJsonString(final String file) {
209 if(ValidateUtil.isStrEmpty(file)) {
215 FileInputStream fileStream = new FileInputStream(new File(file));
216 json = IOUtils.toString(fileStream);
217 } catch(Exception e) {
218 Assert.fail(e.getMessage());
224 * get the response from file <br>
228 * @since ONAP Amsterdam Release
230 private RestfulResponse getResponse(String fileName) {
231 RestfulResponse responseSuccess = new RestfulResponse();
232 responseSuccess.setStatus(HttpCode.RESPOND_OK);
233 if(null != fileName) {
234 String jsonStr = getJsonString(FILE_PATH + fileName);
235 responseSuccess.setResponseContent(jsonStr);
237 return responseSuccess;