update link to upper-constraints.txt
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_query_ns_lcm_op.py
1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
2 # Copyright (c) 2019 ZTE Corporation.
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 import json
17 from rest_framework.test import APIClient
18 from django.test import TestCase
19 from rest_framework import status
20 from lcm.pub.database.models import NSLcmOpOccModel
21 from lcm.ns.tests import OCCURRENCE_DICT, NSLCMOP_WITH_EXCLUDE_DEFAULT_DICT
22
23
24 class TestNSLcmOpOccs(TestCase):
25     def setUp(self):
26         self.client = APIClient()
27         self.multiple_ns_lcm_op = OCCURRENCE_DICT
28         self.single_ns_lcm_op = self.multiple_ns_lcm_op[0]
29         self.nslcmop_with_exclude_default = NSLCMOP_WITH_EXCLUDE_DEFAULT_DICT
30         NSLcmOpOccModel.objects.all().delete()
31
32     def tearDown(self):
33         pass
34
35     def test_get_nslcmopoccs(self):
36         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
37         ns_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
38         NSLcmOpOccModel(
39             id=lcm_op_id,
40             operation_state="STARTING",
41             state_entered_time="2019-01-01",
42             start_time="2019-01-01",
43             ns_instance_id=ns_instance_id,
44             operation="SCALE",
45             is_automatic_invocation=False,
46             operation_params='{}',
47             is_cancel_pending=False,
48             cancel_mode=None,
49             error=None,
50             resource_changes=None,
51             links=json.dumps({"self": {"href": "demo"}, "nsInstance": "demo"})).save()
52         response = self.client.get("/api/nslcm/v1/ns_lcm_op_occs", format='json')
53         self.assertEqual(response.status_code, status.HTTP_200_OK)
54
55     def test_get_nslcmopoccs_with_id_not_exist(self):
56         response = self.client.get("/api/nslcm/v1/ns_lcm_op_occs?id=dummy", format='json')
57         self.assertEqual(response.status_code, status.HTTP_200_OK)
58
59     def test_get_nslcmopoccs_with_filters(self):
60         lcm_op_id = "a6b9415c-ab99-11e8-9d37-dbb5e0378955"
61         ns_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
62         NSLcmOpOccModel(
63             id=lcm_op_id,
64             operation_state="STARTING",
65             state_entered_time="2019-01-01",
66             start_time="2019-01-01",
67             ns_instance_id=ns_instance_id,
68             operation="INSTANTIATE",
69             is_automatic_invocation=False,
70             operation_params='{}',
71             is_cancel_pending=False,
72             cancel_mode=None,
73             error=None,
74             resource_changes=None,
75             links=json.dumps({"self": {"href": "demo"}, "nsInstance": "demo"})).save()
76         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
77         NSLcmOpOccModel(
78             id=lcm_op_id,
79             operation_state="STARTING",
80             state_entered_time="2019-01-01",
81             start_time="2019-01-01",
82             ns_instance_id=ns_instance_id,
83             operation="SCALE",
84             is_automatic_invocation=False,
85             operation_params='{}',
86             is_cancel_pending=False,
87             cancel_mode=None,
88             error=None,
89             resource_changes=None,
90             links=json.dumps({"self": {"href": "demo"}, "nsInstance": "demo"})).save()
91         response = self.client.get("/api/nslcm/v1/ns_lcm_op_occs", format='json')
92         self.assertEqual(response.status_code, status.HTTP_200_OK)
93         response = self.client.get("/api/nslcm/v1/ns_lcm_op_occs?operation=SCALE", format='json')
94         self.assertEqual(response.status_code, status.HTTP_200_OK)
95         response = self.client.get("/api/nslcm/v1/ns_lcm_op_occs?nsInstanceId=%s" % ns_instance_id, format='json')
96         self.assertEqual(response.status_code, status.HTTP_200_OK)
97
98     def test_get_nslcmopoccs_with_extra_flags(self):
99         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
100         ns_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
101         NSLcmOpOccModel(
102             id=lcm_op_id,
103             operation_state="STARTING",
104             state_entered_time="2019-01-01",
105             start_time="2019-01-01",
106             ns_instance_id=ns_instance_id,
107             operation="SCALE",
108             is_automatic_invocation=False,
109             operation_params='{}',
110             is_cancel_pending=False,
111             cancel_mode=None,
112             error=None,
113             resource_changes=None,
114             links=json.dumps({"self": {"href": "demo"}, "nsInstance": "demo"})).save()
115         response = self.client.get("/api/nslcm/v1/ns_lcm_op_occs?exclude_default", format='json')
116         self.assertEqual(response.status_code, status.HTTP_200_OK)
117
118     def test_get_nslcmopocc_with_id(self):
119         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
120         ns_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
121         NSLcmOpOccModel(
122             id=lcm_op_id,
123             operation_state="STARTING",
124             state_entered_time="2019-01-01",
125             start_time="2019-01-01",
126             ns_instance_id=ns_instance_id,
127             operation="SCALE",
128             is_automatic_invocation=False,
129             operation_params='{}',
130             is_cancel_pending=False,
131             cancel_mode=None,
132             error=None,
133             resource_changes=None,
134             links=json.dumps({"self": {"href": "demo"}, "nsInstance": "demo"})).save()
135         response = self.client.get("/api/nslcm/v1/ns_lcm_op_occs/" + lcm_op_id, format='json')
136         self.assertEqual(response.status_code, status.HTTP_200_OK)
137         self.assertEqual(self.single_ns_lcm_op['id'], response.data['id'])
138
139     def test_single_nslcmopocc_with_unknown_id(self):
140         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
141         response = self.client.get("/api/nslcm/v1/ns_lcm_op_occs/" + lcm_op_id, format='json')
142         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
143         expected_data = {
144             "status": 500,
145             "detail": "LCM Operation Occurance does not exist"
146         }
147         self.assertEqual(expected_data, response.data)