Update gvnfm-driver .gitreview file
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / common / servicetoken / VNFRestfulUtilTest.java
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.Modifier;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.junit.Test;
30 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.VNFRestfulUtil;
31 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
32 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
33
34 import net.sf.json.JSONObject;
35
36 /**
37  * <br/>
38  * <p>
39  * </p>
40  *
41  * @author
42  * @version NFVO 0.5 Aug 10, 2016
43  */
44 public class VNFRestfulUtilTest {
45
46     @Test
47     public void testGetRestResByDefaultByNull() {
48         RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
49         assertNull(result);
50     }
51
52     @Test
53     public void testGetRestResByDefaultByGet() {
54         RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
55         assertNotNull(result);
56     }
57
58     @Test
59     public void testGetRestResByDefaultByPut() {
60         RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
61         assertNotNull(result);
62     }
63
64     /*@Test
65     public void testSendReqToApp() {
66         new MockUp<VNFRestfulUtil>() {
67
68             @Mock
69             public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
70                 RestfulResponse restfulResponse = new RestfulResponse();
71                 restfulResponse.setStatus(Constant.HTTP_OK);
72                 String responseString = "{\"retCode\":1,\"data\":\"success\"}";
73                 restfulResponse.setResponseJson(responseString);
74                 return restfulResponse;
75             }
76         };
77         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
78         assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
79     }*/
80
81     /*@Test(expected = ExceptionInInitializerError.class)
82     public void testSendReqToAppByErrorMsg() {
83         new MockUp<VNFRestfulUtil>() {
84
85             @Mock
86             public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
87                 RestfulResponse restfulResponse = new RestfulResponse();
88                 restfulResponse.setStatus(Constant.HTTP_OK);
89                 String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}";
90                 restfulResponse.setResponseJson(responseString);
91                 return restfulResponse;
92             }
93         };
94         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
95         assertEquals(Constant.REST_FAIL, result.get("retCode"));
96     }*/
97
98     /*@Test
99     public void testSendReqToAppByError() {
100         new MockUp<VNFRestfulUtil>() {
101
102             @Mock
103             public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
104                 RestfulResponse restfulResponse = new RestfulResponse();
105                 restfulResponse.setStatus(Constant.HTTP_OK);
106                 String responseString = "{\"retCode\":-1,\"data\":\"fail\"}";
107                 restfulResponse.setResponseJson(responseString);
108                 return restfulResponse;
109             }
110         };
111         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
112         assertEquals(Constant.REST_FAIL, result.get("retCode"));
113     }*/
114
115     @Test
116     public void testSendReqToAppByFail() {
117         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
118         assertEquals(Constant.REST_FAIL, result.get("retCode"));
119     }
120
121     @Test
122     public void testSendReqToAppByVnfmInfoPut() {
123         JSONObject paraJson = new JSONObject();
124         JSONObject vnfmObj = new JSONObject();
125         vnfmObj.put("id", "id");
126         paraJson.put("vnfmInfo", vnfmObj);
127         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", paraJson);
128         assertEquals(Constant.REST_FAIL, result.get("retCode"));
129     }
130
131     @Test
132     public void testGenerateParamsMap() {
133         Map<String, String> result = VNFRestfulUtil.generateParamsMap("url", "methodType", "path");
134         Map<String, String> paramsMap = new HashMap<String, String>(6);
135         paramsMap.put("url", "url");
136         paramsMap.put("methodType", "methodType");
137         paramsMap.put("path", "path");
138         paramsMap.put("authMode", "Certificate");
139         assertEquals(paramsMap, result);
140     }
141
142     @Test
143     public void testGenerateParamsMap2() {
144         Map<String, String> result = VNFRestfulUtil.generateParamsMap("url", "methodType", "path", "authMode");
145         Map<String, String> paramsMap = new HashMap<String, String>(6);
146         paramsMap.put("url", "url");
147         paramsMap.put("methodType", "methodType");
148         paramsMap.put("path", "path");
149         paramsMap.put("authMode", "authMode");
150         assertEquals(paramsMap, result);
151     }
152
153     @Test
154     public void testGetResultToVnfmByVnfmInfoNull() {
155         JSONObject result = VNFRestfulUtil.getResultToVnfm(null, null);
156
157         JSONObject retJson = new JSONObject();
158         retJson.put("retCode", Constant.REST_FAIL);
159         retJson.put("data", "get null result");
160         assertEquals(retJson, result);
161     }
162
163     @Test
164     public void testGetResultToVnfmByVnfmInfoErrorMsg() {
165         JSONObject vnfmInfo = new JSONObject();
166         vnfmInfo.put("retCode", Constant.REST_FAIL);
167         vnfmInfo.put("msg", "ErrorMsg");
168         JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
169
170         JSONObject retJson = new JSONObject();
171         retJson.put("retCode", Constant.REST_FAIL);
172         retJson.put("data", "ErrorMsg");
173         assertEquals(retJson, result);
174     }
175
176     @Test
177     public void testGetResultToVnfmByVnfmInfoError() {
178         JSONObject vnfmInfo = new JSONObject();
179         vnfmInfo.put("retCode", Constant.REST_FAIL);
180         JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
181
182         JSONObject retJson = new JSONObject();
183         retJson.put("retCode", Constant.REST_FAIL);
184         assertEquals(retJson, result);
185     }
186     @Test
187     public void getRemoteResponseTestInvalidGet(){
188         Map<String,String> paramsMap = new HashMap<>();
189         paramsMap.put("path", "http://localhost:8080");
190         paramsMap.put("url", "/openoapi/test");
191         paramsMap.put("methodType","get");
192         paramsMap.put("authMode","test");
193         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
194         assertTrue(resp == null);
195     }
196
197     @Test
198     public void getRemoteResponseTestInvalidPut(){
199         Map<String,String> paramsMap = new HashMap<>();
200         paramsMap.put("path", "http://localhost:8080");
201         paramsMap.put("url", "/openoapi/test");
202         paramsMap.put("methodType","put");
203         paramsMap.put("authMode","test");
204         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
205         assertTrue(resp == null);
206     }
207
208     @Test
209     public void getRemoteResponseTestInvalidPost(){
210         Map<String,String> paramsMap = new HashMap<>();
211         paramsMap.put("path", "http://localhost:8080");
212         paramsMap.put("url", "/openoapi/test");
213         paramsMap.put("methodType","post");
214         paramsMap.put("authMode","test");
215         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
216         assertTrue(resp == null);
217     }
218     @Test
219     public void getRemoteResponseTestInvalidDelete(){
220         Map<String,String> paramsMap = new HashMap<>();
221         paramsMap.put("path", "http://localhost:8080");
222         paramsMap.put("url", "/openoapi/test");
223         paramsMap.put("methodType","delete");
224         paramsMap.put("authMode","test");
225         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
226         assertTrue(resp == null);
227     }
228
229     @Test
230     public void getRemoteResponseTestFalseNfvoApp(){
231         Map<String,String> paramsMap = new HashMap<>();
232         paramsMap.put("path", "http://localhost:8080");
233         paramsMap.put("url", "/openoapi/test");
234         paramsMap.put("methodType","delete");
235         paramsMap.put("authMode","test");
236         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", false);
237         assertTrue(resp == null);
238     }
239
240     @Test
241     public void sentEvtByRestTest(){
242         VNFRestfulUtil.sentEvtByRest("http://localhost:8080", "get", null);
243         assertTrue(true);
244     }
245     @Test
246     public void sentEvtByRestTest2(){
247         JSONObject bodyParam = new JSONObject();
248         VNFRestfulUtil.sentEvtByRest("http://localhost:8080", "get",bodyParam);
249         assertTrue(true);
250     }
251     @Test
252     public void testPrivateConstructor() throws Exception {
253         Constructor constructor = VNFRestfulUtil.class.getDeclaredConstructor();
254         assertTrue("Constructor  private", Modifier.isPrivate(constructor.getModifiers()));
255         constructor.setAccessible(true);
256         constructor.newInstance();
257     }
258 }