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.license.onixpl;
17  
18  import org.apache.solr.common.SolrInputDocument;
19  import org.junit.After;
20  import org.junit.Assert;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.kuali.ole.docstore.discovery.BaseTestCase;
24  import org.kuali.ole.docstore.discovery.solr.work.license.WorkLicenseCommonFields;
25  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
26  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
27  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
28  
29  import java.io.File;
30  import java.net.URL;
31  import java.util.ArrayList;
32  import java.util.List;
33  import java.util.UUID;
34  
35  /**
36   * Class to WorkLicenseOnixplDocBuilder.
37   *
38   * @author Sreekanth
39   */
40  public class WorkLicenseOnixplDocBuilder_UT
41          extends BaseTestCase {
42  
43      WorkLicenseOnixplDocBuilder workLicenseOnixplDocBuilder = new WorkLicenseOnixplDocBuilder();
44      List<SolrInputDocument> solrInputDocuments = null;
45      List<RequestDocument> requestDocumentList = null;
46  
47      @Before
48      public void setUp() throws Exception {
49      }
50  
51      @After
52      public void tearDown() throws Exception {
53          //To change body of created methods use File | Settings | File Templates.
54      }
55  
56      @Test
57      public void testLicenseInputFiles() throws Exception {
58          testLicenseInputFile1();
59          testLicenseInputFile2();
60          testLicenseInputFile3();
61          testLicenseInputFile4();
62          testLicenseInputFile5();
63      }
64  
65      public void testLicenseInputFile1() throws Exception {
66          try {
67              solrInputDocuments = new ArrayList<SolrInputDocument>();
68              String licenseRequestFile = "/work/license/onixpl/OLE-License-ONIXPL.xml";
69              indexONIXPLDocument(licenseRequestFile);
70              verifyFields(solrInputDocuments, requestDocumentList, "SpringerLink US Consortium License",
71                      "[Name of licensee]", "[Name of licensor]", "0104-2007", "supplemental, trial, regular");
72          } catch (Exception e) {
73              LOG.error(e.getMessage() , e);
74          }
75      }
76  
77  
78      public void testLicenseInputFile2() throws Exception {
79          try {
80              solrInputDocuments = new ArrayList<SolrInputDocument>();
81              String licenseRequestFile = "/work/license/onixpl/JISC_Collections_NESLi2_Licence_2011.xml";
82              indexONIXPLDocument(licenseRequestFile);
83              verifyFields(solrInputDocuments, requestDocumentList, "JISC Collections NESLi2 Licence 2011",
84                      "JISC_Collections", "NESLi2_Licence_2011", "0104-2008", "supplemental");
85          } catch (Exception e) {
86              LOG.error(e.getMessage() , e);
87          }
88      }
89  
90      public void testLicenseInputFile3() throws Exception {
91          try {
92              solrInputDocuments = new ArrayList<SolrInputDocument>();
93              String licenseRequestFile = "/work/license/onixpl/JISC_Collections_NESLi2_Licence_2012.xml";
94              indexONIXPLDocument(licenseRequestFile);
95              verifyFields(solrInputDocuments, requestDocumentList, "JISC Collections NESLi2 Licence 2012",
96                      "JISC_Collections", "NESLi2_Licence_2012", "0104-2009", "trial");
97          } catch (Exception e) {
98              LOG.error(e.getMessage() , e);
99          }
100     }
101 
102 
103     public void testLicenseInputFile4() throws Exception {
104         try {
105             solrInputDocuments = new ArrayList<SolrInputDocument>();
106             String licenseRequestFile
107                     = "/work/license/onixpl/Springer ESALC consortium license 081028 onix-pl-i-1223936166640.xml";
108             indexONIXPLDocument(licenseRequestFile);
109             verifyFields(solrInputDocuments, requestDocumentList, "Springer ESALC consortium license",
110                     "Participating members of Euphoria State Electronic Library Consortium",
111                     "Springer Science+Business Media LLC", "0104-2010", "regular");
112         } catch (Exception e) {
113             LOG.error(e.getMessage() , e);
114         }
115     }
116 
117 
118     public void testLicenseInputFile5() throws Exception {
119         try {
120             solrInputDocuments = new ArrayList<SolrInputDocument>();
121             String licenseRequestFile
122                     = "/work/license/onixpl/Springer US consortium template 081028 onix-pl-t-1222116235968.xml";
123             indexONIXPLDocument(licenseRequestFile);
124             verifyFields(solrInputDocuments, requestDocumentList, "Springer US consortium template",
125                     "Participating members of US Consortium", "Springer Science+Business Media LLC", "0104-2011",
126                     "local");
127         } catch (Exception e) {
128             LOG.error(e.getMessage() , e);
129         }
130     }
131 
132     public void indexONIXPLDocument(String inputFile) throws Exception {
133         URL resource = getClass().getResource(inputFile);
134         File file = new File(resource.toURI());
135         String requestContent = readFile(file);
136         RequestHandler requestHandler = new RequestHandler();
137         Request request = requestHandler.toObject(requestContent);
138         requestDocumentList = request.getRequestDocuments();
139         for (RequestDocument requestDocument : requestDocumentList) {
140             requestDocument.setUuid(String.valueOf(Math.round(Math.random())));
141             workLicenseOnixplDocBuilder.buildSolrInputDocument(requestDocument, solrInputDocuments);
142             Assert.assertNotNull(solrInputDocuments);
143             LOG.info("uuid-->" + requestDocument.getUuid());
144         }
145     }
146 
147 
148     public void verifyFields(List<SolrInputDocument> solrInputDocuments, List<RequestDocument> requestDocumentList,
149                              String inputTitle, String inputLicensee, String inputLicensor, String inputContractNum,
150                              String inputType) {
151         Object title = solrInputDocuments.get(0).getField(WorkLicenseCommonFields.TITLE_SEARCH).getValue();
152         LOG.info("title-->" + title);
153         Assert.assertTrue(title.toString().contains(inputTitle));
154         Object lisensee = solrInputDocuments.get(0).getField(WorkLicenseCommonFields.LICENSEE_SEARCH).getValue();
155         LOG.info("licensee-->" + lisensee);
156         Assert.assertEquals(inputLicensee, lisensee);
157         Object licesor = solrInputDocuments.get(0).getField(WorkLicenseCommonFields.LICENSOR_SEARCH).getValue();
158         LOG.info("licensor-->" + licesor);
159         Assert.assertEquals(inputLicensor, licesor);
160         Object contractNumber = solrInputDocuments.get(0).getField(WorkLicenseCommonFields.CONTRACT_NUMBER_SEARCH)
161                 .getValue();
162         LOG.info("contractNumber-->" + contractNumber);
163         Assert.assertEquals(inputContractNum, contractNumber);
164         for (RequestDocument requestDocument : requestDocumentList) {
165             String type = requestDocument.getAdditionalAttributes().getAttribute("type");
166             LOG.info("type-->" + type);
167             Assert.assertEquals(inputType, type);
168         }
169     }
170 }
171 
172