source: protocol/src/test/java/geniusweb/protocol/session/saop/SAOPSettingsTest.java@ 21

Last change on this file since 21 was 21, checked in by bart, 4 years ago

Version 1.5.

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