1 | package geniusweb.protocol.session;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.net.URISyntaxException;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.junit.Before;
|
---|
13 | import org.junit.Test;
|
---|
14 |
|
---|
15 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
17 |
|
---|
18 | import geniusweb.issuevalue.Bid;
|
---|
19 | import geniusweb.issuevalue.DiscreteValue;
|
---|
20 | import geniusweb.issuevalue.Value;
|
---|
21 | import geniusweb.references.Parameters;
|
---|
22 | import geniusweb.references.PartyRef;
|
---|
23 | import geniusweb.references.PartyWithParameters;
|
---|
24 | import geniusweb.references.PartyWithProfile;
|
---|
25 | import geniusweb.references.ProfileRef;
|
---|
26 | import tudelft.utilities.junit.GeneralTests;
|
---|
27 |
|
---|
28 | public class SessionResultTest extends GeneralTests<SessionResult> {
|
---|
29 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
30 | private final RuntimeException error = new RuntimeException("test");
|
---|
31 |
|
---|
32 | private final String ISSUE1 = "issue1";
|
---|
33 | private SessionResult result1, result1a, result2, result3, result4;
|
---|
34 | private String errorstring; // created dynamically: eclipse and maven
|
---|
35 | // generate different stacktrace.
|
---|
36 | private String jsonstring = "{\"participants\":[{\"party\":{\"partyref\":\"party1\",\"parameters\":{}},\"profile\":\"profile1\"},{\"party\":{\"partyref\":\"party2\",\"parameters\":{}},\"profile\":\"profile2\"}],\"agreement\":{\"issuevalues\":{\"issue1\":\"a\"}},\"penalties\":[0.0,0.0],\"error\":null}";
|
---|
37 | private List<Double> nopenalties = Arrays.asList(0d, 0d);
|
---|
38 | private List<Double> penalties = Arrays.asList(0.1d, 0.2d);
|
---|
39 |
|
---|
40 | @Before
|
---|
41 | public void before() throws URISyntaxException, JsonProcessingException {
|
---|
42 | errorstring = "\"error\":{\"java.lang.RuntimeException\":"
|
---|
43 | + jackson.writeValueAsString(error) + "}";
|
---|
44 | System.out.println(errorstring);
|
---|
45 |
|
---|
46 | PartyWithParameters party1 = new PartyWithParameters(
|
---|
47 | new PartyRef("party1"), new Parameters());
|
---|
48 | PartyWithParameters party2 = new PartyWithParameters(
|
---|
49 | new PartyRef("party2"), new Parameters());
|
---|
50 |
|
---|
51 | PartyWithProfile partyprofile1 = new PartyWithProfile(party1,
|
---|
52 | new ProfileRef("profile1"));
|
---|
53 | PartyWithProfile partyprofile2 = new PartyWithProfile(party2,
|
---|
54 | new ProfileRef("profile2"));
|
---|
55 |
|
---|
56 | Map<String, Value> issuevalues1 = new HashMap<>();
|
---|
57 | issuevalues1.put(ISSUE1, new DiscreteValue("a"));
|
---|
58 | Bid bid1 = new Bid(issuevalues1);
|
---|
59 |
|
---|
60 | // different order but that shouldn't matter
|
---|
61 | Map<String, Value> issuevalues2 = new HashMap<>();
|
---|
62 | issuevalues2.put(ISSUE1, new DiscreteValue("b"));
|
---|
63 | Bid bid2 = new Bid(issuevalues2);
|
---|
64 |
|
---|
65 | result1 = new SessionResult(Arrays.asList(partyprofile1, partyprofile2),
|
---|
66 | bid1, nopenalties, null);
|
---|
67 | result1a = new SessionResult(
|
---|
68 | Arrays.asList(partyprofile1, partyprofile2), bid1, nopenalties,
|
---|
69 | null);
|
---|
70 | result2 = new SessionResult(Arrays.asList(partyprofile1, partyprofile2),
|
---|
71 | bid2, nopenalties, null);
|
---|
72 | result3 = new SessionResult(Arrays.asList(partyprofile2, partyprofile1),
|
---|
73 | bid2, nopenalties, null);
|
---|
74 | result4 = new SessionResult(Arrays.asList(partyprofile2, partyprofile1),
|
---|
75 | bid2, penalties, null);
|
---|
76 |
|
---|
77 | // IGNORE ERROR for now, it fails somewhere deep in maven suddenly.
|
---|
78 | // result4 = new SessionResult(Arrays.asList(partyprofile1,
|
---|
79 | // partyprofile2), bid1, error);
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public List<List<SessionResult>> getGeneralTestData() {
|
---|
84 | return Arrays.asList(Arrays.asList(result1, result1a),
|
---|
85 | Arrays.asList(result2), Arrays.asList(result3),
|
---|
86 | Arrays.asList(result4));
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public List<String> getGeneralTestStrings() {
|
---|
91 | return Arrays.asList(
|
---|
92 | "SessionResult.*party1.*profile1.*,.*party2.*profile2.*Bid.*issue1=\"a\".*0\\.0.*0\\.0.*null.*",
|
---|
93 | "SessionResult.*party1.*profile1.*,.*party2.*profile2.*Bid.*issue1=\"b\".*0\\.0.*0\\.0.*null.*",
|
---|
94 | "SessionResult.*party2.*profile2.*,.*party1.*profile1.*Bid.*issue1=\"b\".*0\\.0.*0\\.0.*null.*",
|
---|
95 | "SessionResult.*party2.*profile2.*,.*party1.*profile1.*Bid.*issue1=\"b\".*0\\.1.*0\\.2.*null.*"
|
---|
96 |
|
---|
97 | );
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Test
|
---|
101 | public void serializeTest() throws JsonProcessingException {
|
---|
102 | System.out.println(jackson.writeValueAsString(result1));
|
---|
103 | assertEquals(jsonstring, jackson.writeValueAsString(result1));
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Test
|
---|
107 | public void deserializeTest() throws IOException {
|
---|
108 | SessionResult act = jackson.readValue(jsonstring, SessionResult.class);
|
---|
109 | assertEquals(result1, act);
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|