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.docstore.model.xmlpojo.ingest.Response;
11 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
12 import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
13 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
14 import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
15 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
16 import org.kuali.ole.editor.service.DocstoreHelperService;
17 import org.kuali.ole.ingest.FileUtil;
18 import org.kuali.ole.ingest.pojo.OverlayOption;
19 import org.kuali.ole.pojo.bib.BibliographicRecord;
20 import org.kuali.ole.pojo.bib.Collection;
21 import org.kuali.ole.service.OverlayHelperService;
22 import org.kuali.ole.service.OverlayRetrivalService;
23 import org.kuali.rice.krad.service.BusinessObjectService;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.mockito.MockitoAnnotations;
26 import org.springframework.test.context.ContextConfiguration;
27 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
28 import org.springframework.test.context.transaction.TransactionConfiguration;
29
30 import java.io.File;
31 import java.net.URL;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35
36 import static junit.framework.Assert.assertNotNull;
37 import static org.junit.Assert.assertEquals;
38
39
40
41
42
43
44
45
46
47 @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
48 @RunWith(value = SpringJUnit4ClassRunner.class)
49 @TransactionConfiguration(defaultRollback = true)
50
51 public class OverlayHelperServiceImpl_UT extends OleBaseTestCase {
52 private static final Logger LOG = Logger.getLogger(OverlayHelperServiceImpl_UT.class);
53 private DocstoreHelperService docstoreHelperService;
54 private WorkInstanceOlemlRecordProcessor workInstanceOlemlRecordProcessor;
55 private BibliographicRecordHandler bibliographicRecordHandler;
56 private ResponseHandler responseHandler;
57 private OverlayHelperService overlayHelperService;
58 private OverlayRetrivalService overlayRetrivalService;
59 private BusinessObjectService businessObjectService;
60
61 @Before
62 public void setUp() throws Exception {
63 MockitoAnnotations.initMocks(this);
64 overlayHelperService = new OverlayHelperServiceImpl();
65 docstoreHelperService = new DocstoreHelperService();
66 workInstanceOlemlRecordProcessor = new WorkInstanceOlemlRecordProcessor();
67 bibliographicRecordHandler = new BibliographicRecordHandler();
68 responseHandler = new ResponseHandler();
69 overlayRetrivalService = new OverlayRetrivalServiceImpl();
70 businessObjectService = KRADServiceLocator.getBusinessObjectService();
71 }
72
73 @Test
74 public void testUpdateBibRecord(){
75 try{
76
77 List<String> updationFieldList = new ArrayList<String>();
78 String existingBibUUID = creatBibliographicRecord();
79 BibliographicRecord newBibliographicRecord= getUpdatedBibliographicRecord();
80
81 BibliographicRecord oldBibliographicRecord = overlayHelperService.getBibliographicRecord(existingBibUUID);
82 overlayHelperService.updateBibliographicRecordToDocStore(existingBibUUID, oldBibliographicRecord, newBibliographicRecord);
83 BibliographicRecord updatedBibliographicRecord = overlayHelperService.getBibliographicRecord(existingBibUUID);
84 List<DataField> datafields = updatedBibliographicRecord.getDatafields();
85 for(DataField dataField : datafields){
86 if(dataField.getTag().equals("245")){
87 List<SubField> newSubfields = dataField.getSubFields();
88 for(SubField subField :newSubfields){
89 if(subField.getCode().equals("a")){
90 assertEquals("Sample Book",subField.getValue());
91 if(LOG.isInfoEnabled()){
92 LOG.info("245 value--------->" + subField.getValue());
93 }
94 }
95
96 }
97 }
98
99 }
100 }catch (Exception e){
101 e.printStackTrace();
102 }
103 }
104
105 @Test
106 public void testUpdateBibRecordExcludingGPF(){
107 try{
108 String existingBibUUID = creatBibliographicRecord();
109 BibliographicRecord newBibliographicRecord= getUpdatedBibliographicRecord();
110 BibliographicRecord oldBibliographicRecord = overlayHelperService.getBibliographicRecord(existingBibUUID);
111 List<String> gpfFieldList = overlayRetrivalService.getGloballyProtectedFieldsList();
112 List<OverlayOption> overlayOptionList = getOverlayOption();
113 String response = overlayHelperService.updateBibliographicRecordExcludingGPFToDocStore(existingBibUUID, oldBibliographicRecord, newBibliographicRecord, gpfFieldList,overlayOptionList);
114 BibliographicRecord updatedBibliographicRecord = overlayHelperService.getBibliographicRecord(existingBibUUID);
115 List<DataField> datafields = updatedBibliographicRecord.getDatafields();
116 for(DataField dataField : datafields){
117 if(dataField.getTag().equals("690")){
118 List<SubField> newSubfields = dataField.getSubFields();
119 for(SubField subField :newSubfields){
120 if(subField.getCode().equals("a")){
121 assertEquals("20",subField.getValue());
122 if(LOG.isInfoEnabled()){
123 LOG.info("690 value--------->" + subField.getValue());
124 }
125 }
126
127 }
128 }
129
130 }
131 }catch (Exception e){
132 e.printStackTrace();
133 }
134 }
135
136 @Test
137 public void testUpdateBibliographicRecordByTagToDocStore(){
138 try{
139
140 List<String> updationFieldList = new ArrayList<String>();
141 updationFieldList.add("245---a");
142 String existingBibUUID = creatBibliographicRecord();
143 BibliographicRecord newBibliographicRecord= getUpdatedBibliographicRecord();
144
145 BibliographicRecord oldBibliographicRecord = overlayHelperService.getBibliographicRecord(existingBibUUID);
146 String response = overlayHelperService.updateBibliographicRecordByTagToDocStore(existingBibUUID, oldBibliographicRecord, newBibliographicRecord,updationFieldList);
147 BibliographicRecord updatedBibliographicRecord = overlayHelperService.getBibliographicRecord(existingBibUUID);
148 List<DataField> datafields = updatedBibliographicRecord.getDatafields();
149 for(DataField dataField : datafields){
150 if(dataField.getTag().equals("245")){
151 List<SubField> newSubfields = dataField.getSubFields();
152 for(SubField subField :newSubfields){
153 if(subField.getCode().equals("a")){
154 assertEquals("Sample Book",subField.getValue());
155 if(LOG.isInfoEnabled()){
156 LOG.info("245 value--------->" + subField.getValue());
157 }
158 }
159
160 }
161 }
162
163 }
164 }catch (Exception e){
165 e.printStackTrace();
166 }
167 }
168
169 @Test
170 public void testUpdateInstance(){
171 try{
172 OverlayHelperServiceImpl overlayHelperServiceImpl = new OverlayHelperServiceImpl();
173 String oldInstanceUUID = createNewInstanceRecordToDocstore();
174 InstanceCollection oldInstanceCollection = overlayHelperServiceImpl.getInstanceCollection(oldInstanceUUID);
175 overlayHelperServiceImpl.updateInstanceToDocstore(oldInstanceUUID,oldInstanceCollection,getUpdatedInstanceRecord());
176 String instanceXMLString = docstoreHelperService.getDocstoreData(oldInstanceUUID);
177 InstanceCollection updatedInstanceCollection = workInstanceOlemlRecordProcessor.fromXML(instanceXMLString);
178 List<Instance> updatedInstanceList =updatedInstanceCollection.getInstance();
179 Instance updatedInstance = updatedInstanceList.get(0);
180 OleHoldings updatedOleHoldings = updatedInstance.getOleHoldings();
181 String shelvingOrder = updatedOleHoldings.getCallNumber().getShelvingOrder().getCodeValue();
182 Items updatedItems = updatedInstance.getItems();
183 List<Item> updatedItemList = updatedItems.getItem();
184 assertEquals("ole-order", shelvingOrder);
185 assertEquals("444555",updatedItemList.get(0).getBarcodeARSL() );
186 }catch (Exception e){
187 e.printStackTrace();
188 }
189 }
190
191 @Test
192 public void deleteInstanceRec(){
193 try{
194 OverlayHelperServiceImpl overlayHelperServiceImpl = new OverlayHelperServiceImpl();
195 DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
196
197 String uuid = createNewInstanceRecordToDocstore();
198 String responseXMLString = docstoreHelperService.getDocstoreData(uuid);
199 InstanceCollection instanceCollection = workInstanceOlemlRecordProcessor.fromXML(responseXMLString);
200 List<Instance> instanceList = instanceCollection.getInstance();
201 for(Instance instance : instanceList){
202 responseXMLString = docstoreHelperService.deleteInstanceRecord(instance);
203 }
204 Response response = responseHandler.toObject(responseXMLString);
205 assertEquals("Success",response.getStatus());
206 }catch (Exception e){
207 e.printStackTrace();
208 }
209 }
210
211 @Test
212 public void deleteDocStoreRecord(){
213 try{
214 String uuid = creatBibliographicRecord();
215 String responseXMLString = docstoreHelperService.deleteDocstoreRecord(uuid);
216 Response response = responseHandler.toObject(responseXMLString);
217 assertEquals("Success",response.getStatus());
218 }catch (Exception e){
219 e.printStackTrace();
220 }
221 }
222
223 public String creatBibliographicRecord()throws Exception{
224 BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
225 URL resource = getClass().getResource("sample-marc.xml");
226 File file = new File(resource.toURI());
227 String xmlContent = new FileUtil().readFile(file);
228 String responseFromDocstore = docstoreHelperService.persistToDocstoreForEditor(xmlContent, null, "marc");
229 ResponseHandler responseHandler = new ResponseHandler();
230 Response response = responseHandler.toObject(responseFromDocstore);
231 String bibUUID = overlayHelperService.getUUID(response,OLEConstants.BIB_DOC_TYPE);
232 if(LOG.isInfoEnabled()){
233 LOG.info("bibUUID------------->"+bibUUID);
234 }
235 return bibUUID;
236 }
237
238 public BibliographicRecord getUpdatedBibliographicRecord()throws Exception{
239 System.setProperty("app.environment", "local");
240 URL resource = getClass().getResource("sample-updated-marc.xml");
241 File file = new File(resource.toURI());
242 String xmlContent = new FileUtil().readFile(file);
243 Collection bibliographicRecords = bibliographicRecordHandler.fromXML(xmlContent);
244 List<BibliographicRecord> records = bibliographicRecords.getRecords();
245 BibliographicRecord bibliographicRecord = records.get(0);
246 return bibliographicRecord;
247 }
248
249
250 public String createNewInstanceRecordToDocstore() throws Exception {
251 URL resource = getClass().getResource("sample-instance.xml");
252 File file = new File(resource.toURI());
253 String instanceRecordContent = new FileUtil().readFile(file);
254
255 String responseFromDocstore = docstoreHelperService.instanceRecordCallToDocstore(instanceRecordContent, null, "oleml");
256 Response response = responseHandler.toObject(responseFromDocstore);
257 String instanceUUID = overlayHelperService.getUUID(response, OLEConstants.INSTANCE_DOC_TYPE);
258 if(LOG.isInfoEnabled()){
259 LOG.info("instanceUUID------------->" + instanceUUID);
260 }
261 return instanceUUID;
262 }
263
264 public InstanceCollection getUpdatedInstanceRecord()throws Exception{
265 System.setProperty("app.environment", "local");
266 URL resource = getClass().getResource("sample-updated-instance.xml");
267 File file = new File(resource.toURI());
268 String instanceRecordContent = new FileUtil().readFile(file);
269 InstanceCollection instanceCollection = workInstanceOlemlRecordProcessor.fromXML(instanceRecordContent);
270 return instanceCollection;
271 }
272
273 public List<OverlayOption> getOverlayOption() {
274 HashMap map = new HashMap();
275 map.put("agenda_name", OLEConstants.PROFILE_AGENDA_NM);
276 List<OverlayOption> matching = (List<OverlayOption>) businessObjectService.findMatching(OverlayOption.class, map);
277 return matching;
278 }
279 }