View Javadoc
1   /*
2    * Copyright 2011 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.service;
17  
18  import org.apache.commons.io.FileUtils;
19  import org.junit.After;
20  import org.junit.Before;
21  import org.kuali.ole.BaseTestCase;
22  import org.kuali.ole.RepositoryManager;
23  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
24  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
25  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.jcr.Session;
30  import java.io.File;
31  import java.net.URL;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import static org.junit.Assert.fail;
36  
37  /**
38   * Class to DocumentIndexer_UT.
39   *
40   * @author Rajesh Chowdary K
41   * @created Feb 23, 2012
42   */
43  public class DocumentIndexer_UT
44          extends BaseTestCase {
45  
46      private static final Logger LOG = LoggerFactory.getLogger(DocumentIndexer_UT.class);
47      private DocumentIndexer documentIndexer = new DocumentIndexer();
48      private List<RequestDocument> reqDocuments = new ArrayList<RequestDocument>();
49      private Request req;
50      private Session session;
51  
52      /**
53       * Method to setUp
54       *
55       * @throws java.lang.Exception
56       */
57      @Before
58      public void setUp() throws Exception {
59          URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
60          File file = new File(resource.toURI());
61          String fileContent = FileUtils.readFileToString(file);
62          RequestHandler rh = new RequestHandler();
63          req = rh.toObject(fileContent);
64          reqDocuments = req.getRequestDocuments();
65          session = RepositoryManager.getRepositoryManager().getSession(req.getUser(), req.getOperation());
66          DocumentIngester ingester = new DocumentIngester();
67          for (RequestDocument doc : reqDocuments) {
68              ingester.ingestBibNLinkedInstanceRequestDocuments(doc, session);
69          }
70      }
71  
72      /**
73       * Method to tearDown
74       *
75       * @throws java.lang.Exception
76       */
77      @After
78      public void tearDown() throws Exception {
79          if (session != null) {
80              session.logout();
81          }
82      }
83  
84      /**
85       * /**
86       * Test method for {@link org.kuali.ole.docstore.service.DocumentIndexer#indexDocuments(java.util.List)}.
87       */
88  
89      public final void testIndexDocuments() {
90          try {
91              documentIndexer.indexDocuments(reqDocuments);
92          } catch (Exception e) {
93              LOG.info(e.getMessage(),e);
94              fail("Failed in indexing : " + e);
95          }
96  
97      }
98  
99      /**
100      * Method to testIndexDocumentsForBulk
101      */
102     public final void testIndexDocumentsForBulk() {
103         try {
104             documentIndexer.indexDocumentsForBulk(reqDocuments, true);
105         } catch (Exception e) {
106             LOG.info(e.getMessage(),e);
107             fail("Failed in indexing : " + e);
108         }
109     }
110 
111     /**
112      * Test method for {@link org.kuali.ole.docstore.service.DocumentIndexer#indexDocument(org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument)}.
113      */
114 
115     public final void testIndexDocument() {
116         try {
117             documentIndexer.indexDocuments(reqDocuments);
118         } catch (Exception e) {
119             LOG.info(e.getMessage(),e);
120             fail("Failed in indexing : " + e);
121         }
122     }
123 
124     /**
125      * Test method for {@link org.kuali.ole.docstore.service.DocumentIndexer#rollbackIndexedData(java.util.List)}.
126      */
127 
128     public final void testRollbackIndexedData() {
129         try {
130             documentIndexer.indexDocuments(reqDocuments);
131         } catch (Exception e) {
132             try {
133                 documentIndexer.rollbackIndexedData(reqDocuments);
134             } catch (Exception ex) {
135                 LOG.info(e.getMessage() , e);
136                 fail("Failed in rolling back of indexing : " + ex);
137             }
138         }
139 
140     }
141 
142 }