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

Last change on this file since 1 was 1, checked in by bart, 5 years ago

Initial Release

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