/*
 * Copyright (c) 1998-2019 University Corporation for Atmospheric Research/Unidata
 * See LICENSE for license information.
 */

package ucar.unidata.io.s3;

import static com.google.common.truth.Truth.assertThat;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import software.amazon.awssdk.regions.Region;
import ucar.ma2.Array;
import ucar.ma2.InvalidRangeException;
import ucar.ma2.Section;
import ucar.nc2.Dimension;
import ucar.nc2.NetcdfFile;
import ucar.nc2.NetcdfFiles;
import ucar.nc2.Variable;
import ucar.unidata.util.test.category.NeedsExternalResource;

@Category(NeedsExternalResource.class)
public class TestS3Read {

  @Test
  public void testFullS3Read() throws IOException {
    String region = Region.US_EAST_1.toString();
    String bucket = "noaa-goes16";
    String key =
        "ABI-L1b-RadC/2019/363/21/OR_ABI-L1b-RadC-M6C16_G16_s20193632101189_e20193632103574_c20193632104070.nc";
    String s3uri = "s3://" + bucket + "/" + key;

    System.setProperty("aws.region", region);
    try (NetcdfFile ncfile = NetcdfFiles.open(s3uri)) {

      Dimension x = ncfile.findDimension("x");
      Dimension y = ncfile.findDimension("y");
      assertThat(x).isNotNull();
      assertThat(y).isNotNull();

      Variable radiance = ncfile.findVariable("Rad");
      Assert.assertNotNull(radiance);

      // read full array
      Array array = radiance.read();
      assertThat(array.getRank()).isEqualTo(2);

      // check shape of array is the same as the shape of the variable
      int[] variableShape = radiance.getShape();
      int[] arrayShape = array.getShape();
      assertThat(arrayShape).isEqualTo(arrayShape);
    } finally {
      System.clearProperty("aws.region");
    }
  }

  @Test
  public void testPartialS3Read() throws IOException, InvalidRangeException {
    String region = Region.US_EAST_1.toString();
    String bucket = "noaa-goes16";
    String key =
        "ABI-L1b-RadC/2019/363/21/OR_ABI-L1b-RadC-M6C16_G16_s20193632101189_e20193632103574_c20193632104070.nc";
    String s3uri = "s3://" + bucket + "/" + key;

    System.setProperty("aws.region", region);
    try (NetcdfFile ncfile = NetcdfFiles.open(s3uri)) {

      Variable radiance = ncfile.findVariable("Rad");
      Assert.assertNotNull(radiance);

      // read part of the array
      Section section = new Section("(100:200:2,10:20:1)");
      Array array = radiance.read(section);
      assertThat(array.getRank()).isEqualTo(2);

      // check shape of array is the same as the shape of the section
      assertThat(array.getShape()).isEqualTo(section.getShape());
    } finally {
      System.clearProperty("aws.region");
    }
  }
}

@@ -575,24 +575,133 @@
 2.Variable;%0A
+import ucar.nc2.constants.CF;%0Aimport ucar.nc2.dataset.NetcdfDataset;%0Aimport ucar.nc2.dataset.NetcdfDatasets;%0A
 import ucar.
@@ -750,16 +750,16 @@
 source;%0A
-
 %0A@Catego
@@ -849,24 +849,34 @@
 stFullS3Read
+NetcdfFile
 () throws IO
@@ -1938,20 +1938,26 @@
 artialS3
-Read
+NetcdfFile
 () throw
@@ -2710,16 +2710,16 @@
 section%0A
-
       as
@@ -2843,11 +2843,920 @@
    %7D%0A  %7D
+%0A%0A  @Test%0A  public void testFullS3ReadNetcdfDatasets() throws IOException %7B%0A    String region = Region.US_EAST_1.toString();%0A    String bucket = %22noaa-goes16%22;%0A    String key =%0A        %22ABI-L1b-RadC/2019/363/21/OR_ABI-L1b-RadC-M6C16_G16_s20193632101189_e20193632103574_c20193632104070.nc%22;%0A    String s3uri = %22s3://%22 + bucket + %22/%22 + key;%0A%0A    System.setProperty(%22aws.region%22, region);%0A    try (NetcdfDataset ds = NetcdfDatasets.openDataset(s3uri)) %7B%0A      String partialConventionValue = %22CF-1.%22;%0A      // read conventions string%0A      String conventions = ds.getRootGroup().attributes().findAttValueIgnoreCase(CF.CONVENTIONS, %22%22);%0A%0A      // check that the file was read the CF convention builder%0A      assertThat(conventions).startsWith(partialConventionValue);%0A      assertThat(ds.getConventionUsed()).startsWith(partialConventionValue);%0A%0A    %7D finally %7B%0A      System.clearProperty(%22aws.region%22);%0A    %7D%0A  %7D
 %0A%7D%0A

Add S3 through NetcdfDataset test