/*
 * Copyright Â© 2013-2015 The Hyve B.V.
 *
 * This file is part of transmart-core-db.
 *
 * Transmart-core-db is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * transmart-core-db.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.transmartproject.db.dataquery.highdim.snp_lz

import org.gmock.WithGMock
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException

import java.sql.Blob
import java.util.zip.ZipException

import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.Matchers.*

/**
 * Tests for {@link GzipFieldTokenizer}.
 */
@WithGMock
class GzipFieldTokenizerTests {

    public static final double DELTA = 1e-16.doubleValue()

    @Rule
    public ExpectedException expectedException = ExpectedException.none()

    GzipFieldTokenizer testee

    @Test
    void testAsDoubleArrayNormal() {
        def blob = blobFor(DATA)

        play {
            testee = new GzipFieldTokenizer(blob, DATA_NUMBER_OF_TOKENS)
            def array = testee.asDoubleArray()
            assert array instanceof double[]
            assertThat array as List, allOf(
                    hasSize(DATA_NUMBER_OF_TOKENS),
                    everyItem(
                            anyOf(
                                    closeTo(0.doubleValue(), DELTA),
                                    closeTo(1.doubleValue(), DELTA))))
        }
    }

    @Test
    void testAsDoubleArraySmallerExpected() {
        expectedException.expect(InputMismatchException)
        expectedException.expectMessage('Got more tokens')

        def blob = blobFor(DATA)
        play {
            testee = new GzipFieldTokenizer(blob, DATA_NUMBER_OF_TOKENS - 1)
            testee.asDoubleArray()
        }
    }

    @Test
    void testAsDoubleArrayLargerExpected() {
        expectedException.expect(InputMismatchException)
        expectedException.expectMessage('but got only')

        def blob = blobFor(DATA)
        play {
            testee = new GzipFieldTokenizer(blob, DATA_NUMBER_OF_TOKENS + 1)
            testee.asDoubleArray()
        }
    }

    @Test
    void testAsDoubleCorruptedInput() {
        expectedException.expect(ZipException)

        def corruptedData = DATA as List
        corruptedData[0] = 0xff // corrupt the header

        def blob = blobFor(corruptedData as byte[])

        play {
            testee = new GzipFieldTokenizer(blob, DATA_NUMBER_OF_TOKENS)
            testee.asDoubleArray()
        }
    }

    @Test
    void testAsDoubleArrayCharDataInstead() {
        expectedException.expect(NumberFormatException)

        def blob = blobFor(DATA_CHAR)

        play {
            testee = new GzipFieldTokenizer(blob, DATA_CHAR_NUMBER_OF_TOKENS)
            testee.asDoubleArray()
        }
    }

    @Test
    void testAsDoubleArraySmall() {
        def blob = blobFor(DATA_SMALL_DOUBLE)

        play {
            testee =  new GzipFieldTokenizer(blob, DATA_SMALL_NUMBER_OF_TOKENS)
            def array = testee.asDoubleArray()
            assertThat array as List, contains(DATA_SMALL_DOUBLE_SOURCE)
        }
    }

    @Test
    void testAsStringList() {
        def blob = blobFor(DATA_CHAR)

        play {
            testee =  new GzipFieldTokenizer(blob, DATA_CHAR_NUMBER_OF_TOKENS)
            def l = testee.asStringList()
            assertThat(l.size(), is(DATA_CHAR_SOURCE.size()))
            assertThat(l, contains(DATA_CHAR_SOURCE.collect { c -> equalTo(new String(c)) }))
        }
    }

    @Test
    void testAsStringListTokenWithMoreThanOneCharacter() {
        def blob = blobFor(DATA_STRING)

        play {
            testee =  new GzipFieldTokenizer(blob, 6)
            def l = testee.asStringList()
            assertThat(l.size(), is(DATA_STRING_SOURCE.size()))
            assertThat(l, contains(DATA_STRING_SOURCE.collect { d -> equalTo(d) }))
        }
    }

