package com.openxc.messages.formatters;

import junit.framework.Assert;
import junit.framework.TestCase;

import com.openxc.measurements.UnrecognizedMeasurementTypeException;
import com.openxc.messages.SimpleVehicleMessage;

public class JsonFormatterTest extends TestCase {
    JsonFormatter formatter = new JsonFormatter();
    SimpleVehicleMessage message;
    String messageName = "foo";
    Integer value = Integer.valueOf(42);

    public void testSerializeWithoutTimestamp() {
        message = new SimpleVehicleMessage(messageName, value);
        message.untimestamp();
        String serialized = new String(formatter.serialize(message));
        assertFalse(serialized.contains("timestamp"));
    }

    public void testDeserialize() {
        try {
            message = (SimpleVehicleMessage) formatter.deserialize(
                    "{\"name\": \"" + messageName + "\", \"value\": " +
                    value.toString() + "}");
        } catch(UnrecognizedMeasurementTypeException e) {}
        assertEquals(message.getName(), messageName);
        assertEquals(message.getValue(), value);
    }

    public void testDeserializeInvalidJson() {
        try {
            formatter.deserialize("{\"name\":");
        } catch(UnrecognizedMeasurementTypeException e) {
            return;
        }
        Assert.fail();
    }

    public void testDeserializeMissingAttribute() {
        try {
            formatter.deserialize("{\"name\": \"" + messageName + "\"}");
        } catch(UnrecognizedMeasurementTypeException e) {
            return;
        }
        Assert.fail();
    }

    public void testSerializedTimestamp() {
        String serialized = new String(formatter.serialize(
                    new SimpleVehicleMessage(
                        Long.valueOf(1332432977835L), messageName, value)));
        assertTrue(serialized.contains("1332432977.835"));
    }
}

@@ -389,23 +389,22 @@
 o%22;%0A    
-Integer
+Double
  value =
@@ -408,15 +408,14 @@
 e = 
-Integer
+Double
 .val
@@ -1338,266 +1338,8 @@
  %7D%0A%0A
-    public void testDeserializeMissingAttribute() %7B%0A        try %7B%0A            formatter.deserialize(%22%7B%5C%22name%5C%22: %5C%22%22 + messageName + %22%5C%22%7D%22);%0A        %7D catch(UnrecognizedMeasurementTypeException e) %7B%0A            return;%0A        %7D%0A        Assert.fail();%0A    %7D%0A%0A
     

Update JSON deserialization tests.