FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ScanJobProxyTest.php
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Proxy;
20 
23 use Mockery as M;
24 
25 class ScanJobProxyTest extends \PHPUnit\Framework\TestCase
26 {
27  private $agentDaoMock;
28  private $uploadId = 23;
29  private $agentId = 601;
30  private $agentName = 'scanMe';
32  private $scanJobProxy;
33 
34  protected function setUp()
35  {
36  $this->agentDaoMock = M::mock(AgentDao::class);
37  $this->scanJobProxy = new ScanJobProxy($this->agentDaoMock,$this->uploadId);
38  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
39  }
40 
41  protected function tearDown()
42  {
43  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
44  M::close();
45  }
46 
47  private function prepareScanAgentStatus()
48  {
49  $reflection = new \ReflectionClass(get_class($this->scanJobProxy));
50  $method = $reflection->getMethod('scanAgentStatus');
51  $method->setAccessible(true);
52 
53  return $method;
54  }
55 
56  public function testScanAgentStatus()
57  {
58  $method = $this->prepareScanAgentStatus();
59  $successfulAgents = array(array('agent_id'=>$this->agentId,'agent_rev'=>'a0815','agent_name'=>$this->agentName));
60  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
61  ->andReturn($successfulAgents);
62  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->never();
63  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
64  ->andReturn(new AgentRef($this->agentId, $this->agentName, 'a0815'));
65 
66  $vars = $method->invoke($this->scanJobProxy,$this->agentName);
67  assertThat($vars, is(array(
68  'successfulAgents'=>$successfulAgents,
69  'uploadId'=>$this->uploadId,
70  'agentName'=>$this->agentName,
71  'currentAgentId'=>$this->agentId,
72  'currentAgentRev'=>'a0815'
73  )));
74  }
75 
76 
77  public function testScanAgentStatusLatestStillRuns()
78  {
79  $method = $this->prepareScanAgentStatus();
80  $successfulAgents = array(array('agent_id'=>$this->agentId,'agent_rev'=>'a0815','agent_name'=>$this->agentName));
81  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
82  ->andReturn($successfulAgents);
83  $runningAgentId = $this->agentId+1;
84  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->with($this->uploadId, $this->agentName)
85  ->andReturn(array($runningAgentId))->once();
86  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
87  ->andReturn(new AgentRef($runningAgentId, $this->agentName, 'b1234'));
88 
89  $vars = $method->invoke($this->scanJobProxy,$this->agentName);
90  assertThat($vars, is(array(
91  'successfulAgents'=>$successfulAgents,
92  'uploadId'=>$this->uploadId,
93  'agentName'=>$this->agentName,
94  'isAgentRunning'=>true,
95  'currentAgentId'=>$runningAgentId,
96  'currentAgentRev'=>'b1234'
97  )));
98  }
99 
100 
101  public function testScanAgentStatusLatestNotRuns()
102  {
103  $method = $this->prepareScanAgentStatus();
104  $successfulAgents = array(array('agent_id'=>$this->agentId,'agent_rev'=>'a0815','agent_name'=>$this->agentName));
105  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
106  ->andReturn($successfulAgents);
107  $runningAgentId = $this->agentId+1;
108  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->with($this->uploadId, $this->agentName)
109  ->andReturn(array())->once();
110  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
111  ->andReturn(new AgentRef($runningAgentId, $this->agentName, 'b1234'));
112 
113  $vars = $method->invoke($this->scanJobProxy,$this->agentName);
114  assertThat($vars, is(array(
115  'successfulAgents'=>$successfulAgents,
116  'uploadId'=>$this->uploadId,
117  'agentName'=>$this->agentName,
118  'isAgentRunning'=>false,
119  'currentAgentId'=>$runningAgentId,
120  'currentAgentRev'=>'b1234'
121  )));
122  }
123 
124 
125  public function testScanAgentStatusWithoutSuccess()
126  {
127  $method = $this->prepareScanAgentStatus();
128  $successfulAgents = array();
129  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
130  ->andReturn($successfulAgents);
131  $runningAgentId = $this->agentId+1;
132  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->with($this->uploadId, $this->agentName)
133  ->andReturn(array($runningAgentId))->once();
134  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
135  ->andReturn(new AgentRef($runningAgentId, $this->agentName, 'b1234'));
136 
137  $vars = $method->invoke($this->scanJobProxy,$this->agentName);
138  assertThat($vars, is(array(
139  'successfulAgents'=>$successfulAgents,
140  'uploadId'=>$this->uploadId,
141  'agentName'=>$this->agentName,
142  'isAgentRunning'=>true
143  )));
144  }
145 
146 
147  public function testCreateAgentStatus()
148  {
149  $successfulAgents = array(array('agent_id'=>$this->agentId,'agent_rev'=>'a0815','agent_name'=>$this->agentName));
150  $this->agentDaoMock->shouldReceive('getSuccessfulAgentEntries')->with($this->agentName, $this->uploadId)
151  ->andReturn($successfulAgents);
152  $this->agentDaoMock->shouldReceive('getRunningAgentIds')->never();
153  $this->agentDaoMock->shouldReceive('getCurrentAgentRef')->with($this->agentName)
154  ->andReturn(new AgentRef($this->agentId, $this->agentName, 'a0815'));
155  $fakedAgentName = 'ghost';
156  $this->agentDaoMock->shouldReceive('arsTableExists')->with(M::anyOf($this->agentName,$fakedAgentName))->andReturn(true,false)->twice();
157 
158  $vars = $this->scanJobProxy->createAgentStatus(array($this->agentName,$fakedAgentName));
159  assertThat($vars, is(array(array(
160  'successfulAgents'=>$successfulAgents,
161  'uploadId'=>$this->uploadId,
162  'agentName'=>$this->agentName,
163  'currentAgentId'=>$this->agentId,
164  'currentAgentRev'=>'a0815'
165  ))));
166  }
167 
168 
169  private function pretendScanAgentStatus($successfulAgents)
170  {
171  $reflection = new \ReflectionObject($this->scanJobProxy);
172  $prop = $reflection->getProperty('successfulScanners');
173  $prop->setAccessible(true);
174  $prop->setValue($this->scanJobProxy, $successfulAgents);
175  }
176 
177  public function testGetAgentMap()
178  {
179  $successfulAgents = array($this->agentName=>array(new AgentRef($this->agentId, $this->agentName, 'a0815')));
180  $this->pretendScanAgentStatus($successfulAgents);
181 
182  $expected = array($this->agentId=>"$this->agentName a0815");
183  $map = $this->scanJobProxy->getAgentMap();
184  assertThat($map,is(equalTo($expected)));
185  }
186 
187 
188  public function testGetLatestSuccessfulAgentIds()
189  {
190  $otherAgentName = 'drinkMe';
191  $otherAgentId = 603;
192  $successfulAgents = array($this->agentName=>array(new AgentRef($this->agentId, $this->agentName, 'a0815'),
193  new AgentRef($this->agentId-1, $this->agentName, 'beforeA0815')),
194  $otherAgentName=>array(new AgentRef($otherAgentId, $otherAgentName, 'coffee')));
195  $this->pretendScanAgentStatus($successfulAgents);
196 
197  $expected = array($this->agentName=>$this->agentId,$otherAgentName=>$otherAgentId);
198  $ids = $this->scanJobProxy->getLatestSuccessfulAgentIds();
199  assertThat($ids,is(equalTo($expected)));
200  }
201 
202  public function testGetSuccessfulAgents()
203  {
204  $otherAgentName = 'drinkMe';
205  $otherAgentId = 603;
206  $successfulAgents = array($this->agentName=>array(new AgentRef($this->agentId, $this->agentName, 'a0815'),
207  new AgentRef($this->agentId-1, $this->agentName, 'beforeA0815')),
208  $otherAgentName=>array(new AgentRef($otherAgentId, $otherAgentName, 'coffee')));
209  $this->pretendScanAgentStatus($successfulAgents);
210 
211  $expected = array_merge($successfulAgents[$this->agentName],$successfulAgents[$otherAgentName]);
212  $ids = $this->scanJobProxy->getSuccessfulAgents();
213  assertThat($ids,is(equalTo($expected)));
214  }
215 }