Refactor codes for lcm op occ get ut
[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 import copy
17
18 from django.test import TestCase, Client
19 from rest_framework import status
20
21 from lcm.pub.database.models import VNFLcmOpOccModel
22 from .const import single_vnf_lcm_op
23 from .const import vnflcmop_with_exclude_default
24 from .const import multiple_vnf_lcm_op
25
26
27 class TestVNFLcmOpOccs(TestCase):
28     def setUp(self):
29         self.client = Client()
30         self.vnf_lcm_op_occ_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
31         VNFLcmOpOccModel.objects.all().delete()
32         self.test_single_vnf_lcm_op = single_vnf_lcm_op
33         self.test_vnflcmop_with_exclude_default = vnflcmop_with_exclude_default
34         self.test_multiple_vnf_lcm_op = copy.copy(multiple_vnf_lcm_op)
35         self.test_multiple_vnf_lcm_op.append(self.test_single_vnf_lcm_op)
36
37     def tearDown(self):
38         pass
39
40     def test_get_vnflcmopoccs(self):
41         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
42         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
43         VNFLcmOpOccModel(
44             id=lcm_op_id,
45             operation_state="STARTING",
46             state_entered_time="2018-07-09",
47             start_time="2018-07-09",
48             vnf_instance_id=vnf_instance_id,
49             grant_id=None,
50             operation="SCALE",
51             is_automatic_invocation=False,
52             operation_params='{}',
53             is_cancel_pending=False,
54             cancel_mode=None,
55             error=None,
56             resource_changes=None,
57             changed_ext_connectivity=None,
58             links=json.dumps({
59                 "self": {
60                     "href": "demo"
61                 },
62                 "vnfInstance": "demo"
63             })).save()
64         response = self.client.get(
65             "/api/vnflcm/v1/vnf_lcm_op_occs",
66             format='json'
67         )
68         self.assertEqual(
69             response.status_code,
70             status.HTTP_200_OK
71         )
72         self.assertEqual(
73             [self.test_single_vnf_lcm_op],
74             response.data
75         )
76
77     def test_get_vnflcmopoccs_with_id_not_exist(self):
78         response = self.client.get(
79             "/api/vnflcm/v1/vnf_lcm_op_occs?id=dummy",
80             format='json'
81         )
82         self.assertEqual(
83             response.status_code,
84             status.HTTP_500_INTERNAL_SERVER_ERROR
85         )
86         expected_data = {
87             "status": 500,
88             "detail": "LCM Operation Occurances do not exist"
89         }
90         self.assertEqual(
91             expected_data,
92             response.data
93         )
94
95     def test_get_vnflcmopoccs_with_filters(self):
96         lcm_op_id = "a6b9415c-ab99-11e8-9d37-dbb5e0378955"
97         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
98         VNFLcmOpOccModel(
99             id=lcm_op_id,
100             operation_state="STARTING",
101             state_entered_time="2018-07-09",
102             start_time="2018-07-09",
103             vnf_instance_id=vnf_instance_id,
104             grant_id=None,
105             operation="INSTANTIATE",
106             is_automatic_invocation=False,
107             operation_params='{}',
108             is_cancel_pending=False,
109             cancel_mode=None,
110             error=None,
111             resource_changes=None,
112             changed_ext_connectivity=None,
113             links=json.dumps({
114                 "self": {
115                     "href": "demo"
116                 },
117                 "vnfInstance": "demo"
118             })).save()
119
120         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
121         VNFLcmOpOccModel(
122             id=lcm_op_id,
123             operation_state="STARTING",
124             state_entered_time="2018-07-09",
125             start_time="2018-07-09",
126             vnf_instance_id=vnf_instance_id,
127             grant_id=None,
128             operation="SCALE",
129             is_automatic_invocation=False,
130             operation_params='{}',
131             is_cancel_pending=False,
132             cancel_mode=None,
133             error=None,
134             resource_changes=None,
135             changed_ext_connectivity=None,
136             links=json.dumps({
137                 "self": {
138                     "href": "demo"
139                 },
140                 "vnfInstance": "demo"
141             })).save()
142         response = self.client.get(
143             "/api/vnflcm/v1/vnf_lcm_op_occs",
144             format='json'
145         )
146         self.assertEqual(
147             response.status_code,
148             status.HTTP_200_OK
149         )
150         self.assertEqual(
151             self.test_multiple_vnf_lcm_op,
152             response.data,
153             response.data
154         )
155
156         response = self.client.get(
157             "/api/vnflcm/v1/vnf_lcm_op_occs?operation=SCALE",
158             format='json'
159         )
160         self.assertEqual(
161             response.status_code,
162             status.HTTP_200_OK
163         )
164         self.assertEqual(
165             [self.test_single_vnf_lcm_op],
166             response.data
167         )
168
169         response = self.client.get(
170             "/api/vnflcm/v1/vnf_lcm_op_occs?vnfInstanceId=%s" % vnf_instance_id,
171             format='json'
172         )
173         self.assertEqual(
174             response.status_code,
175             status.HTTP_200_OK
176         )
177         self.assertEqual(
178             self.test_multiple_vnf_lcm_op,
179             response.data
180         )
181
182     def test_get_vnflcmopoccs_with_extra_flags(self):
183         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
184         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
185         VNFLcmOpOccModel(
186             id=lcm_op_id,
187             operation_state="STARTING",
188             state_entered_time="2018-07-09",
189             start_time="2018-07-09",
190             vnf_instance_id=vnf_instance_id,
191             grant_id=None,
192             operation="SCALE",
193             is_automatic_invocation=False,
194             operation_params='{}',
195             is_cancel_pending=False,
196             cancel_mode=None,
197             error=None,
198             resource_changes=None,
199             changed_ext_connectivity=None,
200             links=json.dumps({
201                 "self": {
202                     "href": "demo"
203                 },
204                 "vnfInstance": "demo"
205             })).save()
206         response = self.client.get(
207             "/api/vnflcm/v1/vnf_lcm_op_occs?exclude_default",
208             format='json'
209         )
210         self.assertEqual(
211             response.status_code,
212             status.HTTP_200_OK
213         )
214         self.assertEqual(
215             self.test_vnflcmop_with_exclude_default,
216             response.data
217         )
218
219     def test_get_vnflcmopocc_with_id(self):
220         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
221         vnf_instance_id = "cd552c9c-ab6f-11e8-b354-236c32aa91a1"
222         VNFLcmOpOccModel(
223             id=lcm_op_id,
224             operation_state="STARTING",
225             state_entered_time="2018-07-09",
226             start_time="2018-07-09",
227             vnf_instance_id=vnf_instance_id,
228             grant_id=None,
229             operation="SCALE",
230             is_automatic_invocation=False,
231             operation_params='{}',
232             is_cancel_pending=False,
233             cancel_mode=None,
234             error=None,
235             resource_changes=None,
236             changed_ext_connectivity=None,
237             links=json.dumps({
238                 "self": {
239                     "href": "demo"
240                 },
241                 "vnfInstance": "demo"
242             })).save()
243         response = self.client.get(
244             "/api/vnflcm/v1/vnf_lcm_op_occs/%s" % lcm_op_id,
245             format='json'
246         )
247         self.assertEqual(
248             response.status_code,
249             status.HTTP_200_OK
250         )
251         self.assertEqual(
252             self.test_single_vnf_lcm_op,
253             response.data
254         )
255
256     def test_single_vnflcmopocc_with_unknown_id(self):
257         lcm_op_id = "99442b18-a5c7-11e8-998c-bf1755941f16"
258         response = self.client.get(
259             "/api/vnflcm/v1/vnf_lcm_op_occs/%s" % lcm_op_id,
260             format='json'
261         )
262         self.assertEqual(
263             response.status_code,
264             status.HTTP_500_INTERNAL_SERVER_ERROR
265         )
266         expected_data = {
267             "status": 500,
268             "detail": "LCM Operation Occurance does not exist"
269         }
270         self.assertEqual(
271             expected_data,
272             response.data
273         )