Merge "Reorder modifiers"
[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 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;
39
40 import mockit.Mock;
41 import mockit.MockUp;
42
43 /**
44  * VF-C adapter UT <br>
45  * <p>
46  * </p>
47  * 
48  * @author
49  * @version ONAP Amsterdam Release 2017-08-31
50  */
51 public class VfcAdapterTest {
52
53     private VfcAdapterRest vfcAdapter = new VfcAdapterRest();
54
55     /**
56      * File path
57      */
58     private static final String FILE_PATH = "src/test/resources/json/";
59
60     /**
61      * mock get request body <br>
62      * 
63      * @param fileName
64      * @return
65      * @since ONAP Amsterdam Release
66      */
67     public String getRequestBody(String fileName) {
68         return getJsonString(fileName);
69     }
70
71     /**
72      * Mock the request body form a file <br>
73      * 
74      * @since ONAP Amsterdam Release
75      */
76     private void mockRestfulUtil() {
77         new MockUp<RestfulUtil>() {
78
79             /**
80              * mock get send method <br>
81              * 
82              * @param url
83              * @param methodType
84              * @param content
85              * @return
86              * @since ONAP Amsterdam Release
87              */
88             @Mock
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");
100                 } else {
101                     return null;
102                 }
103             }
104         };
105     }
106
107     /**
108      * Mock the request body form a file <br>
109      * 
110      * @param fileName
111      * @since ONAP Amsterdam Release
112      */
113     private void mockRequestDatabase() {
114         new MockUp<RequestsDatabase>() {
115
116             /**
117              * mock get resource operation status <br>
118              * 
119              * @param request
120              * @return
121              * @since ONAP Amsterdam Release
122              */
123             @Mock
124             public ResourceOperationStatus getResourceOperationStatus(String serviceId, String operationId,
125                     String resourceTemplateUUID) {
126                 ResourceOperationStatus resStatus = new ResourceOperationStatus();
127                 resStatus.setServiceId("111");
128                 resStatus.setOperationId("111");
129                 return resStatus;
130             }
131
132             /**
133              * Mock update Res Oper Status <br>
134              * 
135              * @param operStatus
136              * @since ONAP Amsterdam Release
137              */
138             @Mock
139             public void updateResOperStatus(ResourceOperationStatus operStatus) {
140
141             }
142         };
143     }
144
145     /**
146      * Before executing UT, start mock requst database <br>
147      * 
148      * @since ONAP Amsterdam Release
149      */
150     @Before
151     public void start() {
152         mockRequestDatabase();
153         mockRestfulUtil();
154     }
155
156     /**
157      * After executing UT, close session<br/>
158      * 
159      * @since ONAP Amsterdam Release
160      */
161     @After
162     public void stop() {
163
164     }
165
166     @Test
167     public void createTest() {
168         // get request
169         String createReq = getRequestBody(FILE_PATH + "createNsReq.json");
170         vfcAdapter.createNfvoNs(createReq);
171     }
172
173     @Test
174     public void deleteTest() {
175         // get request
176         String req = getRequestBody(FILE_PATH + "deleteNsReq.json");
177         vfcAdapter.deleteNfvoNs(req, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
178     }
179
180     @Test
181     public void instantiateTest() {
182         // get request
183         String req = getRequestBody(FILE_PATH + "instantiateNsReq.json");
184         vfcAdapter.instantiateNfvoNs(req, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
185     }
186
187     @Test
188     public void terminateTest() {
189         String req = getRequestBody(FILE_PATH + "terminateNsReq.json");
190         vfcAdapter.deleteNfvoNs(req, "9b9f02c0-298b-458a-bc9c-be3692e4f354");
191     }
192
193     @Test
194     public void queryJobTest() {
195         String req = getRequestBody(FILE_PATH + "queryJobReq.json");
196         vfcAdapter.queryNfvoJobStatus(req, "1");
197     }
198
199     /**
200      * Get json string from file.<br/>
201      * 
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
206      */
207     @SuppressWarnings("deprecation")
208     private String getJsonString(final String file) {
209         if(ValidateUtil.isStrEmpty(file)) {
210             return "";
211         }
212
213         String json = null;
214         try {
215             FileInputStream fileStream = new FileInputStream(new File(file));
216             json = IOUtils.toString(fileStream);
217         } catch(Exception e) {
218             Assert.fail(e.getMessage());
219         }
220         return json;
221     }
222
223     /**
224      * get the response from file <br>
225      * 
226      * @param fileName
227      * @return
228      * @since ONAP Amsterdam Release
229      */
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);
236         }
237         return responseSuccess;
238     }
239 }