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);
}
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));
}
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));
}
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);
}
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));
}
Aggregations