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.vnf_package import VnfPkgUploadThread
25 from catalog.pub.database.models import VnfPackageModel
26
27
28 class MockReq():
29     def read(self):
30         return "1"
31
32     def close(self):
33         pass
34
35
36 class TestVnfPackage(TestCase):
37     def setUp(self):
38         self.client = APIClient()
39
40     def tearDown(self):
41         pass
42
43     def test_upload_vnf_pkg(self):
44         data = {'file': open(os.path.join(CATALOG_ROOT_PATH, "empty.txt"), "rb")}
45         VnfPackageModel.objects.create(
46             vnfPackageId="222",
47             onboardingState="CREATED"
48         )
49         response = self.client.put("/api/vnfpkgm/v1/vnf_packages/222/package_content", data=data)
50         self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
51
52     @mock.patch.object(urllib2, 'urlopen')
53     def test_upload_nf_pkg_from_uri(self, mock_urlopen):
54         vnf_pkg = VnfPackageModel.objects.create(
55             vnfPackageId="222",
56             onboardingState="CREATED"
57         )
58         req_data = {"addressInformation": "https://127.0.0.1:1234/sdc/v1/hss.csar"}
59         mock_urlopen.return_value = MockReq()
60         vnf_pkg_id = vnf_pkg.vnfPackageId
61         VnfPkgUploadThread(req_data, vnf_pkg_id).run()
62
63     def test_create_vnf_pkg(self):
64         req_data = {
65             "userDefinedData": {"a": "A"}
66         }
67         response = self.client.post("/api/vnfpkgm/v1/vnf_packages", data=req_data, format="json")
68         resp_data = json.loads(response.content)
69         expect_resp_data = {
70             "id": resp_data.get("id"),
71             "onboardingState": "CREATED",
72             "operationalState": "DISABLED",
73             "usageState": "NOT_IN_USE",
74             "userDefinedData": {"a": "A"},
75             "_links": None  # TODO
76         }
77         self.assertEqual(expect_resp_data, resp_data)
78         self.assertEqual(response.status_code, status.HTTP_201_CREATED)