Deal with nfPackage
[vfc/nfvo/catalog.git] / catalog / packages / tests / test_vnf_package.py
1 # Copyright 2018 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import os
16 import json
17 import mock
18 import urllib2
19
20 from rest_framework.test import APIClient
21 from django.test import TestCase
22 from rest_framework import status
23 from catalog.pub.config.config import CATALOG_ROOT_PATH
24 from catalog.packages.biz.vnfpackage import VnfpkgUploadThread
25
26
27 class MockReq():
28     def read(self):
29         return "1"
30
31     def close(self):
32         pass
33
34
35 class TestVnfPackage(TestCase):
36     def setUp(self):
37         self.client = APIClient()
38
39     def tearDown(self):
40         pass
41
42     def test_upload_vnfPkg(self):
43         data = {'file': open(os.path.join(CATALOG_ROOT_PATH, "empty.txt"), "rb")}
44         response = self.client.put("/api/vnfpkgm/v1/vnf_packages/222/package_content", data=data)
45         self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
46
47     @mock.patch.object(urllib2, 'urlopen')
48     def test_upload_nf_pkg(self, mock_urlopen):
49         req_data = {"addressInformation": "https://127.0.0.1:1234/sdc/v1/hss.csar"}
50         mock_urlopen.return_value = MockReq()
51         vnfPkgId = "222"
52         VnfpkgUploadThread(req_data, vnfPkgId).run()
53
54     def test_create_vnf_pkg(self):
55         req_data = {
56             "userDefinedData": {"a": "A"}
57         }
58         response = self.client.post("/api/vnfpkgm/v1/vnf_packages", data=req_data, format="json")
59         resp_data = json.loads(response.content)
60         expect_resp_data = {
61             "id": resp_data.get("id"),
62             "onboardingState": "CREATED",
63             "operationalState": "DISABLED",
64             "usageState": "NOT_IN_USE",
65             "userDefinedData": {"a": "A"},
66             "_links": None  # TODO
67         }
68         self.assertEqual(expect_resp_data, resp_data)
69         self.assertEqual(response.status_code, status.HTTP_201_CREATED)