import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonStreamParser;
import org.jboss.summit2015.beacon.Beacon;
import org.junit.Test;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Scott Stark (sstark@redhat.com) (C) 2014 Red Hat Inc.
 */
public class TestBeaconSerialization {
   @Test
   public void testReadBeacon() throws IOException {
      FileInputStream fis = new FileInputStream("/tmp/testSerializeBeacon.ser");
      DataInputStream dis = new DataInputStream(fis);
      byte[] data = new byte[fis.available()];
      dis.readFully(data);
      dis.close();

      Beacon beacon = Beacon.fromByteMsg(data);
      System.out.printf("Read beacon: %s\n", beacon);
   }


   @Test
   public void testWriteJson() throws Exception {
      // (String scannerID, String uuid, int code, int manufacturer, int major, int minor, int power, int rssi)
      String scannerID = "testWriteJson";
      String uuid = "DAF246CE836311E4";
      int code = 533;
      int manufacturer = 19456;
      int major = 45334;
      int minor = 4667;
      int power = 0;
      int rssi = -79;
      long time = System.currentTimeMillis();
      Beacon beacon = new Beacon(scannerID, uuid, code, manufacturer, major, minor, power, rssi, time);
      String jsonOutput = beacon.toJSON();
      FileWriter fw = new FileWriter("/tmp/testWriteJson.json");
      fw.write(jsonOutput);
      beacon.setMajor(beacon.getMajor()+1);
      jsonOutput = beacon.toJSON();
      fw.write(jsonOutput);
      fw.close();
   }
   @Test
   public void testReadJson() throws Exception {
      FileReader fr = new FileReader("/tmp/testWriteJson.json");
      Gson gson = new Gson();
      JsonStreamParser parser = new JsonStreamParser(fr);
      while(parser.hasNext()) {
         JsonElement jse = parser.next();
         Beacon beacon = gson.fromJson(jse, Beacon.class);
         System.out.printf("Beacon from json:%s\n", beacon);
      }
   }
}

@@ -58,16 +58,54 @@
 lement;%0A
+import com.google.gson.JsonPrimitive;%0A
 import c
@@ -2096,11 +2096,979 @@
   %7D%0A   %7D
+%0A%0A   @Test%0A   public void testGetMessageTypeTime() %7B%0A      Gson gson = new Gson();%0A      String json = %22%7B%5Cn%22 +%0A         %22  %5C%22scannerID%5C%22: %5C%22Room203%5C%22,%5Cn%22 +%0A         %22  %5C%22uuid%5C%22: %5C%22DAF246CEF20311E4B116123B93F75CBA%5C%22,%5Cn%22 +%0A         %22  %5C%22code%5C%22: 533,%5Cn%22 +%0A         %22  %5C%22manufacturer%5C%22: 19456,%5Cn%22 +%0A         %22  %5C%22major%5C%22: 203,%5Cn%22 +%0A         %22  %5C%22minor%5C%22: 20,%5Cn%22 +%0A         %22  %5C%22power%5C%22: -62,%5Cn%22 +%0A         %22  %5C%22calibratedPower%5C%22: -62,%5Cn%22 +%0A         %22  %5C%22rssi%5C%22: -67,%5Cn%22 +%0A         %22  %5C%22messageType%5C%22: 1,%5Cn%22 +%0A         %22  %5C%22time%5C%22: 1426016481227%5Cn%22 +%0A         %22%7D%22;%0A      JsonStreamParser parser = new JsonStreamParser(json);%0A      JsonElement jse = parser.next();%0A      JsonPrimitive messageType = jse.getAsJsonObject().getAsJsonPrimitive(%22messageType%22);%0A      System.out.printf(%22jse.messageType = %25d%5Cn%22, messageType.getAsInt());%0A      JsonPrimitive time = jse.getAsJsonObject().getAsJsonPrimitive(%22time%22);%0A      System.out.printf(%22jse.time = %25d%5Cn%22, time.getAsLong());%0A   %7D
 %0A%7D%0A

Test JsonObject accessors