1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from django.test import Client
18 from rest_framework import status
20 from mgr.pub.database.models import VnfRegModel
22 class VnfRegTest(unittest.TestCase):
24 self.client = Client()
25 VnfRegModel.objects.filter().delete()
31 "password": "admin123"
38 "password": "admin1234"
44 def test_add_vnf_normal(self):
45 response = self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
46 self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
47 vnfs = VnfRegModel.objects.filter()
48 self.assertEqual(1, len(vnfs))
50 "vnfInstId": vnfs[0].id,
53 "username": vnfs[0].username,
54 "password": vnfs[0].password
56 self.assertEqual(self.vnfInst1, vnfInstActual)
58 def test_add_vnf_when_duplicate(self):
59 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
60 response = self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
61 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
62 self.assertEqual({'error': "Vnf(1) already exists."}, json.loads(response.content))
64 def test_set_vnf_normal(self):
65 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
66 response = self.client.put("/openoapi/vnfmgr/v1/vnfs/1",
67 json.dumps(self.vnfInst1_new), content_type='application/json')
68 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
69 vnfs = VnfRegModel.objects.filter()
70 self.assertEqual(1, len(vnfs))
72 "vnfInstId": vnfs[0].id,
75 "username": vnfs[0].username,
76 "password": vnfs[0].password
78 self.assertEqual(self.vnfInst1_new, vnfInstActual)
80 def test_set_vnf_when_not_exist(self):
81 response = self.client.put("/openoapi/vnfmgr/v1/vnfs/1",
82 json.dumps(self.vnfInst1_new), content_type='application/json')
83 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
84 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
86 def test_get_vnf_normal(self):
87 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
88 response = self.client.get("/openoapi/vnfmgr/v1/vnfs/1")
89 self.assertEqual(status.HTTP_200_OK, response.status_code, response.content)
90 self.assertEqual(self.vnfInst1, json.loads(response.content))
92 def test_get_vnf_when_not_exist(self):
93 response = self.client.get("/openoapi/vnfmgr/v1/vnfs/1")
94 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
95 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
97 def test_del_vnf_normal(self):
98 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
99 response = self.client.delete("/openoapi/vnfmgr/v1/vnfs/1")
100 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
102 def test_del_vnf_when_not_exist(self):
103 response = self.client.delete("/openoapi/vnfmgr/v1/vnfs/1")
104 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
105 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
109 #resp_data = json.loads(response.content)
110 #self.assertEqual({"status": "active"}, resp_data)