Skip to content

Commit

Permalink
Merge pull request #108 from openeuropa/EWPP-3842
Browse files Browse the repository at this point in the history
EWPP-3842: Throw exception on deletion failure.
  • Loading branch information
hernani authored Jan 9, 2024
2 parents 018fd94 + 72c3024 commit 3c13df2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ public function getResponse(RequestInterface $request, array $options): Response
break;

case EuropaSearchMockServerConfigOverrider::ENDPOINT_INGESTION_DELETE:
$response = $this->getDeleteResponse();
$params = urldecode($request->getUri()->getQuery());
// Make it fail on the 5th entity.
if (str_contains($params, 'entity_test_mulrev_changed/5')) {
$response = $this->getFailedDeleteResponse();
}
else {
$response = $this->getDeleteResponse();
}

break;

default:
Expand Down Expand Up @@ -214,4 +222,14 @@ protected function getDeleteResponse(): ResponseInterface {
return new Response(200, [], $this->mockedResponses['delete_document_response'] ?? '{}');
}

/**
* Get mocked failed delete response.
*
* @return \Psr\Http\Message\ResponseInterface
* The mocked response.
*/
protected function getFailedDeleteResponse(): ResponseInterface {
return new Response(500, [], $this->mockedResponses['delete_document_response'] ?? '{}');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Drupal\search_api\Plugin\PluginFormTrait;
use Drupal\search_api\Query\ConditionGroup;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\SearchApiException;
use GuzzleHttp\ClientInterface as HttpClientInterface;
use Laminas\Diactoros\RequestFactory;
use Laminas\Diactoros\StreamFactory;
Expand Down Expand Up @@ -402,6 +403,7 @@ public function deleteItems(IndexInterface $index, array $item_ids): void {
}
catch (\Exception $e) {
$this->getLogger()->warning($e->getMessage());
throw new SearchApiException($e->getMessage(), $e->getCode(), $e);
}
}
}
Expand Down
30 changes: 28 additions & 2 deletions tests/src/Kernel/BackendIngestionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Drupal\oe_search_mock\Config\EuropaSearchMockServerConfigOverrider;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\SearchApiException;
use Drupal\search_api\Utility\Utility as SearchApiUtility;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\search_api\Functional\ExampleContentTrait;
Expand Down Expand Up @@ -108,6 +109,20 @@ class BackendIngestionTest extends KernelTestBase {
*/
protected $mediaType;

/**
* The search api task manager.
*
* @var \Drupal\search_api\Task\TaskManager
*/
protected $taskManager;

/**
* The search api server.
*
* @var \Drupal\search_api\Entity\Server
*/
protected $server;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -156,7 +171,10 @@ public function setUp(): void {
new Settings($settings);

$datasource_manager = $this->container->get('plugin.manager.search_api.datasource');
$this->backend = Server::load('europa_search_server')->getBackend();
$this->taskManager = $this->container->get('search_api.task_manager');

$this->server = Server::load('europa_search_server');
$this->backend = $this->server->getBackend();
$this->index = Index::load($this->indexId);
$this->datasource = $datasource_manager->createInstance('entity:entity_test_mulrev_changed');
$this->datasource->setIndex($this->index);
Expand Down Expand Up @@ -260,7 +278,7 @@ public function testMediaIndexItems(): void {
*/
public function testDeleteItems(): void {
$this->assertServiceMockCalls(EuropaSearchMockServerConfigOverrider::ENDPOINT_INGESTION_DELETE, 0, 0);
$this->backend->deleteItems($this->index, $this->itemIds);
$this->server->deleteItems($this->index, $this->itemIds);
$this->assertServiceMockCalls(EuropaSearchMockServerConfigOverrider::ENDPOINT_INGESTION_DELETE, 5, 5);
// Compare sent data with received data.
$requests = $this->getServiceMockRequests(EuropaSearchMockServerConfigOverrider::ENDPOINT_INGESTION_DELETE);
Expand All @@ -270,13 +288,21 @@ public function testDeleteItems(): void {
$this->assertDeletedItem($requests[2], 3);
$this->assertDeletedItem($requests[3], 4);
$this->assertDeletedItem($requests[4], 5);

// The last item should have failed.
// It should have a retry task in search_api_tasks.
$this->assertEquals(1, $this->taskManager->getTasksCount());
$tasks = $this->taskManager->loadTasks();
$last_task = reset($tasks);
$this->assertEquals('deleteItems', $last_task->getType());
}

/**
* @covers ::deleteAllIndexItems
*/
public function testDeleteAllIndexItems(): void {
$this->assertServiceMockCalls(EuropaSearchMockServerConfigOverrider::ENDPOINT_INGESTION_DELETE, 0, 0);
$this->expectException(SearchApiException::class);
$this->backend->deleteAllIndexItems($this->index);
$this->assertServiceMockCalls(EuropaSearchMockServerConfigOverrider::ENDPOINT_INGESTION_DELETE, 15, 15);
// Compare sent data with received data.
Expand Down

0 comments on commit 3c13df2

Please sign in to comment.