FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AgentLicenseEventProcessorTest.php
1 <?php
2 /*
3 Copyright (C) 2014-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 
20 
27 use Mockery as M;
28 
29 class AgentLicenseEventProcessorTest extends \PHPUnit\Framework\TestCase
30 {
32  private $licenseDao;
34  private $agentsDao;
36  private $itemTreeBounds;
38  private $agentLicenseEventProcessor;
39  private $dbManagerMock;
40  private $latestScanners = array(array('agent_pk'=>23,'agent_name'=>'nomos'),
41  array('agent_pk'=>22,'agent_name'=>'monk'));
42 
43  protected function setUp()
44  {
45  $this->licenseDao = M::mock(LicenseDao::class);
46  $this->agentsDao = M::mock(AgentDao::class);
47 
48  $this->itemTreeBounds = M::mock(ItemTreeBounds::class);
49 
50  $this->agentLicenseEventProcessor = new AgentLicenseEventProcessor($this->licenseDao, $this->agentsDao);
51 
52  global $container;
53  $this->dbManagerMock = M::mock(DbManager::class);
54  $this->dbManagerMock->shouldReceive('prepare');
55  $this->dbManagerMock->shouldReceive('execute');
56  $this->dbManagerMock->shouldReceive('fetchArray')
57  ->andReturn($this->latestScanners[0],$this->latestScanners[1],false);
58  $this->dbManagerMock->shouldReceive('freeResult');
59  $container = M::mock('ContainerBuilder');
60  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManagerMock);
61  }
62 
63  protected function tearDown()
64  {
65  M::close();
66  }
67 
72  {
73  $uploadId = 2;
74  $nomos = $this->latestScanners[0];
75  $monk = $this->latestScanners[1];
76  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "licA", $nomos['agent_pk'], $nomos['agent_name'], 453, null);
77  list($licenseMatch2, $licenseRef2, $agentRef2) = $this->createLicenseMatch(5, "licA", $monk['agent_pk'], $monk['agent_name'], 665, 95);
78  list($licenseMatch3, $licenseRef3, $agentRef3) = $this->createLicenseMatch(7, "licB", $monk['agent_pk'], $monk['agent_name'], 545, 97);
79  $licenseMatches = array($licenseMatch1, $licenseMatch2, $licenseMatch3);
80 
81  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
82  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
83  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
84  ->andReturn($licenseMatches);
85  $scannerDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenses($this->itemTreeBounds);
86 
87  assertThat($scannerDetectedLicenses, is(array(
88  5 => $licenseRef1,
89  7 => $licenseRef3
90  )));
91  }
92 
93  public function testGetScannerDetectedLicenseDetails()
94  {
95  $uploadId = 2;
96  $licId = 5;
97  $nomos = $this->latestScanners[0];
98  $monk = $this->latestScanners[1];
99  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch($licId, "licA", $nomos['agent_pk'], $nomos['agent_name'], 453, null);
100  list($licenseMatch2, $licenseRef2, $agentRef2) = $this->createLicenseMatch($licId, "licA", $monk['agent_pk'], $monk['agent_name'], 665, 95);
101  $licenseMatches = array($licenseMatch1, $licenseMatch2);
102 
103  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
104  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
105  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
106  ->andReturn($licenseMatches);
107 
108  // $latestAgentDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenseDetails($this->itemTreeBounds);
109  $reflection = new \ReflectionClass($this->agentLicenseEventProcessor);
110  $method = $reflection->getMethod('getScannerDetectedLicenseDetails');
111  $method->setAccessible(true);
112  $latestAgentDetectedLicenses = $method->invoke($this->agentLicenseEventProcessor,$this->itemTreeBounds);
113 
114  assertThat($latestAgentDetectedLicenses, array(
115  'nomos' => array(
116  array('id' => $licId, 'licenseRef' => $licenseRef1, 'agentRef' => $agentRef1, 'matchId' => 453, 'percentage' => null)
117  ),
118  'monk' => array(
119  array('id' => $licId, 'licenseRef' => $licenseRef2, 'agentRef' => $agentRef2, 'matchId' => 665, 'percentage' => 95)
120  )
121  ) );
122  }
123 
124  public function testGetScannerDetectedLicenseDetailsWithUnknownAgent()
125  {
126  $uploadId = 2;
127  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "licA", 23, "nomos", 453, null);
128  list($licenseMatch2, $licenseRef2, $agentRef2) = $this->createLicenseMatch(5, "licA", 22, "unknown", 665, 95);
129  $licenseMatches = array($licenseMatch1, $licenseMatch2);
130 
131  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
132  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
133  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
134  ->andReturn($licenseMatches);
135 
136  // $latestAgentDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenseDetails($this->itemTreeBounds);
137  $reflection = new \ReflectionClass($this->agentLicenseEventProcessor);
138  $method = $reflection->getMethod('getScannerDetectedLicenseDetails');
139  $method->setAccessible(true);
140  $latestAgentDetectedLicenses = $method->invoke($this->agentLicenseEventProcessor,$this->itemTreeBounds);
141 
142  assertThat($latestAgentDetectedLicenses, is(array(
143  5 => array(
144  'nomos' => array(
145  array('id' => 5, 'licenseRef' => $licenseRef1, 'agentRef' => $agentRef1, 'matchId' => 453, 'percentage' => null)
146  )
147  )
148  )));
149  }
150 
151  public function testGetScannerDetectedLicenseDetailsWithOutdatedMatches()
152  {
153  $uploadId = 2;
154  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "licA", 17, "nomos", 453, null);
155  list($licenseMatch2, $licenseRef2, $agentRef2) = $this->createLicenseMatch(5, "licA", 18, "monk", 665, 95);
156  $licenseMatches = array($licenseMatch1, $licenseMatch2);
157 
158  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
159  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
160  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
161  ->andReturn($licenseMatches);
162 
163  // $latestAgentDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenseDetails($this->itemTreeBounds);
164  $reflection = new \ReflectionClass($this->agentLicenseEventProcessor);
165  $method = $reflection->getMethod('getScannerDetectedLicenseDetails');
166  $method->setAccessible(true);
167  $latestAgentDetectedLicenses = $method->invoke($this->agentLicenseEventProcessor,$this->itemTreeBounds);
168 
169  assertThat($latestAgentDetectedLicenses, is(array()));
170  }
171 
172  public function testGetScannerDetectedLicenseDetailsNoLicenseFoundShouldBeSkipped()
173  {
174  $uploadId = 2;
175  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "No_license_found", 23, "nomos", 453, null);
176  $licenseMatches = array($licenseMatch1);
177 
178  $this->itemTreeBounds->shouldReceive('getUploadId')->withNoArgs()->andReturn($uploadId);
179  $this->licenseDao->shouldReceive('getAgentFileLicenseMatches')->once()
180  ->withArgs(array($this->itemTreeBounds,LicenseMap::TRIVIAL))
181  ->andReturn($licenseMatches);
182 
183  // $latestAgentDetectedLicenses = $this->agentLicenseEventProcessor->getScannerDetectedLicenseDetails($this->itemTreeBounds);
184  $reflection = new \ReflectionClass($this->agentLicenseEventProcessor);
185  $method = $reflection->getMethod('getScannerDetectedLicenseDetails');
186  $method->setAccessible(true);
187  $latestAgentDetectedLicenses = $method->invoke($this->agentLicenseEventProcessor,$this->itemTreeBounds);
188 
189  assertThat($latestAgentDetectedLicenses, is(array()));
190  }
191 
195  protected function createLicenseMatch($licenseId, $licenseShortName, $agentId, $agentName, $matchId, $percentage)
196  {
197  $licenseRef = M::mock(LicenseRef::class);
198  $licenseRef->shouldReceive("getId")->withNoArgs()->andReturn($licenseId);
199  $licenseRef->shouldReceive("getShortName")->withNoArgs()->andReturn($licenseShortName);
200 
201  $agentRef = M::mock(LicenseRef::class);
202  $agentRef->shouldReceive("getAgentId")->withNoArgs()->andReturn($agentId);
203  $agentRef->shouldReceive("getAgentName")->withNoArgs()->andReturn($agentName);
204  $agentRef->shouldReceive("getAgentName")->withNoArgs()->andReturn($agentName);
205 
206  $licenseMatch = M::mock(LicenseMatch::class);
207  $licenseMatch->shouldReceive("getLicenseRef")->withNoArgs()->andReturn($licenseRef);
208  $licenseMatch->shouldReceive("getAgentRef")->withNoArgs()->andReturn($agentRef);
209  $licenseMatch->shouldReceive("getLicenseFileId")->withNoArgs()->andReturn($matchId);
210  $licenseMatch->shouldReceive("getPercentage")->withNoArgs()->andReturn($percentage);
211  return array($licenseMatch, $licenseRef, $agentRef);
212  }
213 
214  public function testGetScannedLicenses()
215  {
217  list($licenseMatch1, $licenseRef1, $agentRef1) = $this->createLicenseMatch(5, "licA", 23, "nomos", 453, null);
218 
219  $details = array(
220  5 => array(
221  'nomos' => array(
222  array('id' => 5, 'licenseRef' => $licenseRef1, 'agentRef' => $agentRef1, 'matchId' => 453, 'percentage' => null)
223  )
224  )
225  );
226 
227  $result = $this->agentLicenseEventProcessor->getScannedLicenses($details);
228 
229  assertThat($result, is(array($licenseRef1->getId() => $licenseRef1)));
230  }
231 
232  public function testGetScannedLicensesWithEmptyDetails()
233  {
234  assertThat($this->agentLicenseEventProcessor->getScannedLicenses(array()), is(emptyArray()));
235  }
236 }
Contains business rules for FOSSology.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
createLicenseMatch($licenseId, $licenseShortName, $agentId, $agentName, $matchId, $percentage)