43 include_once(__DIR__.
'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
44 include_once(__DIR__.
'/../../../lib/php/Plugin/FO_Plugin.php');
45 include_once(__DIR__.
'/SchedulerTestRunnerCli.php');
46 include_once(__DIR__.
'/SchedulerTestRunnerMock.php');
59 private $testInstaller;
66 private $clearingDecisionProcessor;
68 private $agentLicenseEventProcessor;
72 private $uploadPermDao;
74 private $highlightDao;
86 $this->testDb =
new TestPgDb(
"deciderJobSched".time());
87 $this->
dbManager = $this->testDb->getDbManager();
88 $logger = M::mock(
'Monolog\Logger');
91 $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
100 $this->runnerMock =
new SchedulerTestRunnerMock($this->
dbManager, $agentDao, $this->clearingDao, $this->uploadDao, $this->highlightDao, $this->clearingDecisionProcessor, $this->agentLicenseEventProcessor);
109 $this->testDb->fullDestruct();
110 $this->testDb = null;
112 $this->licenseDao = null;
113 $this->highlightDao = null;
114 $this->clearingDao = null;
122 $sysConf = $this->testDb->getFossSysConf();
124 $this->testInstaller->init();
125 $this->testInstaller->cpRepo();
133 $this->testInstaller->rmRepo();
134 $this->testInstaller->clear();
142 $this->testDb->createPlainTables(array(
'upload',
'upload_reuse',
'uploadtree',
'uploadtree_a',
'license_ref',
'license_ref_bulk',
'clearing_decision',
'clearing_decision_event',
'clearing_event',
'license_file',
'highlight',
'highlight_bulk',
'agent',
'pfile',
'ars_master',
'users',
'group_user_member',
'license_map'),
false);
143 $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);
144 $this->testDb->createViews(array(
'license_file_ref'),
false);
145 $this->testDb->createConstraints(array(
'agent_pkey',
'pfile_pkey',
'upload_pkey_idx',
'clearing_event_pkey'),
false);
146 $this->testDb->alterTables(array(
'agent',
'pfile',
'upload',
'ars_master',
'license_ref_bulk',
'clearing_event',
'clearing_decision',
'license_file',
'highlight'),
false);
147 $this->testDb->createInheritedTables();
148 $this->testDb->createInheritedArsTables(array(
'nomos',
'monk'));
150 $this->testDb->insertData(array(
'pfile',
'upload',
'uploadtree_a',
'users',
'group_user_member',
'agent',
'license_file',
'nomos_ars',
'monk_ars'),
false);
151 $this->testDb->insertData_license_ref();
153 $this->testDb->resetSequenceAsMaxOf(
'agent_agent_pk_seq',
'agent',
'agent_pk');
164 if (preg_match(
"/.*HEART: ([0-9]*).*/", $output, $matches)) {
165 return intval($matches[1]);
182 $this->runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($this->runnerMock);
195 $this->runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($this->runnerCli);
201 private function runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($runner)
208 $licenseRef1 = $this->licenseDao->getLicenseByShortName(
"SPL-1.0")->getRef();
209 $licenseRef2 = $this->licenseDao->getLicenseByShortName(
"Glide")->getRef();
211 $addedLicenses = array($licenseRef1, $licenseRef2);
213 assertThat($addedLicenses, not(arrayContaining(null)));
215 $eventId1 = $this->clearingDao->insertClearingEvent($originallyClearedItemId=23, $userId=2, $groupId=3, $licenseRef1->getId(),
false);
216 $eventId2 = $this->clearingDao->insertClearingEvent($originallyClearedItemId, 5, $groupId, $licenseRef2->getId(),
true);
218 $this->
dbManager->queryOnce(
"UPDATE clearing_event SET job_fk=$jobId");
220 $addedEventIds = array($eventId1, $eventId2);
222 list($success,$output,$retCode) = $runner->run($uploadId=2, $userId, $groupId, $jobId, $args=
"");
224 $this->assertTrue($success,
'cannot run runner');
225 $this->assertEquals($retCode, 0,
'decider failed (did you make test?): '.$output);
229 $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
230 $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
231 assertThat($decisions, is(arrayWithSize(1)));
234 $deciderMadeDecision = $decisions[0];
236 foreach ($deciderMadeDecision->getClearingEvents() as $event) {
237 assertThat($event->getEventId(), is(anyOf($addedEventIds)));
275 $licenseRef1 = $this->licenseDao->getLicenseByShortName(
"SPL-1.0")->getRef();
277 $licId1 = $licenseRef1->getId();
282 $this->
dbManager->queryOnce(
"DELETE FROM license_file");
283 $this->
dbManager->queryOnce(
"INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,$licId1,$pfile,$agentNomosId)");
284 $this->
dbManager->queryOnce(
"INSERT INTO highlight (fl_fk,start,len) VALUES(12222,12,3)");
285 $this->
dbManager->queryOnce(
"INSERT INTO highlight (fl_fk,start,len) VALUES(12222,18,3)");
287 list($success,$output,$retCode) = $runner->run($uploadId=2, $userId=6, $groupId=4, $jobId=31, $args=
"");
289 $this->assertTrue($success,
'cannot run runner');
290 $this->assertEquals($retCode, 0,
'decider failed (did you make test?): '.$output);
294 $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
295 $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
296 assertThat($decisions, is(arrayWithSize(0)));
311 $dbManager = M::mock(DbManager::class);
312 $agentDao = M::mock(AgentDao::class);
313 $clearingDao = M::mock(ClearingDao::class);
314 $uploadDao = M::mock(UploadDao::class);
315 $highlightDao = M::mock(HighlightDao::class);
316 $decisionProcessor = M::mock(ClearingDecisionProcessor::class);
317 $agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
322 $agentDao->shouldReceive(
'arsTableExists')->andReturn(
true);
323 $agentDao->shouldReceive(
'getCurrentAgentId')->andReturn($agentId=24);
324 $agentDao->shouldReceive(
'writeArsRecord')->with(anything(), $agentId, $uploadId)->andReturn($arsId=2);
325 $agentDao->shouldReceive(
'writeArsRecord')->with(anything(), $agentId, $uploadId, $arsId,
true)->andReturn(0);
331 $itemIds = array(4343, 43);
333 $bounds0 = M::mock(ItemTreeBounds::class);
334 $bounds0->shouldReceive(
'getItemId')->andReturn($itemIds[0]);
335 $bounds0->shouldReceive(
'containsFiles')->andReturn(
false);
336 $bounds1 = M::mock(ItemTreeBounds::class);
337 $bounds1->shouldReceive(
'getItemId')->andReturn($itemIds[1]);
338 $bounds1->shouldReceive(
'containsFiles')->andReturn(
false);
339 $bounds = array($bounds0, $bounds1);
341 $uploadDao->shouldReceive(
'getItemTreeBounds')->with($itemIds[0])->andReturn($bounds[0]);
342 $uploadDao->shouldReceive(
'getItemTreeBounds')->with($itemIds[1])->andReturn($bounds[1]);
344 $clearingDao->shouldReceive(
'getEventIdsOfJob')->with($jobId)
345 ->andReturn(array($itemIds[0] => array(), $itemIds[1] => array()));
347 $dbManager->shouldReceive(
'begin')->times(count($itemIds));
348 $dbManager->shouldReceive(
'commit')->times(count($itemIds));
351 $dbManager->shouldReceive(
'prepare');
352 $res = M::Mock(DbManager::class);
353 $dbManager->shouldReceive(
'execute')->andReturn($res);
354 $row1 = array(
'rf_fk' => 2334,
'parent_fk' => 1);
355 $row2 = array(
'rf_fk' => 2333,
'parent_fk' => 1);
356 $dbManager->shouldReceive(
'fetchArray')->with($res)->andReturn($row1, $row2,
false);
357 $dbManager->shouldReceive(
'freeResult')->with($res);
360 $decisionProcessor->shouldReceive(
'hasUnhandledScannerDetectedLicenses')
361 ->with($bounds0, $groupId, array(), anything())->andReturn(
true);
362 $clearingDao->shouldReceive(
'markDecisionAsWip')
363 ->with($itemIds[0], $userId, $groupId);
365 $decisionProcessor->shouldReceive(
'hasUnhandledScannerDetectedLicenses')
366 ->with($bounds1, $groupId, array(), anything())->andReturn(
false);
367 $decisionProcessor->shouldReceive(
'makeDecisionFromLastEvents')
368 ->with($bounds1, $userId, $groupId, DecisionTypes::IDENTIFIED,
false, array());
370 $runner =
new SchedulerTestRunnerMock($dbManager, $agentDao, $clearingDao, $uploadDao, $highlightDao, $decisionProcessor, $agentLicenseEventProcessor);
372 list($success,$output,$retCode) = $runner->run($uploadId, $userId, $groupId, $jobId, $args=
"");
374 $this->assertTrue($success,
'cannot run decider');
375 $this->assertEquals($retCode, 0,
'decider failed: '.$output);
376 assertThat($this->
getHeartCount($output), equalTo(count($itemIds)));
392 $this->runnerDeciderScanWithForceDecision($this->runnerMock);
406 $this->runnerDeciderScanWithForceDecision($this->runnerCli);
412 private function runnerDeciderScanWithForceDecision($runner)
419 $licenseRef1 = $this->licenseDao->getLicenseByShortName(
"SPL-1.0")->getRef();
420 $licenseRef2 = $this->licenseDao->getLicenseByShortName(
"Glide")->getRef();
422 $agentLicId = $this->licenseDao->getLicenseByShortName(
"Adaptec")->getRef()->getId();
424 $addedLicenses = array($licenseRef1, $licenseRef2);
426 assertThat($addedLicenses, not(arrayContaining(null)));
431 $this->
dbManager->queryOnce(
"INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,$agentLicId,$pfile,$agentId)");
433 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($itemId=23);
434 assertThat($this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds), is(not(emptyArray())));
436 $eventId1 = $this->clearingDao->insertClearingEvent($itemId, $userId=2, $groupId=3, $licenseRef1->getId(),
false);
437 $eventId2 = $this->clearingDao->insertClearingEvent($itemId, 5, $groupId, $licenseRef2->getId(),
true);
439 $this->
dbManager->queryOnce(
"UPDATE clearing_event SET job_fk=$jobId");
441 $addedEventIds = array($eventId1, $eventId2);
443 list($success,$output,$retCode) = $runner->run($uploadId=2, $userId, $groupId, $jobId, $args=
"-k1");
445 $this->assertTrue($success,
'cannot run runner');
446 $this->assertEquals($retCode, 0,
'decider failed: '.$output);
449 $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
450 $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
451 assertThat($decisions, is(arrayWithSize(1)));
454 $deciderMadeDecision = $decisions[0];
457 foreach ($deciderMadeDecision->getClearingEvents() as $event) {
458 $eventIds[] = $event->getEventId();
461 assertThat($eventIds, arrayValue($addedEventIds[0]));
462 assertThat($eventIds, arrayValue($addedEventIds[1]));
463 assertThat($eventIds, arrayWithSize(1+count($addedEventIds)));
Namespace for decider job test cases.
testDeciderRealScanWithForceDecision()
testDeciderMockedScanWithTwoEventAndNoAgentShouldMakeADecision()
setUpRepo()
Setup test repository.
testDeciderMockScanWithNoEventsAndOnlyNomosShouldNotMakeADecision()
Utility functions to process ClearingDecision.
Handle events related to license findings.
Functions to process clearing events.
rmRepo()
Destroy test repository.
testDeciderRealScanWithTwoEventAndNoAgentShouldMakeADecision()
getHeartCount($output)
Get the heart count value from the agent output.
tearDown()
Destroy objects, database and repository.
testDeciderScanWithTwoEventAndNoAgentShouldMakeADecision()
setUp()
Setup the objects, database and repository.
testDeciderMockedScanWithForceDecision()
testDeciderRealScanWithNoEventsAndOnlyNomosShouldNotMakeADecision()
fo_dbManager * dbManager
fo_dbManager object
Test interactions between scheduler and agent.
setUpTables()
Create test tables required by agent.
Mock for scheduler inputs.
runnerDeciderScanWithNoEventsAndOnlyNomosShouldNotMakeADecision($runner)
run decider with no events