|
18 | 18 | */ |
19 | 19 | package org.apache.pulsar.broker.service.schema; |
20 | 20 |
|
| 21 | +import static org.testng.Assert.assertThrows; |
21 | 22 | import static org.testng.Assert.fail; |
22 | 23 | import static org.testng.AssertJUnit.assertEquals; |
23 | 24 | import static org.testng.AssertJUnit.assertFalse; |
|
40 | 41 | import java.util.UUID; |
41 | 42 | import java.util.concurrent.CompletableFuture; |
42 | 43 | import java.util.concurrent.ExecutionException; |
| 44 | +import org.apache.pulsar.broker.BrokerTestUtil; |
43 | 45 | import org.apache.pulsar.broker.PulsarServerException; |
44 | 46 | import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; |
45 | 47 | import org.apache.pulsar.broker.service.schema.SchemaRegistry.SchemaAndMetadata; |
46 | 48 | import org.apache.pulsar.broker.stats.PrometheusMetricsTest; |
47 | 49 | import org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsGenerator; |
| 50 | +import org.apache.pulsar.client.admin.PulsarAdminException; |
| 51 | +import org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo; |
48 | 52 | import org.apache.pulsar.common.naming.TopicName; |
49 | 53 | import org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy; |
| 54 | +import org.apache.pulsar.common.protocol.schema.IsCompatibilityResponse; |
50 | 55 | import org.apache.pulsar.common.protocol.schema.SchemaData; |
51 | 56 | import org.apache.pulsar.common.protocol.schema.SchemaVersion; |
| 57 | +import org.apache.pulsar.common.schema.KeyValueEncodingType; |
52 | 58 | import org.apache.pulsar.common.schema.LongSchemaVersion; |
| 59 | +import org.apache.pulsar.common.schema.SchemaInfo; |
| 60 | +import org.apache.pulsar.common.schema.SchemaInfoWithVersion; |
53 | 61 | import org.apache.pulsar.common.schema.SchemaType; |
54 | 62 | import org.testng.Assert; |
55 | 63 | import org.testng.annotations.AfterMethod; |
@@ -385,4 +393,32 @@ private static SchemaData getSchemaData(String schemaJson) { |
385 | 393 | private SchemaVersion version(long version) { |
386 | 394 | return new LongSchemaVersion(version); |
387 | 395 | } |
| 396 | + |
| 397 | + @Test |
| 398 | + public void testKeyValueSchema() throws Exception { |
| 399 | + final String topicName = "persistent://public/default/testKeyValueSchema"; |
| 400 | + admin.topics().createNonPartitionedTopic(BrokerTestUtil.newUniqueName(topicName)); |
| 401 | + |
| 402 | + final SchemaInfo schemaInfo = KeyValueSchemaInfo.encodeKeyValueSchemaInfo( |
| 403 | + "keyValue", |
| 404 | + SchemaInfo.builder().type(SchemaType.STRING).schema(new byte[0]) |
| 405 | + .build(), |
| 406 | + SchemaInfo.builder().type(SchemaType.BOOLEAN).schema(new byte[0]) |
| 407 | + .build(), KeyValueEncodingType.SEPARATED); |
| 408 | + assertThrows(PulsarAdminException.ServerSideErrorException.class, () -> admin.schemas().testCompatibility(topicName, schemaInfo)); |
| 409 | + admin.schemas().createSchema(topicName, schemaInfo); |
| 410 | + |
| 411 | + final IsCompatibilityResponse isCompatibilityResponse = admin.schemas().testCompatibility(topicName, schemaInfo); |
| 412 | + Assert.assertTrue(isCompatibilityResponse.isCompatibility()); |
| 413 | + |
| 414 | + final SchemaInfoWithVersion schemaInfoWithVersion = admin.schemas().getSchemaInfoWithVersion(topicName); |
| 415 | + Assert.assertEquals(schemaInfoWithVersion.getVersion(), 0); |
| 416 | + |
| 417 | + final Long version1 = admin.schemas().getVersionBySchema(topicName, schemaInfo); |
| 418 | + Assert.assertEquals(version1, 0); |
| 419 | + |
| 420 | + final Long version2 = admin.schemas().getVersionBySchema(topicName, schemaInfoWithVersion.getSchemaInfo()); |
| 421 | + Assert.assertEquals(version2, 0); |
| 422 | + |
| 423 | + } |
388 | 424 | } |
0 commit comments