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