View Javadoc
1   /*
2    * Copyright 2012 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.docstore.discovery.solr.work.instance.oleml;
17  
18  import junit.framework.Assert;
19  import org.apache.commons.io.FileUtils;
20  import org.apache.commons.io.IOUtils;
21  import org.apache.commons.jxpath.JXPathContext;
22  import org.apache.solr.common.SolrDocument;
23  import org.apache.solr.common.SolrInputDocument;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Ignore;
27  import org.junit.Test;
28  import org.kuali.ole.docstore.common.document.content.instance.Instance;
29  import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor;
30  import org.kuali.ole.docstore.discovery.BaseTestCase;
31  import org.kuali.ole.docstore.indexer.solr.DocumentIndexerManagerFactory;
32  import org.kuali.ole.docstore.indexer.solr.IndexerService;
33  import org.kuali.ole.docstore.model.enums.DocCategory;
34  import org.kuali.ole.docstore.model.enums.DocType;
35  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
36  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
37  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
38  
39  import java.io.File;
40  import java.net.URL;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * Class to test WorkInstanceOlemlDocBuilder.
46   *
47   * @author Rajesh Chowdary K
48   * @created Jun 28, 2012
49   */
50  @Ignore
51  @Deprecated
52  public class WorkInstanceOlemlDocBuilder_UT extends BaseTestCase {
53  
54      private Instance instance = null;
55  
56      /**
57       * Method to setUp
58       *
59       * @throws java.lang.Exception
60       */
61      @Before
62      public void setUp() throws Exception {
63          InstanceOlemlRecordProcessor xmlProcessor = new InstanceOlemlRecordProcessor();
64          instance = xmlProcessor.fromXML(
65                  IOUtils.toString(getClass().getResourceAsStream("/work/instance/oleml/instance9.1.1.xml"))).getInstance().get(0);
66      }
67  
68      /**
69       * Method to tearDown
70       *
71       * @throws java.lang.Exception
72       */
73      @After
74      public void tearDown() throws Exception {
75          instance = null;
76      }
77  
78      @Test
79      public void testBuildSolrInputDocumentsForInstanceByJXPath() {
80          WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
81          List<SolrInputDocument> inputDocuments = new ArrayList<SolrInputDocument>();
82          docBuilder.buildSolrInputDocumentsForInstanceByJXPath(instance, inputDocuments);
83          Assert.assertEquals(1, inputDocuments.size());
84          System.out.println("" + inputDocuments);
85  
86      }
87  
88      @Test
89      public void testBuildSolrInputDocumentsForHoldingByJXPath() {
90          WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
91          List<SolrInputDocument> inputDocuments = new ArrayList<SolrInputDocument>();
92          docBuilder.buildSolrInputDocumentsForHoldingByJXPath(instance, inputDocuments);
93          Assert.assertEquals(1, inputDocuments.size());
94          System.out.println("" + inputDocuments);
95  
96      }
97  
98      @Test
99      public void testBuildSolrInputDocumentsForItemsByJXPath() {
100         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
101         List<SolrInputDocument> inputDocuments = new ArrayList<SolrInputDocument>();
102         docBuilder.buildSolrInputDocumentsForItemsByJXPath(instance, inputDocuments);
103         Assert.assertEquals(1, inputDocuments.size());
104         System.out.println("" + inputDocuments);
105 
106     }
107 
108     @Test
109     public void testBuildSolrInputDocumentsForInstance() {
110         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
111         List<SolrInputDocument> inputDocuments = new ArrayList<SolrInputDocument>();
112         docBuilder.buildSolrInputDocumentsForinstance(instance, inputDocuments);
113         Assert.assertEquals(1, inputDocuments.size());
114         System.out.println("" + inputDocuments);
115 
116     }
117 
118     @Test
119     public void testBuildSolrInputDocumentsForHolding() {
120         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
121         List<SolrInputDocument> inputDocuments = new ArrayList<SolrInputDocument>();
122         RequestDocument requestDocument=new RequestDocument();
123         docBuilder.buildSolrInputDocumentsForHolding(instance, inputDocuments, requestDocument);
124         Assert.assertEquals(1, inputDocuments.size());
125         System.out.println("" + inputDocuments);
126 
127     }
128 
129     @Test
130     public void testBuildSolrInputDocumentsForItems() {
131         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
132         List<SolrInputDocument> inputDocuments = new ArrayList<SolrInputDocument>();
133         RequestDocument requestDocument=new RequestDocument();
134         docBuilder.buildSolrInputDocumentsForItems(instance, inputDocuments, requestDocument);
135         Assert.assertEquals(1, inputDocuments.size());
136         System.out.println("" + inputDocuments);
137 
138     }
139 
140     @Test
141     public void testPerformanceFromCustomToJxPath() {
142         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
143         List<SolrInputDocument> jxSolrDocs = new ArrayList<SolrInputDocument>();
144         List<SolrInputDocument> custSolrDocs = new ArrayList<SolrInputDocument>();
145         RequestDocument rd = new RequestDocument();
146         rd.setCategory(DocCategory.WORK.getCode());
147         rd.setType(DocType.INSTANCE.getCode());
148         // rd.
149 
150         rd.getContent().setContentObject(instance);
151         Long timeCustom = 0L;
152         Long timeJxPath = System.currentTimeMillis();
153         for (int i = 0; i < 10000; i++) {
154             docBuilder.buildSolrInputDocumentsForInstanceByJXPath(instance, jxSolrDocs);
155             docBuilder.buildSolrInputDocumentsForHoldingByJXPath(instance, jxSolrDocs);
156             docBuilder.buildSolrInputDocumentsForItemsByJXPath(instance, jxSolrDocs);
157             if (i == 0)
158                 System.out.println("JX SOLR DOCS: \n" + jxSolrDocs);
159         }
160         timeJxPath = System.currentTimeMillis() - timeJxPath;
161 
162         timeCustom = System.currentTimeMillis();
163         for (int i = 0; i < 10000; i++) {
164             docBuilder.buildSolrInputDocumentsForinstance(instance, custSolrDocs);
165             RequestDocument requestDocument=new RequestDocument();
166             docBuilder.buildSolrInputDocumentsForHolding(instance, custSolrDocs, requestDocument);
167             docBuilder.buildSolrInputDocumentsForItems(instance, custSolrDocs, requestDocument);
168             if (i == 0)
169                 System.out.println("CUSTOM SOLR DOCS: \n" + custSolrDocs);
170         }
171         timeCustom = System.currentTimeMillis() - timeCustom;
172 
173         System.out.println("Time taken for(10kdocs) Jxpath Builder:(ms) " + timeJxPath);
174         System.out.println("Time taken for(10kdocs) Custom Builder:(ms) " + timeCustom);
175         System.out.println("Time diff. for(10kdocs) (JxPath - Custom) Builder:(ms) " + (timeJxPath - timeCustom));
176 
177     }
178 
179     @Test
180     public void testPerformance() {
181         testPerformanceWithoutJxPath();
182         testPerformanceFromJxPathExisting();
183         testPerformanceFromJxPathModified();
184     }
185 
186     @Test
187     public void testPerformanceFromJxPathModified() {
188         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
189         List<SolrInputDocument> jxSolrDocs = new ArrayList<SolrInputDocument>();
190         List<SolrInputDocument> custSolrDocs = new ArrayList<SolrInputDocument>();
191         RequestDocument rd = new RequestDocument();
192         rd.setCategory(DocCategory.WORK.getCode());
193         rd.setType(DocType.INSTANCE.getCode());
194         rd.getContent().setContentObject(instance);
195         Long timeJxPath = System.currentTimeMillis();
196         JXPathContext instance1 = JXPathContext.newContext(instance);
197         for (int i = 0; i < 1; i++) {
198             docBuilder.buildSolrInputDocumentsForInstanceByJXPathTest(instance1, instance, jxSolrDocs);
199             docBuilder.buildSolrInputDocumentsForHoldingByJXPathTest(instance1, instance, jxSolrDocs);
200             docBuilder.buildSolrInputDocumentsForItemsByJXPathTest(instance1, instance, jxSolrDocs);
201             //                        if (i == 0)
202             //                            System.out.println("Modified JS SOLR DOCS: \n" + jxSolrDocs);
203         }
204         timeJxPath = System.currentTimeMillis() - timeJxPath;
205         System.out.println("testPerformanceFromJxPathModified " + timeJxPath);
206     }
207 
208     @Test
209     public void testPerformanceFromJxPathExisting() {
210         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
211         List<SolrInputDocument> jxSolrDocs = new ArrayList<SolrInputDocument>();
212         List<SolrInputDocument> custSolrDocs = new ArrayList<SolrInputDocument>();
213         RequestDocument rd = new RequestDocument();
214         rd.setCategory(DocCategory.WORK.getCode());
215         rd.setType(DocType.INSTANCE.getCode());
216         rd.getContent().setContentObject(instance);
217         Long timeJxPath = System.currentTimeMillis();
218 
219         for (int i = 0; i < 10000; i++) {
220             docBuilder.buildSolrInputDocumentsForInstanceByJXPath(instance, jxSolrDocs);
221             docBuilder.buildSolrInputDocumentsForHoldingByJXPath(instance, jxSolrDocs);
222             docBuilder.buildSolrInputDocumentsForItemsByJXPath(instance, jxSolrDocs);
223             if (i == 0) {
224                 System.out.println("CUSTOM SOLR DOCS: \n" + custSolrDocs);
225             }
226         }
227         timeJxPath = System.currentTimeMillis() - timeJxPath;
228         System.out.println("testPerformanceFromJxPathExisting timeJxPath " + timeJxPath);
229     }
230 
231     @Test
232     public void testPerformanceWithoutJxPath() {
233         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
234         List<SolrInputDocument> jxSolrDocs = new ArrayList<SolrInputDocument>();
235         List<SolrInputDocument> custSolrDocs = new ArrayList<SolrInputDocument>();
236         RequestDocument rd = new RequestDocument();
237         rd.setCategory(DocCategory.WORK.getCode());
238         rd.setType(DocType.INSTANCE.getCode());
239         rd.getContent().setContentObject(instance);
240         Long timeJxPath = System.currentTimeMillis();
241         for (int i = 0; i < 10000; i++) {
242             docBuilder.buildSolrInputDocumentsForinstance(instance, custSolrDocs);
243             RequestDocument requestDocument=new RequestDocument();
244             docBuilder.buildSolrInputDocumentsForHolding(instance, custSolrDocs, requestDocument);
245             docBuilder.buildSolrInputDocumentsForItems(instance, custSolrDocs, requestDocument);
246             //            if (i == 0)
247             //                System.out.println("CUSTOM SOLR DOCS: \n" + custSolrDocs);
248         }
249         timeJxPath = System.currentTimeMillis() - timeJxPath;
250         System.out.println("testPerformanceWithoutJxPath timeJxPath " + timeJxPath);
251     }
252 
253     @Test
254     public void testUpdateSolrDocumentsWithLinkedDocs() throws Exception {
255         //IndexerService indexerService = IndexerServiceImpl.getInstance();
256         WorkInstanceOlemlDocBuilder docBuilder = new WorkInstanceOlemlDocBuilder();
257         String instanceRequestFile = "/work/instance/oleml/instanceOneRec-Test1.xml";
258         Request request = getRequestObject(instanceRequestFile);
259         List<RequestDocument> requestDocumentList = request.getRequestDocuments();
260         List<SolrInputDocument> solrInputDocuments = null;
261         List<SolrDocument> solrDocuments = null;
262         for (RequestDocument requestDocument : requestDocumentList) {
263             requestDocument.setUuid("UUID" + (long) (Math.random() * 100000000));
264             getIndexerService(requestDocument).indexDocument(requestDocument);
265             solrDocuments = getIndexerService(requestDocument).getSolrDocumentBySolrId(requestDocument.getId());
266             for (SolrDocument solrDocument : solrDocuments) {
267                 docBuilder.updateSolrDocument(requestDocument, solrInputDocuments, solrDocument);
268             }
269         }
270     }
271 
272     private Request getRequestObject(String resourceLocation) throws Exception {
273         URL resource = getClass().getResource(resourceLocation);
274         File file = new File(resource.toURI());
275         RequestHandler requestHandler = new RequestHandler();
276         Request request = requestHandler.toObject(FileUtils.readFileToString(file));
277         return request;
278     }
279 
280     private IndexerService getIndexerService(RequestDocument requestDocument) {
281         IndexerService indexerService = DocumentIndexerManagerFactory.getInstance().getDocumentIndexManager(requestDocument.getCategory(), requestDocument.getType(), requestDocument.getFormat());
282         return indexerService;
283     }
284 
285 }