source: protocol/src/test/java/geniusweb/protocol/session/learn/LearnTest.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.9 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.assertTrue;
7import static org.mockito.Matchers.any;
8import static org.mockito.Matchers.isA;
9import static org.mockito.Mockito.mock;
10import static org.mockito.Mockito.times;
11import static org.mockito.Mockito.verify;
12import static org.mockito.Mockito.when;
13
14import java.io.IOException;
15import java.net.URISyntaxException;
16import java.util.Arrays;
17import java.util.List;
18
19import org.junit.Before;
20import org.junit.Test;
21
22import geniusweb.actions.EndNegotiation;
23import geniusweb.actions.LearningDone;
24import geniusweb.actions.PartyId;
25import geniusweb.deadline.Deadline;
26import geniusweb.deadline.DeadlineTime;
27import geniusweb.inform.Finished;
28import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
29import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
30import geniusweb.protocol.session.TeamInfo;
31import geniusweb.references.Parameters;
32import geniusweb.references.PartyRef;
33import geniusweb.references.PartyWithParameters;
34import geniusweb.references.PartyWithProfile;
35import geniusweb.references.ProfileRef;
36import tudelft.utilities.logging.Reporter;
37import tudelft.utilities.repository.NoResourcesNowException;
38
39public class LearnTest {
40
41 private final Reporter reporter = mock(Reporter.class);
42 private Parameters params = new Parameters();
43 private Deadline deadline = new DeadlineTime(1800);
44 private PartyId party1id = new PartyId("party1");;
45 private PartyId party2id = new PartyId("party2");;
46 private final ProtocolToPartyConn conn1 = mock(ProtocolToPartyConn.class),
47 conn2 = mock(ProtocolToPartyConn.class);
48
49 @Before
50 public void before() {
51 params = params.with("persistentstate",
52 "6bb5f909-0079-43ac-a8ac-a31794391074");
53 params = params.with("negotiationdata",
54 Arrays.asList("12b5f909-0079-43ac-a8ac-a31794391012"));
55
56 }
57
58 @SuppressWarnings("unused")
59 @Test
60 public void smokeTest() {
61 new Learn(mock(LearnState.class), mock(Reporter.class));
62 }
63
64 @Test
65 public void getDescrTest() {
66 Learn l = new Learn(mock(LearnState.class), mock(Reporter.class));
67
68 assertNotNull(l.getDescription());
69 }
70
71 @Test
72 public void getStateTest() {
73 LearnState state = mock(LearnState.class);
74 Learn l = new Learn(state, mock(Reporter.class));
75 assertEquals(state, l.getState());
76 }
77
78 @Test
79 public void getRefTest() {
80 Learn l = new Learn(mock(LearnState.class), mock(Reporter.class));
81 assertEquals("Learn", l.getRef().getURI().getPath().toString());
82 }
83
84 @Test
85 public void finalStateNotificationTest() {
86 // check that listeners get notified when session ends.
87
88 }
89
90 @Test
91 public void testStartStopBasic() throws IOException,
92 NoResourcesNowException, InterruptedException, URISyntaxException {
93 Learn learn = createBasicLearn();
94 ProtocolToPartyConnFactory factory = createFactory();
95 learn.start(factory);
96 assertFalse(learn.getState().isFinal(System.currentTimeMillis()));
97 Thread.sleep(2000);
98 assertTrue(learn.getState().isFinal(System.currentTimeMillis()));
99 }
100
101 @Test
102 public void testStartStopLearn() throws IOException,
103 NoResourcesNowException, InterruptedException, URISyntaxException {
104 Learn learn = createBasicLearn();
105 ProtocolToPartyConnFactory factory = createFactory();
106 learn.start(factory);
107 assertFalse(learn.getState().isFinal(System.currentTimeMillis()));
108
109 // Instead of mocking connectin we call actionRequest directly
110 learn.actionRequest(conn1, new LearningDone(party1id));
111 assertFalse(learn.getState().isFinal(System.currentTimeMillis()));
112 learn.actionRequest(conn2, new LearningDone(party2id));
113 assertTrue(learn.getState().isFinal(System.currentTimeMillis()));
114 }
115
116 @Test(expected = IllegalStateException.class)
117 public void testAddParty() {
118 Learn l = new Learn(mock(LearnState.class), mock(Reporter.class));
119 l.addParticipant(mock(PartyWithProfile.class));
120 }
121
122 @Test
123 public void isFinishSentNormally()
124 throws URISyntaxException, IOException, NoResourcesNowException {
125 Learn learn = createBasicLearn();
126 ProtocolToPartyConnFactory factory = createFactory();
127 learn.start(factory);
128 learn.actionRequest(conn1, new LearningDone(party1id));
129 learn.actionRequest(conn2, new LearningDone(party2id));
130 verify(conn1, times(1)).send(isA(Finished.class));
131 verify(conn2, times(1)).send(isA(Finished.class));
132 }
133
134 @Test
135 public void isFinishSentInError()
136 throws URISyntaxException, IOException, NoResourcesNowException {
137 Learn learn = createBasicLearn();
138 ProtocolToPartyConnFactory factory = createFactory();
139 learn.start(factory);
140 learn.actionRequest(conn1, new EndNegotiation(party1id));
141 verify(conn1, times(1)).send(isA(Finished.class));
142 verify(conn2, times(1)).send(isA(Finished.class));
143 }
144
145 private Learn createBasicLearn() throws URISyntaxException {
146 TeamInfo team1 = createTeam(1);
147 TeamInfo team2 = createTeam(2);
148 LearnSettings settings = new LearnSettings(Arrays.asList(team1, team2),
149 deadline);
150 return (Learn) settings.getProtocol(reporter);
151
152 }
153
154 private TeamInfo createTeam(int nr) throws URISyntaxException {
155 PartyRef party1ref = new PartyRef("party" + nr);
156 PartyWithParameters party1 = new PartyWithParameters(party1ref, params);
157 ProfileRef profile1 = new ProfileRef("prof" + nr);
158 PartyWithProfile partywithp1 = new PartyWithProfile(party1, profile1);
159 List<PartyWithProfile> team1pp = Arrays.asList(partywithp1);
160 TeamInfo team = new TeamInfo(team1pp);
161 return team;
162 }
163
164 private ProtocolToPartyConnFactory createFactory()
165 throws IOException, NoResourcesNowException {
166 when(conn1.getParty()).thenReturn(party1id);
167 when(conn2.getParty()).thenReturn(party2id);
168
169 ProtocolToPartyConnFactory factory = mock(
170 ProtocolToPartyConnFactory.class);
171 // connections = mock(List.class);
172 List<ProtocolToPartyConn> connections = Arrays.asList(conn1, conn2);
173 when(factory.connect(any(List.class))).thenReturn(connections);
174 return factory;
175 }
176
177}
Note: See TracBrowser for help on using the repository browser.