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.assertTrue;
|
---|
7 | import static org.mockito.Matchers.any;
|
---|
8 | import static org.mockito.Matchers.isA;
|
---|
9 | import static org.mockito.Mockito.mock;
|
---|
10 | import static org.mockito.Mockito.times;
|
---|
11 | import static org.mockito.Mockito.verify;
|
---|
12 | import static org.mockito.Mockito.when;
|
---|
13 |
|
---|
14 | import java.io.IOException;
|
---|
15 | import java.net.URISyntaxException;
|
---|
16 | import java.util.Arrays;
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
19 | import org.junit.Before;
|
---|
20 | import org.junit.Test;
|
---|
21 |
|
---|
22 | import geniusweb.actions.EndNegotiation;
|
---|
23 | import geniusweb.actions.LearningDone;
|
---|
24 | import geniusweb.actions.PartyId;
|
---|
25 | import geniusweb.deadline.Deadline;
|
---|
26 | import geniusweb.deadline.DeadlineTime;
|
---|
27 | import geniusweb.inform.Finished;
|
---|
28 | import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
|
---|
29 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
|
---|
30 | import geniusweb.protocol.session.TeamInfo;
|
---|
31 | import geniusweb.references.Parameters;
|
---|
32 | import geniusweb.references.PartyRef;
|
---|
33 | import geniusweb.references.PartyWithParameters;
|
---|
34 | import geniusweb.references.PartyWithProfile;
|
---|
35 | import geniusweb.references.ProfileRef;
|
---|
36 | import tudelft.utilities.logging.Reporter;
|
---|
37 | import tudelft.utilities.repository.NoResourcesNowException;
|
---|
38 |
|
---|
39 | public 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 | }
|
---|