package com.juick.xmpp.server; import com.juick.User; 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.Stanza; import rocks.xmpp.core.stanza.model.server.ServerMessage; import javax.inject.Inject; import java.lang.reflect.InvocationTargetException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; 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 = "Reply by @LexXПохоже нынче можно публично заявлять о своем веганстве. Your contact offline message queue is full. The message has been discarded."; 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); } @Test public void botCommandsTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "PING").get(), is("PONG")); // tag help have two lines, others have 1 assertThat(bot.processCommand(new User(), Jid.of("test@localhost"), "help").get().split("\n").length, is(17)); } }