source: protocol/src/test/java/geniusweb/protocol/session/amop/AMOPSettingsTest.java@ 22

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

Minor fixes

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