Example usage for org.springframework.integration.xmpp.inbound ChatMessageListeningEndpoint ChatMessageListeningEndpoint

List of usage examples for org.springframework.integration.xmpp.inbound ChatMessageListeningEndpoint ChatMessageListeningEndpoint

Introduction

In this page you can find the example usage for org.springframework.integration.xmpp.inbound ChatMessageListeningEndpoint ChatMessageListeningEndpoint.

Prototype

public ChatMessageListeningEndpoint(XMPPConnection xmppConnection) 

Source Link

Usage

From source file:org.springframework.integration.xmpp.inbound.ChatMessageListeningEndpointTests.java

@Test
/*// w  w w  .ja  v  a  2  s  .  c  om
 * Should add/remove StanzaListener when endpoint started/stopped
 */
public void testLifecycle() {
    final Set<StanzaListener> packetListSet = new HashSet<>();
    XMPPConnection connection = mock(XMPPConnection.class);
    ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint(connection);

    willAnswer(invocation -> {
        packetListSet.add(invocation.getArgument(0));
        return null;
    }).given(connection).addAsyncStanzaListener(any(StanzaListener.class), isNull());

    willAnswer(invocation -> {
        packetListSet.remove(invocation.getArgument(0));
        return null;
    }).given(connection).removeAsyncStanzaListener(any(StanzaListener.class));

    assertEquals(0, packetListSet.size());
    endpoint.setOutputChannel(new QueueChannel());
    endpoint.setBeanFactory(mock(BeanFactory.class));
    endpoint.afterPropertiesSet();
    endpoint.start();
    assertEquals(1, packetListSet.size());
    endpoint.stop();
    assertEquals(0, packetListSet.size());
}

From source file:org.springframework.integration.xmpp.inbound.ChatMessageListeningEndpointTests.java

@Test(expected = IllegalArgumentException.class)
public void testNonInitializationFailure() {
    ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint(mock(XMPPConnection.class));
    endpoint.start();//from   w w w  . j a  v a  2  s  .  c om
}

From source file:org.springframework.integration.xmpp.inbound.ChatMessageListeningEndpointTests.java

@Test
public void testExpression() throws Exception {
    TestXMPPConnection testXMPPConnection = new TestXMPPConnection();

    QueueChannel inputChannel = new QueueChannel();

    ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint(testXMPPConnection);
    SpelExpressionParser parser = new SpelExpressionParser();
    endpoint.setPayloadExpression(parser.parseExpression("#root"));
    endpoint.setOutputChannel(inputChannel);
    endpoint.setBeanFactory(mock(BeanFactory.class));
    endpoint.afterPropertiesSet();// ww  w.  ja  v  a2 s. co m
    endpoint.start();

    Message smackMessage = new Message();
    smackMessage.setBody("foo");

    XmlPullParser xmlPullParser = PacketParserUtils.newXmppParser(new StringReader(smackMessage.toString()));
    xmlPullParser.next();
    testXMPPConnection.parseAndProcessStanza(xmlPullParser);

    org.springframework.messaging.Message<?> receive = inputChannel.receive(10000);
    assertNotNull(receive);

    Object payload = receive.getPayload();
    assertThat(payload, instanceOf(Message.class));
    assertEquals(smackMessage.getStanzaId(), ((Message) payload).getStanzaId());
    assertEquals(smackMessage.getBody(), ((Message) payload).getBody());

    Log logger = Mockito.spy(TestUtils.getPropertyValue(endpoint, "logger", Log.class));
    given(logger.isInfoEnabled()).willReturn(true);
    final CountDownLatch logLatch = new CountDownLatch(1);
    willAnswer(invocation -> {
        Object result = invocation.callRealMethod();
        logLatch.countDown();
        return result;
    }).given(logger).info(anyString());

    new DirectFieldAccessor(endpoint).setPropertyValue("logger", logger);

    endpoint.setPayloadExpression(null);

    smackMessage = new Message();
    xmlPullParser = PacketParserUtils.newXmppParser(new StringReader(smackMessage.toString()));
    xmlPullParser.next();
    testXMPPConnection.parseAndProcessStanza(xmlPullParser);

    ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);

    assertTrue(logLatch.await(10, TimeUnit.SECONDS));

    verify(logger).info(argumentCaptor.capture());

    assertEquals("The XMPP Message [" + smackMessage + "] with empty body is ignored.",
            argumentCaptor.getValue());

    endpoint.stop();
}

From source file:org.springframework.integration.xmpp.inbound.ChatMessageListeningEndpointTests.java

@Test
public void testGcmExtension() throws Exception {
    String data = "{\n" + "      \"to\":\"me\",\n" + "     \"notification\": {\n"
            + "        \"title\": \"Something interesting\",\n" + "        \"text\": \"Here we go\"\n"
            + "           },\n" + "      \"time_to_live\":\"600\"\n" + "      }\n" + "}";
    GcmPacketExtension packetExtension = new GcmPacketExtension(data);
    Message smackMessage = new Message();
    smackMessage.addExtension(packetExtension);

    TestXMPPConnection testXMPPConnection = new TestXMPPConnection();

    QueueChannel inputChannel = new QueueChannel();

    ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint(testXMPPConnection);
    Expression payloadExpression = new SpelExpressionParser().parseExpression("#extension.json");
    endpoint.setPayloadExpression(payloadExpression);
    endpoint.setOutputChannel(inputChannel);
    endpoint.setBeanFactory(mock(BeanFactory.class));
    endpoint.afterPropertiesSet();/* w ww . j a  va 2s.  c  o m*/
    endpoint.start();

    XmlPullParser xmlPullParser = PacketParserUtils.newXmppParser(new StringReader(smackMessage.toString()));
    xmlPullParser.next();
    testXMPPConnection.parseAndProcessStanza(xmlPullParser);

    org.springframework.messaging.Message<?> receive = inputChannel.receive(10000);
    assertNotNull(receive);

    assertEquals(data, receive.getPayload());

    endpoint.stop();
}