package com.openxc.sinks;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

import org.json.JSONException;
import org.json.JSONObject;

import com.openxc.remote.RawMeasurement;

import com.openxc.util.FileOpener;

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

public class FileRecorderSinkTest extends AndroidTestCase {
    FileRecorderSink sink;
    FileOpener opener;
    StringWriter outputString;

    String measurementId = "measurement";
    String value = "value";
    String event = "event";

    @Override
    public void setUp() throws IOException, DataSinkException {
        outputString = new StringWriter();
        opener = new MockFileOpener();
        sink = new FileRecorderSink(opener);
    }

    @SmallTest
    public void testReceiveValueOnly() throws DataSinkException {
        assertTrue(outputString.toString().indexOf(measurementId) == -1);
        sink.receive(new RawMeasurement(measurementId, value));
        sink.flush();
        assertTrue(outputString.toString().indexOf(measurementId) != -1);
        assertTrue(outputString.toString().indexOf(value) != -1);
    }

    @SmallTest
    public void testReceiveEvented() throws DataSinkException {
        sink.receive(new RawMeasurement(measurementId, value, event));
        sink.flush();
        assertTrue(outputString.toString().indexOf(measurementId) != -1);
        assertTrue(outputString.toString().indexOf(value) != -1);
        assertTrue(outputString.toString().indexOf(event) != -1);
    }

    @SmallTest
    public void testOutputFormat() throws JSONException, DataSinkException {
        RawMeasurement measurement = new RawMeasurement(measurementId, value);
        measurement.timestamp();
        sink.receive(measurement);
        sink.flush();

        JSONObject message;
        message = new JSONObject(outputString.toString());
        assertTrue(message.getString("name").equals(measurementId));
        assertTrue(message.getString("value").equals(value));
    }

    @SmallTest
    public void testStop() {
        assertTrue(sink.isRunning());
        sink.stop();
        assertFalse(sink.isRunning());
    }

    private class MockFileOpener implements FileOpener {
        public BufferedWriter openForWriting(String path) throws IOException {
            return new BufferedWriter(outputString);
        }
    }
}

@@ -2045,32 +2045,997 @@
 value));%0A    %7D%0A%0A
+    @SmallTest%0A    public void testCounts() throws JSONException, DataSinkException %7B%0A        RawMeasurement measurement = new RawMeasurement(%22first%22, true);%0A        measurement.timestamp();%0A        sink.receive(measurement);%0A%0A        measurement = new RawMeasurement(%22first%22, false);%0A        measurement.timestamp();%0A        sink.receive(measurement);%0A%0A        measurement = new RawMeasurement(%22second%22, true);%0A        measurement.timestamp();%0A        sink.receive(measurement);%0A%0A        measurement = new RawMeasurement(%22second%22, true);%0A        measurement.timestamp();%0A        sink.receive(measurement);%0A%0A        sink.flush();%0A%0A        String%5B%5D records = outputString.toString().split(%22%5Cn%22);%0A        assertEquals(4, records.length);%0A        assertTrue(records%5B0%5D.indexOf(%22first%22) != -1);%0A        assertTrue(records%5B1%5D.indexOf(%22first%22) != -1);%0A        assertTrue(records%5B2%5D.indexOf(%22second%22) != -1);%0A        assertTrue(records%5B3%5D.indexOf(%22second%22) != -1);%0A    %7D%0A%0A
     @SmallTest%0A 

Test ordering and record counts with file recorder.