Changeset 24 for protocol/src/test/java
- Timestamp:
- 10/06/20 13:12:20 (4 years ago)
- Location:
- protocol/src/test/java/geniusweb/protocol/session
- Files:
-
- 4 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
protocol/src/test/java/geniusweb/protocol/session/mopac/MOPACTest.java
r21 r24 2 2 3 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertFalse; 4 5 import static org.junit.Assert.assertNotNull; 5 6 import static org.junit.Assert.assertTrue; 6 7 import static org.mockito.Matchers.any; 8 import static org.mockito.Matchers.anyLong; 7 9 import static org.mockito.Mockito.mock; 8 10 import static org.mockito.Mockito.times; … … 12 14 import java.io.IOException; 13 15 import java.util.Arrays; 16 import java.util.Date; 14 17 import java.util.List; 15 18 16 19 import org.junit.Before; 17 import org.junit.Ignore;18 20 import org.junit.Test; 19 21 … … 28 30 import geniusweb.protocol.partyconnection.ProtocolToPartyConn; 29 31 import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory; 32 import geniusweb.protocol.session.mopac.phase.OfferPhase; 33 import geniusweb.protocol.session.mopac.phase.Phase; 34 import geniusweb.protocol.session.mopac.phase.VotingPhase; 30 35 import tudelft.utilities.logging.Reporter; 31 36 import tudelft.utilities.repository.NoResourcesNowException; … … 57 62 conn3 = mock(ProtocolToPartyConn.class); 58 63 64 private List<ProtocolToPartyConn> connections; 65 59 66 @Before 60 67 public void before() throws JsonParseException, JsonMappingException, … … 63 70 mopac = settings.getProtocol(logger); 64 71 65 Listconnections = Arrays.asList(conn1, conn2, conn3);72 connections = Arrays.asList(conn1, conn2, conn3); 66 73 when(factory.connect(any(List.class))).thenReturn(connections); 67 74 … … 94 101 } 95 102 96 @Ignore97 103 @Test 98 104 public void testWorkingCheckDeadline() … … 101 107 mopac.start(factory); 102 108 103 System.out.println(mopac.getState()); 104 109 assertFalse(mopac.getState().isFinal(System.currentTimeMillis())); 105 110 // we have deadline in 100ms so check 106 111 Thread.sleep(200); 107 108 System.out.println(mopac.getState());109 112 assertTrue(mopac.getState().isFinal(System.currentTimeMillis())); 110 113 } 111 114 112 @Ignore113 115 @Test 114 116 public void testSubscribedToConnections() { … … 119 121 } 120 122 121 @Ignore122 123 @Test 123 124 public void testActionIsBroadcast() … … 134 135 } 135 136 136 @Test 137 public void testAgreementTerminates() { 138 139 } 137 /** 138 * Fatal error occurs, run should fail 139 */ 140 @Test(expected = RuntimeException.class) 141 public void testPartyFailsConnect() 142 throws IOException, NoResourcesNowException { 143 ProtocolToPartyConnFactory factory1 = mock( 144 ProtocolToPartyConnFactory.class); 145 // irrecoverable fail test 146 when(factory1.connect(any(List.class))) 147 .thenThrow(new IOException("Failed to connect to parties")); 148 149 mopac.start(factory1); 150 151 } 152 153 /** 154 * Nonfatal error occurs, protocol should retry till success 155 */ 156 @Test 157 public void testPartyFailsConnect2ndtimeOK() 158 throws IOException, NoResourcesNowException { 159 long now = System.currentTimeMillis(); 160 ProtocolToPartyConnFactory factory1 = mock( 161 ProtocolToPartyConnFactory.class); 162 // recoverable fail, and then success. 163 when(factory1.connect(any(List.class))) 164 .thenThrow(new NoResourcesNowException("try again later", 165 new Date(now + 100))) 166 .thenReturn(connections); 167 168 mopac.start(factory1); 169 170 } 171 172 /** 173 * Test that in Offer phase, and party does action, the next phase is 174 * entered. 175 */ 176 @Test 177 public void testProceedsNextPhase() { 178 // the state before the last party makes the offer 179 MOPACState offerstate = mock(MOPACState.class); 180 when(offerstate.getSettings()).thenReturn(settings); 181 OfferPhase offerphase = mock(OfferPhase.class); 182 when(offerphase.isFinal(anyLong())).thenReturn(false); 183 when(offerstate.getPhase()).thenReturn(offerphase); 184 185 // the phase where all parties placed an offer 186 OfferPhase finalofferphase = mock(OfferPhase.class); 187 when(finalofferphase.isFinal(anyLong())).thenReturn(true); 188 MOPACState finalofferstate = mock(MOPACState.class); 189 when(finalofferstate.getPhase()).thenReturn(finalofferphase); 190 when(finalofferstate.finishPhase()).thenReturn(finalofferstate); 191 192 when(offerstate.with(any(PartyId.class), any(Action.class), anyLong())) 193 .thenReturn(finalofferstate); 194 195 // the phase where all parties can start voting. 196 // this is the state to be reached. 197 long now = System.currentTimeMillis(); 198 MOPACState votingstate = mock(MOPACState.class); 199 Phase votingphase = mock(VotingPhase.class); 200 PartyStates votingpartystates = mock(PartyStates.class); 201 when(votingphase.getDeadline()).thenReturn(now + 100); 202 when(votingphase.getPartyStates()).thenReturn(votingpartystates); 203 when(votingstate.getPhase()).thenReturn(votingphase); 204 205 when(finalofferstate.nextPhase(anyLong())).thenReturn(votingstate); 206 207 mopac = new MOPAC(offerstate, logger); 208 Offer action = mock(Offer.class); 209 mopac.actionRequest(conn3, action, now); 210 211 // check that the action result in reaching the voting phase 212 assertEquals(votingstate, mopac.getState()); 213 } 214 140 215 } -
protocol/src/test/java/geniusweb/protocol/session/mopac/PartyStatesTest.java
r21 r24 3 3 import static org.junit.Assert.assertEquals; 4 4 5 import java.io.IOException; 6 import java.net.URISyntaxException; 5 7 import java.util.Arrays; 6 8 import java.util.Collections; 7 9 import java.util.HashMap; 8 10 import java.util.HashSet; 11 import java.util.List; 9 12 import java.util.Map; 10 13 11 14 import org.junit.Test; 12 15 16 import com.fasterxml.jackson.core.JsonProcessingException; 17 import com.fasterxml.jackson.databind.ObjectMapper; 18 13 19 import geniusweb.actions.PartyId; 14 20 import geniusweb.inform.Agreements; 15 21 import geniusweb.protocol.ProtocolException; 22 import tudelft.utilities.junit.GeneralTests; 16 23 17 public class PartyStatesTest { 24 public class PartyStatesTest extends GeneralTests<PartyStates> { 25 private final ObjectMapper jackson = new ObjectMapper(); 18 26 19 27 private final PartyId party1 = new PartyId("party1"); … … 22 30 23 31 private final Map<PartyId, Integer> powers = new HashMap<>(); 24 private final PartyStates states; 32 private final PartyStates states1, states1a, states2; 33 private final String serialized = "{\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{},\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3}}"; 25 34 26 35 public PartyStatesTest() { 36 37 // FIXME use the other constructor to really test this. 38 // PartyStates(Set<PartyId> notYetActed, List<Action> actions, 39 // Agreements agreements, List<PartyId> walkedAway, 40 // Map<PartyId, ProtocolException> exceptions, 41 // Map<PartyId, Integer> powers) 42 // 27 43 powers.put(party1, 2); 28 44 powers.put(party2, 3); 45 states2 = new PartyStates(powers); 29 46 powers.put(party3, 3); 30 states = new PartyStates(powers); 47 states1 = new PartyStates(powers); 48 states1a = new PartyStates(powers); 49 } 50 51 @Override 52 public List<List<PartyStates>> getGeneralTestData() { 53 return Arrays.asList(Arrays.asList(states1, states1a), 54 Arrays.asList(states2)); 55 } 56 57 @Override 58 public List<String> getGeneralTestStrings() { 59 return Arrays.asList( 60 "PartyStates.*\\[party2, party1, party3\\],\\[\\],Agreements\\{\\},\\[\\],\\{\\}.*", 61 "PartyStates.*\\[party2, party1\\],\\[\\],Agreements\\{\\},\\[\\],\\{\\}.*"); 31 62 } 32 63 33 64 @Test 34 65 public void testBasics() { 35 assertEquals(powers.keySet(), states .getNegotiatingParties());36 assertEquals(powers.keySet(), states .getNotYetActed());37 assertEquals(0, states .getExceptions().size());66 assertEquals(powers.keySet(), states1.getNegotiatingParties()); 67 assertEquals(powers.keySet(), states1.getNotYetActed()); 68 assertEquals(0, states1.getExceptions().size()); 38 69 } 39 70 40 71 @Test 41 72 public void testException() { 42 PartyStates newstates = states 73 PartyStates newstates = states1 43 74 .with(new ProtocolException("bla", party1)); 44 75 assertEquals(new Agreements(), newstates.getAgreements()); 45 assertEquals(powers.keySet(), states .getNegotiatingParties());76 assertEquals(powers.keySet(), states1.getNegotiatingParties()); 46 77 assertEquals(new HashSet<>(Arrays.asList(party2, party3)), 47 78 newstates.getNotYetActed()); … … 50 81 @Test 51 82 public void testFinish() { 52 PartyStates newstates = states .finish();83 PartyStates newstates = states1.finish(); 53 84 assertEquals(new Agreements(), newstates.getAgreements()); 54 assertEquals(powers.keySet(), states .getNegotiatingParties());85 assertEquals(powers.keySet(), states1.getNegotiatingParties()); 55 86 assertEquals(Collections.emptySet(), newstates.getNotYetActed()); 56 87 } 88 89 @Test 90 public void testDeserialize() throws IOException { 91 PartyStates obj = jackson.readValue(serialized, PartyStates.class); 92 System.out.println(obj); 93 assertEquals(states1, obj); 94 } 95 96 @Test 97 public void testSerialize() 98 throws JsonProcessingException, URISyntaxException { 99 100 String string = jackson.writeValueAsString(states1); 101 System.out.println(string); 102 assertEquals(serialized, string); 103 } 104 105 @Test 106 public void testWalkAway() { 107 PartyStates walkawaystate = states1.withWalkAway(party2); 108 assertEquals(new HashSet<>(Arrays.asList(party1, party3)), 109 walkawaystate.getNotYetActed()); 110 assertEquals(Arrays.asList(party2), walkawaystate.getWalkedAway()); 111 } 57 112 } -
protocol/src/test/java/geniusweb/protocol/session/mopac/phase/OfferPhaseTest.java
r21 r24 4 4 import static org.junit.Assert.assertFalse; 5 5 import static org.junit.Assert.assertTrue; 6 import static org.junit.Assert.fail; 6 7 import static org.mockito.Mockito.mock; 7 8 import static org.mockito.Mockito.when; 9 10 import java.io.IOException; 11 import java.net.URISyntaxException; 12 import java.util.Arrays; 8 13 import java.util.Collections; 9 14 import java.util.HashMap; 15 import java.util.List; 10 16 import java.util.Map; 11 17 12 18 import org.junit.Test; 13 19 20 import com.fasterxml.jackson.core.JsonProcessingException; 21 import com.fasterxml.jackson.databind.ObjectMapper; 22 23 import geniusweb.actions.Accept; 14 24 import geniusweb.actions.EndNegotiation; 25 import geniusweb.actions.Offer; 15 26 import geniusweb.actions.PartyId; 16 27 import geniusweb.actions.Votes; 28 import geniusweb.inform.YourTurn; 29 import geniusweb.issuevalue.Bid; 30 import geniusweb.issuevalue.DiscreteValue; 17 31 import geniusweb.protocol.ProtocolException; 18 32 import geniusweb.protocol.session.mopac.PartyStates; 19 33 import geniusweb.voting.VotingEvaluator; 34 import geniusweb.voting.votingevaluators.LargestAgreement; 35 import geniusweb.voting.votingevaluators.LargestAgreementsLoop; 36 import tudelft.utilities.junit.GeneralTests; 20 37 21 38 /** 22 39 * We also test defaultPhase here. 23 40 */ 24 public class OfferPhaseTest { 25 41 public class OfferPhaseTest extends GeneralTests<OfferPhase> { 42 private final ObjectMapper jackson = new ObjectMapper(); 43 44 private static final long DEADLINE = 10l; 26 45 private final PartyId party1 = new PartyId("party1"); 27 46 private final PartyId party2 = new PartyId("party2"); … … 29 48 30 49 private final Map<PartyId, Integer> powers = new HashMap<>(); 31 private final PartyStates states; 32 private final VotingEvaluator evaluator = mock(VotingEvaluator.class); 33 private final OfferPhase phase; 50 private final PartyStates states, states2; 51 private final VotingEvaluator evaluator = new LargestAgreement(); 52 private final VotingEvaluator evaluator2 = new LargestAgreementsLoop(); 53 private final OfferPhase phase, phase1, phase1a, phase2, phase3, phase4; 54 55 // just a test bid 56 private final Bid bid = new Bid("issue", new DiscreteValue("yes")); 57 private final Offer offer = new Offer(party1, bid); 58 59 private final String serialized = "{\"OfferPhase\":{\"partyStates\":{\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{},\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3}},\"evaluator\":{\"LargestAgreement\":{}},\"deadline\":10}}"; 34 60 35 61 public OfferPhaseTest() { 36 62 powers.put(party1, 2); 37 63 powers.put(party2, 3); 64 states2 = new PartyStates(powers); 38 65 powers.put(party3, 3); 39 66 states = new PartyStates(powers); 40 phase = new OfferPhase(null, states, 10l, evaluator); 41 } 42 43 @Test 44 public void isInitFinalTest() { 67 phase = new OfferPhase(states, DEADLINE, evaluator); 68 69 phase1 = new OfferPhase(states, 10l, evaluator); 70 phase1a = new OfferPhase(states, 10l, evaluator); 71 phase2 = new OfferPhase(states2, 10l, evaluator); 72 phase3 = new OfferPhase(states, 20l, evaluator); 73 phase4 = new OfferPhase(states, 10l, evaluator2); 74 75 } 76 77 @Override 78 public List<List<OfferPhase>> getGeneralTestData() { 79 return Arrays.asList(Arrays.asList(phase1, phase1a), 80 Arrays.asList(phase2), Arrays.asList(phase3), 81 Arrays.asList(phase4)); 82 } 83 84 @Override 85 public List<String> getGeneralTestStrings() { 86 return Arrays.asList( 87 "OfferPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*", 88 "OfferPhase.*PartyStates.*party2, party1.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*", 89 "OfferPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*", 90 "OfferPhase.*PartyStates.*party2, party1, party3.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreementsLoop.*"); 91 } 92 93 @Test 94 public void smokeTest() { 95 } 96 97 @Test 98 public void testInitState() { 99 assertEquals(3, phase.getPartyStates().getNotYetActed().size()); 100 } 101 102 @Test 103 public void testInform() { 104 assertEquals(new YourTurn(), phase.getInform()); 105 } 106 107 @Test 108 public void isFinalTest() { 45 109 assertFalse(phase.isFinal(1l)); 46 110 assertTrue(phase.isFinal(10l)); 111 } 112 113 @Test 114 public void isFinalTestActorNotYetActed() { 115 PartyStates actedstate = mock(PartyStates.class); 116 when(actedstate.getNotYetActed()) 117 .thenReturn(Collections.singleton(party1)); 118 OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator); 119 assertFalse(testphase.isFinal(1l)); 120 } 121 122 @Test 123 public void isFinalTestAllActorsActed() { 124 PartyStates actedstate = mock(PartyStates.class); 125 when(actedstate.getNotYetActed()).thenReturn(Collections.emptySet()); 126 OfferPhase testphase = new OfferPhase(actedstate, 10l, evaluator); 127 assertTrue(testphase.isFinal(1l)); 47 128 } 48 129 … … 65 146 } 66 147 148 @Test 149 public void testFinish() { 150 Phase ph = phase.finish(); 151 assertTrue(ph.getPartyStates().getNotYetActed().isEmpty()); 152 assertEquals(3, ph.getPartyStates().getExceptions().size()); 153 assertTrue(phase.getPartyStates().getAgreements().getMap().isEmpty()); 154 } 155 156 @Test(expected = IllegalStateException.class) 157 public void testNextWrong() { 158 // state is not final, should not work 159 phase.next(1, 1000); 160 } 161 162 @Test 163 public void testNextNoParties() { 164 Phase ph = phase.finish(); 165 // no parties are left now, all failed 166 // but this is not part of the next() check. 167 Phase next = ph.next(1, 1000); 168 assertTrue(next instanceof VotingPhase); 169 // no remaining parties, since finish kicked all 170 assertTrue(next.getPartyStates().getNotYetActed().isEmpty()); 171 } 172 173 @Test 174 public void testAllowed() { 175 phase.with(party1, offer, 1); 176 } 177 178 @Test 179 public void testAllowedWrongClass() { 180 try { 181 phase.checkAction(party1, new Accept(party1, bid), 1); 182 } catch (ProtocolException e) { 183 assertTrue(e.getMessage().contains("not allowed in OfferPhase")); 184 return; 185 } 186 fail("checkAction did not throw as expected"); 187 } 188 189 @Test 190 public void testAllowedWrongActor() { 191 try { 192 phase.checkAction(party1, new Accept(party2, bid), 1); 193 } catch (ProtocolException e) { 194 assertTrue( 195 e.getMessage().contains("Incorrect actor info in action")); 196 return; 197 } 198 fail("checkAction did not throw as expected"); 199 } 200 201 @Test 202 public void testAllowedActorAlreadyActed() { 203 OfferPhase testphase = phase.with(party1, offer, 1); 204 205 try { 206 testphase.checkAction(party1, offer, 2); 207 } catch (ProtocolException e) { 208 assertTrue(e.getMessage().contains("can not act anymore")); 209 return; 210 } 211 fail("checkAction did not throw as expected"); 212 } 213 214 @Test 215 public void testAllowedActorAlreadyActed1() { 216 // as theoprevious test, but using with() instead of checkAction 217 OfferPhase testphase = phase.with(party1, offer, 1); 218 219 OfferPhase newphase = testphase.with(party1, offer, 2); 220 // the party must remain as acted, because we can't retract his 221 // action... 222 assertEquals(1, newphase.getPartyStates().getActions().size()); 223 assertEquals(offer, newphase.getPartyStates().getActions().get(0)); 224 assertFalse( 225 newphase.getPartyStates().getExceptions().containsKey(party1)); 226 } 227 228 @Test 229 public void testAllowedActingTooLate() { 230 231 try { 232 phase.checkAction(party1, offer, DEADLINE + 1); 233 } catch (ProtocolException e) { 234 assertTrue(e.getMessage().contains("passed deadline")); 235 return; 236 } 237 fail("checkAction did not throw as expected"); 238 } 239 240 @Test 241 public void getOffersTest() { 242 assertEquals(Collections.emptyList(), phase.getOffers()); 243 List<Bid> offs = phase.with(party1, offer, 1).getOffers(); 244 assertEquals(Arrays.asList(bid), offs); 245 } 246 247 @Test(expected = IllegalArgumentException.class) 248 public void testNextTooShortDuration() { 249 phase.next(1, Phase.PHASE_MINTIME - 1); 250 } 251 252 @Test(expected = IllegalStateException.class) 253 public void testNextNotFinished() { 254 phase.next(1, Phase.PHASE_MINTIME + 1); 255 } 256 257 @Test(expected = IllegalStateException.class) 258 public void testNext() { 259 phase.next(11, Phase.PHASE_MINTIME + 1); 260 } 261 262 @Test 263 public void testDeserialize() throws IOException { 264 Phase obj = jackson.readValue(serialized, Phase.class); 265 System.out.println(obj); 266 assertEquals(phase1, obj); 267 } 268 269 @Test 270 public void testSerialize() 271 throws JsonProcessingException, URISyntaxException { 272 273 String string = jackson.writeValueAsString(phase1); 274 System.out.println(string); 275 assertEquals(serialized, string); 276 } 67 277 } -
protocol/src/test/java/geniusweb/protocol/session/saop/SAOPTest.java
r21 r24 34 34 import geniusweb.events.CurrentState; 35 35 import geniusweb.events.ProtocolEvent; 36 import geniusweb.inform.Agreements; 36 37 import geniusweb.inform.Finished; 37 38 import geniusweb.inform.Inform; … … 177 178 when(state.toString()).thenReturn(asText); 178 179 when(state.with(any(ProtocolException.class))).thenReturn(failstate); 180 when(state.getAgreements()).thenReturn(mock(Agreements.class)); 179 181 } 180 182 … … 260 262 @Test 261 263 public void testActionRequestWrongActor() { 262 263 264 saop.actionRequest(conn2, mock(EndNegotiation.class)); 264 265 … … 290 291 when(state.with(any(PartyId.class), any(EndNegotiation.class))) 291 292 .thenReturn(finalstate); 293 // when(finalstate.getAgreements()).thenReturn(mock(Agreements.class)); 292 294 293 295 saop.actionRequest(conn1, mock(EndNegotiation.class)); … … 311 313 doThrow(new IOException("fail sending yourturn")).when(conn1) 312 314 .send(any(YourTurn.class)); 313 315 // CHECK is the state really final after an exception? 316 when(state.with(any(ProtocolException.class))).thenReturn(finalstate); 317 // when(state.getAgreements()).thenReturn(mock(Agreements.class)); 314 318 saop.actionRequest(conn1, mock(Accept.class)); 315 319 … … 322 326 doThrow(new IOException("fail sending yourturn")).when(conn1) 323 327 .send(any(YourTurn.class)); 328 // when(failstate.getAgreements()).thenReturn(mock(Agreements.class)); 324 329 // not turn of conn2. 325 330 saop.actionRequest(conn2, mock(EndNegotiation.class)); … … 332 337 public void testActionInFinalState() throws IOException { 333 338 saop = new SAOP(finalstate, new ReportToLogger("test")); 339 // when(finalstate.getAgreements()).thenReturn(mock(Agreements.class)); 334 340 335 341 saop.actionRequest(conn1, mock(Offer.class));
Note:
See TracChangeset
for help on using the changeset viewer.