From 32835c17953cc2ebbf4df57973a1acb28305bdee Mon Sep 17 00:00:00 2001 From: Amandeep Singh Date: Fri, 7 Jul 2023 18:19:40 +0530 Subject: [PATCH 1/2] LCH-6559: Throw an exception if search is not responding instead of returning empty document. --- src/ContentHubClient.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ContentHubClient.php b/src/ContentHubClient.php index d4fa391e..c958c007 100644 --- a/src/ContentHubClient.php +++ b/src/ContentHubClient.php @@ -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']); From 0d0e8f722926c0a692baa462f20f6009488b59f9 Mon Sep 17 00:00:00 2001 From: Amandeep Singh Date: Mon, 10 Jul 2023 15:23:39 +0530 Subject: [PATCH 2/2] LCH-6559: Add tests for runtime exception for getEntities. --- test/ContentHubClientTest.php | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/ContentHubClientTest.php b/test/ContentHubClientTest.php index b5276903..66b799f5 100644 --- a/test/ContentHubClientTest.php +++ b/test/ContentHubClientTest.php @@ -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