View Javadoc

1   package org.kuali.ole.docstore.engine.service;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.junit.Assert;
5   import org.junit.Before;
6   import org.junit.Test;
7   import org.kuali.ole.BaseTestCase;
8   import org.kuali.ole.docstore.common.document.*;
9   import org.kuali.ole.docstore.common.search.*;
10  import org.kuali.ole.docstore.common.service.DocstoreService;
11  import org.kuali.ole.docstore.model.enums.DocCategory;
12  import org.kuali.ole.docstore.model.enums.DocFormat;
13  import org.kuali.ole.docstore.model.enums.DocType;
14  import org.kuali.ole.docstore.service.BeanLocator;
15  import org.kuali.rice.krad.service.BusinessObjectService;
16  import org.mockito.Mock;
17  import org.mockito.MockitoAnnotations;
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import java.io.File;
22  
23  /**
24   * Created with IntelliJ IDEA.
25   * User: sambasivam
26   * Date: 12/16/13
27   * Time: 6:17 PM
28   * To change this template use File | Settings | File Templates.
29   */
30  public class DocstoreService_UT extends BaseTestCase {
31  
32      private static final Logger LOG = LoggerFactory.getLogger(DocstoreService_UT.class);
33      DocstoreService docstoreService = BeanLocator.getDocstoreService();
34  
35      @Mock
36      private BusinessObjectService businessObjectService;
37  
38      @Before
39      public void setUp() throws Exception {
40          super.setUp();
41          MockitoAnnotations.initMocks(this);
42      }
43  
44      @Test
45      public void testCreateBib() {
46          Bib bib = getBibRecord();
47          docstoreService.createBib(bib);
48          Assert.assertSame(bib.getId(), "123");
49  
50          //Stimulate an exception while storing.
51          //Assert.assertNull(bib.getId());
52  
53          //Stimulate an exception while indexing
54          //Assert.assertNotSame(bib.getId(), "123");
55  
56          //To clean up record
57          //docstoreService.deleteBib(bib.getId());
58  
59      }
60  
61      @Test
62      public void testCreateHoldings() {
63          Bib bib = getBibRecord();
64          docstoreService.createBib(bib);
65  
66          Holdings holdings = getHoldingsRecord();
67          docstoreService.createHoldings(holdings);
68  
69          Assert.assertEquals(holdings.getId(), "who-1");
70      }
71  
72      @Test
73      public void testCreateItem() {
74          Bib bib = getBibRecord();
75          docstoreService.createBib(bib);
76  
77          Holdings holdings = getHoldingsRecord();
78          docstoreService.createHoldings(holdings);
79          Item item = getItemRecord();
80          docstoreService.createItem(item);
81          Assert.assertEquals(item.getId(), "wio-1");
82      }
83  
84      @Test
85      public void testCreateHoldingsTree() {
86          HoldingsTree holdingsTree = getHoldingsTreeRecord();
87  
88          Bib bib = getBibRecord();
89          docstoreService.createBib(bib);
90  
91          docstoreService.createHoldingsTree(holdingsTree);
92          Assert.assertEquals(holdingsTree.getHoldings().getId(), "who-1");
93          Assert.assertEquals(holdingsTree.getItems().get(0).getId(), "wio-1");
94  
95      }
96  
97      @Test
98      public void testCreateBibTree() {
99          BibTree bibTree = new BibTree();
100         bibTree.setBib(getBibRecord());
101         bibTree.getHoldingsTrees().add(getHoldingsTreeRecord());
102         docstoreService.createBibTree(bibTree);
103         Assert.assertEquals(bibTree.getBib().getId(), "123");
104         HoldingsTree holdingsTree = bibTree.getHoldingsTrees().get(0);
105         Assert.assertEquals(holdingsTree.getHoldings().getId(), "who-1");
106         Assert.assertEquals(holdingsTree.getItems().get(0).getId(), "wio-1");
107 
108     }
109 
110     @Test
111     public void testDeleteBib() {
112         Bib bib = getBibRecord();
113         docstoreService.deleteBib(bib.getId());
114     }
115 
116     @Test
117     public void testDeleteHoldings() {
118         Holdings holdings = getHoldingsRecord();
119         docstoreService.deleteHoldings(holdings.getId());
120     }
121 
122     @Test
123     public void testDeleteItem() {
124         Item item = getItemRecord();
125         docstoreService.deleteItem(item.getId());
126     }
127 
128 
129     private HoldingsTree getHoldingsTreeRecord() {
130         HoldingsTree holdingsTree = new HoldingsTree();
131         holdingsTree.setHoldings(getHoldingsRecord());
132         holdingsTree.getItems().add(getItemRecord());
133         return holdingsTree;
134     }
135 
136     private Item getItemRecord() {
137         Item item = new ItemOleml();
138         item.setCategory(DocCategory.WORK.getCode());
139         item.setType(DocType.ITEM.getCode());
140         item.setFormat(DocFormat.OLEML.getCode());
141         return item;
142     }
143 
144     private Holdings getHoldingsRecord() {
145         Holdings holdings = new PHoldingsOleml();
146         holdings.setCategory(DocCategory.WORK.getCode());
147         holdings.setType(DocType.HOLDINGS.getCode());
148         holdings.setFormat(DocFormat.OLEML.getCode());
149         return holdings;
150     }
151 
152 
153     private Bib getBibRecord() {
154         Bib bib = new BibMarc();
155         bib.setCategory(DocCategory.WORK.getCode());
156         bib.setType(DocType.BIB.getCode());
157         bib.setFormat(DocFormat.MARC.getCode());
158         bib.setContent("<collection xmlns=\"http://www.loc.gov/MARC21/slim\">\n" +
159                 "    <record>\n" +
160                 "        <leader>#####nam#a22######a#4500</leader>\n" +
161                 "\t<controlfield tag=\"001\">3</controlfield>\n" +
162                 "        <controlfield tag=\"003\">OCoLC</controlfield>\n" +
163                 "        <controlfield tag=\"005\">20090213152530.7</controlfield>\n" +
164                 "\t\t<controlfield tag=\"008\">131031s########xxu###########000#0#eng#d</controlfield>\n" +
165                 "        <datafield tag=\"035\" ind1=\" \" ind2=\" \">\n" +
166                 "            <subfield code=\"a\">(OCoLC)ocm62378465</subfield>\n" +
167                 "        </datafield>\n" +
168                 "        <datafield tag=\"040\" ind1=\" \" ind2=\" \">\n" +
169                 "             <subfield code=\"a\">DLC</subfield>\n" +
170                 "\t\t\t <subfield code=\"c\">DLC</subfield>\n" +
171                 "\t\t\t <subfield code=\"d\">DLC</subfield>\n" +
172                 "\t\t\t <subfield code=\"d\">HLS</subfield>\n" +
173                 "\t\t\t <subfield code=\"d\">IUL</subfield>\n" +
174                 "        </datafield>\n" +
175                 "         <datafield tag=\"022\" ind1=\" \" ind2=\" \">\n" +
176                 "            <subfield code=\"a\">1729-1070|20</subfield>\n" +
177                 "        </datafield>\n" +
178                 "        <datafield tag=\"029\" ind1=\"1\" ind2=\" \">\n" +
179                 "            <subfield code=\"a\">AU@|b000040176476</subfield>\n" +
180                 "\t\t\t<subfield code=\"b\">000040176476</subfield>\n" +
181                 "        </datafield>\n" +
182                 "        <datafield tag=\"037\" ind1=\" \" ind2=\" \">\n" +
183                 "            <subfield code=\"b\">The Managing Editor, BIAC Journal, P.O. Box 10026, Gaborone, Botswana</subfield>\n" +
184                 "        </datafield>\n" +
185                 "        <datafield tag=\"042\" ind1=\" \" ind2=\" \">\n" +
186                 "            <subfield code=\"a\">lc</subfield>\n" +
187                 "        </datafield>\n" +
188                 "        <datafield tag=\"043\" ind1=\"1\" ind2=\"0\">\n" +
189                 "            <subfield code=\"a\">f-bs---</subfield>\n" +
190                 "        </datafield>\n" +
191                 "        <datafield tag=\"050\" ind1=\"0\" ind2=\"0\">\n" +
192                 "            <subfield code=\"a\">HD70.B55|bB53</subfield>\n" +
193                 "        </datafield>\n" +
194                 "        <datafield tag=\"049\" ind1=\" \" ind2=\" \">\n" +
195                 "            <subfield code=\"a\">IULA</subfield>\n" +
196                 "        </datafield>\n" +
197                 "         <datafield tag=\"210\" ind1=\"1\" ind2=\" \">\n" +
198                 "            <subfield code=\"a\">BIAC j.</subfield>\n" +
199                 "        </datafield>\n" +
200                 "         <datafield tag=\"222\" ind1=\" \" ind2=\"0\">\n" +
201                 "            <subfield code=\"a\">BIAC journal</subfield>\n" +
202                 "        </datafield>\n" +
203                 "         <datafield tag=\"245\" ind1=\"0\" ind2=\"0\">\n" +
204                 "            <subfield code=\"a\">BIAC journal</subfield>\n" +
205                 "        </datafield>\n" +
206                 "         <datafield tag=\"246\" ind1=\"1\" ind2=\"3\">\n" +
207                 "            <subfield code=\"a\">Botswana Institute of Administration and Commerce journal</subfield>\n" +
208                 "        </datafield>\n" +
209                 "        <datafield tag=\"260\" ind1=\" \" ind2=\" \">\n" +
210                 "            <subfield code=\"a\">Gaborone, Botswana :|bBotswana Institute of Administration and Commerce</subfield>\n" +
211                 "\t\t\t<subfield code=\"b\">Botswana Institute of Administration and Commerce</subfield>\n" +
212                 "        </datafield>\n" +
213                 "        <datafield tag=\"300\" ind1=\" \" ind2=\" \">\n" +
214                 "            <subfield code=\"a\">v. ;</subfield>\n" +
215                 "\t\t\t<subfield code=\"c\">24 cm.</subfield>\n" +
216                 "        </datafield>\n" +
217                 "\t\t<datafield tag=\"300\" ind1=\" \" ind2=\" \">\n" +
218                 "            <subfield code=\"a\">v. ;</subfield>\n" +
219                 "\t\t\t<subfield code=\"c\">24 cm.</subfield>\n" +
220                 "        </datafield>\n" +
221                 "\t\t<datafield tag=\"300\" ind1=\" \" ind2=\" \">\n" +
222                 "            <subfield code=\"a\">v. ;</subfield>\n" +
223                 "\t\t\t<subfield code=\"c\">24 cm.</subfield>\n" +
224                 "        </datafield>\n" +
225                 "\t\t<datafield tag=\"310\" ind1=\" \" ind2=\" \">\n" +
226                 "            <subfield code=\"a\">Semiannual</subfield>\n" +
227                 "        </datafield>\n" +
228                 "\t\t<datafield tag=\"362\" ind1=\"1\" ind2=\" \">\n" +
229                 "            <subfield code=\"a\"> Began in 2004.</subfield>\n" +
230                 "        </datafield>\n" +
231                 "\t\t<datafield tag=\"500\" ind1=\" \" ind2=\" \">\n" +
232                 "            <subfield code=\"a\">Description based on: Vol. 1, no. 1 (May. 2004); title from cover.</subfield>\n" +
233                 "        </datafield>\n" +
234                 "\t\t<datafield tag=\"500\" ind1=\" \" ind2=\" \">\n" +
235                 "            <subfield code=\"a\">Latest issue consulted: Vol. 3, no. 1 (May 2006).</subfield>\n" +
236                 "        </datafield>\n" +
237                 "\t\t<datafield tag=\"650\" ind1=\"0\" ind2=\" \">\n" +
238                 "            <subfield code=\"a\">Industrial management</subfield>\n" +
239                 "\t\t\t<subfield code=\"z\">Botswana</subfield>\n" +
240                 "\t\t\t<subfield code=\"v\">Periodicals.</subfield>\n" +
241                 "        </datafield>\n" +
242                 "\t\t<datafield tag=\"650\" ind1=\"0\" ind2=\" \">\n" +
243                 "            <subfield code=\"a\">Occupational training</subfield>\n" +
244                 "\t\t\t<subfield code=\"z\">Botswana</subfield>\n" +
245                 "\t\t\t<subfield code=\"v\">Periodicals.</subfield>\n" +
246                 "        </datafield>\n" +
247                 "\t\t<datafield tag=\"710\" ind1=\"2\" ind2=\" \">\n" +
248                 "            <subfield code=\"a\">Botswana Institute of Administration and Commerce.</subfield>\n" +
249                 "        </datafield>\n" +
250                 "\t\t<datafield tag=\"850\" ind1=\" \" ind2=\" \">\n" +
251                 "            <subfield code=\"a\">DLC</subfield>\n" +
252                 "        </datafield>\n" +
253                 "\t\t<datafield tag=\"891\" ind1=\"2\" ind2=\"0\">\n" +
254                 "            <subfield code=\"a\">9853|81.1</subfield>\n" +
255                 "\t\t\t<subfield code=\"a\">v.</subfield>\n" +
256                 "\t\t\t<subfield code=\"b\">no</subfield>\n" +
257                 "\t\t\t<subfield code=\"u\">2</subfield>\n" +
258                 "\t\t\t<subfield code=\"v\">r</subfield>\n" +
259                 "\t\t\t<subfield code=\"i\">(year)</subfield>\n" +
260                 "\t\t\t<subfield code=\"j\">(month)</subfield>\n" +
261                 "\t\t\t<subfield code=\"w\">f</subfield>\n" +
262                 "\t\t\t<subfield code=\"x\">05</subfield>\n" +
263                 "        </datafield>\n" +
264                 "\t\t<datafield tag=\"891\" ind1=\"4\" ind2=\"1\">\n" +
265                 "            <subfield code=\"a\">9863|81.1</subfield>\n" +
266                 "\t\t\t<subfield code=\"a\">1</subfield>\n" +
267                 "\t\t\t<subfield code=\"b\">1</subfield>\n" +
268                 "\t\t\t<subfield code=\"i\">2004</subfield>\n" +
269                 "\t\t\t<subfield code=\"j\">05</subfield>\n" +
270                 "        </datafield>\n" +
271                 "\t\t<datafield tag=\"596\" ind1=\" \" ind2=\" \">\n" +
272                 "            <subfield code=\"a\">1</subfield>\n" +
273                 "        </datafield>\n" +
274                 "    </record>\n" +
275                 "</collection>");
276         return bib;
277     }
278 
279     @Test
280     public void testRetrieveBib() {
281         String bibId = "123";
282         Bib bib = docstoreService.retrieveBib(bibId);
283         Assert.assertNotNull(bibId);
284     }
285 
286     @Test
287     public void testRetrieveHoldings() {
288         String holdingsId = "who-1";
289         Holdings holdings = docstoreService.retrieveHoldings(holdingsId);
290         Assert.assertNotNull(holdingsId);
291     }
292 
293     @Test
294     public void testRetrieveItem() {
295         String itemId = "wio-1";
296         Item item = docstoreService.retrieveItem(itemId);
297         Assert.assertNotNull(itemId);
298     }
299 
300     @Test
301     public void testRetrieveHoldingsTree() {
302         String holdingsId = "who-1";
303         HoldingsTree holdingsTree = docstoreService.retrieveHoldingsTree(holdingsId);
304         Assert.assertNotNull(holdingsId);
305     }
306 
307     @Test
308     public void testRetrieveBibTree() {
309         String bibId = "123";
310         BibTree bibTree = docstoreService.retrieveBibTree(bibId);
311         Assert.assertNotNull(bibId);
312     }
313 
314     @Test
315     public void testSearch1() {
316         SearchParams searchParams = getSearchParams1();
317         SearchResponse searchResponse = docstoreService.search(searchParams);
318         Assert.assertNotNull(searchResponse);
319     }
320 
321     @Test
322     public void testSearch2() {
323         SearchParams searchParams = getSearchParams2();
324         SearchResponse searchResponse = docstoreService.search(searchParams);
325         Assert.assertNotNull(searchResponse);
326     }
327 
328     @Test
329     public void testSearch3() {
330         SearchParams searchParams = getSearchParams3();
331         SearchResponse searchResponse = docstoreService.search(searchParams);
332         Assert.assertNotNull(searchResponse);
333     }
334 
335     public SearchParams getSearchParams1() {
336         SearchParams searchParams = new SearchParams();
337         String xml = getXmlAsString("/org/kuali/ole/search/SearchParams1.xml");
338         return (SearchParams) searchParams.deserialize(xml);
339     }
340 
341     public SearchParams getSearchParams2() {
342         SearchParams searchParams = new SearchParams();
343         String xml = getXmlAsString("/org/kuali/ole/search/SearchParams2.xml");
344         return (SearchParams) searchParams.deserialize(xml);
345     }
346 
347     public SearchParams getSearchParams3() {
348         SearchParams searchParams = new SearchParams();
349         String xml = getXmlAsString("/org/kuali/ole/search/SearchParams3.xml");
350         return (SearchParams) searchParams.deserialize(xml);
351     }
352 
353     public String getXmlAsString(String filePath) {
354         String input ="";
355         File file = null;
356         try {
357             file = new File(getClass().getResource(filePath).toURI());
358             input = FileUtils.readFileToString(file);
359         } catch (Exception e) {
360             LOG.error("Exception : ", e);
361         }
362         return input;
363     }
364 
365     /*@Test
366     public void testSearch4() {
367         SearchParams searchParams = getSearchParams4();
368         SearchResponse searchResponse = docstoreService.search(searchParams);
369         Assert.assertNotNull(searchResponse);
370     }
371 
372     public SearchParams getSearchParams4() {
373         SearchParams searchParams = new SearchParams();
374         SearchField searchField1 = searchParams.buildSearchField("item","CallNumber","123");
375         SearchField searchField2 = searchParams.buildSearchField("item","CopyNumber","111CN");
376         searchParams.getSearchConditions().add(searchParams.buildSearchCondition("OR",searchField1,"AND"));
377         searchParams.getSearchConditions().add(searchParams.buildSearchCondition("OR",searchField2,"AND"));
378         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic","Title"));
379         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic","Author"));
380         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic","CallNumber"));
381         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic","PublicationDate"));
382         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic","Publisher"));
383         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic","ISBN"));
384         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic","ISSN"));
385         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("holdings","CallNumber"));
386         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("item","Enumeration"));
387         searchParams.setPageSize(25);
388         searchParams.setStartIndex(0);
389         return searchParams;
390     }   */
391 
392     @Test
393     public void testUpdateBib() {
394         Bib bib = getBibRecord();
395         bib.setContent("mock update content");
396         docstoreService.updateBib(bib);
397     }
398 
399     @Test
400     public void testUpdateHoldings() {
401         Holdings holdings = getHoldingsRecord();
402         holdings.setContent("mock update content");
403         docstoreService.updateHoldings(holdings);
404     }
405 
406     @Test
407     public void testUpdateItem() {
408         Item item = getItemRecord();
409         item.setContent("mock update content");
410         docstoreService.updateItem(item);
411     }
412 
413 }