Add first test for the core.
[xudocci.git] / src / test / java / net / pterodactylus / xdcc / core / CoreTest.java
1 package net.pterodactylus.xdcc.core;
2
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Matchers.anyString;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.verify;
7 import static org.mockito.Mockito.when;
8
9 import net.pterodactylus.irc.Connection;
10 import net.pterodactylus.irc.ConnectionFactory;
11 import net.pterodactylus.xdcc.core.event.CoreStarted;
12 import net.pterodactylus.xdcc.data.Channel;
13 import net.pterodactylus.xdcc.data.Network;
14
15 import com.google.common.eventbus.EventBus;
16 import org.junit.Before;
17 import org.junit.Test;
18
19 public class CoreTest {
20
21         private static final String TEMP_DIRECTORY = "/tmp";
22         private static final String FINAL_DIRECTORY = "/final";
23         private final EventBus eventBus = mock(EventBus.class);
24         private final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
25         private final Core core =
26                         new Core(eventBus, connectionFactory, TEMP_DIRECTORY, FINAL_DIRECTORY);
27         private final Connection firstConnection = mock(Connection.class);
28         private final Connection secondConnection = mock(Connection.class);
29
30         @Before
31         public void setupConnections() {
32                 when(firstConnection.username(anyString())).thenReturn(firstConnection);
33                 when(secondConnection.username(anyString())).thenReturn(secondConnection);
34         }
35
36         @Before
37         public void setupConnectionFactory() {
38                 when(connectionFactory.createConnection("first.net", 6667)).thenReturn(firstConnection);
39                 when(connectionFactory.createConnection("second.net", 6667)).thenReturn(secondConnection);
40         }
41
42         @Test
43         public void startingTheCoreConnectsToConfiguredNetworks() {
44                 Network firstNetwork = Network.builder("FirstNet")
45                                 .addServer()
46                                 .at("first.net")
47                                 .port(6667)
48                                 .endServer()
49                                 .build();
50                 Channel firstChannel = new Channel(firstNetwork, "#first");
51                 Network secondNetwork = Network.builder("SecondNet")
52                                 .addServer()
53                                 .at("second.net")
54                                 .port(6667)
55                                 .endServer()
56                                 .build();
57                 Channel secondChannel = new Channel(secondNetwork, "#second");
58                 core.addChannel(firstChannel);
59                 core.addChannel(secondChannel);
60                 core.startAndWait();
61                 verify(eventBus).post(any(CoreStarted.class));
62                 verify(firstConnection).open();
63                 verify(secondConnection).open();
64         }
65
66 }