source: protocol/src/test/java/geniusweb/protocol/tournament/allpermutationslearn/AllPermutationsLearnProtocolTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

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