<commit_before>package edu.northwestern.bioinformatics.studycalendar.web.schedule;

import edu.northwestern.bioinformatics.studycalendar.dao.ActivityDao;
import edu.northwestern.bioinformatics.studycalendar.dao.ScheduledActivityDao;
import edu.northwestern.bioinformatics.studycalendar.dao.StudyDao;
import edu.northwestern.bioinformatics.studycalendar.domain.*;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.*;
import edu.northwestern.bioinformatics.studycalendar.domain.scheduledactivitystate.Occurred;
import edu.northwestern.bioinformatics.studycalendar.service.DeltaService;
import edu.northwestern.bioinformatics.studycalendar.service.StudyService;
import edu.northwestern.bioinformatics.studycalendar.service.NotificationService;
import edu.northwestern.bioinformatics.studycalendar.core.Fixtures;
import edu.northwestern.bioinformatics.studycalendar.core.*;
import static edu.northwestern.bioinformatics.studycalendar.core.Fixtures.*;
import edu.nwu.bioinformatics.commons.DateUtils;
import gov.nih.nci.cabig.ctms.lang.DateTools;
import gov.nih.nci.cabig.ctms.lang.StaticNowFactory;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import org.easymock.classextension.EasyMock;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

public class StudyServiceTest extends StudyCalendarTestCase {
    private static final Timestamp NOW = DateTools.createTimestamp(2001, Calendar.FEBRUARY, 4);

    private StudyService service;
    private StudyDao studyDao;
    private ActivityDao activityDao;
    private Study study;
    StudySubjectAssignment subjectAssignment;
    ScheduledCalendar calendar;
    StaticNowFactory staticNowFactory;
    private DeltaService deltaService;
    private ScheduledActivityDao scheduledActivityDao;
    private NotificationService notificationService;

    protected void setUp() throws Exception {
        super.setUp();

        studyDao = registerMockFor(StudyDao.class);
        deltaService = registerMockFor(DeltaService.class);
        scheduledActivityDao=registerDaoMockFor(ScheduledActivityDao.class);
        activityDao = registerMockFor(ActivityDao.class);
        notificationService=registerMockFor(NotificationService.class);
        staticNowFactory = new StaticNowFactory();
        staticNowFactory.setNowTimestamp(NOW);

        service = new StudyService();
        service.setStudyDao(studyDao);
        service.setActivityDao(activityDao);
        service.setDeltaService(deltaService);
        service.setNowFactory(staticNowFactory);
        service.setScheduledActivityDao(scheduledActivityDao);
        service.setNotificationService(notificationService);

        study = setId(1 , new Study());

        calendar = new ScheduledCalendar();

        subjectAssignment = new StudySubjectAssignment();
        subjectAssignment.setSubject(createSubject("John", "Doe"));
        subjectAssignment.setScheduledCalendar(calendar);
    }

