414218b1e4e3b5c8c50a8c3409466af90c94abf1
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / test / java / org / openo / nfvo / jujuvnfmadapter / common / VnfmUtilTest.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 package org.openo.nfvo.jujuvnfmadapter.common;
17
18 import static org.junit.Assert.*;
19
20 import org.junit.Test;
21 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
22 import org.openo.nfvo.jujuvnfmadapter.common.servicetoken.VnfmRestfulUtil;
23
24 import mockit.Mock;
25 import mockit.MockUp;
26 import net.sf.json.JSONArray;
27 import net.sf.json.JSONObject;
28
29 public class VnfmUtilTest {
30     
31     @Test
32     public void getVnfmByIdTestNull(){ 
33         JSONObject resp = VnfmUtil.getVnfmById("vnfmId");
34         assertNull(resp);
35     }
36     @Test
37     public void getVnfmByIdTest(){
38         new MockUp<VnfmRestfulUtil>(){
39             @Mock
40             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
41                 RestfulResponse resp = new RestfulResponse();
42                 resp.setStatus(200);
43                 JSONObject json = new JSONObject();
44                 json.put("id", "1234");
45                 resp.setResponseJson(json.toString());
46                 return resp;
47             }
48         };
49         JSONObject resp = VnfmUtil.getVnfmById("vnfmId");
50         assertNotNull(resp);
51     }
52     @Test
53     public void getVnfmByIdTest2(){
54         new MockUp<VnfmRestfulUtil>(){
55             @Mock
56             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
57                 RestfulResponse resp = new RestfulResponse();
58                 resp.setStatus(500);
59                 return resp;
60             }
61         };
62         JSONObject resp = VnfmUtil.getVnfmById("vnfmId");
63         assertNull(resp);
64     }
65     @Test
66     public void getVnfmIdByIpNullResp(){
67         String resp = VnfmUtil.getVnfmIdByIp("1.1.1.1");
68         assertEquals(resp,"");
69     }
70     @Test
71     public void getVnfmIdByIpInternalError(){
72         new MockUp<VnfmRestfulUtil>(){
73             @Mock
74             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
75                 RestfulResponse resp = new RestfulResponse();
76                 resp.setStatus(500);
77                 return resp;
78             }
79         };
80         String resp = VnfmUtil.getVnfmIdByIp("1.1.1.1");
81         assertEquals(resp,"");
82     }
83     @Test
84     public void getVnfmIdByIp(){
85         new MockUp<VnfmRestfulUtil>(){
86             @Mock
87             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
88                 RestfulResponse resp = new RestfulResponse();
89                 JSONArray jsonArray = new JSONArray();
90                 JSONObject jsonobj = new JSONObject();
91                 jsonobj.put("url", "1.1.1.1");
92                 jsonobj.put("vnfmId", "1111");
93                 jsonArray.add(jsonobj);
94                 resp.setResponseJson(jsonArray.toString());
95                 resp.setStatus(200);
96                 return resp;
97             }
98         };
99         String resp = VnfmUtil.getVnfmIdByIp("1.1.1.1");
100         assertEquals(resp,"1111");
101     }
102     
103     @Test
104     public void getVnfmIdByIpInvalidIP(){
105         new MockUp<VnfmRestfulUtil>(){
106             @Mock
107             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
108                 RestfulResponse resp = new RestfulResponse();
109                 JSONArray jsonArray = new JSONArray();
110                 JSONObject jsonobj = new JSONObject();
111                 jsonobj.put("url", "1.1.1.1");
112                 jsonobj.put("vnfmId", "1111");
113                 jsonArray.add(jsonobj);
114                 resp.setResponseJson(jsonArray.toString());
115                 resp.setStatus(200);
116                 return resp;
117             }
118         };
119         String resp = VnfmUtil.getVnfmIdByIp("1.1.1.2");
120         assertEquals(resp,"");
121     }
122
123 }