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