29 use Slim\Psr7\Factory\StreamFactory;
30 use Slim\Psr7\Headers;
31 use Slim\Psr7\Request;
33 use Symfony\Component\HttpFoundation\BinaryFileResponse;
34 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
144 $container = M::mock(
'Psr\Container\ContainerInterface');
145 $this->dbHelper = M::mock(DbHelper::class);
146 $this->
dbManager = M::mock(DbManager::class);
147 $this->restHelper = M::mock(RestHelper::class);
148 $this->uploadDao = M::mock(UploadDao::class);
149 $this->spdxPlugin = M::mock(
'SpdxTwoGeneratorUi');
150 $this->readmeossPlugin = M::mock(
'ReadMeOssPlugin');
151 $this->clixmlPlugin = M::mock(
'CliXmlGeneratorUi');
152 $this->unifiedPlugin = M::mock(
'FoUnifiedReportGenerator');
153 $this->decisionExporterPlugin = M::mock(
'DecisionExporterAgentPlugin');
154 $this->downloadPlugin = M::mock(
'ui_download');
156 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
158 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
159 $this->restHelper->shouldReceive(
'getUploadDao')
160 ->andReturn($this->uploadDao);
161 $this->restHelper->shouldReceive(
'getGroupId')->andReturn($this->groupId);
162 $this->restHelper->shouldReceive(
'getPlugin')
163 ->withArgs(array(
'ui_spdx2'))->andReturn($this->spdxPlugin);
164 $this->restHelper->shouldReceive(
'getPlugin')
165 ->withArgs(array(
'ui_readmeoss'))->andReturn($this->readmeossPlugin);
166 $this->restHelper->shouldReceive(
'getPlugin')
167 ->withArgs(array(
'ui_clixml'))->andReturn($this->clixmlPlugin);
168 $this->restHelper->shouldReceive(
'getPlugin')
169 ->withArgs(array(
'download'))->andReturn($this->downloadPlugin);
170 $this->restHelper->shouldReceive(
'getPlugin')
171 ->withArgs(array(
'agent_founifiedreport'))
172 ->andReturn($this->unifiedPlugin);
173 $this->restHelper->shouldReceive(
'getPlugin')
174 ->withArgs([
'agent_fodecisionexporter'])->andReturn($this->decisionExporterPlugin);
176 $container->shouldReceive(
'get')->withArgs(array(
177 'helper.restHelper'))->andReturn($this->restHelper);
179 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
180 $this->streamFactory =
new StreamFactory();
189 $this->addToAssertionCount(
190 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
202 $response->getBody()->seek(0);
203 return json_decode($response->getBody()->getContents(),
true);
215 $treeTableName =
"uploadtree_a";
219 $filename =
"top$id";
220 $timestamp =
"01-01-2020";
223 $filename =
"child$id";
224 $timestamp =
"02-01-2020";
227 $filename =
"child$id";
228 $timestamp =
"03-01-2020";
233 return new Upload($id, $filename, $description, $treeTableName, $timestamp);
244 $GLOBALS[
"apiBasePath"] =
"/repo/api/v1";
245 $requestHeaders =
new Headers();
246 $requestHeaders->setHeader(
'uploadId', $uploadId);
247 $requestHeaders->setHeader(
'reportFormat', $reportFormat);
248 $body = $this->streamFactory->createStream();
249 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost", 80,
250 "/repo/api/v1/report"), $requestHeaders, [], [], $body);
252 return $this->reportController->getReport($request, $response, []);
265 $this->uploadDao->shouldReceive(
'isAccessible')->withArgs([$uploadId,
266 $this->groupId])->andReturn(
true);
267 $this->uploadDao->shouldReceive(
'getUpload')->withArgs([$uploadId])
268 ->andReturn($upload);
269 $this->spdxPlugin->shouldReceive(
'scheduleAgent')
270 ->withArgs([$this->groupId, $upload, M::anyOf($this->reportsAllowed[0],
271 $this->reportsAllowed[1], $this->reportsAllowed[2])])
272 ->andReturn([32, 33,
""]);
273 $this->readmeossPlugin->shouldReceive(
'scheduleAgent')
274 ->withArgs([$this->groupId, $upload])->andReturn([32, 33,
""]);
275 $this->unifiedPlugin->shouldReceive(
'scheduleAgent')
276 ->withArgs([$this->groupId, $upload])->andReturn([32, 33,
""]);
277 $this->clixmlPlugin->shouldReceive(
'scheduleAgent')
278 ->withArgs([$this->groupId, $upload])->andReturn([32, 33,
""]);
279 $this->decisionExporterPlugin->shouldReceive(
'scheduleAgent')
280 ->withArgs([$this->groupId, $upload])->andReturn([32, 33]);
282 $expectedResponse =
new Info(201,
"http://localhost/repo/api/v1/report/32",
285 foreach ($this->reportsAllowed as $reportFormat) {
287 $this->assertEquals($expectedResponse->getArray(),
288 $this->getResponseJson($actualResponse));
289 $this->assertEquals($expectedResponse->getCode(),
290 $actualResponse->getStatusCode());
302 $reportFormat =
'report';
304 $this->expectException(HttpBadRequestException::class);
317 $reportFormat = $this->reportsAllowed[1];
319 $this->uploadDao->shouldReceive(
'isAccessible')->withArgs([$uploadId,
320 $this->groupId])->andReturn(
false);
322 $this->expectException(HttpForbiddenException::class);
335 $reportFormat = $this->reportsAllowed[1];
338 $this->uploadDao->shouldReceive(
'isAccessible')->withArgs([$uploadId,
339 $this->groupId])->andReturn(
true);
340 $this->uploadDao->shouldReceive(
'getUpload')->withArgs([$uploadId])
341 ->andReturn($upload);
343 $this->expectException(HttpNotFoundException::class);
362 $this->
dbManager->shouldReceive(
'getSingleRow')
363 ->withArgs([
'SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
364 [$reportId],
"reportValidity"])
365 ->andReturn([
"jq_type" => $this->reportsAllowed[1]]);
366 $this->
dbManager->shouldReceive(
'getSingleRow')
367 ->withArgs([
'SELECT job_upload_fk FROM job WHERE job_pk = $1',
368 [$reportId],
"reportFileUpload"])
369 ->andReturn([
"job_upload_fk" => $uploadId]);
370 $this->uploadDao->shouldReceive(
'isAccessible')->withArgs([$uploadId,
371 $this->groupId])->andReturn(
true);
372 $this->
dbManager->shouldReceive(
'getSingleRow')
373 ->withArgs([
'SELECT * FROM reportgen WHERE job_fk = $1',
374 [$reportId],
"reportFileName"])
375 ->andReturn([
"job_upload_fk" => $uploadId]);
377 $tmpfile = tempnam(sys_get_temp_dir(),
"FOO");
379 $handle = fopen($tmpfile,
"w");
380 fwrite($handle,
"writing to tempfile");
383 $fileResponse =
new BinaryFileResponse($tmpfile);
384 $fileResponse->headers->set(
'Content-Type',
'text/plain');
385 $fileResponse->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
386 $fileContent = $fileResponse->getFile();
387 $this->downloadPlugin->shouldReceive(
'getReport')->andReturn($fileResponse);
390 $expectedResponse = $expectedResponse->withHeader(
'Content-Description',
392 ->withHeader(
'Content-Type', $fileResponse->headers->get(
'Content-Type'))
393 ->withHeader(
'Content-Disposition',
394 $fileResponse->headers->get(
'Content-Disposition'))
395 ->withHeader(
'Cache-Control',
'must-revalidate')
396 ->withHeader(
'Pragma',
'private')
397 ->withHeader(
'Content-Length', filesize($fileContent));
399 $actualResponse = $this->reportController->downloadReport(
null,
402 $expectedResponse->getBody()->seek(0);
403 $this->assertEquals(file_get_contents($tmpfile),
404 $actualResponse->getBody()->getContents());
405 $this->assertEquals($expectedResponse->getHeaders(),
406 $actualResponse->getHeaders());
420 $this->
dbManager->shouldReceive(
'getSingleRow')
421 ->withArgs([
'SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
422 [$reportId],
"reportValidity"])
423 ->andReturn([
"jq_type" => $this->reportsAllowed[1]]);
424 $this->
dbManager->shouldReceive(
'getSingleRow')
425 ->withArgs([
'SELECT job_upload_fk FROM job WHERE job_pk = $1',
426 [$reportId],
"reportFileUpload"])
427 ->andReturn([
"job_upload_fk" => $uploadId]);
428 $this->uploadDao->shouldReceive(
'isAccessible')->withArgs([$uploadId,
429 $this->groupId])->andReturn(
false);
431 $this->expectException(HttpForbiddenException::class);
433 $this->reportController->downloadReport(
null,
new ResponseHelper(),
434 [
"id" => $reportId]);
446 $this->
dbManager->shouldReceive(
'getSingleRow')
447 ->withArgs([
'SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
448 [$reportId],
"reportValidity"])
449 ->andReturn([
"jq_type" =>
""]);
451 $this->expectException(HttpNotFoundException::class);
453 $this->reportController->downloadReport(
null,
new ResponseHelper(),
454 [
"id" => $reportId]);
467 $this->
dbManager->shouldReceive(
'getSingleRow')
468 ->withArgs([
'SELECT jq_type FROM jobqueue WHERE jq_job_fk = $1',
469 [$reportId],
"reportValidity"])
470 ->andReturn([
"jq_type" => $this->reportsAllowed[1]]);
471 $this->
dbManager->shouldReceive(
'getSingleRow')
472 ->withArgs([
'SELECT job_upload_fk FROM job WHERE job_pk = $1',
473 [$reportId],
"reportFileUpload"])
474 ->andReturn([
"job_upload_fk" => $uploadId]);
475 $this->uploadDao->shouldReceive(
'isAccessible')->withArgs([$uploadId,
476 $this->groupId])->andReturn(
true);
477 $this->
dbManager->shouldReceive(
'getSingleRow')
478 ->withArgs([
'SELECT * FROM reportgen WHERE job_fk = $1',
479 [$reportId],
"reportFileName"])
482 $this->expectException(HttpServiceUnavailableException::class);
484 $this->reportController->downloadReport(
null,
new ResponseHelper(),
485 [
"id" => $reportId]);
Controller for Maintenance model.
Provides helper methods to access database for REST api.
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Different type of infos provided by REST.
Info model to contain general error and return values.
Tests for ReportController.
testGetReportAllFormats()
testDownloadReportInAccessibleUpload()
getResponseJson($response)
setUp()
Setup test objects.
getResponseForReport($uploadId, $reportFormat)
testDownloadReportTryLater()
testGetReportInaccessibleUpload()
tearDown()
Remove test objects.
testGetReportInvalidUpload()
testDownloadReportInvalidUpload()
testGetReportInvalidFormat()
fo_dbManager * dbManager
fo_dbManager object