FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ClearingDecisionProcessorTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
20 
34 use Mockery as M;
35 
36 function ReportCachePurgeAll()
37 {
38 
39 }
40 
41 class ClearingDecisionProcessorTest extends \PHPUnit\Framework\TestCase
42 {
43  const MATCH_ID = 231;
44  const PERCENTAGE = 315;
46  private $uploadTreeId;
48  private $userId;
50  private $groupId;
52  private $agentLicenseEventProcessor;
54  private $dbManager;
56  private $clearingDao;
58  private $itemTreeBounds;
60  private $clearingDecisionProcessor;
62  private $clearingEventProcessor;
64  private $timestamp;
66  private $includeSubFolders;
67 
68  protected function setUp()
69  {
70  $this->uploadTreeId = 432;
71  $this->pfileId = 32;
72  $this->userId = 12;
73  $this->groupId = 5;
74  $this->timestamp = time();
75  $this->includeSubFolders = false;
76 
77  $this->clearingDao = M::mock(ClearingDao::class);
78  $this->agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
79  $this->clearingEventProcessor = new ClearingEventProcessor();
80 
81  $this->itemTreeBounds = M::mock(ItemTreeBounds::class);
82  $this->itemTreeBounds->shouldReceive("getItemId")->withNoArgs()->andReturn($this->uploadTreeId);
83  $this->itemTreeBounds->shouldReceive("getPfileId")->withNoArgs()->andReturn($this->pfileId);
84 
85  $this->dbManager = M::mock(DbManager::class);
86  $this->dbManager->shouldReceive('begin')->withNoArgs();
87  $this->dbManager->shouldReceive('commit')->withNoArgs();
88 
89  $this->clearingDecisionProcessor = new ClearingDecisionProcessor(
90  $this->clearingDao, $this->agentLicenseEventProcessor, $this->clearingEventProcessor,
91  $this->dbManager);
92  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
93  }
94 
95  protected function tearDown()
96  {
97  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
98  M::close();
99  }
100 
101  public function testMakeDecisionFromLastEvents()
102  {
103  $isGlobal = DecisionScopes::REPO;
104  $addedEvent = $this->createClearingEvent(123, $this->timestamp, 13, "licA", "License A");
105 
106  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
107  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
108  ->andReturn(array($addedEvent));
109  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
110  ->with($this->itemTreeBounds)
111  ->andReturn(array());
112 
113  $clearingDecision = M::mock(ClearingDecision::class);
114  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
115  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IDENTIFIED);
116  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
117 
118  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
119  ->with($this->itemTreeBounds, $this->groupId)
120  ->andReturn($clearingDecision);
121 
122  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()
123  ->with($this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal, is(arrayContainingInAnyOrder(123)));
124 
125  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal);
126  }
127 
128  public function testMakeDecisionFromLastEventsWithNoLicenseKnownTypeShouldNotCreateANewDecisionWhenNoLicensesShouldBeRemoved()
129  {
130  $isGlobal = DecisionScopes::REPO;
131  $addedEvent = $this->createClearingEvent(123, $this->timestamp, 13, "licA", "License A");
132 
133  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
134  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
135  ->andReturn(array($addedEvent));
136  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
137  ->with($this->itemTreeBounds)->andReturn(array());
138 
139  $clearingDecision = M::mock(ClearingDecision::class);
140  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
141  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IDENTIFIED);
142  $clearingDecision->shouldReceive("getScope")->withNoArgs()->andReturn(DecisionScopes::ITEM);
143  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
144 
145  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
146  ->with($this->itemTreeBounds, $this->groupId)
147  ->andReturn($clearingDecision);
148  $eventId = 65;
149  $this->clearingDao->shouldReceive("insertClearingEvent")->with($this->itemTreeBounds->getItemId(), $this->userId, $this->groupId, $addedEvent->getLicenseId(), true)->andReturn($eventId);
150 
151  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with($this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal, array($eventId));
152  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
153 
154  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
155  }
156 
157  public function testMakeDecisionFromLastEventsWithNoLicenseKnownTypeShouldNotCreateANewDecisionWhenNoLicensesShouldBeRemovedAndTheScopeDoesNotChange()
158  {
159  $isGlobal = DecisionScopes::REPO;
160  $addedEvent = $this->createClearingEvent(123, $this->timestamp, 13, "licA", "License A");
161 
162  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
163  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
164  ->andReturn(array($addedEvent));
165  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
166  ->with($this->itemTreeBounds)->andReturn(array());
167 
168  $clearingDecision = M::mock(ClearingDecision::class);
169  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
170  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IDENTIFIED);
171  $clearingDecision->shouldReceive("getScope")->withNoArgs()->andReturn($isGlobal);
172  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
173 
174  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
175  ->with($this->itemTreeBounds, $this->groupId)
176  ->andReturn($clearingDecision);
177 
178  $eventId = 65;
179  $this->clearingDao->shouldReceive("insertClearingEvent")->with($this->itemTreeBounds->getItemId(), $this->userId, $this->groupId, $addedEvent->getLicenseId(), true)->andReturn($eventId);
180  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with($this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal, array($eventId));
181  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
182 
183  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
184  }
185 
186  public function testMakeDecisionFromLastEventsWithNoLicenseKnownType()
187  {
188  $isGlobal = true;
189 
190  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
191  ->with($this->itemTreeBounds)->andReturn(array());
192 
193  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
194  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
195  ->andReturn(array());
196 
197  $clearingDecision = M::mock(ClearingDecision::class);
198  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
199  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IRRELEVANT);
200  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
201 
202  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
203  ->with($this->itemTreeBounds, $this->groupId)
204  ->andReturn($clearingDecision);
205 
206  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with( $this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::REPO, array());
207  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
208 
209  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
210  }
211 
212  public function testMakeDecisionFromLastEventsWithNoLicenseKnownTypeAndAnExistingAddingUserEvent()
213  {
215  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
216  $addedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName(), ClearingEventTypes::USER, $isRemoved = false);
217 
218  $isGlobal = true;
219 
220  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
221  ->with($this->itemTreeBounds)->andReturn(array());
222 
223  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
224  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
225  ->andReturn(array($licenseRef->getId() => $addedEvent));
226 
227  $clearingDecision = M::mock(ClearingDecision::class);
228  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
229  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IRRELEVANT);
230  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
231 
232  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
233  ->with($this->itemTreeBounds, $this->groupId)
234  ->andReturn($clearingDecision);
235 
236  $eventId = 65;
237  $this->clearingDao->shouldReceive("insertClearingEvent")->with($this->itemTreeBounds->getItemId(), $this->userId, $this->groupId, $addedEvent->getLicenseId(), true)->andReturn($eventId);
238  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with( $this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::REPO, array($eventId));
239  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
240 
241  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
242  }
243 
244  public function testMakeDecisionFromLastEventsWithNoLicenseKnownTypeAndAnExistingRemovingUserEvent()
245  {
247  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
248  $removedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName(), ClearingEventTypes::USER, $isRemoved = true);
249 
250  $isGlobal = true;
251 
252  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
253  ->with($this->itemTreeBounds)->andReturn(array());
254 
255  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
256  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
257  ->andReturn(array($licenseRef->getId() => $removedEvent));
258 
259  $clearingDecision = M::mock(ClearingDecision::class);
260  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
261  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IRRELEVANT);
262  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
263 
264  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
265  ->with($this->itemTreeBounds, $this->groupId)
266  ->andReturn($clearingDecision);
267 
268  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with( $this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::REPO, array());
269  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
270 
271  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
272  }
276  public function testMakeDecisionFromLastEventsWithDelayedScanner()
277  {
279  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
280 
281  $isGlobal = false;
282  $removedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName(), ClearingEventTypes::USER, $isRemoved = true);
283 
284  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
285  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
286  ->andReturn(array($licenseRef->getId() => $removedEvent));
287 
288  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
289  ->with($this->itemTreeBounds)->andReturn($scannerResults);
290 
291  $clearingDecision = M::mock(ClearingDecision::class);
292  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
293  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IDENTIFIED);
294  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
295 
296  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
297  ->with($this->itemTreeBounds,$this->groupId)
298  ->andReturn($clearingDecision);
299 
300  $this->clearingDao->shouldReceive("insertClearingEvent")
301  ->never();
302 
303  $this->clearingDao->shouldReceive("createDecisionFromEvents")
304  ->once()
305  ->with($this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::ITEM,
306  is(arrayContainingInAnyOrder($removedEvent->getEventId())));
307  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
308 
309  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal);
310  }
311 
312  public function testMakeDecisionFromLastEventsWithInvalidType()
313  {
314  $this->clearingDao->shouldReceive("getRelevantClearingEvents")->never();
315  $this->agentLicenseEventProcessor->shouldReceive("getScannerDetectedLicenses")->never();
316  $this->clearingDao->shouldReceive("getRelevantClearingDecision")->never();
317 
318  $this->clearingDao->shouldReceive("createDecisionFromEvents")->never();
319  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
320 
321  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE - 1, false);
322  }
323 
324  public function testGetCurrentClearingsWithoutDecisions()
325  {
326  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
327  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)
328  ->andReturn(array());
329  $this->clearingDao->shouldReceive("getRelevantClearingEvents")->with($this->itemTreeBounds, $this->groupId)->andReturn(array());
330 
331  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
332 
333  assertThat($licenseDecisions, is(emptyArray()));
334  assertThat($removedClearings, is(emptyArray()));
335  }
336 
337  public function testGetCurrentClearingsWithUserDecisionsOnly()
338  {
339  $addedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseId=13, "licA", "License A");
340 
341  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
342  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)
343  ->andReturn(array());
344  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
345  ->with($this->itemTreeBounds, $this->groupId)
346  ->andReturn(array($licenseId => $addedEvent));
347 
348  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
349 
350  assertThat($licenseDecisions, is(arrayWithSize(1)));
351 
353  $result = $licenseDecisions[$addedEvent->getLicenseId()];
354  assertThat($result->getLicenseRef(), is($addedEvent->getLicenseRef()));
355  assertThat($result->getClearingEvent(), is($addedEvent));
356  assertThat($result->getAgentDecisionEvents(), is(emptyArray()));
357  assertThat($removedClearings, is(emptyArray()));
358  }
359 
360  public function testGetCurrentClearingsWithAgentDecisionsOnly()
361  {
362  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
363 
364  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
365  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)
366  ->andReturn($scannerResults);
367  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
368  ->with($this->itemTreeBounds, $this->groupId)
369  ->andReturn(array());
370 
371  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
372 
373  assertThat($licenseDecisions, is(arrayWithSize(1)));
374 
376  $result = $licenseDecisions[$licenseRef->getId()];
377  assertThat($result->getLicenseRef(), is($licenseRef));
378  assertThat($result->getClearingEvent(), is(nullValue()));
379  assertThat($result->getAgentDecisionEvents(), is(arrayWithSize(1)));
380  assertThat($removedClearings, is(emptyArray()));
381  }
382 
383  public function testGetCurrentClearingsWithUserAndAgentDecision()
384  {
386  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
387 
388  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
389  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)
390  ->andReturn($scannerResults);
391 
392  $licenseId = $licenseRef->getId();
393 
394  $addedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseId, $licenseRef->getShortName(), $licenseRef->getFullName());
395  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
396  ->with($this->itemTreeBounds, $this->groupId)
397  ->andReturn(array($licenseId => $addedEvent));
398 
399  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
400 
401  assertThat($licenseDecisions, is(arrayWithSize(1)));
402 
404  $result = $licenseDecisions[$licenseRef->getId()];
405  assertThat($result->getLicenseRef(), is($licenseRef));
406  assertThat($result->getClearingEvent(), is($addedEvent));
407  assertThat($result->getAgentDecisionEvents(), is(arrayWithSize(1)));
408  assertThat($removedClearings, is(emptyArray()));
409  }
410 
411  public function testGetCurrentClearingsWithUserRemovedDecisionsOnly()
412  {
414  list($scannerResults, $licenseRef, $agentRef) = $this->createScannerDetectedLicenses();
415  $removedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName(), ClearingEventTypes::USER, true);
416 
417  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
418  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)->andReturn($scannerResults);
419 
420  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
421  ->with($this->itemTreeBounds, $this->groupId)
422  ->andReturn(array($licenseRef->getId() => $removedEvent));
423 
424  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
425 
426  assertThat($licenseDecisions, is(emptyArray()));
427  assertThat($removedClearings, is(arrayWithSize(1)));
428 
430  $result = $removedClearings[$removedEvent->getLicenseId()];
431  assertThat($result->getLicenseRef(), is($removedEvent->getLicenseRef()));
432  assertThat($result->getClearingEvent(), is($removedEvent));
433  $agentClearingEvents = $result->getAgentDecisionEvents();
434  assertThat($agentClearingEvents, is(arrayWithSize(1)));
435 
436  $agentEvent = $agentClearingEvents[0];
437 
438  assertThat($agentEvent->getAgentRef(), is($agentRef));
439  assertThat($agentEvent->getLicenseRef(), is($licenseRef));
440  assertThat($agentEvent->getMatchId(), is(self::MATCH_ID));
441  assertThat($agentEvent->getPercentage(), is(self::PERCENTAGE));
442  }
443 
444  public function testGetUnhandledScannerDetectedLicenses()
445  {
447  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
448 
449  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
450  ->with($this->itemTreeBounds, $this->groupId)
451  ->andReturn(array());
452  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
453  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)->andReturn($scannerResults);
454 
455  $hasUnhandledScannerDetectedLicenses = $this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($this->itemTreeBounds, $this->groupId);
456 
457  assertThat($hasUnhandledScannerDetectedLicenses);
458  }
459 
460  public function testGetUnhandledScannerDetectedLicensesWithMatch()
461  {
463  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
464  $clearingEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName());
465 
466  $this->clearingDao->shouldReceive("getRelevantClearingEvents")->once()
467  ->with($this->itemTreeBounds, $this->groupId)
468  ->andReturn(array($clearingEvent->getLicenseId()=>$clearingEvent));
469  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")->once()
470  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)->andReturn($scannerResults);
471 
472  $hasUnhandledScannerDetectedLicenses = $this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($this->itemTreeBounds, $this->groupId);
473 
474  assertThat( $hasUnhandledScannerDetectedLicenses, is(False));
475  }
476 
477  public function testGetUnhandledScannerDetectedLicensesWithoutMatch()
478  {
480  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
481  $offset = 23;
482  $clearingEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId()+$offset, $licenseRef->getShortName(), $licenseRef->getFullName());
483 
484  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
485  ->with($this->itemTreeBounds, $this->groupId)
486  ->andReturn(array($clearingEvent->getLicenseId()=>$clearingEvent));
487  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
488  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)->andReturn($scannerResults);
489 
490  $hasUnhandledScannerDetectedLicenses = $this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($this->itemTreeBounds, $this->groupId);
491 
492  assertThat($hasUnhandledScannerDetectedLicenses, is(True));
493  }
494 
495  public function testGetUnhandledScannerDetectedLicensesWithMappedMatch()
496  {
498  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
499  $offset = 0;
500  $clearingEvent = $this->createClearingEvent($eventId=123, $this->timestamp, $licenseRef->getId()+$offset, $licenseRef->getShortName(), $licenseRef->getFullName());
501 
502  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
503  ->with($this->itemTreeBounds, $this->groupId)
504  ->andReturn(array($clearingEvent->getLicenseId()=>$clearingEvent));
505  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
506  ->with($this->itemTreeBounds,LicenseMap::CONCLUSION)->andReturn($scannerResults);
507 
508  $licenseMap = M::mock(LicenseMap::class);
509  $licenseMap->shouldReceive('getProjectedId')->andReturnUsing(function($id) {
510  return $id;
511  });
512  $licenseMap->shouldReceive('getUsage')->andReturn(LicenseMap::CONCLUSION);
513 
514  $hasUnhandledScannerDetectedLicenses = $this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($this->itemTreeBounds, $this->groupId, array(), $licenseMap);
515 
516  assertThat( $hasUnhandledScannerDetectedLicenses, is(False) );
517  }
518 
531  private function createClearingEvent($eventId, $timestamp, $licenseId, $licenseShortName, $licenseFullName, $eventType = ClearingEventTypes::USER, $isRemoved = false, $reportInfo = "<reportInfo>", $comment = "<comment>")
532  {
533  $licenseRef = new LicenseRef($licenseId, $licenseShortName, $licenseFullName);
534  $clearingLicense = new ClearingLicense($licenseRef, $isRemoved, $reportInfo, $comment);
535  return new ClearingEvent($eventId, $this->uploadTreeId, $timestamp, $this->userId, $this->groupId, $eventType, $clearingLicense);
536  }
537 
541  protected function createScannerDetectedLicenses($licenseId = 13, $licenseShortname = "licA", $licenseFullName = "License-A")
542  {
543  $licenseRef = new LicenseRef($licenseId, $licenseShortname, $licenseFullName);
544 
545  $agentRef = M::mock(AgentRef::class);
546 
547  $scannerEvents = array(
548  $licenseId => array(new AgentClearingEvent($licenseRef, $agentRef, self::MATCH_ID, self::PERCENTAGE))
549  );
550 
551  return array($scannerEvents, $licenseRef, $agentRef);
552  }
553 }
createScannerDetectedLicenses($licenseId=13, $licenseShortname="licA", $licenseFullName="License-A")
Utility functions to process ClearingDecision.
Contains business rules for FOSSology.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
createClearingEvent($eventId, $timestamp, $licenseId, $licenseShortName, $licenseFullName, $eventType=ClearingEventTypes::USER, $isRemoved=false, $reportInfo="<reportInfo>", $comment="<comment>")