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