/*
 * ====================================================================
 * 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.impl.classic;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.hc.client5.http.classic.ExecRuntime;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

@SuppressWarnings("boxing") // test code
class TestResponseEntityWrapper {

    private InputStream inStream;
    private HttpEntity entity;
    private ExecRuntime execRuntime;
    private ResponseEntityProxy wrapper;

    @BeforeEach
    void setup() throws Exception {
        inStream = Mockito.mock(InputStream.class);
        entity = Mockito.mock(HttpEntity.class);
        Mockito.when(entity.getContent()).thenReturn(inStream);
        execRuntime = Mockito.mock(ExecRuntime.class);
        wrapper = new ResponseEntityProxy(entity, execRuntime);
    }

    @Test
    void testReusableEntityStreamClosed() throws Exception {
        Mockito.when(entity.isStreaming()).thenReturn(true);
        Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
        EntityUtils.consume(wrapper);

        Mockito.verify(inStream, Mockito.times(1)).close();
        Mockito.verify(execRuntime).releaseEndpoint();
    }

    @Test
    void testReusableEntityStreamClosedIOError() throws Exception {
        Mockito.when(entity.isStreaming()).thenReturn(true);
        Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
        Mockito.doThrow(new IOException()).when(inStream).close();
        Assertions.assertThrows(IOException.class, () -> EntityUtils.consume(wrapper));
        Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
    }

    @Test
    void testEntityStreamClosedIOErrorAlreadyReleased() throws Exception {
        Mockito.when(entity.isStreaming()).thenReturn(true);
        Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
        Mockito.when(execRuntime.isEndpointAcquired()).thenReturn(false);
        Mockito.doThrow(new SocketException()).when(inStream).close();
        EntityUtils.consume(wrapper);
        Mockito.verify(execRuntime).discardEndpoint();
    }

    @Test
    void testReusableEntityWriteTo() throws Exception {
        final OutputStream outStream = Mockito.mock(OutputStream.class);
        Mockito.when(entity.isStreaming()).thenReturn(true);
        Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
        wrapper.writeTo(outStream);
        Mockito.verify(execRuntime).releaseEndpoint();
    }

    @Test
    void testReusableEntityWriteToIOError() throws Exception {
        final OutputStream outStream = Mockito.mock(OutputStream.class);
        Mockito.when(entity.isStreaming()).thenReturn(true);
        Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
        Mockito.doThrow(new IOException()).when(entity).writeTo(outStream);
        Assertions.assertThrows(IOException.class, () -> wrapper.writeTo(outStream));
        Mockito.verify(execRuntime, Mockito.never()).releaseEndpoint();
        Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
    }

    @Test
    void testReusableEntityEndOfStream() throws Exception {
        Mockito.when(inStream.read()).thenReturn(-1);
        Mockito.when(entity.isStreaming()).thenReturn(true);
        Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
        final InputStream content = wrapper.getContent();
        Assertions.assertEquals(-1, content.read());
        Mockito.verify(inStream).close();
        Mockito.verify(execRuntime).releaseEndpoint();
    }

    @Test
    void testReusableEntityEndOfStreamIOError() throws Exception {
        Mockito.when(inStream.read()).thenReturn(-1);
        Mockito.when(entity.isStreaming()).thenReturn(true);
        Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
        Mockito.doThrow(new IOException()).when(inStream).close();
        final InputStream content = wrapper.getContent();
        Assertions.assertThrows(IOException.class, () -> content.read());
        Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint();
    }

}
