FOSSology  4.7.1
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 {
40  public $agentDao;
41  public $assertCountBefore;
42 
44  private $dbManager;
46  private $clearingDao;
48  private $clearingDecisionProcessor;
50  private $agentLicenseEventProcessor;
52  private $uploadDao;
54  private $highlightDao;
56  private $showJobsDao;
58  private $copyrightDao;
60  private $compatibilityDao;
62  private $licenseDao;
63 
68  protected function setUp() : void
69  {
70  global $container;
71  $container = M::mock('ContainerBuilder');
72  $this->dbManager = M::mock(DbManager::class);
73  $this->agentDao = M::mock(AgentDao::class);
74  $this->agentDao->shouldReceive('getCurrentAgentId')->andReturn(1234);
75  $this->highlightDao = M::mock(HighlightDao::class);
76  $this->uploadDao = M::mock(UploadDao::class);
77  $this->copyrightDao = M::mock(CopyrightDao::class);
78  $this->showJobsDao = new ShowJobsDao($this->dbManager, $this->uploadDao);
79  $this->copyrightDao = M::mock(CopyrightDao::class);
80  $this->clearingDao = M::mock(ClearingDao::class);
81  $this->compatibilityDao = M::mock(CompatibilityDao::class);
82  $this->licenseDao = M::mock(LicenseDao::class);
83  $this->clearingDecisionProcessor = M::mock(ClearingDecisionProcessor::class);
84  $this->agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
85 
86  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManager);
87  $container->shouldReceive('get')->withArgs(array('dao.agent'))->andReturn($this->agentDao);
88  $container->shouldReceive('get')->with('dao.highlight')->andReturn($this->highlightDao);
89  $container->shouldReceive('get')->with('dao.show_jobs')->andReturn($this->showJobsDao);
90  $container->shouldReceive('get')->with('dao.copyright')->andReturn($this->copyrightDao);
91  $container->shouldReceive('get')->withArgs(array('dao.upload'))->andReturn($this->uploadDao);
92  $container->shouldReceive('get')->withArgs(array('dao.copyright'))->andReturn($this->copyrightDao);
93  $container->shouldReceive('get')->withArgs(array('dao.clearing'))->andReturn($this->clearingDao);
94  $container->shouldReceive('get')->withArgs(array('dao.compatibility'))->andReturn($this->compatibilityDao);
95  $container->shouldReceive('get')->withArgs(array('dao.license'))->andReturn($this->licenseDao);
96  $container->shouldReceive('get')->withArgs(array('decision.types'))->andReturn(M::mock(DecisionTypes::class));
97  $container->shouldReceive('get')->withArgs(array('businessrules.clearing_decision_processor'))->andReturn($this->clearingDecisionProcessor);
98  $container->shouldReceive('get')->withArgs(array('businessrules.agent_license_event_processor'))->andReturn($this->agentLicenseEventProcessor);
99  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
100  }
101 
106  protected function tearDown() : void
107  {
108  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
109  M::close();
110  }
111 
119  {
120  $deciderAgent = new DeciderAgent();
121 
122  $reflection = new \ReflectionClass($deciderAgent);
123  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
124  $method->setAccessible(true);
125 
126  $licenseMatches = array();
127  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
128  }
129 
137  {
138  $deciderAgent = new DeciderAgent();
139 
140  $reflection = new \ReflectionClass($deciderAgent);
141  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
142  $method->setAccessible(true);
143 
144  $this->highlightDao->shouldReceive('getHighlightRegion')->andReturn(array($start=2, $end=5));
145  $licenseMatches = array('nomos'=>
146  array($this->createLicenseMatch('nomos',1))
147  );
148  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
149  }
150 
158  {
159  $deciderAgent = new DeciderAgent();
160 
161  $reflection = new \ReflectionClass($deciderAgent);
162  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
163  $method->setAccessible(true);
164 
165  $this->highlightDao->shouldReceive('getHighlightRegion')->andReturn(array($start=2, $end=5));
166  $licenseMatches = array('monk'=>
167  array($this->createLicenseMatch('monk',1))
168  );
169  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
170  }
171 
181  {
182  $deciderAgent = new DeciderAgent();
183 
184  $reflection = new \ReflectionClass($deciderAgent);
185  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
186  $method->setAccessible(true);
187  $monkId = 1;
188  $nomosId = 2;
189  $this->highlightDao->shouldReceive('getHighlightRegion')->with($monkId)->andReturn(array($start=2, $end=5));
190  $this->highlightDao->shouldReceive('getHighlightRegion')->with($nomosId)->andReturn(array($start=2, $end=8));
191  $licenseMatches = array('monk'=>array($this->createLicenseMatch('monk',$monkId)),
192  'nomos'=>array($this->createLicenseMatch('nomos',$nomosId)));
193  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(false) );
194  }
195 
204  {
205  $deciderAgent = new DeciderAgent();
206 
207  $reflection = new \ReflectionClass($deciderAgent);
208  $method = $reflection->getMethod('areNomosMatchesInsideAMonkMatch');
209  $method->setAccessible(true);
210  $monkId = 1;
211  $nomosId = 2;
212  $this->highlightDao->shouldReceive('getHighlightRegion')->with($monkId)->andReturn(array($start=2, $end=5));
213  $this->highlightDao->shouldReceive('getHighlightRegion')->with($nomosId)->andReturn(array($start=4, $end=5));
214  $licenseMatches = array('monk'=>array($this->createLicenseMatch('monk',$monkId)),
215  'nomos'=>array($this->createLicenseMatch('nomos',$nomosId)));
216  assertThat( $method->invoke($deciderAgent,$licenseMatches), equalTo(true) );
217  }
218 
219 
227  protected function createLicenseMatch($agentName, $matchId)
228  {
229  $licenseMatch = M::mock(LicenseMatch::class);
230  $licenseMatch->shouldReceive("getLicenseFileId")->withNoArgs()->andReturn($matchId);
231  return $licenseMatch;
232  }
233 
241  {
242  $deciderAgent = new DeciderAgent();
243  $licId = 401;
244  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
245  'nomos'=>array($this->createLicenseMatchWithLicId($licId)));
246  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
247  assertThat($agree, equalTo(false) );
248  }
249 
258  {
259  $deciderAgent = new DeciderAgent();
260  $licId = 401;
261  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
262  'nomos'=>array($this->createLicenseMatchWithLicId($licId),$this->createLicenseMatchWithLicId($licId)),
263  'ninka'=>array($this->createLicenseMatchWithLicId($licId)));
264  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
265  assertThat($agree, equalTo(true) );
266  }
267 
268 
277  {
278  $deciderAgent = new DeciderAgent();
279  $licId = 401;
280  $otherLicId = 402;
281  $licenseMatches = array('monk'=>array($this->createLicenseMatchWithLicId($licId)),
282  'nomos'=>array($this->createLicenseMatchWithLicId($licId),$this->createLicenseMatchWithLicId($otherLicId)),
283  'ninka'=>array($this->createLicenseMatchWithLicId($licId)));
284  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent, 'areNomosMonkNinkaAgreed', array($licenseMatches));
285  assertThat($agree, equalTo(false) );
286  }
287 
294  protected function createLicenseMatchWithLicId($licId)
295  {
296  if ($licId == 401) {
297  $licenseShortName = "LicA";
298  $licenseName = "LicenseA";
299  } else {
300  $licenseShortName = "LicB";
301  $licenseName = "LicenseB";
302  }
303  return new LicenseMatch(1,
304  new LicenseRef($licId, $licenseShortName, $licenseName, $licenseShortName),
305  M::mock(AgentRef::class),
306  1);
307  }
308 
315  {
316  $deciderAgent = new DeciderAgent();
317  $licId = 401;
318  $otherLicId = 402;
319  $licenseMatches = [
320  $licId => [
321  'monk' => [
322  $this->createLicenseMatchWithLicId($licId)
323  ],
324  'nomos' => [
325  $this->createLicenseMatchWithLicId($licId),
326  $this->createLicenseMatchWithLicId($otherLicId)
327  ],
328  'ojo' => [
329  $this->createLicenseMatchWithLicId($licId)
330  ]
331  ],
332  $otherLicId => [
333  'nomos' => [
334  $this->createLicenseMatchWithLicId($licId),
335  $this->createLicenseMatchWithLicId($otherLicId)
336  ]
337  ]
338  ];
339  $itemTreeBounds = new ItemTreeBounds(123, "uploadtree", "2", 1, 4);
340 
341  $this->compatibilityDao->shouldReceive("getCompatibilityForFile")
342  ->withArgs([
343  $itemTreeBounds,
344  $this->createLicenseMatchWithLicId($licId)->getLicenseRef()
345  ->getShortName()
346  ])
347  ->andReturn(true);
348  $this->compatibilityDao->shouldReceive("getCompatibilityForFile")
349  ->withArgs([
350  $itemTreeBounds,
351  $this->createLicenseMatchWithLicId($otherLicId)->getLicenseRef()
352  ->getShortName()
353  ])
354  ->andReturn(true);
355 
356  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent,
357  'noLicenseConflict', [$itemTreeBounds, $licenseMatches]);
358  $this->assertTrue($agree, "Wrong result for compatible licenses");
359  }
360 
367  {
368  $deciderAgent = new DeciderAgent();
369  $licId = 401;
370  $otherLicId = 402;
371  $licenseMatches = [
372  $licId => [
373  'monk' => [
374  $this->createLicenseMatchWithLicId($licId)
375  ],
376  'nomos' => [
377  $this->createLicenseMatchWithLicId($licId),
378  $this->createLicenseMatchWithLicId($otherLicId)
379  ],
380  'ojo' => [
381  $this->createLicenseMatchWithLicId($licId)
382  ]
383  ],
384  $otherLicId => [
385  'nomos' => [
386  $this->createLicenseMatchWithLicId($licId),
387  $this->createLicenseMatchWithLicId($otherLicId)
388  ]
389  ]
390  ];
391  $itemTreeBounds = new ItemTreeBounds(123, "uploadtree", "2", 1, 4);
392 
393  $this->compatibilityDao->shouldReceive("getCompatibilityForFile")
394  ->withArgs([
395  $itemTreeBounds,
396  $this->createLicenseMatchWithLicId($licId)->getLicenseRef()
397  ->getShortName()
398  ])
399  ->andReturn(true);
400  $this->compatibilityDao->shouldReceive("getCompatibilityForFile")
401  ->withArgs([
402  $itemTreeBounds,
403  $this->createLicenseMatchWithLicId($otherLicId)->getLicenseRef()
404  ->getShortName()
405  ])
406  ->andReturn(false);
407 
408  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent,
409  'noLicenseConflict', [$itemTreeBounds, $licenseMatches]);
410  $this->assertFalse($agree, "Wrong result for incompatible licenses");
411  }
412 
419  {
420  $deciderAgent = new DeciderAgent();
421  $licId = 401;
422  $otherLicId = 402;
423  $licenseMatches = [
424  $licId => [
425  'monk' => [
426  $this->createLicenseMatchWithLicId($licId)
427  ],
428  'nomos' => [
429  $this->createLicenseMatchWithLicId($licId),
430  $this->createLicenseMatchWithLicId($otherLicId)
431  ],
432  'ojo' => [
433  $this->createLicenseMatchWithLicId($licId)
434  ]
435  ],
436  $otherLicId => [
437  'nomos' => [
438  $this->createLicenseMatchWithLicId($licId),
439  $this->createLicenseMatchWithLicId($otherLicId)
440  ]
441  ]
442  ];
443 
444  $this->licenseDao->shouldReceive("getLicenseType")
445  ->withArgs([$licId])->andReturn("Permissive");
446  $this->licenseDao->shouldReceive("getLicenseType")
447  ->withArgs([$otherLicId])->andReturn("Permissive");
448 
449  $reflector = new \ReflectionProperty(DeciderAgent::class, "licenseType");
450  $reflector->setAccessible(true);
451  $reflector->setValue($deciderAgent, "Permissive");
452 
453  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent,
454  'allLicenseInType', [$licenseMatches]);
455  $this->assertTrue($agree, "Wrong result for compatible license types");
456  }
457 
464  {
465  $deciderAgent = new DeciderAgent();
466  $licId = 401;
467  $otherLicId = 402;
468  $licenseMatches = [
469  $licId => [
470  'monk' => [
471  $this->createLicenseMatchWithLicId($licId)
472  ],
473  'nomos' => [
474  $this->createLicenseMatchWithLicId($licId),
475  $this->createLicenseMatchWithLicId($otherLicId)
476  ],
477  'ojo' => [
478  $this->createLicenseMatchWithLicId($licId)
479  ]
480  ],
481  $otherLicId => [
482  'nomos' => [
483  $this->createLicenseMatchWithLicId($licId),
484  $this->createLicenseMatchWithLicId($otherLicId)
485  ]
486  ]
487  ];
488 
489  $this->licenseDao->shouldReceive("getLicenseType")
490  ->withArgs([$licId])->andReturn("Permissive");
491  $this->licenseDao->shouldReceive("getLicenseType")
492  ->withArgs([$otherLicId])->andReturn("Copyleft");
493 
494  $reflector = new \ReflectionProperty(DeciderAgent::class, "licenseType");
495  $reflector->setAccessible(true);
496  $reflector->setValue($deciderAgent, "Permissive");
497 
498  $agree = Reflectory::invokeObjectsMethodnameWith($deciderAgent,
499  'allLicenseInType', [$licenseMatches]);
500  $this->assertFalse($agree, "Wrong result for non-compatible license types");
501  }
502 }
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: decider.php:13