1 package org.kuali.ole.ingest.action;
2
3 import org.apache.log4j.Logger;
4 import org.joda.time.DateTime;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.kuali.ole.BibliographicRecordHandler;
9 import org.kuali.ole.DataCarrierService;
10 import org.kuali.ole.OLEConstants;
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.xstream.ingest.ResponseHandler;
14 import org.kuali.ole.editor.service.DiscoveryHelperService;
15 import org.kuali.ole.editor.service.DocstoreHelperService;
16 import org.kuali.ole.ingest.FileUtil;
17 import org.kuali.ole.ingest.pojo.OverlayLookupAction;
18 import org.kuali.ole.ingest.pojo.OverlayOption;
19 import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
20 import org.kuali.ole.pojo.bib.BibliographicRecord;
21 import org.kuali.ole.pojo.bib.Collection;
22 import org.kuali.ole.service.OverlayHelperService;
23 import org.kuali.ole.service.impl.OverlayHelperServiceImpl;
24 import org.kuali.rice.krad.service.BusinessObjectService;
25 import org.kuali.rice.krad.service.KRADServiceLocator;
26 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
27 import org.kuali.rice.krms.api.engine.ExecutionOptions;
28 import org.kuali.rice.krms.api.engine.SelectionCriteria;
29 import org.kuali.rice.krms.api.engine.Term;
30 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
31 import org.kuali.rice.krms.framework.engine.BasicExecutionEnvironment;
32 import org.kuali.rice.krms.framework.engine.TermResolutionEngineImpl;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38 import org.springframework.test.context.transaction.TransactionConfiguration;
39 import org.springframework.transaction.annotation.Transactional;
40 import org.springframework.util.Assert;
41
42 import java.io.File;
43 import java.net.URL;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.List;
48
49 import static org.junit.Assert.assertEquals;
50
51
52
53
54
55
56
57
58
59 @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
60 @RunWith(value = SpringJUnit4ClassRunner.class)
61 @TransactionConfiguration(defaultRollback = true)
62
63 public class UpdateBibExcludingGPFieldAction_UT {
64
65 private static final Logger LOG = Logger.getLogger(UpdateBibExcludingGPFieldAction_UT.class);
66 private DocstoreHelperService docstoreHelperService;
67 private OverlayHelperService overlayHelperService;
68 private BibliographicRecordHandler bibliographicRecordHandler;
69 private BusinessObjectService businessObjectService;
70 @Mock
71 private ExecutionEnvironment mockExecutionEnvironment;
72
73 @Mock
74 private DataCarrierService mockDataCarrierService;
75
76 @Before
77 public void setUp() throws Exception {
78 MockitoAnnotations.initMocks(this);
79 docstoreHelperService = new DocstoreHelperService();
80 overlayHelperService = new OverlayHelperServiceImpl();
81 bibliographicRecordHandler = new BibliographicRecordHandler();
82 businessObjectService = KRADServiceLocator.getBusinessObjectService();
83 }
84
85 @Test
86 @Transactional
87 public void testUpdateBibExcludingGPF()throws Exception{
88 System.setProperty("app.environment", "local");
89 UpdateBibExcludingGPFieldAction updateBibExcludingGPFieldAction = new MockUpdateBibExcludingGPFieldAction();
90 String bibUUID = creatBibliographicRecord();
91 DiscoveryHelperService discoveryHelperService = new DiscoveryHelperService();
92 BibliographicRecord bibliographicRecord = docstoreHelperService.getBibliographicRecord(bibUUID);
93 BibliographicRecord newBibliographicRecord= getUpdatedBibliographicRecord();
94 List<ProfileAttributeBo> profileAttributeBos = getProfileAttributeBos();
95 List<OverlayOption> overlayOptionList = getOverlayOption();
96 List<OverlayLookupAction> overlayLookupActionList = getOverlayLookupAction();
97 List list = discoveryHelperService.getResponseFromSOLR("020a", getIsbn(bibliographicRecord));
98 Mockito.when(mockDataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD)).thenReturn(newBibliographicRecord);
99 Mockito.when(mockDataCarrierService.getData(OLEConstants.PROFILE_ATTRIBUTE_LIST)).thenReturn(profileAttributeBos);
100 Mockito.when(mockDataCarrierService.getData(OLEConstants.OVERLAY_OPTION_LIST)).thenReturn(overlayOptionList);
101 Mockito.when(mockDataCarrierService.getData(OLEConstants.OVERLAY_LOOKUP_ACTION_LIST)).thenReturn(overlayLookupActionList);
102 Mockito.when(mockDataCarrierService.getData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE)).thenReturn(list);
103 updateBibExcludingGPFieldAction.execute(getMockExecutionEnvironment());
104 Assert.isTrue((Boolean) mockExecutionEnvironment.getEngineResults().getAttribute(OLEConstants.UPDATE_BIB_EXCLUDING_GPF));
105 }
106
107 public ExecutionEnvironment getMockExecutionEnvironment() {
108 SelectionCriteria sc1 =
109 SelectionCriteria.createCriteria(new DateTime(), Collections.<String, String>emptyMap(), Collections.singletonMap(
110 AgendaDefinition.Constants.EVENT, "foo"));
111 mockExecutionEnvironment = new BasicExecutionEnvironment(sc1, Collections.<Term, Object>emptyMap(), new ExecutionOptions(), new TermResolutionEngineImpl());
112 return mockExecutionEnvironment;
113 }
114 private class MockUpdateBibExcludingGPFieldAction extends UpdateBibExcludingGPFieldAction {
115 @Override
116 protected DataCarrierService getDataCarrierService() {
117 return mockDataCarrierService;
118 }
119 }
120
121 private List<ProfileAttributeBo> getProfileAttributeBos() {
122 ProfileAttributeBo profileAttributeBo1 = new ProfileAttributeBo();
123 profileAttributeBo1.setAgendaName(OLEConstants.PROFILE_AGENDA_NM);
124 profileAttributeBo1.setId(1);
125 profileAttributeBo1.setAttributeName(OLEConstants.PROFILE_ATTRIBUTE_NM);
126 profileAttributeBo1.setAttributeValue(OLEConstants.PROFILE_ATTRIBUTE_VALUE);
127 List<ProfileAttributeBo> mockProfileAttributes = Arrays.asList(profileAttributeBo1);
128 return mockProfileAttributes;
129 }
130
131 public List<OverlayOption> getOverlayOption() {
132 HashMap map = new HashMap();
133 map.put("agenda_name", OLEConstants.PROFILE_AGENDA_NM);
134 List<OverlayOption> matching = (List<OverlayOption>) businessObjectService.findMatching(OverlayOption.class, map);
135 return matching;
136 }
137
138 public List<OverlayLookupAction> getOverlayLookupAction() {
139 HashMap map = new HashMap();
140 map.put("agenda_name", OLEConstants.PROFILE_AGENDA_NM);
141 List<OverlayLookupAction> matching = (List<OverlayLookupAction>) businessObjectService.findMatching(OverlayLookupAction.class, map);
142 return matching;
143 }
144
145 private String getIsbn(BibliographicRecord bibliographicRecord){
146 String isbn=null;
147 List<DataField> marcDataFields = bibliographicRecord.getDatafields() ;
148 for(DataField marcDataField :marcDataFields){
149 if(marcDataField.getTag().equals("020")){
150 isbn = marcDataField.getSubFields().get(0).getValue();
151 break;
152 }
153 }
154 return isbn;
155 }
156
157 public String creatBibliographicRecord()throws Exception{
158 URL resource = getClass().getResource("sample-marc.xml");
159 File file = new File(resource.toURI());
160 String xmlContent = new FileUtil().readFile(file);
161 String responseFromDocstore = docstoreHelperService.persistToDocstoreForEditor(xmlContent, null, "marc");
162 ResponseHandler responseHandler = new ResponseHandler();
163 Response response = responseHandler.toObject(responseFromDocstore);
164 String bibUUID = overlayHelperService.getUUID(response,OLEConstants.BIB_DOC_TYPE);
165 if(LOG.isInfoEnabled()){
166 LOG.info("bibUUID------------->"+bibUUID);
167 }
168 return bibUUID;
169 }
170
171 public BibliographicRecord getUpdatedBibliographicRecord()throws Exception{
172 System.setProperty("app.environment", "local");
173 URL resource = getClass().getResource("sample-updated-marc.xml");
174 File file = new File(resource.toURI());
175 String xmlContent = new FileUtil().readFile(file);
176 Collection bibliographicRecords = bibliographicRecordHandler.fromXML(xmlContent);
177 List<BibliographicRecord> records = bibliographicRecords.getRecords();
178 BibliographicRecord bibliographicRecord = records.get(0);
179 return bibliographicRecord;
180 }
181 }