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

import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;
import edu.northwestern.bioinformatics.studycalendar.domain.Study;
import edu.northwestern.bioinformatics.studycalendar.domain.Epoch;
import edu.northwestern.bioinformatics.studycalendar.domain.StudySegment;
import edu.northwestern.bioinformatics.studycalendar.domain.PlannedCalendar;
import edu.northwestern.bioinformatics.studycalendar.domain.Period;
import edu.northwestern.bioinformatics.studycalendar.domain.PlannedActivity;
import edu.northwestern.bioinformatics.studycalendar.domain.Fixtures;
import edu.northwestern.bioinformatics.studycalendar.domain.PlanTreeNode;
import static edu.northwestern.bioinformatics.studycalendar.domain.Fixtures.*;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Amendment;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.PropertyChange;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Reorder;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Add;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Remove;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.ChildrenChange;
import edu.northwestern.bioinformatics.studycalendar.dao.DynamicMockDaoFinder;

import java.util.List;

import static org.easymock.classextension.EasyMock.*;

/**
 * @author <NAME>
 */
public class RevisionChangesTest extends StudyCalendarTestCase {
    private Study study;
    private Amendment rev;
    private Epoch treatment;
    private StudySegment studySegmentB;
    private DynamicMockDaoFinder mockDaoFinder;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        rev = new Amendment();
        study = createBasicTemplate();
        study.setDevelopmentAmendment(rev);
        treatment = study.getPlannedCalendar().getEpochs().get(1);
        studySegmentB = treatment.getStudySegments().get(1);
        Fixtures.assignIds(study);

