/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.underfs.s3;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;

import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.hamcrest.CoreMatchers.is;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import org.mockito.Mockito;

import org.jets3t.service.S3Service;
import org.jets3t.service.ServiceException;
import org.jets3t.service.model.S3Object;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Unit tests for {@link S3InputStream}.
 */
public class S3InputStreamTest {

  private static final String BUCKET_NAME = "testBucket";
  private static final String OBJECT_KEY = "testObjectKey";

  private S3InputStream mS3InputStream;
  private S3Service mS3Service;
  private S3Object mS3Object;
  private InputStream mInputStreamSpy;

  @Rule
  public ExpectedException mExceptionRule = ExpectedException.none();

  @BeforeClass
  public static void setUpClass() throws ServiceException {}

  @AfterClass
  public static void tearDownClass() {}

  @Before
  public void setUp() throws ServiceException {
    mInputStreamSpy = Mockito.spy(new ByteArrayInputStream(new byte[] {1, 2, 3}));
    mS3Service = Mockito.mock(S3Service.class);
    mS3Object = Mockito.mock(S3Object.class);
    when(mS3Object.getDataInputStream()).thenReturn(mInputStreamSpy);
    when(mS3Service.getObject(BUCKET_NAME, OBJECT_KEY)).thenReturn(mS3Object);
    mS3InputStream = new S3InputStream(BUCKET_NAME, OBJECT_KEY, mS3Service);
  }

  @After
  public void tearDown() {}

  /**
   * Test of close method, of class S3InputStream.
   */
  @Test
  public void close() throws IOException {
    assertNotNull(mS3InputStream);
    mS3InputStream.close();
    verify(mInputStreamSpy).close();

    mExceptionRule.expect(IOException.class);
    mExceptionRule.expectMessage(is("Stream closed"));
    mS3InputStream.read();
  }

  /**
   * Test of read method, of class S3InputStream.
   */
  @Test
  public void read() throws IOException {
    assertNotNull(mS3InputStream);
    assertEquals(1, mS3InputStream.read());
    assertEquals(2, mS3InputStream.read());
    assertEquals(3, mS3InputStream.read());
  }

  /**
   * Test of read method, of class S3InputStream.
   */
  @Test
  public void readWithArgs() throws Exception {
    assertNotNull(mS3InputStream);
    byte[] bytes = new byte[3];
    int readedCount = mS3InputStream.read(bytes, 0, 3);
    assertEquals(3, readedCount);
    assertArrayEquals(new byte[] {1, 2, 3}, bytes);
  }

  /**
   * Test of skip method, of class S3InputStream.
   */
  @Test
  public void skip() throws IOException {
    assertNotNull(mS3InputStream);
    assertEquals(1, mS3InputStream.read());
    mS3InputStream.skip(1);
    assertEquals(3, mS3InputStream.read());
  }
}

@@ -558,34 +558,32 @@
 org.
-junit.Assert.assertNotNull
+hamcrest.CoreMatchers.is
 ;%0Aim
@@ -613,24 +613,29 @@
 ssert.assert
+Array
 Equals;%0Aimpo
@@ -667,29 +667,24 @@
 t.assert
-Array
 Equals;%0A
 %0Aimport 
@@ -675,17 +675,16 @@
 Equals;%0A
-%0A
 import s
@@ -709,20 +709,22 @@
 Mockito.
-when
+verify
 ;%0Aimport
@@ -747,39 +747,38 @@
 ito.Mockito.
-verify;
+when;%0A
 %0Aimport 
 static org.h
@@ -769,46 +769,82 @@
 ort 
-static org.hamcrest.CoreMatchers.is
+org.jets3t.service.S3Service;%0Aimport org.jets3t.service.ServiceException
 ;%0A
-%0A
 impo
@@ -851,26 +851,44 @@
 rt org.j
-unit.After
+ets3t.service.model.S3Object
 ;%0Aimport
@@ -982,20 +982,20 @@
 g.junit.
-Test
+Rule
 ;%0Aimport
@@ -997,36 +997,36 @@
 mport org.junit.
-Rule
+Test
 ;%0Aimport org.jun
@@ -1053,17 +1053,16 @@
 eption;%0A
-%0A
 import o
@@ -1086,132 +1086,8 @@
 o;%0A%0A
-import org.jets3t.service.S3Service;%0Aimport org.jets3t.service.ServiceException;%0Aimport org.jets3t.service.model.S3Object;%0A%0A
 impo
@@ -2201,46 +2201,8 @@
  %7D%0A%0A
-  @After%0A  public void tearDown() %7B%7D%0A%0A
   /*
@@ -2315,43 +2315,8 @@
 n %7B%0A
-    assertNotNull(mS3InputStream);%0A
     
@@ -2626,43 +2626,8 @@
 n %7B%0A
-    assertNotNull(mS3InputStream);%0A
     
@@ -2865,16 +2865,18 @@
  throws 
+IO
 Exceptio
@@ -2883,43 +2883,8 @@
 n %7B%0A
-    assertNotNull(mS3InputStream);%0A
     
@@ -2919,26 +2919,24 @@
     int read
-ed
 Count = mS3I
@@ -2989,18 +2989,16 @@
 (3, read
-ed
 Count);%0A
@@ -3170,43 +3170,8 @@
 n %7B%0A
-    assertNotNull(mS3InputStream);%0A
     

fix remarks from Github