/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 *  Licensed 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 com.google.cloud.hadoop.fs.gcs;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class GoogleHadoopFileSystemSyncableOutputIntegrationTest
    extends GoogleHadoopFileSystemIntegrationTest {

  @ClassRule
  public static NotInheritableExternalResource storageResource =
      new NotInheritableExternalResource(
          GoogleHadoopFileSystemSyncableOutputIntegrationTest.class) {
        @Override
        public void before() throws Throwable {
          GoogleHadoopFileSystemIntegrationTest.storageResource.before();
          ghfs.getConf()
              .set(
                  GoogleHadoopFileSystemConfiguration.GCS_OUTPUT_STREAM_TYPE.getKey(),
                  "SYNCABLE_COMPOSITE");
        }

        @Override
        public void after() {
          GoogleHadoopFileSystemIntegrationTest.storageResource.after();
        }
      };

  @Test @Override
  public void testHsync() throws Exception {
    internalTestHsync();
  }
}

@@ -644,16 +644,567 @@
 s.gcs;%0A%0A
+import static com.google.cloud.hadoop.fs.gcs.GoogleHadoopSyncableOutputStreamTest.hsync;%0Aimport static com.google.common.truth.Truth.assertThat;%0A%0Aimport com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystemBase.OutputStreamType;%0Aimport com.google.cloud.hadoop.gcsio.GoogleCloudStorageFileSystemIntegrationTest;%0Aimport java.net.URI;%0Aimport java.util.Arrays;%0Aimport java.util.Random;%0Aimport org.apache.hadoop.fs.FSDataInputStream;%0Aimport org.apache.hadoop.fs.FSDataOutputStream;%0Aimport org.apache.hadoop.fs.FileStatus;%0Aimport org.apache.hadoop.fs.Path;%0A
 import o
@@ -1223,16 +1223,16 @@
 ssRule;%0A
-
 import o
@@ -1935,17 +1935,33 @@
         
-%22
+OutputStreamType.
 SYNCABLE
@@ -1970,17 +1970,23 @@
 OMPOSITE
-%22
+.name()
 );%0A     
@@ -2139,16 +2139,18 @@
 %0A  @Test
+%0A 
  @Overri
@@ -2197,16 +2197,16 @@
 ption %7B%0A
-
     inte
@@ -2225,11 +2225,1362 @@
 c();%0A  %7D
+%0A%0A  @Test%0A  public void testSyncComposite() throws Exception %7B%0A    URI path = GoogleCloudStorageFileSystemIntegrationTest.getTempFilePath();%0A    Path hadoopPath = ghfsHelper.castAsHadoopPath(path);%0A%0A    // test composing of 5 1-byte writes into 5-byte object%0A    byte%5B%5D expected = new byte%5B5%5D;%0A    new Random().nextBytes(expected);%0A%0A    try (FSDataOutputStream fout = ghfs.create(hadoopPath)) %7B%0A      for (int i = 0; i %3C expected.length; ++i) %7B%0A        fout.write(expected, i, 1);%0A        hsync(fout);%0A%0A        // validate partly composed data%0A        int composedLength = i + 1;%0A        if (composedLength %25 2 == 0) %7B%0A          FileStatus status = ghfs.getFileStatus(hadoopPath);%0A          assertThat(status.getLen()).isEqualTo(composedLength);%0A%0A          byte%5B%5D actualComposed = new byte%5BcomposedLength%5D;%0A          try (FSDataInputStream fin = ghfs.open(hadoopPath)) %7B%0A            fin.readFully(0, actualComposed);%0A          %7D%0A          assertThat(actualComposed).isEqualTo(Arrays.copyOf(expected, composedLength));%0A        %7D%0A      %7D%0A    %7D%0A%0A    FileStatus status = ghfs.getFileStatus(hadoopPath);%0A    assertThat(status.getLen()).isEqualTo(expected.length);%0A%0A    byte%5B%5D actual = new byte%5Bexpected.length%5D;%0A    try (FSDataInputStream fin = ghfs.open(hadoopPath)) %7B%0A      fin.readFully(0, actual);%0A    %7D%0A%0A    assertThat(actual).isEqualTo(expected);%0A  %7D
 %0A%7D%0A

Add composite sync integration test.