package org.motechproject.scheduler.osgi;

import org.motechproject.event.MotechEvent;
import org.motechproject.event.listener.EventListener;
import org.motechproject.event.listener.EventListenerRegistryService;
import org.motechproject.scheduler.MotechSchedulerService;
import org.motechproject.scheduler.domain.RunOnceSchedulableJob;
import org.motechproject.testing.osgi.BaseOsgiIT;
import org.motechproject.testing.utils.IdGenerator;
import org.osgi.framework.ServiceReference;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.util.Arrays.asList;

public class SchedulerBundleIT extends BaseOsgiIT {

    private String TEST_SUBJECT = IdGenerator.id("SchedulerBundleIT");

    public void testRunOnceJob() throws InterruptedException {
        final List<String> receivedEvents = new ArrayList<>();
        EventListenerRegistryService eventRegistry = (EventListenerRegistryService) getApplicationContext().getBean("eventListenerRegistry");
        eventRegistry.registerListener(new EventListener() {
            @Override
            public void handle(MotechEvent event) {
                synchronized (receivedEvents) {
                    receivedEvents.add(event.getSubject());
                    receivedEvents.notify();
                }
            }

            @Override
            public String getIdentifier() {
                return TEST_SUBJECT;
            }
        }, TEST_SUBJECT);

        ServiceReference schedulerServiceReference = bundleContext.getServiceReference(MotechSchedulerService.class.getName());
        assertNotNull(schedulerServiceReference);
        MotechSchedulerService schedulerService = (MotechSchedulerService) bundleContext.getService(schedulerServiceReference);
        assertNotNull(schedulerService);
        final MotechEvent motechEvent = new MotechEvent(TEST_SUBJECT);
        motechEvent.getParameters().put(MotechSchedulerService.JOB_ID_KEY, "jobId");
        schedulerService.unscheduleAllJobs("SchedulerBundleIT");
        schedulerService.scheduleRunOnceJob(new RunOnceSchedulableJob(motechEvent, new Date()));
        synchronized (receivedEvents) {
            System.out.print("\nEvent waiting " + new Date() + "\n");
            receivedEvents.wait(10000);

        }
        assertEquals(1, receivedEvents.size());
        assertEquals(receivedEvents.get(0), TEST_SUBJECT);
    }

    @Override
    protected String[] getConfigLocations() {
        return new String[]{"/META-INF/osgi/testSchedulerBundleContext.xml"};
    }

    @Override
    protected List<String> getImports() {
        return asList(
                "org.motechproject.scheduler",
                "org.motechproject.scheduler.domain"
        );
    }
}

@@ -36,16 +36,47 @@
 .osgi;%0A%0A
+import org.joda.time.DateTime;%0A
 import o
@@ -2131,20 +2131,48 @@
 hEvent, 
-new 
+DateTime.now().plusSeconds(5).to
 Date()))
@@ -2320,16 +2320,15 @@
 it(1
-0
+5
 000);%0A
-%0A
     

Fix for failing SchedulerBundleIT