package testcode.actions; import java.io.IOException; import org.eclipse.jdt.annotation.NonNull; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; /** * * Bit hacky, similar to junit test but avoiding junit because not yet * supported. */ public class ActionsUse { private static final @NonNull ObjectMapper jackson = new ObjectMapper(); private static final @NonNull PartyId id = new PartyId("party1"); private static final @NonNull PartyId id2 = new PartyId("party2"); private static Bid bid; private static Bid bidb; // issue 2 is NUMBER and thus serializes with leading '=' private final @NonNull String acceptstring = "{\"Accept\":{\"actor\":\"party1\",\"bid\":\"issuevalues\"}}"; private static Accept accept, accept1, accept2, acceptb; // workaround for #118 public ActionsUse() { } public void before() { bid = new Bid("issuevalues"); accept = new Accept(id, bid); accept1 = new Accept(id, bid); accept2 = new Accept(id2, bid); // values swapped, so different issuevalues. bidb = new Bid("issuevaluesb"); acceptb = new Accept(id, bidb); } public void serializeAcceptTest() throws JsonProcessingException { before(); System.out.println("serializeAcceptTest"); System.out.println(jackson.writeValueAsString(accept)); assertEquals(acceptstring, jackson.writeValueAsString(accept).replace(" ", "")); } public void deserializeAcceptTest() throws IOException { before(); System.out.println("deserializeAcceptTest"); final @NonNull Action act = jackson.readValue(acceptstring, Action.class); System.out.println(act); assertEquals(accept, act); System.out.println("All tests OK"); } /** * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { @NonNull ActionsUse a = new ActionsUse(); a.before(); a.serializeAcceptTest(); a = new ActionsUse(); a.before(); a.deserializeAcceptTest(); } private void assertEquals(Object a, Object b) { if (!(a.equals(b))) throw new AssertionError( "Expected " + a.toString() + " , got " + b.toString()); } }