source: protocol/src/test/java/geniusweb/protocol/session/SessionResultTest.java@ 21

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

Version 1.5.

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