summaryrefslogtreecommitdiffstats
path: root/huawei/vnfmadapter/VnfmadapterService/service/src/test/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManagerTest.java
blob: 95727339a42c04d10cd069f83d8c040d76c8016a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.DownloadCsarManager;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VNFRestfulUtil;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl.AdapterResourceManager;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.testutils.JsonUtil;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;

import mockit.Mock;
import mockit.MockUp;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class AdapterResourceManagerTest {

    AdapterResourceManager manager = null;

    @Before
    public void setUp() {
        manager = new AdapterResourceManager();
    }

    @Test(expected = JSONException.class)
    public void uploadVNFPackageTestJsonException() {
        JSONObject vnfpkg = new JSONObject();
        Map<String, String> paramsMap = new HashMap<>();
        JSONObject res = manager.uploadVNFPackage(vnfpkg, paramsMap);
        assertTrue(res != null);

    }

    @Test(expected = JSONException.class)
    public void uploadVNFPackageTestJsonException2() {
        JSONObject vnfpkg = new JSONObject();
        Map<String, String> paramsMap = new HashMap<>();
        JSONObject res = manager.uploadVNFPackage(null, paramsMap);
        assertTrue(res != null);

    }

    @Test
    public void uploadVNFPackageTestJsonException3() {
        JSONObject vnfpkg = new JSONObject();
        vnfpkg.put("name", "test");
        Map<String, String> paramsMap = new HashMap<>();
        JSONObject res = manager.uploadVNFPackage(vnfpkg, paramsMap);
        assertTrue(res.get("reason").equals("csarid and vnfmid are null."));

    }

    @Test
    public void uploadVNFPackageTestEmptyParam() {
        JSONObject vnfpkg = new JSONObject();
        vnfpkg.put("name", "test");
        Map<String, String> paramsMap = new HashMap<>();
        JSONObject res = manager.uploadVNFPackage(vnfpkg, paramsMap);
        assertTrue(res.get("reason").equals("csarid and vnfmid are null."));

    }

    @Test
    public void uploadVNFPackageTestNullParam() {
        JSONObject vnfpkg = new JSONObject();
        vnfpkg.put("name", "test");
        Map<String, String> paramsMap = new HashMap<>();
        JSONObject res = manager.uploadVNFPackage(vnfpkg, null);
        assertTrue(res.get("reason").equals("csarid and vnfmid are null."));

    }

    @Test
    public void uploadVNFPackageTestInvalidCsrid() {
        JSONObject vnfpkg = new JSONObject();
        vnfpkg.put("name", "test");
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("csarid", "");
        JSONObject res = manager.uploadVNFPackage(vnfpkg, paramsMap);
        assertTrue(res.get("reason").equals("csarid is null."));

    }

    @Test
    public void uploadVNFPackageTestInvalidCsrid2() {
        JSONObject vnfpkg = new JSONObject();
        vnfpkg.put("name", "test");
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("csarid", null);
        JSONObject res = manager.uploadVNFPackage(vnfpkg, paramsMap);
        assertTrue(res.get("reason").equals("csarid is null."));

    }

    @Test
    public void uploadVNFPackageTestInvalidVnfmid() {
        JSONObject vnfpkg = new JSONObject();
        vnfpkg.put("name", "test");
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("csarid", "csarid123");
        paramsMap.put("vnfmid", null);
        JSONObject res = manager.uploadVNFPackage(vnfpkg, paramsMap);
        assertTrue(res.get("reason").equals("vnfmid is null."));

    }

    @Test
    public void uploadVNFPackageTestInvalidVnfmid2() {
        JSONObject vnfpkg = new JSONObject();
        vnfpkg.put("name", "test");
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("csarid", "csarid123");
        paramsMap.put("vnfmid", "");
        JSONObject res = manager.uploadVNFPackage(vnfpkg, paramsMap);
        assertTrue(res.get("reason").equals("vnfmid is null."));

    }

    @Test
    public void uploadVNFPackageTestNullResp() {
        JSONObject vnfpkg = new JSONObject();
        vnfpkg.put("name", "test");
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("csarid", "csarid123");
        paramsMap.put("vnfmid", "vnfmid1234");
        JSONObject res = manager.uploadVNFPackage(vnfpkg, paramsMap);
        assertTrue(res.get("reason").equals("RestfulResponse is null."));

    }

    @Test
    public void downloadCsarTestNullUrl() {
        String url = null;
        String filePath = null;
        JSONObject res = manager.downloadCsar(url, filePath);
        assertTrue(res.get("reason").equals("url is null."));
    }

    @Test
    public void downloadCsarTestEmptyUrl() {
        String url = "";
        String filePath = null;
        JSONObject res = manager.downloadCsar(url, filePath);
        assertTrue(res.get("reason").equals("url is null."));
    }

    @Test
    public void downloadCsarTestNullFilePath() {
        String url = "http://localhost:8080";
        String filePath = null;
        JSONObject res = manager.downloadCsar(url, filePath);
        assertTrue(res.get("reason").equals("downloadUrl filePath is null."));
    }

    @Test
    public void downloadCsarTestEmptyFilePath() {
        String url = "http://localhost:8080";
        String filePath = "";
        JSONObject res = manager.downloadCsar(url, filePath);
        assertTrue(res.get("reason").equals("downloadUrl filePath is null."));
    }

    @Test
    public void getVnfmConnInfoTestSuccess() {
        new MockUp<VNFRestfulUtil>() {

            @Mock
            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
                RestfulResponse resp = new RestfulResponse();
                resp.setStatus(200);
                Map<String, String> objMap = new HashMap<String, String>();
                objMap.put("id", "test123");

                String responseString = toJson(objMap);
                resp.setResponseJson(responseString);
                return resp;
            }

        };
        Map<String, String> paramsMap = new HashMap<String, String>();
        JSONObject res = manager.getVnfmConnInfo(paramsMap);
        assertTrue(res.get("id").equals("test123"));
    }

    @Test
    public void getVnfmConnInfoTestNullResp() {
        new MockUp<VNFRestfulUtil>() {

            @Mock
            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {

                return null;
            }

        };
        Map<String, String> paramsMap = new HashMap<String, String>();
        JSONObject res = manager.getVnfmConnInfo(paramsMap);
        assertTrue(res.get("reason").equals("RestfulResponse is null."));
    }

    @Test
    public void getVnfmConnInfoTestServerError() {
        new MockUp<VNFRestfulUtil>() {

            @Mock
            public RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
                RestfulResponse resp = new RestfulResponse();
                resp.setStatus(500);
                Map<String, String> objMap = new HashMap<String, String>();
                objMap.put("id", "test123");

                String responseString = toJson(objMap);
                resp.setResponseJson(responseString);
                return resp;
            }

        };
        Map<String, String> paramsMap = new HashMap<String, String>();
        JSONObject res = manager.getVnfmConnInfo(paramsMap);
        assertTrue(res.get("reason").equals("ESR return fail."));
    }

    @Test
    public void uploadTestInternalError() {
        JSONObject vnfpackage = new JSONObject();
        String vnfmurl = "http://localhost";
        String conntoken = "test";
        JSONObject res = manager.upload(vnfpackage, vnfmurl, conntoken);
        assertEquals(res.get("retCode"), 500);
    }

    public static String toJson(Map o) {
        try {
            return JsonUtil.marshal(o);
        } catch(IOException e) {
            return "";
        }
    }

    @Test
    public void testGetAllCloud(){
        String url = null;
        manager.getAllCloud(url,null);
    }

    @Test
    public void testGetAllCloud2(){
        String url = "http://127.0.0.1:31943";
        manager.getAllCloud(url,null);
    }
    @Test
    public void testUnzipCSAR(){
        manager.unzipCSAR(null,null);
    }
    @Test
    public void testUnzipCSAR2(){
        manager.unzipCSAR("vCpe.zip",null);
    }
    @Test
    public void testUnzipCSAR3(){
        manager.unzipCSAR("vCpe.zip","/opt");
    }
    @Test
    public void testUnzipCSAR4(){
        new MockUp<DownloadCsarManager>(){
            @Mock
            public  int unzipCSAR(String fileName,String filePath){
                return 0;
            }
        };
        manager.unzipCSAR("vCpe.zip","/opt");
    }
    @Test
    public void testUnzipCSAR5(){
        new MockUp<DownloadCsarManager>(){
            @Mock
            public  int unzipCSAR(String fileName,String filePath){
                return -1;
            }
        };
        manager.unzipCSAR("vCpe.zip","/opt");
    }

    @Test
    public void testGetVnfdVersion(){
        manager.getVnfdVersion("http://","127.0.0.1","token");
    }

}