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