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

package ucar.nc2.ncml;

import junit.framework.TestCase;
import org.junit.experimental.categories.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ucar.ma2.Array;
import ucar.ma2.DataType;
import ucar.ma2.InvalidRangeException;
import ucar.nc2.NCdumpW;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.unidata.util.test.Assert2;
import ucar.unidata.util.test.TestDir;
import ucar.unidata.util.test.category.NeedsCdmUnitTest;
import java.io.IOException;
import java.io.StringReader;
import java.lang.invoke.MethodHandles;

/**
 * Test aggregation where timeUnitsChange='true'
 *
 * @author caron
 */
@Category(NeedsCdmUnitTest.class)
public class TestOffAggExistingTimeUnitsChange extends TestCase {
  private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

  public TestOffAggExistingTimeUnitsChange(String name) {
    super(name);
  }
  // String location = "file:R:/testdata2/ncml/nc/narr/narr.ncml";

  public void testNamExtract() throws IOException, InvalidRangeException {
    String location = TestDir.cdmUnitTestDir + "ncml/nc/namExtract/test_agg.ncml";
    logger.debug(" TestOffAggExistingTimeUnitsChange.open {}", location);

    NetcdfFile ncfile = NetcdfDataset.openFile(location, null);

    Variable v = ncfile.findVariable("time");
    assert v != null;
    assert v.getDataType() == DataType.DOUBLE;

    String units = v.getUnitsString();
    assert units != null;
    assert units.equals("hours since 2006-09-25T06:00:00Z");

    int count = 0;
    Array data = v.read();
    logger.debug(NCdumpW.toString(data, "time", null));

    while (data.hasNext()) {
      Assert2.assertNearlyEquals(data.nextInt(), (count + 1) * 3);
      count++;
    }

    ncfile.close();
  }

  public void testNarrGrib() throws IOException, InvalidRangeException {
    String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
        + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2'>\n"
        + " <aggregation type='joinExisting' dimName='time' timeUnitsChange='true' >\n"
        + "  <netcdf location='narr-a_221_20070411_0000_000.grb'/>\n"
        + "  <netcdf location='narr-a_221_20070411_0300_000.grb'/>\n"
        + "  <netcdf location='narr-a_221_20070411_0600_000.grb'/>\n" + " </aggregation>\n" + "</netcdf>";

    String location = "file:" + TestDir.cdmUnitTestDir + "ncml/nc/narr/";
    logger.debug(" TestOffAggExistingTimeUnitsChange.testNarrGrib={}\n{}", location, ncml);
    NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), location, null);

    Variable v = ncfile.findVariable("time");
    assert v != null;
    assert v.getDataType() == DataType.DOUBLE;

    String units = v.getUnitsString();
    assert units != null;
    assert units.equalsIgnoreCase("Hour since 2007-04-11T00:00:00Z") : units; // Hour since 2007-04-11T00:00:00.000Z

    int count = 0;
    Array data = v.read();
    logger.debug(NCdumpW.toString(data, "time", null));

    while (data.hasNext()) {
      assert data.nextInt() == count * 3;
      count++;
    }

    ncfile.close();
  }
}

@@ -148,16 +148,168 @@
 .ncml;%0A%0A
+import static com.google.common.truth.Truth.assertThat;%0Aimport java.io.IOException;%0Aimport java.io.StringReader;%0Aimport java.lang.invoke.MethodHandles;%0A
 import j
@@ -333,16 +333,16 @@
 stCase;%0A
-
 import o
@@ -494,47 +494,8 @@
 pe;%0A
-import ucar.ma2.InvalidRangeException;%0A
 impo
@@ -608,16 +608,55 @@
 ataset;%0A
+import ucar.nc2.time.CalendarDateUnit;%0A
 import u
@@ -785,104 +785,8 @@
 est;
-%0Aimport java.io.IOException;%0Aimport java.io.StringReader;%0Aimport java.lang.invoke.MethodHandles;
 %0A%0A/*
@@ -960,16 +960,17 @@
 tCase %7B%0A
+%0A
   privat
@@ -1260,39 +1260,16 @@
 xception
-, InvalidRangeException
  %7B%0A    S
@@ -1524,32 +1524,33 @@
 riable(%22time%22);%0A
+%0A
     assert v != 
@@ -1557,33 +1557,37 @@
 null;%0A    assert
- 
+That(
 v.getDataType() 
@@ -1577,36 +1577,44 @@
 (v.getDataType()
- == 
+).isEqualTo(
 DataType.DOUBLE;
@@ -1604,32 +1604,33 @@
 (DataType.DOUBLE
+)
 ;%0A%0A    String un
@@ -1689,62 +1689,198 @@
     
-assert units.equals(%22hours since 2006-09-25T06:00:00Z%22
+CalendarDateUnit expectedCalendarDateUnit = CalendarDateUnit.of(null, %22hours since 2006-09-25T06:00:00Z%22);%0A    assertThat(units).ignoringCase().isEqualTo(expectedCalendarDateUnit.getUdUnit()
 );%0A%0A
@@ -2125,16 +2125,16 @@
 );%0A  %7D%0A%0A
+
   public
@@ -2176,31 +2176,8 @@
 tion
-, InvalidRangeException
  %7B%0A 

Fix failing Jenkins test