<commit_before>package edu.northwestern.bioinformatics.studycalendar.xml.writers;

import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarXmlTestCase;
import edu.northwestern.bioinformatics.studycalendar.domain.Epoch;
import static edu.northwestern.bioinformatics.studycalendar.domain.Fixtures.setGridId;
import static edu.northwestern.bioinformatics.studycalendar.domain.Fixtures.createNamedInstance;
import edu.northwestern.bioinformatics.studycalendar.dao.EpochDao;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.SCHEMA_NAMESPACE_ATTRIBUTE;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.PSC_NS;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.SCHEMA_LOCATION_ATTRIBUTE;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.SCHEMA_LOCATION;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.XML_SCHEMA_ATTRIBUTE;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.XSI_NS;
import org.dom4j.Element;
import org.dom4j.Document;
import static org.easymock.EasyMock.expect;

import static java.text.MessageFormat.format;

public class PlanTreeNodeXmlSerializerTest extends StudyCalendarXmlTestCase {

    private PlanTreeNodeXmlSerializer serializer;
    private Element element;
    private Epoch epoch;
    private EpochDao epochDao;

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

        element = registerMockFor(Element.class);
        epochDao = registerDaoMockFor(EpochDao.class);

        serializer = new PlanTreeNodeXmlSerializer();
        serializer.setEpochDao(epochDao);

        epoch = setGridId("grid0", createNamedInstance("Epoch A", Epoch.class));

    }

    public void testCreateElement() {
        Element actual = serializer.createElement(epoch);

        assertEquals("Wrong attribute size", 2, actual.attributeCount());
        assertEquals("Wrong grid id", "grid0", actual.attribute("id").getValue());
        assertEquals("Wrong epoch name", "Epoch A", actual.attribute("name").getValue());
    }

    public void testReadElement() {
        expect(element.attributeValue("id")).andReturn("grid0");
        expect(element.getName()).andReturn("epoch").times(2);
        expect(epochDao.getByGridId("grid0")).andReturn(null);
        expect(element.attributeValue("name")).andReturn("Epoch A");
        replayMocks();

        Epoch actual = (Epoch) serializer.readElement(element);
        verifyMocks();

        assertEquals("Wrong grid id", "grid0", actual.getGridId());
        assertEquals("Wrong epoch name", "Epoch A", actual.getName());
    }

    public void testReadElementExists() {
        expect(element.attributeValue("id")).andReturn("grid0");
        expect(element.getName()).andReturn("epoch");
        expect(epochDao.getByGridId("grid0")).andReturn(epoch);
        replayMocks();

        Epoch actual = (Epoch) serializer.readElement(element);
        verifyMocks();

        assertSame("Wrong Epoch", epoch, actual);
    }

    public void testCreateDocument() throws Exception {
        Document actual = serializer.createDocument(epoch);

        assertEquals("Wrong epoch grid id", "grid0", actual.getRootElement().attributeValue("id"));
        assertEquals("Wrong epoch name", "Epoch A", actual.getRootElement().attributeValue("name"));
    }

    public void testCreateDocumentString() throws Exception {
        StringBuffer expected = new StringBuffer();
        expected.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                .append(format("<epoch id=\"{0}\" name=\"{1}\"", epoch.getGridId(), epoch.getName()))
                .append(format("       {0}=\"{1}\""     , SCHEMA_NAMESPACE_ATTRIBUTE, PSC_NS))
                .append(format("       {0}=\"{1} {2}\""     , SCHEMA_LOCATION_ATTRIBUTE, PSC_NS, SCHEMA_LOCATION))
                .append(format("       {0}=\"{1}\"/>"    , XML_SCHEMA_ATTRIBUTE, XSI_NS));

        String actual = serializer.createDocumentString(epoch);
        assertXMLEqual(expected.toString(), actual);
    }


}<commit_msg>Added test cases to plan tree node xml serializer for study segments.<commit_after>package edu.northwestern.bioinformatics.studycalendar.xml.writers;

import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarXmlTestCase;
import edu.northwestern.bioinformatics.studycalendar.domain.Epoch;
import edu.northwestern.bioinformatics.studycalendar.domain.StudySegment;
import static edu.northwestern.bioinformatics.studycalendar.domain.Fixtures.setGridId;
import static edu.northwestern.bioinformatics.studycalendar.domain.Fixtures.createNamedInstance;
import edu.northwestern.bioinformatics.studycalendar.dao.EpochDao;
import edu.northwestern.bioinformatics.studycalendar.dao.StudySegmentDao;
import edu.northwestern.bioinformatics.studycalendar.dao.PeriodDao;
import edu.northwestern.bioinformatics.studycalendar.dao.PlannedActivityDao;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.SCHEMA_NAMESPACE_ATTRIBUTE;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.PSC_NS;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.SCHEMA_LOCATION_ATTRIBUTE;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.SCHEMA_LOCATION;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.XML_SCHEMA_ATTRIBUTE;
import static edu.northwestern.bioinformatics.studycalendar.xml.writers.StudyXMLWriter.XSI_NS;
import org.dom4j.Element;
import org.dom4j.Document;
import static org.easymock.EasyMock.expect;

import static java.text.MessageFormat.format;

public class PlanTreeNodeXmlSerializerTest extends StudyCalendarXmlTestCase {

