39 include_once(__DIR__.
'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
40 include_once(__DIR__.
'/SchedulerTestRunnerCli.php');
41 include_once(__DIR__.
'/SchedulerTestRunnerMock.php');
117 $this->testDb =
new TestPgDb(
"reuserSched");
118 $this->
dbManager = $this->testDb->getDbManager();
121 $logger =
new Logger(
"ReuserSchedulerTest");
122 $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
128 $this->treeDao = \Mockery::mock(TreeDao::class);
133 $this->clearingDao, $this->uploadDao, $this->clearingDecisionFilter,
134 $this->treeDao, $this->copyrightDao);
143 $this->testDb->fullDestruct();
144 $this->testDb =
null;
146 $this->licenseDao =
null;
147 $this->highlightDao =
null;
148 $this->clearingDao =
null;
149 $this->copyrightDao =
null;
157 $sysConf = $this->testDb->getFossSysConf();
159 $this->testInstaller->init();
160 $this->testInstaller->cpRepo();
168 $this->testInstaller->rmRepo();
169 $this->testInstaller->clear();
177 $this->testDb->createPlainTables(array(
'upload',
'upload_reuse',
'uploadtree',
178 'uploadtree_a',
'license_ref',
'license_ref_bulk',
'clearing_decision',
179 'clearing_decision_event',
'clearing_event',
'license_file',
'highlight',
180 'highlight_bulk',
'agent',
'pfile',
'ars_master',
'users',
'group_user_member',
181 'upload_clearing_license',
'report_info'),
false);
182 $this->testDb->createSequences(array(
'agent_agent_pk_seq',
'pfile_pfile_pk_seq',
183 'upload_upload_pk_seq',
'nomos_ars_ars_pk_seq',
'license_file_fl_pk_seq',
184 'license_ref_rf_pk_seq',
'license_ref_bulk_lrb_pk_seq',
185 'clearing_decision_clearing_decision_pk_seq',
186 'clearing_event_clearing_event_pk_seq',
'report_info_pk_seq'),
false);
187 $this->testDb->createViews(array(
'license_file_ref'),
false);
188 $this->testDb->createConstraints(array(
'agent_pkey',
'pfile_pkey',
189 'upload_pkey_idx',
'FileLicense_pkey',
'clearing_event_pkey'),
false);
190 $this->testDb->alterTables(array(
'agent',
'pfile',
'upload',
'ars_master',
191 'license_ref_bulk',
'license_ref',
'clearing_event',
'clearing_decision',
'license_file',
'highlight'),
false);
192 $this->testDb->createInheritedTables();
193 $this->testDb->createInheritedArsTables(array(
'monk'));
195 $this->testDb->insertData(array(
'pfile',
'upload',
'uploadtree_a',
'users',
196 'group_user_member',
'agent',
'license_file',
'monk_ars',
'report_info'),
198 $this->testDb->insertData_license_ref(80);
200 $this->testDb->resetSequenceAsMaxOf(
'agent_agent_pk_seq',
'agent',
'agent_pk');
202 $this->testDb->setupSysconfig();
211 if (preg_match(
"/.*HEART: ([0-9]*).*/", $output, $matches)) {
212 return intval($matches[1]);
226 $bounds = $this->uploadDao->getParentItemBounds($uploadId);
227 return $this->clearingDao->getFileClearingsFolder($bounds,
$groupId);
263 list($success, $output,$retCode) = $runner->
run($uploadId=1, $this->userId);
265 $this->assertTrue($success,
'cannot run runner');
266 $this->assertEquals($retCode, 0,
'reuser failed: '.$output);
270 $bounds = $this->uploadDao->getParentItemBounds($uploadId);
271 assertThat($this->clearingDao->getFileClearingsFolder($bounds,
$groupId=5), is(emptyArray()));
284 $licenseRef1 = $this->licenseDao->getLicenseByShortName(
"SPL-1.0")->getRef();
285 $licenseRef2 = $this->licenseDao->getLicenseByShortName(
"Glide")->getRef();
287 $addedLicenses = array($licenseRef1, $licenseRef2);
288 assertThat($addedLicenses, not(arrayContaining(
null)));
290 $clearingLicense1 =
new ClearingLicense($licenseRef1,
false, ClearingEventTypes::USER,
"42",
"44");
291 $clearingLicense2 =
new ClearingLicense($licenseRef2,
true, ClearingEventTypes::USER,
"-42",
"-44");
293 $eventId1 = $this->clearingDao->insertClearingEvent($originallyClearedItemId, $this->userId, $this->groupId,
294 $licenseRef1->getId(), $clearingLicense1->isRemoved(),
295 $clearingLicense1->getType(), $clearingLicense1->getReportinfo(), $clearingLicense1->getComment());
296 $eventId2 = $this->clearingDao->insertClearingEvent($originallyClearedItemId, 5, $this->groupId,
297 $licenseRef2->getId(), $clearingLicense2->isRemoved(),
298 $clearingLicense2->getType(), $clearingLicense2->getReportinfo(), $clearingLicense2->getComment());
300 $addedEventIds = array($eventId1, $eventId2);
302 $this->clearingDao->createDecisionFromEvents($originallyClearedItemId, $this->userId,
303 $this->groupId, DecisionTypes::IDENTIFIED, $scope, $addedEventIds);
305 return array($clearingLicense1, $clearingLicense2, $addedEventIds);
341 list($success,$output,$retCode) = $runner->run($uploadId=3);
343 $this->assertTrue($success,
'cannot run runner');
344 $this->assertEquals($retCode, 0,
'reuser failed: '.$output);
349 assertThat($decisions, is(emptyArray()));
366 $this->runnerReuserScanWithALocalClearing($this->runnerMock,1);
381 $this->runnerReuserScanWithALocalClearing($this->runnerCli,1);
389 private function runnerReuserScanWithALocalClearing($runner, $heartBeat=0)
394 $this->uploadDao->addReusedUpload($uploadId=3,$reusedUpload=2,$this->groupId,$this->groupId);
400 $reusingUploadItemShift = 20;
402 list($success,$output,$retCode) = $runner->run($uploadId, $this->userId, $this->groupId);
404 $this->assertTrue($success,
'cannot run runner');
405 $this->assertEquals($retCode, 0,
'reuser failed: '.$output);
406 assertThat($this->
getHeartCount($output), equalTo($heartBeat));
411 assertThat($newUploadClearings, is(arrayWithSize(1)));
413 assertThat($potentiallyReusableClearings, is(arrayWithSize(1)));
415 $potentiallyReusableClearing = $potentiallyReusableClearings[0];
417 $newClearing = $newUploadClearings[0];
419 assertThat($newClearing, not(equalTo($potentiallyReusableClearing)));
420 assertThat($newClearing->getClearingId(), not(equalTo($potentiallyReusableClearing->getClearingId())));
422 assertThat($newClearing->getClearingLicenses(), arrayContainingInAnyOrder($clearingLicense1, $clearingLicense2));
424 assertThat($newClearing->getType(), equalTo($potentiallyReusableClearing->getType()));
425 assertThat($newClearing->getScope(), equalTo($potentiallyReusableClearing->getScope()));
427 assertThat($newClearing->getUploadTreeId(),
428 equalTo($potentiallyReusableClearing->getUploadTreeId() + $reusingUploadItemShift));
445 $this->runnerReuserScanWithARepoClearing($this->runnerMock);
460 $this->runnerReuserScanWithARepoClearing($this->runnerCli);
467 private function runnerReuserScanWithARepoClearing($runner)
472 $this->uploadDao->addReusedUpload($uploadId=3,$reusedUpload=2,$this->groupId,$this->groupId);
475 DecisionScopes::REPO,$originallyClearedItemId=23);
476 $clearingLicenses = array($clearingLicense1, $clearingLicense2);
480 $reusingUploadItemShift = 20;
482 list($success,$output,$retCode) = $runner->run($uploadId, $this->userId, $this->groupId);
484 $this->assertTrue($success,
'cannot run runner');
485 $this->assertEquals($retCode, 0,
'reuser failed: '.$output);
492 assertThat($newUploadClearings, is(arrayWithSize(1)));
494 assertThat($potentiallyReusableClearings, is(arrayWithSize(1)));
496 $potentiallyReusableClearing = $potentiallyReusableClearings[0];
498 $newClearing = $newUploadClearings[0];
502 assertThat($newClearing, not(equalTo($potentiallyReusableClearing)));
505 assertThat($newClearing->getClearingId(), equalTo($potentiallyReusableClearing->getClearingId()));
507 assertThat($newClearing->getClearingLicenses(), arrayContainingInAnyOrder($clearingLicenses));
509 assertThat($newClearing->getType(), equalTo($potentiallyReusableClearing->getType()));
510 assertThat($newClearing->getScope(), equalTo($potentiallyReusableClearing->getScope()));
512 assertThat($newClearing->getUploadTreeId(),
513 equalTo($potentiallyReusableClearing->getUploadTreeId() + $reusingUploadItemShift));
516 $bounds = $this->uploadDao->getItemTreeBounds($originallyClearedItemId + $reusingUploadItemShift);
517 $newEvents = $this->clearingDao->getRelevantClearingEvents($bounds, $this->groupId);
519 assertThat($newEvents, is(arrayWithSize(count($clearingLicenses))));
522 foreach ($newEvents as $newEvent) {
523 assertThat($newEvent->getEventId(), anyOf($addedEventIds));
524 assertThat($newEvent->getClearingLicense(), anyOf($clearingLicenses));
543 $this->runnerReuserScanWithARepoClearingEnhanced($this->runnerMock);
550 private function runnerReuserScanWithARepoClearingEnhanced($runner)
555 $originallyClearedItemId = 23;
557 $reusingUploadItemShift = 20;
559 $this->uploadDao->addReusedUpload($uploadId=3,$reusedUpload=2,$this->groupId,$this->groupId,$reuseMode=2);
561 $repoPath = $this->testDb->getFossSysConf().
'/repo/files/';
562 $this->treeDao->shouldReceive(
'getRepoPathOfPfile')->with(4)->andReturn($repoPath
563 .
'04621571bcbabce75c4dd1c6445b87dec0995734.59cacdfce5051cd8a1d8a1f2dcce40a5.12320');
564 $this->treeDao->shouldReceive(
'getRepoPathOfPfile')->with(351)->andReturn($repoPath
565 .
'c518ce1658140b65fa0132ad1130cb91512416bf.8e913e594d24ff3aeabe350107d97815.35829');
568 DecisionScopes::REPO,$originallyClearedItemId);
569 $clearingLicenses = array($clearingLicense1, $clearingLicense2);
571 list($success,$output,$retCode) = $runner->run($uploadId, $this->userId, $this->groupId);
573 $this->assertTrue($success,
'cannot run runner');
574 $this->assertEquals($retCode, 0,
'reuser failed: '.$output);
579 assertThat($newUploadClearings, is(arrayWithSize(1)));
581 assertThat($potentiallyReusableClearings, is(arrayWithSize(1)));
583 $potentiallyReusableClearing = $potentiallyReusableClearings[0];
585 $newClearing = $newUploadClearings[0];
589 assertThat($newClearing, not(equalTo($potentiallyReusableClearing)));
591 assertThat($newClearing->getClearingLicenses(), arrayContainingInAnyOrder($clearingLicenses));
593 assertThat($newClearing->getType(), equalTo($potentiallyReusableClearing->getType()));
594 assertThat($newClearing->getScope(), equalTo($potentiallyReusableClearing->getScope()));
596 assertThat($newClearing->getUploadTreeId(),
597 equalTo($potentiallyReusableClearing->getUploadTreeId() + $reusingUploadItemShift));
600 $bounds = $this->uploadDao->getItemTreeBounds($originallyClearedItemId + $reusingUploadItemShift);
601 $newEvents = $this->clearingDao->getRelevantClearingEvents($bounds, $this->groupId);
603 assertThat($newEvents, is(arrayWithSize(count($clearingLicenses))));
606 foreach ($newEvents as $newEvent) {
607 assertThat($newEvent->getEventId(), anyOf($addedEventIds));
608 assertThat($newEvent->getClearingLicense(), anyOf($clearingLicenses));
611 $this->clearingDao->makeMainLicense($uploadId=2, $this->groupId, $mainLicenseId=402);
612 $mainLicenseIdForReuse = $this->clearingDao->getMainLicenseIds($reusedUploadId=2, $this->groupId);
613 $mainLicenseIdForReuseSingle = array_values($mainLicenseIdForReuse);
614 $this->clearingDao->makeMainLicense($uploadId=3, $this->groupId, $mainLicenseIdForReuseSingle[0]);
615 $mainLicense=$this->clearingDao->getMainLicenseIds($uploadId=3, $this->groupId);
616 $mainLicenseSingle = array_values($mainLicense);
617 $this->assertEquals($mainLicenseIdForReuseSingle, $mainLicenseSingle);
Various utility functions to filter ClearingDecision.
Handles scheduler interaction.
Create mock objects for reuser.
Tests for Reuser agent and scheduler interaction.
insertDecisionFromTwoEvents($scope=DecisionScopes::ITEM, $originallyClearedItemId=23)
Creates two clearing decisions.
testReuserMockedScanWithARepoClearing()
Call runnerReuserScanWithARepoClearing()
testReuserRealScanWithALocalClearing()
Call runnerReuserScanWithALocalClearing()
testReuserRealScanWithoutAnyUploadToCopyAndAClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndAClearing()
testReuserRealScanWithARepoClearing()
Call runnerReuserScanWithARepoClearing()
setUpRepo()
Setup test repo.
tearDown()
Tear down test env.
getFilteredClearings($uploadId, $groupId)
Get clearings for a given upload id.
rmRepo()
Tear down test repo.
testReuserMockedScanWithoutAnyUploadToCopyAndNoClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndNoClearing()
testReuserMockedScanWithoutAnyUploadToCopyAndAClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndAClearing()
getHeartCount($output)
Get the heart count from agent.
runnerReuserScanWithoutAnyUploadToCopyAndNoClearing(SchedulerTestRunner $runner)
Test on an upload with no clearing decisions.
setUpTables()
Setup tables required by the agent.
testReuserRealScanWithoutAnyUploadToCopyAndNoClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndNoClearing()
testReuserMockedScanWithALocalClearing()
Call runnerReuserScanWithALocalClearing()
runnerReuserScanWithoutAnyUploadToCopyAndAClearing($runner)
Run reuser agent with no upload to copy decisions from.
testReuserRealScanWithARepoClearingEnhanced()
Call runnerReuserScanWithARepoClearingEnhanced()
run($uploadId, $userId=2, $groupId=2, $jobId=1, $args="")
Function to run agent from scheduler.
fo_dbManager * dbManager
fo_dbManager object
Namespace to hold test cases for Reuser agent.
list_t type structure used to keep various lists. (e.g. there are multiple lists).