package com.github.dockerjava.core.command;

import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;


public class FrameReaderTest {
    public static final int HEADER_SIZE = 8;

    private final List<Integer> bytes = new ArrayList<>();

    private final InputStream inputStream = new InputStream() {
        @Override
        public int read() throws IOException {
            return bytes.isEmpty() ? -1 : bytes.remove(0);
        }
    };

    private final FrameReader frameReader = new FrameReader(inputStream);

    @Test
    public void endOfStreamReturnsNull() throws Exception {
        assertNull(nextFrame());
    }

    @Test
    public void stdInBytesFrameReturnsFrame() throws Exception {
        assertEquals(new Frame(StreamType.STDIN, new byte[0]), nextFrame(0, 0, 0, 0, 0, 0, 0, 0));
    }

    private Frame nextFrame(int... bytes) throws IOException {
        setBytes(bytes);
        return frameReader.readFrame();
    }

    @Test
    public void stdOutBytesFrameReturnsFrame() throws Exception {
        assertEquals(new Frame(StreamType.STDOUT, new byte[0]), nextFrame(1, 0, 0, 0, 0, 0, 0, 0));
    }

    @Test
    public void stdErrBytesFrameReturnsFrame() throws Exception {
        assertEquals(new Frame(StreamType.STDERR, new byte[0]), nextFrame(2, 0, 0, 0, 0, 0, 0, 0));
    }

    private void setBytes(int... bytes) {
        this.bytes.clear();
        for (int aByte : bytes) {
            this.bytes.add(aByte);
        }
    }
}
