package com.linkedin.camus.etl.kafka.coders;

import com.linkedin.camus.coders.CamusWrapper;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

import java.util.Properties;

public class TestJsonStringMessageDecoder {

    @Test
    public void testDecodeUnixMilliseconds() {

        // Test that the decoder extracts unix_milliseconds
        // It should take and return milliseconds

        long testTimestamp = 1406947271534L;
        long expectedTimestamp = testTimestamp;
        
        Properties testProperties = new Properties();
        testProperties.setProperty("camus.message.timestamp.format", "unix_milliseconds");

        JsonStringMessageDecoder testDecoder = new JsonStringMessageDecoder();
        testDecoder.init(testProperties, "testTopic");
        String payload = "{\"timestamp\":  "+ testTimestamp + ", \"myData\": \"myValue\"}";
        byte[] bytePayload = payload.getBytes();

        CamusWrapper actualResult = testDecoder.decode(bytePayload);
        long actualTimestamp = actualResult.getTimestamp();
        assertEquals(actualTimestamp, expectedTimestamp);
    }

    @Test
    public void testDecodeUnixSeconds() {

        // Test that the decoder extracts unix_seconds
        // It should receive seconds and return milliseconds

        long testTimestamp = 140694727L;
        long expectedTimestamp = 140694727000L;

        Properties testProperties = new Properties();
        testProperties.setProperty("camus.message.timestamp.format", "unix_seconds");

        JsonStringMessageDecoder testDecoder = new JsonStringMessageDecoder();
        testDecoder.init(testProperties, "testTopic");
        String payload = "{\"timestamp\":  "+ testTimestamp + ", \"myData\": \"myValue\"}";
        byte[] bytePayload = payload.getBytes();
        CamusWrapper actualResult = testDecoder.decode(bytePayload);
        long actualTimestamp = actualResult.getTimestamp();
        System.out.println(actualTimestamp);

        assertEquals(expectedTimestamp, actualTimestamp);
    }

}

@@ -1073,22 +1073,24 @@
 tEquals(
-actual
+expected
 Timestam
@@ -1092,24 +1092,22 @@
 estamp, 
-expected
+actual
 Timestam
@@ -1925,53 +1925,8 @@
 p();
-%0A        System.out.println(actualTimestamp);
 %0A%0A  
@@ -1988,10 +1988,951 @@
 %0A    %7D%0A%0A
+    @Test public void testDecodeWithTimestampFormat() %7B%0A%0A        // Test that we can specify a date and a pattern and%0A        // get back unix timestamp milliseconds%0A%0A        String testFormat = %22yyyy-MM-dd HH:mm:ss Z%22;%0A        String testTimestamp = %222014-02-01 01:15:27 UTC%22;%0A        long expectedTimestamp = 1391217327000L;%0A%0A        Properties testProperties = new Properties();%0A        testProperties.setProperty(%22camus.message.timestamp.format%22, testFormat);%0A%0A        JsonStringMessageDecoder testDecoder = new JsonStringMessageDecoder();%0A        testDecoder.init(testProperties, %22testTopic%22);%0A        String payload = %22%7B%5C%22timestamp%5C%22:  %5C%22%22+ testTimestamp + %22%5C%22, %5C%22myData%5C%22: %5C%22myValue%5C%22%7D%22;%0A        byte%5B%5D bytePayload = payload.getBytes();%0A        CamusWrapper actualResult = testDecoder.decode(bytePayload);%0A        long actualTimestamp = actualResult.getTimestamp();%0A%0A        assertEquals(expectedTimestamp, actualTimestamp);%0A%0A%0A    %7D%0A%0A
 %7D%0A

test for parsing json timestamp with a format string