1 | package geniusweb.protocol.tournament.allpermutationslearn;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertNotNull;
|
---|
5 | import static org.junit.Assert.assertNull;
|
---|
6 | import static org.mockito.Matchers.any;
|
---|
7 | import static org.mockito.Matchers.anyLong;
|
---|
8 | import static org.mockito.Mockito.mock;
|
---|
9 | import static org.mockito.Mockito.times;
|
---|
10 | import static org.mockito.Mockito.verify;
|
---|
11 | import static org.mockito.Mockito.when;
|
---|
12 |
|
---|
13 | import java.math.BigInteger;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import org.junit.Before;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.mockito.ArgumentCaptor;
|
---|
20 |
|
---|
21 | import geniusweb.events.ProtocolEvent;
|
---|
22 | import geniusweb.inform.Agreements;
|
---|
23 | import geniusweb.protocol.CurrentNegoState;
|
---|
24 | import geniusweb.protocol.NegoState;
|
---|
25 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
|
---|
26 | import geniusweb.protocol.session.SessionProtocol;
|
---|
27 | import geniusweb.protocol.session.SessionResult;
|
---|
28 | import geniusweb.protocol.session.SessionSettings;
|
---|
29 | import geniusweb.protocol.session.SessionState;
|
---|
30 | import geniusweb.references.PartyWithProfile;
|
---|
31 | import geniusweb.references.ProtocolRef;
|
---|
32 | import tudelft.utilities.listener.Listener;
|
---|
33 | import tudelft.utilities.logging.ReportToLogger;
|
---|
34 |
|
---|
35 | public class AllPermutationsLearnProtocolTest {
|
---|
36 |
|
---|
37 | private final AllPermutationsLearnState state = mock(
|
---|
38 | AllPermutationsLearnState.class),
|
---|
39 | newstate = mock(AllPermutationsLearnState.class);
|
---|
40 | private final ReportToLogger log = new ReportToLogger("test");
|
---|
41 | private final AllPermutationsLearnProtocol app = new AllPermutationsLearnProtocol(
|
---|
42 | state, log);
|
---|
43 | private final ProtocolToPartyConnFactory factory = mock(
|
---|
44 | ProtocolToPartyConnFactory.class);
|
---|
45 | private final SessionSettings settings = mock(SessionSettings.class);
|
---|
46 | private final SessionState finalsessionstate = mock(SessionState.class);
|
---|
47 | private Agreements agreement = mock(Agreements.class);
|
---|
48 | private final long NOW = 1000;
|
---|
49 | private SessionResult finalstate = mock(SessionResult.class);
|
---|
50 |
|
---|
51 | @Before
|
---|
52 | public void before() {
|
---|
53 | when(state.getSize()).thenReturn(BigInteger.ZERO);
|
---|
54 | when(finalsessionstate.isFinal(anyLong())).thenReturn(true);
|
---|
55 | when(finalsessionstate.getSettings()).thenReturn(settings);
|
---|
56 | when(finalsessionstate.getAgreements()).thenReturn(agreement);
|
---|
57 | when(finalsessionstate.getResult()).thenReturn(finalstate);
|
---|
58 | when(finalstate.getAgreements()).thenReturn(agreement);
|
---|
59 | when(state.with(any())).thenReturn(newstate);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Test
|
---|
63 | public void smokeTest() {
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Test
|
---|
67 | public void startTestNoSessions() {
|
---|
68 | AllPermutationsLearnProtocol app = new AllPermutationsLearnProtocol(
|
---|
69 | state, log);
|
---|
70 | app.start(factory);
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Test
|
---|
75 | public void getDescrTest() {
|
---|
76 | assertNotNull(app.getDescription());
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Test
|
---|
80 | public void getStateTest() {
|
---|
81 | assertEquals(state, app.getState());
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Test
|
---|
85 | public void getRefTest() {
|
---|
86 | assertEquals("APPLearn", app.getRef().getURI().toString());
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void runOneSessionTest() throws InterruptedException {
|
---|
91 | MockSessionProtocol sessionprotocol = startSession();
|
---|
92 | sessionprotocol.getListener();
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Test
|
---|
96 | public void runOneSessionAndUnhandledEvent() throws InterruptedException {
|
---|
97 | MockSessionProtocol sessionprotocol = startSession();
|
---|
98 | sessionprotocol.getListener().notifyChange(mock(ProtocolEvent.class));
|
---|
99 | verify(state, times(0)).with(any());
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Check that a started session correctly handles a session event.
|
---|
104 | */
|
---|
105 | @Test
|
---|
106 | public void CurrentNegoStateEventTest() throws InterruptedException {
|
---|
107 | MockSessionProtocol sessionprotocol = startSession();
|
---|
108 |
|
---|
109 | CurrentNegoState evt = mock(CurrentNegoState.class);
|
---|
110 | when(evt.getState()).thenReturn(finalsessionstate);
|
---|
111 | sessionprotocol.getListener().notifyChange(evt);
|
---|
112 |
|
---|
113 | ArgumentCaptor<SessionResult> argument = ArgumentCaptor
|
---|
114 | .forClass(SessionResult.class);
|
---|
115 | verify(state, times(1)).with(argument.capture());
|
---|
116 | assertEquals(agreement, argument.getValue().getAgreements());
|
---|
117 | assertNull(argument.getValue().getError());
|
---|
118 | }
|
---|
119 |
|
---|
120 | private MockSessionProtocol startSession() throws InterruptedException {
|
---|
121 | SessionSettings setting = mock(SessionSettings.class);
|
---|
122 | MockSessionProtocol sessionprotocol = new MockSessionProtocol();
|
---|
123 | when(setting.getProtocol(log)).thenReturn(sessionprotocol);
|
---|
124 | when(state.getNextSettings()).thenReturn(setting);
|
---|
125 |
|
---|
126 | app.start(factory);
|
---|
127 | Thread.sleep(500); // allow app to start session
|
---|
128 | // this call should return fine, and have listener registered to our
|
---|
129 | // protocol. app now waits for a notification
|
---|
130 | return sessionprotocol;
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | *
|
---|
137 | * The protocol needs to record the listners, we need a listener to trigger
|
---|
138 | * further protocol actions. Mockito can't handle this, we mneed our own mock...
|
---|
139 | *
|
---|
140 | */
|
---|
141 | class MockSessionProtocol implements SessionProtocol {
|
---|
142 | private final List<Listener<ProtocolEvent>> listeners = new LinkedList<>();
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public void start(ProtocolToPartyConnFactory connectionfactory) {
|
---|
146 |
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public String getDescription() {
|
---|
151 | return null;
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public NegoState getState() {
|
---|
156 | return null;
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | public ProtocolRef getRef() {
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public void addParticipant(PartyWithProfile party) {
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public void addListener(Listener<ProtocolEvent> l) {
|
---|
170 | listeners.add(l);
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public void removeListener(Listener<ProtocolEvent> l) {
|
---|
175 | }
|
---|
176 |
|
---|
177 | public Listener<ProtocolEvent> getListener() {
|
---|
178 | if (listeners.size() != 1) {
|
---|
179 | throw new IllegalStateException("Not having exactly 1 listener");
|
---|
180 | }
|
---|
181 | return listeners.get(0);
|
---|
182 | }
|
---|
183 |
|
---|
184 | } |
---|