All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.openxc.remote.RawMeasurementTest Maven / Gradle / Ivy

The newest version!
package com.openxc.remote;

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

import com.openxc.TestUtils;
import com.openxc.measurements.UnrecognizedMeasurementTypeException;
import com.openxc.measurements.serializers.JsonSerializer;

public class RawMeasurementTest extends TestCase {
    RawMeasurement measurement;
    final static String measurementName = "measurement_type";
    final static Double measurementValue = Double.valueOf(42.0);

    public void testValue() {
        measurement = new RawMeasurement(measurementName, measurementValue);
    }

    public void testHasAge() {
        measurement = new RawMeasurement(measurementName, measurementValue);
        assertTrue(measurement.getTimestamp() > 0);
    }

    public void testStopsAging() {
        measurement = new RawMeasurement(measurementName, measurementValue);
        double timestamp = measurement.getTimestamp();
        TestUtils.pause(10);
        assertEquals(timestamp, measurement.getTimestamp(), 0);
    }

    public void testInvalidTimestampGetsTimestampped() {
        measurement = new RawMeasurement(measurementName, measurementValue,
                null, 0);
        assertTrue(measurement.isTimestamped());
        assertFalse(0 == measurement.getTimestamp());
    }

    public void testUntimestamp() {
        measurement = new RawMeasurement(measurementName, measurementValue);
        assertTrue(measurement.isTimestamped());
        measurement.untimestamp();
        assertFalse(measurement.isTimestamped());
    }

    public void testSerializedTimestamp() {
        String serialized = JsonSerializer.serialize(measurementName,
                measurementValue, null, 1332432977835L);
        assertTrue(serialized.contains("1332432977.835"));
    }

    public void testSerializeWithoutTimestamp() {
        measurement = new RawMeasurement(measurementName, measurementValue);
        measurement.untimestamp();
        String serialized = measurement.serialize();
        assertFalse(serialized.contains("timestamp"));
    }

    public void testDeserialize() {
        try {
            measurement = new RawMeasurement(
                    "{\"name\": \"" + measurementName + "\", \"value\": " +
                    measurementValue.toString() + "}");
        } catch(UnrecognizedMeasurementTypeException e) {}
        assertEquals(measurement.getName(), measurementName);
        assertEquals(measurement.getValue(), measurementValue);
    }

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

    public void testDeserializeMissingAttribute() {
        try {
            new RawMeasurement("{\"name\": \"" + measurementName + "\"}");
        } catch(UnrecognizedMeasurementTypeException e) {
            return;
        }
        Assert.fail();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy