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      @Test
30      public void testExtractBibliographicRecordsFromXML() throws Exception {
31          BibliographicRecordHandler bibliographicRecordHandler =
32                  new BibliographicRecordHandler();
33  
34          URL resource = getClass().getResource("input-marc.xml");
35          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
36  
37          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
38          assertNotNull(bibliographicRecordCollection);
39          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
40  
41      }
42  
43      @Test
44      public void testExtractBibliographicRecordsFromSpeicalCharsXML() throws Exception {
45          BibliographicRecordHandler bibliographicRecordHandler =
46                  new BibliographicRecordHandler();
47  
48          URL resource = getClass().getResource("input-marc-special-chars.xml");
49          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
50  
51          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
52          assertNotNull(bibliographicRecordCollection);
53          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
54  
55      }
56  
57  
58      @Ignore
59      @Test
60      public void testExtractBibliographicRecordsForFailedXML() throws Exception {
61          //TODO: not sure what happened to the xml; will need to re-validate this test.
62          BibliographicRecordHandler bibliographicRecordHandler =
63                  new BibliographicRecordHandler();
64  
65          URL resource = getClass().getResource("failed-marc.xml");
66          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
67  
68          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
69          assertNotNull(bibliographicRecordCollection);
70          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
71  
72      }
73  
74  
75  
76      @Test
77      public void testBibWithout8xxFields() throws Exception {
78          BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
79          BibliographicRecord modifiedBibWithout8xxFields = bibliographicRecordHandler.getModifiedBibWithout8xxFields(generateMockBib());
80  
81          assertNotNull(modifiedBibWithout8xxFields);
82          String xml = bibliographicRecordHandler.generateXML(modifiedBibWithout8xxFields);
83          LOG.info(xml);
84          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xml);
85          List<BibliographicRecord> records = bibliographicRecordCollection.getRecords();
86          for (Iterator<BibliographicRecord> iterator = records.iterator(); iterator.hasNext(); ) {
87              BibliographicRecord record = iterator.next();
88              List<DataField> datafields = record.getDatafields();
89              for (Iterator<DataField> marcDataFieldIterator = datafields.iterator(); marcDataFieldIterator.hasNext(); ) {
90                  DataField dataField = marcDataFieldIterator.next();
91                  assertFalse(dataField.getTag().contains("8"));
92              }
93          }
94      }
95  
96      @Test
97      public void testGenerateXML() throws Exception {
98  
99          BibliographicRecordHandler bibliographicRecordHandler =
100                 new BibliographicRecordHandler();
101         BibliographicRecord bibliographicRecord = generateMockBib();
102         String xml = bibliographicRecordHandler.generateXML(bibliographicRecord);
103         LOG.info(xml);
104 
105 
106     }
107 
108     @Test
109     public void testValuesMapFromXML() {
110         Map<String, String> xmlValuesMap = null;
111         BibliographicRecordHandler bibliographicRecordHandler =
112                 new BibliographicRecordHandler();
113         BibliographicRecord bibliographicRecord = generateMockBib();
114         String xml = bibliographicRecordHandler.generateXML(bibliographicRecord);
115         xmlValuesMap = bibliographicRecordHandler.valuesMapFromXML(xml);
116         assertNotNull("Map contains values",xmlValuesMap);
117 
118     }
119 
120 
121     private BibliographicRecord generateMockBib() {
122         BibliographicRecord bibliographicRecord = new BibliographicRecord();
123         bibliographicRecord.setLeader("MOCK_LEADER");
124 
125         ControlField marcControlField = new ControlField();
126         marcControlField.setTag("001");
127         marcControlField.setValue("1223");
128         ControlField marcControlField1 = new ControlField();
129         marcControlField1.setTag("008");
130         marcControlField1.setValue("12323424");
131         bibliographicRecord.setControlfields(Arrays.asList(marcControlField, marcControlField1));
132 
133         DataField marcDataField = new DataField();
134         marcDataField.setTag("020");
135         marcDataField.setInd1("");
136         marcDataField.setInd1("");
137         SubField marcSubField = new SubField();
138         marcSubField.setCode("a");
139         marcSubField.setValue("CAMBRIDGE INTRODUCTION TO C++ Books & READ**.");
140         SubField marcSubField1 = new SubField();
141         marcSubField1.setCode("c");
142         marcSubField1.setValue("19.99");
143         marcDataField.setSubFields(Arrays.asList(marcSubField, marcSubField1));
144 
145         DataField marcDataField1 = new DataField();
146         marcDataField1.setTag("852");
147         marcDataField1.setInd1("");
148         marcDataField1.setInd1("");
149         SubField marcSubField2 = new SubField();
150         marcSubField2.setCode("a");
151         marcSubField2.setValue("123");
152         SubField marcSubField3 = new SubField();
153         marcSubField3.setCode("b");
154         marcSubField3.setValue("42657");
155         marcDataField1.setSubFields(Arrays.asList(marcSubField2, marcSubField3));
156         bibliographicRecord.setDatafields(Arrays.asList(marcDataField, marcDataField1));
157         return bibliographicRecord;
158     }
159 
160     @Test
161     public void testGenerateXMLFromRecords() throws Exception {
162 
163         BibliographicRecordHandler bibliographicRecordHandler =
164                 new BibliographicRecordHandler();
165         BibliographicRecord bibliographicRecord = generateMockBib();
166         BibliographicRecord bibliographicRecord1 = generateMockBib();
167         BibliographicRecord bibliographicRecord2 = new BibliographicRecord();
168         DataField marcDataField = new DataField();
169         marcDataField.setTag("020");
170         marcDataField.setInd1("");
171         marcDataField.setInd1("");
172         SubField marcSubField = new SubField();
173         marcSubField.setCode("a");
174         marcSubField.setValue("CAMBRIDGE INTRODUCTION TO C++ Books & READ**.");
175         SubField marcSubField1 = new SubField();
176         marcSubField1.setCode("c");
177         marcSubField1.setValue("19.99");
178         bibliographicRecord2.addDataField(marcDataField);
179         marcDataField.setSubFields(Arrays.asList(marcSubField, marcSubField1));
180         List<BibliographicRecord> bibliographicRecords = new ArrayList<BibliographicRecord>();
181         bibliographicRecords.add(bibliographicRecord);
182         bibliographicRecords.add(bibliographicRecord1);
183         bibliographicRecords.add(bibliographicRecord2);
184         Collection records = new Collection();
185         records.setRecords(bibliographicRecords);
186         String xml = bibliographicRecordHandler.generateXML(records);
187         LOG.info(xml);
188 
189 
190     }
191 
192 }