2e1c15a0685e89c3bb8a22838b167023679cfc7e
[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": "demo",
46                 "vnfInstance": "demo"
47             }
48         }]
49         self.test_vnflcmop_with_exclude_default = [{
50             "id": "99442b18-a5c7-11e8-998c-bf1755941f16",
51             "operationState": "STARTING",
52             "stateEnteredTime": "2018-07-09",
53             "startTime": "2018-07-09",
54             "vnfInstanceId": "cd552c9c-ab6f-11e8-b354-236c32aa91a1",
55             "grantId": None,
56             "operation": "SCALE",
57             "isAutomaticInvocation": False,
58             "isCancelPending": False,
59             "cancelMode": None,
60             "_links": {
61                 "self": "demo",
62                 "vnfInstance": "demo"
63             }
64         }]
65
66         self.test_multiple_vnf_lcm_op = [{
67             "id": "a6b9415c-ab99-11e8-9d37-dbb5e0378955",
68             "operationState": "STARTING",
69             "stateEnteredTime": "2018-07-09",
70             "startTime": "2018-07-09",
71             "vnfInstanceId": "cd552c9c-ab6f-11e8-b354-236c32aa91a1",
72             "grantId": None,
73             "operation": "INSTANTIATE",
74             "isAutomaticInvocation": False,
75             "operationParams": {},
76             "isCancelPending": False,
77             "cancelMode": None,
78             "error": None,
79             "resourceChanges": None,
80             "changedInfo": None,
81             "changedExtConnectivity": None,
82             "_links": {
83                 "self": "demo",
84                 "vnfInstance": "demo"
85             }
86         }]
87         self.test_multiple_vnf_lcm_op.append(
88             self.test_single_vnf_lcm_op[0])
89
90     def tearDown(self):
91         pass
92
93     def test_get_vnflcmopocc(self):
94         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
95         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
96         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
97                          state_entered_time="2018-07-09", start_time="2018-07-09",
98                          vnf_instance_id=vnf_instance_id,
99                          grant_id=None, operation="SCALE", is_automatic_invocation=False,
100                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
101                          error=None, resource_changes=None, changed_ext_connectivity=None,
102                          links=json.dumps({"self": "demo", "vnfInstance": "demo"})).save()
103         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs", format='json')
104         self.assertEqual(response.status_code, status.HTTP_200_OK)
105         self.assertEqual(self.test_single_vnf_lcm_op, response.data)
106
107     def test_get_vnflcmopocc_with_id_not_exist(self):
108         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs?id=dummy", format='json')
109         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
110         expected_data = {
111             "status": 500,
112             "detail": "LCM Operation Occurances do not exist"
113         }
114         self.assertEqual(expected_data, response.data)
115
116     def test_get_vnflcmopocc_with_filters(self):
117         lcm_op_id = "a6b9415c-ab99-11e8-9d37-dbb5e0378955"
118         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
119         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
120                          state_entered_time="2018-07-09", start_time="2018-07-09",
121                          vnf_instance_id=vnf_instance_id,
122                          grant_id=None, operation="INSTANTIATE", is_automatic_invocation=False,
123                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
124                          error=None, resource_changes=None, changed_ext_connectivity=None,
125                          links=json.dumps({"self": "demo", "vnfInstance": "demo"})).save()
126
127         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
128         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
129                          state_entered_time="2018-07-09", start_time="2018-07-09",
130                          vnf_instance_id=vnf_instance_id,
131                          grant_id=None, operation="SCALE", is_automatic_invocation=False,
132                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
133                          error=None, resource_changes=None, changed_ext_connectivity=None,
134                          links=json.dumps({"self": "demo", "vnfInstance": "demo"})).save()
135         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs", format='json')
136         self.assertEqual(response.status_code, status.HTTP_200_OK)
137         self.assertEqual(self.test_multiple_vnf_lcm_op, response.data)
138
139         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs?operation=SCALE", format='json')
140         self.assertEqual(response.status_code, status.HTTP_200_OK)
141         self.assertEqual(self.test_single_vnf_lcm_op, response.data)
142
143         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs?vnfInstanceId=%s" % vnf_instance_id, format='json')
144         self.assertEqual(response.status_code, status.HTTP_200_OK)
145         self.assertEqual(self.test_multiple_vnf_lcm_op, response.data)
146
147     def test_get_vnflcmopocc_with_extra_flags(self):
148         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
149         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
150         VNFLcmOpOccModel(id=lcm_op_id, operation_state="STARTING",
151                          state_entered_time="2018-07-09", start_time="2018-07-09",
152                          vnf_instance_id=vnf_instance_id,
153                          grant_id=None, operation="SCALE", is_automatic_invocation=False,
154                          operation_params='{}', is_cancel_pending=False, cancel_mode=None,
155                          error=None, resource_changes=None, changed_ext_connectivity=None,
156                          links=json.dumps({"self": "demo", "vnfInstance": "demo"})).save()
157         response = self.client.get("/api/vnflcm/v1/vnf_lcm_op_occs?exclude_default", format='json')
158         self.assertEqual(response.status_code, status.HTTP_200_OK)
159         self.assertEqual(self.test_vnflcmop_with_exclude_default, response.data)