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