0c3464343409416eb135d5cef9b8fc2acfc1ae46
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / test / java / org / openo / nfvo / jujuvnfmadapter / common / servicetoken / JujuVnfmRestfulUtilTest.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.*;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Modifier;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.junit.Test;
27 import org.openo.baseservice.remoteservice.exception.ServiceException;
28 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
29 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
30 import org.openo.nfvo.jujuvnfmadapter.common.servicetoken.VNFRestfulUtil;
31 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
32
33 import mockit.Mock;
34 import mockit.MockUp;
35 import net.sf.json.JSONObject;
36
37 /**
38  * <br/>
39  * <p>
40  * </p>
41  *
42  * @author
43  * @version NFVO 0.5 Aug 10, 2016
44  */
45 public class JujuVnfmRestfulUtilTest {
46
47
48     @Test
49     public void testGetRemoteResponseByVnfmInfoNull() {
50         RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(new HashMap(), null);
51         assertEquals(null, result);
52     }
53
54     @Test
55     public void testGetRemoteResponseByPost() {
56         Map testMap = new HashMap();
57         testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
58         testMap.put("methodType","post");
59         RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
60
61         assertEquals(null, result);
62     }
63
64     @Test
65     public void testGetRemoteResponseByVnfmInfo() {
66         Map testMap = new HashMap();
67         testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
68         testMap.put("methodType","get");
69         RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
70
71         assertEquals(null, result);
72     }
73
74     @Test
75     public void testGetRemoteResponseByVnfmInfoPut() {
76         Map testMap = new HashMap();
77         testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
78         testMap.put("methodType","put");
79         RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
80
81         assertEquals(null, result);
82     }
83
84     @Test
85     public void testGetRemoteResponseByVnfmInfoDelete() {
86         Map testMap = new HashMap();
87         testMap.put("url", "/openoapi/extsys/v1/vnfms/11111");
88         testMap.put("methodType","delete");
89         RestfulResponse result = JujuVnfmRestfulUtil.getRemoteResponse(testMap, "");
90
91         assertEquals(null, result);
92     }
93
94     @Test
95     public void getVimResponseContentGetInvalid(){
96         RestfulParametes restParametes = new RestfulParametes();
97         Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "put");
98         assertTrue(result.isEmpty());
99     }
100     @Test
101     public void getVimResponseContentAddInvalid(){
102         RestfulParametes restParametes = new RestfulParametes();
103         Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "add");
104         assertTrue(result.isEmpty());
105     }
106
107     @Test
108     public void getVimResponseContentPutInvalid(){
109         RestfulParametes restParametes = new RestfulParametes();
110         Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "put");
111         assertTrue(result.isEmpty());
112     }
113     @Test
114     public void getVimResponseContentDeleteInvalid(){
115         RestfulParametes restParametes = new RestfulParametes();
116         Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "delete");
117         assertTrue(result.isEmpty());
118     }
119
120     @Test
121     public void getVimResponseContentPatchInvalid(){
122         RestfulParametes restParametes = new RestfulParametes();
123         Map<String, Object> result = JujuVnfmRestfulUtil.getVimResponseContent("http://127.0.0.1:8080", restParametes, null, "patch");
124         assertTrue(result.isEmpty());
125     }
126
127     @Test
128     public void testGetResultToVnfmByVnfmInfoErrorMsg() {
129         JSONObject vnfmInfo = new JSONObject();
130         vnfmInfo.put("retCode", Constant.REST_FAIL);
131         vnfmInfo.put("msg", "ErrorMsg");
132         JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
133
134         JSONObject retJson = new JSONObject();
135         retJson.put("retCode", Constant.REST_FAIL);
136         retJson.put("data", "ErrorMsg");
137         assertEquals(retJson, result);
138     }
139
140     @Test
141     public void testGetResultToVnfmByVnfmInfoError() {
142         JSONObject vnfmInfo = new JSONObject();
143         vnfmInfo.put("retCode", Constant.REST_FAIL);
144         JSONObject result = VNFRestfulUtil.getResultToVnfm(vnfmInfo, "vnfmId");
145
146         JSONObject retJson = new JSONObject();
147         retJson.put("retCode", Constant.REST_FAIL);
148         assertEquals(retJson, result);
149     }
150
151     @Test
152     public void vimRestfulResponseTestGetInvalid(){
153         RestfulParametes restParametes = new RestfulParametes();
154         RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "get");
155         assertTrue(resp == null);
156     }
157     @Test
158     public void vimRestfulResponseTestAddInvalid(){
159         RestfulParametes restParametes = new RestfulParametes();
160         RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "add");
161         assertTrue(resp == null);
162     }
163
164     @Test
165     public void vimRestfulResponseTestPutInvalid(){
166         RestfulParametes restParametes = new RestfulParametes();
167         RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "put");
168         assertTrue(resp == null);
169     }
170
171     @Test
172     public void vimRestfulResponseTestDeleteInvalid(){
173         RestfulParametes restParametes = new RestfulParametes();
174         RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "delete");
175         assertTrue(resp == null);
176     }
177
178     @Test
179     public void vimRestfulResponseTestPatchInvalid(){
180         RestfulParametes restParametes = new RestfulParametes();
181         RestfulResponse resp = JujuVnfmRestfulUtil.vimRestfulResponse("http://127.0.0.1:8080", restParametes, null, "patch");
182         assertTrue(resp == null);
183     }
184
185     @Test
186     public void getRemoteResponseTestGetInvalid(){
187         Map<String,String> paramsMap = new HashMap<>();
188         paramsMap.put("path", "http://localhost:8080");
189         paramsMap.put("methodType","get");
190         RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
191         assertNull(resp);
192
193     }
194
195     @Test
196     public void getRemoteResponseTestGetHttpsInvalid(){
197         Map<String,String> paramsMap = new HashMap<>();
198         paramsMap.put("path", "http://localhost:8080");
199         paramsMap.put("methodType","get");
200         RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", true);
201         assertNull(resp);
202
203     }
204
205     @Test
206     public void getRemoteResponseTestPostInvalid(){
207         Map<String,String> paramsMap = new HashMap<>();
208         paramsMap.put("path", "http://localhost:8080");
209         paramsMap.put("methodType","post");
210         RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
211         assertNull(resp);
212
213     }
214
215     @Test
216     public void getRemoteResponseTestPutInvalid(){
217         Map<String,String> paramsMap = new HashMap<>();
218         paramsMap.put("path", "http://localhost:8080");
219         paramsMap.put("methodType","put");
220         RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
221         assertNull(resp);
222
223     }
224
225     @Test
226     public void getRemoteResponseTestDeleteInvalid(){
227         Map<String,String> paramsMap = new HashMap<>();
228         paramsMap.put("path", "http://localhost:8080");
229         paramsMap.put("methodType","delete");
230         RestfulResponse resp = JujuVnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test", false);
231         assertNull(resp);
232
233     }
234     @Test
235     public void generateParametesMapTest(){
236         Map<String, String> paramsMap = JujuVnfmRestfulUtil.generateParametesMap("http://localhost:8080", "get", "openoapi/test", "test");
237         assertTrue("http://localhost:8080".equals(paramsMap.get("url")) && "get".equals(paramsMap.get("methodType"))
238                 && "openoapi/test".equals(paramsMap.get("path")) && "test".equals(paramsMap.get("authMode")));
239     }
240     @Test
241     public void testPrivateConstructor() throws Exception {
242         Constructor constructor = JujuVnfmRestfulUtil.class.getDeclaredConstructor();
243         assertTrue("Constructor  private", Modifier.isPrivate(constructor.getModifiers()));
244         constructor.setAccessible(true);
245         constructor.newInstance();
246     }
247 }