View Javadoc
1   package org.kuali.ole;
2   
3   import org.junit.Ignore;
4   import org.junit.Test;
5   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.ControlField;
6   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
7   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
8   import org.kuali.ole.docstore.xstream.FileUtil;
9   import org.kuali.ole.pojo.bib.BibliographicRecord;
10  import org.kuali.ole.pojo.bib.Collection;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  import java.io.File;
15  import java.net.URL;
16  import java.util.*;
17  
18  import static junit.framework.Assert.*;
19  
20  /**
21   * Created by IntelliJ IDEA.
22   * User: pvsubrah
23   * Date: 4/2/12
24   * Time: 3:48 PM
25   * To change this template use File | Settings | File Templates.
26   */
27  public class BibliographicRecordHandler_UT {
28      public static final Logger LOG = LoggerFactory.getLogger(BibliographicRecordHandler_UT.class);
29  
30      @Test
31      public void testExtractBibliographicRecordsFromXML() throws Exception {
32          BibliographicRecordHandler bibliographicRecordHandler =
33                  new BibliographicRecordHandler();
34  
35          URL resource = getClass().getResource("input-marc.xml");
36          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
37  
38          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
39          assertNotNull(bibliographicRecordCollection);
40          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
41  
42      }
43  
44      @Test
45      public void testExtractBibliographicRecordsFromSpeicalCharsXML() throws Exception {
46          BibliographicRecordHandler bibliographicRecordHandler =
47                  new BibliographicRecordHandler();
48  
49          URL resource = getClass().getResource("input-marc-special-chars.xml");
50          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
51  
52          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
53          assertNotNull(bibliographicRecordCollection);
54          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
55  
56      }
57  
58  
59      @Ignore
60      @Test
61      public void testExtractBibliographicRecordsForFailedXML() throws Exception {
62          //TODO: not sure what happened to the xml; will need to re-validate this test.
63          BibliographicRecordHandler bibliographicRecordHandler =
64                  new BibliographicRecordHandler();
65  
66          URL resource = getClass().getResource("failed-marc.xml");
67          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
68  
69          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
70          assertNotNull(bibliographicRecordCollection);
71          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
72  
73      }
74  
75      @Test
76      public void testGenerateXML() throws Exception {
77  
78          BibliographicRecordHandler bibliographicRecordHandler =
79                  new BibliographicRecordHandler();
80          BibliographicRecord bibliographicRecord = generateMockBib();
81          String xml = bibliographicRecordHandler.generateXML(bibliographicRecord);
82          LOG.info(xml);
83  
84  
85      }
86  
87      private BibliographicRecord generateMockBib() {
88          BibliographicRecord bibliographicRecord = new BibliographicRecord();
89          bibliographicRecord.setLeader("MOCK_LEADER");
90  
91          ControlField marcControlField = new ControlField();
92          marcControlField.setTag("001");
93          marcControlField.setValue("1223");
94          ControlField marcControlField1 = new ControlField();
95          marcControlField1.setTag("008");
96          marcControlField1.setValue("12323424");
97          bibliographicRecord.setControlfields(Arrays.asList(marcControlField, marcControlField1));
98  
99          DataField marcDataField = new DataField();
100         marcDataField.setTag("020");
101         marcDataField.setInd1("");
102         marcDataField.setInd1("");
103         SubField marcSubField = new SubField();
104         marcSubField.setCode("a");
105         marcSubField.setValue("CAMBRIDGE INTRODUCTION TO C++ Books & READ**.");
106         SubField marcSubField1 = new SubField();
107         marcSubField1.setCode("c");
108         marcSubField1.setValue("19.99");
109         marcDataField.setSubFields(Arrays.asList(marcSubField, marcSubField1));
110 
111         DataField marcDataField1 = new DataField();
112         marcDataField1.setTag("852");
113         marcDataField1.setInd1("");
114         marcDataField1.setInd1("");
115         SubField marcSubField2 = new SubField();
116         marcSubField2.setCode("a");
117         marcSubField2.setValue("123");
118         SubField marcSubField3 = new SubField();
119         marcSubField3.setCode("b");
120         marcSubField3.setValue("42657");
121         marcDataField1.setSubFields(Arrays.asList(marcSubField2, marcSubField3));
122         bibliographicRecord.setDatafields(Arrays.asList(marcDataField, marcDataField1));
123         return bibliographicRecord;
124     }
125 
126 }