1 | package actions;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import org.junit.Test;
|
---|
12 |
|
---|
13 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
14 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
15 |
|
---|
16 | import geniusweb.actions.Action;
|
---|
17 | import geniusweb.actions.Offer;
|
---|
18 | import geniusweb.actions.PartyId;
|
---|
19 | import geniusweb.issuevalue.Bid;
|
---|
20 | import geniusweb.issuevalue.DiscreteValue;
|
---|
21 | import geniusweb.issuevalue.NumberValue;
|
---|
22 | import geniusweb.issuevalue.Value;
|
---|
23 | import tudelft.utilities.junit.GeneralTests;
|
---|
24 |
|
---|
25 | public class OfferTest extends GeneralTests<Offer> {
|
---|
26 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
27 |
|
---|
28 | private static final PartyId id = new PartyId("party1");
|
---|
29 | private static final Map<String, Value> issuevalues = new HashMap<String, Value>();
|
---|
30 | private static final Map<String, Value> issuevaluesb = new HashMap<String, Value>();
|
---|
31 | private static Bid bid;
|
---|
32 | private static Bid bidb;
|
---|
33 | private static final String ISSUE1 = "issue1";
|
---|
34 | private static final Value VALUE1 = new DiscreteValue("value1");
|
---|
35 | private static final String ISSUE2 = "issue2";
|
---|
36 | private static final Value VALUE2 = new NumberValue("10");
|
---|
37 | private static final String acceptstring = "{\"offer\":{\"actor\":\"party1\",\"bid\":{\"issuevalues\":{\"issue2\":10,\"issue1\":\"value1\"}}}}";
|
---|
38 |
|
---|
39 | private static Offer offer;
|
---|
40 | private static Offer offer1;
|
---|
41 | private static Offer offerb;
|
---|
42 |
|
---|
43 | static {
|
---|
44 | issuevalues.put(ISSUE1, VALUE1);
|
---|
45 | issuevalues.put(ISSUE2, VALUE2);
|
---|
46 | bid = new Bid(issuevalues);
|
---|
47 | offer = new Offer(id, bid);
|
---|
48 | offer1 = new Offer(id, bid);
|
---|
49 |
|
---|
50 | // values swapped, so different issuevalues.
|
---|
51 | issuevaluesb.put(ISSUE1, VALUE2);
|
---|
52 | issuevaluesb.put(ISSUE2, VALUE2);
|
---|
53 | bidb = new Bid(issuevaluesb);
|
---|
54 | offerb = new Offer(id, bidb);
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public List<List<Offer>> getGeneralTestData() {
|
---|
60 | return Arrays.asList(Arrays.asList(offer, offer1),
|
---|
61 | Arrays.asList(offerb));
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public List<String> getGeneralTestStrings() {
|
---|
66 | return Arrays.asList("Offer\\[.*" + id + ".*Bid.*\\]",
|
---|
67 | "Offer\\[.*" + id + ".*Bid.*\\]");
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Test
|
---|
71 | public void serializeAcceptTest() throws JsonProcessingException {
|
---|
72 | System.out.println(jackson.writeValueAsString(offer));
|
---|
73 | assertEquals(acceptstring, jackson.writeValueAsString(offer));
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Test
|
---|
77 | public void deserializeAcceptTest() throws IOException {
|
---|
78 | Action act = jackson.readValue(acceptstring, Action.class);
|
---|
79 | assertEquals(offer, act);
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|