//////////////////////////////////////////////////////////////////////////////
// 
//                    Copyright 2019, Cornutum Project
//                             www.cornutum.org
//
//////////////////////////////////////////////////////////////////////////////

package org.cornutum.tcases.openapi;

import org.junit.Test;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import static java.util.Calendar.*;

/**
 * Runs tests for {@link FormattedString} methods.
 */
public class FormattedStringTest
  {
  @Test
  public void whenFormatDate()
    {
    // Given...
    Date date =
      new Calendar.Builder()
      .setDate( 2019, 0, 1)
      .build()
      .getTime();
    
    // When...
    FormattedString formatted = FormattedString.of( "date", date);
    
    // Then...
    assertThat( "Date string", formatted.toString(), is( "2019-01-01"));
    }
  
  @Test
  public void whenFormatDateTime()
    {
    // Given...
    Calendar calendar =
      new Calendar.Builder()
      .setDate( 2019, 7, 9)
      .setTimeOfDay( 23, 59, 59, 876)
      .setTimeZone( TimeZone.getTimeZone( "GMT"))
      .build();

    Date date = calendar.getTime();
    int localZoneOffset = TimeZone.getDefault().getOffset( System.currentTimeMillis()) / 60000;
      
    // When...
    FormattedString formatted = FormattedString.of( "date-time", date);
    
    // Then...
    assertThat( "Date string", formatted.toString(), is( toDateTimeString( calendar, localZoneOffset)));
    }
  
  @Test
  public void whenFormatByte()
    {
    // Given...
    byte[] bytes = "Hello, world".getBytes();
    
    // When...
    FormattedString formatted = FormattedString.of( "byte", bytes);
    
    // Then...
    assertThat( "Bytes", formatted.toString(), is( "SGVsbG8sIHdvcmxk"));
    }

  /**
   * Returns the expected string representation of the given time value, using the given local time zone offset.
   */
  private String toDateTimeString( Calendar calendar, int localZoneOffset)
    {
    int zonedHrMin = 60 * calendar.get( HOUR_OF_DAY) + calendar.get( MINUTE) + localZoneOffset;
    int zonedHr = zonedHrMin / 60;
    int zonedMin = zonedHrMin % 60;

    return
      String.format(
        "%s-%02d-%02dT%02d:%02d:%02d.%s%s%02d:%02d",
        calendar.get( YEAR),
        calendar.get( MONTH) + 1,
        calendar.get( DAY_OF_MONTH),
        zonedHr,
        zonedMin,
        calendar.get( SECOND),
        calendar.get( MILLISECOND),
        localZoneOffset >= 0? "+" : "-",
        Math.abs( localZoneOffset / 60),
        Math.abs( localZoneOffset % 60));

    }

  }

@@ -1264,104 +1264,8 @@
 ();%0A
-    int localZoneOffset = TimeZone.getDefault().getOffset( System.currentTimeMillis()) / 60000;%0A
     
@@ -1461,25 +1461,8 @@
 ndar
-, localZoneOffset
 )));
@@ -1847,48 +1847,8 @@
 alue
-, using the given local time zone offset
 .%0A  
@@ -1907,201 +1907,227 @@
 ndar
-, int localZoneOffset)%0A    %7B%0A    int zonedHrMin = 60 * calendar.get( HOUR_OF_DAY) + calendar.get( MINUTE) + localZoneOffset;%0A    int zonedHr = zonedHrMin / 60;%0A    int zonedMin = zonedHrMin %25 6
+)%0A    %7B%0A    Calendar localCalendar = (Calendar) calendar.clone();%0A    localCalendar.setTimeZone( TimeZone.getDefault());%0A    %0A    int localZoneOffset = TimeZone.getDefault().getOffset( calendar.getTimeInMillis()) / 6000
 0;%0A%0A
@@ -2211,33 +2211,38 @@
 :%2502d%22,%0A        
-c
+localC
 alendar.get( YEA
@@ -2245,35 +2245,40 @@
  YEAR),%0A        
+lo
 cal
+Cal
 endar.get( MONTH
@@ -2284,33 +2284,38 @@
 H) + 1,%0A        
-c
+localC
 alendar.get( DAY
@@ -2338,33 +2338,75 @@
     
-zonedHr,%0A        zonedMin
+localCalendar.get( HOUR_OF_DAY),%0A        localCalendar.get( MINUTE)
 ,%0A  
@@ -2403,33 +2403,38 @@
 INUTE),%0A        
-c
+localC
 alendar.get( SEC
@@ -2447,17 +2447,22 @@
         
-c
+localC
 alendar.
@@ -2604,20 +2604,18 @@
 %25 60));%0A
-%0A
     %7D%0A
-%0A
   %7D%0A

Fix issue #91