package io.github.benas.jpopulator.randomizers;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

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

/**
 * Test class for {@link io.github.benas.jpopulator.randomizers.DateStringRandomizer}.
 *
 * @author Nikola Milivojevic (0dziga0@gmail.com)
 */
public class DateStringRandomizerTest {

    private static final String DATE_FORMAT = "YYYY-MM-dd";
    private static final String DEFAULT_DATE_FORMAT = "E M dd hh:mm:ss a zzz";
    
	private DateStringRandomizer dateStringRandomizer;

    private Date today, tomorrow;

    @Before
    public void setUp() throws Exception {
        today = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);
        tomorrow = calendar.getTime();
    }

    @org.junit.Test
    public void returnedStringShouldBeDate() {
        dateStringRandomizer = new DateStringRandomizer();
        String randomDate = dateStringRandomizer.getRandomValue();
        Assert.assertNotNull(randomDate);
        try {
			convertToDate(randomDate, DEFAULT_DATE_FORMAT);
		} catch (ParseException e) {
			Assert.fail("Returned date doesn't have a proper format " + randomDate);
		}
    }
    
    @org.junit.Test
    public void returnedStringDateShouldBeInRange() throws ParseException {
        dateStringRandomizer = new DateStringRandomizer(today, tomorrow);
        Date actual = convertToDate(dateStringRandomizer.getRandomValue(), DEFAULT_DATE_FORMAT);
        Assert.assertNotNull(actual);
        Assert.assertTrue(today.compareTo(actual) * tomorrow.compareTo(actual) > 0);
    }

    @org.junit.Test
    public void returnedStringDateShouldHaveSpecifiedFormat() throws Exception {
    	dateStringRandomizer = new DateStringRandomizer(DATE_FORMAT, today, tomorrow);
        String randomDate = dateStringRandomizer.getRandomValue();
        Assert.assertNotNull(randomDate);
        System.out.println(randomDate);
        Assert.assertTrue(randomDate.matches("\\d{4}-\\d{2}-\\d{2}"));
    }

    @After
    public void tearDown() throws Exception {
        dateStringRandomizer = null;
        System.gc();
    }
    
    private Date convertToDate(String date, String format) throws ParseException {
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.parse(date);
    }
    
    private String convertToString (Date date, String format) {
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

}

@@ -568,14 +568,13 @@
 z%22;%0A
+%0A
     
-%0A%09
 priv
@@ -1126,19 +1126,28 @@
   try %7B%0A
-%09%09%09
+            
 convertT
@@ -1186,18 +1186,24 @@
 ORMAT);%0A
-%09%09
+        
 %7D catch 
@@ -1227,11 +1227,20 @@
 ) %7B%0A
-%09%09%09
+            
 Asse
@@ -1312,18 +1312,24 @@
 e);%0A
-%09%09
+        
 %7D%0A    %7D%0A
     
@@ -1320,29 +1320,25 @@
     %7D%0A    %7D%0A
-    
 %0A
+
     @org.jun
@@ -1827,17 +1827,20 @@
 n %7B%0A    
-%09
+    
 dateStri
@@ -2023,48 +2023,8 @@
 e);%0A
-        System.out.println(randomDate);%0A
     
@@ -2210,37 +2210,33 @@
 tem.gc();%0A    %7D%0A
-    
 %0A
+
     private Date
@@ -2302,25 +2302,28 @@
 ption %7B%0A    
-%09
+    
 SimpleDateFo
@@ -2430,200 +2430,8 @@
    %7D
-%0A    %0A    private String convertToString (Date date, String format) %7B%0A    %09SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);%0A        return simpleDateFormat.format(date);%0A    %7D
 %0A%0A%7D%0A

remove System.out.println() remove unused method