    public void testScheduleReconsentAfterScheduledActivityOnOccurredEvent() throws Exception{
        staticNowFactory.setNowTimestamp(DateTools.createTimestamp(2005, Calendar.JULY, 2));

        ScheduledStudySegment studySegment0 = new ScheduledStudySegment();
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("AAA", 2005, Calendar.JULY, 1));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("BBB", 2005, Calendar.JULY, 2,
                new Occurred(null, DateUtils.createDate(2005, Calendar.JULY, 3))));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("CCC", 2005, Calendar.JULY, 4));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("DDD", 2005, Calendar.JULY, 8));
        calendar.addStudySegment(studySegment0);

        ScheduledStudySegment studySegment1 = new ScheduledStudySegment();
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("EEE", 2005, Calendar.AUGUST, 1,
                new Occurred(null, DateUtils.createDate(2005, Calendar.AUGUST, 2))));
        studySegment1.addEvent(edu.northwestern.bioinformatics.studycalendar.core.Fixtures.createScheduledActivityWithStudy("FFF", 2005, Calendar.AUGUST, 3));
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("GGG", 2005, Calendar.AUGUST, 8));
        calendar.addStudySegment(studySegment1);

        List<StudySubjectAssignment> assignments = Collections.singletonList(subjectAssignment);
        expect(studyDao.getAssignmentsForStudy(study.getId())).andReturn(assignments);

        Activity reconsent = setId(1, createNamedInstance("Reconsent", Activity.class));
        expect(activityDao.getByName("Reconsent")).andReturn(reconsent);

        studyDao.save(study);
        scheduledActivityDao.save(isA(ScheduledActivity.class));
        notificationService.notifyUsersForNewScheduleNotifications(isA(Notification.class));

        replayMocks();
        service.scheduleReconsent(study, staticNowFactory.getNow(), "Reconsent Details");
        verifyMocks();

        List<ScheduledActivity> list = studySegment0.getActivitiesByDate().get(DateTools.createTimestamp(2005, Calendar.JULY, 4));
        
        assertEquals("Wrong number of events on July 4th", 2, list.size());
        assertEquals("Reconsent Details should be destails", "Reconsent Details", list.get(1).getDetails());
        assertEquals("Reconsent should be activity name", "Reconsent", list.get(1).getActivity().getName());
    }

    public void testScheduleReconsentForSecondArmOnSameDayAsScheduledActivity() throws Exception{
        staticNowFactory.setNowTimestamp(DateTools.createTimestamp(2005, Calendar.AUGUST, 3));

        ScheduledStudySegment studySegment0 = new ScheduledStudySegment();
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("AAA", 2005, Calendar.JULY, 1,
                new Occurred(null, DateUtils.createDate(2005, Calendar.JULY, 2))));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("BBB", 2005, Calendar.JULY, 3));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("CCC", 2005, Calendar.JULY, 8));
        calendar.addStudySegment(studySegment0);

        ScheduledStudySegment studySegment1 = new ScheduledStudySegment();
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("DDD", 2005, Calendar.AUGUST, 1,
                new Occurred(null, DateUtils.createDate(2005, Calendar.AUGUST, 2))));
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("EEE", 2005, Calendar.AUGUST, 3));
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("FFF", 2005, Calendar.AUGUST, 8));
        calendar.addStudySegment(studySegment1);

        List<StudySubjectAssignment> assignments = Collections.singletonList(subjectAssignment);
        expect(studyDao.getAssignmentsForStudy(study.getId())).andReturn(assignments);

        Activity reconsent = setId(1, createNamedInstance("Reconsent", Activity.class));
        expect(activityDao.getByName("Reconsent")).andReturn(reconsent);
        scheduledActivityDao.save(isA(ScheduledActivity.class));
        studyDao.save(study);
        notificationService.notifyUsersForNewScheduleNotifications(isA(Notification.class));
        
        replayMocks();
        service.scheduleReconsent(study, staticNowFactory.getNow(), "Reconsent Details");
        verifyMocks();

        List<ScheduledActivity> list = studySegment1.getActivitiesByDate().get(DateTools.createTimestamp(2005, Calendar.AUGUST, 3));

        assertEquals("Wrong number of events on August 8th", 2, list.size());
        assertEquals("Reconsent Details should be destails", "Reconsent Details", list.get(1).getDetails());
        assertEquals("Reconsent should be activity name", "Reconsent", list.get(1).getActivity().getName());
    }

    public void testSave() {
        Study study = createNamedInstance("Study A", Study.class);
        Amendment amend0 = Fixtures.createAmendments("Amendment A");
        Amendment amend1 = Fixtures.createAmendments("Amendment B");
        amend1.setPreviousAmendment(amend0);
        study.setAmendment(amend1);

        studyDao.save(study);
        deltaService.saveRevision(amend1);
        deltaService.saveRevision(amend0);
        replayMocks();

        service.save(study);<commit_msg>Correct expectations for change in sort behavior implemented in r4186.<commit_after>package edu.northwestern.bioinformatics.studycalendar.web.schedule;

import edu.northwestern.bioinformatics.studycalendar.dao.ActivityDao;
import edu.northwestern.bioinformatics.studycalendar.dao.ScheduledActivityDao;
import edu.northwestern.bioinformatics.studycalendar.dao.StudyDao;
import edu.northwestern.bioinformatics.studycalendar.domain.*;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.*;
import edu.northwestern.bioinformatics.studycalendar.domain.scheduledactivitystate.Occurred;
import edu.northwestern.bioinformatics.studycalendar.service.DeltaService;
import edu.northwestern.bioinformatics.studycalendar.service.StudyService;
import edu.northwestern.bioinformatics.studycalendar.service.NotificationService;
import edu.northwestern.bioinformatics.studycalendar.core.Fixtures;
import edu.northwestern.bioinformatics.studycalendar.core.*;
import static edu.northwestern.bioinformatics.studycalendar.core.Fixtures.*;
import edu.nwu.bioinformatics.commons.DateUtils;
import gov.nih.nci.cabig.ctms.lang.DateTools;
import gov.nih.nci.cabig.ctms.lang.StaticNowFactory;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import org.easymock.classextension.EasyMock;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

public class StudyServiceTest extends StudyCalendarTestCase {
    private static final Timestamp NOW = DateTools.createTimestamp(2001, Calendar.FEBRUARY, 4);

    private StudyService service;
    private StudyDao studyDao;
    private ActivityDao activityDao;
    private Study study;
    StudySubjectAssignment subjectAssignment;
    ScheduledCalendar calendar;
    StaticNowFactory staticNowFactory;
    private DeltaService deltaService;
    private ScheduledActivityDao scheduledActivityDao;
    private NotificationService notificationService;

    protected void setUp() throws Exception {
        super.setUp();

        studyDao = registerMockFor(StudyDao.class);
        deltaService = registerMockFor(DeltaService.class);
        scheduledActivityDao=registerDaoMockFor(ScheduledActivityDao.class);
        activityDao = registerMockFor(ActivityDao.class);
        notificationService=registerMockFor(NotificationService.class);
        staticNowFactory = new StaticNowFactory();
        staticNowFactory.setNowTimestamp(NOW);

        service = new StudyService();
        service.setStudyDao(studyDao);
        service.setActivityDao(activityDao);
        service.setDeltaService(deltaService);
        service.setNowFactory(staticNowFactory);
        service.setScheduledActivityDao(scheduledActivityDao);
        service.setNotificationService(notificationService);

        study = setId(1 , new Study());

        calendar = new ScheduledCalendar();

        subjectAssignment = new StudySubjectAssignment();
        subjectAssignment.setSubject(createSubject("John", "Doe"));
        subjectAssignment.setScheduledCalendar(calendar);
    }

    public void testScheduleReconsentAfterScheduledActivityOnOccurredEvent() throws Exception{
        staticNowFactory.setNowTimestamp(DateTools.createTimestamp(2005, Calendar.JULY, 2));

        ScheduledStudySegment studySegment0 = new ScheduledStudySegment();
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("AAA", 2005, Calendar.JULY, 1));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("BBB", 2005, Calendar.JULY, 2,
                new Occurred(null, DateUtils.createDate(2005, Calendar.JULY, 3))));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("CCC", 2005, Calendar.JULY, 4));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("DDD", 2005, Calendar.JULY, 8));
        calendar.addStudySegment(studySegment0);

        ScheduledStudySegment studySegment1 = new ScheduledStudySegment();
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("EEE", 2005, Calendar.AUGUST, 1,
                new Occurred(null, DateUtils.createDate(2005, Calendar.AUGUST, 2))));
        studySegment1.addEvent(edu.northwestern.bioinformatics.studycalendar.core.Fixtures.createScheduledActivityWithStudy("FFF", 2005, Calendar.AUGUST, 3));
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("GGG", 2005, Calendar.AUGUST, 8));
        calendar.addStudySegment(studySegment1);

        List<StudySubjectAssignment> assignments = Collections.singletonList(subjectAssignment);
        expect(studyDao.getAssignmentsForStudy(study.getId())).andReturn(assignments);

        Activity reconsent = setId(1, createNamedInstance("Reconsent", Activity.class));
        expect(activityDao.getByName("Reconsent")).andReturn(reconsent);

        studyDao.save(study);
        scheduledActivityDao.save(isA(ScheduledActivity.class));
        notificationService.notifyUsersForNewScheduleNotifications(isA(Notification.class));

        replayMocks();
        service.scheduleReconsent(study, staticNowFactory.getNow(), "Reconsent Details");
        verifyMocks();

        List<ScheduledActivity> list = studySegment0.getActivitiesByDate().get(DateTools.createTimestamp(2005, Calendar.JULY, 4));
        
        assertEquals("Wrong number of events on July 4th", 2, list.size());
        assertEquals("Reconsent Details should be details", "Reconsent Details", list.get(0).getDetails());
        assertEquals("Reconsent should be activity name", "Reconsent", list.get(0).getActivity().getName());
    }

    public void testScheduleReconsentForSecondArmOnSameDayAsScheduledActivity() throws Exception{
        staticNowFactory.setNowTimestamp(DateTools.createTimestamp(2005, Calendar.AUGUST, 3));

        ScheduledStudySegment studySegment0 = new ScheduledStudySegment();
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("AAA", 2005, Calendar.JULY, 1,
                new Occurred(null, DateUtils.createDate(2005, Calendar.JULY, 2))));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("BBB", 2005, Calendar.JULY, 3));
        studySegment0.addEvent(Fixtures.createScheduledActivityWithStudy("CCC", 2005, Calendar.JULY, 8));
        calendar.addStudySegment(studySegment0);

        ScheduledStudySegment studySegment1 = new ScheduledStudySegment();
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("DDD", 2005, Calendar.AUGUST, 1,
                new Occurred(null, DateUtils.createDate(2005, Calendar.AUGUST, 2))));
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("EEE", 2005, Calendar.AUGUST, 3));
        studySegment1.addEvent(Fixtures.createScheduledActivityWithStudy("FFF", 2005, Calendar.AUGUST, 8));
        calendar.addStudySegment(studySegment1);

        List<StudySubjectAssignment> assignments = Collections.singletonList(subjectAssignment);
        expect(studyDao.getAssignmentsForStudy(study.getId())).andReturn(assignments);

        Activity reconsent = setId(1, createNamedInstance("Reconsent", Activity.class));
        expect(activityDao.getByName("Reconsent")).andReturn(reconsent);
        scheduledActivityDao.save(isA(ScheduledActivity.class));
        studyDao.save(study);
        notificationService.notifyUsersForNewScheduleNotifications(isA(Notification.class));
        
        replayMocks();
        service.scheduleReconsent(study, staticNowFactory.getNow(), "Reconsent Details");
        verifyMocks();

        List<ScheduledActivity> list = studySegment1.getActivitiesByDate().get(DateTools.createTimestamp(2005, Calendar.AUGUST, 3));

        assertEquals("Wrong number of events on August 8th", 2, list.size());
        assertEquals("Reconsent Details should be details", "Reconsent Details", list.get(0).getDetails());
        assertEquals("Reconsent should be activity name", "Reconsent", list.get(0).getActivity().getName());
    }

    public void testSave() {
        Study study = createNamedInstance("Study A", Study.class);
        Amendment amend0 = Fixtures.createAmendments("Amendment A");
        Amendment amend1 = Fixtures.createAmendments("Amendment B");
        amend1.setPreviousAmendment(amend0);
        study.setAmendment(amend1);

        studyDao.save(study);
        deltaService.saveRevision(amend1);
        deltaService.saveRevision(amend0);
        replayMocks();

        service.save(study);
{'fraction_non_alphanumeric': 0.10042014320373986, 'fraction_numerical': 0.018107580330197052, 'mean_word_length': 4.78568983224923, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3408547', 'n_tokens_mistral': 5043, 'n_tokens_neox': 4559, 'n_words': 724}