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 
22 use Mockery as M;
23 use Slim\Psr7\Factory\StreamFactory;
24 use Slim\Psr7\Headers;
25 use Slim\Psr7\Request;
26 use Slim\Psr7\Uri;
27 
28 require_once dirname(__DIR__, 4) . "/lib/php/Plugin/FO_Plugin.php";
29 
34 class JobControllerTest extends \PHPUnit\Framework\TestCase
35 {
40  private $dbHelper;
41 
46  private $restHelper;
47 
52  private $jobDao;
53 
58  private $showJobsDao;
59 
64  private $jobController;
65 
71 
76  private $streamFactory;
77 
82  protected function setUp() : void
83  {
84  global $container;
85  $container = M::mock('ContainerBuilder');
86  $this->dbHelper = M::mock(DbHelper::class);
87  $this->restHelper = M::mock(RestHelper::class);
88  $this->jobDao = M::mock(JobDao::class);
89  $this->showJobsDao = M::mock(ShowJobsDao::class);
90 
91  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
92  $this->restHelper->shouldReceive('getJobDao')->andReturn($this->jobDao);
93  $this->restHelper->shouldReceive('getShowJobDao')->andReturn($this->showJobsDao);
94 
95  $container->shouldReceive('get')->withArgs(array(
96  'helper.restHelper'))->andReturn($this->restHelper);
97  $this->jobController = new JobController($container);
98  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
99  $this->streamFactory = new StreamFactory();
100  }
101 
106  protected function tearDown() : void
107  {
108  $this->addToAssertionCount(
109  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
110  M::close();
111  }
112 
113 
120  private function getResponseJson($response)
121  {
122  $response->getBody()->seek(0);
123  return json_decode($response->getBody()->getContents(), true);
124  }
125 
131  private function getUsers($userIds)
132  {
133  $userArray = array();
134  foreach ($userIds as $userId) {
135  if ($userId == 2) {
136  $accessLevel = PLUGIN_DB_ADMIN;
137  } elseif ($userId > 2 && $userId <= 4) {
138  $accessLevel = PLUGIN_DB_WRITE;
139  } elseif ($userId == 5) {
140  $accessLevel = PLUGIN_DB_READ;
141  } else {
142  continue;
143  }
144  $user = new User($userId, "user$userId", "User $userId",
145  "user$userId@example.com", $accessLevel, 2, 4, "");
146  $userArray[] = $user->getArray();
147  }
148  return $userArray;
149  }
150 
156  public function testGetJobs()
157  {
158  $job = new Job(11, "job_name", "01-01-2020", 4, 2, 2, 0, "Completed");
159  $this->jobDao->shouldReceive('getAllJobStatus')->withArgs(array(4, 2, 2))
160  ->andReturn(['11' => 0]);
161  $this->showJobsDao->shouldReceive('getEstimatedTime')
162  ->withArgs(array(11, '', 0, 4))->andReturn("0");
163  $this->showJobsDao->shouldReceive('getDataForASingleJob')
164  ->withArgs(array(11))->andReturn(["jq_endtext"=>'Completed']);
165 
166  $requestHeaders = new Headers();
167  $body = $this->streamFactory->createStream();
168  $request = new Request("GET", new Uri("HTTP", "localhost"),
169  $requestHeaders, [], [], $body);
170  $response = new ResponseHelper();
171  $userId = 2;
172  $user = $this->getUsers([$userId]);
173  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
174  $this->dbHelper->shouldReceive('getUserJobs')->withArgs(array(null, 2, 0, 1))
175  ->andReturn([[$job], 1]);
176  $actualResponse = $this->jobController->getJobs($request, $response, []);
177  $expectedResponse = $job->getArray();
178  $this->assertEquals(200, $actualResponse->getStatusCode());
179  $this->assertEquals($expectedResponse,
180  $this->getResponseJson($actualResponse)[0]);
181  $this->assertEquals('1',
182  $actualResponse->getHeaderLine('X-Total-Pages'));
183  }
184 
190  public function testGetJobsLimitPage()
191  {
192  $jobTwo = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
193  $this->jobDao->shouldReceive('getAllJobStatus')->withArgs(array(4, 2, 2))
194  ->andReturn(['11' => 0]);
195  $this->jobDao->shouldReceive('getAllJobStatus')->withArgs(array(5, 2, 2))
196  ->andReturn(['12' => 0]);
197  $this->showJobsDao->shouldReceive('getEstimatedTime')
198  ->withArgs(array(M::anyOf(11, 12), '', 0, M::anyOf(4, 5)))->andReturn("0");
199  $this->showJobsDao->shouldReceive('getDataForASingleJob')
200  ->withArgs([M::anyOf(11, 12)])->andReturn(["jq_endtext"=>'Completed']);
201 
202  $requestHeaders = new Headers();
203  $requestHeaders->setHeader('limit', '1');
204  $requestHeaders->setHeader('page', '2');
205  $body = $this->streamFactory->createStream();
206  $request = new Request("GET", new Uri("HTTP", "localhost"),
207  $requestHeaders, [], [], $body);
208  $response = new ResponseHelper();
209  $userId = 2;
210  $user = $this->getUsers([$userId]);
211  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
212  $this->dbHelper->shouldReceive('getUserJobs')->withArgs(array(null, 2, 1, 2))
213  ->andReturn([[$jobTwo], 2]);
214  $actualResponse = $this->jobController->getJobs($request, $response, []);
215  $expectedResponse = $jobTwo->getArray();
216  $this->assertEquals(200, $actualResponse->getStatusCode());
217  $this->assertEquals($expectedResponse,
218  $this->getResponseJson($actualResponse)[0]);
219  $this->assertEquals('2',
220  $actualResponse->getHeaderLine('X-Total-Pages'));
221  }
222 
228  public function testGetInvalidJob()
229  {
230  $this->dbHelper->shouldReceive('doesIdExist')
231  ->withArgs(["job", "job_pk", 2])->andReturn(false);
232 
233  $requestHeaders = new Headers();
234  $requestHeaders->setHeader('limit', '1');
235  $requestHeaders->setHeader('page', '2');
236  $body = $this->streamFactory->createStream();
237  $request = new Request("GET", new Uri("HTTP", "localhost"),
238  $requestHeaders, [], [], $body);
239  $response = new ResponseHelper();
240  $userId = 2;
241  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
242  $this->expectException(HttpNotFoundException::class);
243 
244  $this->jobController->getJobs($request, $response, ["id" => 2]);
245  }
246 
252  public function testGetJobFromId()
253  {
254  $job = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
255  $this->dbHelper->shouldReceive('doesIdExist')
256  ->withArgs(["job", "job_pk", 12])->andReturn(true);
257  $this->dbHelper->shouldReceive('getJobs')->withArgs(array(12, 0, 1))
258  ->andReturn([[$job], 1]);
259  $this->jobDao->shouldReceive('getAllJobStatus')->withArgs(array(5, 2, 2))
260  ->andReturn(['12' => 0]);
261  $this->showJobsDao->shouldReceive('getEstimatedTime')
262  ->withArgs(array(12, '', 0, 5))->andReturn("0");
263  $this->showJobsDao->shouldReceive('getDataForASingleJob')
264  ->withArgs([12])->andReturn(["jq_endtext"=>'Completed']);
265 
266  $requestHeaders = new Headers();
267  $body = $this->streamFactory->createStream();
268  $request = new Request("GET", new Uri("HTTP", "localhost"),
269  $requestHeaders, [], [], $body);
270  $response = new ResponseHelper();
271  $userId = 2;
272  $user = $this->getUsers([$userId]);
273  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
274  $actualResponse = $this->jobController->getJobs($request, $response, [
275  "id" => 12]);
276  $expectedResponse = $job->getArray();
277  $this->assertEquals(200, $actualResponse->getStatusCode());
278  $this->assertEquals($expectedResponse,
279  $this->getResponseJson($actualResponse));
280  $this->assertEquals('1',
281  $actualResponse->getHeaderLine('X-Total-Pages'));
282  }
283 
289  public function testGetJobsFromUpload()
290  {
291  $job = new Job(12, "job_two", "01-01-2020", 5, 2, 2, 0, "Completed");
292  $this->dbHelper->shouldReceive('doesIdExist')
293  ->withArgs(["upload", "upload_pk", 5])->andReturn(true);
294  $this->dbHelper->shouldReceive('doesIdExist')
295  ->withArgs(['job', 'job_pk', 12])->andReturn(true);
296  $this->dbHelper->shouldReceive('getJobs')->withArgs(array(null, 0, 1, 5))
297  ->andReturn([[$job], 1]);
298  $this->jobDao->shouldReceive('getAllJobStatus')->withArgs(array(5, 2, 2))
299  ->andReturn(['12' => 0]);
300  $this->showJobsDao->shouldReceive('getEstimatedTime')
301  ->withArgs(array(12, '', 0, 5))->andReturn("0");
302  $this->showJobsDao->shouldReceive('getDataForASingleJob')
303  ->withArgs([12])->andReturn(["jq_endtext"=>'Completed']);
304 
305  $requestHeaders = new Headers();
306  $body = $this->streamFactory->createStream();
307  $request = new Request("GET", new Uri("HTTP", "localhost"),
308  $requestHeaders, [], [], $body);
309  $request = $request->withQueryParams([JobController::UPLOAD_PARAM => 5]);
310  $response = new ResponseHelper();
311  $userId = 2;
312  $user = $this->getUsers([$userId]);
313  $this->restHelper->shouldReceive('getUserId')->andReturn($userId);
314  $actualResponse = $this->jobController->getJobs($request, $response, []);
315  $expectedResponse = $job->getArray();
316  $this->assertEquals(200, $actualResponse->getStatusCode());
317  $this->assertEquals($expectedResponse,
318  $this->getResponseJson($actualResponse)[0]);
319  $this->assertEquals('1',
320  $actualResponse->getHeaderLine('X-Total-Pages'));
321  }
322 
329  public function testGetUploadEtaInSeconds()
330  {
331  $jobId = 11;
332  $uploadId = 5;
333  $completedJob = 5;
334  $completedUpload = 3;
335  $this->showJobsDao->shouldReceive('getEstimatedTime')
336  ->withArgs([$jobId, '', 0, $uploadId])
337  ->andReturn("3:10:23");
338  $this->showJobsDao->shouldReceive('getEstimatedTime')
339  ->withArgs([$completedJob, '', 0, $completedUpload])
340  ->andReturn("0");
341  $reflection = new \ReflectionClass(get_class($this->jobController));
342  $method = $reflection->getMethod('getUploadEtaInSeconds');
343  $method->setAccessible(true);
344 
345  $result = $method->invokeArgs($this->jobController, [$jobId, $uploadId]);
346  $this->assertEquals((3 * 3600) + (10 * 60) + 23, $result);
347 
348  $result = $method->invokeArgs($this->jobController,
349  [$completedJob, $completedUpload]);
350  $this->assertEquals(0, $result);
351  }
352 
361  public function testGetJobStatus()
362  {
363  $jobCompleted = [1, 2];
364  $jobQueued = [3, 4];
365  $jobFailed = [5, 6];
366  $this->showJobsDao->shouldReceive('getDataForASingleJob')
367  ->withArgs([M::anyof(1, 2, 5)])
368  ->andReturn(["jq_endtext" => "Completed"]);
369  $this->showJobsDao->shouldReceive('getDataForASingleJob')
370  ->withArgs([3])->andReturn(["jq_endtext" => "Started"]);
371  $this->showJobsDao->shouldReceive('getDataForASingleJob')
372  ->withArgs([4])->andReturn(["jq_endtext" => "Processing",
373  "jq_endtime" => ""]);
374  $this->showJobsDao->shouldReceive('getDataForASingleJob')
375  ->withArgs([6])->andReturn(["jq_endtext" => "Failed",
376  "jq_endtime" => "01-01-2020 00:00:00"]);
377 
378  $reflection = new \ReflectionClass(get_class($this->jobController));
379  $method = $reflection->getMethod('getJobStatus');
380  $method->setAccessible(true);
381 
382  $result = $method->invokeArgs($this->jobController, [$jobCompleted]);
383  $this->assertEquals("Completed", $result);
384 
385  $result = $method->invokeArgs($this->jobController, [$jobQueued]);
386  $this->assertEquals("Processing", $result);
387 
388  $result = $method->invokeArgs($this->jobController, [$jobFailed]);
389  $this->assertEquals("Failed", $result);
390  }
391 }
Override Slim response for withJson function.
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