1 | package geniusweb.simplerunner;
|
---|
2 |
|
---|
3 | import static org.mockito.Matchers.any;
|
---|
4 | import static org.mockito.Matchers.anyLong;
|
---|
5 | import static org.mockito.Matchers.eq;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.spy;
|
---|
8 | import static org.mockito.Mockito.times;
|
---|
9 | import static org.mockito.Mockito.verify;
|
---|
10 | import static org.mockito.Mockito.when;
|
---|
11 |
|
---|
12 | import java.util.logging.Level;
|
---|
13 |
|
---|
14 | import org.junit.Before;
|
---|
15 | import org.junit.Test;
|
---|
16 | import org.mockito.ArgumentCaptor;
|
---|
17 |
|
---|
18 | import geniusweb.actions.PartyId;
|
---|
19 | import geniusweb.protocol.CurrentNegoState;
|
---|
20 | import geniusweb.protocol.NegoState;
|
---|
21 | import geniusweb.protocol.ProtocolException;
|
---|
22 | import geniusweb.protocol.session.SessionProtocol;
|
---|
23 | import geniusweb.protocol.session.SessionSettings;
|
---|
24 | import geniusweb.protocol.session.SessionState;
|
---|
25 | import tudelft.utilities.listener.Listener;
|
---|
26 | import tudelft.utilities.logging.ReportToLogger;
|
---|
27 | import tudelft.utilities.logging.Reporter;
|
---|
28 |
|
---|
29 | public class SessionRunnerTest {
|
---|
30 | private static final ProtocolException PROTOCOL_EXC = new ProtocolException(
|
---|
31 | "fake protocol exception", new PartyId("test"));
|
---|
32 | private CurrentNegoState finishedEvent;
|
---|
33 | private final long NOW = 1000;
|
---|
34 | private final Reporter logger = new ReportToLogger("test");
|
---|
35 |
|
---|
36 | @Before
|
---|
37 | public void before() {
|
---|
38 | finishedEvent = mock(CurrentNegoState.class);
|
---|
39 | SessionState finishedstate = mock(SessionState.class);
|
---|
40 | when(finishedstate.isFinal(anyLong())).thenReturn(true);
|
---|
41 | when(finishedEvent.getState()).thenReturn(finishedstate);
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | @SuppressWarnings("unchecked")
|
---|
46 | @Test
|
---|
47 | public void smokeTest() {
|
---|
48 | new NegoRunner(mock(SessionSettings.class),
|
---|
49 | mock(ClassPathConnectionFactory.class), logger, 5000);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Test
|
---|
53 | public void testRun() {
|
---|
54 | SessionSettings settings = mock(SessionSettings.class);
|
---|
55 | SessionProtocol protocol = mock(SessionProtocol.class);
|
---|
56 | when(settings.getProtocol(logger)).thenReturn(protocol);
|
---|
57 | @SuppressWarnings("unchecked")
|
---|
58 | ClassPathConnectionFactory factory = mock(
|
---|
59 | ClassPathConnectionFactory.class);
|
---|
60 |
|
---|
61 | NegoRunner runner = spy(
|
---|
62 | new NegoRunner(settings, factory, logger, 5000));
|
---|
63 | runner.run();
|
---|
64 |
|
---|
65 | verify(protocol, times(1)).addListener(any());
|
---|
66 | verify(protocol, times(1)).start(factory);
|
---|
67 |
|
---|
68 | verify(runner, times(0)).stop();
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Test
|
---|
73 | public void testStopNormally() {
|
---|
74 | SessionSettings settings = mock(SessionSettings.class);
|
---|
75 | SessionProtocol protocol = mock(SessionProtocol.class);
|
---|
76 | when(settings.getProtocol(any())).thenReturn(protocol);
|
---|
77 | SessionState state = mock(SessionState.class);
|
---|
78 | when(protocol.getState()).thenReturn(state);
|
---|
79 | Reporter logger = mock(Reporter.class);
|
---|
80 | @SuppressWarnings("unchecked")
|
---|
81 | ClassPathConnectionFactory factory = mock(
|
---|
82 | ClassPathConnectionFactory.class);
|
---|
83 | NegoRunner runner = spy(
|
---|
84 | new NegoRunner(settings, factory, logger, 5000) {
|
---|
85 | @Override
|
---|
86 | protected void logFinal(Level level, NegoState state) {
|
---|
87 | log.log(level, state.toString());
|
---|
88 | }
|
---|
89 | });
|
---|
90 | ArgumentCaptor<Listener> listener = ArgumentCaptor
|
---|
91 | .forClass(Listener.class);
|
---|
92 | runner.run();
|
---|
93 |
|
---|
94 | verify(protocol, times(1)).addListener(listener.capture());
|
---|
95 |
|
---|
96 | // make the finixhed event.... too complex...
|
---|
97 | listener.getValue().notifyChange(finishedEvent);
|
---|
98 | verify(runner, times(1)).stop();
|
---|
99 | verify(logger, times(0)).log(eq(Level.WARNING), any(String.class));
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Test
|
---|
104 | public void testStopWithError() throws InterruptedException {
|
---|
105 | Reporter logger = mock(Reporter.class);
|
---|
106 | SessionSettings settings = mock(SessionSettings.class);
|
---|
107 | SessionProtocol protocol = mock(SessionProtocol.class);
|
---|
108 | when(settings.getProtocol(any())).thenReturn(protocol);
|
---|
109 | SessionState state = mock(SessionState.class);
|
---|
110 | when(protocol.getState()).thenReturn(state);
|
---|
111 | @SuppressWarnings("unchecked")
|
---|
112 | ClassPathConnectionFactory factory = mock(
|
---|
113 | ClassPathConnectionFactory.class);
|
---|
114 | NegoRunner runner = spy(
|
---|
115 | new NegoRunner(settings, factory, logger, 5000) {
|
---|
116 | @Override
|
---|
117 | protected void logFinal(Level level, NegoState state) {
|
---|
118 | log.log(level, state.toString());
|
---|
119 | }
|
---|
120 | });
|
---|
121 | ArgumentCaptor<Listener> listener = ArgumentCaptor
|
---|
122 | .forClass(Listener.class);
|
---|
123 | runner.run();
|
---|
124 |
|
---|
125 | verify(protocol, times(1)).addListener(listener.capture());
|
---|
126 |
|
---|
127 | listener.getValue().notifyChange(finishedEvent);
|
---|
128 | verify(runner, times(1)).stop();
|
---|
129 | // check final state is logged
|
---|
130 | String statestr = state.toString();
|
---|
131 | verify(logger, times(1)).log(any(), eq(statestr));
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | }
|
---|