Search in sources :

Example 1 with ProducerBuilderImpl

use of org.apache.pulsar.client.impl.ProducerBuilderImpl in project pulsar by apache.

the class AuthorizationProducerConsumerTest method testPermissionForProducerCreateInitialSubscription.

@Test
public void testPermissionForProducerCreateInitialSubscription() throws Exception {
    log.info("-- Starting {} test --", methodName);
    conf.setAuthorizationProvider(PulsarAuthorizationProvider.class.getName());
    setup();
    String lookupUrl = pulsar.getBrokerServiceUrl();
    final String invalidRole = "invalid-role";
    final String producerRole = "producer-role";
    final String topic = "persistent://my-property/my-ns/my-topic";
    final String initialSubscriptionName = "init-sub";
    TopicName tn = TopicName.get(topic);
    Authentication adminAuthentication = new ClientAuthentication("superUser");
    Authentication authenticationInvalidRole = new ClientAuthentication(invalidRole);
    Authentication authenticationProducerRole = new ClientAuthentication(producerRole);
    @Cleanup PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(brokerUrl.toString()).authentication(adminAuthentication).build();
    admin.clusters().createCluster("test", ClusterData.builder().serviceUrl(brokerUrl.toString()).build());
    admin.tenants().createTenant("my-property", new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet("test")));
    admin.namespaces().createNamespace("my-property/my-ns", Sets.newHashSet("test"));
    admin.topics().grantPermission(topic, invalidRole, Collections.singleton(AuthAction.produce));
    admin.topics().grantPermission(topic, producerRole, Sets.newHashSet(AuthAction.produce, AuthAction.consume));
    @Cleanup PulsarClient pulsarClientInvalidRole = PulsarClient.builder().serviceUrl(lookupUrl).authentication(authenticationInvalidRole).build();
    try {
        Producer<byte[]> invalidRoleProducer = ((ProducerBuilderImpl<byte[]>) pulsarClientInvalidRole.newProducer()).initialSubscriptionName(initialSubscriptionName).topic(topic).create();
        invalidRoleProducer.close();
        fail("Should not pass");
    } catch (PulsarClientException.AuthorizationException ex) {
    // ok
    }
    // If the producer doesn't have permission to create the init sub, we should also avoid creating the topic.
    Assert.assertFalse(admin.namespaces().getTopics(tn.getNamespace()).contains(tn.getLocalName()));
    @Cleanup PulsarClient pulsarClientProducerRole = PulsarClient.builder().serviceUrl(lookupUrl).authentication(authenticationProducerRole).build();
    Producer<byte[]> producer = ((ProducerBuilderImpl<byte[]>) pulsarClientProducerRole.newProducer()).initialSubscriptionName(initialSubscriptionName).topic(topic).create();
    producer.close();
    Assert.assertTrue(admin.topics().getSubscriptions(topic).contains(initialSubscriptionName));
    log.info("-- Exiting {} test --", methodName);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Cleanup(lombok.Cleanup) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) TopicName(org.apache.pulsar.common.naming.TopicName) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) PulsarAuthorizationProvider(org.apache.pulsar.broker.authorization.PulsarAuthorizationProvider) Test(org.testng.annotations.Test)

Example 2 with ProducerBuilderImpl

use of org.apache.pulsar.client.impl.ProducerBuilderImpl in project pulsar by apache.

the class ProducerCreationTest method testInitialSubscriptionCreationWithAutoCreationDisable.

@Test
public void testInitialSubscriptionCreationWithAutoCreationDisable() throws PulsarAdminException, PulsarClientException {
    pulsar.getConfiguration().setAllowAutoSubscriptionCreation(false);
    final TopicName topic = TopicName.get("persistent", "public", "default", "testInitialSubscriptionCreationWithAutoCreationDisable");
    final String initialSubscriptionName = "init-sub";
    admin.topics().createNonPartitionedTopic(topic.toString());
    try {
        Producer<byte[]> producer = ((ProducerBuilderImpl<byte[]>) pulsarClient.newProducer()).initialSubscriptionName(initialSubscriptionName).topic(topic.toString()).create();
        fail("Should not pass");
    } catch (PulsarClientException.NotAllowedException exception) {
    // ok
    }
    Assert.assertFalse(admin.topics().getSubscriptions(topic.toString()).contains(initialSubscriptionName));
}
Also used : ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 3 with ProducerBuilderImpl

use of org.apache.pulsar.client.impl.ProducerBuilderImpl in project pulsar by apache.

the class ProducerCreationTest method testCreateInitialSubscriptionOnPartitionedTopic.

@Test
public void testCreateInitialSubscriptionOnPartitionedTopic() throws PulsarAdminException, PulsarClientException {
    final TopicName topic = TopicName.get("persistent", "public", "default", "testCreateInitialSubscriptionOnPartitionedTopic");
    final String initialSubscriptionName = "init-sub";
    admin.topics().createPartitionedTopic(topic.toString(), 10);
    Producer<byte[]> producer = ((ProducerBuilderImpl<byte[]>) pulsarClient.newProducer()).initialSubscriptionName(initialSubscriptionName).topic(topic.toString()).create();
    producer.close();
    Assert.assertTrue(admin.topics().getSubscriptions(topic.toString()).contains(initialSubscriptionName));
}
Also used : ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 4 with ProducerBuilderImpl

