View Javadoc

1   package org.kuali.ole;
2   
3   import org.junit.Ignore;
4   import org.junit.Test;
5   import org.kuali.ole.pojo.bib.*;
6   
7   import java.io.File;
8   import java.net.URL;
9   import java.util.ArrayList;
10  import java.util.Arrays;
11  import java.util.Iterator;
12  import java.util.List;
13  
14  import static junit.framework.Assert.*;
15  
16  /**
17   * Created by IntelliJ IDEA.
18   * User: pvsubrah
19   * Date: 4/2/12
20   * Time: 3:48 PM
21   * To change this template use File | Settings | File Templates.
22   */
23  public class BibliographicRecordHandler_UT {
24      @Test
25      public void testExtractBibliographicRecordsFromXML() throws Exception {
26          BibliographicRecordHandler bibliographicRecordHandler =
27                  new BibliographicRecordHandler();
28  
29          URL resource = getClass().getResource("input-marc.xml");
30          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
31  
32          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
33          assertNotNull(bibliographicRecordCollection);
34          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
35  
36      }
37  
38      @Test
39      public void testExtractBibliographicRecordsFromSpeicalCharsXML() throws Exception {
40          BibliographicRecordHandler bibliographicRecordHandler =
41                  new BibliographicRecordHandler();
42  
43          URL resource = getClass().getResource("input-marc-special-chars.xml");
44          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
45  
46          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
47          assertNotNull(bibliographicRecordCollection);
48          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
49  
50      }
51  
52  
53      @Ignore
54      @Test
55      public void testExtractBibliographicRecordsForFailedXML() throws Exception {
56          //TODO: not sure what happened to the xml; will need to re-validate this test.
57          BibliographicRecordHandler bibliographicRecordHandler =
58                  new BibliographicRecordHandler();
59  
60          URL resource = getClass().getResource("failed-marc.xml");
61          String xmlContent = new FileUtil().readFile(new File(resource.toURI()));
62  
63          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xmlContent);
64          assertNotNull(bibliographicRecordCollection);
65          assertTrue(!bibliographicRecordCollection.getRecords().isEmpty());
66  
67      }
68  
69  
70  
71      @Test
72      public void testBibWithout8xxFields() throws Exception {
73          BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
74          BibliographicRecord modifiedBibWithout8xxFields = bibliographicRecordHandler.getModifiedBibWithout8xxFields(generateMockBib());
75  
76          assertNotNull(modifiedBibWithout8xxFields);
77          String xml = bibliographicRecordHandler.generateXML(modifiedBibWithout8xxFields);
78          System.out.println(xml);
79          Collection bibliographicRecordCollection = bibliographicRecordHandler.fromXML(xml);
80          List<BibliographicRecord> records = bibliographicRecordCollection.getRecords();
81          for (Iterator<BibliographicRecord> iterator = records.iterator(); iterator.hasNext(); ) {
82              BibliographicRecord record = iterator.next();
83              List<MarcDataField> datafields = record.getDatafields();
84              for (Iterator<MarcDataField> marcDataFieldIterator = datafields.iterator(); marcDataFieldIterator.hasNext(); ) {
85                  MarcDataField dataField = marcDataFieldIterator.next();
86                  assertFalse(dataField.getTag().contains("8"));
87              }
88          }
89      }
90  
91      @Test
92      public void testGenerateXML() throws Exception {
93  
94          BibliographicRecordHandler bibliographicRecordHandler =
95                  new BibliographicRecordHandler();
96          BibliographicRecord bibliographicRecord = generateMockBib();
97          String xml = bibliographicRecordHandler.generateXML(bibliographicRecord);
98          System.out.println(xml);
99  
100 
101     }
102 
103     private BibliographicRecord generateMockBib() {
104         BibliographicRecord bibliographicRecord = new BibliographicRecord();
105         bibliographicRecord.setLeader("MOCK_LEADER");
106 
107         MarcControlField marcControlField = new MarcControlField();
108         marcControlField.setTag("001");
109         marcControlField.setValue("1223");
110         MarcControlField marcControlField1 = new MarcControlField();
111         marcControlField1.setTag("008");
112         marcControlField1.setValue("12323424");
113         bibliographicRecord.setControlfields(Arrays.asList(marcControlField, marcControlField1));
114 
115         MarcDataField marcDataField = new MarcDataField();
116         marcDataField.setTag("020");
117         marcDataField.setInd1("");
118         marcDataField.setInd1("");
119         MarcSubField marcSubField = new MarcSubField();
120         marcSubField.setCode("a");
121         marcSubField.setValue("CAMBRIDGE INTRODUCTION TO C++ Books & READ**.");
122         MarcSubField marcSubField1 = new MarcSubField();
123         marcSubField1.setCode("c");
124         marcSubField1.setValue("19.99");
125         marcDataField.setSubfields(Arrays.asList(marcSubField, marcSubField1));
126 
127         MarcDataField marcDataField1 = new MarcDataField();
128         marcDataField1.setTag("852");
129         marcDataField1.setInd1("");
130         marcDataField1.setInd1("");
131         MarcSubField marcSubField2 = new MarcSubField();
132         marcSubField2.setCode("a");
133         marcSubField2.setValue("123");
134         MarcSubField marcSubField3 = new MarcSubField();
135         marcSubField3.setCode("b");
136         marcSubField3.setValue("42657");
137         marcDataField1.setSubfields(Arrays.asList(marcSubField2, marcSubField3));
138         bibliographicRecord.setDatafields(Arrays.asList(marcDataField, marcDataField1));
139         return bibliographicRecord;
140     }
141 
142     @Test
143     public void testGenerateXMLFromRecords() throws Exception {
144 
145         BibliographicRecordHandler bibliographicRecordHandler =
146                 new BibliographicRecordHandler();
147         BibliographicRecord bibliographicRecord = generateMockBib();
148         BibliographicRecord bibliographicRecord1 = generateMockBib();
149         List<BibliographicRecord> bibliographicRecords = new ArrayList<BibliographicRecord>();
150         bibliographicRecords.add(bibliographicRecord);
151         bibliographicRecords.add(bibliographicRecord1);
152         Collection records = new Collection();
153         records.setRecords(bibliographicRecords);
154         String xml = bibliographicRecordHandler.generateXML(records);
155         System.out.println(xml);
156 
157 
158     }
159 }