Add notification system util in GVNFM
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / tests / test_query_vnf_lcm_op.py
1 # Copyright (C) 2018 Verizon. All Rights Reserved
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 VNFLcmOpOccModel
21
22
23 class TestVNFLcmOpOccs(TestCase):
24     def setUp(self):
25         self.client = Client()
26         self.vnf_lcm_op_occ_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
27         VNFLcmOpOccModel.objects.all().delete()
28         self.test_single_vnf_lcm_op = {
29             "id": "99442b18-a5c7-11e8-998c-bf1755941f16",
30             "operationState": "STARTING",
31             "stateEnteredTime": "2018-07-09",
32             "startTime": "2018-07-09",
33             "vnfInstanceId": "cd552c9c-ab6f-11e8-b354-236c32aa91a1",
34             "grantId": None,
35             "operation": "SCALE",
36             "isAutomaticInvocation": False,
37             "operationParams": {},
38             "isCancelPending": False,
39             "cancelMode": None,
40             "error": None,
41             "resourceChanges": None,
42             "changedInfo": None,
43             "changedExtConnectivity": None,
44             "_links": {
45                 "self": {
46                     "href": "demo"
47                 },
48                 "vnfInstance": "demo"
49             }
50         }
51         self.test_vnflcmop_with_exclude_default = [{
52             "id": "99442b18-a5c7-11e8-998c-bf1755941f16",
53             "operationState": "STARTING",
54             "stateEnteredTime": "2018-07-09",
55             "startTime": "2018-07-09",
56             "vnfInstanceId": "cd552c9c-ab6f-11e8-b354-236c32aa91a1",
57             "grantId": None,
58             "operation": "SCALE",
59             "isAutomaticInvocation": False,
60             "isCancelPending": False,
61             "cancelMode": None,
62             "_links": {
63                 "self": {
64                     "href": "demo"
65                 },
66                 "vnfInstance": "demo"
67             }
68         }]
69
70         self.test_multiple_vnf_lcm_op = [{
71             "id": "a6b9415c-ab99-11e8-9d37-dbb5e0378955",
72             "operationState": "STARTING",
73             "stateEnteredTime": "2018-07-09",
74             "startTime": "2018-07-09",
75             "vnfInstanceId": "cd552c9c-ab6f-11e8-b354-236c32aa91a1",
76             "grantId": None,
77             "operation": "INSTANTIATE",
78             "isAutomaticInvocation": False,
79             "operationParams": {},
80             "isCancelPending": False,
81             "cancelMode": None,
82             "error": None,
83             "resourceChanges": None,
84             "changedInfo": None,
85             "changedExtConnectivity": None,
86             "_links": {
87                 "self": {
88                     "href": "demo"
89                 },
90                 "vnfInstance": "demo"
91             }
92         }]
93         self.test_multiple_vnf_lcm_op.append(
94             self.test_single_vnf_lcm_op)
95
96     def tearDown(self):
97         pass
98
99     def test_get_vnflcmopoccs(self):
100         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
101         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
102         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
103                          state_entered_time="2018-07-09", start_time="2018-07-09",
104                          vnf_instance_id=vnf_instance_id,
105                          grant_id=None, operation="SCALE", is_automatic_invocation=False,
106                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
107                          error=None, resource_changes=None, changed_ext_connectivity=None,
108                          links=json.dumps({"self": {"href": "demo"}, "vnfInstance": "demo"})).save()
109         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs", format='json')
110         self.assertEqual(response.status_code, status.HTTP_200_OK)
111         self.assertEqual([self.test_single_vnf_lcm_op], response.data)
112
113     def test_get_vnflcmopoccs_with_id_not_exist(self):
114         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs?id=dummy", format='json')
115         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
116         expected_data = {
117             "status": 500,
118             "detail": "LCM Operation Occurances do not exist"
119         }
120         self.assertEqual(expected_data, response.data)
121
122     def test_get_vnflcmopoccs_with_filters(self):
123         lcm_op_id = "a6b9415c-ab99-11e8-9d37-dbb5e0378955"
124         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
125         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
126                          state_entered_time="2018-07-09", start_time="2018-07-09",
127                          vnf_instance_id=vnf_instance_id,
128                          grant_id=None, operation="INSTANTIATE", is_automatic_invocation=False,
129                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
130                          error=None, resource_changes=None, changed_ext_connectivity=None,
131                          links=json.dumps({"self": {"href": "demo"}, "vnfInstance": "demo"})).save()
132
133         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
134         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
135                          state_entered_time="2018-07-09", start_time="2018-07-09",
136                          vnf_instance_id=vnf_instance_id,
137                          grant_id=None, operation="SCALE", is_automatic_invocation=False,
138                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
139                          error=None, resource_changes=None, changed_ext_connectivity=None,
140                          links=json.dumps({"self": {"href": "demo"}, "vnfInstance": "demo"})).save()
141         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs", format='json')
142         self.assertEqual(response.status_code, status.HTTP_200_OK)
143         self.assertEqual(self.test_multiple_vnf_lcm_op, response.data)
144
145         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs?operation=SCALE", format='json')
146         self.assertEqual(response.status_code, status.HTTP_200_OK)
147         self.assertEqual([self.test_single_vnf_lcm_op], response.data)
148
149         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs?vnfInstanceId=%s" % vnf_instance_id, format='json')
150         self.assertEqual(response.status_code, status.HTTP_200_OK)
151         self.assertEqual(self.test_multiple_vnf_lcm_op, response.data)
152
153     def test_get_vnflcmopoccs_with_extra_flags(self):
154         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
155         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
156         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
157                          state_entered_time="2018-07-09", start_time="2018-07-09",
158                          vnf_instance_id=vnf_instance_id,
159                          grant_id=None, operation="SCALE", is_automatic_invocation=False,
160                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
161                          error=None, resource_changes=None, changed_ext_connectivity=None,
162                          links=json.dumps({"self": {"href": "demo"}, "vnfInstance": "demo"})).save()
163         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs?exclude_default", format='json')
164         self.assertEqual(response.status_code, status.HTTP_200_OK)
165         self.assertEqual(self.test_vnflcmop_with_exclude_default, response.data)
166
167     def test_get_vnflcmopocc_with_id(self):
168         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
169         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
170         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
171                          state_entered_time="2018-07-09", start_time="2018-07-09",
172                          vnf_instance_id=vnf_instance_id,
173                          grant_id=None, operation="SCALE", is_automatic_invocation=False,
174                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
175                          error=None, resource_changes=None, changed_ext_connectivity=None,
176                          links=json.dumps({"self": {"href": "demo"}, "vnfInstance": "demo"})).save()
177         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs/" + lcm_op_id, format='json')
178         self.assertEqual(response.status_code, status.HTTP_200_OK)
179         self.assertEqual(self.test_single_vnf_lcm_op, response.data)
180
181     def test_single_vnflcmopocc_with_unknown_id(self):
182         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
183         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs/" + lcm_op_id, format='json')
184         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
185         expected_data = {
186             "status": 500,
187             "detail": "LCM Operation Occurance does not exist"
188         }
189         self.assertEqual(expected_data, response.data)