Search in sources :

Example 1 with ProducerMessages

use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by apache.

the class TopicsTest method testProduceNoSchema.

// Default schema is String schema
@Test
public void testProduceNoSchema() throws Exception {
    String topicName = "persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName;
    admin.topics().createNonPartitionedTopic(topicName);
    AsyncResponse asyncResponse = mock(AsyncResponse.class);
    Consumer consumer = pulsarClient.newConsumer(StringSchema.utf8()).topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();
    ProducerMessages producerMessages = new ProducerMessages();
    String message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:3\",\"eventTime\":1603045262772,\"sequenceId\":3}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:4\",\"eventTime\":1603045262772,\"sequenceId\":4}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:5\",\"eventTime\":1603045262772,\"sequenceId\":5}]";
    producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
    }));
    topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName, false, producerMessages);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
    Object responseEntity = responseCaptor.getValue().getEntity();
    Assert.assertTrue(responseEntity instanceof ProducerAcks);
    ProducerAcks response = (ProducerAcks) responseEntity;
    Assert.assertEquals(response.getMessagePublishResults().size(), 5);
    Assert.assertEquals(response.getSchemaVersion(), 0);
    for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
        Assert.assertEquals(Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2]), -1);
        Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
        Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
    }
    List<String> expectedMsg = Arrays.asList("RestProducer:1", "RestProducer:2", "RestProducer:3", "RestProducer:4", "RestProducer:5");
    Message<String> msg = null;
    // Assert all messages published by REST producer can be received by consumer in expected order.
    for (int i = 0; i < 5; i++) {
        msg = consumer.receive(2, TimeUnit.SECONDS);
        Assert.assertEquals(expectedMsg.get(i), StringSchema.utf8().decode(msg.getData()));
        Assert.assertEquals("my-key", msg.getKey());
    }
}
Also used : ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) Consumer(org.apache.pulsar.client.api.Consumer) ProducerMessages(org.apache.pulsar.websocket.data.ProducerMessages) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AsyncResponse(javax.ws.rs.container.AsyncResponse) ProducerAcks(org.apache.pulsar.websocket.data.ProducerAcks) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with ProducerMessages

use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by apache.

the class TopicsTest method testProduceWithRestThenConsumeWithClient.

@Test
public void testProduceWithRestThenConsumeWithClient() throws Exception {
    String topicName = "persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName;
    admin.topics().createNonPartitionedTopic(topicName);
    AsyncResponse asyncResponse = mock(AsyncResponse.class);
    Schema keyValueSchema = KeyValueSchemaImpl.of(StringSchema.utf8(), StringSchema.utf8(), KeyValueEncodingType.SEPARATED);
    Consumer consumer = pulsarClient.newConsumer(keyValueSchema).topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();
    ProducerMessages producerMessages = new ProducerMessages();
    producerMessages.setKeySchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(StringSchema.utf8().getSchemaInfo()));
    producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(StringSchema.utf8().getSchemaInfo()));
    String message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:3\",\"eventTime\":1603045262772,\"sequenceId\":3}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:4\",\"eventTime\":1603045262772,\"sequenceId\":4}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:5\",\"eventTime\":1603045262772,\"sequenceId\":5}]";
    producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
    }));
    topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName, false, producerMessages);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
    Object responseEntity = responseCaptor.getValue().getEntity();
    Assert.assertTrue(responseEntity instanceof ProducerAcks);
    ProducerAcks response = (ProducerAcks) responseEntity;
    Assert.assertEquals(response.getMessagePublishResults().size(), 5);
    Assert.assertEquals(response.getSchemaVersion(), 0);
    for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
        Assert.assertEquals(Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2]), -1);
        Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
        Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
    }
    // Specify schema version to use existing schema.
    producerMessages = new ProducerMessages();
    producerMessages.setSchemaVersion(response.getSchemaVersion());
    message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:6\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:7\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:8\",\"eventTime\":1603045262772,\"sequenceId\":3}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:9\",\"eventTime\":1603045262772,\"sequenceId\":4}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:10\",\"eventTime\":1603045262772,\"sequenceId\":5}]";
    producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
    }));
    topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName, false, producerMessages);
    verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
    responseEntity = responseCaptor.getValue().getEntity();
    Assert.assertTrue(responseEntity instanceof ProducerAcks);
    response = (ProducerAcks) responseEntity;
    Assert.assertEquals(response.getMessagePublishResults().size(), 5);
    Assert.assertEquals(response.getSchemaVersion(), 0);
    for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
        Assert.assertEquals(Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2]), -1);
        Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
        Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
    }
    List<String> expectedMsg = Arrays.asList("RestProducer:1", "RestProducer:2", "RestProducer:3", "RestProducer:4", "RestProducer:5", "RestProducer:6", "RestProducer:7", "RestProducer:8", "RestProducer:9", "RestProducer:10");
    Message<String> msg = null;
    // Assert all messages published by REST producer can be received by consumer in expected order.
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(2, TimeUnit.SECONDS);
        Assert.assertEquals(expectedMsg.get(i), StringSchema.utf8().decode(msg.getData()));
        Assert.assertEquals("bXkta2V5", msg.getKey());
    }
}
Also used : JSONSchema(org.apache.pulsar.client.impl.schema.JSONSchema) AvroSchema(org.apache.pulsar.client.impl.schema.AvroSchema) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.client.api.Schema) GenericJsonSchema(org.apache.pulsar.client.impl.schema.generic.GenericJsonSchema) GenericAvroSchema(org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) Consumer(org.apache.pulsar.client.api.Consumer) ProducerMessages(org.apache.pulsar.websocket.data.ProducerMessages) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AsyncResponse(javax.ws.rs.container.AsyncResponse) ProducerAcks(org.apache.pulsar.websocket.data.ProducerAcks) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with ProducerMessages

