1 # Copyright 2016 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
18 from lcm.pub.database.models import JobModel, JobStatusModel
19 from lcm.jobs.tests import UPDATE_JOB_DICT
22 class JobsViewTest(TestCase):
24 self.job_id = 'test_job_id'
25 self.client = APIClient()
26 JobModel.objects.all().delete()
27 JobStatusModel.objects.all().delete()
30 JobModel.objects.all().delete()
31 JobStatusModel.objects.all().delete()
34 JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
35 JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
36 response = self.client.get("/api/nslcm/v1/jobs/%s" % self.job_id)
37 self.assertEqual(status.HTTP_200_OK, response.status_code, response.data)
38 self.assertIn('jobId', response.data)
39 self.assertIn('responseDescriptor', response.data)
40 self.assertEqual(20, response.data['responseDescriptor']['progress'])
42 def test_non_exiting_job(self):
43 job_id = 'test_new_job_id'
44 JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
45 JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
46 response = self.client.get("/api/nslcm/v1/jobs/%s" % job_id)
47 self.assertEqual(status.HTTP_200_OK, response.status_code)
48 self.assertIn('jobId', response.data)
49 self.assertNotIn('responseDescriptor', response.data)
51 def test_query_job_with_response_id(self):
52 JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
53 JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
54 JobStatusModel(indexid=2, jobid=self.job_id, status='inst', progress=50, descp='inst', errcode="0").save()
55 JobStatusModel(indexid=3, jobid=self.job_id, status='inst', progress=80, descp='inst', errcode="0").save()
56 JobStatusModel(indexid=4, jobid=self.job_id, status='inst', progress=100, descp='inst', errcode="0").save()
57 response = self.client.get("/api/nslcm/v1/jobs/%s?responseId=2" % self.job_id)
58 self.assertEqual(status.HTTP_200_OK, response.status_code)
59 self.assertEqual(self.job_id, response.data.get('jobId'))
60 self.assertIn('responseDescriptor', response.data)
61 self.assertEqual(100, response.data['responseDescriptor']['progress'])
62 self.assertEqual(1, len(response.data['responseDescriptor']['responseHistoryList']))
64 def test_up_job(self):
65 JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
66 JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
67 response = self.client.post("/api/nslcm/v1/jobs/%s" % self.job_id, format='json', data=UPDATE_JOB_DICT)
68 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)