move resources unit tests to tests/unit/resources
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / resources / 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 httplib
10 import unittest
11
12 from opnfv_testapi.common import message
13 from opnfv_testapi.resources import pod_models
14 from opnfv_testapi.tests.unit import executor
15 from opnfv_testapi.tests.unit.resources import test_base as base
16
17
18 class TestPodBase(base.TestBase):
19     def setUp(self):
20         super(TestPodBase, self).setUp()
21         self.req_d = pod_models.PodCreateRequest('zte-1', 'virtual',
22                                                  'zte pod 1', 'ci-pod')
23         self.req_e = pod_models.PodCreateRequest('zte-2', 'metal', 'zte pod 2')
24         self.get_res = pod_models.Pod
25         self.list_res = pod_models.Pods
26         self.basePath = '/api/v1/pods'
27
28     def assert_get_body(self, pod, req=None):
29         if not req:
30             req = self.req_d
31         self.assertEqual(pod.name, req.name)
32         self.assertEqual(pod.mode, req.mode)
33         self.assertEqual(pod.details, req.details)
34         self.assertEqual(pod.role, req.role)
35         self.assertIsNotNone(pod.creation_date)
36         self.assertIsNotNone(pod._id)
37
38
39 class TestPodCreate(TestPodBase):
40     @executor.create(httplib.BAD_REQUEST, message.no_body())
41     def test_withoutBody(self):
42         return None
43
44     @executor.create(httplib.BAD_REQUEST, message.missing('name'))
45     def test_emptyName(self):
46         return pod_models.PodCreateRequest('')
47
48     @executor.create(httplib.BAD_REQUEST, message.missing('name'))
49     def test_noneName(self):
50         return pod_models.PodCreateRequest(None)
51
52     @executor.create(httplib.OK, 'assert_create_body')
53     def test_success(self):
54         return self.req_d
55
56     @executor.create(httplib.FORBIDDEN, message.exist_base)
57     def test_alreadyExist(self):
58         self.create_d()
59         return self.req_d
60
61
62 class TestPodGet(TestPodBase):
63     def setUp(self):
64         super(TestPodGet, self).setUp()
65         self.create_d()
66         self.create_e()
67
68     @executor.get(httplib.NOT_FOUND, message.not_found_base)
69     def test_notExist(self):
70         return 'notExist'
71
72     @executor.get(httplib.OK, 'assert_get_body')
73     def test_getOne(self):
74         return self.req_d.name
75
76     @executor.get(httplib.OK, '_assert_list')
77     def test_list(self):
78         return None
79
80     def _assert_list(self, body):
81         self.assertEqual(len(body.pods), 2)
82         for pod in body.pods:
83             if self.req_d.name == pod.name:
84                 self.assert_get_body(pod)
85             else:
86                 self.assert_get_body(pod, self.req_e)
87
88 if __name__ == '__main__':
89     unittest.main()