package org.kuali.kfs.pdp.batch.service.impl;

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

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import org.easymock.EasyMock;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kuali.kfs.krad.util.ObjectUtils;
import org.kuali.kfs.pdp.businessobject.AutoCheckFormat;
import org.kuali.kfs.pdp.businessobject.CustomerProfile;
import org.kuali.kfs.pdp.businessobject.DisbursementNumberRange;
import org.kuali.kfs.pdp.businessobject.FormatSelection;
import org.kuali.rice.core.api.datetime.DateTimeService;
import org.kuali.rice.location.api.campus.CampusService;

public class AutoCheckFormatServiceImplTest {

    @TestSubject
    private AutoCheckFormatServiceImpl autoCheckFormatServ = new AutoCheckFormatServiceImpl();
    
    @Mock
    private static FormatSelection fs = new FormatSelection();
    
    @BeforeClass
    public static void setUp() throws Exception{
        fs = EasyMock.createMock(FormatSelection.class);
    }
    
    @Test
    public void testNullCustomerListInFormatSelectionWhenGeneratingFomatReadyCustomerList() throws Exception {
        EasyMock.expect(fs.getCustomerList()).andReturn(null);
        EasyMock.replay(fs);
        
        assertNotNull(autoCheckFormatServ.generateListOfCustomerProfilesReadyForFormat(fs));
        EasyMock.reset(fs);
    }
    
    @Test
    public void testGeneratingFomatReadyCustomersListWithFormatSelectionCustomersWithWrongCampus() throws Exception {
        CustomerProfile cp1 = new CustomerProfile();
        cp1.setDefaultPhysicalCampusProcessingCode("IR");
        
        ArrayList<CustomerProfile> customerList = new ArrayList<CustomerProfile>();
        customerList.add(cp1);
        
        EasyMock.expect(fs.getCustomerList()).andReturn(customerList);
        EasyMock.expect(fs.getCampus()).andReturn("BL");
        EasyMock.replay(fs);
        
        //there is only only item in the list and it is marked for format as FALSE
        assertFalse(autoCheckFormatServ.generateListOfCustomerProfilesReadyForFormat(fs).get(0).isSelectedForFormat());
        
        EasyMock.reset(fs);
    }
    
    @Test
    public void testGeneratingFomatReadyCustomersListWithFormatSelectionCustomersWithMatchingCampuses() throws Exception {
        CustomerProfile cp1 = new CustomerProfile();
        cp1.setDefaultPhysicalCampusProcessingCode("BL");
        
        ArrayList<CustomerProfile> customerList = new ArrayList<CustomerProfile>();
        customerList.add(cp1);
        
        EasyMock.expect(fs.getCustomerList()).andReturn(customerList);
        EasyMock.expect(fs.getCampus()).andReturn("BL");
        EasyMock.replay(fs);
        
        //there is only only item in the list and it is marked for format as TRUE
        assertTrue(autoCheckFormatServ.generateListOfCustomerProfilesReadyForFormat(fs).get(0).isSelectedForFormat());
        
        EasyMock.reset(fs);
    }
    
    @Test
    public void testCreationOfAutoCheckFormatWhenFormatSelectionHasStartDate() throws Exception {
        //set format selection to have date already set
        EasyMock.expect(fs.getStartDate()).andReturn(new Date()).times(3);
        EasyMock.replay(fs);
        
        DateTimeService dtService = EasyMock.createMock(DateTimeService.class);
        EasyMock.expect(dtService.toDateTimeString(fs.getStartDate())).andReturn("date time");
        EasyMock.replay(dtService);
        autoCheckFormatServ.setDateTimeService(dtService);
        
        // the method should return a null
        assertTrue(ObjectUtils.isNull(autoCheckFormatServ.createAutoCheckFormat(fs)));
        
        EasyMock.reset(fs);
    }
    
    @Test
	public void testProcessChecksByCustomerProfileWhenProfileIDisNull() throws Exception {
    	CampusService cs = EasyMock.createMock(CampusService.class);
    	EasyMock.expect(cs.findAllCampuses()).andReturn(new ArrayList<Campus>())
    	
        EasyMock.expect(fs.getCampusService()).andReturn(cs);
        EasyMock.replay(fs);
    	
    	boolean results = autoCheckFormatServ.processChecksByCustomerProfile(null);
		
		assertTrue(results);
		
	}
    
    @Test
    public void testCreateAutoCheckFormatHasSetFormatSelectionData() throws Exception {
        // start date is null, so can proceed
        EasyMock.expect(fs.getStartDate()).andReturn(null);
        EasyMock.expect(fs.getCampus()).andReturn("BL");
        
        DateTimeService dtService = EasyMock.createMock(DateTimeService.class);
        long currentTimeMillis = System.currentTimeMillis();
        EasyMock.expect(dtService.getCurrentTimestamp()).andReturn(new Timestamp(currentTimeMillis));
        EasyMock.expect(dtService.toDateString(new Timestamp(currentTimeMillis))).andReturn(new Date().toString());
        EasyMock.replay(dtService);
        autoCheckFormatServ.setDateTimeService(dtService);
        
        EasyMock.expect(fs.getRangeList()).andReturn(new ArrayList<DisbursementNumberRange>());
        EasyMock.expect(fs.getCustomerList()).andReturn(null);
        EasyMock.replay(fs);
        
        AutoCheckFormat autoCheckFormat = autoCheckFormatServ.createAutoCheckFormat(fs);
        
        assertEquals(autoCheckFormat.getCampus(), "BL");
        assertEquals(autoCheckFormat.getPaymentTypes(), "all");
        assertEquals(autoCheckFormat.getRanges().size(), 0);
        assertEquals(autoCheckFormat.getCustomers().size(), 0);
        
        EasyMock.reset(fs);
    }

    
}

@@ -436,24 +436,81 @@
 junit.Test;%0A
+import org.kuali.kfs.krad.service.BusinessObjectService;%0A
 import org.k
@@ -826,32 +826,82 @@
 ateTimeService;%0A
+import org.kuali.rice.location.api.campus.Campus;%0A
 import org.kuali
@@ -4232,71 +4232,30 @@
 %3E())
+;
 %0A    %09
-%0A        EasyMock.expect(fs.getCampusService()).andReturn
+EasyMock.replay
 (cs)
@@ -4260,37 +4260,61 @@
 s);%0A    
+%09%0A
     
-EasyMock.replay(f
+%09autoCheckFormatServ.setCampusService(c
 s);%0A    
@@ -4423,17 +4423,548 @@
 ults);%0A%09
-%09
+%7D%0A%0A    @Test%0A%09public void testProcessChecksByCustomerProfileWhenNoCustomerProfileExist() throws Exception %7B%0A    %09String profileId = %2210001%22;%0A%0A    %09BusinessObjectService bos = EasyMock.createMock(BusinessObjectService.class);%0A%09%09EasyMock.expect(bos.findBySinglePrimaryKey(CustomerProfile.class, profileId)).andReturn(null);%0A    %09EasyMock.replay(bos);%0A    %09%0A    %09autoCheckFormatServ.setBusinessObjectService(bos);%0A    %09%0A    %09boolean results = autoCheckFormatServ.processChecksByCustomerProfile(profileId);%0A    %09%0A%09%09assertFalse(results);
 %0A%09%7D%0A    

add additional tests cases to validate for new format job by customer profile