    @Test
    void testAsStringListSmallerThanExpected() {
        expectedException.expect(InputMismatchException)
        expectedException.expectMessage('tokens, but got only')

        def blob = blobFor(DATA_CHAR)

        play {
            testee =  new GzipFieldTokenizer(blob, DATA_CHAR_NUMBER_OF_TOKENS + 1)
            testee.asStringList()
        }
    }

    @Test
    void testAsStringListLargerThanExpected() {
        expectedException.expect(InputMismatchException)
        expectedException.expectMessage('Got more tokens')

        def blob = blobFor(DATA_CHAR)

        play {
            testee =  new GzipFieldTokenizer(blob, DATA_CHAR_NUMBER_OF_TOKENS - 1)
            testee.asStringList()
        }
    }

    @Test
    void testAsStringListEmptyInput() {
        def blob = blobFor(EMPTY_INPUT)

        play {
            testee =  new GzipFieldTokenizer(blob, 0)
            def l = testee.asStringList()
            assert l.size() == 0
        }
    }

    private Blob blobFor(byte[] bytes) {
        Blob blob = mock(Blob)
        blob.binaryStream.returns(new ByteArrayInputStream(bytes))
        blob
    }

    // Real data from dump given to us
    private final static byte[] DATA = [
        0x1f, 0x8b, 0x08, 0x00, 0x0b, 0xde, 0xec, 0x55, 0x02, 0x03, 0xb5, 0x9b, 0x51, 0x4e, 0x04, 0x31, 0x0c, 0x43,
        0xaf, 0xc2, 0x11, 0xb6, 0xf7, 0xbf, 0x1c, 0x20, 0x84, 0x04, 0x3b, 0xd3, 0xc6, 0xcf, 0xf6, 0x7c, 0xb0, 0xac,
        0x4a, 0xa7, 0xd3, 0xa6, 0x89, 0xeb, 0x38, 0xe5, 0xf5, 0xf1, 0xfa, 0x58, 0x5f, 0x3f, 0x3f, 0x9f, 0x3f, 0xdf,
        0xbe, 0x7f, 0xff, 0x7e, 0xbb, 0xfe, 0x75, 0xfd, 0x69, 0xbf, 0xb6, 0xbc, 0x8f, 0xb3, 0xff, 0xbc, 0xb6, 0x2a,
        0x7d, 0xde, 0xfb, 0xff, 0x9f, 0x27, 0x9d, 0xc9, 0x6e, 0x45, 0xe7, 0x3e, 0x5e, 0xfb, 0x64, 0xe1, 0x17, 0xb2,
        0x5b, 0xde, 0xe7, 0x34, 0xe7, 0xc9, 0xaa, 0xba, 0x3d, 0xd5, 0xa7, 0xf6, 0xfe, 0x46, 0xdf, 0xbb, 0xa0, 0x25,
        0xe7, 0x9d, 0xba, 0x6b, 0x39, 0xbf, 0xeb, 0xd4, 0x4e, 0x56, 0x9a, 0x5b, 0xd2, 0x8b, 0x2c, 0x3d, 0x6a, 0x14,
        0x2b, 0xcd, 0x6f, 0xbb, 0xb3, 0x89, 0xee, 0xdb, 0x74, 0xe6, 0xfa, 0xf8, 0x0a, 0x9e, 0xe4, 0xfe, 0xe6, 0x79,
        0xaf, 0xee, 0xa5, 0x0a, 0xfe, 0x50, 0x0c, 0xf4, 0xfc, 0xe7, 0xfc, 0x76, 0x7d, 0x2d, 0x4f, 0xec, 0x35, 0xb5,
        0x89, 0xe2, 0x39, 0xca, 0xfc, 0xcf, 0x73, 0xd2, 0x91, 0x4a, 0x79, 0x96, 0xe2, 0x89, 0x77, 0x5e, 0x7b, 0x91,
        0x75, 0xea, 0xb9, 0xc7, 0x87, 0x33, 0x63, 0xa1, 0x31, 0x48, 0xe3, 0xb1, 0x8b, 0x3c, 0xf4, 0x6c, 0xca, 0x23,
        0xbd, 0x65, 0x4f, 0x86, 0x09, 0x2b, 0x62, 0x3b, 0xca, 0xda, 0xe7, 0x75, 0xed, 0x23, 0x25, 0xe1, 0x18, 0xf7,
        0x23, 0x4c, 0xeb, 0xd5, 0x51, 0x4e, 0x8d, 0xeb, 0x69, 0xef, 0x4e, 0x33, 0x51, 0x90, 0xf9, 0xee, 0xf9, 0x79,
        0xb4, 0xbd, 0x7d, 0x75, 0xae, 0x32, 0x47, 0xc7, 0x7e, 0x6e, 0x4d, 0x96, 0xe5, 0x3e, 0x3b, 0x47, 0x99, 0x6e,
        0x5b, 0x82, 0xc6, 0xf4, 0x34, 0xe9, 0xe2, 0x58, 0xc2, 0xf1, 0xf4, 0x58, 0xd3, 0x51, 0x3d, 0xcf, 0x01, 0x97,
        0xc5, 0xe1, 0xf3, 0x53, 0x38, 0xf7, 0x4f, 0x8a, 0x36, 0xc9, 0x4a, 0x9f, 0x88, 0x9d, 0xdc, 0x33, 0x1d, 0xdc,
        0x26, 0x59, 0x15, 0x3b, 0xcd, 0x39, 0x67, 0xcb, 0x15, 0x00, 0x9d, 0xbd, 0x9f, 0xc6, 0xc9, 0x66, 0xae, 0xf6,
        0x99, 0x38, 0x79, 0xcb, 0xa3, 0xf2, 0xd3, 0xa1, 0xc5, 0x55, 0xde, 0x2d, 0xbc, 0x67, 0x2c, 0x7a, 0x46, 0xbc,
        0x8e, 0x23, 0xd0, 0x33, 0x37, 0xf5, 0xd5, 0x49, 0x5d, 0x6c, 0xa9, 0x16, 0x34, 0x93, 0x6d, 0x31, 0x76, 0xaa,
        0x29, 0xe9, 0x4c, 0x2f, 0xb7, 0xed, 0x3d, 0x43, 0xbb, 0xfa, 0x06, 0xe1, 0x90, 0x54, 0x65, 0x6d, 0x29, 0x2a,
        0x4f, 0x8c, 0x90, 0xef, 0x7b, 0xbe, 0xfb, 0xf9, 0xd9, 0xaa, 0xe8, 0x1b, 0x3e, 0x8f, 0x9a, 0xc6, 0x51, 0xbe,
        0x77, 0x55, 0x02, 0x0f, 0x8b, 0x68, 0x4e, 0x4a, 0xf7, 0x28, 0x39, 0xc7, 0x3d, 0xaf, 0xd6, 0xde, 0xd2, 0xcc,
        0xbb, 0xf5, 0x7d, 0x7f, 0x22, 0x5b, 0x49, 0x38, 0x8f, 0x67, 0x6d, 0xca, 0x87, 0x3d, 0xbb, 0x39, 0xb5, 0x0c,
        0xe2, 0xc3, 0x7a, 0xbe, 0xe3, 0xe8, 0xd5, 0x77, 0x59, 0xff, 0xcc, 0x21, 0xb9, 0x82, 0x97, 0x67, 0x10, 0x94,
        0x93, 0xe7, 0xf5, 0x2c, 0xe6, 0x57, 0xa4, 0x0e, 0xeb, 0x29, 0x3c, 0x93, 0x65, 0xfc, 0x0a, 0x0e, 0x53, 0xf5,
        0x35, 0x95, 0x8c, 0x32, 0x6a, 0x4f, 0x65, 0x62, 0x79, 0xdc, 0x99, 0x99, 0x4f, 0xbe, 0x74, 0xb5, 0x73, 0xc7,
        0xc3, 0xbd, 0xbc, 0x49, 0xcb, 0x32, 0x5a, 0x55, 0x27, 0x9a, 0x6b, 0xb4, 0xf0, 0xf6, 0xbc, 0x6a, 0x96, 0x1f,
        0x51, 0xbd, 0x31, 0x53, 0xbf, 0xbd, 0xbd, 0xa0, 0x18, 0x9e, 0x68, 0x83, 0xa9, 0x22, 0xda, 0xc8, 0xf4, 0x95,
        0xbb, 0x22, 0x89, 0x92, 0xd3, 0xca, 0x23, 0x3c, 0xc5, 0xac, 0x65, 0xe1, 0x6e, 0x9d, 0x5d, 0xe7, 0xfc, 0xf9,
        0x7d, 0x21, 0xfd, 0x34, 0x49, 0xf2, 0x41, 0x27, 0x2f, 0x5b, 0x00, 0xc9, 0x67, 0xa4, 0x9a, 0xd9, 0x51, 0xa7,
        0x9e, 0xdb, 0xb4, 0x7c, 0xa6, 0x9b, 0x9d, 0xac, 0xd4, 0xe2, 0x54, 0x19, 0xd2, 0x26, 0xb5, 0x09, 0x47, 0xff,
        0xec, 0xe5, 0xc5, 0x49, 0xcf, 0x44, 0xeb, 0x60, 0x55, 0xc2, 0x76, 0xb5, 0xc5, 0xe3, 0x3f, 0xcd, 0x3a, 0x82,
        0xe7, 0x09, 0x8d, 0x8a, 0x43, 0xb3, 0xca, 0x46, 0x18, 0xfe, 0x53, 0x73, 0x20, 0xdc, 0xde, 0x53, 0x1b, 0xd2,
        0xbb, 0x16, 0xfa, 0xda, 0xb5, 0xbb, 0x46, 0xde, 0xfd, 0x3a, 0x8a, 0x21, 0xf3, 0x59, 0x70, 0x9e, 0xad, 0xb6,
        0x73, 0x34, 0x1b, 0xdd, 0x65, 0x46, 0xb9, 0x57, 0x24, 0x7a, 0x6c, 0x5e, 0x29, 0xf0, 0x4e, 0x58, 0x87, 0x85,
        0x72, 0x36, 0xf2, 0x74, 0x9d, 0x2b, 0xef, 0x93, 0xdf, 0xb9, 0x3a, 0x8d, 0x4f, 0x14, 0xf2, 0x84, 0xe1, 0xab,
        0xb7, 0xda, 0x7a, 0x37, 0x93, 0x93, 0x5b, 0x82, 0x2a, 0x32, 0x90, 0xdc, 0xd6, 0x8f, 0x38, 0x52, 0x7d, 0xd0,
        0x2b, 0x59, 0xba, 0xad, 0xe8, 0xe9, 0x4c, 0xeb, 0x74, 0xfd, 0x5a, 0x55, 0x72, 0x1f, 0xac, 0xa1, 0x36, 0x24,
        0x0c, 0xea, 0xac, 0x4d, 0xed, 0x62, 0xaa, 0x39, 0x13, 0x2d, 0x4e, 0x57, 0xe8, 0x0f, 0x3d, 0x4e, 0xe5, 0xd5,
        0x3e, 0xe8, 0x7d, 0xd1, 0xbc, 0xda, 0xa2, 0xdc, 0xab, 0x49, 0x6f, 0xa3, 0x71, 0x1d, 0xa0, 0xa5, 0x94, 0x7a,
        0xf9, 0x32, 0xc5, 0x2e, 0x3f, 0xee, 0x5c, 0x4c, 0xc8, 0x2b, 0xbc, 0x4f, 0xfc, 0x47, 0x8f, 0xa2, 0xe5, 0xde,
        0xfb, 0xf9, 0x84, 0x18, 0x3a, 0x7a, 0x53, 0x65, 0xb5, 0x55, 0x09, 0x45, 0x15, 0xbd, 0x4f, 0x81, 0xd4, 0xe4,
        0x83, 0xf1, 0x37, 0x00, 0x00
    ] as byte[]

