Search in sources :

Example 1 with Schemas

use of org.apache.pulsar.client.admin.Schemas in project pulsar by apache.

the class PulsarAdminToolTest method topics.

@Test
public void topics() throws Exception {
    PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
    Topics mockTopics = mock(Topics.class);
    when(admin.topics()).thenReturn(mockTopics);
    Schemas mockSchemas = mock(Schemas.class);
    when(admin.schemas()).thenReturn(mockSchemas);
    Lookup mockLookup = mock(Lookup.class);
    when(admin.lookups()).thenReturn(mockLookup);
    CmdTopics cmdTopics = new CmdTopics(() -> admin);
    cmdTopics.run(split("truncate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).truncate("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("delete persistent://myprop/clust/ns1/ds1 -f -d"));
    verify(mockTopics).delete("persistent://myprop/clust/ns1/ds1", true, true);
    cmdTopics.run(split("unload persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).unload("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("permissions persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getPermissions("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("grant-permission persistent://myprop/clust/ns1/ds1 --role admin --actions produce,consume"));
    verify(mockTopics).grantPermission("persistent://myprop/clust/ns1/ds1", "admin", Sets.newHashSet(AuthAction.produce, AuthAction.consume));
    cmdTopics.run(split("revoke-permission persistent://myprop/clust/ns1/ds1 --role admin"));
    verify(mockTopics).revokePermissions("persistent://myprop/clust/ns1/ds1", "admin");
    cmdTopics.run(split("list myprop/clust/ns1"));
    verify(mockTopics).getList("myprop/clust/ns1", null, null);
    cmdTopics.run(split("lookup persistent://myprop/clust/ns1/ds1"));
    verify(mockLookup).lookupTopic("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("partitioned-lookup persistent://myprop/clust/ns1/ds1"));
    verify(mockLookup).lookupPartitionedTopic("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("partitioned-lookup persistent://myprop/clust/ns1/ds1 --sort-by-broker"));
    verify(mockLookup).lookupPartitionedTopic("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("bundle-range persistent://myprop/clust/ns1/ds1"));
    verify(mockLookup).getBundleRange("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("subscriptions persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getSubscriptions("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("unsubscribe persistent://myprop/clust/ns1/ds1 -s sub1"));
    verify(mockTopics).deleteSubscription("persistent://myprop/clust/ns1/ds1", "sub1", false);
    cmdTopics.run(split("stats persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getStats("persistent://myprop/clust/ns1/ds1", false, false, false);
    cmdTopics.run(split("stats-internal persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getInternalStats("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("get-backlog-quotas persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getBacklogQuotaMap("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("set-backlog-quota persistent://myprop/clust/ns1/ds1 -l 10 -p producer_request_hold"));
    verify(mockTopics).setBacklogQuota("persistent://myprop/clust/ns1/ds1", BacklogQuota.builder().limitSize(10).retentionPolicy(RetentionPolicy.producer_request_hold).build(), BacklogQuota.BacklogQuotaType.destination_storage);
    // cmd with option cannot be executed repeatedly.
    cmdTopics = new CmdTopics(() -> admin);
    cmdTopics.run(split("set-backlog-quota persistent://myprop/clust/ns1/ds1 -lt 1000 -p producer_request_hold -t message_age"));
    verify(mockTopics).setBacklogQuota("persistent://myprop/clust/ns1/ds1", BacklogQuota.builder().limitSize(-1).limitTime(1000).retentionPolicy(RetentionPolicy.producer_request_hold).build(), BacklogQuota.BacklogQuotaType.message_age);
    // cmd with option cannot be executed repeatedly.
    cmdTopics = new CmdTopics(() -> admin);
    cmdTopics.run(split("remove-backlog-quota persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeBacklogQuota("persistent://myprop/clust/ns1/ds1", BacklogQuota.BacklogQuotaType.destination_storage);
    // cmd with option cannot be executed repeatedly.
    cmdTopics = new CmdTopics(() -> admin);
    cmdTopics.run(split("remove-backlog-quota persistent://myprop/clust/ns1/ds1 -t message_age"));
    verify(mockTopics).removeBacklogQuota("persistent://myprop/clust/ns1/ds1", BacklogQuota.BacklogQuotaType.message_age);
    cmdTopics.run(split("info-internal persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getInternalInfo("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("partitioned-stats persistent://myprop/clust/ns1/ds1 --per-partition"));
    verify(mockTopics).getPartitionedStats("persistent://myprop/clust/ns1/ds1", true, false, false);
    cmdTopics.run(split("partitioned-stats-internal persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getPartitionedInternalStats("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("clear-backlog persistent://myprop/clust/ns1/ds1 -s sub1"));
    verify(mockTopics).skipAllMessages("persistent://myprop/clust/ns1/ds1", "sub1");
    cmdTopics.run(split("skip persistent://myprop/clust/ns1/ds1 -s sub1 -n 100"));
    verify(mockTopics).skipMessages("persistent://myprop/clust/ns1/ds1", "sub1", 100);
    cmdTopics.run(split("expire-messages persistent://myprop/clust/ns1/ds1 -s sub1 -t 100"));
    verify(mockTopics).expireMessages("persistent://myprop/clust/ns1/ds1", "sub1", 100);
    cmdTopics.run(split("get-subscribe-rate persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getSubscribeRate("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("set-subscribe-rate persistent://myprop/clust/ns1/ds1 -sr 2 -st 60"));
    verify(mockTopics).setSubscribeRate("persistent://myprop/clust/ns1/ds1", new SubscribeRate(2, 60));
    cmdTopics.run(split("remove-subscribe-rate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeSubscribeRate("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-replicated-subscription-status persistent://myprop/clust/ns1/ds1 -s sub1 -e"));
    verify(mockTopics).setReplicatedSubscriptionStatus("persistent://myprop/clust/ns1/ds1", "sub1", true);
    // cmd with option cannot be executed repeatedly.
    cmdTopics = new CmdTopics(() -> admin);
    cmdTopics.run(split("expire-messages persistent://myprop/clust/ns1/ds1 -s sub1 -p 1:1 -e"));
    verify(mockTopics).expireMessages(eq("persistent://myprop/clust/ns1/ds1"), eq("sub1"), eq(new MessageIdImpl(1, 1, -1)), eq(true));
    cmdTopics.run(split("expire-messages-all-subscriptions persistent://myprop/clust/ns1/ds1 -t 100"));
    verify(mockTopics).expireMessagesForAllSubscriptions("persistent://myprop/clust/ns1/ds1", 100);
    cmdTopics.run(split("create-subscription persistent://myprop/clust/ns1/ds1 -s sub1 --messageId earliest"));
    verify(mockTopics).createSubscription("persistent://myprop/clust/ns1/ds1", "sub1", MessageId.earliest);
    cmdTopics.run(split("create-partitioned-topic persistent://myprop/clust/ns1/ds1 --partitions 32"));
    verify(mockTopics).createPartitionedTopic("persistent://myprop/clust/ns1/ds1", 32, null);
    cmdTopics.run(split("create-missed-partitions persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).createMissedPartitions("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("create persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).createNonPartitionedTopic("persistent://myprop/clust/ns1/ds1", new HashMap<>());
    cmdTopics.run(split("list-partitioned-topics myprop/clust/ns1"));
    verify(mockTopics).getPartitionedTopicList("myprop/clust/ns1");
    cmdTopics.run(split("update-partitioned-topic persistent://myprop/clust/ns1/ds1 -p 6"));
    verify(mockTopics).updatePartitionedTopic("persistent://myprop/clust/ns1/ds1", 6, false, false);
    cmdTopics.run(split("get-partitioned-topic-metadata persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getPartitionedTopicMetadata("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("delete-partitioned-topic persistent://myprop/clust/ns1/ds1 -d -f"));
    verify(mockTopics).deletePartitionedTopic("persistent://myprop/clust/ns1/ds1", true, true);
    cmdTopics.run(split("peek-messages persistent://myprop/clust/ns1/ds1 -s sub1 -n 3"));
    verify(mockTopics).peekMessages("persistent://myprop/clust/ns1/ds1", "sub1", 3);
    MessageImpl message = mock(MessageImpl.class);
    when(message.getData()).thenReturn(new byte[] {});
    when(message.getMessageId()).thenReturn(new MessageIdImpl(1L, 1L, 1));
    when(mockTopics.examineMessage("persistent://myprop/clust/ns1/ds1", "latest", 1)).thenReturn(message);
    cmdTopics.run(split("examine-messages persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).examineMessage("persistent://myprop/clust/ns1/ds1", "latest", 1);
    cmdTopics.run(split("enable-deduplication persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).enableDeduplication("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("disable-deduplication persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).enableDeduplication("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("set-deduplication persistent://myprop/clust/ns1/ds1 --disable"));
    verify(mockTopics).setDeduplicationStatus("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("set-subscription-dispatch-rate persistent://myprop/clust/ns1/ds1 -md -1 -bd -1 -dt 2"));
    verify(mockTopics).setSubscriptionDispatchRate("persistent://myprop/clust/ns1/ds1", DispatchRate.builder().dispatchThrottlingRateInMsg(-1).dispatchThrottlingRateInByte(-1).ratePeriodInSecond(2).build());
    cmdTopics.run(split("get-subscription-dispatch-rate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getSubscriptionDispatchRate("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("remove-subscription-dispatch-rate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeSubscriptionDispatchRate("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("remove-deduplication persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeDeduplicationStatus("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-replicator-dispatch-rate persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getReplicatorDispatchRate("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("set-subscription-types-enabled persistent://myprop/clust/ns1/ds1 -t Shared,Failover"));
    verify(mockTopics).setSubscriptionTypesEnabled("persistent://myprop/clust/ns1/ds1", Sets.newHashSet(SubscriptionType.Shared, SubscriptionType.Failover));
    cmdTopics.run(split("get-subscription-types-enabled persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getSubscriptionTypesEnabled("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("remove-subscription-types-enabled persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeSubscriptionTypesEnabled("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-replicator-dispatch-rate persistent://myprop/clust/ns1/ds1 -md 10 -bd 11 -dt 12"));
    verify(mockTopics).setReplicatorDispatchRate("persistent://myprop/clust/ns1/ds1", DispatchRate.builder().dispatchThrottlingRateInMsg(10).dispatchThrottlingRateInByte(11).ratePeriodInSecond(12).build());
    cmdTopics.run(split("remove-replicator-dispatch-rate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeReplicatorDispatchRate("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-deduplication-enabled persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getDeduplicationStatus("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-deduplication persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics, times(2)).getDeduplicationStatus("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-offload-policies persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getOffloadPolicies("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("remove-offload-policies persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeOffloadPolicies("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-delayed-delivery persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getDelayedDeliveryPolicy("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("set-delayed-delivery persistent://myprop/clust/ns1/ds1 -t 10s --enable"));
    verify(mockTopics).setDelayedDeliveryPolicy("persistent://myprop/clust/ns1/ds1", DelayedDeliveryPolicies.builder().tickTime(10000).active(true).build());
    cmdTopics.run(split("remove-delayed-delivery persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeDelayedDeliveryPolicy("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-offload-policies persistent://myprop/clust/ns1/ds1 -d s3 -r region -b bucket -e endpoint -m 8 -rb 9 -t 10 -orp tiered-storage-first"));
    OffloadPoliciesImpl offloadPolicies = OffloadPoliciesImpl.create("s3", "region", "bucket", "endpoint", null, null, null, null, 8, 9, 10L, null, OffloadedReadPriority.TIERED_STORAGE_FIRST);
    verify(mockTopics).setOffloadPolicies("persistent://myprop/clust/ns1/ds1", offloadPolicies);
    cmdTopics.run(split("get-max-unacked-messages-on-consumer persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("get-max-unacked-messages-per-consumer persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics, times(2)).getMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("remove-max-unacked-messages-on-consumer persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("remove-max-unacked-messages-per-consumer persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics, times(2)).removeMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-max-unacked-messages-on-consumer persistent://myprop/clust/ns1/ds1 -m 999"));
    verify(mockTopics).setMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1", 999);
    cmdTopics.run(split("set-max-unacked-messages-per-consumer persistent://myprop/clust/ns1/ds1 -m 999"));
    verify(mockTopics, times(2)).setMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1", 999);
    cmdTopics.run(split("get-max-unacked-messages-on-subscription persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("get-max-unacked-messages-per-subscription persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics, times(2)).getMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("remove-max-unacked-messages-on-subscription persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("remove-max-unacked-messages-per-subscription persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics, times(2)).removeMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-publish-rate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getPublishRate("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-publish-rate persistent://myprop/clust/ns1/ds1 -m 100 -b 10240"));
    verify(mockTopics).setPublishRate("persistent://myprop/clust/ns1/ds1", new PublishRate(100, 10240L));
    cmdTopics.run(split("remove-publish-rate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removePublishRate("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-max-unacked-messages-on-subscription persistent://myprop/clust/ns1/ds1 -m 99"));
    verify(mockTopics).setMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1", 99);
    cmdTopics.run(split("set-max-unacked-messages-per-subscription persistent://myprop/clust/ns1/ds1 -m 99"));
    verify(mockTopics, times(2)).setMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1", 99);
    cmdTopics.run(split("get-compaction-threshold persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getCompactionThreshold("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("set-compaction-threshold persistent://myprop/clust/ns1/ds1 -t 10k"));
    verify(mockTopics).setCompactionThreshold("persistent://myprop/clust/ns1/ds1", 10 * 1024);
    cmdTopics.run(split("remove-compaction-threshold persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeCompactionThreshold("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-max-message-size persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getMaxMessageSize("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("remove-max-message-size persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeMaxMessageSize("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-max-consumers-per-subscription persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getMaxConsumersPerSubscription("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-max-consumers-per-subscription persistent://myprop/clust/ns1/ds1 -c 5"));
    verify(mockTopics).setMaxConsumersPerSubscription("persistent://myprop/clust/ns1/ds1", 5);
    cmdTopics.run(split("remove-max-consumers-per-subscription persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeMaxConsumersPerSubscription("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-max-message-size persistent://myprop/clust/ns1/ds1 -m 99"));
    verify(mockTopics).setMaxMessageSize("persistent://myprop/clust/ns1/ds1", 99);
    cmdTopics.run(split("get-message-by-id persistent://myprop/clust/ns1/ds1 -l 10 -e 2"));
    verify(mockTopics).getMessageById("persistent://myprop/clust/ns1/ds1", 10, 2);
    cmdTopics.run(split("get-dispatch-rate persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getDispatchRate("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("remove-dispatch-rate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeDispatchRate("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-dispatch-rate persistent://myprop/clust/ns1/ds1 -md -1 -bd -1 -dt 2"));
    verify(mockTopics).setDispatchRate("persistent://myprop/clust/ns1/ds1", DispatchRate.builder().dispatchThrottlingRateInMsg(-1).dispatchThrottlingRateInByte(-1).ratePeriodInSecond(2).build());
    cmdTopics.run(split("get-max-producers persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getMaxProducers("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("remove-max-producers persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeMaxProducers("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-max-producers persistent://myprop/clust/ns1/ds1 -p 99"));
    verify(mockTopics).setMaxProducers("persistent://myprop/clust/ns1/ds1", 99);
    cmdTopics.run(split("get-max-consumers persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getMaxConsumers("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("remove-max-consumers persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeMaxConsumers("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-max-consumers persistent://myprop/clust/ns1/ds1 -c 99"));
    verify(mockTopics).setMaxConsumers("persistent://myprop/clust/ns1/ds1", 99);
    cmdTopics.run(split("get-deduplication-snapshot-interval persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getDeduplicationSnapshotInterval("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("remove-deduplication-snapshot-interval persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeDeduplicationSnapshotInterval("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-deduplication-snapshot-interval persistent://myprop/clust/ns1/ds1 -i 99"));
    verify(mockTopics).setDeduplicationSnapshotInterval("persistent://myprop/clust/ns1/ds1", 99);
    cmdTopics.run(split("get-inactive-topic-policies persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getInactiveTopicPolicies("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("remove-inactive-topic-policies persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeInactiveTopicPolicies("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-inactive-topic-policies persistent://myprop/clust/ns1/ds1" + " -e -t 1s -m delete_when_no_subscriptions"));
    verify(mockTopics).setInactiveTopicPolicies("persistent://myprop/clust/ns1/ds1", new InactiveTopicPolicies(InactiveTopicDeleteMode.delete_when_no_subscriptions, 1, true));
    cmdTopics.run(split("get-max-subscriptions persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getMaxSubscriptionsPerTopic("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-max-subscriptions persistent://myprop/clust/ns1/ds1 -m 100"));
    verify(mockTopics).setMaxSubscriptionsPerTopic("persistent://myprop/clust/ns1/ds1", 100);
    cmdTopics.run(split("remove-max-subscriptions persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeMaxSubscriptionsPerTopic("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("get-persistence persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getPersistence("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-persistence persistent://myprop/clust/ns1/ds1 -e 2 -w 1 -a 1 -r 100.0"));
    verify(mockTopics).setPersistence("persistent://myprop/clust/ns1/ds1", new PersistencePolicies(2, 1, 1, 100.0d));
    cmdTopics.run(split("remove-persistence persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removePersistence("persistent://myprop/clust/ns1/ds1");
    // range of +/- 1 second of the expected timestamp
    class TimestampMatcher implements ArgumentMatcher<Long> {

        @Override
        public boolean matches(Long timestamp) {
            long expectedTimestamp = System.currentTimeMillis() - (1 * 60 * 1000);
            if (timestamp < (expectedTimestamp + 1000) && timestamp > (expectedTimestamp - 1000)) {
                return true;
            }
            return false;
        }
    }
    cmdTopics.run(split("reset-cursor persistent://myprop/clust/ns1/ds1 -s sub1 -t 1m"));
    verify(mockTopics).resetCursor(eq("persistent://myprop/clust/ns1/ds1"), eq("sub1"), longThat(new TimestampMatcher()));
    when(mockTopics.terminateTopicAsync("persistent://myprop/clust/ns1/ds1")).thenReturn(CompletableFuture.completedFuture(new MessageIdImpl(1L, 1L, 1)));
    cmdTopics.run(split("terminate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).terminateTopicAsync("persistent://myprop/clust/ns1/ds1");
    Map<Integer, MessageId> results = new HashMap<>();
    results.put(0, new MessageIdImpl(1, 1, 0));
    when(mockTopics.terminatePartitionedTopic("persistent://myprop/clust/ns1/ds1")).thenReturn(results);
    cmdTopics.run(split("partitioned-terminate persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).terminatePartitionedTopic("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("compact persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).triggerCompaction("persistent://myprop/clust/ns1/ds1");
    when(mockTopics.compactionStatus("persistent://myprop/clust/ns1/ds1")).thenReturn(new LongRunningProcessStatus());
    cmdTopics.run(split("compaction-status persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).compactionStatus("persistent://myprop/clust/ns1/ds1");
    PersistentTopicInternalStats stats = new PersistentTopicInternalStats();
    stats.ledgers = new ArrayList<>();
    stats.ledgers.add(newLedger(0, 10, 1000));
    stats.ledgers.add(newLedger(1, 10, 2000));
    stats.ledgers.add(newLedger(2, 10, 3000));
    when(mockTopics.getInternalStats("persistent://myprop/clust/ns1/ds1", false)).thenReturn(stats);
    cmdTopics.run(split("offload persistent://myprop/clust/ns1/ds1 -s 1k"));
    verify(mockTopics).triggerOffload("persistent://myprop/clust/ns1/ds1", new MessageIdImpl(2, 0, -1));
    when(mockTopics.offloadStatus("persistent://myprop/clust/ns1/ds1")).thenReturn(new OffloadProcessStatusImpl());
    cmdTopics.run(split("offload-status persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).offloadStatus("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("last-message-id persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getLastMessageId(eq("persistent://myprop/clust/ns1/ds1"));
    cmdTopics.run(split("get-message-ttl persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).getMessageTTL("persistent://myprop/clust/ns1/ds1", false);
    cmdTopics.run(split("set-message-ttl persistent://myprop/clust/ns1/ds1 -t 10"));
    verify(mockTopics).setMessageTTL("persistent://myprop/clust/ns1/ds1", 10);
    cmdTopics.run(split("remove-message-ttl persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeMessageTTL("persistent://myprop/clust/ns1/ds1");
    cmdTopics.run(split("set-replicated-subscription-status persistent://myprop/clust/ns1/ds1 -s sub1 -d"));
    verify(mockTopics).setReplicatedSubscriptionStatus("persistent://myprop/clust/ns1/ds1", "sub1", false);
    cmdTopics.run(split("get-replicated-subscription-status persistent://myprop/clust/ns1/ds1 -s sub1"));
    verify(mockTopics).getReplicatedSubscriptionStatus("persistent://myprop/clust/ns1/ds1", "sub1");
    // cmd with option cannot be executed repeatedly.
    cmdTopics = new CmdTopics(() -> admin);
    cmdTopics.run(split("get-max-unacked-messages-on-subscription persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("reset-cursor persistent://myprop/clust/ns1/ds2 -s sub1 -m 1:1 -e"));
    verify(mockTopics).resetCursor(eq("persistent://myprop/clust/ns1/ds2"), eq("sub1"), eq(new MessageIdImpl(1, 1, -1)), eq(true));
    cmdTopics.run(split("get-maxProducers persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getMaxProducers("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("set-maxProducers persistent://myprop/clust/ns1/ds1 -p 3"));
    verify(mockTopics).setMaxProducers("persistent://myprop/clust/ns1/ds1", 3);
    cmdTopics.run(split("remove-maxProducers persistent://myprop/clust/ns1/ds2"));
    verify(mockTopics).removeMaxProducers("persistent://myprop/clust/ns1/ds2");
    cmdTopics.run(split("get-message-ttl persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getMessageTTL("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("get-offload-policies persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getOffloadPolicies("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("get-max-unacked-messages-on-consumer persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("get-inactive-topic-policies persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getInactiveTopicPolicies("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("get-delayed-delivery persistent://myprop/clust/ns1/ds1 --applied"));
    verify(mockTopics).getDelayedDeliveryPolicy("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("get-max-consumers persistent://myprop/clust/ns1/ds1 -ap"));
    verify(mockTopics).getMaxConsumers("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("get-replication-clusters persistent://myprop/clust/ns1/ds1 --applied"));
    verify(mockTopics).getReplicationClusters("persistent://myprop/clust/ns1/ds1", true);
    cmdTopics.run(split("set-replication-clusters persistent://myprop/clust/ns1/ds1 -c test"));
    verify(mockTopics).setReplicationClusters("persistent://myprop/clust/ns1/ds1", Lists.newArrayList("test"));
    cmdTopics.run(split("remove-replication-clusters persistent://myprop/clust/ns1/ds1"));
    verify(mockTopics).removeReplicationClusters("persistent://myprop/clust/ns1/ds1");
}
Also used : PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) HashMap(java.util.HashMap) OffloadProcessStatusImpl(org.apache.pulsar.client.admin.internal.OffloadProcessStatusImpl) ArgumentMatcher(org.mockito.ArgumentMatcher) PersistentTopicInternalStats(org.apache.pulsar.common.policies.data.PersistentTopicInternalStats) Lookup(org.apache.pulsar.client.admin.Lookup) NonPersistentTopics(org.apache.pulsar.client.admin.NonPersistentTopics) Topics(org.apache.pulsar.client.admin.Topics) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) LongRunningProcessStatus(org.apache.pulsar.client.admin.LongRunningProcessStatus) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) PublishRate(org.apache.pulsar.common.policies.data.PublishRate) Schemas(org.apache.pulsar.client.admin.Schemas) OffloadPoliciesImpl(org.apache.pulsar.common.policies.data.OffloadPoliciesImpl) SubscribeRate(org.apache.pulsar.common.policies.data.SubscribeRate) InactiveTopicPolicies(org.apache.pulsar.common.policies.data.InactiveTopicPolicies) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 2 with Schemas

use of org.apache.pulsar.client.admin.Schemas in project pulsar by apache.

the class TestCmdTopics method setup.

@BeforeMethod
public void setup() throws Exception {
    pulsarAdmin = Mockito.mock(PulsarAdmin.class);
    Topics mockTopics = mock(Topics.class);
    when(pulsarAdmin.topics()).thenReturn(mockTopics);
    Schemas mockSchemas = mock(Schemas.class);
    when(pulsarAdmin.schemas()).thenReturn(mockSchemas);
    mockLookup = mock(Lookup.class);
    when(pulsarAdmin.lookups()).thenReturn(mockLookup);
    cmdTopics = spy(new CmdTopics(() -> pulsarAdmin));
    partitionedLookup = spy(cmdTopics.getPartitionedLookup());
}
Also used : Topics(org.apache.pulsar.client.admin.Topics) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Lookup(org.apache.pulsar.client.admin.Lookup) Schemas(org.apache.pulsar.client.admin.Schemas) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with Schemas

use of org.apache.pulsar.client.admin.Schemas in project pulsar by apache.

the class TestPulsarConnector method setup.

@BeforeMethod
public void setup() throws Exception {
    this.pulsarConnectorConfig = spy(PulsarConnectorConfig.class);
    this.pulsarConnectorConfig.setMaxEntryReadBatchSize(1);
    this.pulsarConnectorConfig.setMaxSplitEntryQueueSize(10);
    this.pulsarConnectorConfig.setMaxSplitMessageQueueSize(100);
    Tenants tenants = mock(Tenants.class);
    doReturn(new LinkedList<>(topicNames.stream().map(TopicName::getTenant).collect(Collectors.toSet()))).when(tenants).getTenants();
    Namespaces namespaces = mock(Namespaces.class);
    when(namespaces.getNamespaces(anyString())).thenAnswer(new Answer<List<String>>() {

        @Override
        public List<String> answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            String tenant = (String) args[0];
            List<String> ns = getNamespace(tenant);
            if (ns.isEmpty()) {
                ClientErrorException cee = new ClientErrorException(Response.status(404).build());
                throw new PulsarAdminException(cee, cee.getMessage(), cee.getResponse().getStatus());
            }
            return ns;
        }
    });
    Topics topics = mock(Topics.class);
    when(topics.getList(anyString(), any())).thenAnswer(new Answer<List<String>>() {

        @Override
        public List<String> answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String ns = (String) args[0];
            List<String> topics = getTopics(ns);
            if (topics.isEmpty()) {
                ClientErrorException cee = new ClientErrorException(Response.status(404).build());
                throw new PulsarAdminException(cee, cee.getMessage(), cee.getResponse().getStatus());
            }
            return topics;
        }
    });
    when(topics.getPartitionedTopicList(anyString())).thenAnswer(new Answer<List<String>>() {

        @Override
        public List<String> answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String ns = (String) args[0];
            List<String> topics = getPartitionedTopics(ns);
            if (topics.isEmpty()) {
                ClientErrorException cee = new ClientErrorException(Response.status(404).build());
                throw new PulsarAdminException(cee, cee.getMessage(), cee.getResponse().getStatus());
            }
            return topics;
        }
    });
    when(topics.getPartitionedTopicMetadata(anyString())).thenAnswer(new Answer<PartitionedTopicMetadata>() {

        @Override
        public PartitionedTopicMetadata answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String topic = (String) args[0];
            int partitions = partitionedTopicsToPartitions.get(topic) == null ? 0 : partitionedTopicsToPartitions.get(topic);
            return new PartitionedTopicMetadata(partitions);
        }
    });
    schemas = mock(Schemas.class);
    when(schemas.getSchemaInfo(anyString())).thenAnswer(new Answer<SchemaInfo>() {

        @Override
        public SchemaInfo answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String topic = (String) args[0];
            if (topicsToSchemas.get(topic) != null) {
                return topicsToSchemas.get(topic);
            } else {
                ClientErrorException cee = new ClientErrorException(Response.status(404).build());
                throw new PulsarAdminException(cee, cee.getMessage(), cee.getResponse().getStatus());
            }
        }
    });
    pulsarAdmin = mock(PulsarAdmin.class);
    doReturn(tenants).when(pulsarAdmin).tenants();
    doReturn(namespaces).when(pulsarAdmin).namespaces();
    doReturn(topics).when(pulsarAdmin).topics();
    doReturn(schemas).when(pulsarAdmin).schemas();
    doReturn(pulsarAdmin).when(this.pulsarConnectorConfig).getPulsarAdmin();
    this.pulsarMetadata = new PulsarMetadata(pulsarConnectorId, this.pulsarConnectorConfig, dispatchingRowDecoderFactory);
    this.pulsarSplitManager = Mockito.spy(new PulsarSplitManager(pulsarConnectorId, this.pulsarConnectorConfig));
    ManagedLedgerFactory managedLedgerFactory = mock(ManagedLedgerFactory.class);
    when(managedLedgerFactory.openReadOnlyCursor(any(), any(), any())).then(new Answer<ReadOnlyCursor>() {

        private Map<String, Integer> positions = new HashMap<>();

        private int count = 0;

        @Override
        public ReadOnlyCursor answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String topic = (String) args[0];
            PositionImpl positionImpl = (PositionImpl) args[1];
            int position = positionImpl.getEntryId() == -1 ? 0 : (int) positionImpl.getEntryId();
            positions.put(topic, position);
            String schemaName = TopicName.get(TopicName.get(topic.replaceAll("/persistent", "")).getPartitionedTopicName()).getSchemaName();
            long entries = topicsToNumEntries.get(schemaName);
            ReadOnlyCursorImpl readOnlyCursor = mock(ReadOnlyCursorImpl.class);
            doReturn(entries).when(readOnlyCursor).getNumberOfEntries();
            doAnswer(new Answer<Void>() {

                @Override
                public Void answer(InvocationOnMock invocation) throws Throwable {
                    Object[] args = invocation.getArguments();
                    Integer skipEntries = (Integer) args[0];
                    positions.put(topic, positions.get(topic) + skipEntries);
                    return null;
                }
            }).when(readOnlyCursor).skipEntries(anyInt());
            when(readOnlyCursor.getReadPosition()).thenAnswer(new Answer<PositionImpl>() {

                @Override
                public PositionImpl answer(InvocationOnMock invocationOnMock) throws Throwable {
                    return PositionImpl.get(0, positions.get(topic));
                }
            });
            doAnswer(new Answer() {

                @Override
                public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                    Object[] args = invocationOnMock.getArguments();
                    Integer readEntries = (Integer) args[0];
                    AsyncCallbacks.ReadEntriesCallback callback = (AsyncCallbacks.ReadEntriesCallback) args[2];
                    Object ctx = args[3];
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            List<Entry> entries = new LinkedList<>();
                            for (int i = 0; i < readEntries; i++) {
                                TestPulsarConnector.Bar bar = new TestPulsarConnector.Bar();
                                bar.field1 = fooFunctions.get("bar.field1").apply(count) == null ? null : (int) fooFunctions.get("bar.field1").apply(count);
                                bar.field2 = fooFunctions.get("bar.field2").apply(count) == null ? null : (String) fooFunctions.get("bar.field2").apply(count);
                                bar.field3 = (float) fooFunctions.get("bar.field3").apply(count);
                                Foo foo = new Foo();
                                foo.field1 = (int) fooFunctions.get("field1").apply(count);
                                foo.field2 = (String) fooFunctions.get("field2").apply(count);
                                foo.field3 = (float) fooFunctions.get("field3").apply(count);
                                foo.field4 = (double) fooFunctions.get("field4").apply(count);
                                foo.field5 = (boolean) fooFunctions.get("field5").apply(count);
                                foo.field6 = (long) fooFunctions.get("field6").apply(count);
                                foo.timestamp = (long) fooFunctions.get("timestamp").apply(count);
                                foo.time = (int) fooFunctions.get("time").apply(count);
                                foo.date = (int) fooFunctions.get("date").apply(count);
                                foo.bar = bar;
                                foo.field7 = (Foo.TestEnum) fooFunctions.get("field7").apply(count);
                                MessageMetadata messageMetadata = new MessageMetadata().setProducerName("test-producer").setSequenceId(positions.get(topic)).setPublishTime(System.currentTimeMillis());
                                Schema schema = topicsToSchemas.get(schemaName).getType() == SchemaType.AVRO ? AvroSchema.of(Foo.class) : JSONSchema.of(Foo.class);
                                ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(schema.encode(foo));
                                ByteBuf byteBuf = serializeMetadataAndPayload(Commands.ChecksumType.Crc32c, messageMetadata, payload);
                                completedBytes += byteBuf.readableBytes();
                                entries.add(EntryImpl.create(0, positions.get(topic), byteBuf));
                                positions.put(topic, positions.get(topic) + 1);
                                count++;
                            }
                            callback.readEntriesComplete(entries, ctx);
                        }
                    }).start();
                    return null;
                }
            }).when(readOnlyCursor).asyncReadEntries(anyInt(), anyLong(), any(), any(), any());
            when(readOnlyCursor.hasMoreEntries()).thenAnswer(new Answer<Boolean>() {

                @Override
                public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
                    return positions.get(topic) < entries;
                }
            });
            when(readOnlyCursor.findNewestMatching(any(), any())).then(new Answer<Position>() {

                @Override
                public Position answer(InvocationOnMock invocationOnMock) throws Throwable {
                    Object[] args = invocationOnMock.getArguments();
                    com.google.common.base.Predicate<Entry> predicate = (com.google.common.base.Predicate<Entry>) args[1];
                    String schemaName = TopicName.get(TopicName.get(topic.replaceAll("/persistent", "")).getPartitionedTopicName()).getSchemaName();
                    List<Entry> entries = getTopicEntries(schemaName);
                    Integer target = null;
                    for (int i = entries.size() - 1; i >= 0; i--) {
                        Entry entry = entries.get(i);
                        if (predicate.apply(entry)) {
                            target = i;
                            break;
                        }
                    }
                    return target == null ? null : new PositionImpl(0, target);
                }
            });
            when(readOnlyCursor.getNumberOfEntries(any())).then(new Answer<Long>() {

                @Override
                public Long answer(InvocationOnMock invocationOnMock) throws Throwable {
                    Object[] args = invocationOnMock.getArguments();
                    com.google.common.collect.Range<PositionImpl> range = (com.google.common.collect.Range<PositionImpl>) args[0];
                    return (range.upperEndpoint().getEntryId() + 1) - range.lowerEndpoint().getEntryId();
                }
            });
            when(readOnlyCursor.getCurrentLedgerInfo()).thenReturn(MLDataFormats.ManagedLedgerInfo.LedgerInfo.newBuilder().setLedgerId(0).build());
            return readOnlyCursor;
        }
    });
    PulsarConnectorCache.instance = mock(PulsarConnectorCache.class);
    when(PulsarConnectorCache.instance.getManagedLedgerFactory()).thenReturn(managedLedgerFactory);
    for (Map.Entry<TopicName, PulsarSplit> split : splits.entrySet()) {
        PulsarRecordCursor pulsarRecordCursor = spy(new PulsarRecordCursor(topicsToColumnHandles.get(split.getKey()), split.getValue(), pulsarConnectorConfig, managedLedgerFactory, new ManagedLedgerConfig(), new PulsarConnectorMetricsTracker(new NullStatsProvider()), dispatchingRowDecoderFactory));
        this.pulsarRecordCursors.put(split.getKey(), pulsarRecordCursor);
    }
}
Also used : Namespaces(org.apache.pulsar.client.admin.Namespaces) HashMap(java.util.HashMap) JSONSchema(org.apache.pulsar.client.impl.schema.JSONSchema) AvroSchema(org.apache.pulsar.client.impl.schema.AvroSchema) Schema(org.apache.pulsar.client.api.Schema) Mockito.anyString(org.mockito.Mockito.anyString) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Schemas(org.apache.pulsar.client.admin.Schemas) LinkedList(java.util.LinkedList) TopicName(org.apache.pulsar.common.naming.TopicName) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Map(java.util.Map) HashMap(java.util.HashMap) ReadOnlyCursorImpl(org.apache.bookkeeper.mledger.impl.ReadOnlyCursorImpl) ReadOnlyCursor(org.apache.bookkeeper.mledger.ReadOnlyCursor) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) Tenants(org.apache.pulsar.client.admin.Tenants) ByteBuf(io.netty.buffer.ByteBuf) Entry(org.apache.bookkeeper.mledger.Entry) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) Topics(org.apache.pulsar.client.admin.Topics) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClientErrorException(javax.ws.rs.ClientErrorException) NullStatsProvider(org.apache.bookkeeper.stats.NullStatsProvider) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with Schemas

use of org.apache.pulsar.client.admin.Schemas in project pulsar by apache.

the class TestPulsarRecordCursor method testGetSchemaInfo.

@Test
public void testGetSchemaInfo() throws Exception {
    String topic = "get-schema-test";
    PulsarSplit pulsarSplit = Mockito.mock(PulsarSplit.class);
    Mockito.when(pulsarSplit.getTableName()).thenReturn(TopicName.get(topic).getLocalName());
    Mockito.when(pulsarSplit.getSchemaName()).thenReturn("public/default");
    PulsarAdmin pulsarAdmin = Mockito.mock(PulsarAdmin.class);
    Schemas schemas = Mockito.mock(Schemas.class);
    Mockito.when(pulsarAdmin.schemas()).thenReturn(schemas);
    PulsarConnectorConfig connectorConfig = spy(PulsarConnectorConfig.class);
    Mockito.when(connectorConfig.getPulsarAdmin()).thenReturn(pulsarAdmin);
    PulsarRecordCursor pulsarRecordCursor = spy(new PulsarRecordCursor(new ArrayList<>(), pulsarSplit, connectorConfig, Mockito.mock(ManagedLedgerFactory.class), new ManagedLedgerConfig(), null, null));
    Class<PulsarRecordCursor> clazz = PulsarRecordCursor.class;
    Method getSchemaInfo = clazz.getDeclaredMethod("getSchemaInfo", PulsarSplit.class);
    getSchemaInfo.setAccessible(true);
    Field currentMessage = clazz.getDeclaredField("currentMessage");
    currentMessage.setAccessible(true);
    RawMessage rawMessage = Mockito.mock(RawMessage.class);
    currentMessage.set(pulsarRecordCursor, rawMessage);
    // If the schemaType of pulsarSplit is NONE or BYTES, using bytes schema
    Mockito.when(pulsarSplit.getSchemaType()).thenReturn(SchemaType.NONE);
    SchemaInfo schemaInfo = (SchemaInfo) getSchemaInfo.invoke(pulsarRecordCursor, pulsarSplit);
    assertEquals(SchemaType.BYTES, schemaInfo.getType());
    Mockito.when(pulsarSplit.getSchemaType()).thenReturn(SchemaType.BYTES);
    schemaInfo = (SchemaInfo) getSchemaInfo.invoke(pulsarRecordCursor, pulsarSplit);
    assertEquals(SchemaType.BYTES, schemaInfo.getType());
    Mockito.when(pulsarSplit.getSchemaName()).thenReturn(Schema.BYTEBUFFER.getSchemaInfo().getName());
    schemaInfo = (SchemaInfo) getSchemaInfo.invoke(pulsarRecordCursor, pulsarSplit);
    assertEquals(SchemaType.BYTES, schemaInfo.getType());
    // If the schemaVersion of the message is not null, try to get the schema.
    Mockito.when(pulsarSplit.getSchemaType()).thenReturn(SchemaType.AVRO);
    Mockito.when(rawMessage.getSchemaVersion()).thenReturn(new LongSchemaVersion(0).bytes());
    Mockito.when(schemas.getSchemaInfo(anyString(), eq(0L))).thenReturn(Schema.AVRO(Foo.class).getSchemaInfo());
    schemaInfo = (SchemaInfo) getSchemaInfo.invoke(pulsarRecordCursor, pulsarSplit);
    assertEquals(SchemaType.AVRO, schemaInfo.getType());
    String schemaTopic = "persistent://public/default/" + topic;
    // If the schemaVersion of the message is null and the schema of pulsarSplit is null, throw runtime exception.
    Mockito.when(pulsarSplit.getSchemaInfo()).thenReturn(null);
    Mockito.when(rawMessage.getSchemaVersion()).thenReturn(null);
    try {
        schemaInfo = (SchemaInfo) getSchemaInfo.invoke(pulsarRecordCursor, pulsarSplit);
        fail("The message schema version is null and the latest schema is null, should fail.");
    } catch (InvocationTargetException e) {
        assertTrue(e.getCause() instanceof RuntimeException);
        assertTrue(e.getCause().getMessage().contains("schema of the table " + topic + " is null"));
    }
    // If the schemaVersion of the message is null, try to get the latest schema.
    Mockito.when(rawMessage.getSchemaVersion()).thenReturn(null);
    Mockito.when(pulsarSplit.getSchemaInfo()).thenReturn(Schema.AVRO(Foo.class).getSchemaInfo());
    schemaInfo = (SchemaInfo) getSchemaInfo.invoke(pulsarRecordCursor, pulsarSplit);
    assertEquals(Schema.AVRO(Foo.class).getSchemaInfo(), schemaInfo);
    // If the specific version schema is null, throw runtime exception.
    Mockito.when(rawMessage.getSchemaVersion()).thenReturn(new LongSchemaVersion(1L).bytes());
    Mockito.when(schemas.getSchemaInfo(schemaTopic, 1)).thenReturn(null);
    try {
        schemaInfo = (SchemaInfo) getSchemaInfo.invoke(pulsarRecordCursor, pulsarSplit);
        fail("The specific version " + 1 + " schema is null, should fail.");
    } catch (InvocationTargetException e) {
        String schemaVersion = BytesSchemaVersion.of(new LongSchemaVersion(1L).bytes()).toString();
        assertTrue(e.getCause() instanceof RuntimeException);
        assertTrue(e.getCause().getMessage().contains("schema of the topic " + schemaTopic + " is null"));
    }
    // Get the specific version schema.
    Mockito.when(rawMessage.getSchemaVersion()).thenReturn(new LongSchemaVersion(2L).bytes());
    Mockito.when(schemas.getSchemaInfo(schemaTopic, 2)).thenReturn(Schema.AVRO(Foo.class).getSchemaInfo());
    schemaInfo = (SchemaInfo) getSchemaInfo.invoke(pulsarRecordCursor, pulsarSplit);
    assertEquals(Schema.AVRO(Foo.class).getSchemaInfo(), schemaInfo);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Method(java.lang.reflect.Method) Schemas(org.apache.pulsar.client.admin.Schemas) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) RawMessage(org.apache.pulsar.common.api.raw.RawMessage) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) Test(org.testng.annotations.Test)

Example 5 with Schemas

use of org.apache.pulsar.client.admin.Schemas in project pulsar by yahoo.

the class PulsarAdminToolTest method schemas.

@Test
void schemas() throws Exception {
    PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
    Schemas schemas = Mockito.mock(Schemas.class);
    doReturn(schemas).when(admin).schemas();
    CmdSchemas cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("get -v 1 persistent://tn1/ns1/tp1"));
    verify(schemas).getSchemaInfo("persistent://tn1/ns1/tp1", 1);
    cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("get -a persistent://tn1/ns1/tp1"));
    verify(schemas).getAllSchemas("persistent://tn1/ns1/tp1");
    cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("get persistent://tn1/ns1/tp1"));
    verify(schemas).getSchemaInfoWithVersion("persistent://tn1/ns1/tp1");
    cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("delete persistent://tn1/ns1/tp1"));
    verify(schemas).deleteSchema("persistent://tn1/ns1/tp1", false);
    cmdSchemas = new CmdSchemas(() -> admin);
    cmdSchemas.run(split("delete persistent://tn1/ns1/tp1 -f"));
    verify(schemas).deleteSchema("persistent://tn1/ns1/tp1", true);
    cmdSchemas = new CmdSchemas(() -> admin);
    String schemaFile = PulsarAdminToolTest.class.getClassLoader().getResource("test_schema_create.json").getFile();
    cmdSchemas.run(split("upload -f " + schemaFile + " persistent://tn1/ns1/tp1"));
    PostSchemaPayload input = new ObjectMapper().readValue(new File(schemaFile), PostSchemaPayload.class);
    verify(schemas).createSchema("persistent://tn1/ns1/tp1", input);
    cmdSchemas = new CmdSchemas(() -> admin);
    String jarFile = PulsarAdminToolTest.class.getClassLoader().getResource("dummyexamples.jar").getFile();
    String className = SchemaDemo.class.getName();
    cmdSchemas.run(split("extract -j " + jarFile + " -c " + className + " -t json persistent://tn1/ns1/tp1"));
    File file = new File(jarFile);
    ClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });
    Class cls = cl.loadClass(className);
    SchemaDefinition<Object> schemaDefinition = SchemaDefinition.builder().withPojo(cls).withAlwaysAllowNull(true).build();
    PostSchemaPayload postSchemaPayload = new PostSchemaPayload();
    postSchemaPayload.setType("JSON");
    postSchemaPayload.setSchema(SchemaExtractor.getJsonSchemaInfo(schemaDefinition));
    postSchemaPayload.setProperties(schemaDefinition.getProperties());
    verify(schemas).createSchema("persistent://tn1/ns1/tp1", postSchemaPayload);
}
Also used : PostSchemaPayload(org.apache.pulsar.common.protocol.schema.PostSchemaPayload) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Schemas(org.apache.pulsar.client.admin.Schemas) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Aggregations

PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)18 Schemas (org.apache.pulsar.client.admin.Schemas)18 Test (org.testng.annotations.Test)12 Lookup (org.apache.pulsar.client.admin.Lookup)9 Topics (org.apache.pulsar.client.admin.Topics)9 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)5 InactiveTopicPolicies (org.apache.pulsar.common.policies.data.InactiveTopicPolicies)4 PersistencePolicies (org.apache.pulsar.common.policies.data.PersistencePolicies)4 PublishRate (org.apache.pulsar.common.policies.data.PublishRate)4 SubscribeRate (org.apache.pulsar.common.policies.data.SubscribeRate)4 ByteBuf (io.netty.buffer.ByteBuf)3 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3