        mockDaoFinder = new DynamicMockDaoFinder();
    }

    public void testChangesFlattened() throws Exception {
        getTestingDeltaService().updateRevision(rev, treatment, PropertyChange.create("name", "Treatment", "Megatreatment"));
        getTestingDeltaService().updateRevision(rev, treatment, Reorder.create(treatment.getStudySegments().get(1), 1, 2));

        RevisionChanges c = new RevisionChanges(mockDaoFinder, rev, study);
        List<RevisionChanges.Flat> flattened = c.getFlattened();

        assertEquals(2, flattened.size());
        assertTrue(flattened.get(0).getChange() instanceof PropertyChange);
        assertEquals(treatment, flattened.get(0).getNode());
        assertTrue(flattened.get(1).getChange() instanceof Reorder);
        assertEquals(treatment, flattened.get(1).getNode());
    }

    public void testChangesOnlyForTargetNodeAndChildrenIfProvided() throws Exception {
        Period p1 = setId(1, Fixtures.createPeriod("P1", 3, 6, 1));
        Period p2 = setId(2, Fixtures.createPeriod("P2", 1, 17, 42));
        PlannedActivity event1 = setId(3, new PlannedActivity());
        PlannedActivity event2 = setId(4, new PlannedActivity());
        studySegmentB.addPeriod(p1);
        p1.addPlannedActivity(event1);
        studySegmentB.addPeriod(p2);
        p2.addPlannedActivity(event2);

        getTestingDeltaService().updateRevision(rev, treatment, PropertyChange.create("name", "Treatment", "Megatreatment"));
        getTestingDeltaService().updateRevision(rev, studySegmentB, PropertyChange.create("name", "B", "Beta"));
        getTestingDeltaService().updateRevision(rev, p1, PropertyChange.create("name", null, "Aleph"));
        getTestingDeltaService().updateRevision(rev, p2, PropertyChange.create("name", null, "Zep"));
        getTestingDeltaService().updateRevision(rev, event1, PropertyChange.create("day", "5", "9"));
        getTestingDeltaService().updateRevision(rev, event2, PropertyChange.create("day", "7", "4"));

        List<RevisionChanges.Flat> forPeriod = new RevisionChanges(mockDaoFinder, rev, study, p1).getFlattened();
        assertEquals("Wrong list: " + forPeriod, 2, forPeriod.size());
        assertEquals(p1, forPeriod.get(0).getNode());
        assertEquals("name", ((PropertyChange) forPeriod.get(0).getChange()).getPropertyName());
        assertEquals(event1, forPeriod.get(1).getNode());
        assertEquals("day", ((PropertyChange) forPeriod.get(1).getChange()).getPropertyName());

        assertEquals(1, new RevisionChanges(mockDaoFinder, rev, study, event1).getFlattened().size());
        assertEquals(5, new RevisionChanges(mockDaoFinder, rev, study, studySegmentB).getFlattened().size());
        assertEquals(6, new RevisionChanges(mockDaoFinder, rev, study, treatment).getFlattened().size());
        assertEquals(6, new RevisionChanges(mockDaoFinder, rev, study, study.getPlannedCalendar()).getFlattened().size());
    }
    
    public void testNodeNameForNamed() throws Exception {
        assertEquals("Test setup failure", "Treatment", treatment.getName());
        assertEquals("epoch Treatment", RevisionChanges.getNodeName(treatment));<commit_msg>Implementation of PlannedActivity#equals required that test instances actually differ<commit_after>package edu.northwestern.bioinformatics.studycalendar.web.delta;

import edu.northwestern.bioinformatics.studycalendar.dao.DynamicMockDaoFinder;
import edu.northwestern.bioinformatics.studycalendar.domain.Epoch;
import edu.northwestern.bioinformatics.studycalendar.domain.Fixtures;
import static edu.northwestern.bioinformatics.studycalendar.domain.Fixtures.*;
import edu.northwestern.bioinformatics.studycalendar.domain.Period;
import edu.northwestern.bioinformatics.studycalendar.domain.PlanTreeNode;
import edu.northwestern.bioinformatics.studycalendar.domain.PlannedActivity;
import edu.northwestern.bioinformatics.studycalendar.domain.PlannedCalendar;
import edu.northwestern.bioinformatics.studycalendar.domain.Study;
import edu.northwestern.bioinformatics.studycalendar.domain.StudySegment;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Add;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Amendment;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.ChildrenChange;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.PropertyChange;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Remove;
import edu.northwestern.bioinformatics.studycalendar.domain.delta.Reorder;
import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;
import static org.easymock.classextension.EasyMock.expect;

import java.util.List;

/**
 * @author <NAME>
 */
public class RevisionChangesTest extends StudyCalendarTestCase {
    private Study study;
    private Amendment rev;
    private Epoch treatment;
    private StudySegment studySegmentB;
    private DynamicMockDaoFinder mockDaoFinder;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        rev = new Amendment();
        study = createBasicTemplate();
        study.setDevelopmentAmendment(rev);
        treatment = study.getPlannedCalendar().getEpochs().get(1);
        studySegmentB = treatment.getStudySegments().get(1);
        Fixtures.assignIds(study);

        mockDaoFinder = new DynamicMockDaoFinder();
    }

    public void testChangesFlattened() throws Exception {
        getTestingDeltaService().updateRevision(rev, treatment, PropertyChange.create("name", "Treatment", "Megatreatment"));
        getTestingDeltaService().updateRevision(rev, treatment, Reorder.create(treatment.getStudySegments().get(1), 1, 2));

        RevisionChanges c = new RevisionChanges(mockDaoFinder, rev, study);
        List<RevisionChanges.Flat> flattened = c.getFlattened();

        assertEquals(2, flattened.size());
        assertTrue(flattened.get(0).getChange() instanceof PropertyChange);
        assertEquals(treatment, flattened.get(0).getNode());
        assertTrue(flattened.get(1).getChange() instanceof Reorder);
        assertEquals(treatment, flattened.get(1).getNode());
    }

    public void testChangesOnlyForTargetNodeAndChildrenIfProvided() throws Exception {
        Period p1 = setId(1, Fixtures.createPeriod("P1", 3, 6, 1));
        Period p2 = setId(2, Fixtures.createPeriod("P2", 1, 17, 42));
        PlannedActivity pa1 = setId(3, createPlannedActivity("PA1", 5));
        PlannedActivity pa2 = setId(4, createPlannedActivity("PA2", 7));
        studySegmentB.addPeriod(p1);
        p1.addPlannedActivity(pa1);
        studySegmentB.addPeriod(p2);
        p2.addPlannedActivity(pa2);

        getTestingDeltaService().updateRevision(rev, treatment, PropertyChange.create("name", "Treatment", "Megatreatment"));
        getTestingDeltaService().updateRevision(rev, studySegmentB, PropertyChange.create("name", "B", "Beta"));
        getTestingDeltaService().updateRevision(rev, p1, PropertyChange.create("name", null, "Aleph"));
        getTestingDeltaService().updateRevision(rev, p2, PropertyChange.create("name", null, "Zep"));
        getTestingDeltaService().updateRevision(rev, pa1, PropertyChange.create("day", "5", "9"));
        getTestingDeltaService().updateRevision(rev, pa2, PropertyChange.create("day", "7", "4"));

        List<RevisionChanges.Flat> forPeriod = new RevisionChanges(mockDaoFinder, rev, study, p1).getFlattened();
        assertEquals("Wrong list: " + forPeriod, 2, forPeriod.size());
        assertEquals(p1, forPeriod.get(0).getNode());
        assertEquals("name", ((PropertyChange) forPeriod.get(0).getChange()).getPropertyName());
        assertEquals(pa1, forPeriod.get(1).getNode());
        assertEquals("day", ((PropertyChange) forPeriod.get(1).getChange()).getPropertyName());

        assertEquals(1, new RevisionChanges(mockDaoFinder, rev, study, pa1).getFlattened().size());
        assertEquals(5, new RevisionChanges(mockDaoFinder, rev, study, studySegmentB).getFlattened().size());
        assertEquals(6, new RevisionChanges(mockDaoFinder, rev, study, treatment).getFlattened().size());
        assertEquals(6, new RevisionChanges(mockDaoFinder, rev, study, study.getPlannedCalendar()).getFlattened().size());
    }
    
    public void testNodeNameForNamed() throws Exception {
        assertEquals("Test setup failure", "Treatment", treatment.getName());
        assertEquals("epoch Treatment", RevisionChanges.getNodeName(treatment));
{'fraction_non_alphanumeric': 0.120837297811608, 'fraction_numerical': 0.010846812559467175, 'mean_word_length': 5.118160651920838, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4235319', 'n_tokens_mistral': 3089, 'n_tokens_neox': 2861, 'n_words': 493}