Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versioning #74

Merged
merged 25 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9000d12
add call to createVersion in chullo when a node is updated
May 21, 2019
7601e5c
better logging
elizoller May 22, 2019
8aa4fa1
Merge branch 'master' into versioning
elizoller May 23, 2019
59aea41
pass in the resource URI not the versioning URI to allow it to change…
elizoller May 23, 2019
5ed3e35
some obnoxiously loud logging
elizoller May 24, 2019
e56c721
cleanup
elizoller May 28, 2019
37798ae
Merge branch 'master' into versioning
elizoller May 29, 2019
616f5ba
pass null body by default
elizoller Jun 12, 2019
53827cb
Merge branch 'versioning' of https://github.com/asulibraries/Crayfish…
elizoller Jun 12, 2019
4cec8b2
Merge branch 'master' into versioning
elizoller Jul 23, 2019
07c2d3e
Merge remote-tracking branch 'upstream/dev' into versioning
elizoller Jul 29, 2019
ecf9602
pass the event type from the header set in alpaca through to the save…
elizoller Jul 29, 2019
75e9239
only create the version when the version event type is sent, wrap the…
elizoller Jul 29, 2019
e56c641
updates for phpcs
elizoller Jul 29, 2019
10f0e9a
update tests
elizoller Jul 30, 2019
26989ae
return the fedora response
elizoller Jul 30, 2019
ec303cc
add CreateVersionTest
elizoller Jul 30, 2019
9173f40
change milliner handling of versioning from using a header on the sav…
elizoller Aug 6, 2019
b8e02aa
Merge remote-tracking branch 'upstream/dev' into versioning
elizoller Aug 7, 2019
a29b355
Merge remote-tracking branch 'upstream/dev' into versioning
elizoller Oct 14, 2019
a95a819
Merge remote-tracking branch 'upstream/dev' into versioning
elizoller Oct 24, 2019
bdc4067
add more tests
elizoller Oct 24, 2019
fbed0ff
wrap it in a check for empty
elizoller Nov 7, 2019
4cef19c
fix formatting errors
elizoller Nov 7, 2019
2bcc1b6
adding a test for when gemini returns an empty array
elizoller Nov 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Milliner/src/Controller/MillinerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,28 @@ public function saveExternal($uuid, Request $request)
return new Response($e->getMessage(), $code);
}
}

/**
* @param string $uuid
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function createVersion($uuid, Request $request)
{
$token = $request->headers->get("Authorization", null);
try {
$response = $this->milliner->createVersion(
$uuid,
$token
);
return new Response(
$response->getBody(),
$response->getStatusCode()
);
} catch (\Exception $e) {
$this->log->error("", ['Exception' => $e]);
$code = $e->getCode() == 0 ? 500 : $e->getCode();
return new Response($e->getMessage(), $code);
}
}
}
42 changes: 42 additions & 0 deletions Milliner/src/Service/MillinerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Islandora\Chullo\IFedoraApi;
use Islandora\Crayfish\Commons\Client\GeminiClient;
use Psr\Log\LoggerInterface;
use \DateTime;

/**
* Class MillinerService
Expand Down Expand Up @@ -542,4 +543,45 @@ public function saveExternal(
// Return the response from Fedora.
return $response;
}

/**
* Creates a new LDP-RS in Fedora from a Node.
* {@inheritDoc}
*/
public function createVersion(
$uuid,
$token = null
) {
$urls = $this->gemini->getUrls($uuid, $token);
if (!empty($urls)) {
$fedora_url = $urls['fedora'];
$headers = empty($token) ? [] : ['Authorization' => $token];
$date = new DateTime();
$timestamp = $date->format("D, d M Y H:i:s O");
// create version in Fedora.
try {
$response = $this->fedora->createVersion(
$fedora_url,
$timestamp,
null,
$headers
);
$status = $response->getStatusCode();
if (!in_array($status, [201])) {
$reason = $response->getReasonPhrase();
throw new \RuntimeException(
"Client error: `POST $fedora_url` resulted in `$status $reason` response: " .
$response->getBody(),
$status
);
}
// Return the response from Fedora.
return $response;
} catch (Exception $e) {
$this->log->error('Caught exception when creating version: ', $e->getMessage(), "\n");
}
} else {
return new Response(404);
}
}
}
13 changes: 13 additions & 0 deletions Milliner/src/Service/MillinerServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ public function saveExternal(
$external_url,
$token = null
);