    private final static int DATA_NUMBER_OF_TOKENS = 7161

    /** Created with:
     *
     * CREATE GLOBAL TEMPORARY TABLE biomart_user.a(d blob);
     * INSERT INTO biomart_user.a(d)
     * SELECT UTL_COMPRESS.LZ_COMPRESS(utl_raw.cast_to_raw(
     *     '0.103344 0.542503 0.636123 0.984706 0.652751 0.900511 0.504302'), 9) FROM DUAL;
     *
     * echo implode(
     *     array_map(
     *         function ($s) { return "0x$s"; },
     *         str_split(
     *             unpack('H*', file_get_contents('/tmp/blob_s'))[1],
     *             2)),
     *     ', ');
     */
    private final static double[] DATA_SMALL_DOUBLE_SOURCE = [
        0.103344d, 0.542503d, 0.636123d, 0.984706d, 0.652751d, 0.900511d, 0.504302d] as double[]
    private final static byte[] DATA_SMALL_DOUBLE = [
        0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x15, 0xc8, 0xc1, 0x11, 0x00, 0x20, 0x08, 0x03,
        0xb0, 0x55, 0x9c, 0xc0, 0x2b, 0xb4, 0x05, 0xdd, 0x7f, 0x31, 0xe5, 0x97, 0x0b, 0x76, 0x80, 0x94, 0x16, 0xb6,
        0x95, 0x06, 0x3f, 0x8a, 0x15, 0x39, 0xb8, 0x47, 0x8d, 0x9a, 0x71, 0xb6, 0x63, 0x06, 0x70, 0x0c, 0x0c, 0x11,
        0xf9, 0x00, 0xcb, 0xe2, 0x16, 0x1a, 0x3e, 0x00, 0x00, 0x00
    ] as byte[]
    private final static int DATA_SMALL_NUMBER_OF_TOKENS = 7

