View Javadoc

1   package org.kuali.ole.service.impl;
2   
3   import org.apache.log4j.Logger;
4   import org.junit.Before;
5   import org.junit.Test;
6   import org.junit.runner.RunWith;
7   import org.kuali.ole.BibliographicRecordHandler;
8   import org.kuali.ole.OLEConstants;
9   import org.kuali.ole.OleBaseTestCase;
10  import org.kuali.ole.PropertyUtil;
11  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
12  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
13  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
14  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
15  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
16  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
17  import org.kuali.ole.editor.service.DiscoveryHelperService;
18  import org.kuali.ole.editor.service.DocstoreHelperService;
19  import org.kuali.ole.ingest.FileUtil;
20  import org.kuali.ole.ingest.pojo.OleOverlayAction;
21  import org.kuali.ole.ingest.pojo.OverlayOption;
22  import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
23  import org.kuali.ole.pojo.bib.BibliographicRecord;
24  import org.kuali.ole.pojo.bib.Collection;
25  import org.kuali.ole.service.OverlayHelperService;
26  import org.kuali.ole.service.OverlayRetrivalService;
27  import org.kuali.rice.krad.service.BusinessObjectService;
28  import org.kuali.rice.krad.service.KRADServiceLocator;
29  import org.mockito.MockitoAnnotations;
30  import org.springframework.test.context.ContextConfiguration;
31  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32  import org.springframework.test.context.transaction.TransactionConfiguration;
33  
34  import java.io.File;
35  import java.net.URL;
36  import java.net.URLEncoder;
37  import java.util.*;
38  
39  import static org.junit.Assert.assertEquals;
40  
41  /**
42   * Created with IntelliJ IDEA.
43   * User: ?
44   * Date: 11/29/12
45   * Time: 3:18 PM
46   * To change this template use File | Settings | File Templates.
47   */
48  
49  @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
50  @RunWith(value = SpringJUnit4ClassRunner.class)
51  @TransactionConfiguration(defaultRollback = true)
52  
53  public class OverlayHelperServiceImpl_UT extends OleBaseTestCase {
54      private static final Logger LOG = Logger.getLogger(OverlayHelperServiceImpl_UT.class);
55      private DocstoreHelperService docstoreHelperService;
56      private DiscoveryHelperService discoveryHelperService;
57      private WorkInstanceOlemlRecordProcessor workInstanceOlemlRecordProcessor;
58      private BibliographicRecordHandler bibliographicRecordHandler;
59      private ResponseHandler responseHandler;
60      private OverlayHelperService overlayHelperService;
61      private OverlayRetrivalService overlayRetrivalService;
62      private BusinessObjectService businessObjectService;
63      private String CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING = "docAction=ingestContent&stringContent=";
64      private String DOCSTORE_URL = "docstore.url";
65  
66      @Before
67      public void setUp() throws Exception {
68          MockitoAnnotations.initMocks(this);
69          overlayHelperService = new OverlayHelperServiceImpl();
70          docstoreHelperService = new DocstoreHelperService();
71          discoveryHelperService = new DiscoveryHelperService();
72          workInstanceOlemlRecordProcessor = new WorkInstanceOlemlRecordProcessor();
73          bibliographicRecordHandler = new BibliographicRecordHandler();
74          responseHandler = new ResponseHandler();
75          overlayRetrivalService = new OverlayRetrivalServiceImpl();
76          businessObjectService = KRADServiceLocator.getBusinessObjectService();
77      }
78  
79  /*    @Test
80      public void testUpdateBibRecord()throws Exception {
81          List<String> updationFieldList = new ArrayList<String>();
82          String existingBibUUID = creatBibliographicRecord();
83          BibliographicRecord newBibliographicRecord = getUpdatedBibliographicRecord();
84          BibliographicRecord oldBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
85          overlayHelperService.updateBibliographicRecordToDocStore(existingBibUUID, oldBibliographicRecord, newBibliographicRecord);
86          BibliographicRecord updatedBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
87          List<DataField> datafields = updatedBibliographicRecord.getDatafields();
88          for (DataField dataField : datafields) {
89              if (dataField.getTag().equals("245")) {
90                  List<SubField> newSubfields = dataField.getSubFields();
91                  for (SubField subField : newSubfields) {
92                      if (subField.getCode().equals("a")) {
93                          assertEquals("Sample Book", subField.getValue());
94                          if (LOG.isInfoEnabled()) {
95                              LOG.info("245 value--------->" + subField.getValue());
96                          }
97                      }
98  
99                  }
100             }
101 
102         }
103 
104     }*/
105 
106 /*    @Test
107     public void testReplaceBibRecord()throws Exception {
108         String existingBibUUID = creatBibliographicRecord();
109         BibliographicRecord newBibliographicRecord = getUpdatedBibliographicRecord();
110         overlayHelperService.updateReplaceBibliographicRecordToDocStore(existingBibUUID, newBibliographicRecord);
111         BibliographicRecord updatedBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
112         List<DataField> datafields = updatedBibliographicRecord.getDatafields();
113         for (DataField dataField : datafields) {
114             if (dataField.getTag().equals("245")) {
115                 List<SubField> newSubfields = dataField.getSubFields();
116                 for (SubField subField : newSubfields) {
117                     if (subField.getCode().equals("a")) {
118                         assertEquals("Sample Book", subField.getValue());
119                         if (LOG.isInfoEnabled()) {
120                             LOG.info("245 value--------->" + subField.getValue());
121                         }
122                     }
123 
124                 }
125             }
126         }
127     }*/
128 
129     @Test
130     public void testUpdateBibRecordExcludingGPF()throws Exception {
131         String existingBibUUID = creatBibliographicRecord();
132         String instancUUID = getInstanceUUIDUsingBibId(existingBibUUID);
133         BibliographicRecord newBibliographicRecord = getUpdatedBibliographicRecord();
134         BibliographicRecord oldBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
135         List<String> gpfFieldList = overlayRetrivalService.getGloballyProtectedFieldsList();
136         List<OverlayOption> overlayOptionList = getOverlayOption();
137         List<OleOverlayAction> overlayLookupActionList = getOverlayLookupActionList();
138         HashMap<String,String> uuids = new HashMap<String, String>();
139         uuids.put(OLEConstants.BIBlIOGRAPHICUUID,existingBibUUID);
140         uuids.put(OLEConstants.OVERLAY_INSTANCEUUID,instancUUID);
141         //String response = overlayHelperService.updateBibliographicRecordExcludingGPFToDocStore(existingBibUUID, oldBibliographicRecord, newBibliographicRecord, gpfFieldList, overlayOptionList);
142         BibliographicRecord response = overlayHelperService.updateBibliographicRecordExcludingGPF(oldBibliographicRecord, newBibliographicRecord, gpfFieldList, overlayOptionList);
143         BibliographicRecord updatedBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
144         List<DataField> datafields = updatedBibliographicRecord.getDatafields();
145         for (DataField dataField : datafields) {
146             if (dataField.getTag().equals("690")) {
147                 List<SubField> newSubfields = dataField.getSubFields();
148                 for (SubField subField : newSubfields) {
149                     if (subField.getCode().equals("a")) {
150                         assertEquals("20", subField.getValue());
151                         if (LOG.isInfoEnabled()) {
152                             LOG.info("690 value--------->" + subField.getValue());
153                         }
154                     }
155 
156                 }
157             }
158 
159         }
160     }
161 
162     @Test
163     public void testUpdateBibRecordIncludingGPF()throws Exception{
164         String existingBibUUID = creatBibliographicRecord();
165         String instancUUID = getInstanceUUIDUsingBibId(existingBibUUID);
166         BibliographicRecord newBibliographicRecord = getUpdatedBibliographicRecord();
167         BibliographicRecord oldBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
168         List<String> gpfFieldList = overlayRetrivalService.getGloballyProtectedFieldsList();
169         List<OverlayOption> overlayOptionList = getOverlayOption();
170         List<OleOverlayAction> overlayLookupActionList = getOverlayLookupActionList();
171         HashMap<String,String> uuids = new HashMap<String, String>();
172         uuids.put(OLEConstants.BIBlIOGRAPHICUUID,existingBibUUID);
173         uuids.put(OLEConstants.OVERLAY_INSTANCEUUID,instancUUID);
174         BibliographicRecord response = overlayHelperService.updateBibliographicRecordIncludingGPF(oldBibliographicRecord, newBibliographicRecord, gpfFieldList, overlayOptionList);
175         BibliographicRecord updatedBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
176         List<DataField> datafields = updatedBibliographicRecord.getDatafields();
177         for (DataField dataField : datafields) {
178             if (dataField.getTag().equals("690")) {
179                 List<SubField> newSubfields = dataField.getSubFields();
180                 for (SubField subField : newSubfields) {
181                     if (subField.getCode().equals("a")) {
182                         assertEquals("20", subField.getValue());
183                         if (LOG.isInfoEnabled()) {
184                             LOG.info("690 value--------->" + subField.getValue());
185                         }
186                     }
187                 }
188             }
189         }
190     }
191 
192 /*    @Test
193     public void testUpdateBibliographicRecordByTagToDocStore()throws Exception {
194         List<String> updationFieldList = new ArrayList<String>();
195         updationFieldList.add("245---a");
196         String existingBibUUID = creatBibliographicRecord();
197         BibliographicRecord newBibliographicRecord = getUpdatedBibliographicRecord();
198         //updationFieldList.add("245");
199         BibliographicRecord oldBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
200         String response = overlayHelperService.updateBibliographicRecordByTagToDocStore(existingBibUUID, oldBibliographicRecord, newBibliographicRecord, updationFieldList);
201         BibliographicRecord updatedBibliographicRecord = docstoreHelperService.getBibliographicRecord(existingBibUUID);
202         List<DataField> datafields = updatedBibliographicRecord.getDatafields();
203         for (DataField dataField : datafields) {
204             if (dataField.getTag().equals("245")) {
205                 List<SubField> newSubfields = dataField.getSubFields();
206                 for (SubField subField : newSubfields) {
207                     if (subField.getCode().equals("a")) {
208                         assertEquals("Sample Book", subField.getValue());
209                         if (LOG.isInfoEnabled()) {
210                             LOG.info("245 value--------->" + subField.getValue());
211                         }
212                     }
213 
214                 }
215             }
216 
217         }
218     }*/
219 
220     @Test
221     public void testUpdateInstance()throws Exception{
222         OverlayHelperServiceImpl overlayHelperServiceImpl = new OverlayHelperServiceImpl();
223         String oldInstanceUUID = createNewInstanceRecordToDocstore();
224         if (null != oldInstanceUUID) {
225             InstanceCollection oldInstanceCollection = docstoreHelperService.getInstanceCollection(oldInstanceUUID);
226             overlayHelperServiceImpl.updateInstanceToDocstore(oldInstanceUUID, oldInstanceCollection, getUpdatedInstanceRecord());
227             String instanceXMLString = docstoreHelperService.getDocstoreData(oldInstanceUUID);
228             InstanceCollection updatedInstanceCollection = workInstanceOlemlRecordProcessor.fromXML(instanceXMLString);
229             List<Instance> updatedInstanceList = updatedInstanceCollection.getInstance();
230             Instance updatedInstance = updatedInstanceList.get(0);
231             OleHoldings updatedOleHoldings = updatedInstance.getOleHoldings();
232             String shelvingOrder = updatedOleHoldings.getCallNumber().getShelvingOrder().getCodeValue();
233             Items updatedItems = updatedInstance.getItems();
234             List<Item> updatedItemList = updatedItems.getItem();
235             assertEquals("ole-order", shelvingOrder);
236             assertEquals("444555", updatedItemList.get(0).getBarcodeARSL());
237         }
238     }
239 
240     @Test
241     public void deleteInstanceRec()throws Exception {
242         OverlayHelperServiceImpl overlayHelperServiceImpl = new OverlayHelperServiceImpl();
243         DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
244 
245         String uuid = createNewInstanceRecordToDocstore();
246         String responseXMLString = docstoreHelperService.getDocstoreData(uuid);
247         InstanceCollection instanceCollection = workInstanceOlemlRecordProcessor.fromXML(responseXMLString);
248         List<Instance> instanceList = instanceCollection.getInstance();
249         for (Instance instance : instanceList) {
250             responseXMLString = docstoreHelperService.deleteInstanceRecord(instance);
251         }
252         Response response = responseHandler.toObject(responseXMLString);
253         assertEquals("Success", response.getStatus());
254     }
255 
256     @Test
257     public void deleteDocStoreRecord()throws Exception {
258         String uuid = creatBibliographicRecord();
259         String responseXMLString = docstoreHelperService.deleteDocstoreRecord(uuid);
260         Response response = responseHandler.toObject(responseXMLString);
261         assertEquals("Success", response.getStatus());
262     }
263 
264     @Test
265     public void testCheckWildCardDetailsForGPF() throws Exception {
266         //String existingBibUUID = creatBibliographicRecord();
267         /*BibliographicRecord newBibliographicRecord= getUpdatedBibliographicRecord();
268         BibliographicRecord oldBibliographicRecord = overlayHelperService.getBibliographicRecord(existingBibUUID);
269         List<DataField> oldDatafields = oldBibliographicRecord.getDatafields();*/
270         List<DataField> oldDatafields = new ArrayList<DataField>();
271         int tag = 600;
272         for(int i=0;i<5;i++){
273             DataField dataField = new DataField();
274             dataField.setTag(String.valueOf(tag));
275             oldDatafields.add(dataField);
276             tag = tag+1;
277         }
278         List<String> gpfList = new ArrayList<String>();
279         gpfList.add("123");
280         gpfList.add("6**");
281         gpfList = new OverlayHelperServiceImpl().checkWildCardSearchForGPF(oldDatafields,gpfList);
282         LOG.info(" gpf ----------------------------> " + gpfList.toString());
283     }
284 
285     public String creatBibliographicRecord()throws Exception{
286         BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
287         URL resource = getClass().getResource("sample-marc.xml");
288         File file = new File(resource.toURI());
289         String xmlContent = new FileUtil().readFile(file);
290         //String responseFromDocstore = docstoreHelperService.persistToDocstoreForEditor(xmlContent, null, "marc");
291         //BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
292         Collection bibliographicRecords = bibliographicRecordHandler.fromXML(xmlContent);
293         BibliographicRecord bibliographicRecord = new BibliographicRecord();
294         for(BibliographicRecord tempBibliographicRecord : bibliographicRecords.getRecords()){
295             bibliographicRecord = tempBibliographicRecord;
296         }
297         String responseFromDocstore = docstoreHelperService.persistNewToDocstoreForIngest(bibliographicRecord,getProfileAttributeBos());
298         ResponseHandler responseHandler = new ResponseHandler();
299         Response response = responseHandler.toObject(responseFromDocstore);
300         String bibUUID = overlayHelperService.getUUID(response,OLEConstants.BIB_DOC_TYPE);
301         if(LOG.isInfoEnabled()){
302             LOG.info("bibUUID------------->"+bibUUID);
303         }
304         return bibUUID;
305     }
306 
307     public BibliographicRecord getUpdatedBibliographicRecord()throws Exception{
308         System.setProperty("app.environment", "local");
309         URL resource = getClass().getResource("sample-updated-marc.xml");
310         File file = new File(resource.toURI());
311         String xmlContent = new FileUtil().readFile(file);
312         Collection bibliographicRecords = bibliographicRecordHandler.fromXML(xmlContent);
313         List<BibliographicRecord> records = bibliographicRecords.getRecords();
314         BibliographicRecord bibliographicRecord = records.get(0);
315         return bibliographicRecord;
316     }
317 
318 
319     public String createNewInstanceRecordToDocstore() throws Exception {
320         URL resource = getClass().getResource("sample-instance.xml");
321         File file = new File(resource.toURI());
322         String instanceRecordContent = new FileUtil().readFile(file);
323         String responseFromDocstore = persistStringContentToDocstoreForIngest(instanceRecordContent);
324         ResponseHandler responseHandler = new ResponseHandler();
325         Response response = responseHandler.toObject(responseFromDocstore);
326         String instanceUUID = overlayHelperService.getUUID(response,OLEConstants.INSTANCE_DOC_TYPE);
327         if(LOG.isInfoEnabled()){
328             LOG.info("instanceUUID------------->"+instanceUUID);
329         }
330         return instanceUUID;
331     }
332 
333     private InstanceCollection getUpdatedInstanceRecord()throws Exception{
334         System.setProperty("app.environment", "local");
335         URL resource = getClass().getResource("sample-updated-instance.xml");
336         File file = new File(resource.toURI());
337         String instanceRecordContent = new FileUtil().readFile(file);
338         InstanceCollection instanceCollection = workInstanceOlemlRecordProcessor.fromXML(instanceRecordContent);
339         return instanceCollection;
340     }
341 
342     private List<OverlayOption> getOverlayOption() {
343         HashMap map = new HashMap();
344         map.put("agenda_name", OLEConstants.PROFILE_AGENDA_NM);
345         List<OverlayOption> overlayOptionList = (List<OverlayOption>) businessObjectService.findMatching(OverlayOption.class, map);
346         return overlayOptionList;
347     }
348     
349     private List<OleOverlayAction> getOverlayLookupActionList(){
350         HashMap map = new HashMap();
351         map.put("profileName", OLEConstants.PROFILE_AGENDA_NM);
352         List<OleOverlayAction> overlayLookupActionList = (List<OleOverlayAction>) businessObjectService.findMatching(OleOverlayAction.class, map);
353         return overlayLookupActionList;
354     }
355 
356     private List<ProfileAttributeBo> getProfileAttributeBos() {
357         List<ProfileAttributeBo> mockProfileAttributes = new ArrayList<ProfileAttributeBo>();
358         ProfileAttributeBo profileAttributeBo1 = new ProfileAttributeBo();
359         profileAttributeBo1.setAgendaName(OLEConstants.PROFILE_AGENDA_NM);
360         profileAttributeBo1.setId(1);
361         profileAttributeBo1.setAttributeName(OLEConstants.PROFILE_ATTRIBUTE_NM);
362         profileAttributeBo1.setAttributeValue(OLEConstants.PROFILE_ATTRIBUTE_VALUE);
363         mockProfileAttributes.add(profileAttributeBo1);
364         return mockProfileAttributes;
365     }
366 
367     public String persistStringContentToDocstoreForIngest(String requestXML) throws Exception {
368         String queryString = CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING + URLEncoder.encode(requestXML);
369         String docstoreURL = PropertyUtil.getPropertyUtil().getProperty(DOCSTORE_URL);
370         String responseXML = docstoreHelperService.postData(docstoreURL, queryString + queryString);
371         return responseXML;
372     }
373     
374     private String getInstanceUUIDUsingBibId(String bibUUID)throws Exception{
375         List responseFromSOLR =  discoveryHelperService.getResponseFromSOLR("DocType=bibliographic and id", bibUUID);
376         String instanceUUID = null;
377         for (Iterator iterator = responseFromSOLR.iterator(); iterator.hasNext(); ) {
378             Map map = (Map) iterator.next();
379             if (map.containsKey(OLEConstants.BIB_INSTANCE_ID)) {
380                 List list =(List)map.get(OLEConstants.BIB_INSTANCE_ID);
381                 instanceUUID =list !=null & list.size()>0 ?(String) list.get(0):null;
382                 break;
383             }
384         }
385         return instanceUUID;
386     }
387 }