source: events/src/test/java/geniusweb/inform/SettingsTest.java@ 53

Last change on this file since 53 was 52, checked in by ruud, 21 months ago

Fixed small issues in domaineditor.

File size: 4.2 KB
Line 
1package geniusweb.inform;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.net.URI;
7import java.net.URISyntaxException;
8import java.util.Arrays;
9import java.util.Date;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13
14import org.junit.Before;
15import org.junit.Test;
16
17import com.fasterxml.jackson.core.JsonParseException;
18import com.fasterxml.jackson.core.JsonProcessingException;
19import com.fasterxml.jackson.databind.JsonMappingException;
20import com.fasterxml.jackson.databind.ObjectMapper;
21
22import geniusweb.actions.PartyId;
23import geniusweb.progress.ProgressRounds;
24import geniusweb.references.Parameters;
25import geniusweb.references.PartyRef;
26import geniusweb.references.ProfileRef;
27import geniusweb.references.ProtocolRef;
28import tudelft.utilities.junit.GeneralTests;
29
30public class SettingsTest extends GeneralTests<Settings> {
31
32 private PartyId id = new PartyId("party1");
33 private PartyId id2 = new PartyId("party2");
34
35 private final String asJson = "{\"Settings\":"
36 + "{\"id\":\"party1\",\"profile\":\"ws:profile1\","
37 + "\"protocol\":\"ws:localhost/protocol1\","
38 + "\"progress\":{\"ProgressRounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":999}},"
39 + "\"parameters\":{}}}";
40
41 private Settings negoinfo1, negoinfo1a;
42 private Settings negoinfo2, negoinfo3, negoinfo4, negoinfo5;
43 private final Date date = new Date(999);
44 private final static ObjectMapper jackson = new ObjectMapper();
45
46 @Before
47 public void before() throws URISyntaxException, JsonParseException,
48 JsonMappingException, IOException {
49 // we can't mock because we want to test the serializer
50 ProfileRef profile1 = new ProfileRef(new URI("ws:profile1"));
51 // jackson.readValue(serialized, Profile.class);
52
53 ProtocolRef protocol1 = new ProtocolRef(
54 new URI("ws:localhost/protocol1"));
55 ProtocolRef protocol1a = new ProtocolRef(
56 new URI("ws:localhost/protocol1"));
57 ProtocolRef protocol2 = new ProtocolRef(
58 new URI("ws:localhost/protocol2"));
59 ProgressRounds progress1 = new ProgressRounds(10, 0, date);
60 ProgressRounds progress1a = new ProgressRounds(10, 0, date);
61 ProgressRounds progress2 = new ProgressRounds(12, 0, date);
62
63 Parameters settings1 = new Parameters();
64 Parameters settings2 = new Parameters().with("a", 1);
65
66 negoinfo1 = new Settings(id, profile1, protocol1, progress1, settings1);
67 negoinfo1a = new Settings(id, profile1, protocol1a, progress1a,
68 settings1);
69 negoinfo2 = new Settings(id, profile1, protocol2, progress1, settings1);
70 negoinfo3 = new Settings(id, profile1, protocol1, progress2, settings1);
71 negoinfo4 = new Settings(id2, profile1, protocol1, progress2,
72 settings1);
73 negoinfo5 = new Settings(id, profile1, protocol1, progress1, settings2);
74 }
75
76 @Override
77 public List<List<Settings>> getGeneralTestData() {
78 return Arrays.asList(Arrays.asList(negoinfo1, negoinfo1a),
79 Arrays.asList(negoinfo2), Arrays.asList(negoinfo3),
80 Arrays.asList(negoinfo4), Arrays.asList(negoinfo5));
81 }
82
83 @Override
84 public List<String> getGeneralTestStrings() {
85 return Arrays.asList(
86 "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*10.*",
87 "Settings.*party1.*ProtocolRef.*protocol2.*ProgressRounds.*0.*10.*",
88 "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*12.*",
89 "Settings.*party2.*ProtocolRef.*protocol1.*ProgressRounds.*0.*12.*",
90 "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*10.*");
91 }
92
93 @Test
94 public void smokeTest() throws URISyntaxException {
95 new PartyRef(new URI("ws:localhost"));
96 }
97
98 @Test(expected = IllegalArgumentException.class)
99 public void nullTest() throws URISyntaxException {
100 new PartyRef((URI) null);
101 }
102
103 @Test
104 public void testSerialize() throws JsonProcessingException {
105
106 String json = jackson.writeValueAsString(negoinfo1);
107 System.out.println(json);
108 assertEquals(asJson, json);
109 }
110
111 @Test
112 public void testDeserialize()
113 throws JsonParseException, JsonMappingException, IOException {
114 Settings p = (Settings) jackson.readValue(asJson, Inform.class);
115 System.out.println(p);
116 assertEquals(negoinfo1, p);
117 }
118
119 @Test
120 public void testGeneralDict()
121 throws JsonParseException, JsonMappingException, IOException {
122 Map val = jackson.readValue("{\"a\":0.3,\"b\":{\"x\":3},\"c\":[1,2,3]}",
123 HashMap.class);
124 System.out.println(val);
125 }
126
127}
Note: See TracBrowser for help on using the repository browser.