use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by apache.

the class TopicsTest method testProduceWithInCompatibleSchema.

@Test
public void testProduceWithInCompatibleSchema() throws Exception {
    String topicName = "persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName;
    admin.topics().createNonPartitionedTopic(topicName);
    AsyncResponse asyncResponse = mock(AsyncResponse.class);
    Producer producer = pulsarClient.newProducer(StringSchema.utf8()).topic(topicName).create();
    for (int i = 0; i < 3; i++) {
        producer.send("message");
    }
    ProducerMessages producerMessages = new ProducerMessages();
    producerMessages.setKeySchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(StringSchema.utf8().getSchemaInfo()));
    producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(StringSchema.utf8().getSchemaInfo()));
    String message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:3\",\"eventTime\":1603045262772,\"sequenceId\":3}]";
    producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
    }));
    topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName, false, producerMessages);
    ArgumentCaptor<RestException> responseCaptor = ArgumentCaptor.forClass(RestException.class);
    verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertTrue(responseCaptor.getValue().getMessage().startsWith("Fail to publish message:" + "java.util.concurrent.ExecutionException: org.apache.pulsar.broker.service.schema.exceptions." + "SchemaException: Unable to add schema SchemaData(type=KEY_VALUE, isDeleted=false, " + "timestamp="));
    Assert.assertTrue(responseCaptor.getValue().getMessage().endsWith("user=Rest Producer, data=[0, 0, 0, 0, 0, 0, 0, 0], " + "props={key.schema.properties={\"__charset\":\"UTF-8\"}, value.schema.properties={\"__charset\":" + "\"UTF-8\"}, value.schema.type=STRING, key.schema.name=String, value.schema.name=String, " + "kv.encoding.type=SEPARATED, key.schema.type=STRING}) to topic persistent:" + "//my-tenant/my-namespace/my-topic"));
}
Also used : Producer(org.apache.pulsar.client.api.Producer) RestException(org.apache.pulsar.broker.web.RestException) ProducerMessages(org.apache.pulsar.websocket.data.ProducerMessages) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AsyncResponse(javax.ws.rs.container.AsyncResponse) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with ProducerMessages

use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by apache.

the class TopicsTest method testProduceToPartitionedTopicSpecificPartition.

