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;
17  
18  import org.apache.commons.io.FileUtils;
19  import org.junit.Assert;
20  import org.junit.Before;
21  import org.junit.Ignore;
22  import org.junit.Test;
23  import org.kuali.ole.docstore.model.enums.DocCategory;
24  import org.kuali.ole.docstore.model.enums.DocFormat;
25  import org.kuali.ole.docstore.model.enums.DocType;
26  import org.kuali.ole.docstore.model.repopojo.FolderNode;
27  import org.kuali.ole.docstore.model.repopojo.RepositoryData;
28  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
29  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
30  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
31  import org.kuali.ole.docstore.service.BeanLocator;
32  import org.kuali.ole.docstore.service.IngestNIndexHandlerService;
33  import org.kuali.ole.pojo.OleException;
34  import org.mockito.MockitoAnnotations;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  import javax.jcr.Node;
39  import javax.jcr.RepositoryException;
40  import javax.jcr.Session;
41  import java.io.File;
42  import java.net.URL;
43  import java.util.Iterator;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.Set;
47  
48  import static junit.framework.Assert.assertNotNull;
49  import static org.junit.Assert.assertTrue;
50  
51  /**
52   * Created by IntelliJ IDEA.
53   * User: peris
54   * Date: 5/23/11
55   * Time: 10:20 AM
56   * To change this template use File | Settings | File Templates.
57   */
58  public class RepositoryBrowser_UT extends BaseTestCase
59           {
60  
61      private static final Logger                     LOG                        = LoggerFactory
62              .getLogger(RepositoryBrowser_UT.class);
63      private              IngestNIndexHandlerService ingestNIndexHandlerService = BeanLocator
64              .getIngestNIndexHandlerService();
65  
66      @Before
67      public void setUp() throws Exception {
68        //  super.setUp();
69         // MockitoAnnotations.initMocks(this);
70      }
71  
72               @Test
73               public void testBrowseRepository() throws Exception {
74                   RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
75                   List<OleDocStoreData> oleDocStoreDatas = repositoryBrowser.browseDataSetup();
76                   for (Iterator<OleDocStoreData> iterator = oleDocStoreDatas.iterator(); iterator.hasNext(); ) {
77                       OleDocStoreData oleDocStoreData = iterator.next();
78                       LOG.info(oleDocStoreData.getCategory());
79                       Map<String, List<String>> typeFormatMap = oleDocStoreData.getTypeFormatMap();
80                       Set<String> keys = typeFormatMap.keySet();
81                       for (Iterator<String> stringIterator = keys.iterator(); stringIterator.hasNext(); ) {
82                           String docType = stringIterator.next();
83                           LOG.info(docType + ": " + typeFormatMap.get(docType));
84                       }
85                   }
86               }
87  
88               @Test
89               public void testGetRandomUUIDs() throws Exception {
90                   URL url = this.getClass().getResource("repository/request.xml");
91                   File file = new File(url.toURI());
92  
93                   String docStoreDocuments = ingestNIndexHandlerService.ingestNIndexRequestDocuments(readFile(file));
94                   // System.out.println("testGetRandomUUIDs docStoreDocuments length "+docStoreDocuments);
95                   assertTrue(!docStoreDocuments.isEmpty());
96                   RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
97                   List<String> uuiDs = repositoryBrowser
98                           .getUUIDs(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.MARC.getCode(),
99                                     1);
100                  System.out.println("testGetRandomUUIDs uuiDs length " + uuiDs.size());
101                  if (uuiDs.size() > 0) {
102                      Assert.assertNotNull(uuiDs);
103                  }
104                  uuiDs = repositoryBrowser.getUUIDs(DocCategory.WORK.getCode(), DocType.LICENSE.getDescription(),
105                                                     DocFormat.ONIXPL.getCode(), 1);
106                  System.out.println("uuiDs LICENSE" + uuiDs);
107                  if (uuiDs.size() > 0) {
108                      Assert.assertNotNull(uuiDs);
109                  }
110                  uuiDs = repositoryBrowser
111                          .getUUIDs(DocCategory.WORK.getCode(), DocType.ITEM.getDescription(), DocFormat.OLEML.getCode(),
112                                    1);
113                  System.out.println("uuiDs ITEM" + uuiDs.size());
114                  Assert.assertNotNull(uuiDs);
115                  uuiDs = repositoryBrowser.getUUIDs(DocCategory.WORK.getCode(), DocType.HOLDINGS.getDescription(),
116                                                     DocFormat.OLEML.getCode(), 1);
117                  Assert.assertNotNull(uuiDs);
118                  System.out.println("uuiDs HOLDING" + uuiDs.size());
119                  for (Iterator<String> iterator = uuiDs.iterator(); iterator.hasNext(); ) {
120                      String uuid = iterator.next();
121                      LOG.info(uuid);
122                  }
123              }
124 
125              public void testGetUUIDs() {
126                  try {
127                      URL url = this.getClass().getResource("repository/request.xml");
128                      File file = new File(url.toURI());
129 
130                      String docStoreDocuments = ingestNIndexHandlerService.ingestNIndexRequestDocuments(readFile(file));
131                      //  System.out.println("testGetRandomUUIDs docStoreDocuments length "+docStoreDocuments);
132                      assertTrue(!docStoreDocuments.isEmpty());
133                      RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
134                      List<String> uuiDs = repositoryBrowser
135                              .getUUIDs(DocCategory.WORK.getCode(), DocType.BIB.getDescription(),
136                                        DocFormat.MARC.getCode(), 1, 2);
137 
138                      System.out.println("testGetRandomUUIDs uuiDs length " + uuiDs.size());
139                      assertTrue(!uuiDs.isEmpty());
140                      uuiDs = repositoryBrowser.getUUIDs(DocCategory.WORK.getCode(), DocType.LICENSE.getDescription(),
141                                                         DocFormat.ONIXPL.getCode(), 1, 1);
142 
143                      System.out.println("uuiDs LICENSE" + uuiDs);
144                      //assertTrue(!uuiDs.isEmpty());
145                      uuiDs = repositoryBrowser.getUUIDs(DocCategory.WORK.getCode(), DocType.ITEM.getDescription(),
146                                                         DocFormat.OLEML.getCode(), 1, 1);
147                      System.out.println("uuiDs ITEM" + uuiDs);
148                      //   assertTrue(!uuiDs.isEmpty());
149                      uuiDs = repositoryBrowser.getUUIDs(DocCategory.WORK.getCode(), DocType.HOLDINGS.getDescription(),
150                                                         DocFormat.OLEML.getCode(), 1, 1);
151                      System.out.println("uuiDs ITEM" + uuiDs);
152                      for (Iterator<String> iterator = uuiDs.iterator(); iterator.hasNext(); ) {
153                          String uuid = iterator.next();
154                          LOG.info(uuid);
155                      }
156                  }
157                  catch (Exception e) {
158                      e.printStackTrace();
159                  }
160              }
161 
162     @Test
163     public void testRepositoryNodeCount() throws Exception {
164         // MarcIngester marcIngester = new MarcIngester();
165         // marcIngester.setSolrIndexer(mockSolrIndexer);
166         // URL url = this.getClass().getResource("sample.xml");
167         // File file = new File(url.toURI());
168         // List<RequestDocument> docStoreDocuments = marcIngester.ingestDocument(readFile(file));
169         // assertTrue(!docStoreDocuments.isEmpty());
170 
171         RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
172         List<OleDocStoreData> oleDocStoreDataList = repositoryBrowser.getFilesCount();
173         assertNotNull(oleDocStoreDataList);
174         for (Iterator<OleDocStoreData> iterator = oleDocStoreDataList.iterator(); iterator.hasNext(); ) {
175             OleDocStoreData oleDocStoreData = iterator.next();
176             Map<String, Map<String, Long>> typeFormatMapWithNodeCount = oleDocStoreData.getTypeFormatMapWithNodeCount();
177             for (Iterator<String> oleDocStoreDataIterator = typeFormatMapWithNodeCount.keySet()
178                                                                                       .iterator(); oleDocStoreDataIterator
179                     .hasNext(); ) {
180                 String key = oleDocStoreDataIterator.next();
181                 LOG.info(key + ": " + typeFormatMapWithNodeCount.get(key));
182             }
183         }
184     }
185 
186     @Test
187     public void repoDump() throws RepositoryException, OleException {
188         RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
189         String dump = repositoryBrowser.getRepositoryDump();
190         LOG.info("dump" + dump);
191     }
192     @Test
193     public void getDetails() throws Exception{
194           RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager();
195         Session session = repositoryManager.getSession("mockUser", "test");
196         System.out.println("getName " + session.getWorkspace().getName());
197     }
198 
199     @Ignore
200     @Test
201     public void getRepositoryDumpRange() throws Exception {
202         RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
203         String category = DocCategory.WORK.getCode();
204         String type = DocType.BIB.getDescription();
205         String format = DocFormat.MARC.getCode();
206         int fromIndex = 1;
207         int Count = 1;
208         cleanRepository(category, type);
209         String beforedump = repositoryBrowser.getRepositoryDump();
210         LOG.info("beforedump" + beforedump);
211         loadBulkMarcRecords(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.MARC.getCode(),
212                             "/org/kuali/ole/bulkhandler/OLE-Bib-bulkIngest-IU-Set1-split.xml");
213         String dump = repositoryBrowser.getRepositoryDump();
214         LOG.info("dump" + dump);
215         String dumpRange = repositoryBrowser.getRepositoryRangeDump(category, type, format, fromIndex, Count);
216         LOG.info("dumpRange" + dumpRange);
217     }
218 
219     public void cleanRepository(String category, String type) throws OleException, RepositoryException {
220         RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager();
221         Session session = repositoryManager.getSession("mockUser", "test");
222         Node rootNode = session.getRootNode();
223         Node catNode = rootNode.getNode(category);
224         Node typeNode = catNode.getNode(type);
225         for (Iterator<Node> typeiterator = typeNode.getNodes(); typeiterator.hasNext(); ) {
226             Node formatNode = typeiterator.next();
227             if (!formatNode.getName().equals("jcr:system")) {
228                 formatNode.remove();
229             }
230         }
231         session.save();
232         repositoryManager.logout(session);
233     }
234 
235     private void loadBulkMarcRecords(String category, String type, String format, String inputXml) throws Exception {
236         File file = new File(getClass().getResource("/org/kuali/ole/repository/request.xml").toURI());
237         String input = FileUtils.readFileToString(file);
238         RequestHandler rh = new RequestHandler();
239         Request request = rh.toObject(input);
240         Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
241 
242         // BulkLoadHandler bulkLoadHandler = new BulkLoadHandler();
243         // assertNotNull(bulkLoadHandler);
244         // List<String> recordUUIDs = bulkLoadHandler.loadBulk(category,
245         // type,
246         // format,
247         // new File(getClass().getResource(inputXml).toURI()),
248         // "testUser", "testLoadBulk");
249         // assertNotNull(recordUUIDs);
250         // Assert.assertTrue(!recordUUIDs.isEmpty());
251         // for (Iterator<String> iterator = recordUUIDs.iterator(); iterator.hasNext(); ) {
252         // String recordUUID = iterator.next();
253         // LOG.info("recordUUID"+recordUUID);
254         // }
255     }
256 
257              @Test
258              public void loadRepository() throws OleException, RepositoryException {
259                  RepositoryManager oleRepositoryManager = RepositoryManager.getRepositoryManager();
260                  RepositoryData repositoryData = new RepositoryData();
261                  RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
262                  FolderNode folderNode = repositoryBrowser
263                          .computeNodeCount(oleRepositoryManager.getSession().getRootNode(), repositoryData);
264                  String repoDump = repositoryData.getRepositoryDump(repositoryData);
265                  LOG.info("repoDump" + repoDump);
266                  repositoryBrowser.browseRepositoryContent();
267                  repositoryBrowser.generateNodeCount();
268              }
269 
270          }