/**
* @param $uuid
* @param $token
*
* @throws \Exception
*
* @return \GuzzleHttp\Psr7\Response
*/
public function createVersion(
$uuid,
$token = null
);
}
1 change: 1 addition & 0 deletions Milliner/src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
$app->delete('/node/{uuid}', "milliner.controller:deleteNode");
$app->post('/media/{source_field}', "milliner.controller:saveMedia");
$app->post('/external/{uuid}', "milliner.controller:saveExternal");
$app->post('/version/{uuid}', "milliner.controller:createVersion");

return $app;
272 changes: 272 additions & 0 deletions Milliner/tests/Islandora/Milliner/Tests/CreateVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
<?php

namespace Islandora\Milliner\Tests;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;
use Islandora\Chullo\IFedoraApi;
use Islandora\Crayfish\Commons\Client\GeminiClient;
use Islandora\Milliner\Service\MillinerService;
use Monolog\Handler\NullHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;

/**
* Class MillinerServiceTest
* @package Islandora\Milliner\Tests
* @coversDefaultClass \Islandora\Milliner\Service\MillinerService
*/
class CreateVersionTest extends TestCase
{
/**
* @var LoggerInterface
*/
protected $logger;

/**
* @var string
*/
protected $modifiedDatePredicate;

/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();

$this->logger = new Logger('milliner');
$this->logger->pushHandler(new NullHandler());

$this->modifiedDatePredicate = "http://schema.org/dateModified";
}


