summaryrefslogtreecommitdiffstats
path: root/lcm/ns/tests/test_ns_create.py
blob: 7f6c7dac753bfbdec1836357520ba2e64842d2aa (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
# Copyright 2017 ZTE Corporation.
#
# 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.
import uuid

import mock
from django.test import TestCase, Client
from rest_framework import status

from lcm.ns.ns_create import CreateNSService
from lcm.pub.database.models import NSInstModel
from lcm.pub.exceptions import NSLCMException


class TestNsInstantiate(TestCase):
    def setUp(self):
        self.client = Client()
        self.nsd_id = str(uuid.uuid4())
        self.ns_package_id = str(uuid.uuid4())

    def tearDown(self):
        NSInstModel.objects.all().delete()

    # @mock.patch.object(restcall, 'call_req')
    # def test_create_ns(self, mock_call_req):
    #     nspackage_info = {
    #         "csarId": self.ns_package_id,
    #         "packageInfo": {}
    #     }
    #     r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(nspackage_info), '201']
    #     r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
    #     mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai]
    #     data = {
    #         "context": {
    #             "global-customer-id": "global-customer-id-test1",
    #             "service-type": "service-type-test1"
    #         },
    #         "csarId": self.nsd_id,
    #         "nsName": "ns",
    #         "description": "description"
    #     }
    #     response = self.client.post("/api/nslcm/v1/ns", data=data, format='json')
    #     self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)

    @mock.patch.object(CreateNSService, "do_biz")
    def test_create_ns_empty_data(self, mock_do_biz):
        mock_do_biz.side_effect = Exception("Exception in CreateNS.")

        data = {}

        response = self.client.post("/api/nslcm/v1/ns", data=data)
        self.assertEqual(response.data["error"], "Exception in CreateNS.")
        self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
        self.assertIn("error", response.data)

    @mock.patch.object(CreateNSService, "do_biz")
    def test_create_ns_non_existing_nsd(self, mock_do_biz):
        mock_do_biz.side_effect = NSLCMException("nsd not exists.")
        new_nsd_id = '1'

        data = {
            'nsdid': new_nsd_id,
            'nsname': 'ns',
            'description': 'description'}

        response = self.client.post("/api/nslcm/v1/ns", data=data)
        self.assertEqual(response.data["error"], "nsd not exists.")
        self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
        self.assertIn("error", response.data)