Skip to content

Commit

Permalink
Metastore service.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell committed Nov 20, 2019
1 parent dd24b0e commit 311ca89
Show file tree
Hide file tree
Showing 30 changed files with 1,317 additions and 1,183 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
command: |
export PATH=$PATH:~/dkan-tools/bin
cd ~/sandbox
dktl get 8.7.8
dktl get 8.7.10
- run:
name: Make DKAN
command: |
Expand Down
10 changes: 5 additions & 5 deletions cypress/integration/01_metastore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ context('Metastore', () => {
},
failOnStatusCode: false
}).then((response) => {
expect(response.status).eql(406)
expect(response.status).eql(400)
})

cy.request({
Expand All @@ -130,7 +130,7 @@ context('Metastore', () => {
auth: user_credentials,
failOnStatusCode: false
}).then((response) => {
expect(response.status).eql(406)
expect(response.status).eql(400)
})
})
})
Expand All @@ -145,7 +145,7 @@ context('Metastore', () => {
},
failOnStatusCode: false
}).then((response) => {
expect(response.status).eql(406)
expect(response.status).eql(400)
})

cy.request({
Expand All @@ -154,7 +154,7 @@ context('Metastore', () => {
auth: user_credentials,
failOnStatusCode: false
}).then((response) => {
expect(response.status).eql(406)
expect(response.status).eql(400)
})
})

Expand Down Expand Up @@ -223,7 +223,7 @@ context('Metastore', () => {
body: jsonShouldNotExist,
failOnStatusCode: false
}).then((response) => {
expect(response.status).eql(404)
expect(response.status).eql(412)
})
})

Expand Down
5 changes: 5 additions & 0 deletions modules/custom/dkan_api/dkan_api.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
dkan_api.docs:
class: \Drupal\dkan_api\Controller\Docs
arguments:
- '@module_handler'
4 changes: 2 additions & 2 deletions modules/custom/dkan_api/docs/dkan_api_openapi_spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ paths:
schema:
type: object
required:
- endpoint
- message
properties:
endpoint:
message:
type: string
'401':
description: Unauthorized
Expand Down
28 changes: 15 additions & 13 deletions modules/custom/dkan_harvest/src/Load/Dataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\dkan_harvest\Load;

use Drupal\dkan_metastore\Service;
use Harvest\ETL\Load\Load;

/**
Expand All @@ -13,32 +14,33 @@ class Dataset extends Load {
* Public.
*/
public function removeItem($id) {
$engine = $this->getDatasetEngine();
$engine->delete($id);
$service = $this->getMetastoreService();
$service->delete("dataset", "{$id}");
}

/**
* Private.
*/
protected function saveItem($item) {
$engine = $this->getDatasetEngine();
$engine->post(json_encode($item));
$service = $this->getMetastoreService();
if (!is_string($item)) {
$item = json_encode($item);
}
$service->post('dataset', $item);
}

/**
* Get the engine from the Dataset Controller.
* Get the metastore service.
*
* @TODO Shouldn't use controller inner workings like this. Should refactor to service.
*
* @return \Sae\Sae
* Sae object.
* @return \Drupal\dkan_metastore\Service
* Metastore service.
*
* @codeCoverageIgnore
*/
protected function getDatasetEngine() {
/** @var \Drupal\dkan_api\Controller\Dataset $dataset_controller */
$dataset_controller = \Drupal::service('dkan_metastore.controller');
return $dataset_controller->getEngine('dataset');
protected function getMetastoreService(): Service
{
$service = \Drupal::service('dkan_metastore.service');
return $service;
}

}
60 changes: 16 additions & 44 deletions modules/custom/dkan_harvest/test/src/Unit/Load/DatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace Drupal\Tests\dkan_harvest\Unit\Extract;

use Contracts\Mock\Storage\Memory;
use Drupal\Core\DependencyInjection\Container;
use Drupal\dkan_common\Tests\DkanTestBase;
use Drupal\dkan_common\Tests\Mock\Chain;
use Drupal\dkan_common\Tests\Mock\Options;
use Drupal\dkan_harvest\Load\Dataset;
use Drupal\dkan_metastore\Service;
use Sae\Sae;

/**
Expand All @@ -19,53 +23,21 @@ class DatasetTest extends DkanTestBase {
* Public.
*/
public function test() {
$load = $this->getMockBuilder(Dataset::class)
->disableOriginalConstructor()
->setMethods(['getDatasetEngine'])
->getMock();
$container = (new Chain($this))
->add(Container::class, "get", (new Options())
->add('dkan_metastore.service', Service::class)
)
->add(Service::class, "post", "1");

$storage = new Memory();
$storage->store("This is a string", "1");
$this->assertEquals(1, count($storage->retrieveAll()));
\Drupal::setContainer($container->getMock());

$engine = new Sae($storage, "{
\"\$schema\": \"http://json-schema.org/draft-07/schema#\",
\"title\": \"Yep\",
\"type\": \"string\"");
$hashStorage = new Memory();
$itemStorage = new Memory();
$load = new Dataset((object) ["identifier" => "plan"], $hashStorage, $itemStorage);
$load->run((object) ["identifier" => "1"]);

$load->method('getDatasetEngine')->willReturn($engine);
$load->removeItem("1");

$this->assertEquals(0, count($storage->retrieveAll()));
}

