View Javadoc
1   package org.kuali.ole.docstore.discovery.solr.work.bib.marc;
2   
3   import junit.framework.Assert;
4   import org.apache.commons.io.FileUtils;
5   import org.apache.commons.io.IOUtils;
6   import org.apache.commons.lang.time.StopWatch;
7   import org.apache.solr.common.SolrInputDocument;
8   import org.junit.Test;
9   import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
10  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
11  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecord;
12  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecords;
13  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
14  import org.kuali.ole.docstore.model.xstream.work.bib.marc.WorkBibMarcRecordProcessor;
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  import java.io.File;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.UUID;
23  
24  /**
25   * JUnit Test Case to test WorkBibMarcDocBuilder.
26   *
27   * @author Rajesh Chowdary K
28   */
29  public class WorkBibMarcDocBuilder_UT extends junit.framework.TestCase {
30  
31      private static final Logger LOG = LoggerFactory.getLogger(WorkBibMarcDocBuilder_UT.class);
32  
33      private WorkBibMarcRecord record = null;
34      WorkBibMarcDocBuilder workBibMarcDocBuilder = new WorkBibMarcDocBuilder();
35      List<SolrInputDocument> solrInputDocuments = new ArrayList<SolrInputDocument>();
36      StopWatch buildSolrInputDocTimer = new StopWatch();
37      StopWatch xmlToPojoTimer = new StopWatch();
38  
39  
40      @Override
41      public void setUp() {
42          WorkBibMarcRecords recs = null;
43          try {
44              recs = new WorkBibMarcRecordProcessor().fromXML(IOUtils.toString(getClass().getResourceAsStream("/bib/bib/marc/OneMarcRecord.xml")));
45          } catch (Exception e) {
46              LOG.error(e.getMessage() , e);
47          }
48          record = recs.getRecords().get(0);
49  
50      }
51  
52      @Override
53      public void tearDown() {
54          record = null;
55      }
56  
57      @Test
58      public void testBuildSolrInputDocument() {
59          try {
60              SolrInputDocument solrInputDocument = new WorkBibMarcDocBuilder().buildSolrInputDocument(record);
61              LOG.info("Output: " + solrInputDocument);
62          } catch (Exception e) {
63              LOG.error(e.getMessage() , e);
64              fail(e.getMessage());
65          }
66      }
67  
68      @Test
69      public void testBuildSolrInputDocuments() {
70          try {
71              List<WorkBibMarcRecord> records = new ArrayList<WorkBibMarcRecord>();
72              records.add(record);
73              List<SolrInputDocument> solrInputDocuments = new WorkBibMarcDocBuilder().buildSolrInputDocuments(records);
74              LOG.info("Output: " + solrInputDocuments);
75          } catch (Exception e) {
76              LOG.error(e.getMessage() , e);
77              fail(e.getMessage());
78          }
79      }
80  
81      @Test
82      public void testWholeXMLtoSolrConverter() {
83          WorkBibMarcRecordProcessor workBibMarcRecordProcessor = new WorkBibMarcRecordProcessor();
84          String resFile = "/bib/bib/marc/marc_test.xml";
85  
86          try {
87              URL resource = getClass().getResource(resFile);
88              File file = new File(resource.toURI());
89              String docContent = FileUtils.readFileToString(file);
90              WorkBibMarcRecords records = workBibMarcRecordProcessor.fromXML(docContent);
91              List<SolrInputDocument> solrInputDocuments = new WorkBibMarcDocBuilder().buildSolrInputDocuments(records.getRecords());
92              Assert.assertEquals(records.getRecords().size(), solrInputDocuments.size());
93              LOG.info("Output: " + solrInputDocuments);
94          } catch (Exception e) {
95              LOG.error(e.getMessage() , e);
96              fail("Failed due to: " + e);
97          }
98  
99      }
100 
101     @Test
102     public void testSecondIndicatorForMarc() throws Exception {
103         String resFile = "/bib/bib/marc/request-Marc2ndIndicatorTest.xml";
104         URL resource = getClass().getResource(resFile);
105         File file = new File(resource.toURI());
106         String requestContent = FileUtils.readFileToString(file);
107         RequestHandler requestHandler = new RequestHandler();
108         Request request = requestHandler.toObject(requestContent);
109         List<RequestDocument> requestDocumentList = request.getRequestDocuments();
110         buildSolrInputDocTimer.start();
111         buildSolrInputDocTimer.suspend();
112         xmlToPojoTimer.start();
113         xmlToPojoTimer.suspend();
114         for (RequestDocument requestDocument : requestDocumentList) {
115             requestDocument.setUuid("1");
116             workBibMarcDocBuilder.buildSolrInputDocument(requestDocument, solrInputDocuments, buildSolrInputDocTimer, xmlToPojoTimer);
117             assertNotNull(solrInputDocuments);
118         }
119 
120         Object titleSort = solrInputDocuments.get(0).getField("Title_sort").getValue();
121         LOG.info("Title after applying 2nd indicator-->" + titleSort.toString());
122         Assert.assertEquals(titleSort, "April Shower");
123         Object authorSort = solrInputDocuments.get(0).getField("Author_sort").getValue();
124         LOG.info("Author after applying 2nd indicator-->" + authorSort.toString());
125         Assert.assertEquals(authorSort, "Philip");
126 
127 
128     }
129 
130     @Test
131     public void testSearchFieldsXMLtoSolrConverter() {
132         WorkBibMarcRecordProcessor workBibMarcRecordProcessor = new WorkBibMarcRecordProcessor();
133         String resFile = "/bib/bib/marc/marc_subject_test.xml";
134         try {
135             URL resource = getClass().getResource(resFile);
136             File file = new File(resource.toURI());
137             String docContent = FileUtils.readFileToString(file);
138             WorkBibMarcRecords records = workBibMarcRecordProcessor.fromXML(docContent);
139             List<SolrInputDocument> solrInputDocuments = new WorkBibMarcDocBuilder().buildSolrInputDocuments(records.getRecords());
140             Assert.assertEquals(records.getRecords().size(), solrInputDocuments.size());
141             LOG.info("Output: " + solrInputDocuments);
142         } catch (Exception e) {
143             LOG.error(e.getMessage() , e);
144             fail("Failed due to: " + e);
145         }
146 
147     }
148 }