/*
 * Copyright 2013 Rackspace
 *
 *    Licensed 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.rackspacecloud.blueflood.io;

import com.rackspacecloud.blueflood.types.*;
import org.apache.commons.codec.binary.Base64;
import org.junit.Assert;
import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;

public class HistogramSerializationTest {
    private static HistogramRollup histogramRollup;

    static {
        Points<SimpleNumber> points = new Points<SimpleNumber>();
        long startTime = 12345678L;
        for (double val : TestData.DOUBLE_SRC) {
            points.add(new Points.Point<SimpleNumber>(startTime++, new SimpleNumber(val)));
        }

        try {
             histogramRollup = (HistogramRollup) Rollup.buildRollupFromRawSamples(
                points, Rollup.Type.HISTOGRAM);
        } catch (Exception ex) {
            Assert.fail("Test data generation failed");
        }
    }

    @Test
    public void testSerializationDeserializationVersion1() throws Exception {
        if (System.getProperty("GENERATE_HIST_SERIALIZATION") != null) {
            OutputStream os = new FileOutputStream("src/test/resources/serializations/histogram_version_" +
                    Constants.VERSION_1_HISTOGRAM + ".bin", false);

            os.write(Base64.encodeBase64(HistogramSerializer.get().toByteBuffer(histogramRollup).array()));
            os.write("\n".getBytes());
            os.close();
        }

        Assert.assertTrue(new File("src/test/resources/serializations").exists());

        // ensure we can read historical serializations.
        int version = 0;
        int maxVersion = Constants.VERSION_1_HISTOGRAM;
        while (version <= maxVersion) {
            BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/serializations/histogram_version_" + version + ".bin"));
            ByteBuffer bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
            HistogramRollup histogramRollupDes = HistogramSerializer.get().fromByteBuffer(bb);
            Assert.assertArrayEquals(histogramRollup.getBins().toArray(), histogramRollupDes.getBins().toArray());
            version++;
        }
    }

    @Test
    public void testBadSerializationVersion() {
        byte[] buf = new byte[] {99, 99};  // hopefully we won't have 99 different serialization versions.
        try {
            HistogramSerializer.get().fromByteBuffer(ByteBuffer.wrap(buf));
            Assert.fail(String.format("Should have errored out. Such a version doesn't exist for histogram."));
        } catch (RuntimeException ex) {
            Assert.assertTrue(ex.getCause().getMessage().startsWith("Unexpected serialization version"));
        }
    }
}

@@ -656,16 +656,89 @@
 od.io;%0A%0A
+import com.bigml.histogram.Bin;%0Aimport com.bigml.histogram.SimpleTarget;%0A
 import c
@@ -770,24 +770,24 @@
 od.types.*;%0A
-
 import org.a
@@ -915,16 +915,93 @@
 eBuffer;
+%0Aimport java.util.Collection;%0Aimport java.util.Map;%0Aimport java.util.TreeMap;
 %0A%0Apublic
@@ -2756,19 +2756,31 @@
 sert
-Array
+True(areHistograms
 Equal
-s
 (his
@@ -2791,36 +2791,16 @@
 amRollup
-.getBins().toArray()
 , histog
@@ -2815,27 +2815,8 @@
 pDes
-.getBins().toArray(
 ));%0A
@@ -3364,24 +3364,24 @@
 version%22));%0A
-
         %7D%0A  
@@ -3383,11 +3383,1031 @@
  %7D%0A    %7D
+%0A%0A    private boolean areHistogramsEqual(HistogramRollup first, HistogramRollup second) %7B%0A        final TreeMap%3CDouble, Double%3E firstBinsAsOrderedMap = getNonZeroBinsAsMap(first);%0A        final TreeMap%3CDouble, Double%3E secondBinsAsOrderedMap = getNonZeroBinsAsMap(second);%0A%0A        for (Map.Entry%3CDouble, Double%3E firstBin: firstBinsAsOrderedMap.entrySet()) %7B%0A            Double val = secondBinsAsOrderedMap.get(firstBin.getKey());%0A            if (val == null %7C%7C !firstBin.getValue().equals(val)) %7B%0A                return false;%0A            %7D%0A        %7D%0A%0A        return true;%0A    %7D%0A%0A    private TreeMap%3CDouble, Double%3E getNonZeroBinsAsMap(HistogramRollup histogramRollup) %7B%0A        Collection%3CBin%3CSimpleTarget%3E%3E bins = histogramRollup.getBins();%0A%0A        final TreeMap%3CDouble, Double%3E binsMap = new TreeMap%3CDouble, Double%3E();%0A        for (Bin%3CSimpleTarget%3E bin : bins) %7B%0A            if (bin.getCount() %3E 0) %7B%0A                binsMap.put(bin.getMean(), bin.getCount());%0A            %7D%0A        %7D%0A%0A        return binsMap;%0A    %7D
 %0A%7D%0A

Use stricter evaluation of histogram equals for tests