/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2020 . All rights reserved.
 *
 */
package net.northking.cctp.common.util;

import org.junit.Test;

import java.text.ParseException;
import java.util.Calendar;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


public class DateUtilTest
{

  @Test
  public void toYearMonthDay()
  {
    assertEquals("", DateUtil.toYearMonthDay(null));

    Calendar calendar = Calendar.getInstance();
    calendar.set(2020, 4, 10);
    assertEquals("2020-05-10", DateUtil.toYearMonthDay(calendar.getTime()));
  }

  @Test
  public void toYearMonthDay24HourMinSecond()
  {
    assertEquals("", DateUtil.toYearMonthDay24HourMinSecond(null));

    Calendar calendar = Calendar.getInstance();
    calendar.set(2020, 4, 10, 10, 10, 10);
    assertEquals("2020-05-10 10:10:10", DateUtil.toYearMonthDay24HourMinSecond(calendar.getTime()));
  }

  @Test
  public void toYearMonthDay24HourMinSecondAndMicroSecond()
  {
    assertEquals("", DateUtil.toYearMonthDay24HourMinSecondAndMicroSecond(null));

    Calendar calendar = Calendar.getInstance();
    calendar.set(2020, 4, 10, 10, 10, 10);
    assertTrue(DateUtil.toYearMonthDay24HourMinSecondAndMicroSecond(calendar.getTime()).startsWith("2020-05-10 10:10:10 "));
  }

  @Test
  public void fromYearMonthDay() throws ParseException
  {
    assertEquals(null, DateUtil.fromYearMonthDay(""));

    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2020, 4, 10);
    assertEquals(calendar.getTime().getTime(), DateUtil.fromYearMonthDay("2020-05-10").getTime());

  }

  @Test
  public void fromYearMonthDay24HourMinSecond() throws ParseException
  {
    assertEquals(null, DateUtil.fromYearMonthDay24HourMinSecond(""));

    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2020, 4, 10, 10, 10, 10);
    assertEquals(calendar.getTimeInMillis(), DateUtil.fromYearMonthDay24HourMinSecond("2020-05-10 10:10:10").getTime());
  }

  @Test
  public void fromYearMonthDay24HourMinSecondAndMicroSecond() throws ParseException
  {
    assertEquals(null, DateUtil.fromYearMonthDay24HourMinSecondAndMicroSecond(""));

    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2020, 4, 10, 10, 10, 10);
    assertEquals(calendar.getTimeInMillis(), DateUtil.fromYearMonthDay24HourMinSecondAndMicroSecond("2020-05-10 10:10:10 000").getTime());

  }

  @Test
  public void parse() throws ParseException
  {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2020, 4, 10, 0, 0, 0);
    assertEquals(calendar.getTimeInMillis(), DateUtil.parse("2020-05-10 00:00:00 000").getTime());
  }

  @Test
  public void testParse() throws ParseException
  {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2020, 4, 10, 0, 0, 0);
    assertEquals(calendar.getTimeInMillis(), DateUtil.parse("2020-05-10 00:00:00 000").getTime());
  }

}