source: protocol/src/test/java/geniusweb/protocol/session/saop/SAOPSettingsTest.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.2 KB
Line 
1package geniusweb.protocol.session.saop;
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.ProfileRef;
27import tudelft.utilities.junit.GeneralTests;
28import tudelft.utilities.logging.ReportToLogger;
29
30public class SAOPSettingsTest extends GeneralTests<SAOPSettings> {
31
32 private TeamInfo partyprof1 = mock(TeamInfo.class);
33 private TeamInfo partyprof2 = mock(TeamInfo.class);
34 private TeamInfo partyprof3 = mock(TeamInfo.class);
35 private List<TeamInfo> participants2 = Arrays.asList(partyprof1,
36 partyprof2);
37 private List<TeamInfo> participants3 = Arrays.asList(partyprof1, partyprof2,
38 partyprof3);
39
40 private DeadlineTime deadline = mock(DeadlineTime.class);
41 private Deadline deadline2 = mock(Deadline.class);
42 private SAOPSettings settings1 = new SAOPSettings(participants2, deadline);
43 private SAOPSettings settings1a = new SAOPSettings(participants2, deadline);
44 private SAOPSettings settings2 = new SAOPSettings(participants3, deadline);
45 private SAOPSettings settings3 = new SAOPSettings(participants2, deadline2);
46 private final ObjectMapper jackson = new ObjectMapper();
47
48 private SAOPSettings sersettings;
49 private final String serialized = "{\"SAOPSettings\":{\"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}}}}";
50
51 @Before
52 public void before() throws URISyntaxException {
53 when(deadline.getDuration()).thenReturn(1000l);
54 when(deadline.toString()).thenReturn("deadline");
55 when(deadline2.toString()).thenReturn("deadline2");
56
57 when(partyprof1.getSize()).thenReturn(1);
58 when(partyprof2.getSize()).thenReturn(1);
59 when(partyprof3.getSize()).thenReturn(1);
60
61 when(partyprof1.toString()).thenReturn("party and profile 1");
62 when(partyprof2.toString()).thenReturn("party and profile 2");
63 when(partyprof3.toString()).thenReturn("party and profile 3");
64
65 // SERIALIZABLE version with REAL objects. Still no workaround for
66 // this...
67 PartyWithParameters party1 = new PartyWithParameters(
68 new PartyRef("http://party1"), new Parameters());
69 ProfileRef profile1 = new ProfileRef("http://profile1");
70 PartyWithParameters party2 = new PartyWithParameters(
71 new PartyRef("http://party2"), new Parameters());
72 ProfileRef profile2 = new ProfileRef("http://profile2");
73 TeamInfo partywithprof1 = new TeamInfo(party1, profile1);
74 TeamInfo partywithprof2 = new TeamInfo(party2, profile2);
75 List<TeamInfo> participants = Arrays.asList(partywithprof1,
76 partywithprof2);
77
78 Deadline deadlinetime = new DeadlineTime(100);
79 sersettings = new SAOPSettings(participants, deadlinetime);
80
81 }
82
83 @Override
84 public List<List<SAOPSettings>> getGeneralTestData() {
85 return Arrays.asList(Arrays.asList(settings1, settings1a),
86 Arrays.asList(settings2), Arrays.asList(settings3));
87 }
88
89 @Override
90 public List<String> getGeneralTestStrings() {
91 return Arrays.asList(
92 "SAOPSettings.party and profile 1, party and profile 2.,deadline.",
93 "SAOPSettings.party and profile 1, party and profile 2, party and profile 3.,deadline.",
94 "SAOPSettings.party and profile 1, party and profile 2.,deadline2.");
95 }
96
97 @Test
98 public void getProtocolTest() {
99 assertEquals("SAOP", settings1.getProtocol(new ReportToLogger("test"))
100 .getRef().getURI().toString());
101 }
102
103 @Test(expected = IllegalArgumentException.class)
104 public void constructorNoDeadlineTest() {
105 new SAOPSettings(participants2, null);
106 }
107
108 @Test
109 public void testMaxRuntime() {
110 when(deadline.getDuration()).thenReturn(234l);
111 assertEquals((Double) .234d, settings1.getMaxRunTime());
112 }
113
114 @Test
115 public void testMaxRuntimeRounds() {
116 deadline = mock(DeadlineRounds.class);
117 when(deadline.getDuration()).thenReturn(12000l);
118
119 SAOPSettings settings = new SAOPSettings(participants2, deadline);
120
121 assertEquals((Double) 12d, settings.getMaxRunTime());
122 }
123
124 @Test
125 public void testDeserialize() throws IOException {
126 SessionSettings obj = jackson.readValue(serialized,
127 SessionSettings.class);
128 System.out.println(obj);
129 assertEquals(sersettings, obj);
130 }
131
132 @Test
133 public void testSerialize()
134 throws JsonProcessingException, URISyntaxException {
135
136 String string = jackson.writeValueAsString(sersettings);
137 System.out.println(string);
138 assertEquals(serialized, string);
139 }
140
141 @Test
142 public void testWith() {
143 SAOPSettings saop = new SAOPSettings(participants2, deadline);
144 SessionSettings saop2 = saop.with(partyprof3);
145 assertEquals(3, saop2.getTeams().size());
146 }
147
148}
Note: See TracBrowser for help on using the repository browser.