/*
 * Licensed to the University of California, Berkeley 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.
 */

package tachyon.underfs.s3;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.reflect.Whitebox;

/**
 * Tests for the private helper methods in {@link tachyon.underfs.s3.S3UnderFileSystem} that do
 * not require an S3 backend
 */
public class S3UnderFileSystemTest {
  private static S3UnderFileSystem sMockS3UnderFileSystem;

  @BeforeClass
  public static final void beforeClass() {
    sMockS3UnderFileSystem = PowerMockito.mock(S3UnderFileSystem.class);
    Whitebox.setInternalState(sMockS3UnderFileSystem, "mBucketName", "test-bucket");
  }

  @Test
  public void convertToFolderNameTest() throws Exception {
    String result = Whitebox.invokeMethod(sMockS3UnderFileSystem, "convertToFolderName", "test");
    Assert.assertEquals(result, "test_$folder$");
  }

  @Test
  public void getParentKeyTest() throws Exception {
    String input1 = "s3n://test-bucket/parent-is-root";
    String input2 = "s3n://test-bucket/";
    String input3 = "s3n://test-bucket/parent/child";
    String input4 = "s3n://test-bucket";
    String result1 = Whitebox.invokeMethod(sMockS3UnderFileSystem, "getParentKey", input1);
    String result2 = Whitebox.invokeMethod(sMockS3UnderFileSystem, "getParentKey", input2);
    String result3 = Whitebox.invokeMethod(sMockS3UnderFileSystem, "getParentKey", input3);
    String result4 = Whitebox.invokeMethod(sMockS3UnderFileSystem, "getParentKey", input4);

    Assert.assertEquals(result1, "s3n://test-bucket");
    Assert.assertEquals(result2, null);
    Assert.assertEquals(result3, "s3n://test-bucket/parent");
    Assert.assertEquals(result4, null);
  }

}

@@ -1490,32 +1490,60 @@
 ows Exception %7B%0A
+    String input1 = %22test%22;%0A
     String resul
@@ -1543,16 +1543,17 @@
 g result
+1
  = White
@@ -1620,17 +1620,18 @@
 e%22, 
-%22test%22
+input1
 );%0A
+%0A
     
@@ -1656,16 +1656,17 @@
 s(result
+1
 , %22test_
@@ -2457,24 +2457,24 @@
 t/parent%22);%0A
-
     Assert.a
@@ -2506,10 +2506,1347 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void isRootTest() throws Exception %7B%0A    String input1 = %22s3n://%22;%0A    String input2 = %22s3n://test-bucket%22;%0A    String input3 = %22s3n://test-bucket/%22;%0A    String input4 = %22s3n://test-bucket/file%22;%0A    String input5 = %22s3n://test-bucket/dir/file%22;%0A    String input6 = %22s3n://test-bucket-wrong/%22;%0A    boolean result1 = Whitebox.invokeMethod(sMockS3UnderFileSystem, %22isRoot%22, input1);%0A    boolean result2 = Whitebox.invokeMethod(sMockS3UnderFileSystem, %22isRoot%22, input2);%0A    boolean result3 = Whitebox.invokeMethod(sMockS3UnderFileSystem, %22isRoot%22, input3);%0A    boolean result4 = Whitebox.invokeMethod(sMockS3UnderFileSystem, %22isRoot%22, input4);%0A    boolean result5 = Whitebox.invokeMethod(sMockS3UnderFileSystem, %22isRoot%22, input5);%0A    boolean result6 = Whitebox.invokeMethod(sMockS3UnderFileSystem, %22isRoot%22, input6);%0A%0A    Assert.assertFalse(result1);%0A    Assert.assertTrue(result2);%0A    Assert.assertTrue(result3);%0A    Assert.assertFalse(result4);%0A    Assert.assertFalse(result5);%0A    Assert.assertFalse(result6);%0A  %7D%0A%0A  @Test%0A  public void stripPrefixIfPresentTest() throws Exception %7B%0A    String input1 = %22s3n://test-bucket%22;%0A    String input2 = %22s3n://test-bucket/%22;%0A    String input3 = %22s3n://test-bucket/file%22;%0A    String input4 = %22s3n://test-bucket/dir/file%22;%0A    String input4 = %22s3n://test-bucket/dir/file%22;%0A  %7D%0A%0A
 %7D%0A

Add tests for isRoot and stripPrefix.