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