/*
 * Copyright 2010 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.ning.metrics.serialization.event;

import com.ning.metrics.serialization.smile.SmileBucket;
import com.ning.metrics.serialization.smile.SmileBucketDeserializer;
import com.ning.metrics.serialization.smile.SmileBucketSerializer;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TestSmileBucketEvent
{
    // { "eventDate": "1242", "granularity": "hourly", "meh": [1,2,3] }
    // { "eventDate": "1243", "granularity": "hourly", "meh": [1,2,3], "bleh": "user-agent" }
    private final static String fileData = new File(new File("").getAbsolutePath(), "src/test/java/com/ning/metrics/serialization/smile/sampleEvents.json").getPath();

    private SmileBucketEvent bucketEvent;
    private ByteArrayOutputStream finalStream;

    @BeforeTest
    public void setUp() throws IOException
    {
        // We feed plain json but get Smile back
        // FileInputStream inStream = new FileInputStream(fileData);
        // eventsBytes = new byte[inStream.available()];
        // inStream.read(eventsBytes);
        // inStream.close();
        SmileBucket bucket2 = SmileBucketDeserializer.deserialize(new FileInputStream(fileData));
        finalStream = new ByteArrayOutputStream();
        SmileBucketSerializer.serialize(bucket2, finalStream);

        SmileBucket bucket = SmileBucketDeserializer.deserialize(new FileInputStream(fileData));
        bucketEvent = new SmileBucketEvent("testEvent", Granularity.HOURLY, bucket);
    }

    @Test
    public void testNumberOfEvents() throws Exception
    {
        Assert.assertEquals(bucketEvent.getNumberOfEvent(), 2);
    }

    @Test
    public void testGetData() throws Exception
    {
        byte[] bytes = ((ByteArrayOutputStream) bucketEvent.getData()).toByteArray();

        Assert.assertEquals(bytes, finalStream.toByteArray());

        // Test we get Smile back
        Assert.assertEquals(bytes[0], ':');
        Assert.assertEquals(bytes[1], ')');
        Assert.assertEquals(bytes[2], '\n');
    }

    @Test
    public void testGetSerializedEvent() throws Exception
    {
        Assert.assertEquals(bucketEvent.getSerializedEvent(), finalStream.toByteArray());
    }

    @Test
    public void testReadWriteExternal() throws Exception
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = new ObjectOutputStream(out);
        bucketEvent.writeExternal(outputStream);
        outputStream.close();

        SmileBucketEvent bucketEvent2 = new SmileBucketEvent();
        bucketEvent2.readExternal(new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())));

        Assert.assertEquals(bucketEvent2.getName(), bucketEvent.getName());
        Assert.assertEquals(bucketEvent2.getGranularity(), bucketEvent.getGranularity());
        Assert.assertEquals(bucketEvent2.getSerializedEvent(), bucketEvent.getSerializedEvent());

        Assert.assertEquals(bucketEvent2.getNumberOfEvent(), bucketEvent.getNumberOfEvent());
    }
}

@@ -1219,16 +1219,79 @@
 Event%0A%7B%0A
+    private static final String OUTPUT_DIR = %22/hello/world/%22;%0A%0A
     // %7B
@@ -1658,16 +1658,71 @@
 tEvent;%0A
+    private SmileBucketEvent bucketEventWithOutputDir;%0A
     priv
@@ -2464,36 +2464,163 @@
 ucket);%0A    
-%7D%0A%0A    @Test
+    bucketEventWithOutputDir = new SmileBucketEvent(%22testEvent%22, Granularity.HOURLY, OUTPUT_DIR, bucket);%0A    %7D%0A%0A    @Test(groups = %22fast%22)
 %0A    public 
@@ -2740,32 +2740,49 @@
     %7D%0A%0A    @Test
+(groups = %22fast%22)
 %0A    public void
@@ -3145,32 +3145,49 @@
     %7D%0A%0A    @Test
+(groups = %22fast%22)
 %0A    public void
@@ -3337,24 +3337,41 @@
 %7D%0A%0A    @Test
+(groups = %22fast%22)
 %0A    public 
@@ -3414,32 +3414,685 @@
 Exception%0A    %7B%0A
+        SmileBucketEvent event = doTestsRW(bucketEvent);%0A        try %7B%0A            event.getOutputDir(%22%22);%0A            Assert.fail();%0A        %7D%0A        catch (RuntimeException e) %7B%0A            // Hasn't been set, should trigger an exception%0A            Assert.assertTrue(true);%0A        %7D%0A    %7D%0A%0A    @Test(groups = %22fast%22)%0A    public void testReadWriteExternalWithOutputDir() throws Exception%0A    %7B%0A        SmileBucketEvent event = doTestsRW(bucketEventWithOutputDir);%0A        Assert.assertEquals(event.getOutputDir(%22%22), OUTPUT_DIR);%0A    %7D%0A%0A    private SmileBucketEvent doTestsRW(SmileBucketEvent bucket) throws IOException, ClassNotFoundException%0A    %7B%0A
         ByteArra
@@ -4225,21 +4225,16 @@
   bucket
-Event
 .writeEx
@@ -4508,29 +4508,24 @@
 me(), bucket
-Event
 .getName());
@@ -4586,29 +4586,24 @@
 ty(), bucket
-Event
 .getGranular
@@ -4675,29 +4675,24 @@
 nt(), bucket
-Event
 .getSerializ
@@ -4767,29 +4767,24 @@
 nt(), bucket
-Event
 .getNumberOf
@@ -4788,21 +4788,51 @@
 rOfEvent());
+%0A%0A        return bucketEvent2;
 %0A    %7D%0A%7D%0A

add outputDir serialization tests