[39] | 1 | package geniusweb.protocol.session.learn;
|
---|
| 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.assertNull;
|
---|
| 7 | import static org.junit.Assert.assertTrue;
|
---|
| 8 | import static org.mockito.Matchers.any;
|
---|
| 9 | import static org.mockito.Mockito.mock;
|
---|
| 10 | import static org.mockito.Mockito.when;
|
---|
| 11 |
|
---|
| 12 | import java.net.URISyntaxException;
|
---|
| 13 | import java.util.Arrays;
|
---|
| 14 | import java.util.List;
|
---|
| 15 |
|
---|
| 16 | import org.junit.Before;
|
---|
| 17 | import org.junit.Test;
|
---|
| 18 |
|
---|
| 19 | import geniusweb.actions.EndNegotiation;
|
---|
| 20 | import geniusweb.actions.LearningDone;
|
---|
| 21 | import geniusweb.actions.PartyId;
|
---|
| 22 | import geniusweb.deadline.Deadline;
|
---|
| 23 | import geniusweb.deadline.DeadlineTime;
|
---|
| 24 | import geniusweb.inform.Agreements;
|
---|
| 25 | import geniusweb.progress.Progress;
|
---|
| 26 | import geniusweb.protocol.ProtocolException;
|
---|
| 27 | import geniusweb.protocol.session.SessionResult;
|
---|
| 28 | import geniusweb.protocol.session.TeamInfo;
|
---|
| 29 | import geniusweb.references.Parameters;
|
---|
| 30 | import geniusweb.references.PartyRef;
|
---|
| 31 | import geniusweb.references.PartyWithParameters;
|
---|
| 32 | import geniusweb.references.PartyWithProfile;
|
---|
| 33 | import geniusweb.references.ProfileRef;
|
---|
| 34 |
|
---|
| 35 | public class LearnStateTest {
|
---|
| 36 |
|
---|
| 37 | private static final String PARTY2 = "party2";
|
---|
| 38 | private static final String PARTY1 = "party1";
|
---|
| 39 | private static final PartyId party1id = new PartyId(PARTY1);
|
---|
| 40 | private static final PartyId party2id = new PartyId(PARTY2);
|
---|
| 41 | private LearnState state;
|
---|
| 42 | private final Deadline deadline = new DeadlineTime(10000);
|
---|
| 43 | private Parameters params;
|
---|
| 44 | private PartyWithProfile partyprofile1 = mock(PartyWithProfile.class);
|
---|
| 45 | private PartyWithProfile partyprofile2 = mock(PartyWithProfile.class);
|
---|
| 46 | private LearnState connectedstate;
|
---|
| 47 | private final Progress progressnotdone = mock(Progress.class);
|
---|
| 48 | private final Progress progressdone = mock(Progress.class);
|
---|
| 49 |
|
---|
| 50 | @Before
|
---|
| 51 | public void before() throws URISyntaxException {
|
---|
| 52 | TeamInfo team1, team2;
|
---|
| 53 | params = new Parameters();
|
---|
| 54 | params = params.with("persistentstate",
|
---|
| 55 | "6bb5f909-0079-43ac-a8ac-a31794391074");
|
---|
| 56 | params = params.with("negotiationdata",
|
---|
| 57 | Arrays.asList("12b5f909-0079-43ac-a8ac-a31794391012"));
|
---|
| 58 |
|
---|
| 59 | when(progressnotdone.isPastDeadline(any())).thenReturn(false);
|
---|
| 60 | when(progressdone.isPastDeadline(any())).thenReturn(true);
|
---|
| 61 |
|
---|
| 62 | // define team1
|
---|
| 63 | PartyRef party1ref = new PartyRef(PARTY1);
|
---|
| 64 | PartyWithParameters party1 = new PartyWithParameters(party1ref, params);
|
---|
| 65 | ProfileRef profile1 = new ProfileRef("http://prof1");
|
---|
| 66 | PartyWithProfile partywithp1 = new PartyWithProfile(party1, profile1);
|
---|
| 67 | List<PartyWithProfile> team1pp = Arrays.asList(partywithp1);
|
---|
| 68 | team1 = new TeamInfo(team1pp);
|
---|
| 69 |
|
---|
| 70 | // define team2
|
---|
| 71 | PartyRef party2ref = new PartyRef(PARTY2);
|
---|
| 72 | PartyWithParameters party2 = new PartyWithParameters(party1ref, params);
|
---|
| 73 | ProfileRef profile2 = new ProfileRef("http://prof2");
|
---|
| 74 | PartyWithProfile partywithp2 = new PartyWithProfile(party2, profile2);
|
---|
| 75 | List<PartyWithProfile> team2pp = Arrays.asList(partywithp2);
|
---|
| 76 | team2 = new TeamInfo(team1pp);
|
---|
| 77 |
|
---|
| 78 | List<TeamInfo> participants = Arrays.asList(team1, team2);
|
---|
| 79 | LearnSettings settings = new LearnSettings(participants, deadline);
|
---|
| 80 | this.state = new LearnState(settings);
|
---|
| 81 |
|
---|
| 82 | this.connectedstate = state.with(party1id, partyprofile1)
|
---|
| 83 | .with(party2id, partyprofile2).with(progressnotdone);
|
---|
| 84 |
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | @SuppressWarnings("unused")
|
---|
| 88 | @Test(expected = IllegalArgumentException.class)
|
---|
| 89 | public void smokeTestNull() {
|
---|
| 90 | new LearnState(null);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | @SuppressWarnings("unused")
|
---|
| 94 | @Test
|
---|
| 95 | public void smokeTest() {
|
---|
| 96 | LearnSettings settings = mock(LearnSettings.class);
|
---|
| 97 | new LearnState(settings);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Test
|
---|
| 101 | public void getAgreementsTest() {
|
---|
| 102 | assertEquals(new Agreements(), state.getAgreements());
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | @Test
|
---|
| 106 | public void isFinalTest() {
|
---|
| 107 | // initial state is false, because we check
|
---|
| 108 | // that there are no learningDone reports
|
---|
| 109 | assertFalse(state.isFinal(0));
|
---|
| 110 | // not final because there are connected parties not yet done.
|
---|
| 111 | assertFalse(connectedstate.isFinal(0));
|
---|
| 112 | LearnState finalstate = connectedstate.with(progressdone);
|
---|
| 113 | assertTrue(finalstate.isFinal(0));
|
---|
| 114 |
|
---|
| 115 | LearnState errorstate = connectedstate
|
---|
| 116 | .with(new ProtocolException("test", party1id));
|
---|
| 117 | assertTrue(errorstate.isFinal(0));
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | @Test
|
---|
| 121 | public void checkActionTest() {
|
---|
| 122 | // check basic illegal actions in Learning
|
---|
| 123 | assertNotNull(connectedstate.checkAction(party1id, null));
|
---|
| 124 | assertNotNull(connectedstate.checkAction(new PartyId("unknown"), null));
|
---|
| 125 | assertNotNull(connectedstate.checkAction(party1id,
|
---|
| 126 | new EndNegotiation(party1id)));
|
---|
| 127 | assertNotNull(connectedstate.checkAction(party1id,
|
---|
| 128 | new LearningDone(party2id)));
|
---|
| 129 |
|
---|
| 130 | // check a good action is allowed
|
---|
| 131 | assertNull(connectedstate.checkAction(party1id,
|
---|
| 132 | new LearningDone(party1id)));
|
---|
| 133 |
|
---|
| 134 | // no learning twice not ok
|
---|
| 135 | LearnState learned = connectedstate.with(party1id,
|
---|
| 136 | new LearningDone(party1id));
|
---|
| 137 | assertNotNull(
|
---|
| 138 | learned.checkAction(party1id, new LearningDone(party1id)));
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | @SuppressWarnings("unused")
|
---|
| 142 | @Test
|
---|
| 143 | public void LearnDoneActionTest() {
|
---|
| 144 | LearnState learned = connectedstate.with(party1id,
|
---|
| 145 | new LearningDone(party1id));
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | @SuppressWarnings("unused")
|
---|
| 149 | @Test
|
---|
| 150 | public void isFinalWhenAllLearned() {
|
---|
| 151 | LearnState learned = connectedstate
|
---|
| 152 | .with(party1id, new LearningDone(party1id))
|
---|
| 153 | .with(party2id, new LearningDone(party2id));
|
---|
| 154 | assertTrue(learned.isFinal(0));
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | @Test
|
---|
| 158 | public void getResultTest() {
|
---|
| 159 | SessionResult res = connectedstate.getResult();
|
---|
| 160 | assertTrue(res.getAgreements().getMap().isEmpty());
|
---|
| 161 | }
|
---|
| 162 | }
|
---|