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

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

some minor fixes

File size: 5.0 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\":{\"party2\":{\"party\":{\"partyref\":\"party2\",\"parameters\":{}},\"profile\":\"profile2\"},\"party1\":{\"party\":{\"partyref\":\"party1\",\"parameters\":{}},\"profile\":\"profile1\"}},\"agreements\":{\"party2\":{\"issuevalues\":{\"issue1\":\"a\"}},\"party1\":{\"issuevalues\":{\"issue1\":\"a\"}}},\"penalties\":{\"party2\":0.0,\"party1\":0.0},\"error\":null}";
38 private Map<PartyId, Double> nopenalties = new HashMap<>();
39 private Map<PartyId, Double> penalties = new HashMap<>();
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 penalties.put(PARTY1, 0.1d);
48 penalties.put(PARTY2, 0.2d);
49 nopenalties.put(PARTY1, 0d);
50 nopenalties.put(PARTY2, 0d);
51
52 String errorstring = "\"error\":{\"java.lang.RuntimeException\":"
53 + jackson.writeValueAsString(error) + "}";
54 // System.out.println(errorstring);
55
56 PartyWithParameters party1 = new PartyWithParameters(
57 new PartyRef("party1"), new Parameters());
58 PartyWithParameters party2 = new PartyWithParameters(
59 new PartyRef("party2"), new Parameters());
60
61 PartyWithProfile partyprofile1 = new PartyWithProfile(party1,
62 new ProfileRef("profile1"));
63 PartyWithProfile partyprofile2 = new PartyWithProfile(party2,
64 new ProfileRef("profile2"));
65
66 Map<String, Value> issuevalues1 = new HashMap<>();
67 issuevalues1.put(ISSUE1, new DiscreteValue("a"));
68 Bid bid1 = new Bid(issuevalues1);
69 Agreements agreement1 = new Agreements().with(new Agreements(bid1,
70 new HashSet<>(Arrays.asList(PARTY1, PARTY2))));
71
72 // different order but that shouldn't matter
73 Map<String, Value> issuevalues2 = new HashMap<>();
74 issuevalues2.put(ISSUE1, new DiscreteValue("b"));
75 Bid bid2 = new Bid(issuevalues2);
76 Agreements agreement2 = new Agreements().with(new Agreements(bid2,
77 new HashSet<>(Arrays.asList(PARTY1, PARTY3))));
78
79 Map<PartyId, PartyWithProfile> partiesmap = new HashMap<>();
80 partiesmap.put(PARTY1, partyprofile1);
81 partiesmap.put(PARTY2, partyprofile2);
82
83 Map<PartyId, PartyWithProfile> partiesmap2 = new HashMap<>();
84 partiesmap2.put(PARTY1, partyprofile2);
85 partiesmap2.put(PARTY3, partyprofile1);
86
87 result1 = new SessionResult(partiesmap, agreement1, nopenalties, null);
88 result1a = new SessionResult(partiesmap, agreement1, nopenalties, null);
89 result2 = new SessionResult(partiesmap, agreement2, nopenalties, null);
90 result3 = new SessionResult(partiesmap2, agreement1, nopenalties, null);
91 result4 = new SessionResult(partiesmap2, agreement1, penalties, null);
92
93 // IGNORE ERROR for now, it fails somewhere deep in maven suddenly.
94 // result4 = new SessionResult(Arrays.asList(partyprofile1,
95 // partyprofile2), bid1, error);
96 }
97
98 @Override
99 public List<List<SessionResult>> getGeneralTestData() {
100 return Arrays.asList(Arrays.asList(result1, result1a),
101 Arrays.asList(result2), Arrays.asList(result3),
102 Arrays.asList(result4));
103 }
104
105 @Override
106 public List<String> getGeneralTestStrings() {
107 return Arrays.asList(
108 "SessionResult.*party2.*profile2.*,.*party1.*profile1.*Agreements.*Bid.*issue1=\"a\".*0\\.0.*0\\.0.*null.*",
109 "SessionResult.*party2.*profile2.*,.*party1.*profile1.*Agreements.*Bid.*issue1=\"b\".*0\\.0.*0\\.0.*null.*",
110 "SessionResult.*party1.*profile2.*,.*party3.*profile1.*Agreements.*Bid.*issue1=\"a\".*0\\.0.*0\\.0.*null.*",
111 "SessionResult.*party1.*profile2.*,.*party3.*profile1.*Agreements.*Bid.*issue1=\"a\".*party2.*0\\.2.*party1.*0\\.1.*null.*"
112
113 );
114 }
115
116 @Test
117 public void serializeTest() throws JsonProcessingException {
118 System.out.println(jackson.writeValueAsString(result1));
119 assertEquals(jsonstring, jackson.writeValueAsString(result1));
120 }
121
122 @Test
123 public void deserializeTest() throws IOException {
124 SessionResult act = jackson.readValue(jsonstring, SessionResult.class);
125 assertEquals(result1, act);
126 }
127
128}
Note: See TracBrowser for help on using the repository browser.