source: protocol/src/test/java/geniusweb/protocol/session/learn/LearnStateTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 5.3 KB
Line 
1package geniusweb.protocol.session.learn;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8import static org.mockito.ArgumentMatchers.any;
9import static org.mockito.Mockito.mock;
10import static org.mockito.Mockito.when;
11
12import java.net.URISyntaxException;
13import java.util.Arrays;
14import java.util.List;
15
16import org.junit.Before;
17import org.junit.Test;
18
19import geniusweb.actions.EndNegotiation;
20import geniusweb.actions.LearningDone;
21import geniusweb.actions.PartyId;
22import geniusweb.deadline.Deadline;
23import geniusweb.deadline.DeadlineTime;
24import geniusweb.inform.Agreements;
25import geniusweb.progress.Progress;
26import geniusweb.protocol.ProtocolException;
27import geniusweb.protocol.session.SessionResult;
28import geniusweb.protocol.session.TeamInfo;
29import geniusweb.references.Parameters;
30import geniusweb.references.PartyRef;
31import geniusweb.references.PartyWithParameters;
32import geniusweb.references.PartyWithProfile;
33import geniusweb.references.ProfileRef;
34
35public 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(party2ref, 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(team2pp);
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.getResults().get(0);
160 assertTrue(res.getAgreements().getMap().isEmpty());
161 }
162}
Note: See TracBrowser for help on using the repository browser.