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

Modifying dataset route to adapt instance feature

parent 4d2bae16
......@@ -24,7 +24,7 @@ $app->map([OPTIONS, GET, POST], '/output-category', App\Action\OutputCategoryLis
$app->map([OPTIONS, GET, PUT, DELETE], '/output-category/{id}', App\Action\OutputCategoryAction::class);
$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], '/dataset', App\Action\DatasetListAction::class);
$app->map([OPTIONS, GET, POST], '/instance/{name}/dataset', App\Action\DatasetListAction::class);
$app->map([OPTIONS, GET, PUT, DELETE], '/dataset/{name}', App\Action\DatasetAction::class);
$app->map([OPTIONS, GET], '/dataset/{name}/attribute', App\Action\AttributeListAction::class);
$app->map([OPTIONS, GET, PUT], '/dataset/{name}/attribute/{id}', App\Action\AttributeAction::class);
......
......@@ -15,6 +15,7 @@ namespace App\Action;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use Doctrine\ORM\EntityManagerInterface;
use App\Utils\DBALConnectionFactory;
use App\Entity\Database;
......@@ -56,8 +57,18 @@ final class DatasetListAction extends AbstractAction
return $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
}
$instance = $this->em->find('App\Entity\Instance', $args['name']);
// Returns HTTP 404 if the instance is not found
if (is_null($instance)) {
throw new HttpNotFoundException(
$request,
'Instance with name ' . $args['name'] . ' is not found'
);
}
if ($request->getMethod() === GET) {
$datasets = $this->em->getRepository('App\Entity\Dataset')->findAll();
$datasets = $this->em->getRepository('App\Entity\Dataset')->findByInstance($instance);
$payload = json_encode($datasets);
}
......@@ -76,7 +87,6 @@ final class DatasetListAction extends AbstractAction
'data_path',
'selectable_row',
'project_name',
'instance_name',
'id_dataset_family'
);
foreach ($fields as $a) {
......@@ -97,15 +107,6 @@ final class DatasetListAction extends AbstractAction
);
}
// Instance is mandatory to add a new dataset
$instance = $this->em->find('App\Entity\Instance', $parsedBody['instance_name']);
if (is_null($instance)) {
throw new HttpBadRequestException(
$request,
'Instance with name ' . $parsedBody['instance_name'] . ' is not found'
);
}
$family = $this->em->find('App\Entity\DatasetFamily', $parsedBody['id_dataset_family']);
if (is_null($family)) {
throw new HttpBadRequestException(
......
......@@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\SqliteSchemaManager;
use Doctrine\DBAL\Schema\Column;
......@@ -46,11 +47,20 @@ final class DatasetListActionTest extends TestCase
$this->assertSame($response->getHeaderLine('Access-Control-Allow-Methods'), 'GET, POST, OPTIONS');
}
public function testInstanceNotFound(): void
{
$this->expectException(HttpNotFoundException::class);
$this->expectExceptionMessage('Instance with name aspic is not found');
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertEquals(404, (int) $response->getStatusCode());
}
public function testGetAllDatasets(): void
{
$datasets = $this->addDatasets();
$request = $this->getRequest('GET');
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertSame(
json_encode($datasets),
(string) $response->getBody()
......@@ -59,20 +69,22 @@ final class DatasetListActionTest extends TestCase
public function testAddANewDatasetEmptyNameField(): void
{
$this->addInstance();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Param name needed to add a new dataset');
$request = $this->getRequest('POST')->withParsedBody(array());
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testAddANewDatasetProjectNotFound(): void
{
$this->addInstance();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Project with name anis_project is not found');
$fields = $this->getNewDatasetFields();
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
......@@ -84,18 +96,7 @@ final class DatasetListActionTest extends TestCase
$this->expectExceptionMessage('Dataset family with id 1 is not found');
$fields = $this->getNewDatasetFields();
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$this->assertEquals(400, (int) $response->getStatusCode());
}
public function testAddANewDatasetInstanceNotFound(): void
{
$this->addProject();
$this->expectException(HttpBadRequestException::class);
$this->expectExceptionMessage('Instance with name aspic is not found');
$fields = $this->getNewDatasetFields();
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertEquals(400, (int) $response->getStatusCode());
}
......@@ -106,7 +107,7 @@ final class DatasetListActionTest extends TestCase
$this->addDatasetFamily();
$fields = $this->getNewDatasetFields();
$request = $this->getRequest('POST')->withParsedBody($fields);
$response = ($this->action)($request, new Response(), array());
$response = ($this->action)($request, new Response(), array('name' => 'aspic'));
$this->assertSame(
json_encode($fields),
(string) $response->getBody()
......@@ -121,7 +122,7 @@ final class DatasetListActionTest extends TestCase
private function getRequest(string $method): ServerRequest
{
return new ServerRequest($method, '/dataset', array(
return new ServerRequest($method, '/instance/aspic/dataset', array(
'Content-Type' => 'application/json'
));
}
......
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