1 | package geniusweb.logconverter;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.math.BigDecimal;
|
---|
7 | import java.net.URISyntaxException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.HashSet;
|
---|
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.JsonProcessingException;
|
---|
18 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
19 |
|
---|
20 | import geniusweb.actions.PartyId;
|
---|
21 | import geniusweb.inform.Agreements;
|
---|
22 | import geniusweb.issuevalue.Bid;
|
---|
23 | import geniusweb.issuevalue.DiscreteValue;
|
---|
24 | import geniusweb.issuevalue.Value;
|
---|
25 | import geniusweb.protocol.session.SessionResult;
|
---|
26 | import geniusweb.references.Parameters;
|
---|
27 | import geniusweb.references.PartyRef;
|
---|
28 | import geniusweb.references.PartyWithParameters;
|
---|
29 | import geniusweb.references.PartyWithProfile;
|
---|
30 | import geniusweb.references.ProfileRef;
|
---|
31 | import tudelft.utilities.junit.GeneralTests;
|
---|
32 |
|
---|
33 | public class SessionResultWithUtilsTest
|
---|
34 | extends GeneralTests<SessionResultWithUtils> {
|
---|
35 |
|
---|
36 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
37 | private final RuntimeException error = new RuntimeException("test");
|
---|
38 |
|
---|
39 | private final String ISSUE1 = "salary";
|
---|
40 | private final String ISSUE2 = "fte";
|
---|
41 | private String jsonstring = "{\"participants\":{\"party2\":{\"party\":{\"partyref\":\"party2\",\"parameters\":{}},\"profile\":\"file:src/test/resources/profile2.json\"},\"party1\":{\"party\":{\"partyref\":\"party1\",\"parameters\":{}},\"profile\":\"file:src/test/resources/profile1.json\"}},\"agreements\":{\"party2\":{\"issuevalues\":{\"salary\":\"3500\",\"fte\":\"0.8\"}},\"party1\":{\"issuevalues\":{\"salary\":\"3500\",\"fte\":\"0.8\"}}},\"penalties\":{\"party2\":0.002,\"party1\":0.001},\"error\":null,\"utilities\":{\"party2\":0.7,\"party1\":0.8,\"party3\":0.4}}";
|
---|
42 | private Map<PartyId, Double> penalties = new HashMap<>();
|
---|
43 |
|
---|
44 | private PartyId PARTY1 = new PartyId("party1");
|
---|
45 | private PartyId PARTY2 = new PartyId("party2");
|
---|
46 | private PartyId PARTY3 = new PartyId("party3");
|
---|
47 |
|
---|
48 | private SessionResult result1, result1a, result2, result3;
|
---|
49 | private SessionResultWithUtils reswithutil1, reswithutil1a, reswithutil2;
|
---|
50 |
|
---|
51 | @Before
|
---|
52 | public void before() throws URISyntaxException, JsonProcessingException {
|
---|
53 | penalties.put(PARTY1, 0.001d);
|
---|
54 | penalties.put(PARTY2, 0.002d);
|
---|
55 |
|
---|
56 | String errorstring = "\"error\":{\"java.lang.RuntimeException\":"
|
---|
57 | + jackson.writeValueAsString(error) + "}";
|
---|
58 | // System.out.println(errorstring);
|
---|
59 |
|
---|
60 | PartyWithParameters party1 = new PartyWithParameters(
|
---|
61 | new PartyRef("party1"), new Parameters());
|
---|
62 | PartyWithParameters party2 = new PartyWithParameters(
|
---|
63 | new PartyRef("party2"), new Parameters());
|
---|
64 |
|
---|
65 | PartyWithProfile partyprofile1 = new PartyWithProfile(party1,
|
---|
66 | new ProfileRef("file:src/test/resources/profile1.json"));
|
---|
67 | PartyWithProfile partyprofile2 = new PartyWithProfile(party2,
|
---|
68 | new ProfileRef("file:src/test/resources/profile2.json"));
|
---|
69 |
|
---|
70 | Map<String, Value> issuevalues1 = new HashMap<>();
|
---|
71 | issuevalues1.put(ISSUE1, new DiscreteValue("3500"));
|
---|
72 | issuevalues1.put(ISSUE2, new DiscreteValue("0.8"));
|
---|
73 | Bid bid1 = new Bid(issuevalues1);
|
---|
74 | Agreements agreement1 = new Agreements().with(new Agreements(bid1,
|
---|
75 | new HashSet<>(Arrays.asList(PARTY1, PARTY2))));
|
---|
76 |
|
---|
77 | // different order but that shouldn't matter
|
---|
78 | Map<String, Value> issuevalues2 = new HashMap<>();
|
---|
79 | issuevalues2.put(ISSUE1, new DiscreteValue("3500"));
|
---|
80 | issuevalues2.put(ISSUE2, new DiscreteValue("0.8"));
|
---|
81 | Bid bid2 = new Bid(issuevalues2);
|
---|
82 | Agreements agreement2 = new Agreements().with(new Agreements(bid2,
|
---|
83 | new HashSet<>(Arrays.asList(PARTY1, PARTY3))));
|
---|
84 |
|
---|
85 | Map<PartyId, PartyWithProfile> partiesmap = new HashMap<>();
|
---|
86 | partiesmap.put(PARTY1, partyprofile1);
|
---|
87 | partiesmap.put(PARTY2, partyprofile2);
|
---|
88 |
|
---|
89 | Map<PartyId, PartyWithProfile> partiesmap2 = new HashMap<>();
|
---|
90 | partiesmap2.put(PARTY1, partyprofile2);
|
---|
91 | partiesmap2.put(PARTY3, partyprofile1);
|
---|
92 |
|
---|
93 | result1 = new SessionResult(partiesmap, agreement1, penalties, null);
|
---|
94 | result2 = new SessionResult(partiesmap, agreement2, penalties, null);
|
---|
95 | result3 = new SessionResult(partiesmap, new Agreements(), penalties,
|
---|
96 | null);
|
---|
97 |
|
---|
98 | HashMap<PartyId, BigDecimal> utils1 = new HashMap<>();
|
---|
99 | utils1.put(PARTY1, new BigDecimal("0.8"));
|
---|
100 | utils1.put(PARTY2, new BigDecimal("0.7"));
|
---|
101 | utils1.put(PARTY3, new BigDecimal("0.4"));
|
---|
102 | HashMap<PartyId, BigDecimal> utils2 = new HashMap<>();
|
---|
103 | utils2.put(PARTY1, new BigDecimal("0.8"));
|
---|
104 | utils2.put(PARTY2, new BigDecimal("0.9"));
|
---|
105 | utils2.put(PARTY3, new BigDecimal("0.4"));
|
---|
106 |
|
---|
107 | reswithutil1 = new SessionResultWithUtils(partiesmap, agreement1,
|
---|
108 | penalties, null, utils1);
|
---|
109 | reswithutil1a = new SessionResultWithUtils(partiesmap, agreement1,
|
---|
110 | penalties, null, utils1);
|
---|
111 | reswithutil2 = new SessionResultWithUtils(partiesmap, agreement1,
|
---|
112 | penalties, null, utils2);
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public List<List<SessionResultWithUtils>> getGeneralTestData() {
|
---|
118 | return Arrays.asList(Arrays.asList(reswithutil1, reswithutil1a),
|
---|
119 | Arrays.asList(reswithutil2));
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public List<String> getGeneralTestStrings() {
|
---|
124 | return Arrays.asList(
|
---|
125 | "SessionResultWithUtils.*party2=0.7, party1=0.8, party3=0.4.*",
|
---|
126 | "SessionResultWithUtils.*party2=0.9, party1=0.8, party3=0.4.*");
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Test
|
---|
130 | public void serializeTest() throws JsonProcessingException {
|
---|
131 | System.out.println(jackson.writeValueAsString(reswithutil1));
|
---|
132 | assertEquals(jsonstring, jackson.writeValueAsString(reswithutil1));
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test
|
---|
136 | public void deserializeTest() throws IOException {
|
---|
137 | SessionResultWithUtils act = jackson.readValue(jsonstring,
|
---|
138 | SessionResultWithUtils.class);
|
---|
139 | assertEquals(reswithutil1, act);
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Test
|
---|
143 | public void createTest() {
|
---|
144 | TestReporter reporter = new TestReporter();
|
---|
145 | SessionResultWithUtils res = SessionResultWithUtils.create(result1,
|
---|
146 | reporter);
|
---|
147 | System.out.println(res);
|
---|
148 | assertEquals(null, reporter.getError());
|
---|
149 | assertEquals(new BigDecimal("0.5590"), // 0.56 - 0.001
|
---|
150 | res.getUtilities().get(PARTY1));
|
---|
151 | assertEquals(new BigDecimal("0.448"), // 0.45 - 0.002
|
---|
152 | res.getUtilities().get(PARTY2));
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Test
|
---|
157 | public void createNoAgreementTest() {
|
---|
158 | TestReporter reporter = new TestReporter();
|
---|
159 | SessionResultWithUtils res = SessionResultWithUtils.create(result3,
|
---|
160 | reporter);
|
---|
161 | System.out.println(res);
|
---|
162 | assertEquals(null, reporter.getError());
|
---|
163 | assertEquals(new BigDecimal("0.5600"), // "salary": "3500","fte": "0.8"
|
---|
164 | res.getUtilities().get(PARTY1));
|
---|
165 | assertEquals(new BigDecimal("0.50"), // "salary": "3000", "fte": "0.8"
|
---|
166 | res.getUtilities().get(PARTY2));
|
---|
167 |
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Test
|
---|
171 | public void blaTest() {
|
---|
172 | // and if there is no resbid, is util=0?
|
---|
173 |
|
---|
174 | }
|
---|
175 |
|
---|
176 | }
|
---|