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