[505] | 1 | package testcode.actions;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 |
|
---|
[1116] | 5 | import org.eclipse.jdt.annotation.NonNull;
|
---|
| 6 |
|
---|
[505] | 7 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 8 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | *
|
---|
| 12 | * Bit hacky, similar to junit test but avoiding junit because not yet
|
---|
| 13 | * supported.
|
---|
| 14 | */
|
---|
| 15 | public class ActionsUse {
|
---|
[1116] | 16 | private static final @NonNull ObjectMapper jackson = new ObjectMapper();
|
---|
[505] | 17 |
|
---|
[1116] | 18 | private static final @NonNull PartyId id = new PartyId("party1");
|
---|
| 19 | private static final @NonNull PartyId id2 = new PartyId("party2");
|
---|
[505] | 20 | private static Bid bid;
|
---|
| 21 | private static Bid bidb;
|
---|
| 22 | // issue 2 is NUMBER and thus serializes with leading '='
|
---|
[1116] | 23 | private final @NonNull String acceptstring = "{\"Accept\":{\"actor\":\"party1\",\"bid\":\"issuevalues\"}}";
|
---|
[505] | 24 |
|
---|
| 25 | private static Accept accept, accept1, accept2, acceptb;
|
---|
| 26 |
|
---|
| 27 | // workaround for #118
|
---|
| 28 | public ActionsUse() {
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public void before() {
|
---|
| 32 | bid = new Bid("issuevalues");
|
---|
| 33 | accept = new Accept(id, bid);
|
---|
| 34 | accept1 = new Accept(id, bid);
|
---|
| 35 |
|
---|
| 36 | accept2 = new Accept(id2, bid);
|
---|
| 37 |
|
---|
| 38 | // values swapped, so different issuevalues.
|
---|
| 39 | bidb = new Bid("issuevaluesb");
|
---|
| 40 | acceptb = new Accept(id, bidb);
|
---|
| 41 |
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public void serializeAcceptTest() throws JsonProcessingException {
|
---|
| 45 | before();
|
---|
| 46 | System.out.println("serializeAcceptTest");
|
---|
| 47 | System.out.println(jackson.writeValueAsString(accept));
|
---|
| 48 | assertEquals(acceptstring,
|
---|
| 49 | jackson.writeValueAsString(accept).replace(" ", ""));
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public void deserializeAcceptTest() throws IOException {
|
---|
| 53 | before();
|
---|
| 54 | System.out.println("deserializeAcceptTest");
|
---|
[1116] | 55 | final @NonNull Action act = jackson.readValue(acceptstring,
|
---|
| 56 | Action.class);
|
---|
[505] | 57 | System.out.println(act);
|
---|
| 58 | assertEquals(accept, act);
|
---|
| 59 | System.out.println("All tests OK");
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | *
|
---|
| 64 | * @param args
|
---|
| 65 | * @throws IOException
|
---|
| 66 | */
|
---|
| 67 | public static void main(String[] args) throws IOException {
|
---|
[1116] | 68 | @NonNull
|
---|
[505] | 69 | ActionsUse a = new ActionsUse();
|
---|
| 70 | a.before();
|
---|
| 71 | a.serializeAcceptTest();
|
---|
| 72 | a = new ActionsUse();
|
---|
| 73 | a.before();
|
---|
| 74 | a.deserializeAcceptTest();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private void assertEquals(Object a, Object b) {
|
---|
| 78 | if (!(a.equals(b)))
|
---|
| 79 | throw new AssertionError(
|
---|
| 80 | "Expected " + a.toString() + " , got " + b.toString());
|
---|
| 81 | }
|
---|
| 82 | }
|
---|