FOSSology  4.4.0
Open Source License Compliance by Open Source Software
JobControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
14 
24 use Mockery as M;
25 use Slim\Psr7\Factory\StreamFactory;
26 use Slim\Psr7\Headers;
27 use Slim\Psr7\Request;
28 use Slim\Psr7\Uri;
29 
30 require_once dirname(__DIR__, 4) . "/lib/php/Plugin/FO_Plugin.php";
31 
36 class JobControllerTest extends \PHPUnit\Framework\TestCase
37 {
42  private $dbHelper;
43 
48  private $restHelper;
49 
54  private $jobDao;
55 
60  private $showJobsDao;
61 
66  private $jobController;
67 
73 
78  private $streamFactory;
79 
84  protected function setUp() : void
85  {
86  global $container;
87  $container = M::mock('ContainerBuilder');
88  $this->dbHelper = M::mock(DbHelper::class);
89  $this->restHelper = M::mock(RestHelper::class);
90  $this->jobDao = M::mock(JobDao::class);
91  $this->showJobsDao = M::mock(ShowJobsDao::class);
92 
93  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
94  $this->restHelper->shouldReceive('getJobDao')->andReturn($this->jobDao);
95  $this->restHelper->shouldReceive('getShowJobDao')->andReturn($this->showJobsDao);
96 
97  $container->shouldReceive('get')->withArgs(array(
98  'helper.restHelper'))->andReturn($this->restHelper);
99  $this->jobController = new JobController($container);
100  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
101  $this->streamFactory = new StreamFactory();
102  }
103 
108  protected function tearDown() : void
109  {
110  $this->addToAssertionCount(
111  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
112  M::close();
113  }
114 
115 
122  private function getResponseJson($response)
123  {
124  $response->getBody()->seek(0);
125  return json_decode($response->getBody()->getContents(), true);
126  }
127 
133  private function getUsers($userIds)
134  {
135  $userArray = array();
136  foreach ($userIds as $userId) {
137  if ($userId == 2) {
138  $accessLevel = PLUGIN_DB_ADMIN;
139  } elseif ($userId > 2 && $userId <= 4) {
140  $accessLevel = PLUGIN_DB_WRITE;
141  } elseif ($userId == 5) {
142  $accessLevel = PLUGIN_DB_READ;
143  } else {
144  continue;
145  }
146  $user = new User($userId, "user$userId", "User $userId",
147  "user$userId@example.com", $accessLevel, 2, 4, "");
148  $userArray[] = $user->getArray();
149  }
150  return $userArray;
151  }
152 
158  public function testGetJobs()
159  {
160  $job = new Job(11, "job_name", "01-01-2020", 4, 2, 2, 0, "Completed");
161  $jobQueue = new JobQueue(44, 'readmeoss', '2020-01-01 20:41:49', '2020-01-01 20:41:50',
162  'Completed', 0, null, [], 0, true, false, true,
163  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']);
164  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(11))
165  ->andReturn(['44' => 0]);
166  $this->showJobsDao->shouldReceive('getEstimatedTime')
167  ->withArgs(array(11, '', 0, 4))->andReturn("0");
168  $this->showJobsDao->shouldReceive('getDataForASingleJob')
169  ->withArgs(array(44))->andReturn(["jq_endtext"=>'Completed']);
170 
171  $requestHeaders = new Headers();
172  $body = $this->streamFactory->createStream();
173  $request = new Request("GET", new Uri("HTTP", "localhost"),
174  $requestHeaders, [], [], $body);
175  $response = new ResponseHelper();
176  $userId = 2;
177  $user = $this->getUsers([$userId]);
178  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
179  $this->dbHelper->shouldReceive('getUserJobs')->withArgs(array(null, 2, 0, 1))
180  ->andReturn([[$job], 1]);
181  $actualResponse = $this->jobController->getJobs($request, $response, []);
182  $expectedResponse = $job->getArray(ApiVersion::V1);
183  $this->assertEquals(200, $actualResponse->getStatusCode());
184  $this->assertEquals($expectedResponse,
185  $this->getResponseJson($actualResponse)[0]);
186  $this->assertEquals('1',
187  $actualResponse->getHeaderLine('X-Total-Pages'));
188  }
189 
195  public function testGetJobsLimitPage()
196  {
197  $jobTwo = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
198  $jobTwoQueue = new JobQueue(45, 'readmeoss', '2020-01-01 20:41:49', '2020-01-01 20:41:50',
199  'Completed', 0, null, [], 0, true, false, true,
200  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']);
201  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(11))
202  ->andReturn(['44' => 0]);
203  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(12))
204  ->andReturn(['45' => 0]);
205  $this->showJobsDao->shouldReceive('getEstimatedTime')
206  ->withArgs(array(M::anyOf(11, 12), '', 0, M::anyOf(4, 5)))->andReturn("0");
207  $this->showJobsDao->shouldReceive('getDataForASingleJob')
208  ->withArgs([M::anyOf(44, 45)])->andReturn(["jq_endtext"=>'Completed']);
209 
210  $requestHeaders = new Headers();
211  $requestHeaders->setHeader('limit', '1');
212  $requestHeaders->setHeader('page', '2');
213  $body = $this->streamFactory->createStream();
214  $request = new Request("GET", new Uri("HTTP", "localhost"),
215  $requestHeaders, [], [], $body);
216  $response = new ResponseHelper();
217  $userId = 2;
218  $user = $this->getUsers([$userId]);
219  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
220  $this->dbHelper->shouldReceive('getUserJobs')->withArgs(array(null, 2, 1, 2))
221  ->andReturn([[$jobTwo], 2]);
222  $actualResponse = $this->jobController->getJobs($request, $response, []);
223  $expectedResponse = $jobTwo->getArray(ApiVersion::V1);
224  $this->assertEquals(200, $actualResponse->getStatusCode());
225  $this->assertEquals($expectedResponse,
226  $this->getResponseJson($actualResponse)[0]);
227  $this->assertEquals('2',
228  $actualResponse->getHeaderLine('X-Total-Pages'));
229  }
230 
236  public function testGetInvalidJob()
237  {
238  $this->dbHelper->shouldReceive('doesIdExist')
239  ->withArgs(["job", "job_pk", 2])->andReturn(false);
240 
241  $requestHeaders = new Headers();
242  $requestHeaders->setHeader('limit', '1');
243  $requestHeaders->setHeader('page', '2');
244  $body = $this->streamFactory->createStream();
245  $request = new Request("GET", new Uri("HTTP", "localhost"),
246  $requestHeaders, [], [], $body);
247  $response = new ResponseHelper();
248  $userId = 2;
249  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
250  $this->expectException(HttpNotFoundException::class);
251 
252  $this->jobController->getJobs($request, $response, ["id" => 2]);
253  }
254 
260  public function testGetJobFromId()
261  {
262  $job = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
263  $jobTwoQueue = new JobQueue(45, 'readmeoss', '2020-01-01 20:41:49', '2020-01-01 20:41:50',
264  'Completed', 0, null, [], 0, true, false, true,
265  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']);
266  $this->dbHelper->shouldReceive('doesIdExist')
267  ->withArgs(["job", "job_pk", 12])->andReturn(true);
268  $this->dbHelper->shouldReceive('getJobs')->withArgs(array(12, 0, 1))
269  ->andReturn([[$job], 1]);
270  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(12))
271  ->andReturn(['45' => 0]);
272  $this->showJobsDao->shouldReceive('getEstimatedTime')
273  ->withArgs(array(12, '', 0, 5))->andReturn("0");
274  $this->showJobsDao->shouldReceive('getDataForASingleJob')
275  ->withArgs([45])->andReturn(["jq_endtext"=>'Completed']);
276 
277  $requestHeaders = new Headers();
278  $body = $this->streamFactory->createStream();
279  $request = new Request("GET", new Uri("HTTP", "localhost"),
280  $requestHeaders, [], [], $body);
281  $response = new ResponseHelper();
282  $userId = 2;
283  $user = $this->getUsers([$userId]);
284  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
285  $actualResponse = $this->jobController->getJobs($request, $response, [
286  "id" => 12]);
287  $expectedResponse = $job->getArray(ApiVersion::V1);
288  $this->assertEquals(200, $actualResponse->getStatusCode());
289  $this->assertEquals($expectedResponse,
290  $this->getResponseJson($actualResponse));
291  $this->assertEquals('1',
292  $actualResponse->getHeaderLine('X-Total-Pages'));
293  }
294 
300  public function testGetJobsFromUpload()
301  {
302  $job = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
303  $jobTwoQueue = new JobQueue(45, 'readmeoss', '2020-01-01 20:41:49', '2020-01-01 20:41:50',
304  'Completed', 0, null, [], 0, true, false, true,
305  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']);
306  $this->dbHelper->shouldReceive('doesIdExist')
307  ->withArgs(["upload", "upload_pk", 5])->andReturn(true);
308  $this->dbHelper->shouldReceive('doesIdExist')
309  ->withArgs(['job', 'job_pk', 12])->andReturn(true);
310  $this->dbHelper->shouldReceive('getJobs')->withArgs(array(null, 0, 1, 5))
311  ->andReturn([[$job], 1]);
312  $this->jobDao->shouldReceive('getChlidJobStatus')->withArgs(array(12))
313  ->andReturn(['45' => 0]);
314  $this->showJobsDao->shouldReceive('getEstimatedTime')
315  ->withArgs(array(12, '', 0, 5))->andReturn("0");
316  $this->showJobsDao->shouldReceive('getDataForASingleJob')
317  ->withArgs([45])->andReturn(["jq_endtext"=>'Completed']);
318 
319  $requestHeaders = new Headers();
320  $body = $this->streamFactory->createStream();
321  $request = new Request("GET", new Uri("HTTP", "localhost"),
322  $requestHeaders, [], [], $body);
323  $request = $request->withQueryParams([JobController::UPLOAD_PARAM => 5]);
324  $response = new ResponseHelper();
325  $userId = 2;
326  $user = $this->getUsers([$userId]);
327  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
328  $actualResponse = $this->jobController->getJobs($request, $response, []);
329  $expectedResponse = $job->getArray(ApiVersion::V1);
330  $this->assertEquals(200, $actualResponse->getStatusCode());
331  $this->assertEquals($expectedResponse,
332  $this->getResponseJson($actualResponse)[0]);
333  $this->assertEquals('1',
334  $actualResponse->getHeaderLine('X-Total-Pages'));
335  }
336 
343  public function testGetUploadEtaInSeconds()
344  {
345  $jobId = 11;
346  $uploadId = 5;
347  $completedJob = 5;
348  $completedUpload = 3;
349  $this->showJobsDao->shouldReceive('getEstimatedTime')
350  ->withArgs([$jobId, '', 0, $uploadId])
351  ->andReturn("3:10:23");
352  $this->showJobsDao->shouldReceive('getEstimatedTime')
353  ->withArgs([$completedJob, '', 0, $completedUpload])
354  ->andReturn("0");
355  $reflection = new \ReflectionClass(get_class($this->jobController));
356  $method = $reflection->getMethod('getUploadEtaInSeconds');
357  $method->setAccessible(true);
358 
359  $result = $method->invokeArgs($this->jobController, [$jobId, $uploadId]);
360  $this->assertEquals((3 * 3600) + (10 * 60) + 23, $result);
361 
362  $result = $method->invokeArgs($this->jobController,
363  [$completedJob, $completedUpload]);
364  $this->assertEquals(0, $result);
365  }
366 
375  public function testGetJobStatus()
376  {
377  $jobCompleted = [1, 2];
378  $jobQueued = [3, 4];
379  $jobFailed = [5, 6];
380  $this->showJobsDao->shouldReceive('getDataForASingleJob')
381  ->withArgs([M::anyof(1, 2, 5)])
382  ->andReturn(["jq_endtext" => "Completed"]);
383  $this->showJobsDao->shouldReceive('getDataForASingleJob')
384  ->withArgs([3])->andReturn(["jq_endtext" => "Started"]);
385  $this->showJobsDao->shouldReceive('getDataForASingleJob')
386  ->withArgs([4])->andReturn(["jq_endtext" => "Processing",
387  "jq_endtime" => ""]);
388  $this->showJobsDao->shouldReceive('getDataForASingleJob')
389  ->withArgs([6])->andReturn(["jq_endtext" => "Failed",
390  "jq_endtime" => "01-01-2020 00:00:00"]);
391 
392  $reflection = new \ReflectionClass(get_class($this->jobController));
393  $method = $reflection->getMethod('getJobStatus');
394  $method->setAccessible(true);
395 
396  $result = $method->invokeArgs($this->jobController, [$jobCompleted]);
397  $this->assertEquals("Completed", $result);
398 
399  $result = $method->invokeArgs($this->jobController, [$jobQueued]);
400  $this->assertEquals("Processing", $result);
401 
402  $result = $method->invokeArgs($this->jobController, [$jobFailed]);
403  $this->assertEquals("Failed", $result);
404  }
405 }
Override Slim response for withJson function.
Model class to hold JobQueue info.
Definition: JobQueue.php:18
Model to hold user information.
Definition: User.php:21
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39