@Test
public void testProduceToPartitionedTopicSpecificPartition() throws Exception {
    admin.topics().createPartitionedTopic("persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName, 5);
    AsyncResponse asyncResponse = mock(AsyncResponse.class);
    Schema<String> schema = StringSchema.utf8();
    ProducerMessages producerMessages = new ProducerMessages();
    producerMessages.setKeySchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
    producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
    String message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:3\",\"eventTime\":1603045262772,\"sequenceId\":3}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:4\",\"eventTime\":1603045262772,\"sequenceId\":4}]";
    producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
    }));
    topics.produceOnPersistentTopicPartition(asyncResponse, testTenant, testNamespace, testTopicName, 2, false, producerMessages);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
    Object responseEntity = responseCaptor.getValue().getEntity();
    Assert.assertTrue(responseEntity instanceof ProducerAcks);
    ProducerAcks response = (ProducerAcks) responseEntity;
    Assert.assertEquals(response.getMessagePublishResults().size(), 4);
    Assert.assertEquals(response.getSchemaVersion(), 0);
    for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
        Assert.assertEquals(Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2]), 2);
        Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
        Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
    }
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) ProducerMessages(org.apache.pulsar.websocket.data.ProducerMessages) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AsyncResponse(javax.ws.rs.container.AsyncResponse) ProducerAcks(org.apache.pulsar.websocket.data.ProducerAcks) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 5 with ProducerMessages

use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by apache.

the class TopicsTest method testProduceToPartitionedTopic.

@Test
public void testProduceToPartitionedTopic() throws Exception {
    admin.topics().createPartitionedTopic("persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName + "-p", 5);
    AsyncResponse asyncResponse = mock(AsyncResponse.class);
    Schema<String> schema = StringSchema.utf8();
    ProducerMessages producerMessages = new ProducerMessages();
    producerMessages.setKeySchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
    producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
    String message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:3\",\"eventTime\":1603045262772,\"sequenceId\":3}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:4\",\"eventTime\":1603045262772,\"sequenceId\":4}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:5\",\"eventTime\":1603045262772,\"sequenceId\":5}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:6\",\"eventTime\":1603045262772,\"sequenceId\":6}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:7\",\"eventTime\":1603045262772,\"sequenceId\":7}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:8\",\"eventTime\":1603045262772,\"sequenceId\":8}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:9\",\"eventTime\":1603045262772,\"sequenceId\":9}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:10\",\"eventTime\":1603045262772,\"sequenceId\":10}]";
    producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
    }));
    topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName + "-p", false, producerMessages);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
    Object responseEntity = responseCaptor.getValue().getEntity();
    Assert.assertTrue(responseEntity instanceof ProducerAcks);
    ProducerAcks response = (ProducerAcks) responseEntity;
    Assert.assertEquals(response.getMessagePublishResults().size(), 10);
    Assert.assertEquals(response.getSchemaVersion(), 0);
    int[] messagePerPartition = new int[5];
    for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
        messagePerPartition[Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2])]++;
        Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
        Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
    }
    for (int index = 0; index < messagePerPartition.length; index++) {
        // We publish to each partition in round robin mode so each partition should get at most 2 message.
        Assert.assertTrue(messagePerPartition[index] <= 2);
    }
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) ProducerMessages(org.apache.pulsar.websocket.data.ProducerMessages) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AsyncResponse(javax.ws.rs.container.AsyncResponse) ProducerAcks(org.apache.pulsar.websocket.data.ProducerAcks) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)30 ProducerMessage (org.apache.pulsar.websocket.data.ProducerMessage)30 ProducerMessages (org.apache.pulsar.websocket.data.ProducerMessages)30 AsyncResponse (javax.ws.rs.container.AsyncResponse)28 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)28 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)28 Test (org.testng.annotations.Test)28 Response (javax.ws.rs.core.Response)24 ProducerAcks (org.apache.pulsar.websocket.data.ProducerAcks)20 Consumer (org.apache.pulsar.client.api.Consumer)14 RestException (org.apache.pulsar.broker.web.RestException)8 GenericSchema (org.apache.pulsar.client.api.schema.GenericSchema)8 CompletableFuture (java.util.concurrent.CompletableFuture)6 NamespaceService (org.apache.pulsar.broker.namespace.NamespaceService)6 Producer (org.apache.pulsar.client.api.Producer)6 Schema (org.apache.pulsar.client.api.Schema)6 AvroSchema (org.apache.pulsar.client.impl.schema.AvroSchema)6 JSONSchema (org.apache.pulsar.client.impl.schema.JSONSchema)6 StringSchema (org.apache.pulsar.client.impl.schema.StringSchema)6 GenericAvroSchema (org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema)6