Update gvnfm-driver .gitreview file
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / test / java / org / openo / nfvo / 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.openo.nfvo.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.openo.baseservice.roa.util.restclient.RestfulResponse;
31 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
32
33 import net.sf.json.JSONObject;
34
35 /**
36  * <br/>
37  * <p>
38  * </p>
39  *
40  * @author
41  * @version NFVO 0.5 Aug 10, 2016
42  */
43 public class VNFRestfulUtilTest {
44
45     @Test
46     public void testGetRestResByDefaultByNull() {
47         RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
48         assertNull(result);
49     }
50
51     @Test
52     public void testGetRestResByDefaultByGet() {
53         RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
54         assertNotNull(result);
55     }
56
57     @Test
58     public void testGetRestResByDefaultByPut() {
59         RestfulResponse result = VNFRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
60         assertNotNull(result);
61     }
62
63     /*@Test
64     public void testSendReqToApp() {
65         new MockUp<VNFRestfulUtil>() {
66
67             @Mock
68             public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
69                 RestfulResponse restfulResponse = new RestfulResponse();
70                 restfulResponse.setStatus(Constant.HTTP_OK);
71                 String responseString = "{\"retCode\":1,\"data\":\"success\"}";
72                 restfulResponse.setResponseJson(responseString);
73                 return restfulResponse;
74             }
75         };
76         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
77         assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
78     }*/
79
80     /*@Test(expected = ExceptionInInitializerError.class)
81     public void testSendReqToAppByErrorMsg() {
82         new MockUp<VNFRestfulUtil>() {
83
84             @Mock
85             public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
86                 RestfulResponse restfulResponse = new RestfulResponse();
87                 restfulResponse.setStatus(Constant.HTTP_OK);
88                 String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}";
89                 restfulResponse.setResponseJson(responseString);
90                 return restfulResponse;
91             }
92         };
93         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
94         assertEquals(Constant.REST_FAIL, result.get("retCode"));
95     }*/
96
97     /*@Test
98     public void testSendReqToAppByError() {
99         new MockUp<VNFRestfulUtil>() {
100
101             @Mock
102             public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
103                 RestfulResponse restfulResponse = new RestfulResponse();
104                 restfulResponse.setStatus(Constant.HTTP_OK);
105                 String responseString = "{\"retCode\":-1,\"data\":\"fail\"}";
106                 restfulResponse.setResponseJson(responseString);
107                 return restfulResponse;
108             }
109         };
110         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
111         assertEquals(Constant.REST_FAIL, result.get("retCode"));
112     }*/
113
114     @Test
115     public void testSendReqToAppByFail() {
116         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", new JSONObject());
117         assertEquals(Constant.REST_FAIL, result.get("retCode"));
118     }
119
120     @Test
121     public void testSendReqToAppByVnfmInfoPut() {
122         JSONObject paraJson = new JSONObject();
123         JSONObject vnfmObj = new JSONObject();
124         vnfmObj.put("id", "id");
125         paraJson.put("vnfmInfo", vnfmObj);
126         JSONObject result = VNFRestfulUtil.sendReqToApp("path", "put", paraJson);
127         assertEquals(Constant.REST_FAIL, result.get("retCode"));
128     }
129
130     @Test
131     public void testGenerateParamsMap() {
132         Map<String, String> result = VNFRestfulUtil.generateParamsMap("url", "methodType", "path");
133         Map<String, String> paramsMap = new HashMap<String, String>(6);
134         paramsMap.put("url", "url");
135         paramsMap.put("methodType", "methodType");
136         paramsMap.put("path", "path");
137         paramsMap.put("authMode", "Certificate");
138         assertEquals(paramsMap, result);
139     }
140
141     @Test
142     public void testGenerateParamsMap2() {
143         Map<String, String> result = VNFRestfulUtil.generateParamsMap("url", "methodType", "path", "authMode");
144         Map<String, String> paramsMap = new HashMap<String, String>(6);
145         paramsMap.put("url", "url");
146         paramsMap.put("methodType", "methodType");
147         paramsMap.put("path", "path");
148         paramsMap.put("authMode", "authMode");
149         assertEquals(paramsMap, result);
150     }
151
152     @Test
153     public void testGetResultToVnfmByVnfmInfoNull() {
154         JSONObject result = VNFRestfulUtil.getResultToVnfm(null, null);
155
156         JSONObject retJson = new JSONObject();
157         retJson.put("retCode", Constant.REST_FAIL);
158         retJson.put("data", "get null result");
159         assertEquals(retJson, result);
160     }
161
162     @Test
163     public void testGetResultToVnfmByVnfmInfoErrorMsg() {
164         JSONObject vnfmInfo = new JSONObject();
165         vnfmInfo.put("retCode", Constant.REST_FAIL);
166         vnfmInfo.put("msg", "ErrorMsg");
167         JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
168
169         JSONObject retJson = new JSONObject();
170         retJson.put("retCode", Constant.REST_FAIL);
171         retJson.put("data", "ErrorMsg");
172         assertEquals(retJson, result);
173     }
174
175     @Test
176     public void testGetResultToVnfmByVnfmInfoError() {
177         JSONObject vnfmInfo = new JSONObject();
178         vnfmInfo.put("retCode", Constant.REST_FAIL);
179         JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
180
181         JSONObject retJson = new JSONObject();
182         retJson.put("retCode", Constant.REST_FAIL);
183         assertEquals(retJson, result);
184     }
185     @Test
186     public void getRemoteResponseTestInvalidGet(){
187         Map<String,String> paramsMap = new HashMap<>();
188         paramsMap.put("path", "http://localhost:8080");
189         paramsMap.put("url", "/openoapi/test");
190         paramsMap.put("methodType","get");
191         paramsMap.put("authMode","test");
192         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
193         assertTrue(resp == null);
194     }
195
196     @Test
197     public void getRemoteResponseTestInvalidPut(){
198         Map<String,String> paramsMap = new HashMap<>();
199         paramsMap.put("path", "http://localhost:8080");
200         paramsMap.put("url", "/openoapi/test");
201         paramsMap.put("methodType","put");
202         paramsMap.put("authMode","test");
203         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
204         assertTrue(resp == null);
205     }
206
207     @Test
208     public void getRemoteResponseTestInvalidPost(){
209         Map<String,String> paramsMap = new HashMap<>();
210         paramsMap.put("path", "http://localhost:8080");
211         paramsMap.put("url", "/openoapi/test");
212         paramsMap.put("methodType","post");
213         paramsMap.put("authMode","test");
214         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
215         assertTrue(resp == null);
216     }
217     @Test
218     public void getRemoteResponseTestInvalidDelete(){
219         Map<String,String> paramsMap = new HashMap<>();
220         paramsMap.put("path", "http://localhost:8080");
221         paramsMap.put("url", "/openoapi/test");
222         paramsMap.put("methodType","delete");
223         paramsMap.put("authMode","test");
224         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", true);
225         assertTrue(resp == null);
226     }
227
228     @Test
229     public void getRemoteResponseTestFalseNfvoApp(){
230         Map<String,String> paramsMap = new HashMap<>();
231         paramsMap.put("path", "http://localhost:8080");
232         paramsMap.put("url", "/openoapi/test");
233         paramsMap.put("methodType","delete");
234         paramsMap.put("authMode","test");
235         RestfulResponse resp = VNFRestfulUtil.getRemoteResponse(paramsMap, null, "test123", false);
236         assertTrue(resp == null);
237     }
238
239     @Test
240     public void sentEvtByRestTest(){
241         VNFRestfulUtil.sentEvtByRest("http://localhost:8080", "get", null);
242         assertTrue(true);
243     }
244     @Test
245     public void sentEvtByRestTest2(){
246         JSONObject bodyParam = new JSONObject();
247         VNFRestfulUtil.sentEvtByRest("http://localhost:8080", "get",bodyParam);
248         assertTrue(true);
249     }
250     @Test
251     public void testPrivateConstructor() throws Exception {
252         Constructor constructor = VNFRestfulUtil.class.getDeclaredConstructor();
253         assertTrue("Constructor  private", Modifier.isPrivate(constructor.getModifiers()));
254         constructor.setAccessible(true);
255         constructor.newInstance();
256     }
257 }