View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.solr.client.solrj.SolrQuery;
5   import org.apache.solr.client.solrj.response.QueryResponse;
6   import org.apache.solr.common.SolrDocument;
7   import org.apache.solr.common.SolrDocumentList;
8   import org.junit.Before;
9   import org.junit.Test;
10  import org.kuali.ole.docstore.discovery.circulation.XmlContentHandler;
11  import org.kuali.ole.docstore.discovery.circulation.json.CircInstance;
12  import org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection;
13  import org.kuali.ole.docstore.discovery.service.OleDocstoreDataRetrieveService;
14  import org.kuali.ole.docstore.service.BeanLocator;
15  import org.kuali.ole.docstore.service.IngestNIndexHandlerService;
16  import org.kuali.ole.repository.CheckoutManager;
17  import org.mockito.Mock;
18  import org.mockito.Mockito;
19  import org.mockito.MockitoAnnotations;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  
26  import static junit.framework.Assert.assertNotNull;
27  
28  /**
29   * Created with IntelliJ IDEA.
30   * User: ?
31   * Date: 2/7/13
32   * Time: 5:50 PM
33   * To change this template use File | Settings | File Templates.
34   */
35  public class OleDocstoreDataRetrieveService_UT {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(OleDocstoreDataRetrieveService_UT.class);
38      private OleDocstoreDataRetrieveService oleDocstoreDataRetrieveService;
39      private IngestNIndexHandlerService ingestNIndexHandlerService = BeanLocator.getIngestNIndexHandlerService();
40  
41      @Mock
42      private org.apache.solr.client.solrj.SolrServer mockSolrServer;
43      @Mock
44      private QueryResponse mockQueryResponse;
45      @Mock
46      private CheckoutManager mockCheckoutManager;
47  
48      private SolrDocumentList mockSolrDocumentList;
49  
50      private String mockResponse;
51  
52  
53      @Before
54      public void setUp() throws Exception {
55          mockResponse = FileUtils.readFileToString(new File(getClass().getResource("mock-instance-response.xml").toURI()));
56          System.out.println(mockResponse);
57          oleDocstoreDataRetrieveService = new OleDocstoreDataRetrieveService();
58          MockitoAnnotations.initMocks(this);
59          SolrDocument mockSolrDocument = new SolrDocument();
60          ArrayList<String> instanceIdentifierList = new ArrayList<String>();
61          instanceIdentifierList.add("instanceUUID1");
62          mockSolrDocument.put("instanceIdentifier", instanceIdentifierList);
63  
64          mockSolrDocumentList = new SolrDocumentList();
65          mockSolrDocumentList.add(mockSolrDocument);
66  
67          Mockito.when(mockSolrServer.query(Mockito.any(SolrQuery.class))).thenReturn(mockQueryResponse);
68          Mockito.when(mockQueryResponse.getResults()).thenReturn(mockSolrDocumentList);
69          Mockito.when(mockCheckoutManager.checkOut("instanceUUID1", null, "checkout")).thenReturn(mockResponse);
70      }
71  
72      @Test
73      public void testGetJSONForCircInstance() throws Exception {
74          oleDocstoreDataRetrieveService.setCheckoutManager(mockCheckoutManager);
75          oleDocstoreDataRetrieveService.setSolrServer(mockSolrServer);
76  
77          ArrayList<String> bibUUIDs = new ArrayList<String>();
78          bibUUIDs.add("uuid1");
79          String jsonFormat = oleDocstoreDataRetrieveService.getInstanceDetails(bibUUIDs, "json");
80          assertNotNull(jsonFormat);
81          System.out.println(jsonFormat);
82      }
83  
84      @Test
85      public void testGetXMLResponse() throws Exception {
86          oleDocstoreDataRetrieveService.setCheckoutManager(mockCheckoutManager);
87          oleDocstoreDataRetrieveService.setSolrServer(mockSolrServer);
88  
89          ArrayList<String> bibUUIDs = new ArrayList<String>();
90          bibUUIDs.add("uuid1");
91          bibUUIDs.add("uuid2");
92          String xmlFormat = oleDocstoreDataRetrieveService.getInstanceDetails(bibUUIDs, "xml");
93          assertNotNull(xmlFormat);
94          System.out.println(xmlFormat);
95      }
96  }