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
12 import java.io.File;
13 import java.net.URL;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import static junit.framework.Assert.*;
20
21
22
23
24
25
26
27
28 public class BibliographicRecordHandler_UT {
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
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 System.out.println(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 System.out.println(xml);
104
105
106 }
107
108 private BibliographicRecord generateMockBib() {
109 BibliographicRecord bibliographicRecord = new BibliographicRecord();
110 bibliographicRecord.setLeader("MOCK_LEADER");
111
112 ControlField marcControlField = new ControlField();
113 marcControlField.setTag("001");
114 marcControlField.setValue("1223");
115 ControlField marcControlField1 = new ControlField();
116 marcControlField1.setTag("008");
117 marcControlField1.setValue("12323424");
118 bibliographicRecord.setControlfields(Arrays.asList(marcControlField, marcControlField1));
119
120 DataField marcDataField = new DataField();
121 marcDataField.setTag("020");
122 marcDataField.setInd1("");
123 marcDataField.setInd1("");
124 SubField marcSubField = new SubField();
125 marcSubField.setCode("a");
126 marcSubField.setValue("CAMBRIDGE INTRODUCTION TO C++ Books & READ**.");
127 SubField marcSubField1 = new SubField();
128 marcSubField1.setCode("c");
129 marcSubField1.setValue("19.99");
130 marcDataField.setSubFields(Arrays.asList(marcSubField, marcSubField1));
131
132 DataField marcDataField1 = new DataField();
133 marcDataField1.setTag("852");
134 marcDataField1.setInd1("");
135 marcDataField1.setInd1("");
136 SubField marcSubField2 = new SubField();
137 marcSubField2.setCode("a");
138 marcSubField2.setValue("123");
139 SubField marcSubField3 = new SubField();
140 marcSubField3.setCode("b");
141 marcSubField3.setValue("42657");
142 marcDataField1.setSubFields(Arrays.asList(marcSubField2, marcSubField3));
143 bibliographicRecord.setDatafields(Arrays.asList(marcDataField, marcDataField1));
144 return bibliographicRecord;
145 }
146
147 @Test
148 public void testGenerateXMLFromRecords() throws Exception {
149
150 BibliographicRecordHandler bibliographicRecordHandler =
151 new BibliographicRecordHandler();
152 BibliographicRecord bibliographicRecord = generateMockBib();
153 BibliographicRecord bibliographicRecord1 = generateMockBib();
154 List<BibliographicRecord> bibliographicRecords = new ArrayList<BibliographicRecord>();
155 bibliographicRecords.add(bibliographicRecord);
156 bibliographicRecords.add(bibliographicRecord1);
157 Collection records = new Collection();
158 records.setRecords(bibliographicRecords);
159 String xml = bibliographicRecordHandler.generateXML(records);
160 System.out.println(xml);
161
162
163 }
164 }