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 / VnfmRestfulUtilTest.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 package org.openo.nfvo.jujuvnfmadapter.common.servicetoken;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Modifier;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.junit.Test;
29 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
30 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
31
32 import mockit.Mock;
33 import mockit.MockUp;
34 import net.sf.json.JSONObject;
35
36 public class VnfmRestfulUtilTest {
37
38     @Test
39     public void testGetRestResByDefaultByNull() {
40         RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "methodNames", new JSONObject());
41         assertNull(result);
42     }
43     @Test
44     public void testGetRestResByDefaultByGet() {
45         RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "get", new JSONObject());
46         assertNotNull(result);
47     }
48
49     @Test
50     public void testGetRestResByDefaultByPut() {
51         RestfulResponse result = VnfmRestfulUtil.getRestResByDefault("path", "put", new JSONObject());
52         assertNotNull(result);
53     }
54
55     ////
56     @Test
57     public void testSendReqToAppByFail() {
58         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", new JSONObject());
59         assertEquals(Constant.REST_FAIL, result.get("retCode"));
60     }
61
62     @Test
63     public void testSendReqToAppByVnfmInfoPut() {
64         JSONObject paraJson = new JSONObject();
65         JSONObject vnfmObj = new JSONObject();
66         vnfmObj.put("id", "id");
67         paraJson.put("vnfmInfo", vnfmObj);
68         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
69         assertEquals(Constant.REST_FAIL, result.get("retCode"));
70     }
71
72     @Test
73     public void testSendReqToAppByVnfmInfoPutNormal() {
74         new MockUp<VnfmRestfulUtil>(){
75             @Mock
76             public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
77                 RestfulResponse resp = new RestfulResponse();
78                 resp.setStatus(200);
79                 JSONObject obj = new JSONObject();
80                 obj.put("retCode", 1);
81                 obj.put("data", new JSONObject());
82                 resp.setResponseJson(obj.toString());
83                 return resp;
84             }
85         };
86         JSONObject paraJson = new JSONObject();
87         JSONObject vnfmObj = new JSONObject();
88         vnfmObj.put("id", "id");
89         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
90         assertEquals(Constant.REST_SUCCESS, result.get("retCode"));
91     }
92     @Test
93     public void testSendReqToAppByVnfmInfoPutNormal2() {
94         new MockUp<VnfmRestfulUtil>(){
95             @Mock
96             public RestfulResponse getRestResByDefault(String path, String methodNames, JSONObject bodyParam) {
97                 RestfulResponse resp = new RestfulResponse();
98                 resp.setStatus(200);
99                 JSONObject obj = new JSONObject();
100                 obj.put("retCode", -1);
101                 obj.put("data", new JSONObject());
102                 resp.setResponseJson(obj.toString());
103                 return resp;
104             }
105         };
106         JSONObject paraJson = new JSONObject();
107         JSONObject vnfmObj = new JSONObject();
108         vnfmObj.put("id", "id");
109         JSONObject result = VnfmRestfulUtil.sendReqToApp("path", "put", paraJson);
110         assertEquals(Constant.REST_FAIL, result.get("retCode"));
111     }
112
113     @Test
114     public void testGenerateParamsMap() {
115         Map<String, String> result = VnfmRestfulUtil.generateParamsMap("url", "methodType", "path","auth");
116         Map<String, String> paramsMap = new HashMap<String, String>(6);
117         paramsMap.put("url", "url");
118         paramsMap.put("methodType", "methodType");
119         paramsMap.put("path", "path");
120         paramsMap.put("authMode", "auth");
121         assertEquals(paramsMap, result);
122     }
123     @Test
124     public void getRemoteResponseTestInvalidGet(){
125         Map<String,String> paramsMap = new HashMap<>();
126         paramsMap.put("path", "/test/abc");
127         paramsMap.put("url", "http://localhost:8080");
128         paramsMap.put("methodType","get");
129         paramsMap.put("authMode","test");
130         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
131         assertTrue(resp == null);
132     }
133
134     @Test
135     public void getRemoteResponseTestInvalidPut(){
136         Map<String,String> paramsMap = new HashMap<>();
137         paramsMap.put("path", "/test/abc");
138         paramsMap.put("url", "http://localhost:8080");
139         paramsMap.put("methodType","put");
140         paramsMap.put("authMode","test");
141         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
142         assertTrue(resp == null);
143     }
144
145     @Test
146     public void getRemoteResponseTestInvalidPost(){
147         Map<String,String> paramsMap = new HashMap<>();
148         paramsMap.put("path", "/openoapi/test");
149         paramsMap.put("url", "http://localhost:8080");
150         paramsMap.put("methodType","post");
151         paramsMap.put("authMode","test");
152         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
153         assertTrue(resp == null);
154     }
155     @Test
156     public void getRemoteResponseTestInvalidDelete(){
157         Map<String,String> paramsMap = new HashMap<>();
158         paramsMap.put("path", "/openoapi/test");
159         paramsMap.put("url", "http://localhost:8080");
160         paramsMap.put("methodType","delete");
161         paramsMap.put("authMode","test");
162         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
163         assertTrue(resp == null);
164     }
165
166     @Test
167     public void getRemoteResponse2TestInvalidGet(){
168         String url = "http://localhost:8080";
169         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "get", "test123");
170         assertTrue(resp == null);
171     }
172
173     @Test
174     public void getRemoteResponse2TestInvalidPut(){
175         String url = "http://localhost:8080";
176         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "put", "test123");
177         assertTrue(resp == null);
178     }
179
180     @Test
181     public void getRemoteResponse2TestInvalidPost(){
182         String url = "http://localhost:8080";
183         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "post", "test123");
184         assertTrue(resp == null);
185     }
186     @Test
187     public void getRemoteResponse2TestInvalidDelete(){
188
189         String url = "http://localhost:8080";
190
191         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(url, "delete", "test123");
192         assertTrue(resp == null);
193     }
194
195     @Test
196     public void getRemoteResponseTestFalseNfvoApp(){
197         Map<String,String> paramsMap = new HashMap<>();
198         paramsMap.put("path", "/openoapi/test");
199         paramsMap.put("url", "http://localhost:8080");
200         paramsMap.put("methodType","delete");
201         paramsMap.put("authMode","test");
202         RestfulResponse resp = VnfmRestfulUtil.getRemoteResponse(paramsMap, null, "test123");
203         assertTrue(resp == null);
204     }
205
206    /* @Test
207     public void sentEvtByRestTest(){
208         VnfmRestfulUtil.sentEvtByRest("http://localhost:8080", "get", null);
209         assertTrue(true);
210     }
211     @Test
212     public void sentEvtByRestTest2(){
213         JSONObject bodyParam = new JSONObject();
214         VnfmRestfulUtil.sentEvtByRest("http://localhost:8080", "get",bodyParam);
215         assertTrue(true);
216     }*/
217     @Test
218     public void testPrivateConstructor() throws Exception {
219         Constructor constructor = VnfmRestfulUtil.class.getDeclaredConstructor();
220         assertTrue("Constructor  private", Modifier.isPrivate(constructor.getModifiers()));
221         constructor.setAccessible(true);
222         constructor.newInstance();
223     }
224 }