/*
 * 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.s3a;

import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.File;

/**
 * Unit tests for the {@link S3ADirectOutputStream}.
 */
public class S3ADirectOutputStreamTest {
  private static final String BUCKET_NAME = "testBucket";
  private static final String KEY = "testKey";

  private File mFile;
  private TransferManager mManager;
  private S3ADirectOutputStream mStream;

  /**
   * Sets the properties and configuration before each test runs.
   */
  @Before
  public void before() throws Exception {
    mFile = Mockito.mock(File.class);
    mManager = Mockito.mock(TransferManager.class);
    Upload result = Mockito.mock(Upload.class);

    Mockito.when(mManager.upload(Mockito.any(PutObjectRequest.class))).thenReturn(result);
    mStream = new S3ADirectOutputStream(BUCKET_NAME, KEY, mManager);
  }

  /**
   * Tests to ensure the permanent file path is used when upload is called.
   */
  @Test
  public void close() throws Exception {
    mStream.close();
    Mockito.verify(mManager).upload(
        new PutObjectRequest(BUCKET_NAME, KEY, mFile).withMetadata(Mockito
            .any(ObjectMetadata.class)));
  }
}

@@ -821,23 +821,251 @@
 org.
-mockito.Mockito
+junit.runner.RunWith;%0Aimport org.mockito.Matchers;%0Aimport org.mockito.Mockito;%0Aimport org.powermock.api.mockito.PowerMockito;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMockRunner
 ;%0A%0Ai
@@ -1146,16 +1146,93 @@
 m%7D.%0A */%0A
+@RunWith(PowerMockRunner.class)%0A@PrepareForTest(S3ADirectOutputStream.class)%0A
 public c
@@ -1374,30 +1374,8 @@
 %22;%0A%0A
-  private File mFile;%0A
   pr
@@ -1582,46 +1582,8 @@
 n %7B%0A
-    mFile = Mockito.mock(File.class);%0A
     
@@ -1989,25 +1989,74 @@
     
-mStream.close(
+PutObjectRequest request = Mockito.mock(PutObjectRequest.class
 );%0A
+%0A
     
@@ -2067,45 +2067,116 @@
 ito.
-verify(mManager).upload(%0A        new 
+when(request.withMetadata(Mockito.any(ObjectMetadata.class))).thenReturn(request);%0A    PowerMockito.whenNew(
 PutO
@@ -2191,94 +2191,271 @@
 uest
-(BUCKET_NAME, KEY
+.class)%0A        .withParameterTypes(String.class, String.class
 , 
-m
 File
-).withMetadata(Mockito%0A            .any(ObjectMetadata.class))
+.class)%0A        .withArguments(Matchers.eq(BUCKET_NAME), Matchers.eq(KEY), Mockito.any(File.class))%0A        .thenReturn(request);%0A%0A    mStream.close();%0A    Mockito.verify(mManager).upload(request
 );%0A 

Update s3a direct output stream test.