/**
* Tests saveItem().
*/
public function testSaveItem() {
// Setup.
$mock = $this->getMockBuilder(Dataset::class)
->disableOriginalConstructor()
->setMethods(['getDatasetEngine'])
->getMock();

$mockEngine = $this->getMockBuilder(Sae::class)
->setMethods(['post'])
->disableOriginalConstructor()
->getMock();

$item = (object) ['foo' => 'bar'];
// Expect.
$mock->expects($this->once())
->method('getDatasetEngine')
->willReturn($mockEngine);

$mockEngine->expects($this->once())
->method('post')
->willReturn(json_encode($item));

// Assert.
$this->invokeProtectedMethod($mock, 'saveItem', $item);
// We just want to run the code.
$this->assertTrue(true);
}

}
2 changes: 1 addition & 1 deletion modules/custom/dkan_lunr/dkan_lunr.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ description: 'PHP Lunr index generator and endpoint.'
type: module
core: 8.x
dependencies:
- dkan_api
- dkan_metastore
package: DKAN
2 changes: 1 addition & 1 deletion modules/custom/dkan_lunr/dkan_lunr.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ services:
dkan_lunr.search:
class: Drupal\dkan_lunr\Search
arguments:
- '@dkan_metastore.controller'
- '@dkan_metastore.service'
shared: false
15 changes: 5 additions & 10 deletions modules/custom/dkan_lunr/src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\dkan_lunr;

use Drupal\dkan_metastore\Controller\Api;
use Drupal\dkan_metastore\Service;
use Drupal\dkan_lunr\Service\DatasetModifier;
use LunrPHP\BuildLunrIndex;

Expand All @@ -13,18 +13,13 @@
*/
class Search {

/**
* Api controller.
*
* @var \Drupal\dkan_metastore\Controller\Api
*/
private $apiController;
private $metastoreService;

/**
* Search constructor.
*/
public function __construct(Api $controller) {
$this->apiController = $controller;
public function __construct(Service $metastoreService) {
$this->metastoreService = $metastoreService;
}

/**
Expand Down Expand Up @@ -167,7 +162,7 @@ protected function getDatasets() {
function ($item) {
return json_decode($item);
},
$this->apiController->getEngine('dataset')->get()
$this->metastoreService->getAll('dataset')
);
}

Expand Down
18 changes: 9 additions & 9 deletions modules/custom/dkan_metastore/dkan_metastore.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ dkan_metastore.1.metastore.schemas:
path: '/api/1/metastore/schemas'
methods: [GET]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::getSchemas'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApi::getSchemas'}
requirements:
_permission: 'access content'

dkan_metastore.1.metastore.schemas.id:
path: '/api/1/metastore/schemas/{identifier}'
methods: [GET]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::getSchema'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApi::getSchema'}
requirements:
_permission: 'access content'

dkan_metastore.1.metastore.schemas.id.items:
path: '/api/1/metastore/schemas/{schema_id}/items'
methods: [GET]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::getAll'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApi::getAll'}
requirements:
_permission: 'access content'

dkan_metastore.1.metastore.schemas.id.items.post:
path: '/api/1/metastore/schemas/{schema_id}/items'
methods: [POST]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::post'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApi::post'}
requirements:
_permission: 'post put delete datasets through the api'
options:
Expand All @@ -44,15 +44,15 @@ dkan_metastore.1.metastore.schemas.id.items.id:
path: '/api/1/metastore/schemas/{schema_id}/items/{identifier}'
methods: [GET]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::get'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApi::get'}
requirements:
_permission: 'access content'

dkan_metastore.1.metastore.schemas.id.items.id.put:
path: '/api/1/metastore/schemas/{schema_id}/items/{identifier}'
methods: [PUT]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::put'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApi::put'}
requirements:
_permission: 'post put delete datasets through the api'
options:
Expand All @@ -62,7 +62,7 @@ dkan_metastore.1.metastore.schemas.id.items.id.patch:
path: '/api/1/metastore/schemas/{schema_id}/items/{identifier}'
methods: [PATCH]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::patch'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApi::patch'}
requirements:
_permission: 'post put delete datasets through the api'
options:
Expand All @@ -72,7 +72,7 @@ dkan_metastore.1.metastore.schemas.id.items.id.delete:
path: '/api/1/metastore/schemas/{schema_id}/items/{identifier}'
methods: [DELETE]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::delete'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApi::delete'}
requirements:
_permission: 'post put delete datasets through the api'
options:
Expand All @@ -82,6 +82,6 @@ dkan_metastore.1.metastore.schemas.dataset.items.id.docs:
path: '/api/1/metastore/schemas/dataset/items/{identifier}/docs'
methods: [GET]
defaults:
{ _controller: '\Drupal\dkan_metastore\Controller\Api::getDatasetSpecific'}
{ _controller: '\Drupal\dkan_metastore\WebServiceApiDocs::getDatasetSpecific'}
requirements:
_permission: 'access content'
14 changes: 7 additions & 7 deletions modules/custom/dkan_metastore/dkan_metastore.services.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
services:
dkan_metastore.controller:
class: \Drupal\dkan_metastore\Controller\Api
dkan_metastore.service:
class: \Drupal\dkan_metastore\Service
arguments:
- '@request_stack'
- '@dkan_schema.schema_retriever'
- '@dkan_data.storage'
dkan_metastore.route_provider:
class: \Drupal\dkan_metastore\Routing\RouteProvider
- '@dkan_metastore.sae_factory'
dkan_metastore.sae_factory:
class: \Drupal\dkan_metastore\Factory\Sae
arguments:
- '@config.factory'
- '@dkan_schema.schema_retriever'
- '@dkan_data.storage'
Loading

0 comments on commit 311ca89

Please sign in to comment.