4dc8fffdc6956eae71bdf8132414bc7f2214f279
[so.git] / adapters / mso-vfc-adapter / src / test / java / org / openecomp / mso / adapters / vfc / VfcAdapterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.adapters.vfc;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26
27 import javax.servlet.http.HttpServletRequest;
28
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;
41
42 import mockit.Mock;
43 import mockit.MockUp;
44
45 /**
46  * VF-C adapter UT
47  * <br>
48  * <p>
49  * </p>
50  * 
51  * @author
52  * @version ONAP Amsterdam Release 2017-08-31
53  */
54 public class VfcAdapterTest {
55
56     private VfcAdapterRest vfcAdapter = new VfcAdapterRest();
57
58     /**
59      * File path
60      */
61     private static final String FILE_PATH = "src/test/resources/json/";
62
63     /**
64      * Mock the request body form a file
65      * <br>
66      * 
67      * @param fileName
68      * @since ONAP Amsterdam Release
69      */
70     private void mockRestfulUtil(String fileName) {
71         new MockUp<RestfulUtil>() {
72
73             /**
74              * mock get request body
75              * <br>
76              * 
77              * @param request
78              * @return
79              * @since ONAP Amsterdam Release
80              */
81             @Mock
82             public String getRequestBody(HttpServletRequest request) {
83                 return getJsonString(fileName);
84             }
85
86             /**
87              * mock get send method
88              * <br>
89              * 
90              * @param url
91              * @param methodType
92              * @param content
93              * @return
94              * @since ONAP Amsterdam Release
95              */
96             @Mock
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");
108                 }
109                 else {
110                     return null;
111                 }
112             }
113         };
114     }
115
116     /**
117      * Mock the request body form a file
118      * <br>
119      * 
120      * @param fileName
121      * @since ONAP Amsterdam Release
122      */
123     private void mockRequestDatabase() {
124         new MockUp<RequestsDatabase>() {
125
126             /**
127              * mock get resource operation status
128              * <br>
129              * 
130              * @param request
131              * @return
132              * @since ONAP Amsterdam Release
133              */
134             @Mock
135             public ResourceOperationStatus getResourceOperationStatus(String serviceId, String operationId,
136                     String resourceTemplateUUID) {
137                 ResourceOperationStatus resStatus = new ResourceOperationStatus();
138                 resStatus.setServiceId("111");
139                 resStatus.setOperationId("111");
140                 return resStatus;
141             }
142
143             /**
144              * Mock update Res Oper Status
145              * <br>
146              * 
147              * @param operStatus
148              * @since ONAP Amsterdam Release
149              */
150             @Mock
151             public void updateResOperStatus(ResourceOperationStatus operStatus) {
152
153             }
154         };
155     }
156
157     /**
158      * Before executing UT, start mock requst database
159      * <br>
160      * 
161      * @since ONAP Amsterdam Release
162      */
163     @Before
164     public void start() {
165         mockRequestDatabase();
166     }
167
168     /**
169      * After executing UT, close session<br/>
170      * 
171      * @since ONAP Amsterdam Release
172      */
173     @After
174     public void stop() {
175
176     }
177
178     @Test
179     public void createTest() {
180         // get request
181         mockRestfulUtil(FILE_PATH + "createNsReq.json");
182         vfcAdapter.createNfvoNs(null);
183     }
184
185     @Test
186     public void deleteTest() {
187         // get request
188         mockRestfulUtil(FILE_PATH + "deleteNsReq.json");
189         vfcAdapter.deleteNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
190     }
191
192     @Test
193     public void instantiateTest() {
194         // get request
195         mockRestfulUtil(FILE_PATH + "instantiateNsReq.json");
196         vfcAdapter.instantiateNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
197     }
198
199     @Test
200     public void terminateTest() {
201         mockRestfulUtil(FILE_PATH + "terminateNsReq.json");
202         vfcAdapter.terminateNfvoNs(null, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
203     }
204
205     @Test
206     public void queryJobTest() {
207         mockRestfulUtil(FILE_PATH + "queryJobReq.json");
208         vfcAdapter.queryNfvoJobStatus(null, "1");
209     }
210
211     /**
212      * Get json string from file.<br/>
213      * 
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
218      */
219     @SuppressWarnings("deprecation")
220     private String getJsonString(final String file) {
221         if(ValidateUtil.isStrEmpty(file)) {
222             return "";
223         }
224
225         String json = null;
226         try {
227             FileInputStream fileStream = new FileInputStream(new File(file));
228             json = IOUtils.toString(fileStream);
229         } catch(Exception e) {
230             Assert.fail(e.getMessage());
231         }
232         return json;
233     }
234
235     /**
236      * get the response from file
237      * <br>
238      * 
239      * @param fileName
240      * @return
241      * @since ONAP Amsterdam Release
242      */
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);
249         }
250         return responseSuccess;
251     }
252 }