package com.martiansoftware.nailgun;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class NGCommunicatorTest {
  // Mocks
  private Socket socket;
  private InputStream istream;
  private OutputStream ostream;

  @BeforeEach
  private void before() throws IOException {
    socket = mock(Socket.class);

    istream = mock(InputStream.class);
    ostream = mock(OutputStream.class);

    when(socket.getInputStream()).thenReturn(istream);
    when(socket.getOutputStream()).thenReturn(ostream);
  }

  @Test
  void canRun() throws IOException {
    NGCommunicator comm = new NGCommunicator(socket, 0);
    assertNotNull(comm);
  }

  @Test
  void canReadCommand() throws IOException {
    String command = "some_command";
    byte[] commandBin = command.getBytes(StandardCharsets.UTF_8);
    byte[] payload;
    try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        DataOutputStream stream = new DataOutputStream(byteStream)) {
      stream.writeInt(commandBin.length);
      stream.writeByte(NGConstants.CHUNKTYPE_COMMAND);
      stream.write(commandBin);
      stream.flush();
      payload = byteStream.toByteArray();
    }

    ByteArrayInputStream istream = new ByteArrayInputStream(payload);
    when(socket.getInputStream()).thenReturn(istream);

    // ByteArrayInputStream is = new ByteArrayInputStream()
    NGCommunicator comm = new NGCommunicator(socket, 0);
    CommandContext context = comm.readCommandContext();
    assertEquals(command, context.getCommand());
  }
}

@@ -194,16 +194,58 @@
 o.mock;%0A
+import static org.mockito.Mockito.verify;%0A
 import s
@@ -1810,68 +1810,8 @@
 );%0A%0A
-    // ByteArrayInputStream is = new ByteArrayInputStream()%0A
     
@@ -1971,11 +1971,280 @@
 ());%0A  %7D
+%0A%0A  @Test%0A  void canWriteData() throws IOException %7B%0A    NGCommunicator comm = new NGCommunicator(socket, 0);%0A%0A    byte%5B%5D data = %7B0x01, 0x02, 0x03%7D;%0A    comm.send(NGConstants.CHUNKTYPE_STDOUT, data, 0, data.length);%0A%0A    verify(ostream).write(data, 0, data.length);%0A  %7D
 %0A%7D%0A

Test for writes for NGCommunicator