View Javadoc

1   package org.kuali.ole.ingest;
2   
3   import org.junit.Before;
4   import org.junit.Test;
5   import org.junit.runner.RunWith;
6   import org.kuali.ole.ingest.pojo.OlePatron;
7   import org.kuali.ole.ingest.pojo.OlePatronGroup;
8   import org.kuali.ole.ingest.pojo.OlePatronPostalAddress;
9   import org.kuali.ole.patron.bo.OlePatronDocument;
10  import org.kuali.ole.patron.bo.OlePatronIngestSummaryRecord;
11  import org.kuali.ole.service.OlePatronConverterService;
12  import org.kuali.ole.service.OlePatronService;
13  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
14  import org.kuali.rice.krad.service.DocumentService;
15  import org.springframework.beans.factory.annotation.Autowired;
16  import org.springframework.context.ApplicationContext;
17  import org.springframework.test.context.ContextConfiguration;
18  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
19  import org.springframework.test.context.transaction.TransactionConfiguration;
20  import org.springframework.transaction.annotation.Transactional;
21  
22  import java.io.File;
23  import java.net.URL;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import static junit.framework.Assert.assertEquals;
28  import static junit.framework.Assert.assertNotNull;
29  import static junit.framework.Assert.assertTrue;
30  
31  /**
32   * Created by IntelliJ IDEA.
33   * User: pvsubrah
34   * Date: 3/27/12
35   * Time: 12:39 PM
36   * To change this template use File | Settings | File Templates.
37   */
38  
39  @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
40  @RunWith(value = SpringJUnit4ClassRunner.class)
41  @TransactionConfiguration(defaultRollback = true)
42  public class OlePatronConverterService_UT {
43      private OlePatronConverterService olePatronConverterService;
44  
45      @Autowired
46      private ApplicationContext applicationContext;
47  
48      @Before
49      public void setUp() throws Exception {
50          olePatronConverterService = new OlePatronConverterService();
51      }
52  
53      @Test
54      @Transactional
55      public void testCreatePatronRecordDocument() throws Exception {
56          URL resource = getClass().getResource("samplePatronRecord.xml");
57          File file = new File(resource.toURI());
58          String fileContent = new FileUtil().readFile(file);
59          OlePatronIngestSummaryRecord olePatronIngestSummaryRecord =  new OlePatronIngestSummaryRecord();
60          OlePatronService olePatronService = (OlePatronService) GlobalResourceLoader.getService("olePatronService");
61          olePatronConverterService.setOlePatronService(olePatronService);
62          List<OlePatronDocument> olePatronDocuments = olePatronConverterService.persistPatronFromFileContent(fileContent, true,"PatronIngest",olePatronIngestSummaryRecord);
63          assertNotNull(olePatronDocuments);
64          for (Iterator<OlePatronDocument> iterator = olePatronDocuments.iterator(); iterator.hasNext(); ) {
65              OlePatronDocument olePatronDocument = iterator.next();
66              assertNotNull(olePatronDocument.getOlePatronId());
67          }
68      }
69  
70      @Test
71      @Transactional
72      public void testUpdatePatronRecordDocument() throws Exception {
73          URL resource = getClass().getResource("samplePatronRecord.xml");
74          File file = new File(resource.toURI());
75          String fileContent = new FileUtil().readFile(file);
76          OlePatronIngestSummaryRecord olePatronIngestSummaryRecord =  new OlePatronIngestSummaryRecord();
77          OlePatronService olePatronService = (OlePatronService) GlobalResourceLoader.getService("olePatronService");
78          olePatronConverterService.setOlePatronService(olePatronService);
79          List<OlePatronDocument> olePatronDocuments = olePatronConverterService.persistPatronFromFileContent(fileContent, true,"PatronIngest",olePatronIngestSummaryRecord);
80          assertNotNull(olePatronDocuments);
81          assertTrue(olePatronDocuments.size() == 1);
82          for (Iterator<OlePatronDocument> iterator = olePatronDocuments.iterator(); iterator.hasNext(); ) {
83              OlePatronDocument olePatronDocument = iterator.next();
84              assertNotNull(olePatronDocument.getOlePatronId());
85              assertEquals(olePatronDocument.getName().getLastName(), "Sample");
86          }
87  
88          URL resource1 = getClass().getResource("UpdateSamplePatronRecord.xml");
89          File file1 = new File(resource1.toURI());
90          String fileContent1 = new FileUtil().readFile(file1);
91  
92          List<OlePatronDocument> olePatronDocuments2ndTime =
93                  olePatronConverterService.persistPatronFromFileContent(fileContent1, true, "PatronIngest",olePatronIngestSummaryRecord);
94  
95          assertTrue(olePatronDocuments2ndTime.size() == 1);
96          for (Iterator<OlePatronDocument> iterator = olePatronDocuments2ndTime.iterator(); iterator.hasNext(); ) {
97              OlePatronDocument olePatronDocument = iterator.next();
98              assertNotNull(olePatronDocument.getOlePatronId());
99              assertEquals(olePatronDocument.getName().getLastName(), "SampleChanged");
100         }
101 
102     }
103 
104     @Test
105     @Transactional
106     public void testBuildPatronFromFileContent() throws Exception {
107         URL resource = getClass().getResource("samplePatronRecord.xml");
108         File file = new File(resource.toURI());
109         String fileContent = new FileUtil().readFile(file);
110         OlePatronGroup olePatronGroup = olePatronConverterService.buildPatronFromFileContent(fileContent);
111         assertNotNull(olePatronGroup);
112         assertNotNull(olePatronGroup.getPatronGroup());
113         List<OlePatron> patrons = olePatronGroup.getPatronGroup();
114         assertTrue(!patrons.isEmpty());
115         for (Iterator<OlePatron> iterator = patrons.iterator(); iterator.hasNext(); ) {
116             OlePatron patron = iterator.next();
117             assertNotNull(patron);
118             assertNotNull(patron.getPatronID());
119             assertTrue(!patron.getPostalAddresses().isEmpty());
120             List<OlePatronPostalAddress> postalAddresses = patron.getPostalAddresses();
121             if (null != postalAddresses && !postalAddresses.isEmpty()) {
122                 for (Iterator<OlePatronPostalAddress> stringIterator = postalAddresses.iterator(); stringIterator.hasNext(); ) {
123                     OlePatronPostalAddress postalAddress = stringIterator.next();
124                     assertNotNull(postalAddress);
125                 }
126             }
127         }
128     }
129 
130 
131 }