Add test for network without servers.
[xudocci.git] / src / test / java / net / pterodactylus / xdcc / core / CoreTest.java
1 package net.pterodactylus.xdcc.core;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.containsInAnyOrder;
5 import static org.hamcrest.Matchers.instanceOf;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Matchers.anyInt;
8 import static org.mockito.Matchers.anyString;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.never;
11 import static org.mockito.Mockito.times;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14
15 import net.pterodactylus.irc.Connection;
16 import net.pterodactylus.irc.ConnectionFactory;
17 import net.pterodactylus.xdcc.core.event.CoreStarted;
18 import net.pterodactylus.xdcc.core.event.GenericError;
19 import net.pterodactylus.xdcc.core.event.GenericMessage;
20 import net.pterodactylus.xdcc.data.Channel;
21 import net.pterodactylus.xdcc.data.Network;
22
23 import com.google.common.eventbus.EventBus;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28
29 public class CoreTest {
30
31         private static final String TEMP_DIRECTORY = "/tmp";
32         private static final String FINAL_DIRECTORY = "/final";
33         private final EventBus eventBus = mock(EventBus.class);
34         private final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
35         private final Core core =
36                         new Core(eventBus, connectionFactory, TEMP_DIRECTORY, FINAL_DIRECTORY);
37         private final Connection firstConnection = mock(Connection.class);
38         private final Connection secondConnection = mock(Connection.class);
39
40         @Before
41         public void setupConnections() {
42                 when(firstConnection.username(anyString())).thenReturn(firstConnection);
43                 when(secondConnection.username(anyString())).thenReturn(secondConnection);
44         }
45
46         @Before
47         public void setupConnectionFactory() {
48                 when(connectionFactory.createConnection("first.net", 6667)).thenReturn(firstConnection);
49                 when(connectionFactory.createConnection("second.net", 6667)).thenReturn(secondConnection);
50         }
51
52         @After
53         public void tearDown() {
54                 core.stopAndWait();
55         }
56
57         @Test
58         public void startingTheCoreWithAnEmptyNetworkStartsButDoesNotConnect() {
59                 Network firstNetwork = Network.builder("FirstNet").build();
60                 Channel firstChannel = new Channel(firstNetwork, "#first");
61                 core.addChannel(firstChannel);
62                 core.startAndWait();
63                 ArgumentCaptor<Object> eventCaptor = ArgumentCaptor.forClass(Object.class);
64                 verify(eventBus, times(2)).post(eventCaptor.capture());
65                 assertThat(eventCaptor.getAllValues(), containsInAnyOrder(
66                                 instanceOf(GenericError.class), instanceOf(CoreStarted.class)
67                 ));
68                 verify(connectionFactory, never()).createConnection(anyString(), anyInt());
69         }
70
71         @Test
72         public void startingTheCoreConnectsToConfiguredNetworks() {
73                 Network firstNetwork = Network.builder("FirstNet")
74                                 .addServer()
75                                 .at("first.net")
76                                 .port(6667)
77                                 .endServer()
78                                 .build();
79                 Channel firstChannel = new Channel(firstNetwork, "#first");
80                 Network secondNetwork = Network.builder("SecondNet")
81                                 .addServer()
82                                 .at("second.net")
83                                 .port(6667)
84                                 .endServer()
85                                 .build();
86                 Channel secondChannel = new Channel(secondNetwork, "#second");
87                 core.addChannel(firstChannel);
88                 core.addChannel(secondChannel);
89                 core.startAndWait();
90                 verify(eventBus).post(any(CoreStarted.class));
91                 verify(firstConnection).open();
92                 verify(secondConnection).open();
93         }
94
95 }