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