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

LCH-6559: Throw an exception if search is not responding instead of returning empty document. #180

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions src/ContentHubClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ public function getEntities(array $uuids) {
];
$options['body'] = json_encode($query);
$results = self::getResponseJson($this->get('_search', $options));
if (!isset($results['hits'])) {
throw new \RuntimeException('Content Hub Search endpoint is not reachable.');
}
if (isset($results['hits']['total'])) {
foreach ($results['hits']['hits'] as $key => $item) {
$objects[] = $this->getCDFObject($item['_source']['data']);
Expand Down
41 changes: 41 additions & 0 deletions test/ContentHubClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,47 @@ public function testGetEntitiesReturnsCDFDocumentWithEmptyObjectSetIfNothingFoun
$this->assertCount(0, $result->getEntities());
}

/**
* Tests that runtime exception is thrown.
*
* When search endpoint doesn't respond with hits.
*
* @throws \Exception
*/
public function testGetEntitiesThrowsExceptionWhenSearchNotAvailable(): void {
$total = 50;
$chunk_size = 50;
$uuids = array_fill(0, $total, 'some-non-existing-uuid');

foreach (array_chunk($uuids, $chunk_size) as $chunk) {
$call_params = [
'size' => $chunk_size,
'query' => [
'constant_score' => [
'filter' => [
'terms' => [
'uuid' => $chunk,
],
],
],
],
];
$this->ch_client
->shouldReceive('get')
->once()
->with('_search', ['body' => json_encode($call_params)])
->andReturn($this->makeMockResponse(SymfonyResponse::HTTP_OK, [],
json_encode([
'error' => 'Search not available',
])));
}

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Content Hub Search endpoint is not reachable.');
$this->ch_client->getEntities($uuids);

}

/**
* @covers \Acquia\ContentHubClient\ContentHubClient::getCDFObject
* @throws \ReflectionException
Expand Down