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