use of org.apache.pulsar.client.impl.ProducerBuilderImpl in project pulsar by apache.

the class ContextImplTest method setup.

@BeforeMethod
public void setup() throws PulsarClientException {
    config = new InstanceConfig();
    config.setExposePulsarAdminClientEnabled(true);
    FunctionDetails functionDetails = FunctionDetails.newBuilder().setUserConfig("").build();
    config.setFunctionDetails(functionDetails);
    logger = mock(Logger.class);
    pulsarAdmin = mock(PulsarAdmin.class);
    client = mock(PulsarClientImpl.class);
    when(client.newProducer()).thenReturn(new ProducerBuilderImpl(client, Schema.BYTES));
    when(client.createProducerAsync(any(ProducerConfigurationData.class), any(), any())).thenReturn(CompletableFuture.completedFuture(producer));
    when(client.getSchema(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    when(producer.sendAsync(anyString())).thenReturn(CompletableFuture.completedFuture(null));
    clientBuilder = mock(ClientBuilder.class);
    when(clientBuilder.build()).thenReturn(client);
    TypedMessageBuilder messageBuilder = spy(new TypedMessageBuilderImpl(mock(ProducerBase.class), Schema.STRING));
    doReturn(new CompletableFuture<>()).when(messageBuilder).sendAsync();
    when(producer.newMessage()).thenReturn(messageBuilder);
    context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
    context.setCurrentMessageContext((Record<String>) () -> null);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(org.slf4j.Logger) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) EnvironmentBasedSecretsProvider(org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with ProducerBuilderImpl

use of org.apache.pulsar.client.impl.ProducerBuilderImpl in project pulsar by yahoo.

the class ProducerCreationTest method testInitialSubscriptionCreation.

@Test(dataProvider = "topicDomainProvider")
public void testInitialSubscriptionCreation(TopicDomain domain) throws PulsarClientException, PulsarAdminException {
    final String initialSubscriptionName = "init-sub";
    final TopicName topic = TopicName.get(domain.value(), "public", "default", "testInitialSubscriptionCreation");
    // Should not create initial subscription when the initialSubscriptionName is null or empty
    Producer<byte[]> nullInitSubProducer = ((ProducerBuilderImpl<byte[]>) pulsarClient.newProducer()).initialSubscriptionName(null).topic(topic.toString()).create();
    nullInitSubProducer.close();
    Assert.assertFalse(admin.topics().getSubscriptions(topic.toString()).contains(initialSubscriptionName));
    Producer<byte[]> emptyInitSubProducer = ((ProducerBuilderImpl<byte[]>) pulsarClient.newProducer()).initialSubscriptionName("").topic(topic.toString()).create();
    emptyInitSubProducer.close();
    Assert.assertFalse(admin.topics().getSubscriptions(topic.toString()).contains(initialSubscriptionName));
    Producer<byte[]> producer = ((ProducerBuilderImpl<byte[]>) pulsarClient.newProducer()).initialSubscriptionName(initialSubscriptionName).topic(topic.toString()).create();
    producer.close();
    // Initial subscription will only be created if the topic is persistent
    Assert.assertEquals(topic.isPersistent(), admin.topics().getSubscriptions(topic.toString()).contains(initialSubscriptionName));
    // Existing subscription should not fail the producer creation.
    Producer<byte[]> otherProducer = ((ProducerBuilderImpl<byte[]>) pulsarClient.newProducer()).initialSubscriptionName(initialSubscriptionName).topic(topic.toString()).create();
    otherProducer.close();
    Assert.assertEquals(topic.isPersistent(), admin.topics().getSubscriptions(topic.toString()).contains(initialSubscriptionName));
}
Also used : ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Aggregations

ProducerBuilderImpl (org.apache.pulsar.client.impl.ProducerBuilderImpl)20 Test (org.testng.annotations.Test)17 TopicName (org.apache.pulsar.common.naming.TopicName)15 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)6 ClientBuilder (org.apache.pulsar.client.api.ClientBuilder)4 ProducerConfigurationData (org.apache.pulsar.client.impl.conf.ProducerConfigurationData)4 Cleanup (lombok.Cleanup)3 PulsarAuthorizationProvider (org.apache.pulsar.broker.authorization.PulsarAuthorizationProvider)3 TypedMessageBuilder (org.apache.pulsar.client.api.TypedMessageBuilder)3 PulsarClientImpl (org.apache.pulsar.client.impl.PulsarClientImpl)3 TypedMessageBuilderImpl (org.apache.pulsar.client.impl.TypedMessageBuilderImpl)3 TenantInfoImpl (org.apache.pulsar.common.policies.data.TenantInfoImpl)3 InstanceStateManager (org.apache.pulsar.functions.instance.state.InstanceStateManager)3 FunctionDetails (org.apache.pulsar.functions.proto.Function.FunctionDetails)3 EnvironmentBasedSecretsProvider (org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Logger (org.slf4j.Logger)3 BeforeMethod (org.testng.annotations.BeforeMethod)3 Properties (java.util.Properties)2 Producer (org.apache.pulsar.client.api.Producer)2