49 include_once(__DIR__.
'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
50 include_once(__DIR__.
'/SchedulerTestRunnerCli.php');
51 include_once(__DIR__.
'/SchedulerTestRunnerMock.php');
123 $this->testDb =
new TestPgDb(
"reuserSched");
124 $this->
dbManager = $this->testDb->getDbManager();
127 $logger =
new Logger(
"ReuserSchedulerTest");
128 $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
133 $this->treeDao = \Mockery::mock(TreeDao::class);
138 $this->clearingDao, $this->uploadDao, $this->clearingDecisionFilter, $this->treeDao);
147 $this->testDb->fullDestruct();
148 $this->testDb = null;
150 $this->licenseDao = null;
151 $this->highlightDao = null;
152 $this->clearingDao = null;
160 $sysConf = $this->testDb->getFossSysConf();
162 $this->testInstaller->init();
163 $this->testInstaller->cpRepo();
171 $this->testInstaller->rmRepo();
172 $this->testInstaller->clear();
180 $this->testDb->createPlainTables(array(
'upload',
'upload_reuse',
'uploadtree',
181 'uploadtree_a',
'license_ref',
'license_ref_bulk',
'clearing_decision',
182 'clearing_decision_event',
'clearing_event',
'license_file',
'highlight',
183 'highlight_bulk',
'agent',
'pfile',
'ars_master',
'users',
'group_user_member',
185 'upload_clearing_license'),
false);
186 $this->testDb->createSequences(array(
'agent_agent_pk_seq',
'pfile_pfile_pk_seq',
187 'upload_upload_pk_seq',
'nomos_ars_ars_pk_seq',
'license_file_fl_pk_seq',
188 'license_ref_rf_pk_seq',
'license_ref_bulk_lrb_pk_seq',
189 'clearing_decision_clearing_decision_pk_seq',
'clearing_event_clearing_event_pk_seq'),
false);
190 $this->testDb->createViews(array(
'license_file_ref'),
false);
191 $this->testDb->createConstraints(array(
'agent_pkey',
'pfile_pkey',
192 'upload_pkey_idx',
'FileLicense_pkey',
'clearing_event_pkey'),
false);
193 $this->testDb->alterTables(array(
'agent',
'pfile',
'upload',
'ars_master',
194 'license_ref_bulk',
'clearing_event',
'clearing_decision',
'license_file',
'highlight'),
false);
195 $this->testDb->createInheritedTables();
196 $this->testDb->createInheritedArsTables(array(
'monk'));
198 $this->testDb->insertData(array(
'pfile',
'upload',
'uploadtree_a',
'users',
199 'group_user_member',
'agent',
'license_file',
'monk_ars'),
false);
200 $this->testDb->insertData_license_ref(80);
202 $this->testDb->resetSequenceAsMaxOf(
'agent_agent_pk_seq',
'agent',
'agent_pk');
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);
getFilteredClearings($uploadId, $groupId)
Get clearings for a given upload id.
testReuserMockedScanWithARepoClearing()
Call runnerReuserScanWithARepoClearing()
tearDown()
Tear down test env.
testReuserRealScanWithARepoClearingEnhanced()
Call runnerReuserScanWithARepoClearingEnhanced()
testReuserMockedScanWithALocalClearing()
Call runnerReuserScanWithALocalClearing()
setUpTables()
Setup tables required by the agent.
Create mock objects for reuser.
testReuserRealScanWithARepoClearing()
Call runnerReuserScanWithARepoClearing()
Tests for Reuser agent and scheduler interaction.
insertDecisionFromTwoEvents($scope=DecisionScopes::ITEM, $originallyClearedItemId=23)
Creates two clearing decisions.
runnerReuserScanWithoutAnyUploadToCopyAndAClearing($runner)
Run reuser agent with no upload to copy decisions from.
rmRepo()
Tear down test repo.
Handles scheduler interaction.
Various utility functions to filter ClearingDecision.
testReuserRealScanWithoutAnyUploadToCopyAndAClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndAClearing()
testReuserMockedScanWithoutAnyUploadToCopyAndAClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndAClearing()
runnerReuserScanWithoutAnyUploadToCopyAndNoClearing(SchedulerTestRunner $runner)
Test on an upload with no clearing decisions.
testReuserMockedScanWithoutAnyUploadToCopyAndNoClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndNoClearing()
fo_dbManager * dbManager
fo_dbManager object
run($uploadId, $userId=2, $groupId=2, $jobId=1, $args="")
Function to run agent from scheduler.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
setUpRepo()
Setup test repo.
testReuserRealScanWithoutAnyUploadToCopyAndNoClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndNoClearing()
getHeartCount($output)
Get the heart count from agent.
testReuserRealScanWithALocalClearing()
Call runnerReuserScanWithALocalClearing()
Namespace to hold test cases for Reuser agent.