/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package org.apache.hc.client5.http.entity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;
import java.util.zip.GZIPInputStream;

import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class TestDecompressingEntity {

    @Test
    void testNonStreaming() throws Exception {
        final CRC32 crc32 = new CRC32();
        final StringEntity wrapped = new StringEntity("1234567890", StandardCharsets.US_ASCII);
        final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
        Assertions.assertFalse(entity.isStreaming());
        final String s = EntityUtils.toString(entity);
        Assertions.assertEquals("1234567890", s);
        Assertions.assertEquals(639479525L, crc32.getValue());
        final InputStream in1 = entity.getContent();
        final InputStream in2 = entity.getContent();
        Assertions.assertNotSame(in1, in2);
    }

    @Test
    void testStreaming() throws Exception {
        final CRC32 crc32 = new CRC32();
        final ByteArrayInputStream in = new ByteArrayInputStream("1234567890".getBytes(StandardCharsets.US_ASCII));
        final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.DEFAULT_TEXT);
        final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
        Assertions.assertTrue(entity.isStreaming());
        final String s = EntityUtils.toString(entity);
        Assertions.assertEquals("1234567890", s);
        Assertions.assertEquals(639479525L, crc32.getValue());
        final InputStream in1 = entity.getContent();
        final InputStream in2 = entity.getContent();
        Assertions.assertSame(in1, in2);
        EntityUtils.consume(entity);
        EntityUtils.consume(entity);
    }

    @Test
    void testStreamingMarking() throws Exception {
        final CRC32 crc32 = new CRC32();
        final ByteArrayInputStream in = new ByteArrayInputStream("1234567890".getBytes(StandardCharsets.US_ASCII));
        final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.DEFAULT_TEXT);
        final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
        final InputStream in1 = entity.getContent();
        Assertions.assertEquals('1', in1.read());
        Assertions.assertEquals('2', in1.read());
        in1.mark(1);
        Assertions.assertEquals('3', in1.read());
        in1.reset();
        Assertions.assertEquals('3', in1.read());
        EntityUtils.consume(entity);
    }

    @Test
    void testWriteToStream() throws Exception {
        final CRC32 crc32 = new CRC32();
        final StringEntity wrapped = new StringEntity("1234567890", StandardCharsets.US_ASCII);
        try (final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32)) {
            Assertions.assertFalse(entity.isStreaming());

            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            entity.writeTo(out);

            final String s = new String(out.toByteArray(), StandardCharsets.US_ASCII);
            Assertions.assertEquals("1234567890", s);
            Assertions.assertEquals(639479525L, crc32.getValue());
        }
    }

    @Test
    void testMalformedGzipContentCanBeClosedWithoutReading() throws Exception {
        final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
        final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.APPLICATION_OCTET_STREAM);
        final HttpEntity entity = new org.apache.hc.client5.http.entity.compress.DecompressingEntity(
                wrapped, GZIPInputStream::new);

        Assertions.assertDoesNotThrow(() -> entity.getContent().close());
    }

    @Test
    void testMalformedGzipContentFailsOnRead() throws Exception {
        final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
        final InputStreamEntity wrapped = new InputStreamEntity(in, -1, ContentType.APPLICATION_OCTET_STREAM);
        final HttpEntity entity = new org.apache.hc.client5.http.entity.compress.DecompressingEntity(
                wrapped, GZIPInputStream::new);

        try (final InputStream content = entity.getContent()) {
            Assertions.assertThrows(IOException.class, content::read);
        }
    }

    static class ChecksumEntity extends org.apache.hc.client5.http.entity.compress.DecompressingEntity {

        public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
            super(wrapped, inStream -> new CheckedInputStream(inStream, checksum));
        }

    }

}

