Search in sources :

Example 1 with MessageService

use of com.github.jamesnetherton.zulip.client.api.message.MessageService in project zulip-java-client by jamesnetherton.

the class ZulipEventIT method messageEventsWithNarrow.

@Test
public void messageEventsWithNarrow() throws Exception {
    CountDownLatch latch = new CountDownLatch(5);
    List<String> messages = new ArrayList<>();
    String streamA = UUID.randomUUID().toString().split("-")[0];
    String streamB = UUID.randomUUID().toString().split("-")[0];
    zulip.streams().subscribe(StreamSubscriptionRequest.of(streamA, streamA), StreamSubscriptionRequest.of(streamB, streamB)).execute();
    for (int i = 0; i < 10; i++) {
        List<Stream> streams = zulip.streams().getAll().execute();
        List<Stream> matches = streams.stream().filter(stream -> stream.getName().equals(streamA) || stream.getName().equals(streamB)).collect(Collectors.toList());
        if (matches.size() == 2) {
            break;
        }
        Thread.sleep(500);
    }
    EventPoller eventPoller = zulip.events().captureMessageEvents(new MessageEventListener() {

        @Override
        public void onEvent(Message event) {
            messages.add(event.getContent());
            latch.countDown();
        }
    }, Narrow.of("stream", streamA));
    try {
        eventPoller.start();
        MessageService messageService = zulip.messages();
        for (int i = 0; i < 10; i++) {
            String streamName = i % 2 == 0 ? streamA : streamB;
            messageService.sendStreamMessage("Stream " + streamName + " Content " + i, streamName, "testtopic").execute();
        }
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        int count = 0;
        for (int i = 0; i < 5; i++) {
            assertEquals("Stream " + streamA + " Content " + count, messages.get(i));
            count += 2;
        }
    } catch (ZulipClientException e) {
        e.printStackTrace();
        throw e;
    } finally {
        eventPoller.stop();
    }
}
Also used : Message(com.github.jamesnetherton.zulip.client.api.message.Message) StreamService(com.github.jamesnetherton.zulip.client.api.stream.StreamService) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) MessageService(com.github.jamesnetherton.zulip.client.api.message.MessageService) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) EventPoller(com.github.jamesnetherton.zulip.client.api.event.EventPoller) StreamSubscriptionRequest(com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest) ZulipIntegrationTestBase(com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase) UUID(java.util.UUID) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageEventListener(com.github.jamesnetherton.zulip.client.api.event.MessageEventListener) Narrow(com.github.jamesnetherton.zulip.client.api.narrow.Narrow) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) EventPoller(com.github.jamesnetherton.zulip.client.api.event.EventPoller) Message(com.github.jamesnetherton.zulip.client.api.message.Message) MessageEventListener(com.github.jamesnetherton.zulip.client.api.event.MessageEventListener) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MessageService(com.github.jamesnetherton.zulip.client.api.message.MessageService) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) Test(org.junit.jupiter.api.Test)

Example 2 with MessageService

use of com.github.jamesnetherton.zulip.client.api.message.MessageService in project zulip-java-client by jamesnetherton.

the class ZulipEventIT method messageEvents.

@Test
public void messageEvents() throws Exception {
    CountDownLatch latch = new CountDownLatch(3);
    List<String> messages = new ArrayList<>();
    String streamName = UUID.randomUUID().toString().split("-")[0];
    StreamSubscriptionRequest subscriptionRequest = StreamSubscriptionRequest.of(streamName, streamName);
    StreamService streamService = zulip.streams();
    streamService.subscribe(subscriptionRequest).execute();
    for (int i = 0; i < 10; i++) {
        List<Stream> streams = streamService.getAll().execute();
        List<Stream> matches = streams.stream().filter(stream -> stream.getName().equals(streamName)).collect(Collectors.toList());
        if (matches.size() == 1) {
            break;
        }
        Thread.sleep(500);
    }
    EventPoller eventPoller = zulip.events().captureMessageEvents(new MessageEventListener() {

        @Override
        public void onEvent(Message event) {
            messages.add(event.getContent());
            latch.countDown();
        }
    });
    try {
        eventPoller.start();
        MessageService messageService = zulip.messages();
        for (int i = 0; i < 3; i++) {
            messageService.sendStreamMessage("Test Content " + i, streamName, "testtopic").execute();
        }
        assertTrue(latch.await(5, TimeUnit.SECONDS));
        for (int i = 0; i < 3; i++) {
            assertEquals("Test Content " + i, messages.get(i));
        }
    } catch (ZulipClientException e) {
        e.printStackTrace();
        throw e;
    } finally {
        eventPoller.stop();
    }
}
Also used : StreamService(com.github.jamesnetherton.zulip.client.api.stream.StreamService) Message(com.github.jamesnetherton.zulip.client.api.message.Message) StreamService(com.github.jamesnetherton.zulip.client.api.stream.StreamService) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) MessageService(com.github.jamesnetherton.zulip.client.api.message.MessageService) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) EventPoller(com.github.jamesnetherton.zulip.client.api.event.EventPoller) StreamSubscriptionRequest(com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest) ZulipIntegrationTestBase(com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase) UUID(java.util.UUID) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageEventListener(com.github.jamesnetherton.zulip.client.api.event.MessageEventListener) Narrow(com.github.jamesnetherton.zulip.client.api.narrow.Narrow) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZulipClientException(com.github.jamesnetherton.zulip.client.exception.ZulipClientException) EventPoller(com.github.jamesnetherton.zulip.client.api.event.EventPoller) Message(com.github.jamesnetherton.zulip.client.api.message.Message) StreamSubscriptionRequest(com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest) MessageEventListener(com.github.jamesnetherton.zulip.client.api.event.MessageEventListener) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MessageService(com.github.jamesnetherton.zulip.client.api.message.MessageService) Stream(com.github.jamesnetherton.zulip.client.api.stream.Stream) Test(org.junit.jupiter.api.Test)

Aggregations

EventPoller (com.github.jamesnetherton.zulip.client.api.event.EventPoller)2 MessageEventListener (com.github.jamesnetherton.zulip.client.api.event.MessageEventListener)2 ZulipIntegrationTestBase (com.github.jamesnetherton.zulip.client.api.integration.ZulipIntegrationTestBase)2 Message (com.github.jamesnetherton.zulip.client.api.message.Message)2 MessageService (com.github.jamesnetherton.zulip.client.api.message.MessageService)2 Narrow (com.github.jamesnetherton.zulip.client.api.narrow.Narrow)2 Stream (com.github.jamesnetherton.zulip.client.api.stream.Stream)2 StreamService (com.github.jamesnetherton.zulip.client.api.stream.StreamService)2 StreamSubscriptionRequest (com.github.jamesnetherton.zulip.client.api.stream.StreamSubscriptionRequest)2 ZulipClientException (com.github.jamesnetherton.zulip.client.exception.ZulipClientException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 UUID (java.util.UUID)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 Disabled (org.junit.jupiter.api.Disabled)2 Test (org.junit.jupiter.api.Test)2