package org.itevents.service.transactional;

import com.google.common.collect.Multimap;
import org.itevents.dao.EventDao;
import org.itevents.model.Event;
import org.itevents.model.User;
import org.itevents.service.mail.MailReminderAboutEventService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.inject.Inject;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.assertFalse;

/**
 * Created by ramax on 11/7/15.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:applicationContext.xml")
public class MailReminderAboutEventServiceTest {

    @Inject
    private MailReminderAboutEventService mailReminderAboutEventService;

    @Inject
    EventDao eventDao;

    private static final int MILLISECONDS_TO_DAYS = 24 * 60 * 60 * 1000;


    private int getDaysTillEventTest() {
        long result;
        // Get difference of days between today and date of event with id -1
        result = (eventDao.getEvent(-1).getEventDate().getTime()) //day of event with id -1
                - (new Date().getTime()); //today
        return (int) (result / MILLISECONDS_TO_DAYS) + 1;
    }

    @Test
    public void getEventByDaysTillEventTest() {
        List<Event> eventsByDaysTillEvent = mailReminderAboutEventService.getEventsByDaysTillEvent(getDaysTillEventTest());
        for(Event event : eventsByDaysTillEvent){
            System.out.println("Event: "+event.getTitle());
        }
        System.out.println(eventsByDaysTillEvent.size());
        assertFalse(eventsByDaysTillEvent.isEmpty());
    }

    @Test
    public void getUsersAndEventsByDaysTillEventTest() {
        Multimap<User, Event> usersAndEvents = mailReminderAboutEventService.getUsersAndEventsByDaysTillEvent(getDaysTillEventTest());
        for (User userWhoGoToNearestEvent : usersAndEvents.keySet()) {
            Iterator nearestEventOfUserIterator = usersAndEvents.get(userWhoGoToNearestEvent).iterator();
            while(nearestEventOfUserIterator.hasNext()) {
                Event nowEvent = (Event)nearestEventOfUserIterator.next();
                System.out.println(userWhoGoToNearestEvent.getLogin()+"--"+nowEvent.getTitle());
            }
        }
        assertFalse(usersAndEvents.isEmpty());
    }
}
@@ -490,35 +490,8 @@
 te;%0A
-import java.util.Iterator;%0A
 impo
@@ -1485,186 +1485,8 @@
 ));%0A
-        for(Event event : eventsByDaysTillEvent)%7B%0A            System.out.println(%22Event: %22+event.getTitle());%0A        %7D%0A        System.out.println(eventsByDaysTillEvent.size());%0A
     
@@ -1748,439 +1748,8 @@
 ));%0A
-        for (User userWhoGoToNearestEvent : usersAndEvents.keySet()) %7B%0A            Iterator nearestEventOfUserIterator = usersAndEvents.get(userWhoGoToNearestEvent).iterator();%0A            while(nearestEventOfUserIterator.hasNext()) %7B%0A                Event nowEvent = (Event)nearestEventOfUserIterator.next();%0A                System.out.println(userWhoGoToNearestEvent.getLogin()+%22--%22+nowEvent.getTitle());%0A            %7D%0A        %7D%0A
     

add getEventsByDate to EventDao