1 | package geniusweb.inform;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.net.URI;
|
---|
7 | import java.net.URISyntaxException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Date;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import org.junit.Before;
|
---|
15 | import org.junit.Test;
|
---|
16 |
|
---|
17 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
18 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
19 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
20 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
21 |
|
---|
22 | import geniusweb.actions.PartyId;
|
---|
23 | import geniusweb.progress.ProgressRounds;
|
---|
24 | import geniusweb.references.Parameters;
|
---|
25 | import geniusweb.references.PartyRef;
|
---|
26 | import geniusweb.references.ProfileRef;
|
---|
27 | import geniusweb.references.ProtocolRef;
|
---|
28 | import tudelft.utilities.junit.GeneralTests;
|
---|
29 |
|
---|
30 | public 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 | }
|
---|