FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ScanJobProxyTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Proxy;
9 
12 use Mockery as M;
13 
14 class ScanJobProxyTest extends \PHPUnit\Framework\TestCase
15 {
16  private $agentDaoMock;
17  private $uploadId = 23;
18  private $agentId = 601;
19  private $agentName = 'scanMe';
21  private $scanJobProxy;
22 
23  protected function setUp() : void
24  {
25  $this->agentDaoMock = M::mock(AgentDao::class);
26  $this->scanJobProxy = new ScanJobProxy($this->agentDaoMock,$this->uploadId);
27  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
28  }
29 
30  protected function tearDown() : void
31  {
32  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
33  M::close();
34  }
35 
36  private function prepareScanAgentStatus()
37  {
38  $reflection = new \ReflectionClass(get_class($this->scanJobProxy));
39  $method = $reflection->getMethod('scanAgentStatus');
40  $method->setAccessible(true);
41 
42  return $method;
43  }
44 
45  public function testScanAgentStatus()
46  {
47  $method = $this->prepareScanAgentStatus();
48  $successfulAgents = array(array('agent_id'=>$this->agentId,'agent_rev'=>'a0815','agent_name'=>$this->agentName));
49  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
50  ->andReturn($successfulAgents);
51  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->never();
52  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
53  ->andReturn(new AgentRef($this->agentId, $this->agentName, 'a0815'));
54 
55  $vars = $method->invoke($this->scanJobProxy,$this->agentName);
56  assertThat($vars, is(array(
57  'successfulAgents'=>$successfulAgents,
58  'uploadId'=>$this->uploadId,
59  'agentName'=>$this->agentName,
60  'currentAgentId'=>$this->agentId,
61  'currentAgentRev'=>'a0815'
62  )));
63  }
64 
65 
66  public function testScanAgentStatusLatestStillRuns()
67  {
68  $method = $this->prepareScanAgentStatus();
69  $successfulAgents = array(array('agent_id'=>$this->agentId,'agent_rev'=>'a0815','agent_name'=>$this->agentName));
70  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
71  ->andReturn($successfulAgents);
72  $runningAgentId = $this->agentId+1;
73  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->with($this->uploadId, $this->agentName)
74  ->andReturn(array($runningAgentId))->once();
75  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
76  ->andReturn(new AgentRef($runningAgentId, $this->agentName, 'b1234'));
77 
78  $vars = $method->invoke($this->scanJobProxy,$this->agentName);
79  assertThat($vars, is(array(
80  'successfulAgents'=>$successfulAgents,
81  'uploadId'=>$this->uploadId,
82  'agentName'=>$this->agentName,
83  'isAgentRunning'=>true,
84  'currentAgentId'=>$runningAgentId,
85  'currentAgentRev'=>'b1234'
86  )));
87  }
88 
89 
90  public function testScanAgentStatusLatestNotRuns()
91  {
92  $method = $this->prepareScanAgentStatus();
93  $successfulAgents = array(array('agent_id'=>$this->agentId,'agent_rev'=>'a0815','agent_name'=>$this->agentName));
94  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
95  ->andReturn($successfulAgents);
96  $runningAgentId = $this->agentId+1;
97  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->with($this->uploadId, $this->agentName)
98  ->andReturn(array())->once();
99  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
100  ->andReturn(new AgentRef($runningAgentId, $this->agentName, 'b1234'));
101 
102  $vars = $method->invoke($this->scanJobProxy,$this->agentName);
103  assertThat($vars, is(array(
104  'successfulAgents'=>$successfulAgents,
105  'uploadId'=>$this->uploadId,
106  'agentName'=>$this->agentName,
107  'isAgentRunning'=>false,
108  'currentAgentId'=>$runningAgentId,
109  'currentAgentRev'=>'b1234'
110  )));
111  }
112 
113 
114  public function testScanAgentStatusWithoutSuccess()
115  {
116  $method = $this->prepareScanAgentStatus();
117  $successfulAgents = array();
118  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
119  ->andReturn($successfulAgents);
120  $runningAgentId = $this->agentId+1;
121  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->with($this->uploadId, $this->agentName)
122  ->andReturn(array($runningAgentId))->once();
123  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
124  ->andReturn(new AgentRef($runningAgentId, $this->agentName, 'b1234'));
125 
126  $vars = $method->invoke($this->scanJobProxy,$this->agentName);
127  assertThat($vars, is(array(
128  'successfulAgents'=>$successfulAgents,
129  'uploadId'=>$this->uploadId,
130  'agentName'=>$this->agentName,
131  'isAgentRunning'=>true
132  )));
133  }
134 
135 
136  public function testCreateAgentStatus()
137  {
138  $successfulAgents = array(array('agent_id'=>$this->agentId,'agent_rev'=>'a0815','agent_name'=>$this->agentName));
139  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
140  ->andReturn($successfulAgents);
141  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->never();
142  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
143  ->andReturn(new AgentRef($this->agentId, $this->agentName, 'a0815'));
144  $fakedAgentName = 'ghost';
145  $this->agentDaoMock->shouldReceive('arsTableExists')->with(M::anyOf($this->agentName,$fakedAgentName))->andReturn(true,false)->twice();
146 
147  $vars = $this->scanJobProxy->createAgentStatus(array($this->agentName,$fakedAgentName));
148  assertThat($vars, is(array(array(
149  'successfulAgents'=>$successfulAgents,
150  'uploadId'=>$this->uploadId,
151  'agentName'=>$this->agentName,
152  'currentAgentId'=>$this->agentId,
153  'currentAgentRev'=>'a0815'
154  ))));
155  }
156 
157 
158  private function pretendScanAgentStatus($successfulAgents)
159  {
160  $reflection = new \ReflectionObject($this->scanJobProxy);
161  $prop = $reflection->getProperty('successfulScanners');
162  $prop->setAccessible(true);
163  $prop->setValue($this->scanJobProxy, $successfulAgents);
164  }
165 
166  public function testGetAgentMap()
167  {
168  $successfulAgents = array($this->agentName=>array(new AgentRef($this->agentId, $this->agentName, 'a0815')));
169  $this->pretendScanAgentStatus($successfulAgents);
170 
171  $expected = array($this->agentId=>"$this->agentName a0815");
172  $map = $this->scanJobProxy->getAgentMap();
173  assertThat($map,is(equalTo($expected)));
174  }
175 
176 
177  public function testGetLatestSuccessfulAgentIds()
178  {
179  $otherAgentName = 'drinkMe';
180  $otherAgentId = 603;
181  $successfulAgents = array($this->agentName=>array(new AgentRef($this->agentId, $this->agentName, 'a0815'),
182  new AgentRef($this->agentId-1, $this->agentName, 'beforeA0815')),
183  $otherAgentName=>array(new AgentRef($otherAgentId, $otherAgentName, 'coffee')));
184  $this->pretendScanAgentStatus($successfulAgents);
185 
186  $expected = array($this->agentName=>$this->agentId,$otherAgentName=>$otherAgentId);
187  $ids = $this->scanJobProxy->getLatestSuccessfulAgentIds();
188  assertThat($ids,is(equalTo($expected)));
189  }
190 
191  public function testGetSuccessfulAgents()
192  {
193  $otherAgentName = 'drinkMe';
194  $otherAgentId = 603;
195  $successfulAgents = array($this->agentName=>array(new AgentRef($this->agentId, $this->agentName, 'a0815'),
196  new AgentRef($this->agentId-1, $this->agentName, 'beforeA0815')),
197  $otherAgentName=>array(new AgentRef($otherAgentId, $otherAgentName, 'coffee')));
198  $this->pretendScanAgentStatus($successfulAgents);
199 
200  $expected = array_merge($successfulAgents[$this->agentName],$successfulAgents[$otherAgentName]);
201  $ids = $this->scanJobProxy->getSuccessfulAgents();
202  assertThat($ids,is(equalTo($expected)));
203  }
204 }