001package org.kuali.ole.service;
002
003import org.apache.commons.io.FileUtils;
004import org.apache.solr.client.solrj.SolrQuery;
005import org.apache.solr.client.solrj.response.QueryResponse;
006import org.apache.solr.common.SolrDocument;
007import org.apache.solr.common.SolrDocumentList;
008import org.junit.Before;
009import org.junit.Test;
010import org.kuali.ole.docstore.discovery.circulation.XmlContentHandler;
011import org.kuali.ole.docstore.discovery.circulation.json.CircInstance;
012import org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection;
013import org.kuali.ole.docstore.discovery.service.OleDocstoreDataRetrieveService;
014import org.kuali.ole.docstore.service.BeanLocator;
015import org.kuali.ole.docstore.service.IngestNIndexHandlerService;
016import org.kuali.ole.repository.CheckoutManager;
017import org.mockito.Mock;
018import org.mockito.Mockito;
019import org.mockito.MockitoAnnotations;
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022
023import java.io.File;
024import java.util.ArrayList;
025
026import static junit.framework.Assert.assertNotNull;
027
028/**
029 * Created with IntelliJ IDEA.
030 * User: ?
031 * Date: 2/7/13
032 * Time: 5:50 PM
033 * To change this template use File | Settings | File Templates.
034 */
035public class OleDocstoreDataRetrieveService_UT {
036
037    private static final Logger LOG = LoggerFactory.getLogger(OleDocstoreDataRetrieveService_UT.class);
038    private OleDocstoreDataRetrieveService oleDocstoreDataRetrieveService;
039    private IngestNIndexHandlerService ingestNIndexHandlerService = BeanLocator.getIngestNIndexHandlerService();
040
041    @Mock
042    private org.apache.solr.client.solrj.SolrServer mockSolrServer;
043    @Mock
044    private QueryResponse mockQueryResponse;
045    @Mock
046    private CheckoutManager mockCheckoutManager;
047
048    private SolrDocumentList mockSolrDocumentList;
049
050    private String mockResponse;
051
052
053    @Before
054    public void setUp() throws Exception {
055        mockResponse = FileUtils.readFileToString(new File(getClass().getResource("mock-instance-response.xml").toURI()));
056        System.out.println(mockResponse);
057        oleDocstoreDataRetrieveService = new OleDocstoreDataRetrieveService();
058        MockitoAnnotations.initMocks(this);
059        SolrDocument mockSolrDocument = new SolrDocument();
060        ArrayList<String> instanceIdentifierList = new ArrayList<String>();
061        instanceIdentifierList.add("instanceUUID1");
062        mockSolrDocument.put("instanceIdentifier", instanceIdentifierList);
063
064        mockSolrDocumentList = new SolrDocumentList();
065        mockSolrDocumentList.add(mockSolrDocument);
066
067        Mockito.when(mockSolrServer.query(Mockito.any(SolrQuery.class))).thenReturn(mockQueryResponse);
068        Mockito.when(mockQueryResponse.getResults()).thenReturn(mockSolrDocumentList);
069        Mockito.when(mockCheckoutManager.checkOut("instanceUUID1", null, "checkout")).thenReturn(mockResponse);
070    }
071
072    @Test
073    public void testGetJSONForCircInstance() throws Exception {
074        oleDocstoreDataRetrieveService.setCheckoutManager(mockCheckoutManager);
075        oleDocstoreDataRetrieveService.setSolrServer(mockSolrServer);
076
077        ArrayList<String> bibUUIDs = new ArrayList<String>();
078        bibUUIDs.add("uuid1");
079        String jsonFormat = oleDocstoreDataRetrieveService.getInstanceDetails(bibUUIDs, "json");
080        assertNotNull(jsonFormat);
081        System.out.println(jsonFormat);
082    }
083
084    @Test
085    public void testGetXMLResponse() throws Exception {
086        oleDocstoreDataRetrieveService.setCheckoutManager(mockCheckoutManager);
087        oleDocstoreDataRetrieveService.setSolrServer(mockSolrServer);
088
089        ArrayList<String> bibUUIDs = new ArrayList<String>();
090        bibUUIDs.add("uuid1");
091        bibUUIDs.add("uuid2");
092        String xmlFormat = oleDocstoreDataRetrieveService.getInstanceDetails(bibUUIDs, "xml");
093        assertNotNull(xmlFormat);
094        System.out.println(xmlFormat);
095    }
096}