Junit coverage increased
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / test / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / servicetoken / VnfmRestfulUtilTest.java
1 /*
2  * Copyright 2016 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.svnfm.vnfmadapter.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
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.junit.Test;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
29 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
30
31 import mockit.Mock;
32 import mockit.MockUp;
33 import net.sf.json.JSONObject;
34
35 public class VnfmRestfulUtilTest {
36     @Test
37     public void testGetRestResByDefaultByNull() {
38         RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
39         assertNull(result);
40     }
41
42     @Test
43     public void testGetRestResByDefaultByGet() {
44         RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
45         assertNotNull(result);
46     }
47
48     @Test
49     public void testGetRestResByDefaultByPut() {
50         RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
51         assertNotNull(result);
52     }
53
54     @Test
55     public void testSendReqToApp() {
56 //        new MockUp<VnfmRestfulUtil>() {
57 //
58 //            @Mock
59 //            public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
60 //                RestfulResponse restfulResponse = new RestfulResponse();
61 //                restfulResponse.setStatus(Constant.HTTP_OK);
62 //                String responseString = "{\"retCode\":1,\"data\":\"success\"}";
63 //                restfulResponse.setResponseJson(responseString);
64 //                return restfulResponse;
65 //            }
66 //        };
67         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
68         assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
69     }
70
71     @Test
72     public void testSendReqToAppByErrorMsg() {
73 //        new MockUp<VnfmRestfulUtil>() {
74 //
75 //            @Mock
76 //            public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
77 //                RestfulResponse restfulResponse = new RestfulResponse();
78 //                restfulResponse.setStatus(Constant.HTTP_OK);
79 //                String responseString = "{\"retCode\":-1,\"data\":\"fail\",\"msg\":\"fail\"}";
80 //                restfulResponse.setResponseJson(responseString);
81 //                return restfulResponse;
82 //            }
83 //        };
84         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
85         assertEquals(Constant.REST_FAIL, result.get("retCode"));
86     }
87
88     @Test
89     public void testSendReqToAppByError() {
90 //        new MockUp<VnfmRestfulUtil>() {
91 //
92 //            @Mock
93 //            public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
94 //                RestfulResponse restfulResponse = new RestfulResponse();
95 //                restfulResponse.setStatus(Constant.HTTP_OK);
96 //                String responseString = "{\"retCode\":-1,\"data\":\"fail\"}";
97 //                restfulResponse.setResponseJson(responseString);
98 //                return restfulResponse;
99 //            }
100 //        };
101         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
102         assertEquals(Constant.REST_FAIL, result.get("retCode"));
103     }
104
105     @Test
106     public void testSendReqToAppByFail() {
107         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
108         assertEquals(Constant.REST_FAIL, result.get("retCode"));
109     }
110
111     @Test
112     public void testSendReqToAppByVnfmInfo() {
113         JSONObject paraJson = new JSONObject();
114         JSONObject vnfmObj = new JSONObject();
115         vnfmObj.put("id", "id");
116         paraJson.put("vnfmInfo", vnfmObj);
117         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
118         assertEquals(Constant.REST_FAIL, result.get("retCode"));
119     }
120
121     @Test
122     public void testGenerateParamsMap2() {
123         Map<String, String> result = VnfmRestfulUtil.generateParamsMap("url", "methodType", "path", "authMode");
124         Map<String, String> paramsMap = new HashMap<String, String>(6);
125         paramsMap.put("url", "url");
126         paramsMap.put("methodType", "methodType");
127         paramsMap.put("path", "path");
128         paramsMap.put("authMode", "authMode");
129         assertEquals(paramsMap, result);
130     }
131     @Test
132     public void getRemoteResponseTestGet(){
133         Map<String, String> paramsMap = new HashMap<>();
134         paramsMap.put("url", "/test/123");
135         paramsMap.put("methodType", "get");
136         paramsMap.put("path", "http://localhost:8080");
137         paramsMap.put("authMode", "test");
138         boolean isNfvoApp = false;
139         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
140         assertNull(resp);
141     }
142     @Test
143     public void getRemoteResponseTestGetTrueNfvo(){
144         Map<String, String> paramsMap = new HashMap<>();
145         paramsMap.put("url", "/test/123");
146         paramsMap.put("methodType", "get");
147         paramsMap.put("path", "http://localhost:8080");
148         paramsMap.put("authMode", "test");
149         boolean isNfvoApp = true;
150         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
151         assertNull(resp);
152     }
153     @Test
154     public void getRemoteResponseTestPost(){
155         Map<String, String> paramsMap = new HashMap<>();
156         paramsMap.put("url", "/test/123");
157         paramsMap.put("methodType", "post");
158         paramsMap.put("path", "http://localhost:8080");
159         paramsMap.put("authMode", "test");
160
161         boolean isNfvoApp = false;
162         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
163         assertNull(resp);
164     }
165     @Test
166     public void getRemoteResponseTestPut(){
167         Map<String, String> paramsMap = new HashMap<>();
168         paramsMap.put("url", "/test/123");
169         paramsMap.put("methodType", "put");
170         paramsMap.put("path", "http://localhost:8080");
171         paramsMap.put("authMode", "test");
172
173         boolean isNfvoApp = false;
174         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
175         assertNull(resp);
176     }
177
178     @Test
179     public void getRemoteResponseTestDelete(){
180         Map<String, String> paramsMap = new HashMap<>();
181         paramsMap.put("url", "/test/123");
182         paramsMap.put("methodType", "delete");
183         paramsMap.put("path", "http://localhost:8080");
184         paramsMap.put("authMode", "test");
185
186         boolean isNfvoApp = false;
187         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, "", "test123", isNfvoApp);
188         assertNull(resp);
189     }
190
191     @Test
192     public void getRemoteResponse2TestDelete(){
193         Map<String, String> paramsMap = new HashMap<>();
194         paramsMap.put("url", "/test/123");
195         paramsMap.put("methodType", "delete");
196         paramsMap.put("path", "http://localhost:8080");
197         paramsMap.put("authMode", "test");
198         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
199         assertNull(resp);
200     }
201     @Test
202     public void getRemoteResponse2TestGet(){
203         Map<String, String> paramsMap = new HashMap<>();
204         paramsMap.put("url", "/test/123");
205         paramsMap.put("methodType", "get");
206         paramsMap.put("path", "http://localhost:8080");
207         paramsMap.put("authMode", "test");
208         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
209         assertNull(resp);
210     }
211     @Test
212     public void getRemoteResponse2Testput(){
213         Map<String, String> paramsMap = new HashMap<>();
214         paramsMap.put("url", "/test/123");
215         paramsMap.put("methodType", "put");
216         paramsMap.put("path", "http://localhost:8080");
217         paramsMap.put("authMode", "test");
218         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
219         assertNull(resp);
220     }
221     @Test
222     public void getRemoteResponse2TestPost(){
223         Map<String, String> paramsMap = new HashMap<>();
224         paramsMap.put("url", "/test/123");
225         paramsMap.put("methodType", "put");
226         paramsMap.put("path", "http://localhost:8080");
227         paramsMap.put("authMode", "post");
228         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
229         assertNull(resp);
230     }
231     @Test
232     public void getRemoteResponse2TestPatch(){
233         Map<String, String> paramsMap = new HashMap<>();
234         paramsMap.put("url", "/test/123");
235         paramsMap.put("methodType", "patch");
236         paramsMap.put("path", "http://localhost:8080");
237         paramsMap.put("authMode", "post");
238         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse("/test/123", "", "test123");
239         assertNull(resp);
240     }
241     
242     @Test
243     public void getRemoteResponse() {
244         Map <String, String > headerMap = new HashMap<String, String>();
245         
246         VnfmRestfulUtil.getRemoteResponse( "url",  "get",  headerMap,"params");
247         VnfmRestfulUtil.getRemoteResponse( "url",  "post",  headerMap,"params");
248         VnfmRestfulUtil.getRemoteResponse( "url",  "put",  headerMap,"params");
249         assertNull(VnfmRestfulUtil.getRemoteResponse( "url",  "delete",  headerMap,"params"));
250
251
252
253     }
254 }