source: protocol/src/test/java/geniusweb/protocol/tournament/allpermutations/AllPermutationsProtocolTest.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.3 KB
Line 
1package geniusweb.protocol.tournament.allpermutations;
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 AllPermutationsProtocolTest {
37
38 private final AllPermutationsState state = mock(AllPermutationsState.class),
39 newstate = mock(AllPermutationsState.class);
40 private final ReportToLogger log = new ReportToLogger("test");
41 private final AllPermutationsProtocol app = new AllPermutationsProtocol(
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.getResults())
58 .thenReturn(Arrays.asList(finalstate));
59 when(finalstate.getAgreements()).thenReturn(agreement);
60 when(state.with(any())).thenReturn(newstate);
61 }
62
63 @Test
64 public void smokeTest() {
65 }
66
67 @Test
68 public void startTestNoSessions() {
69 AllPermutationsProtocol app = new AllPermutationsProtocol(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("APP", 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<List<SessionResult>> argument = ArgumentCaptor
114 .forClass(List.class);
115 verify(state, times(1)).with(argument.capture());
116 assertEquals(agreement, argument.getValue().get(0).getAgreements());
117 assertNull(argument.getValue().get(0).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 */
141class 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}
Note: See TracBrowser for help on using the repository browser.