    private PlanTreeNodeXmlSerializer serializer;
    private Element element;
    private Epoch epoch;
    private EpochDao epochDao;
    private StudySegment segment;
    private PeriodDao periodDao;
    private StudySegmentDao studySegmentDao;
    private PlannedActivityDao plannedActivityDao;

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

        element = registerMockFor(Element.class);
        epochDao = registerDaoMockFor(EpochDao.class);
        periodDao = registerDaoMockFor(PeriodDao.class);
        studySegmentDao = registerDaoMockFor(StudySegmentDao.class);
        plannedActivityDao = registerDaoMockFor(PlannedActivityDao.class);

        serializer = new PlanTreeNodeXmlSerializer();
        serializer.setEpochDao(epochDao);
        serializer.setPeriodDao(periodDao);
        serializer.setStudySegmentDao(studySegmentDao);
        serializer.setPlannedActivityDao(plannedActivityDao);

        epoch = setGridId("grid0", createNamedInstance("Epoch A", Epoch.class));
        segment = setGridId("grid1", createNamedInstance("Segment A", StudySegment.class));

    }

    public void testCreateElementEpoch() {
        Element actual = serializer.createElement(epoch);

        assertEquals("Wrong attribute size", 2, actual.attributeCount());
        assertEquals("Wrong grid id", "grid0", actual.attribute("id").getValue());
        assertEquals("Wrong epoch name", "Epoch A", actual.attribute("name").getValue());
    }

    public void testReadElementEpoch() {
        expect(element.attributeValue("id")).andReturn("grid0");
        expect(element.getName()).andReturn("epoch").times(2);
        expect(epochDao.getByGridId("grid0")).andReturn(null);
        expect(element.attributeValue("name")).andReturn("Epoch A");
        replayMocks();

        Epoch actual = (Epoch) serializer.readElement(element);
        verifyMocks();

        assertEquals("Wrong grid id", "grid0", actual.getGridId());
        assertEquals("Wrong epoch name", "Epoch A", actual.getName());
    }

    public void testReadElementExistsEpoch() {
        expect(element.attributeValue("id")).andReturn("grid0");
        expect(element.getName()).andReturn("epoch");
        expect(epochDao.getByGridId("grid0")).andReturn(epoch);
        replayMocks();

        Epoch actual = (Epoch) serializer.readElement(element);
        verifyMocks();

        assertSame("Wrong Epoch", epoch, actual);
    }

    public void testCreateDocumentEpoch() throws Exception {
        Document actual = serializer.createDocument(epoch);

        assertEquals("Element should be an epoch", "epoch", actual.getRootElement().getName());
        assertEquals("Wrong epoch grid id", "grid0", actual.getRootElement().attributeValue("id"));
        assertEquals("Wrong epoch name", "Epoch A", actual.getRootElement().attributeValue("name"));
    }

    public void testCreateDocumentStringEpoch() throws Exception {
        StringBuffer expected = new StringBuffer();
        expected.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                .append(format("<epoch id=\"{0}\" name=\"{1}\"", epoch.getGridId(), epoch.getName()))
                .append(format("       {0}=\"{1}\""     , SCHEMA_NAMESPACE_ATTRIBUTE, PSC_NS))
                .append(format("       {0}=\"{1} {2}\""     , SCHEMA_LOCATION_ATTRIBUTE, PSC_NS, SCHEMA_LOCATION))
                .append(format("       {0}=\"{1}\"/>"    , XML_SCHEMA_ATTRIBUTE, XSI_NS));

        String actual = serializer.createDocumentString(epoch);
        assertXMLEqual(expected.toString(), actual);
    }

    public void testCreateElementStudySegment() {
        Element actual = serializer.createElement(segment);

        assertEquals("Wrong attribute size", 2, actual.attributeCount());
        assertEquals("Wrong grid id", "grid1", actual.attribute("id").getValue());
        assertEquals("Wrong segment name", "Segment A", actual.attribute("name").getValue());
    }

    public void testReadElementStudySegment() {
        expect(element.attributeValue("id")).andReturn("grid1");
        expect(element.getName()).andReturn("study-segment").times(2);
        expect(studySegmentDao.getByGridId("grid1")).andReturn(null);
        expect(element.attributeValue("name")).andReturn("Segment A");
        replayMocks();

        StudySegment actual = (StudySegment) serializer.readElement(element);
        verifyMocks();

        assertEquals("Wrong grid id", "grid1", actual.getGridId());
        assertEquals("Wrong segment name", "Segment A", actual.getName());
    }

    public void testReadElementExistsStudySegment() {
        expect(element.attributeValue("id")).andReturn("grid1");
        expect(element.getName()).andReturn("study-segment");
        expect(studySegmentDao.getByGridId("grid1")).andReturn(segment);
        replayMocks();

        StudySegment actual = (StudySegment) serializer.readElement(element);
        verifyMocks();

        assertSame("Wrong Epoch", segment, actual);
    }



}
{'fraction_non_alphanumeric': 0.12332291858114318, 'fraction_numerical': 0.00523800771916927, 'mean_word_length': 4.224675948151704, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 2, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 17}, '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': '1379637', 'n_tokens_mistral': 3153, 'n_tokens_neox': 3023, 'n_words': 509}