package edu.northwestern.bioinformatics.studycalendar.service;

import edu.northwestern.bioinformatics.studycalendar.dao.PlannedActivityDao;
import edu.northwestern.bioinformatics.studycalendar.dao.SourceDao;
import edu.northwestern.bioinformatics.studycalendar.domain.Activity;
import edu.northwestern.bioinformatics.studycalendar.domain.ActivityType;
import edu.northwestern.bioinformatics.studycalendar.domain.Fixtures;
import edu.northwestern.bioinformatics.studycalendar.domain.Source;
import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;

/**
 * @author Saurabh Agrawal
 */
public class SourceServiceTest extends StudyCalendarTestCase {

    private SourceService sourceService;

    private SourceDao sourceDao;

    private PlannedActivityDao plannedActivityDao;
    private Source targetSource;
    private Source source;
    private final String TARGET_SOURCE = "targetSource";
    private final String SOURCE = "source";

    private Activity activity, anotherActivity, activityToUpdate, activityToDelete;

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

        sourceDao = registerDaoMockFor(SourceDao.class);
        plannedActivityDao = registerDaoMockFor(PlannedActivityDao.class);
        sourceService = new SourceService();

        sourceService.setSourceDao(sourceDao);
        sourceService.setPlannedActivityDao(plannedActivityDao);
        source = Fixtures.createSource(SOURCE);
        targetSource = Fixtures.createSource(TARGET_SOURCE);

        activity = Fixtures.createActivity("activity1", "code", null, ActivityType.LAB_TEST);
        anotherActivity = Fixtures.createActivity("anotherActivity", "code2", null, ActivityType.LAB_TEST);


        activityToUpdate = Fixtures.createActivity("activityToUpdate", "code2", null, ActivityType.LAB_TEST);


        activityToDelete = Fixtures.createActivity("activityToDelete", "code3", null, ActivityType.LAB_TEST);

    }

    public void testUpdateSourceWhenSourceHasNoActivity() {
        sourceService.updateSource(source, targetSource);

        assertEquals(SOURCE, targetSource.getName());
    }

    public void testUpdateSourceForNewActivity() {
        source.addActivity(activity);
        source.addActivity(anotherActivity);
        sourceService.updateSource(source, targetSource);

        assertEquals(SOURCE, targetSource.getName());
        assertEquals(2, targetSource.getActivities().size());
        assertTrue(targetSource.getActivities().contains(activity));
        assertTrue(targetSource.getActivities().contains(anotherActivity));
    }

    public void testUpdateSourceForUpdateAndDeleteActivity() {
        source.addActivity(activity);
        source.addActivity(anotherActivity);

        targetSource.addActivity(activityToUpdate);
        targetSource.addActivity(activityToDelete);

        sourceService.updateSource(source, targetSource);

        assertEquals(SOURCE, targetSource.getName());
        assertEquals(2, targetSource.getActivities().size());
        assertTrue(targetSource.getActivities().contains(activity));
        assertTrue(targetSource.getActivities().contains(activityToUpdate));
        assertEquals(anotherActivity.toString(), activityToUpdate.toString());

    }
}

@@ -667,17 +667,16 @@
 tCase %7B%0A
-%0A
     priv
@@ -683,155 +683,111 @@
 ate 
-SourceService sourceService;%0A%0A    private SourceDao sourceDao;%0A%0A    private PlannedActivityDao plannedActivityDao;%0A    private Source targetS
+static final String TARGET_SOURCE = %22targetSource%22;%0A    private static final String SOURCE = %22s
 ource
-;
+%22;%0A
 %0A   
@@ -805,59 +805,51 @@
 urce
- source;%0A    private final String TARGET_SOURCE = %22
+Service sourceService;%0A%0A    private Source 
 targ
@@ -848,33 +848,32 @@
 rce targetSource
-%22
 ;%0A    private fi
@@ -874,38 +874,21 @@
 ate 
-final String SOURCE = %22
+Source 
 source
-%22
 ;%0A%0A 
@@ -1045,24 +1045,34 @@
 );%0A%0A        
+SourceDao 
 sourceDao = 
@@ -1116,16 +1116,35 @@
         
+PlannedActivityDao 
 plannedA
@@ -1194,32 +1194,33 @@
 vityDao.class);%0A
+%0A
         sourceSe
@@ -1565,16 +1565,17 @@
 _TEST);%0A
+%0A
         
@@ -1666,34 +1666,32 @@
 Type.LAB_TEST);%0A
-%0A%0A
         activity
@@ -1784,18 +1784,16 @@
 _TEST);%0A
-%0A%0A
         
@@ -1890,25 +1890,24 @@
 .LAB_TEST);%0A
-%0A
     %7D%0A%0A    p
@@ -3151,27 +3151,16 @@
 Activity
-.toString()
 , activi
@@ -3173,23 +3173,11 @@
 date
-.toString()
 );%0A
-%0A
     

Replace assertEquals(...toString...) with simple equals check