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