1 | package geniusweb.protocol.session.mopac2;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 | import static org.mockito.Matchers.any;
|
---|
8 | import static org.mockito.Matchers.anyLong;
|
---|
9 | import static org.mockito.Mockito.mock;
|
---|
10 | import static org.mockito.Mockito.times;
|
---|
11 | import static org.mockito.Mockito.verify;
|
---|
12 | import static org.mockito.Mockito.when;
|
---|
13 |
|
---|
14 | import java.io.IOException;
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.util.Date;
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | import org.junit.Before;
|
---|
20 | import org.junit.Test;
|
---|
21 |
|
---|
22 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
23 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
24 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
25 |
|
---|
26 | import geniusweb.actions.Action;
|
---|
27 | import geniusweb.actions.Offer;
|
---|
28 | import geniusweb.actions.PartyId;
|
---|
29 | import geniusweb.inform.Inform;
|
---|
30 | import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
|
---|
31 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
|
---|
32 | import geniusweb.protocol.session.mopac2.phase.OfferPhase;
|
---|
33 | import geniusweb.protocol.session.mopac2.phase.Phase;
|
---|
34 | import geniusweb.protocol.session.mopac2.phase.VotingPhase;
|
---|
35 | import tudelft.utilities.logging.Reporter;
|
---|
36 | import tudelft.utilities.repository.NoResourcesNowException;
|
---|
37 |
|
---|
38 | public class MOPAC2Test {
|
---|
39 |
|
---|
40 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
41 |
|
---|
42 | private static final PartyId PARTY1ID = new PartyId("party1");
|
---|
43 | private static final PartyId PARTY2ID = new PartyId("party2");
|
---|
44 | private static final PartyId PARTY3ID = new PartyId("party3");
|
---|
45 |
|
---|
46 | // don't try to mock settings, all parts are needed and mocking
|
---|
47 | // makes the test unreadable.
|
---|
48 | // notice, 3 parties, this differs from MopacSettingsTest output.
|
---|
49 | private final String setstr = "{\"MOPAC2Settings\":{\"participants\":["
|
---|
50 | + "{\"TeamInfo\":{\"parties\":[ {\"party\":{\"partyref\":\"http://party1\",\"parameters\":{}},\"profile\":\"http://profile1\"} ]}},"
|
---|
51 | + "{\"TeamInfo\":{\"parties\":[ {\"party\":{\"partyref\":\"http://party2\",\"parameters\":{}},\"profile\":\"http://profile2\"}]}},"
|
---|
52 | + "{\"TeamInfo\":{\"parties\":[ {\"party\":{\"partyref\":\"http://party3\",\"parameters\":{}},\"profile\":\"http://profile3\"} ]}} ],"
|
---|
53 | + "\"deadline\":{\"DeadlineTime\":{\"durationms\":100}},\"votingevaluator\":{\"LargestAgreementWithValue\":{}}}}";
|
---|
54 |
|
---|
55 | // {"MOPAC2Settings":{"participants":[{"TeamInfo":{"parties":[{"party":{"partyref":"http://party1","parameters":{}},"profile":"http://profile1"}]}},{"TeamInfo":{"parties":[{"party":{"partyref":"http://party2","parameters":{}},"profile":"http://profile2"}]}}],"deadline":{"deadlinetime":{"durationms":100}},"votingevaluator":{"LargestAgreementWithValue":{}}}}
|
---|
56 |
|
---|
57 | private MOPAC2Settings settings;
|
---|
58 | private Reporter logger = mock(Reporter.class);
|
---|
59 | private MOPAC2 mopac;
|
---|
60 |
|
---|
61 | private final ProtocolToPartyConnFactory factory = mock(
|
---|
62 | ProtocolToPartyConnFactory.class);
|
---|
63 | private final ProtocolToPartyConn conn1 = mock(ProtocolToPartyConn.class),
|
---|
64 | conn2 = mock(ProtocolToPartyConn.class),
|
---|
65 | conn3 = mock(ProtocolToPartyConn.class);
|
---|
66 |
|
---|
67 | private List<ProtocolToPartyConn> connections;
|
---|
68 |
|
---|
69 | @Before
|
---|
70 | public void before() throws JsonParseException, JsonMappingException,
|
---|
71 | IOException, NoResourcesNowException {
|
---|
72 | settings = jackson.readValue(setstr, MOPAC2Settings.class);
|
---|
73 | mopac = settings.getProtocol(logger);
|
---|
74 |
|
---|
75 | connections = Arrays.asList(conn1, conn2, conn3);
|
---|
76 | when(factory.connect(any(List.class))).thenReturn(connections);
|
---|
77 |
|
---|
78 | // hack the state.with(connection) stuff simplistically
|
---|
79 |
|
---|
80 | when(conn1.getParty()).thenReturn(PARTY1ID);
|
---|
81 | when(conn2.getParty()).thenReturn(PARTY2ID);
|
---|
82 | when(conn3.getParty()).thenReturn(PARTY3ID);
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Test
|
---|
86 | public void testSmoke() {
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void testGetDescr() {
|
---|
91 | assertNotNull(mopac.getDescription());
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test
|
---|
95 | public void testInitialState() {
|
---|
96 | MOPAC2State state = mopac.getState();
|
---|
97 | assertEquals(settings, state.getSettings());
|
---|
98 | assertTrue(state.getActions().isEmpty());
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Test
|
---|
102 | public void testProtocol() {
|
---|
103 | assertEquals("MOPAC2", mopac.getRef().getURI().getPath());
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Test
|
---|
107 | public void testWorkingCheckDeadline()
|
---|
108 | throws IOException, NoResourcesNowException, InterruptedException {
|
---|
109 |
|
---|
110 | mopac.start(factory);
|
---|
111 |
|
---|
112 | assertFalse(mopac.getState().isFinal(System.currentTimeMillis()));
|
---|
113 | // we have deadline in 100ms so check
|
---|
114 | Thread.sleep(200);
|
---|
115 | assertTrue(mopac.getState().isFinal(System.currentTimeMillis()));
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Test
|
---|
119 | public void testSubscribedToConnections() {
|
---|
120 | mopac.start(factory);
|
---|
121 | verify(conn1, times(1)).addListener(any());
|
---|
122 | verify(conn2, times(1)).addListener(any());
|
---|
123 | verify(conn3, times(1)).addListener(any());
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Test
|
---|
127 | public void testActionIsBroadcast()
|
---|
128 | throws IOException, NoResourcesNowException, InterruptedException {
|
---|
129 |
|
---|
130 | mopac.start(factory);
|
---|
131 | Action offer = mock(Offer.class);
|
---|
132 | mopac.actionRequest(conn1, offer, 1l);
|
---|
133 |
|
---|
134 | // 1 time for YourTurn, one time for ActionDone.
|
---|
135 | // but filtering ActionDone here (instead of Action) does not work,
|
---|
136 | // maybe bug in Mockito?
|
---|
137 | verify(conn1, times(2)).send(any(Inform.class));
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Fatal error occurs, run should fail
|
---|
142 | */
|
---|
143 | @Test(expected = RuntimeException.class)
|
---|
144 | public void testPartyFailsConnect()
|
---|
145 | throws IOException, NoResourcesNowException {
|
---|
146 | ProtocolToPartyConnFactory factory1 = mock(
|
---|
147 | ProtocolToPartyConnFactory.class);
|
---|
148 | // irrecoverable fail test
|
---|
149 | when(factory1.connect(any(List.class)))
|
---|
150 | .thenThrow(new IOException("Failed to connect to parties"));
|
---|
151 |
|
---|
152 | mopac.start(factory1);
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Nonfatal error occurs, protocol should retry till success
|
---|
158 | */
|
---|
159 | @Test
|
---|
160 | public void testPartyFailsConnect2ndtimeOK()
|
---|
161 | throws IOException, NoResourcesNowException {
|
---|
162 | long now = System.currentTimeMillis();
|
---|
163 | ProtocolToPartyConnFactory factory1 = mock(
|
---|
164 | ProtocolToPartyConnFactory.class);
|
---|
165 | // recoverable fail, and then success.
|
---|
166 | when(factory1.connect(any(List.class)))
|
---|
167 | .thenThrow(new NoResourcesNowException("try again later",
|
---|
168 | new Date(now + 100)))
|
---|
169 | .thenReturn(connections);
|
---|
170 |
|
---|
171 | mopac.start(factory1);
|
---|
172 |
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Test that in Offer phase, and party does action, the next phase is
|
---|
177 | * entered.
|
---|
178 | */
|
---|
179 | @Test
|
---|
180 | public void testProceedsNextPhase() {
|
---|
181 | // the state before the last party makes the offer
|
---|
182 | MOPAC2State offerstate = mock(MOPAC2State.class);
|
---|
183 | when(offerstate.getSettings()).thenReturn(settings);
|
---|
184 | OfferPhase offerphase = mock(OfferPhase.class);
|
---|
185 | when(offerphase.isFinal(anyLong())).thenReturn(false);
|
---|
186 | when(offerstate.getPhase()).thenReturn(offerphase);
|
---|
187 |
|
---|
188 | // the phase where all parties placed an offer
|
---|
189 | OfferPhase finalofferphase = mock(OfferPhase.class);
|
---|
190 | when(finalofferphase.isFinal(anyLong())).thenReturn(true);
|
---|
191 | MOPAC2State finalofferstate = mock(MOPAC2State.class);
|
---|
192 | when(finalofferstate.getPhase()).thenReturn(finalofferphase);
|
---|
193 | when(finalofferstate.finishPhase()).thenReturn(finalofferstate);
|
---|
194 |
|
---|
195 | when(offerstate.with(any(PartyId.class), any(Action.class), anyLong()))
|
---|
196 | .thenReturn(finalofferstate);
|
---|
197 |
|
---|
198 | // the phase where all parties can start voting.
|
---|
199 | // this is the state to be reached.
|
---|
200 | long now = System.currentTimeMillis();
|
---|
201 | MOPAC2State votingstate = mock(MOPAC2State.class);
|
---|
202 | Phase votingphase = mock(VotingPhase.class);
|
---|
203 | PartyStates votingpartystates = mock(PartyStates.class);
|
---|
204 | when(votingphase.getDeadline()).thenReturn(now + 100);
|
---|
205 | when(votingphase.getPartyStates()).thenReturn(votingpartystates);
|
---|
206 | when(votingstate.getPhase()).thenReturn(votingphase);
|
---|
207 |
|
---|
208 | when(finalofferstate.nextPhase(anyLong())).thenReturn(votingstate);
|
---|
209 |
|
---|
210 | mopac = new MOPAC2(offerstate, logger);
|
---|
211 | Offer action = mock(Offer.class);
|
---|
212 | mopac.actionRequest(conn3, action, now);
|
---|
213 |
|
---|
214 | // check that the action result in reaching the voting phase
|
---|
215 | assertEquals(votingstate, mopac.getState());
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|