package geniusweb.protocol.session.learn; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; import org.junit.Before; import org.junit.Test; import geniusweb.actions.EndNegotiation; import geniusweb.actions.LearningDone; import geniusweb.actions.PartyId; import geniusweb.deadline.Deadline; import geniusweb.deadline.DeadlineTime; import geniusweb.inform.Agreements; import geniusweb.progress.Progress; import geniusweb.protocol.ProtocolException; import geniusweb.protocol.session.SessionResult; import geniusweb.protocol.session.TeamInfo; import geniusweb.references.Parameters; import geniusweb.references.PartyRef; import geniusweb.references.PartyWithParameters; import geniusweb.references.PartyWithProfile; import geniusweb.references.ProfileRef; public class LearnStateTest { private static final String PARTY2 = "party2"; private static final String PARTY1 = "party1"; private static final PartyId party1id = new PartyId(PARTY1); private static final PartyId party2id = new PartyId(PARTY2); private LearnState state; private final Deadline deadline = new DeadlineTime(10000); private Parameters params; private PartyWithProfile partyprofile1 = mock(PartyWithProfile.class); private PartyWithProfile partyprofile2 = mock(PartyWithProfile.class); private LearnState connectedstate; private final Progress progressnotdone = mock(Progress.class); private final Progress progressdone = mock(Progress.class); @Before public void before() throws URISyntaxException { TeamInfo team1, team2; params = new Parameters(); params = params.with("persistentstate", "6bb5f909-0079-43ac-a8ac-a31794391074"); params = params.with("negotiationdata", Arrays.asList("12b5f909-0079-43ac-a8ac-a31794391012")); when(progressnotdone.isPastDeadline(any())).thenReturn(false); when(progressdone.isPastDeadline(any())).thenReturn(true); // define team1 PartyRef party1ref = new PartyRef(PARTY1); PartyWithParameters party1 = new PartyWithParameters(party1ref, params); ProfileRef profile1 = new ProfileRef("http://prof1"); PartyWithProfile partywithp1 = new PartyWithProfile(party1, profile1); List team1pp = Arrays.asList(partywithp1); team1 = new TeamInfo(team1pp); // define team2 PartyRef party2ref = new PartyRef(PARTY2); PartyWithParameters party2 = new PartyWithParameters(party1ref, params); ProfileRef profile2 = new ProfileRef("http://prof2"); PartyWithProfile partywithp2 = new PartyWithProfile(party2, profile2); List team2pp = Arrays.asList(partywithp2); team2 = new TeamInfo(team1pp); List participants = Arrays.asList(team1, team2); LearnSettings settings = new LearnSettings(participants, deadline); this.state = new LearnState(settings); this.connectedstate = state.with(party1id, partyprofile1) .with(party2id, partyprofile2).with(progressnotdone); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void smokeTestNull() { new LearnState(null); } @SuppressWarnings("unused") @Test public void smokeTest() { LearnSettings settings = mock(LearnSettings.class); new LearnState(settings); } @Test public void getAgreementsTest() { assertEquals(new Agreements(), state.getAgreements()); } @Test public void isFinalTest() { // initial state is false, because we check // that there are no learningDone reports assertFalse(state.isFinal(0)); // not final because there are connected parties not yet done. assertFalse(connectedstate.isFinal(0)); LearnState finalstate = connectedstate.with(progressdone); assertTrue(finalstate.isFinal(0)); LearnState errorstate = connectedstate .with(new ProtocolException("test", party1id)); assertTrue(errorstate.isFinal(0)); } @Test public void checkActionTest() { // check basic illegal actions in Learning assertNotNull(connectedstate.checkAction(party1id, null)); assertNotNull(connectedstate.checkAction(new PartyId("unknown"), null)); assertNotNull(connectedstate.checkAction(party1id, new EndNegotiation(party1id))); assertNotNull(connectedstate.checkAction(party1id, new LearningDone(party2id))); // check a good action is allowed assertNull(connectedstate.checkAction(party1id, new LearningDone(party1id))); // no learning twice not ok LearnState learned = connectedstate.with(party1id, new LearningDone(party1id)); assertNotNull( learned.checkAction(party1id, new LearningDone(party1id))); } @SuppressWarnings("unused") @Test public void LearnDoneActionTest() { LearnState learned = connectedstate.with(party1id, new LearningDone(party1id)); } @SuppressWarnings("unused") @Test public void isFinalWhenAllLearned() { LearnState learned = connectedstate .with(party1id, new LearningDone(party1id)) .with(party2id, new LearningDone(party2id)); assertTrue(learned.isFinal(0)); } @Test public void getResultTest() { SessionResult res = connectedstate.getResult(); assertTrue(res.getAgreements().getMap().isEmpty()); } }