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