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 / 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
17 package org.openo.nfvo.vnfmadapter.common;
18
19 import static org.junit.Assert.*;
20
21 import org.junit.Test;
22 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
23 import org.openo.nfvo.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
24
25 import mockit.Mock;
26 import mockit.MockUp;
27 import net.sf.json.JSONArray;
28 import net.sf.json.JSONObject;
29
30 public class VnfmUtilTest {
31
32     @Test
33     public void getVnfmByIdTestNullResp(){
34         JSONObject resp = VnfmUtil.getVnfmById("1234");
35         assertNull(resp);
36     }
37
38     @Test
39     public void getVnfmByIdTestSuccess(){
40         new MockUp<VnfmRestfulUtil>(){
41             @Mock
42             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
43                 RestfulResponse resp = new RestfulResponse();
44                 resp.setStatus(200);
45                 return resp;
46             }
47         };
48         JSONObject resp = VnfmUtil.getVnfmById("1234");
49         assertNotNull(resp);
50     }
51
52     @Test
53     public void getVnfmIdByIpTestNullResp(){
54         String resp = VnfmUtil.getVnfmIdByIp("localhost");
55         assertTrue("".equals(resp));
56     }
57
58     @Test
59     public void getVnfmIdByIpTestSuccess(){
60         new MockUp<VnfmRestfulUtil>(){
61             @Mock
62             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
63                 RestfulResponse resp = new RestfulResponse();
64                 resp.setStatus(200);
65                 JSONArray respArray = new JSONArray();
66                 JSONObject obj = new JSONObject();
67                 obj.put("url", "localhost");
68                 obj.put("vnfmId", "1234");
69                 respArray.add(obj);
70                 resp.setResponseJson(respArray.toString());
71                 return resp;
72             }
73         };
74         String resp = VnfmUtil.getVnfmIdByIp("localhost");
75         assertTrue("1234".equals(resp));
76     }
77
78     @Test
79     public void getVnfmIdByIpTestSuccessInvalidIP(){
80         new MockUp<VnfmRestfulUtil>(){
81             @Mock
82             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
83                 RestfulResponse resp = new RestfulResponse();
84                 resp.setStatus(200);
85                 JSONArray respArray = new JSONArray();
86                 JSONObject obj = new JSONObject();
87                 obj.put("url", "127.0.0.1");
88                 obj.put("vnfmId", "1234");
89                 respArray.add(obj);
90                 resp.setResponseJson(respArray.toString());
91                 return resp;
92             }
93         };
94         String resp = VnfmUtil.getVnfmIdByIp("localhost");
95         assertTrue("".equals(resp));
96     }
97     @Test
98     public void getVnfmIdByIpTestSuccessEmptyResp(){
99         new MockUp<VnfmRestfulUtil>(){
100             @Mock
101             public RestfulResponse getRemoteResponse(String url, String methodType, String params) {
102                 RestfulResponse resp = new RestfulResponse();
103                 resp.setStatus(200);
104                 JSONArray respArray = new JSONArray();
105                 resp.setResponseJson(respArray.toString());
106                 return resp;
107             }
108         };
109         String resp = VnfmUtil.getVnfmIdByIp("localhost");
110         assertTrue("".equals(resp));
111     }
112
113 }