[1] | 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.LinkedList;
|
---|
| 9 | import java.util.List;
|
---|
| 10 | import java.util.Map;
|
---|
| 11 |
|
---|
| 12 | import org.junit.Before;
|
---|
| 13 | import org.junit.Test;
|
---|
| 14 |
|
---|
| 15 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 17 |
|
---|
| 18 | import geniusweb.actions.Accept;
|
---|
| 19 | import geniusweb.actions.Action;
|
---|
| 20 | import geniusweb.actions.PartyId;
|
---|
| 21 | import geniusweb.issuevalue.Bid;
|
---|
| 22 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 23 | import geniusweb.issuevalue.NumberValue;
|
---|
| 24 | import geniusweb.issuevalue.Value;
|
---|
| 25 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 26 |
|
---|
| 27 | public class AcceptTest extends GeneralTests<Accept> {
|
---|
| 28 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 29 |
|
---|
| 30 | private static final PartyId id = new PartyId("party1");
|
---|
| 31 | private static final PartyId id2 = new PartyId("party2");
|
---|
| 32 | private final static Map<String, Value> issuevalues = new HashMap<String, Value>();
|
---|
| 33 | private final static Map<String, Value> issuevaluesb = new HashMap<String, Value>();
|
---|
| 34 | private static Bid bid;
|
---|
| 35 | private static Bid bidb;
|
---|
| 36 | private final static String ISSUE1 = "issue1";
|
---|
| 37 | private final static Value VALUE1 = new DiscreteValue("value1");
|
---|
| 38 | private final static String ISSUE2 = "issue2";
|
---|
| 39 | private final static Value VALUE2 = new NumberValue("10");
|
---|
| 40 | // issue 2 is NUMBER and thus serializes with leading '='
|
---|
| 41 | private final String acceptstring = "{\"accept\":{\"actor\":\"party1\",\"bid\":{\"issuevalues\":{\"issue2\":10,\"issue1\":\"value1\"}}}}";
|
---|
| 42 |
|
---|
| 43 | private static Accept accept, accept1, accept2, acceptb;
|
---|
| 44 |
|
---|
| 45 | static {
|
---|
| 46 | issuevalues.put(ISSUE1, VALUE1);
|
---|
| 47 | issuevalues.put(ISSUE2, VALUE2);
|
---|
| 48 | bid = new Bid(issuevalues);
|
---|
| 49 | accept = new Accept(id, bid);
|
---|
| 50 | accept1 = new Accept(id, bid);
|
---|
| 51 |
|
---|
| 52 | accept2 = new Accept(id2, bid);
|
---|
| 53 |
|
---|
| 54 | // values swapped, so different issuevalues.
|
---|
| 55 | issuevaluesb.put(ISSUE1, VALUE2);
|
---|
| 56 | issuevaluesb.put(ISSUE2, VALUE2);
|
---|
| 57 | bidb = new Bid(issuevaluesb);
|
---|
| 58 | acceptb = new Accept(id, bidb);
|
---|
| 59 |
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @Override
|
---|
| 63 | public List<List<Accept>> getGeneralTestData() {
|
---|
| 64 | List<List<Accept>> list = new LinkedList<>();
|
---|
| 65 | list.add(Arrays.asList(accept, accept1));
|
---|
| 66 | list.add(Arrays.asList(accept2));
|
---|
| 67 | list.add(Arrays.asList(acceptb));
|
---|
| 68 | return list;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public List<String> getGeneralTestStrings() {
|
---|
| 73 | return Arrays.asList("Accept.*" + id + ".*]", "Accept.*" + id2 + ".*]",
|
---|
| 74 | "Accept.*" + id + ".*]");
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Before
|
---|
| 78 | public void before() {
|
---|
| 79 |
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Test
|
---|
| 83 | public void serializeAcceptTest() throws JsonProcessingException {
|
---|
| 84 | System.out.println(jackson.writeValueAsString(accept));
|
---|
| 85 | assertEquals(acceptstring, jackson.writeValueAsString(accept));
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | @Test
|
---|
| 89 | public void deserializeAcceptTest() throws IOException {
|
---|
| 90 | Action act = jackson.readValue(acceptstring, Action.class);
|
---|
| 91 | assertEquals(accept, act);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | }
|
---|