Commit cca62613 authored by François Agneray's avatar François Agneray
Browse files

DatasetListByInstanceAction => done

parent a47d2e48
......@@ -95,6 +95,10 @@ $container->set('App\Action\DatasetFamilyListAction', function (ContainerInterfa
return new App\Action\DatasetFamilyListAction($c->get('em'));
});
$container->set('App\Action\DatasetListByInstanceAction', function (ContainerInterface $c) {
return new App\Action\DatasetListByInstanceAction($c->get('em'));
});
$container->set('App\Action\DatasetListAction', function (ContainerInterface $c) {
return new App\Action\DatasetListAction($c->get('em'), new App\Utils\DBALConnectionFactory());
});
......
......@@ -21,7 +21,7 @@ $app->map([OPTIONS, GET, PUT, DELETE], '/project/{name}', App\Action\ProjectActi
$app->map([OPTIONS, GET, POST], '/instance', App\Action\InstanceListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/instance/{name}', App\Action\InstanceAction::class);
$app->map([OPTIONS, GET, POST], '/instance/{name}/dataset-family', App\Action\DatasetFamilyListAction::class);
$app->map([OPTIONS, GET], '/instance/{name}/dataset', App\Action\InstanceAction::class);
$app->map([OPTIONS, GET], '/instance/{name}/dataset', App\Action\DatasetListByInstanceAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/dataset-family/{id}', App\Action\DatasetFamilyAction::class);
$app->map([OPTIONS, GET, POST], '/dataset-family/{id}/dataset', App\Action\DatasetListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/dataset/{name}', App\Action\DatasetAction::class);
......
......@@ -23,6 +23,7 @@ final class DatasetFamilyListAction extends AbstractAction
{
/**
* `GET` Returns a list of all dataset family for a given instance
* `POST` Add a new dataset family to a given instance
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
......
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpNotFoundException;
final class DatasetListByInstanceAction extends AbstractAction
{
/**
* `GET` Returns a list of all datasets for a given instance
*
* @param ServerRequestInterface $request PSR-7 This object represents the HTTP request
* @param ResponseInterface $response PSR-7 This object represents the HTTP response
* @param string[] $args This table contains information transmitted in the URL (see routes.php)
*
* @return ResponseInterface
*/
public function __invoke(Request $request, Response $response, array $args): Response
{
if ($request->getMethod() === OPTIONS) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
}
$instance = $this->em->find('App\Entity\Instance', $args['name']);
// Returns HTTP 404 if the dataset is not found
if (is_null($instance)) {
throw new HttpNotFoundException(
$request,
'Instance with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$qb = $this->em->createQueryBuilder();
$qb->select('d')
->from('App\Entity\Dataset', 'd')
->join('d.datasetFamily', 'f')
->where($qb->expr()->eq('IDENTITY(f.instance)', ':instanceName'))
->setParameter('instanceName', $instance->getName());
$datasets = $qb->getQuery()->getResult();
$payload = json_encode($datasets);
}
$response->getBody()->write($payload);
return $response;
}
}
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Tests\Action;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use App\tests\EntityManagerBuilder;
use App\Entity\Database;
use App\Entity\Project;
use App\Entity\Instance;
use App\Entity\DatasetFamily;
use App\Entity\Dataset;
final class DatasetListByInstanceActionTest extends TestCase
{
private $action;
private $entityManager;
protected function setUp(): void
{
$this->entityManager = EntityManagerBuilder::getInstance();
$this->action = new \App\Action\DatasetListByInstanceAction($this->entityManager);
}
public function testOptionsHttpMethod(): void
{
$request = $this->getRequest('OPTIONS');
$response = ($this->action)($request, new Response(), array());
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, OPTIONS');
}
public function testInstanceIsNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Instance with name default is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertEquals(404, (int) $response->getStatusCode());
}
public function testGetAllDatasetsForAnInstance(): void
{
$datasets = $this->addDatasets();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'default'));
$this->assertSame(
json_encode($datasets),
(string) $response->getBody()
);
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->close();
}
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/instance/default/dataset', array(
'Content-Type' => 'application/json'
));
}
private function addInstance(): Instance
{
$instance = new Instance('default', 'Default instance');
$instance->setClientUrl('http://anis.lam.fr');
$this->entityManager->persist($instance);
$this->entityManager->flush();
return $instance;
}
private function addDatasetFamily(): DatasetFamily
{
$instance = $this->addInstance();
$family = new DatasetFamily($instance);
$family->setLabel('Default dataset family');
$family->setDisplay(10);
$this->entityManager->persist($family);
return $family;
}
private function addProject(): Project
{
$database = new Database();
$database->setLabel('Test1');
$database->setDbName('test1');
$database->setType('pgsql');
$database->setHost('db');
$database->setPort(5432);
$database->setLogin('test');
$database->setPassword('test');
$this->entityManager->persist($database);
$project = new Project('anis_project');
$project->setLabel('Test project');
$project->setDescription('Test description');
$project->setLink('http://test.com');
$project->setManager('User1');
$project->setDatabase($database);
$this->entityManager->persist($project);
return $project;
}
private function addDatasets(): array
{
$project = $this->addProject();
$family = $this->addDatasetFamily();
$dataset = new Dataset('obs_cat');
$dataset->setTableRef('v_obs_cat');
$dataset->setLabel('Obscat label');
$dataset->setDescription('Obscat description');
$dataset->setDisplay(10);
$dataset->setCount(10000);
$dataset->setVo(false);
$dataset->setDataPath('/mnt/obs_cat');
$dataset->setSelectableRow(false);
$dataset->setProject($project);
$dataset->setDatasetFamily($family);
$this->entityManager->persist($dataset);
$dataset2 = new Dataset('observations');
$dataset2->setTableRef('v_observations');
$dataset2->setLabel('Observations label');
$dataset2->setDescription('Observations description');
$dataset2->setDisplay(20);
$dataset2->setCount(5000);
$dataset2->setVo(false);
$dataset2->setDataPath('/mnt/observations');
$dataset2->setSelectableRow(false);
$dataset2->setProject($project);
$dataset2->setDatasetFamily($family);
$this->entityManager->persist($dataset2);
$this->entityManager->flush();
return array($dataset, $dataset2);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment