View Javadoc

1   package org.kuali.ole.ingest.action;
2   
3   import org.joda.time.DateTime;
4   import org.junit.Before;
5   import org.junit.Test;
6   import org.kuali.ole.BibliographicRecordHandler;
7   import org.kuali.ole.DataCarrierService;
8   import org.kuali.ole.OLEConstants;
9   import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
10  import org.kuali.ole.editor.service.DiscoveryHelperService;
11  import org.kuali.ole.ingest.FileUtil;
12  import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
13  import org.kuali.ole.pojo.bib.BibliographicRecord;
14  import org.kuali.ole.pojo.bib.Collection;
15  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
16  import org.kuali.rice.krms.api.engine.ExecutionOptions;
17  import org.kuali.rice.krms.api.engine.SelectionCriteria;
18  import org.kuali.rice.krms.api.engine.Term;
19  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
20  import org.kuali.rice.krms.framework.engine.BasicExecutionEnvironment;
21  import org.kuali.rice.krms.framework.engine.TermResolutionEngineImpl;
22  import org.mockito.Mock;
23  import org.mockito.Mockito;
24  import org.mockito.MockitoAnnotations;
25  import org.springframework.util.Assert;
26  
27  import java.io.File;
28  import java.net.URL;
29  import java.util.Arrays;
30  import java.util.Collections;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * Created by IntelliJ IDEA.
36   * User: vivekb
37   * Date: 5/11/12
38   * Time: 6:51 PM
39   * To change this template use File | Settings | File Templates.
40   */
41  public class UpdateItemAction_UT {
42  
43      @Mock
44      private ExecutionEnvironment mockExecutionEnvironment;
45  
46      @Mock
47      private DataCarrierService mockDataCarrierService;
48  
49      @Before
50      public void setUp() throws Exception {
51          MockitoAnnotations.initMocks(this);
52      }
53  
54      @Test
55      public void testRouteToPrincipal() throws Exception {
56          System.setProperty("app.environment", "local");
57          UpdateItemAction updateItemAction = new MockUpdateItemAction();
58          BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
59          URL resource = getClass().getResource("update-item-marc.xml");
60          File file = new File(resource.toURI());
61          String xmlContent = new FileUtil().readFile(file);
62          DiscoveryHelperService discoveryHelperService = new DiscoveryHelperService();
63  
64          Collection bibliographicRecords = bibliographicRecordHandler.fromXML(xmlContent);
65          List<BibliographicRecord> records = bibliographicRecords.getRecords();
66          List<ProfileAttributeBo> profileAttributeBos = getProfileAttributeBos();
67          for (Iterator<BibliographicRecord> iterator = records.iterator(); iterator.hasNext(); ) {
68              BibliographicRecord bibliographicRecord = iterator.next();
69              List list = discoveryHelperService.getResponseFromSOLR("020a", getIsbn(bibliographicRecord));
70              Mockito.when(mockDataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD)).thenReturn(bibliographicRecord);
71              Mockito.when(mockDataCarrierService.getData(OLEConstants.PROFILE_ATTRIBUTE_LIST)).thenReturn(profileAttributeBos);
72              Mockito.when(mockDataCarrierService.getData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE)).thenReturn(list);
73          }
74          updateItemAction.execute(getMockExecutionEnvironment());
75          Assert.isTrue((Boolean)mockExecutionEnvironment.getEngineResults().getAttribute(OLEConstants.UPDATE_ITEM_FLAG));
76      }
77  
78  
79  
80      private List<ProfileAttributeBo> getProfileAttributeBos() {
81          ProfileAttributeBo profileAttributeBo1 = new ProfileAttributeBo();
82          profileAttributeBo1.setAgendaName(OLEConstants.PROFILE_AGENDA_NM);
83          profileAttributeBo1.setId(1);
84          profileAttributeBo1.setAttributeName(OLEConstants.PROFILE_ATTRIBUTE_NM);
85          profileAttributeBo1.setAttributeValue(OLEConstants.PROFILE_ATTRIBUTE_VALUE);
86          List<ProfileAttributeBo> mockProfileAttributes = Arrays.asList(profileAttributeBo1);
87          return mockProfileAttributes;
88      }
89  
90      private String getIsbn(BibliographicRecord bibliographicRecord){
91          String isbn=null;
92          List<DataField> marcDataFields = bibliographicRecord.getDatafields() ;
93          for(DataField marcDataField :marcDataFields){
94                 if(marcDataField.getTag().equals("020")){
95                    isbn = marcDataField.getSubFields().get(0).getValue();
96                    break;
97              }
98          }
99         return isbn;
100     }
101 
102     public ExecutionEnvironment getMockExecutionEnvironment() {
103        SelectionCriteria sc1 =
104                SelectionCriteria.createCriteria(new DateTime(), Collections.<String, String>emptyMap(), Collections.singletonMap(
105                         AgendaDefinition.Constants.EVENT, "foo"));
106        mockExecutionEnvironment = new BasicExecutionEnvironment(sc1, Collections.<Term, Object>emptyMap(), new ExecutionOptions(), new TermResolutionEngineImpl());
107        return mockExecutionEnvironment;
108     }
109     private class MockUpdateItemAction extends UpdateItemAction{
110         @Override
111         protected DataCarrierService getDataCarrierService() {
112             return mockDataCarrierService;    //To change body of overridden methods use File | Settings | File Templates.
113         }
114     }
115 }