922bd46e2cb1e5cab05b98912f177868e8051ae1
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / test_pod.py
1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation
3 # feng.xiaowei@zte.com.cn
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import unittest
10
11 from opnfv_testapi.common import constants
12 from opnfv_testapi.resources import pod_models
13 import test_base as base
14
15
16 class TestPodBase(base.TestBase):
17     def setUp(self):
18         super(TestPodBase, self).setUp()
19         self.req_d = pod_models.PodCreateRequest('zte-1', 'virtual',
20                                                  'zte pod 1', 'ci-pod')
21         self.req_e = pod_models.PodCreateRequest('zte-2', 'metal', 'zte pod 2')
22         self.get_res = pod_models.Pod
23         self.list_res = pod_models.Pods
24         self.basePath = '/api/v1/pods'
25
26     def assert_get_body(self, pod, req=None):
27         if not req:
28             req = self.req_d
29         self.assertEqual(pod.name, req.name)
30         self.assertEqual(pod.mode, req.mode)
31         self.assertEqual(pod.details, req.details)
32         self.assertEqual(pod.role, req.role)
33         self.assertIsNotNone(pod.creation_date)
34         self.assertIsNotNone(pod._id)
35
36
37 class TestPodCreate(TestPodBase):
38     def test_withoutBody(self):
39         (code, body) = self.create()
40         self.assertEqual(code, constants.HTTP_BAD_REQUEST)
41
42     def test_emptyName(self):
43         req_empty = pod_models.PodCreateRequest('')
44         (code, body) = self.create(req_empty)
45         self.assertEqual(code, constants.HTTP_BAD_REQUEST)
46         self.assertIn('name missing', body)
47
48     def test_noneName(self):
49         req_none = pod_models.PodCreateRequest(None)
50         (code, body) = self.create(req_none)
51         self.assertEqual(code, constants.HTTP_BAD_REQUEST)
52         self.assertIn('name missing', body)
53
54     def test_success(self):
55         code, body = self.create_d()
56         self.assertEqual(code, constants.HTTP_OK)
57         self.assert_create_body(body)
58
59     def test_alreadyExist(self):
60         self.create_d()
61         code, body = self.create_d()
62         self.assertEqual(code, constants.HTTP_FORBIDDEN)
63         self.assertIn('already exists', body)
64
65
66 class TestPodGet(TestPodBase):
67     def test_notExist(self):
68         code, body = self.get('notExist')
69         self.assertEqual(code, constants.HTTP_NOT_FOUND)
70
71     def test_getOne(self):
72         self.create_d()
73         code, body = self.get(self.req_d.name)
74         self.assert_get_body(body)
75
76     def test_list(self):
77         self.create_d()
78         self.create_e()
79         code, body = self.get()
80         self.assertEqual(len(body.pods), 2)
81         for pod in body.pods:
82             if self.req_d.name == pod.name:
83                 self.assert_get_body(pod)
84             else:
85                 self.assert_get_body(pod, self.req_e)
86
87 if __name__ == '__main__':
88     unittest.main()