    private final static char[] DATA_CHAR_SOURCE = [
        'T', 'T', 'T', 'A', 'T', 'T', 'N', 'N'] as char[]
    private final static byte[] DATA_CHAR = [
        0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x0b, 0x51, 0x08, 0x01, 0x42, 0x47, 0x30, 0xe9,
        0xa7, 0xe0, 0x07, 0x00, 0x4d, 0x42, 0x86, 0xbb, 0x0f, 0x00, 0x00, 0x00
    ] as byte[]
    private final static int DATA_CHAR_NUMBER_OF_TOKENS = 8

    // Created from 'T T A N AN T'
    private final static List<String> DATA_STRING_SOURCE = ["T", "T", "A", "N", "AN", "T"]
    private final static byte[] DATA_STRING = [
        0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x0b, 0x51, 0x08, 0x51, 0x70, 0x54, 0xf0, 0x53,
        0x70, 0xf4, 0x53, 0x08, 0x01, 0x00, 0x70, 0x1d, 0x0d, 0x8e, 0x0c, 0x00, 0x00, 0x00
    ] as byte[]
    private final static byte[] DATA_CHAR_BAD = DATA_STRING

    // echo -n "" | gzip -c -f | od -t x1
    private final static byte[] EMPTY_INPUT = [
        0x1f, 0x8b, 0x08, 0x00, 0x10, 0xa6, 0x65, 0x56, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ] as byte[]
}
