32 include_once(__DIR__.
'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
33 include_once(__DIR__.
'/../../../lib/php/Plugin/FO_Plugin.php');
34 include_once(__DIR__.
'/SchedulerTestRunnerCli.php');
35 include_once(__DIR__.
'/SchedulerTestRunnerMock.php');
48 private $testInstaller;
55 private $clearingDecisionProcessor;
57 private $agentLicenseEventProcessor;
61 private $uploadPermDao;
63 private $highlightDao;
73 protected function setUp() : void
75 $this->testDb =
new TestPgDb(
"deciderJobSched".time());
76 $this->
dbManager = $this->testDb->getDbManager();
77 $logger = M::mock(
'Monolog\Logger');
78 $this->testDb->setupSysconfig();
81 $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
90 $this->runnerMock =
new SchedulerTestRunnerMock($this->
dbManager, $agentDao, $this->clearingDao, $this->uploadDao, $this->highlightDao, $this->clearingDecisionProcessor, $this->agentLicenseEventProcessor);
99 $this->testDb->fullDestruct();
100 $this->testDb =
null;
102 $this->licenseDao =
null;
103 $this->highlightDao =
null;
104 $this->clearingDao =
null;
112 $sysConf = $this->testDb->getFossSysConf();
114 $this->testInstaller->init();
115 $this->testInstaller->cpRepo();
123 $this->testInstaller->rmRepo();
124 $this->testInstaller->clear();
132 $this->testDb->createPlainTables(array(
'upload',
'upload_reuse',
'uploadtree',
133 'uploadtree_a',
'license_ref',
'license_ref_bulk',
'clearing_decision',
134 'clearing_decision_event',
'clearing_event',
'license_file',
'highlight',
135 'highlight_bulk',
'agent',
'pfile',
'ars_master',
'users',
'group_user_member',
136 'license_map',
'report_info'),
false);
137 $this->testDb->createSequences(array(
'agent_agent_pk_seq',
'pfile_pfile_pk_seq',
'upload_upload_pk_seq',
'nomos_ars_ars_pk_seq',
'license_file_fl_pk_seq',
'license_ref_rf_pk_seq',
'license_ref_bulk_lrb_pk_seq',
'clearing_decision_clearing_decision_pk_seq',
'clearing_event_clearing_event_pk_seq',
'FileLicense_pkey'),
false);
138 $this->testDb->createViews(array(
'license_file_ref'),
false);
139 $this->testDb->createConstraints(array(
'agent_pkey',
'pfile_pkey',
'upload_pkey_idx',
'clearing_event_pkey'),
false);
140 $this->testDb->alterTables(array(
'agent',
'pfile',
'upload',
'ars_master',
'license_ref_bulk',
'license_ref',
'clearing_event',
'clearing_decision',
'license_file',
'highlight'),
false);
141 $this->testDb->createInheritedTables();
142 $this->testDb->createInheritedArsTables(array(
'nomos',
'monk'));
144 $this->testDb->insertData(array(
'pfile',
'upload',
'uploadtree_a',
'users',
'group_user_member',
'agent',
'license_file',
'nomos_ars',
'monk_ars'),
false);
145 $this->testDb->insertData_license_ref();
147 $this->testDb->resetSequenceAsMaxOf(
'agent_agent_pk_seq',
'agent',
'agent_pk');
158 if (preg_match(
"/.*HEART: ([0-9]*).*/", $output, $matches)) {
159 return intval($matches[1]);
176 $this->runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($this->runnerMock);
189 $this->runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($this->runnerCli);
195 private function runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($runner)
202 $licenseRef1 = $this->licenseDao->getLicenseByShortName(
"SPL-1.0")->getRef();
203 $licenseRef2 = $this->licenseDao->getLicenseByShortName(
"Glide")->getRef();
205 $addedLicenses = array($licenseRef1, $licenseRef2);
207 assertThat($addedLicenses, not(arrayContaining(
null)));
209 $eventId1 = $this->clearingDao->insertClearingEvent($originallyClearedItemId=23, $userId=2, $groupId=3, $licenseRef1->getId(),
false);
210 $eventId2 = $this->clearingDao->insertClearingEvent($originallyClearedItemId, 5, $groupId, $licenseRef2->getId(),
true);
212 $this->
dbManager->queryOnce(
"UPDATE clearing_event SET job_fk=$jobId");
214 $addedEventIds = array($eventId1, $eventId2);
216 list($success,$output,$retCode) = $runner->run($uploadId=2, $userId, $groupId, $jobId, $args=
"");
218 $this->assertTrue($success,
'cannot run runner');
219 $this->assertEquals($retCode, 0,
'decider failed (did you make test?): '.$output);
223 $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
224 $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
225 assertThat($decisions, is(arrayWithSize(1)));
228 $deciderMadeDecision = $decisions[0];
230 foreach ($deciderMadeDecision->getClearingEvents() as $event) {
231 assertThat($event->getEventId(), is(anyOf($addedEventIds)));
269 $licenseRef1 = $this->licenseDao->getLicenseByShortName(
"SPL-1.0")->getRef();
271 $licId1 = $licenseRef1->getId();
276 $this->
dbManager->queryOnce(
"DELETE FROM license_file");
277 $this->
dbManager->queryOnce(
"INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,$licId1,$pfile,$agentNomosId)");
278 $this->
dbManager->queryOnce(
"INSERT INTO highlight (fl_fk,start,len) VALUES(12222,12,3)");
279 $this->
dbManager->queryOnce(
"INSERT INTO highlight (fl_fk,start,len) VALUES(12222,18,3)");
281 list($success,$output,$retCode) = $runner->run($uploadId=2, $userId=6, $groupId=4, $jobId=31, $args=
"");
283 $this->assertTrue($success,
'cannot run runner');
284 $this->assertEquals($retCode, 0,
'decider failed (did you make test?): '.$output);
288 $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
289 $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
290 assertThat($decisions, is(arrayWithSize(0)));
305 $dbManager = M::mock(DbManager::class);
306 $agentDao = M::mock(AgentDao::class);
307 $clearingDao = M::mock(ClearingDao::class);
308 $uploadDao = M::mock(UploadDao::class);
309 $highlightDao = M::mock(HighlightDao::class);
310 $decisionProcessor = M::mock(ClearingDecisionProcessor::class);
311 $agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
316 $agentDao->shouldReceive(
'arsTableExists')->andReturn(
true);
317 $agentDao->shouldReceive(
'getCurrentAgentId')->andReturn($agentId=24);
318 $agentDao->shouldReceive(
'writeArsRecord')->with(anything(), $agentId, $uploadId)->andReturn($arsId=2);
319 $agentDao->shouldReceive(
'writeArsRecord')->with(anything(), $agentId, $uploadId, $arsId,
true)->andReturn(0);
325 $itemIds = array(4343, 43);
327 $bounds0 = M::mock(ItemTreeBounds::class);
328 $bounds0->shouldReceive(
'getItemId')->andReturn($itemIds[0]);
329 $bounds0->shouldReceive(
'containsFiles')->andReturn(
false);
330 $bounds1 = M::mock(ItemTreeBounds::class);
331 $bounds1->shouldReceive(
'getItemId')->andReturn($itemIds[1]);
332 $bounds1->shouldReceive(
'containsFiles')->andReturn(
false);
333 $bounds = array($bounds0, $bounds1);
335 $uploadDao->shouldReceive(
'getItemTreeBounds')->with($itemIds[0])->andReturn($bounds[0]);
336 $uploadDao->shouldReceive(
'getItemTreeBounds')->with($itemIds[1])->andReturn($bounds[1]);
338 $clearingDao->shouldReceive(
'getEventIdsOfJob')->with($jobId)
339 ->andReturn(array($itemIds[0] => array(), $itemIds[1] => array()));
341 $dbManager->shouldReceive(
'begin')->times(count($itemIds));
342 $dbManager->shouldReceive(
'commit')->times(count($itemIds));
345 $dbManager->shouldReceive(
'prepare');
346 $res = M::Mock(DbManager::class);
347 $dbManager->shouldReceive(
'execute')->andReturn($res);
348 $row1 = array(
'rf_fk' => 2334,
'parent_fk' => 1);
349 $row2 = array(
'rf_fk' => 2333,
'parent_fk' => 1);
350 $dbManager->shouldReceive(
'fetchArray')->with($res)->andReturn($row1, $row2,
false);
351 $dbManager->shouldReceive(
'freeResult')->with($res);
354 $decisionProcessor->shouldReceive(
'hasUnhandledScannerDetectedLicenses')
355 ->with($bounds0, $groupId, array(), anything())->andReturn(
true);
356 $clearingDao->shouldReceive(
'markDecisionAsWip')
357 ->with($itemIds[0], $userId, $groupId);
359 $decisionProcessor->shouldReceive(
'hasUnhandledScannerDetectedLicenses')
360 ->with($bounds1, $groupId, array(), anything())->andReturn(
false);
361 $decisionProcessor->shouldReceive(
'makeDecisionFromLastEvents')
362 ->with($bounds1, $userId, $groupId, DecisionTypes::IDENTIFIED,
false, array());
364 $runner =
new SchedulerTestRunnerMock($dbManager, $agentDao, $clearingDao, $uploadDao, $highlightDao, $decisionProcessor, $agentLicenseEventProcessor);
366 list($success,$output,$retCode) = $runner->run($uploadId, $userId, $groupId, $jobId, $args=
"");
368 $this->assertTrue($success,
'cannot run decider');
369 $this->assertEquals($retCode, 0,
'decider failed: '.$output);
370 assertThat($this->
getHeartCount($output), equalTo(count($itemIds)));
386 $this->runnerDeciderScanWithForceDecision($this->runnerMock);
400 $this->runnerDeciderScanWithForceDecision($this->runnerCli);
406 private function runnerDeciderScanWithForceDecision($runner)
413 $licenseRef1 = $this->licenseDao->getLicenseByShortName(
"SPL-1.0")->getRef();
414 $licenseRef2 = $this->licenseDao->getLicenseByShortName(
"Glide")->getRef();
416 $agentLicId = $this->licenseDao->getLicenseByShortName(
"Adaptec")->getRef()->getId();
418 $addedLicenses = array($licenseRef1, $licenseRef2);
420 assertThat($addedLicenses, not(arrayContaining(
null)));
425 $this->
dbManager->queryOnce(
"INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,$agentLicId,$pfile,$agentId)");
427 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($itemId=23);
428 assertThat($this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds), is(not(emptyArray())));
430 $eventId1 = $this->clearingDao->insertClearingEvent($itemId, $userId=2, $groupId=3, $licenseRef1->getId(),
false);
431 $eventId2 = $this->clearingDao->insertClearingEvent($itemId, 5, $groupId, $licenseRef2->getId(),
true);
433 $this->
dbManager->queryOnce(
"UPDATE clearing_event SET job_fk=$jobId");
435 $addedEventIds = array($eventId1, $eventId2);
437 list($success,$output,$retCode) = $runner->run($uploadId=2, $userId, $groupId, $jobId, $args=
"-k1");
439 $this->assertTrue($success,
'cannot run runner');
440 $this->assertEquals($retCode, 0,
'decider failed: '.$output);
443 $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
444 $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
445 assertThat($decisions, is(arrayWithSize(1)));
448 $deciderMadeDecision = $decisions[0];
451 foreach ($deciderMadeDecision->getClearingEvents() as $event) {
452 $eventIds[] = $event->getEventId();
455 assertThat($eventIds, arrayValue($addedEventIds[0]));
456 assertThat($eventIds, arrayValue($addedEventIds[1]));
457 assertThat($eventIds, arrayWithSize(1+count($addedEventIds)));
Mock for scheduler inputs.
Test interactions between scheduler and agent.
setUpRepo()
Setup test repository.
getHeartCount($output)
Get the heart count value from the agent output.
testDeciderRealScanWithNoEventsAndOnlyNomosShouldNotMakeADecision()
testDeciderScanWithTwoEventAndNoAgentShouldMakeADecision()
testDeciderMockedScanWithForceDecision()
tearDown()
Destroy objects, database and repository.
rmRepo()
Destroy test repository.
setUpTables()
Create test tables required by agent.
setUp()
Setup the objects, database and repository.
testDeciderRealScanWithForceDecision()
testDeciderMockScanWithNoEventsAndOnlyNomosShouldNotMakeADecision()
testDeciderRealScanWithTwoEventAndNoAgentShouldMakeADecision()
testDeciderMockedScanWithTwoEventAndNoAgentShouldMakeADecision()
runnerDeciderScanWithNoEventsAndOnlyNomosShouldNotMakeADecision($runner)
run decider with no events
Handle events related to license findings.
Utility functions to process ClearingDecision.
Functions to process clearing events.
fo_dbManager * dbManager
fo_dbManager object
Namespace for decider job test cases.