source: protocol/src/test/java/geniusweb/protocol/session/mopac/MOPACSettingsTest.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.0 KB
Line 
1package geniusweb.protocol.session.mopac;
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.VotingEvaluator;
29import geniusweb.voting.votingevaluators.LargestAgreement;
30import tudelft.utilities.junit.GeneralTests;
31import tudelft.utilities.logging.ReportToLogger;
32
33public class MOPACSettingsTest extends GeneralTests<MOPACSettings> {
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 VotingEvaluator ve = new LargestAgreement();
46
47 private DeadlineTime deadline = mock(DeadlineTime.class);
48 private Deadline deadline2 = mock(Deadline.class);
49 private MOPACSettings settings1 = new MOPACSettings(participants2, deadline,
50 ve);
51 private MOPACSettings settings1a = new MOPACSettings(participants2,
52 deadline, ve);
53 private MOPACSettings settings2 = new MOPACSettings(participants3, deadline,
54 ve);
55 private MOPACSettings settings3 = new MOPACSettings(participants2,
56 deadline2, ve);
57
58 private MOPACSettings sersettings;
59 private final String serialized = "{\"MOPACSettings\":{\"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\":{\"LargestAgreement\":{}}}}";
60 private List<TeamInfo> participants;
61 private PartyWithProfile partywithprof1;
62 private PartyWithProfile partywithprof2;
63
64 private TeamInfo mockParty(String partyname, Parameters params) {
65 TeamInfo team = mock(TeamInfo.class);
66 when(team.getSize()).thenReturn(1);
67 PartyWithParameters pwithp = mock(PartyWithParameters.class);
68 when(team.getParties()).thenReturn(Arrays
69 .asList(new PartyWithProfile(pwithp, mock(ProfileRef.class))));
70 when(team.toString()).thenReturn(partyname);
71 when(pwithp.toString()).thenReturn(partyname);
72 when(pwithp.getParameters()).thenReturn(params);
73 return team;
74 }
75
76 @Before
77 public void before() throws URISyntaxException {
78
79 when(deadline.getDuration()).thenReturn(1000l);
80 when(deadline.toString()).thenReturn("deadline");
81 when(deadline2.toString()).thenReturn("deadline2");
82
83 // SERIALIZABLE version with REAL objects. Still no workaround for
84 // this...
85 PartyWithParameters party1 = new PartyWithParameters(
86 new PartyRef("http://party1"), new Parameters());
87 ProfileRef profile1 = new ProfileRef("http://profile1");
88 PartyWithParameters party2 = new PartyWithParameters(
89 new PartyRef("http://party2"), new Parameters());
90 ProfileRef profile2 = new ProfileRef("http://profile2");
91 partywithprof1 = new PartyWithProfile(party1, profile1);
92 partywithprof2 = new PartyWithProfile(party2, profile2);
93 participants = Arrays.asList(
94 new TeamInfo(Arrays.asList(partywithprof1)),
95 new TeamInfo(Arrays.asList(partywithprof2)));
96
97 Deadline deadlinetime = new DeadlineTime(100);
98 sersettings = new MOPACSettings(participants, deadlinetime, ve);
99
100 }
101
102 @Override
103 public List<List<MOPACSettings>> getGeneralTestData() {
104 return Arrays.asList(Arrays.asList(settings1, settings1a),
105 Arrays.asList(settings2), Arrays.asList(settings3));
106 }
107
108 @Override
109 public List<String> getGeneralTestStrings() {
110 return Arrays.asList(
111 "MOPACSettings.*party1.*party2.*deadline.*LargestAgreement.*",
112 "MOPACSettings.*party1.*party2.*deadline.*LargestAgreement.*",
113 "MOPACSettings.*party1.*party2.*deadline2.*LargestAgreement.*");
114 }
115
116 @Test
117 public void getProtocolTest() {
118 assertEquals("MOPAC", settings1.getProtocol(new ReportToLogger("test"))
119 .getRef().getURI().toString());
120 }
121
122 @Test(expected = IllegalArgumentException.class)
123 public void constructorNoDeadlineTest() {
124 new MOPACSettings(participants2, null, ve);
125 }
126
127 @Test
128 public void testMaxRuntime() {
129 when(deadline.getDuration()).thenReturn(234l);
130 assertEquals((Double) .234d, settings1.getMaxRunTime());
131 }
132
133 @Test
134 public void testMaxRuntimeRounds() {
135 deadline = mock(DeadlineRounds.class);
136 when(deadline.getDuration()).thenReturn(12000l);
137
138 MOPACSettings settings = new MOPACSettings(participants2, deadline, 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 MOPACSettings saop = new MOPACSettings(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.