Skip to content

Commit

Permalink
LCH-6559: Throw an exception if search is not responding instead of r…
Browse files Browse the repository at this point in the history
…eturning empty document. (#180)

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

* LCH-6559: Add tests for runtime exception for getEntities.
  • Loading branch information
amangrover90 committed Jul 11, 2023
1 parent 27940ab commit 205d929
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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

0 comments on commit 205d929

Please sign in to comment.