Add unit test for job query of nslcm
[vfc/nfvo/lcm.git] / lcm / jobs / tests / tests.py
1 # Copyright 2016 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 from django.test import TestCase
15 from rest_framework.test import APIClient
16 from rest_framework import status
17
18 from lcm.pub.database.models import JobModel, JobStatusModel
19
20
21 class JobsViewTest(TestCase):
22     def setUp(self):
23         self.job_id = 'test_job_id'
24         self.client = APIClient()
25         JobModel.objects.all().delete()
26         JobStatusModel.objects.all().delete()
27
28     def tearDown(self):
29         JobModel.objects.all().delete()
30         JobStatusModel.objects.all().delete()
31
32     def test_job(self):
33         JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
34         JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
35         response = self.client.get("/api/nslcm/v1/jobs/%s" % self.job_id)
36         self.assertEqual(status.HTTP_200_OK, response.status_code, response.data)
37         self.assertIn('jobId', response.data)
38         self.assertIn('responseDescriptor', response.data)
39         self.assertEqual(20, response.data['responseDescriptor']['progress'])
40
41     def test_non_exiting_job(self):
42         job_id = 'test_new_job_id'
43         JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
44         JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
45         response = self.client.get("/api/nslcm/v1/jobs/%s" % job_id)
46         self.assertEqual(status.HTTP_200_OK, response.status_code)
47         self.assertIn('jobId', response.data)
48         self.assertNotIn('responseDescriptor', response.data)
49
50     def test_query_job_with_response_id(self):
51         JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
52         JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
53         JobStatusModel(indexid=2, jobid=self.job_id, status='inst', progress=50, descp='inst', errcode="0").save()
54         JobStatusModel(indexid=3, jobid=self.job_id, status='inst', progress=80, descp='inst', errcode="0").save()
55         JobStatusModel(indexid=4, jobid=self.job_id, status='inst', progress=100, descp='inst', errcode="0").save()
56         response = self.client.get("/api/nslcm/v1/jobs/%s?responseId=2" % self.job_id)
57         self.assertEqual(status.HTTP_200_OK, response.status_code)
58         self.assertEqual(self.job_id, response.data.get('jobId'))
59         self.assertIn('responseDescriptor', response.data)
60         self.assertEqual(100, response.data['responseDescriptor']['progress'])
61         self.assertEqual(1, len(response.data['responseDescriptor']['responseHistoryList']))