FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DeciderAgentTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Decider;
9 
27 use Mockery as M;
28 
29 
30 global $container;
31 require_once(__DIR__ . '/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
32 require_once(__DIR__ . '/../../agent/DeciderAgent.php');
33 
38 class DeciderAgentTest extends \PHPUnit\Framework\TestCase
39 {
41  private $dbManager;
43  private $clearingDao;
45  private $clearingDecisionProcessor;
47  private $agentLicenseEventProcessor;
49  private $uploadDao;
51  private $highlightDao;
53  private $showJobsDao;
55  private $copyrightDao;
57  private $compatibilityDao;
59  private $licenseDao;
60 
65  protected function setUp() : void
66  {
67  global $container;
68  $container = M::mock('ContainerBuilder');
69  $this->dbManager = M::mock(DbManager::class);
70  $this->agentDao = M::mock(AgentDao::class);
71  $this->agentDao->shouldReceive('getCurrentAgentId')->andReturn(1234);
72  $this->highlightDao = M::mock(HighlightDao::class);
73  $this->uploadDao = M::mock(UploadDao::class);
74  $this->copyrightDao = M::mock(CopyrightDao::class);
75  $this->showJobsDao = new ShowJobsDao($this->dbManager, $this->uploadDao);
76  $this->copyrightDao = M::mock(CopyrightDao::class);
77  $this->clearingDao = M::mock(ClearingDao::class);
78  $this->compatibilityDao = M::mock(CompatibilityDao::class);
79  $this->licenseDao = M::mock(LicenseDao::class);
80  $this->clearingDecisionProcessor = M::mock(ClearingDecisionProcessor::class);
81  $this->agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
82 
83  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManager);
84  $container->shouldReceive('get')->withArgs(array('dao.agent'))->andReturn($this->agentDao);
85  $container->shouldReceive('get')->with('dao.highlight')->andReturn($this->highlightDao);
86  $container->shouldReceive('get')->with('dao.show_jobs')->andReturn($this->showJobsDao);
87  $container->shouldReceive('get')->with('dao.copyright')->andReturn($this->copyrightDao);
88  $container->shouldReceive('get')->withArgs(array('dao.upload'))->andReturn($this->uploadDao);
89  $container->shouldReceive('get')->withArgs(array('dao.copyright'))->andReturn($this->copyrightDao);
90  $container->shouldReceive('get')->withArgs(array('dao.clearing'))->andReturn($this->clearingDao);
91  $container->shouldReceive('get')->withArgs(array('dao.compatibility'))->andReturn($this->compatibilityDao);
92  $container->shouldReceive('get')->withArgs(array('dao.license'))->andReturn($this->licenseDao);
93  $container->shouldReceive('get')->withArgs(array('decision.types'))->andReturn(M::mock(DecisionTypes::class));
94  $container->shouldReceive('get')->withArgs(array('businessrules.clearing_decision_processor'))->andReturn($this->clearingDecisionProcessor);
95  $container->shouldReceive('get')->withArgs(array('businessrules.agent_license_event_processor'))->andReturn($this->agentLicenseEventProcessor);
96  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
97  }
98 
103  protected function tearDown() : void
104  {
105  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
106  M::close();
107  }
108 
116  {
117  $deciderAgent = new DeciderAgent();
118 
119  $reflection = new \ReflectionClass($deciderAgent);
120  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
121  $method->setAccessible(true);
122 
123  $licenseMatches = array();
124  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
125  }
126 
134  {
135  $deciderAgent = new DeciderAgent();
136 
137  $reflection = new \ReflectionClass($deciderAgent);
138  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
139  $method->setAccessible(true);
140 
141  $this->highlightDao->shouldReceive('getHighlightRegion')->andReturn(array($start=2, $end=5));
142  $licenseMatches = array('nomos'=>
143  array($this->createLicenseMatch('nomos',1))
144  );
145  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
146  }
147 
155  {
156  $deciderAgent = new DeciderAgent();
157 
158  $reflection = new \ReflectionClass($deciderAgent);
159  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
160  $method->setAccessible(true);
161 
162  $this->highlightDao->shouldReceive('getHighlightRegion')->andReturn(array($start=2, $end=5));
163  $licenseMatches = array('monk'=>
164  array($this->createLicenseMatch('monk',1))
165  );
166  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
167  }
168 
178  {
179  $deciderAgent = new DeciderAgent();
180 
181  $reflection = new \ReflectionClass($deciderAgent);
182  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
183  $method->setAccessible(true);
184  $monkId = 1;
185  $nomosId = 2;
186  $this->highlightDao->shouldReceive('getHighlightRegion')->with($monkId)->andReturn(array($start=2, $end=5));
187  $this->highlightDao->shouldReceive('getHighlightRegion')->with($nomosId)->andReturn(array($start=2, $end=8));
188  $licenseMatches = array('monk'=>array($this->createLicenseMatch('monk',$monkId)),
189  'nomos'=>array($this->createLicenseMatch('nomos',$nomosId)));
190  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
191  }
192 
201  {
202  $deciderAgent = new DeciderAgent();
203 
204  $reflection = new \ReflectionClass($deciderAgent);
205  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
206  $method->setAccessible(true);
207  $monkId = 1;
208  $nomosId = 2;
209  $this->highlightDao->shouldReceive('getHighlightRegion')->with($monkId)->andReturn(array($start=2, $end=5));
210  $this->highlightDao->shouldReceive('getHighlightRegion')->with($nomosId)->andReturn(array($start=4, $end=5));
211  $licenseMatches = array('monk'=>array($this->createLicenseMatch('monk',$monkId)),
212  'nomos'=>array($this->createLicenseMatch('nomos',$nomosId)));
213  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(true) );
214  }
215 
216 
224  protected function createLicenseMatch($agentName, $matchId)
225  {
226  $licenseMatch = M::mock(LicenseMatch::class);
227  $licenseMatch->shouldReceive("getLicenseFileId")->withNoArgs()->andReturn($matchId);
228  return $licenseMatch;
229  }
230 
238  {
239  $deciderAgent = new DeciderAgent();
240  $licId = 401;
241  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
242  'nomos'=>array($this->createLicenseMatchWithLicId($licId)));
243  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
244  assertThat($agree, equalTo(false) );
245  }
246 
255  {
256  $deciderAgent = new DeciderAgent();
257  $licId = 401;
258  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
259  'nomos'=>array($this->createLicenseMatchWithLicId($licId),$this->createLicenseMatchWithLicId($licId)),
260  'ninka'=>array($this->createLicenseMatchWithLicId($licId)));
261  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
262  assertThat($agree, equalTo(true) );
263  }
264 
265 
274  {
275  $deciderAgent = new DeciderAgent();
276  $licId = 401;
277  $otherLicId = 402;
278  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
279  'nomos'=>array($this->createLicenseMatchWithLicId($licId),$this->createLicenseMatchWithLicId($otherLicId)),
280  'ninka'=>array($this->createLicenseMatchWithLicId($licId)));
281  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
282  assertThat($agree, equalTo(false) );
283  }
284 
291  protected function createLicenseMatchWithLicId($licId)
292  {
293  if ($licId == 401) {
294  $licenseShortName = "LicA";
295  $licenseName = "LicenseA";
296  } else {
297  $licenseShortName = "LicB";
298  $licenseName = "LicenseB";
299  }
300  return new LicenseMatch(1,
301  new LicenseRef($licId, $licenseShortName, $licenseName, $licenseShortName),
302  M::mock(AgentRef::class),
303  1);
304  }
305 
312  {
313  $deciderAgent = new DeciderAgent();
314  $licId = 401;
315  $otherLicId = 402;
316  $licenseMatches = [
317  $licId => [
318  'monk' => [
319  $this->createLicenseMatchWithLicId($licId)
320  ],
321  'nomos' => [
322  $this->createLicenseMatchWithLicId($licId),
323  $this->createLicenseMatchWithLicId($otherLicId)
324  ],
325  'ojo' => [
326  $this->createLicenseMatchWithLicId($licId)
327  ]
328  ],
329  $otherLicId => [
330  'nomos' => [
331  $this->createLicenseMatchWithLicId($licId),
332  $this->createLicenseMatchWithLicId($otherLicId)
333  ]
334  ]
335  ];
336  $itemTreeBounds = new ItemTreeBounds(123, "uploadtree", "2", 1, 4);
337 
338  $this->compatibilityDao->shouldReceive("getCompatibilityForFile")
339  ->withArgs([
340  $itemTreeBounds,
341  $this->createLicenseMatchWithLicId($licId)->getLicenseRef()
342  ->getShortName()
343  ])
344  ->andReturn(true);
345  $this->compatibilityDao->shouldReceive("getCompatibilityForFile")
346  ->withArgs([
347  $itemTreeBounds,
348  $this->createLicenseMatchWithLicId($otherLicId)->getLicenseRef()
349  ->getShortName()
350  ])
351  ->andReturn(true);
352 
353  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent,
354  'noLicenseConflict', [$itemTreeBounds, $licenseMatches]);
355  $this->assertTrue($agree, "Wrong result for compatible licenses");
356  }
357 
364  {
365  $deciderAgent = new DeciderAgent();
366  $licId = 401;
367  $otherLicId = 402;
368  $licenseMatches = [
369  $licId => [
370  'monk' => [
371  $this->createLicenseMatchWithLicId($licId)
372  ],
373  'nomos' => [
374  $this->createLicenseMatchWithLicId($licId),
375  $this->createLicenseMatchWithLicId($otherLicId)
376  ],
377  'ojo' => [
378  $this->createLicenseMatchWithLicId($licId)
379  ]
380  ],
381  $otherLicId => [
382  'nomos' => [
383  $this->createLicenseMatchWithLicId($licId),
384  $this->createLicenseMatchWithLicId($otherLicId)
385  ]
386  ]
387  ];
388  $itemTreeBounds = new ItemTreeBounds(123, "uploadtree", "2", 1, 4);
389 
390  $this->compatibilityDao->shouldReceive("getCompatibilityForFile")
391  ->withArgs([
392  $itemTreeBounds,
393  $this->createLicenseMatchWithLicId($licId)->getLicenseRef()
394  ->getShortName()
395  ])
396  ->andReturn(true);
397  $this->compatibilityDao->shouldReceive("getCompatibilityForFile")
398  ->withArgs([
399  $itemTreeBounds,
400  $this->createLicenseMatchWithLicId($otherLicId)->getLicenseRef()
401  ->getShortName()
402  ])
403  ->andReturn(false);
404 
405  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent,
406  'noLicenseConflict', [$itemTreeBounds, $licenseMatches]);
407  $this->assertFalse($agree, "Wrong result for incompatible licenses");
408  }
409 
416  {
417  $deciderAgent = new DeciderAgent();
418  $licId = 401;
419  $otherLicId = 402;
420  $licenseMatches = [
421  $licId => [
422  'monk' => [
423  $this->createLicenseMatchWithLicId($licId)
424  ],
425  'nomos' => [
426  $this->createLicenseMatchWithLicId($licId),
427  $this->createLicenseMatchWithLicId($otherLicId)
428  ],
429  'ojo' => [
430  $this->createLicenseMatchWithLicId($licId)
431  ]
432  ],
433  $otherLicId => [
434  'nomos' => [
435  $this->createLicenseMatchWithLicId($licId),
436  $this->createLicenseMatchWithLicId($otherLicId)
437  ]
438  ]
439  ];
440 
441  $this->licenseDao->shouldReceive("getLicenseType")
442  ->withArgs([$licId])->andReturn("Permissive");
443  $this->licenseDao->shouldReceive("getLicenseType")
444  ->withArgs([$otherLicId])->andReturn("Permissive");
445 
446  $reflector = new \ReflectionProperty(DeciderAgent::class, "licenseType");
447  $reflector->setAccessible(true);
448  $reflector->setValue($deciderAgent, "Permissive");
449 
450  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent,
451  'allLicenseInType', [$licenseMatches]);
452  $this->assertTrue($agree, "Wrong result for compatible license types");
453  }
454 
461  {
462  $deciderAgent = new DeciderAgent();
463  $licId = 401;
464  $otherLicId = 402;
465  $licenseMatches = [
466  $licId => [
467  'monk' => [
468  $this->createLicenseMatchWithLicId($licId)
469  ],
470  'nomos' => [
471  $this->createLicenseMatchWithLicId($licId),
472  $this->createLicenseMatchWithLicId($otherLicId)
473  ],
474  'ojo' => [
475  $this->createLicenseMatchWithLicId($licId)
476  ]
477  ],
478  $otherLicId => [
479  'nomos' => [
480  $this->createLicenseMatchWithLicId($licId),
481  $this->createLicenseMatchWithLicId($otherLicId)
482  ]
483  ]
484  ];
485 
486  $this->licenseDao->shouldReceive("getLicenseType")
487  ->withArgs([$licId])->andReturn("Permissive");
488  $this->licenseDao->shouldReceive("getLicenseType")
489  ->withArgs([$otherLicId])->andReturn("Copyleft");
490 
491  $reflector = new \ReflectionProperty(DeciderAgent::class, "licenseType");
492  $reflector->setAccessible(true);
493  $reflector->setValue($deciderAgent, "Permissive");
494 
495  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent,
496  'allLicenseInType', [$licenseMatches]);
497  $this->assertFalse($agree, "Wrong result for non-compatible license types");
498  }
499 }
createLicenseMatchWithLicId($licId)
Create mock LicenseMatch object with getLicenseId returning $licId.
setUp()
Setup test objects, database and repo.
createLicenseMatch($agentName, $matchId)
Create mock LicenseMatch object with getLicenseFileId returning $matchId.
Agent to decide license findings in an upload.
Utility functions to process ClearingDecision.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Namespace for decider agent.
Definition: BulkReuser.php:8