source: protocol/src/test/java/geniusweb/protocol/session/mopac2/MOPAC2SettingsTest.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: 6.1 KB
Line 
1package geniusweb.protocol.session.mopac2;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Mockito.mock;
5import static org.mockito.Mockito.when;
6
7import java.io.IOException;
8import java.net.URISyntaxException;
9import java.util.Arrays;
10import java.util.List;
11
12import org.junit.Before;
13import org.junit.Test;
14
15import com.fasterxml.jackson.core.JsonProcessingException;
16import com.fasterxml.jackson.databind.ObjectMapper;
17
18import geniusweb.deadline.Deadline;
19import geniusweb.deadline.DeadlineRounds;
20import geniusweb.deadline.DeadlineTime;
21import geniusweb.protocol.session.SessionSettings;
22import geniusweb.protocol.session.TeamInfo;
23import geniusweb.references.Parameters;
24import geniusweb.references.PartyRef;
25import geniusweb.references.PartyWithParameters;
26import geniusweb.references.PartyWithProfile;
27import geniusweb.references.ProfileRef;
28import geniusweb.voting.VotingEvaluatorWithValue;
29import geniusweb.voting.evaluatorwithvalue.LargestAgreementWithValue;
30import tudelft.utilities.junit.GeneralTests;
31import tudelft.utilities.logging.ReportToLogger;
32
33public class MOPAC2SettingsTest extends GeneralTests<MOPAC2Settings> {
34
35 private final ObjectMapper jackson = new ObjectMapper();
36 private final Parameters noparams = new Parameters();
37 private TeamInfo partyprof1 = mockParty("party1", noparams);
38 private TeamInfo partyprof2 = mockParty("party2", noparams);
39 private TeamInfo partyprof3 = mockParty("party3", noparams);
40 private List<TeamInfo> participants2 = Arrays.asList(partyprof1,
41 partyprof2);
42 private List<TeamInfo> participants3 = Arrays.asList(partyprof1, partyprof2,
43 partyprof3);
44
45 private final VotingEvaluatorWithValue ve = new LargestAgreementWithValue();
46
47 private DeadlineTime deadline = mock(DeadlineTime.class);
48 private Deadline deadline2 = mock(Deadline.class);
49 private MOPAC2Settings settings1 = new MOPAC2Settings(participants2,
50 deadline, ve);
51 private MOPAC2Settings settings1a = new MOPAC2Settings(participants2,
52 deadline, ve);
53 private MOPAC2Settings settings2 = new MOPAC2Settings(participants3,
54 deadline, ve);
55 private MOPAC2Settings settings3 = new MOPAC2Settings(participants2,
56 deadline2, ve);
57
58 private MOPAC2Settings sersettings;
59 private final String serialized = "{\"MOPAC2Settings\":{\"participants\":[{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party1\",\"parameters\":{}},\"profile\":\"http://profile1\"}]}},{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party2\",\"parameters\":{}},\"profile\":\"http://profile2\"}]}}],\"deadline\":{\"DeadlineTime\":{\"durationms\":100}},\"votingevaluator\":{\"LargestAgreementWithValue\":{}}}}";
60 private List<TeamInfo> participants;
61 private PartyWithProfile partywithprof1, partywithprof2;
62
63 private TeamInfo mockParty(String partyname, Parameters params) {
64 TeamInfo team = mock(TeamInfo.class);
65 when(team.getSize()).thenReturn(1);
66 PartyWithParameters pwithp = mock(PartyWithParameters.class);
67 when(team.getParties()).thenReturn(Arrays
68 .asList(new PartyWithProfile(pwithp, mock(ProfileRef.class))));
69 when(team.toString()).thenReturn(partyname);
70 when(pwithp.toString()).thenReturn(partyname);
71 when(pwithp.getParameters()).thenReturn(params);
72 return team;
73 }
74
75 @Before
76 public void before() throws URISyntaxException {
77
78 when(deadline.getDuration()).thenReturn(1000l);
79 when(deadline.toString()).thenReturn("deadline");
80 when(deadline2.toString()).thenReturn("deadline2");
81
82 // SERIALIZABLE version with REAL objects. Still no workaround for
83 // this...
84 PartyWithParameters party1 = new PartyWithParameters(
85 new PartyRef("http://party1"), new Parameters());
86 ProfileRef profile1 = new ProfileRef("http://profile1");
87 PartyWithParameters party2 = new PartyWithParameters(
88 new PartyRef("http://party2"), new Parameters());
89 ProfileRef profile2 = new ProfileRef("http://profile2");
90 partywithprof1 = new PartyWithProfile(party1, profile1);
91 partywithprof2 = new PartyWithProfile(party2, profile2);
92 participants = Arrays.asList(
93 new TeamInfo(Arrays.asList(partywithprof1)),
94 new TeamInfo(Arrays.asList(partywithprof2)));
95
96 Deadline deadlinetime = new DeadlineTime(100);
97 sersettings = new MOPAC2Settings(participants, deadlinetime, ve);
98
99 }
100
101 @Override
102 public List<List<MOPAC2Settings>> getGeneralTestData() {
103 return Arrays.asList(Arrays.asList(settings1, settings1a),
104 Arrays.asList(settings2), Arrays.asList(settings3));
105 }
106
107 @Override
108 public List<String> getGeneralTestStrings() {
109 return Arrays.asList(
110 "MOPAC2Settings.*party1.*party2.*deadline.*LargestAgreement.*",
111 "MOPAC2Settings.*party1.*party2.*deadline.*LargestAgreement.*",
112 "MOPAC2Settings.*party1.*party2.*deadline2.*LargestAgreement.*");
113 }
114
115 @Test
116 public void getProtocolTest() {
117 assertEquals("MOPAC2", settings1.getProtocol(new ReportToLogger("test"))
118 .getRef().getURI().toString());
119 }
120
121 @Test(expected = IllegalArgumentException.class)
122 public void constructorNoDeadlineTest() {
123 new MOPAC2Settings(participants2, null, ve);
124 }
125
126 @Test
127 public void testMaxRuntime() {
128 when(deadline.getDuration()).thenReturn(234l);
129 assertEquals((Double) .234d, settings1.getMaxRunTime());
130 }
131
132 @Test
133 public void testMaxRuntimeRounds() {
134 deadline = mock(DeadlineRounds.class);
135 when(deadline.getDuration()).thenReturn(12000l);
136
137 MOPAC2Settings settings = new MOPAC2Settings(participants2, deadline,
138 ve);
139
140 assertEquals((Double) 12d, settings.getMaxRunTime());
141 }
142
143 @Test
144 public void testDeserialize() throws IOException {
145 SessionSettings obj = jackson.readValue(serialized,
146 SessionSettings.class);
147 System.out.println(obj);
148 assertEquals(sersettings, obj);
149 }
150
151 @Test
152 public void testSerialize()
153 throws JsonProcessingException, URISyntaxException {
154
155 String string = jackson.writeValueAsString(sersettings);
156 System.out.println(string);
157 assertEquals(serialized, string);
158 }
159
160 @Test
161 public void testWith() {
162 MOPAC2Settings saop = new MOPAC2Settings(participants2, deadline, ve);
163 SessionSettings saop2 = saop.with(partyprof3);
164 assertEquals(3, saop2.getTeams().size());
165 }
166
167 @Test
168 public void testGetAllParties() {
169 assertEquals(Arrays.asList(partywithprof1, partywithprof2),
170 sersettings.getAllParties());
171 }
172
173}
Note: See TracBrowser for help on using the repository browser.