//    Copyright 2018 Google Inc.
//
//    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
//
//        https://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.fhir.stu3;

import static com.google.common.truth.Truth.assertThat;

import com.google.fhir.stu3.proto.Date;
import java.time.ZoneId;
import java.time.ZoneOffset;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link DateWrapper}. */
@RunWith(JUnit4.class)
public final class DateWrapperTest {

  @Test
  public void parseYear() {
    Date parsed = new DateWrapper("1971", ZoneOffset.UTC).getWrapped();
    Date expected =
        Date.newBuilder()
            .setValueUs(31536000000000L)
            .setPrecision(Date.Precision.YEAR)
            .setTimezone("Z")
            .build();
    assertThat(parsed).isEqualTo(expected);

    parsed = new DateWrapper("1971", ZoneId.of("Australia/Sydney")).getWrapped();
    expected =
        Date.newBuilder()
            .setValueUs(31500000000000L)
            .setPrecision(Date.Precision.YEAR)
            .setTimezone("Australia/Sydney")
            .build();
    assertThat(parsed).isEqualTo(expected);
  }

  @Test
  public void parseYearMonth() {
    Date parsed = new DateWrapper("1970-02", ZoneOffset.UTC).getWrapped();
    Date expected =
        Date.newBuilder()
            .setValueUs(2678400000000L)
            .setPrecision(Date.Precision.MONTH)
            .setTimezone("Z")
            .build();
    assertThat(parsed).isEqualTo(expected);

    parsed = new DateWrapper("1970-02", ZoneId.of("Australia/Sydney")).getWrapped();
    expected =
        Date.newBuilder()
            .setValueUs(2642400000000L)
            .setPrecision(Date.Precision.MONTH)
            .setTimezone("Australia/Sydney")
            .build();
    assertThat(parsed).isEqualTo(expected);
  }

  @Test
  public void parseYearMonthDay() {
    Date parsed = new DateWrapper("1970-01-01", ZoneOffset.UTC).getWrapped();
    Date expected =
        Date.newBuilder().setValueUs(0).setPrecision(Date.Precision.DAY).setTimezone("Z").build();
    assertThat(parsed).isEqualTo(expected);

    parsed = new DateWrapper("1970-01-01", ZoneId.of("Australia/Sydney")).getWrapped();
    expected =
        Date.newBuilder()
            .setValueUs(-36000000000L)
            .setPrecision(Date.Precision.DAY)
            .setTimezone("Australia/Sydney")
            .build();
    assertThat(parsed).isEqualTo(expected);
  }

  @Test
  public void printYear() {
    Date input =
        Date.newBuilder()
            .setValueUs(0)
            .setPrecision(Date.Precision.YEAR)
            .setTimezone("Z")
            .build();
    assertThat(new DateWrapper(input).toString()).isEqualTo("1970");
    assertThat(new DateWrapper(input, ZoneId.of("Australia/Sydney")).toString()).isEqualTo("1970");

    input = input.toBuilder().setValueUs(-36000000000L).setTimezone("Australia/Sydney").build();
    assertThat(new DateWrapper(input).toString()).isEqualTo("1970");
    assertThat(new DateWrapper(input, ZoneId.of("Australia/Sydney")).toString()).isEqualTo("1970");
  }

  @Test
  public void printYearMonth() {
    Date input =
        Date.newBuilder()
            .setValueUs(0)
            .setPrecision(Date.Precision.MONTH)
            .setTimezone("Z")
            .build();
    assertThat(new DateWrapper(input).toString()).isEqualTo("1970-01");
    assertThat(new DateWrapper(input, ZoneId.of("Australia/Sydney")).toString())
        .isEqualTo("1970-01");

    input = input.toBuilder().setValueUs(-36000000000L).setTimezone("Australia/Sydney").build();
    assertThat(new DateWrapper(input).toString()).isEqualTo("1970-01");
    assertThat(new DateWrapper(input, ZoneId.of("Australia/Sydney")).toString())
        .isEqualTo("1970-01");
  }

  @Test
  public void printYearMonthDay() {
    Date input =
        Date.newBuilder().setValueUs(0).setPrecision(Date.Precision.DAY).setTimezone("Z").build();
    assertThat(new DateWrapper(input).toString()).isEqualTo("1970-01-01");
    assertThat(new DateWrapper(input, ZoneId.of("Australia/Sydney")).toString())
        .isEqualTo("1970-01-01");

    input = input.toBuilder().setValueUs(-36000000000L).setTimezone("Australia/Sydney").build();
    assertThat(new DateWrapper(input).toString()).isEqualTo("1970-01-01");
    assertThat(new DateWrapper(input, ZoneId.of("Australia/Sydney")).toString())
        .isEqualTo("1970-01-01");
  }
}

@@ -2940,32 +2940,637 @@
 expected);%0A  %7D%0A%0A
+  @Test%0A  public void parseYearMonthDayWithOffset() %7B%0A    Date parsed = new DateWrapper(%221970-01-01%22, ZoneOffset.UTC).getWrapped();%0A    Date expected =%0A        Date.newBuilder().setValueUs(0).setPrecision(Date.Precision.DAY).setTimezone(%22Z%22).build();%0A    assertThat(parsed).isEqualTo(expected);%0A%0A    parsed = new DateWrapper(%221970-01-01%22, ZoneId.of(%22-05:00%22)).getWrapped();%0A    expected =%0A        Date.newBuilder()%0A            .setValueUs(18000000000L)%0A            .setPrecision(Date.Precision.DAY)%0A            .setTimezone(%22-05:00%22)%0A            .build();%0A    assertThat(parsed).isEqualTo(expected);%0A  %7D%0A%0A
   @Test%0A  public

Document Java parser behavior for parsing dates when the default timezone is a time offset.