Add Vnf Register code of gvnfm-mgr
[vfc/gvnfm/vnfmgr.git] / mgr / mgr / vnfreg / tests.py
1 # Copyright 2017 ZTE Corporation.
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 unittest
16 import json
17 from django.test import Client
18 from rest_framework import status
19
20 from mgr.pub.database.models import VnfRegModel
21
22 class VnfRegTest(unittest.TestCase):
23     def setUp(self):
24         self.client = Client()
25         VnfRegModel.objects.filter().delete()
26         self.vnfInst1 = {
27             "vnfInstId": "1",
28             "ip": "192.168.0.1",
29             "port": "2324",
30             "username": "admin",
31             "password": "admin123"
32         }
33         self.vnfInst1_new = {
34             "vnfInstId": "1",
35             "ip": "192.168.0.2",
36             "port": "2325",
37             "username": "admin1",
38             "password": "admin1234"
39         }
40
41     def tearDown(self):
42         pass
43
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))
49         vnfInstActual = {
50             "vnfInstId": vnfs[0].id,
51             "ip": vnfs[0].ip,
52             "port": vnfs[0].port,
53             "username": vnfs[0].username,
54             "password": vnfs[0].password
55         }
56         self.assertEqual(self.vnfInst1, vnfInstActual)
57         
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))
63         
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))
71         vnfInstActual = {
72             "vnfInstId": vnfs[0].id,
73             "ip": vnfs[0].ip,
74             "port": vnfs[0].port,
75             "username": vnfs[0].username,
76             "password": vnfs[0].password
77         }
78         self.assertEqual(self.vnfInst1_new, vnfInstActual)
79         
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))
85         
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))
91         
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))
96         
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)
101         
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))
106         
107     def test_url(self):
108         pass
109         #resp_data = json.loads(response.content)
110         #self.assertEqual({"status": "active"}, resp_data)