source: protocol/src/test/java/geniusweb/protocol/tournament/allpermutations/AllPermutationsProtocolTest.java@ 27

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