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.
15 from django.test import TestCase
16 from lcm.jobs.tests import UPDATE_JOB_DICT
17 from lcm.pub.database.models import JobModel, JobStatusModel
18 from rest_framework import status
19 from rest_framework.test import APIClient
22 class JobsViewTest(TestCase):
24 self.job_id = 'test_job_id'
27 self.client = APIClient()
28 JobModel.objects.all().delete()
29 JobStatusModel.objects.all().delete()
32 JobModel.objects.all().delete()
33 JobStatusModel.objects.all().delete()
36 JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
37 JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
38 response = self.client.get("/api/nslcm/v1/jobs/%s" % self.job_id)
39 self.assertEqual(status.HTTP_200_OK, response.status_code, response.data)
40 self.assertIn('jobId', response.data)
41 self.assertIn('responseDescriptor', response.data)
42 self.assertEqual(20, response.data['responseDescriptor']['progress'])
44 def test_non_exiting_job(self):
45 job_id = 'test_new_job_id'
46 JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
47 JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
48 response = self.client.get("/api/nslcm/v1/jobs/%s" % job_id)
49 self.assertEqual(status.HTTP_200_OK, response.status_code)
50 self.assertIn('jobId', response.data)
51 self.assertNotIn('responseDescriptor', response.data)
53 def test_query_job_with_response_id(self):
54 JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
55 JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
56 JobStatusModel(indexid=2, jobid=self.job_id, status='inst', progress=50, descp='inst', errcode="0").save()
57 JobStatusModel(indexid=3, jobid=self.job_id, status='inst', progress=80, descp='inst', errcode="0").save()
58 JobStatusModel(indexid=4, jobid=self.job_id, status='inst', progress=100, descp='inst', errcode="0").save()
59 response = self.client.get("/api/nslcm/v1/jobs/%s?responseId=2" % self.job_id)
60 self.assertEqual(status.HTTP_200_OK, response.status_code)
61 self.assertEqual(self.job_id, response.data.get('jobId'))
62 self.assertIn('responseDescriptor', response.data)
63 self.assertEqual(100, response.data['responseDescriptor']['progress'])
64 self.assertEqual(1, len(response.data['responseDescriptor']['responseHistoryList']))
66 def test_up_job(self):
67 JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
68 JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst', errcode="0").save()
69 response = self.client.post("/api/nslcm/v1/jobs/%s" % self.job_id, format='json', data=UPDATE_JOB_DICT)
70 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)