rename result_collection_api to testapi
[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 test_base import TestBase
12 from opnfv_testapi.resources.pod_models import PodCreateRequest, Pod, Pods
13 from opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
14     HTTP_FORBIDDEN, HTTP_NOT_FOUND
15
16
17 class TestPodBase(TestBase):
18     def setUp(self):
19         super(TestPodBase, self).setUp()
20         self.req_d = PodCreateRequest('zte-1', 'virtual',
21                                       'zte pod 1', 'ci-pod')
22         self.req_e = PodCreateRequest('zte-2', 'metal', 'zte pod 2')
23         self.get_res = Pod
24         self.list_res = Pods
25         self.basePath = '/api/v1/pods'
26
27     def assert_get_body(self, pod, req=None):
28         if not req:
29             req = self.req_d
30         self.assertEqual(pod.name, req.name)
31         self.assertEqual(pod.mode, req.mode)
32         self.assertEqual(pod.details, req.details)
33         self.assertEqual(pod.role, req.role)
34         self.assertIsNotNone(pod.creation_date)
35         self.assertIsNotNone(pod._id)
36
37
38 class TestPodCreate(TestPodBase):
39     def test_withoutBody(self):
40         (code, body) = self.create()
41         self.assertEqual(code, HTTP_BAD_REQUEST)
42
43     def test_emptyName(self):
44         req_empty = PodCreateRequest('')
45         (code, body) = self.create(req_empty)
46         self.assertEqual(code, HTTP_BAD_REQUEST)
47         self.assertIn('name missing', body)
48
49     def test_noneName(self):
50         req_none = PodCreateRequest(None)
51         (code, body) = self.create(req_none)
52         self.assertEqual(code, HTTP_BAD_REQUEST)
53         self.assertIn('name missing', body)
54
55     def test_success(self):
56         code, body = self.create_d()
57         self.assertEqual(code, HTTP_OK)
58         self.assert_create_body(body)
59
60     def test_alreadyExist(self):
61         self.create_d()
62         code, body = self.create_d()
63         self.assertEqual(code, HTTP_FORBIDDEN)
64         self.assertIn('already exists', body)
65
66
67 class TestPodGet(TestPodBase):
68     def test_notExist(self):
69         code, body = self.get('notExist')
70         self.assertEqual(code, HTTP_NOT_FOUND)
71
72     def test_getOne(self):
73         self.create_d()
74         code, body = self.get(self.req_d.name)
75         self.assert_get_body(body)
76
77     def test_list(self):
78         self.create_d()
79         self.create_e()
80         code, body = self.get()
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()