|
19 | 19 | package org.apache.pulsar.schema.compatibility;
|
20 | 20 |
|
21 | 21 | import static java.nio.charset.StandardCharsets.UTF_8;
|
| 22 | +import static org.apache.pulsar.common.naming.TopicName.DEFAULT_NAMESPACE; |
22 | 23 | import static org.apache.pulsar.common.naming.TopicName.PUBLIC_TENANT;
|
23 | 24 | import static org.testng.Assert.assertEquals;
|
| 25 | +import static org.testng.Assert.assertTrue; |
24 | 26 | import static org.testng.Assert.fail;
|
25 | 27 | import com.google.common.collect.Sets;
|
26 | 28 | import java.util.Collections;
|
27 | 29 | import java.util.concurrent.ThreadLocalRandom;
|
28 | 30 | import java.util.stream.Collectors;
|
29 | 31 | import lombok.extern.slf4j.Slf4j;
|
| 32 | +import org.apache.pulsar.broker.BrokerTestUtil; |
30 | 33 | import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
|
| 34 | +import org.apache.pulsar.client.admin.PulsarAdminException; |
31 | 35 | import org.apache.pulsar.client.api.Consumer;
|
32 | 36 | import org.apache.pulsar.client.api.ConsumerBuilder;
|
33 | 37 | import org.apache.pulsar.client.api.Message;
|
|
36 | 40 | import org.apache.pulsar.client.api.Schema;
|
37 | 41 | import org.apache.pulsar.client.api.SchemaSerializationException;
|
38 | 42 | import org.apache.pulsar.client.api.schema.SchemaDefinition;
|
| 43 | +import org.apache.pulsar.client.impl.schema.SchemaInfoImpl; |
39 | 44 | import org.apache.pulsar.common.naming.NamespaceName;
|
40 | 45 | import org.apache.pulsar.common.naming.TopicDomain;
|
41 | 46 | import org.apache.pulsar.common.naming.TopicName;
|
@@ -68,6 +73,8 @@ public void setup() throws Exception {
|
68 | 73 | .allowedClusters(Collections.singleton(CLUSTER_NAME))
|
69 | 74 | .build();
|
70 | 75 | admin.tenants().createTenant(PUBLIC_TENANT, tenantInfo);
|
| 76 | + String namespaceName = PUBLIC_TENANT + "/" + DEFAULT_NAMESPACE; |
| 77 | + admin.namespaces().createNamespace(namespaceName, Sets.newHashSet(CLUSTER_NAME)); |
71 | 78 | }
|
72 | 79 |
|
73 | 80 | @AfterMethod(alwaysRun = true)
|
@@ -483,9 +490,8 @@ public void testProducerSendWithOldSchemaAndConsumerCanRead(SchemaCompatibilityS
|
483 | 490 |
|
484 | 491 | @Test
|
485 | 492 | public void testSchemaLedgerAutoRelease() throws Exception {
|
486 |
| - String namespaceName = PUBLIC_TENANT + "/default"; |
487 |
| - String topicName = "persistent://" + namespaceName + "/tp"; |
488 |
| - admin.namespaces().createNamespace(namespaceName, Sets.newHashSet(CLUSTER_NAME)); |
| 493 | + String namespaceName = PUBLIC_TENANT + "/" + DEFAULT_NAMESPACE; |
| 494 | + String topicName = BrokerTestUtil.newUniqueName("persistent://" + namespaceName + "/tp"); |
489 | 495 | admin.namespaces().setSchemaCompatibilityStrategy(namespaceName, SchemaCompatibilityStrategy.ALWAYS_COMPATIBLE);
|
490 | 496 | // Update schema 100 times.
|
491 | 497 | for (int i = 0; i < 100; i++){
|
@@ -516,6 +522,46 @@ public void testSchemaLedgerAutoRelease() throws Exception {
|
516 | 522 | admin.topics().delete(topicName, true);
|
517 | 523 | }
|
518 | 524 |
|
| 525 | + @Test |
| 526 | + public void testAddUnionAvroSchema() throws Exception { |
| 527 | + String namespaceName = PUBLIC_TENANT + "/" + DEFAULT_NAMESPACE; |
| 528 | + String topicName = BrokerTestUtil.newUniqueName(namespaceName + "/tp"); |
| 529 | + admin.topics().createNonPartitionedTopic(topicName); |
| 530 | + |
| 531 | + // Create a union type schema. |
| 532 | + SchemaInfoImpl schemaInfo = new SchemaInfoImpl(); |
| 533 | + schemaInfo.setType(SchemaType.AVRO); |
| 534 | + schemaInfo.setSchema( |
| 535 | + """ |
| 536 | + [{ |
| 537 | + "namespace": "org.apache.pulsar.schema.compatibility.TestA", |
| 538 | + "type": "enum", |
| 539 | + "name": "EventSource", |
| 540 | + "symbols": ["AUTO_EVENTING", "HOODLUM", "OPTA", "ISD", "LIVE_STATS", "NGSS", "UNIFIED"] |
| 541 | + }, { |
| 542 | + "namespace": "org.apache.pulsar.schema.compatibility.TestB", |
| 543 | + "type": "enum", |
| 544 | + "name": "PeriodType", |
| 545 | + "symbols": ["REGULAR", "EXTRA_TIME"] |
| 546 | + }] |
| 547 | + """.getBytes(UTF_8)); |
| 548 | + schemaInfo.setName(topicName); |
| 549 | + schemaInfo.setTimestamp(System.currentTimeMillis()); |
| 550 | + try { |
| 551 | + admin.schemas().createSchema(topicName, schemaInfo); |
| 552 | + fail("avro-union schema is not supported"); |
| 553 | + } catch (PulsarAdminException e) { |
| 554 | + assertTrue(e.getMessage().contains("Avro schema typed [UNION] is not supported")); |
| 555 | + } |
| 556 | + |
| 557 | + // Create a producer with auto_produce schema. |
| 558 | + Producer producer = pulsarClient.newProducer(Schema.AUTO_PRODUCE_BYTES()).topic(topicName).create(); |
| 559 | + |
| 560 | + // Cleanup. |
| 561 | + producer.close(); |
| 562 | + admin.topics().delete(topicName, false); |
| 563 | + } |
| 564 | + |
519 | 565 | @Test
|
520 | 566 | public void testAutoProduceSchemaAlwaysCompatible() throws Exception {
|
521 | 567 | final String tenant = PUBLIC_TENANT;
|
|
0 commit comments