aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp/src/test/java/com/juick/xmpp/server/XMPPServerTests.java
blob: ccc4f1d4de180c0878fc46f5ac7d79bd5d0cfe59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.juick.xmpp.server;

import com.juick.components.JuickBot;
import com.juick.components.XMPPServer;
import com.juick.components.configuration.XmppAppConfiguration;
import com.juick.configuration.MockDataConfiguration;
import com.juick.server.configuration.BaseWebConfiguration;
import com.juick.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.Message;
import rocks.xmpp.core.stanza.model.Stanza;
import rocks.xmpp.core.stanza.model.StanzaError;
import rocks.xmpp.core.stanza.model.errors.Condition;
import rocks.xmpp.core.stanza.model.server.ServerMessage;

import javax.inject.Inject;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class XMPPServerTests {
    @Configuration
    @Import(value = {
            BaseWebConfiguration.class, XmppAppConfiguration.class, MockDataConfiguration.class
    })
    static class Config { }

    @Inject
    private WebApplicationContext wac;
    @Inject
    private XMPPServer server;
    @Inject
    private JuickBot bot;
    @Inject
    private UserService userService;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }
    @Test
    public void statusPageIsUp() throws Exception {
        mockMvc.perform(get("http://localhost:8080/status")).andExpect(status().isOk());
        assertThat(server.getJid().toEscapedString(), equalTo("juick.com"));
    }
    @Test
    public void botIsUpAndProcessingResourceConstraints() {
        String xmlMessage = "<message xmlns=\"jabber:server\" from=\"renha@bitcheese.net\" to=\"juick@juick.com/Juick\" type=\"error\"><body>Reply by @LexX</body><juick xmlns=\"http://juick.com/message\" mid=\"2885759\" privacy=\"1\" replyto=\"0\" rid=\"8\" ts=\"2017-10-10 07:41:10\"><body>Похоже нынче можно публично заявлять о своем веганстве. </body><user xmlns=\"http://juick.com/user\" uname=\"LexX\" uid=\"6340\"></user></juick><error type=\"wait\"><resource-constraint xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"></resource-constraint><text xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">Your contact offline message queue is full. The message has been discarded.</text></error></message>";
        Jid from = Jid.of("renha@bitcheese.net");
        when(userService.setActiveStatusForJID(from.toEscapedString(), UserService.ActiveStatus.Inactive)).thenReturn(true);
        Stanza msg = server.parse(xmlMessage);
        bot.incomingMessage((ServerMessage)msg);
    }
}