/**
* @covers ::__construct
* @covers ::createVersion
*/
public function testCreateVersionReturnsFedora201()
{
$mapping = [
'drupal' => '"http://localhost:8000/node/1?_format=jsonld"',
'fedora' => 'http://localhost:8080/fcrepo/rest/95/41/c0/c1/9541c0c1-5bee-4973-a9d0-e55c1658bc8'
];
$gemini = $this->prophesize(GeminiClient::class);
$gemini->getUrls(Argument::any(), Argument::any())
->willReturn($mapping);
$gemini->saveUrls(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn(true);
$gemini = $gemini->reveal();

$drupal_response = new Response(
200,
['Content-Type' => 'application/ld+json'],
file_get_contents(__DIR__ . '/../../../../static/Content.jsonld')
);
$drupal = $this->prophesize(Client::class);
$drupal->get(Argument::any(), Argument::any())
->willReturn($drupal_response);
$drupal = $drupal->reveal();

$fedora_response = new Response(201);
$fedora = $this->prophesize(IFedoraApi::class);
$fedora->createVersion(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn($fedora_response);
$fedora = $fedora->reveal();

$milliner = new MillinerService(
$fedora,
$drupal,
$gemini,
$this->logger,
$this->modifiedDatePredicate,
false
);

$response = $milliner->createVersion(
"9541c0c1-5bee-4973-a9d0-e55c1658bc81",
"Bearer islandora"
);

$status = $response->getStatusCode();
$this->assertTrue(
$status == 201,
"Milliner must return 201 when Fedora returns 201. Received: $status"
);
}

/**
* @covers ::__construct
* @covers ::createVersion
* @expectedException \RuntimeException
* @expectedExceptionCode 404
*/

public function testCreateVersionReturnsFedora404()
{
$mapping = [
'drupal' => '"http://localhost:8000/node/1?_format=jsonld"',
'fedora' => 'http://localhost:8080/fcrepo/rest/95/41/c0/c1/9541c0c1-5bee-4973-a9d0-9998'
];
$gemini = $this->prophesize(GeminiClient::class);
$gemini->getUrls(Argument::any(), Argument::any())
->willReturn($mapping);
$gemini->saveUrls(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn(true);
$gemini = $gemini->reveal();

$drupal_response = new Response(
200,
['Content-Type' => 'application/ld+json'],
file_get_contents(__DIR__ . '/../../../../static/Content.jsonld')
);
$drupal = $this->prophesize(Client::class);
$drupal->get(Argument::any(), Argument::any())
->willReturn($drupal_response);
$drupal = $drupal->reveal();

$fedora_response = new Response(404);
$fedora = $this->prophesize(IFedoraApi::class);
$fedora->createVersion(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn($fedora_response);
$fedora = $fedora->reveal();

$milliner = new MillinerService(
$fedora,
$drupal,
$gemini,
$this->logger,
$this->modifiedDatePredicate,
false
);

$response = $milliner->createVersion(
"9541c0c1-5bee-4973-a9d0-9998",
"Bearer islandora"
);

$status = $response->getStatusCode();
$this->assertTrue(
$status == 404,
"Milliner must return 404 when Fedora returns 404. Received: $status"
);
}


/**
* @covers ::__construct
* @covers ::createVersion
* @expectedException \RuntimeException
* @expectedExceptionCode 403
*/
public function testcreateVersionThrowsOnFedoraSaveError()
{
$mapping = [
'drupal' => '"http://localhost:8000/node/1?_format=jsonld"',
'fedora' => 'http://localhost:8080/fcrepo/rest/95/41/c0/c1/9541c0c1-5bee-4973-a9d0-e55c1658bc8'
];
$gemini = $this->prophesize(GeminiClient::class);
$gemini->getUrls(Argument::any(), Argument::any())
->willReturn($mapping);
$gemini = $gemini->reveal();

$drupal_response = new Response(
200,
['Content-Type' => 'application/ld+json'],
file_get_contents(__DIR__ . '/../../../../static/Content.jsonld')
);
$drupal = $this->prophesize(Client::class);
$drupal->get(Argument::any(), Argument::any())
->willReturn($drupal_response);
$drupal = $drupal->reveal();

$fedora_get_response = new Response(
200,
['Content-Type' => 'application/ld+json'],
file_get_contents(__DIR__ . '/../../../../static/ContentLDP-RS.jsonld')
);
$fedora_response = new Response(403, [], null, '1.1', 'UNAUTHORIZED');
$fedora = $this->prophesize(IFedoraApi::class);
$fedora->createVersion(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn($fedora_response);
$fedora = $fedora->reveal();


$milliner = new MillinerService(
$fedora,
$drupal,
$gemini,
$this->logger,
$this->modifiedDatePredicate,
false
);

$response = $milliner->createVersion(
"9541c0c1-5bee-4973-a9d0-e55c1658bc81",
"Bearer islandora"
);

$status = $response->getStatusCode();
$this->assertTrue(
$status == 403,
"Milliner must return 403 when Fedora returns 403. Received: $status"
);
}

/**
* @covers ::__construct
* @covers ::createVersion
* @expectedExceptionCode 404
*/

public function testCreateVersionReturns404IfNotInGemini()
{
$mapping = [];
$gemini = $this->prophesize(GeminiClient::class);
$gemini->getUrls(Argument::any(), Argument::any())
->willReturn($mapping);
$gemini->saveUrls(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn(true);
$gemini = $gemini->reveal();

$drupal_response = new Response(
200,
['Content-Type' => 'application/ld+json'],
file_get_contents(__DIR__ . '/../../../../static/Content.jsonld')
);
$drupal = $this->prophesize(Client::class);
$drupal->get(Argument::any(), Argument::any())
->willReturn($drupal_response);
$drupal = $drupal->reveal();

$fedora_response = new Response(404);
$fedora = $this->prophesize(IFedoraApi::class);
$fedora->createVersion(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn($fedora_response);
$fedora = $fedora->reveal();

$milliner = new MillinerService(
$fedora,
$drupal,
$gemini,
$this->logger,
$this->modifiedDatePredicate,
false
);

$response = $milliner->createVersion(
"9541c0c1-5bee-4973-a9d0-9998",
"Bearer islandora"
);

$status = $response->getStatusCode();
$this->assertTrue(
$status == 404,
"Milliner must return 404 when not in Gemini. Received: $status"
);
}
}
Loading