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