Skip to content
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
9 changes: 7 additions & 2 deletions src/KafkaSchemaRegistryApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jobcloud\Kafka\SchemaRegistryClient;

use Jobcloud\Kafka\SchemaRegistryClient\Exception\SchemaRegistryExceptionInterface;
use Jobcloud\Kafka\SchemaRegistryClient\Exception\VersionNotFoundException;
use Psr\Http\Client\ClientExceptionInterface;
use Jobcloud\Kafka\SchemaRegistryClient\Exception\SchemaNotFoundException;
use Jobcloud\Kafka\SchemaRegistryClient\Exception\SubjectNotFoundException;
Expand Down Expand Up @@ -111,7 +112,7 @@ public function registerNewSchemaVersion(string $subjectName, string $schema): a
/**
* @throws ClientExceptionInterface
* @throws SchemaRegistryExceptionInterface
* @throws JsonException
* @throws JsonException|VersionNotFoundException
*/
public function checkSchemaCompatibilityForVersion(
string $subjectName,
Expand All @@ -126,7 +127,11 @@ public function checkSchemaCompatibilityForVersion(
sprintf('compatibility/subjects/%s/versions/%s', $subjectName, $version),
$this->createRequestBodyFromSchema($schema)
);
} catch (SubjectNotFoundException $e) {
} catch (SubjectNotFoundException|VersionNotFoundException $e) {
if ($e instanceof VersionNotFoundException && self::VERSION_LATEST !== $version) {
throw $e;
}

return true;
}

Expand Down
53 changes: 52 additions & 1 deletion tests/KafkaSchemaRegistryApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Jobcloud\Kafka\SchemaRegistryClient\Exception\ImportException;
use Jobcloud\Kafka\SchemaRegistryClient\Exception\SchemaNotFoundException;
use Jobcloud\Kafka\SchemaRegistryClient\Exception\SubjectNotFoundException;
use Jobcloud\Kafka\SchemaRegistryClient\Exception\VersionNotFoundException;
use Jobcloud\Kafka\SchemaRegistryClient\HttpClient;
use Jobcloud\Kafka\SchemaRegistryClient\HttpClientInterface;
use Jobcloud\Kafka\SchemaRegistryClient\KafkaSchemaRegistryApiClient;
Expand Down Expand Up @@ -208,7 +209,7 @@ public function testCheckSchemaCompatibilityForVersionFalse(): void
self::assertFalse($result);
}

public function testCheckSchemaCompatibilityForVersionNotFound(): void
public function testCheckSchemaCompatibilityForSubjectNotFound(): void
{
$httpClientMock = $this->getHttpClientMock();

Expand All @@ -231,6 +232,56 @@ public function testCheckSchemaCompatibilityForVersionNotFound(): void
self::assertTrue($result);
}

public function testCheckSchemaCompatibilityForVersionNotFound(): void
{
self::expectException(VersionNotFoundException::class);
$httpClientMock = $this->getHttpClientMock();

$httpClientMock
->expects(self::once())
->method('call')
->with(
'POST',
sprintf('compatibility/subjects/%s/versions/%s', self::TEST_SUBJECT_NAME, self::TEST_VERSION),
['schema' => '[]']
)
->willThrowException(new VersionNotFoundException());

$api = new KafkaSchemaRegistryApiClient($httpClientMock);
$result = $api->checkSchemaCompatibilityForVersion(
self::TEST_SUBJECT_NAME,
self::TEST_SCHEMA,
self::TEST_VERSION
);
}

public function testCheckSchemaCompatibilityForVersionNotFoundLatest(): void
{
$httpClientMock = $this->getHttpClientMock();

$httpClientMock
->expects(self::once())
->method('call')
->with(
'POST',
sprintf(
'compatibility/subjects/%s/versions/%s',
self::TEST_SUBJECT_NAME,
KafkaSchemaRegistryApiClient::VERSION_LATEST
),
['schema' => '[]']
)
->willThrowException(new VersionNotFoundException());

$api = new KafkaSchemaRegistryApiClient($httpClientMock);
$result = $api->checkSchemaCompatibilityForVersion(
self::TEST_SUBJECT_NAME,
self::TEST_SCHEMA,
KafkaSchemaRegistryApiClient::VERSION_LATEST
);
self::assertTrue($result);
}

public function testGetSubjectCompatibilityLevel(): void
{
$httpClientMock = $this->getHttpClientMock();
Expand Down