View Javadoc

1   package org.kuali.ole.service;
2   
3   import org.junit.Before;
4   import org.junit.Test;
5   import org.junit.runner.RunWith;
6   import org.kuali.ole.fixture.OlePatronDocumentFixture;
7   import org.kuali.ole.patron.api.OlePatronDefinition;
8   import org.kuali.ole.patron.api.OlePatronDefintionHelper;
9   import org.kuali.ole.patron.api.OlePatronQueryResults;
10  import org.kuali.ole.patron.bo.OleBorrowerType;
11  import org.kuali.ole.patron.bo.OlePatronDocument;
12  import org.kuali.rice.core.api.criteria.CriteriaLookupService;
13  import org.kuali.rice.core.api.criteria.GenericQueryResults;
14  import org.kuali.rice.core.api.criteria.Predicate;
15  import org.kuali.rice.core.api.criteria.QueryByCriteria;
16  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
17  import org.kuali.rice.kim.api.identity.IdentityService;
18  import org.kuali.rice.kim.api.identity.entity.EntityQueryResults;
19  import org.kuali.rice.kim.api.identity.name.EntityName;
20  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
21  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
22  import org.kuali.rice.kim.impl.identity.email.EntityEmailTypeBo;
23  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
24  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
25  import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
26  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
27  import org.kuali.rice.krad.UserSession;
28  import org.kuali.rice.krad.service.BusinessObjectService;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.context.ApplicationContext;
33  import org.springframework.test.context.ContextConfiguration;
34  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35  import org.springframework.test.context.transaction.TransactionConfiguration;
36  import org.springframework.transaction.annotation.Transactional;
37  
38  import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
39  import static org.kuali.rice.core.api.criteria.PredicateFactory.like;
40  
41  import java.util.*;
42  
43  import static junit.framework.Assert.*;
44  
45  /**
46   * Created by IntelliJ IDEA.
47   * User: pvsubrah
48   * Date: 5/9/12
49   * Time: 2:34 PM
50   * To change this template use File | Settings | File Templates.
51   */
52  
53  @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
54  @RunWith(value = SpringJUnit4ClassRunner.class)
55  @TransactionConfiguration(defaultRollback = true)
56  public class OlePatronService_UT {
57  
58      private OlePatronService olePatronService;
59      private BusinessObjectService businessObjectService;
60      private CriteriaLookupService criteriaLookupService;
61      IdentityService identityService;
62  
63      @Before
64      public void setUp() throws Exception {
65          olePatronService = new OlePatronServiceImpl();
66      }
67  
68      @Test
69      @Transactional
70      public void getPatron() {
71          OlePatronDefinition olePatronDefinition = OlePatronDefintionHelper.create();
72          OlePatronDefinition patron = olePatronService.createPatron(olePatronDefinition);
73          assertNotNull(patron);
74          String olePatronId = patron.getOlePatronId();
75          assertNotNull(olePatronId);
76          OlePatronDefinition persistedPatron = olePatronService.getPatron(olePatronId);
77          assertNotNull(persistedPatron);
78          assertEquals(persistedPatron.getOlePatronId(), olePatronId);
79      }
80  
81      @Test
82      @Transactional
83      public void createPatron() {
84          OlePatronDefinition olePatronDefinition = OlePatronDefintionHelper.create();
85          OlePatronDefinition patron = olePatronService.createPatron(olePatronDefinition);
86          assertNotNull(patron);
87          assertNotNull(patron.getOlePatronId());
88      }
89  
90      @Test
91      @Transactional
92      public void updatePatron() {
93          OlePatronDefinition olePatronDefinition = OlePatronDefintionHelper.create();
94          OlePatronDefinition patron = olePatronService.createPatron(olePatronDefinition);
95          assertNotNull(patron);
96          assertNotNull(patron.getOlePatronId());
97  
98          OlePatronDocument updatedPatron = OlePatronDocument.from(patron);
99          updatedPatron.setActiveIndicator(false);
100         OlePatronDefinition patronDefToBeUpdated = OlePatronDocument.to(updatedPatron);
101         OlePatronDefinition updatedPatronDefinition = olePatronService.updatePatron(patronDefToBeUpdated);
102         assertNotNull(updatedPatronDefinition);
103         assertFalse(updatedPatronDefinition.isActiveIndicator());
104 
105     }
106 
107     @Test
108     @Transactional
109     public void getPatrons() {
110         OlePatronDefinition olePatronDefinition = OlePatronDefintionHelper.create();
111         OlePatronDefinition patron = olePatronService.createPatron(olePatronDefinition);
112         assertNotNull(patron);
113         assertNotNull(patron.getOlePatronId());
114         OlePatronQueryResults patronResults = olePatronService.getPatrons();
115         assertNotNull(patronResults);
116         assertTrue(patronResults.getResults().size() > 0);
117     }
118 
119 
120     @Test
121     @Transactional
122     public void inactivatePatron() {
123         OlePatronDefinition olePatronDefinition = OlePatronDefintionHelper.create();
124         OlePatronDefinition patron = olePatronService.createPatron(olePatronDefinition);
125         assertNotNull(patron);
126         assertNotNull(patron.getOlePatronId());
127 
128         OlePatronDocument updatedPatron = OlePatronDocument.from(patron);
129         updatedPatron.setActiveIndicator(false);
130         OlePatronDefinition patronDefToBeUpdated = OlePatronDocument.to(updatedPatron);
131         OlePatronDefinition updatedPatronDefinition = olePatronService.updatePatron(patronDefToBeUpdated);
132         assertNotNull(updatedPatronDefinition);
133         assertFalse(updatedPatronDefinition.isActiveIndicator());
134 
135     }
136 
137     @Test
138     @Transactional
139     public void updateName() {
140         OlePatronDefinition olePatronDefinition = OlePatronDefintionHelper.create();
141         EntityNameTypeBo entityNameTypeBo = EntityNameTypeBo.from(olePatronDefinition.getName().getNameType());
142         OlePatronDefinition patron = olePatronService.createPatron(olePatronDefinition);
143         assertNotNull(patron);
144         assertNotNull(patron.getOlePatronId());
145         OlePatronDefinition persistedPatron = olePatronService.getPatron(patron.getOlePatronId());
146         OlePatronDocument patronToUpdate = OlePatronDocument.from(patron);
147         EntityNameBo nameBo = patronToUpdate.getEntity().getNames().get(0);
148         nameBo.setFirstName("Joe");
149         EntityName entityName = olePatronService.updateName(EntityNameBo.to(nameBo));
150         assertNotNull(entityName);
151 
152     }
153 
154     @Test
155     @Transactional
156     public void inactivateName() {
157 
158     }
159 
160     @Test
161     @Transactional
162     public void updateEmail() {
163         OlePatronDefinition olePatronDefinition = OlePatronDefintionHelper.create();
164         OlePatronDefinition patron = olePatronService.createPatron(olePatronDefinition);
165         assertNotNull(patron);
166         assertNotNull(patron.getOlePatronId());
167         OlePatronDocument savedPatron = OlePatronDocument.from(patron);
168         EntityEmailBo updatingEmail = savedPatron.getEntity().getEntityTypeContactInfos().get(0).getEmailAddresses().get(0);
169         assertNotNull(updatingEmail);
170         updatingEmail.setEmailAddress("update@mock");
171         assertTrue(olePatronService.updateEmail(EntityEmailBo.to(updatingEmail)));
172     }
173 
174 
175     @Test
176     @Transactional
177     public void findPatron() {
178         OlePatronDefinition olePatronDefinition = OlePatronDefintionHelper.create();
179         OlePatronDefinition patron = olePatronService.createPatron(olePatronDefinition);
180         assertNotNull(patron);
181         assertNotNull(patron.getOlePatronId());
182         assertEquals(patron.getEntity().getNames().get(0).getFirstName(),"Bob");
183         String firstName =  patron.getEntity().getNames().get(0).getFirstName();
184         OlePatronQueryResults patronQueryResults = olePatronService.findPatron(setUpQueryCriteria(firstName));
185         assertNotNull(patronQueryResults);
186         assertTrue(patronQueryResults.getResults().size()>0);
187     }
188 
189     private QueryByCriteria setUpQueryCriteria(String firstName) {
190         OlePatronHelperService olePatronHelperService = GlobalResourceLoader.getService("olePatronHelperService");
191         Map<String,String> criteria = new HashMap<String,String>(1);
192         criteria.put("firstName", firstName);
193         Map<String, String> entityCriteria = olePatronHelperService.convertPatronPropertiesToEntityProperties(criteria);
194         entityCriteria.put("entityTypeContactInfos.entityTypeCode", "PERSON");
195         QueryByCriteria.Builder query = QueryByCriteria.Builder.create();
196         List<Predicate> predicates = new ArrayList<Predicate>();
197         for (String key : entityCriteria.keySet()) {
198             predicates.add(like(key, entityCriteria.get(key)));
199         }
200         if (!predicates.isEmpty()) {
201             query.setPredicates(and(predicates.toArray(new Predicate[] {})));
202         }
203         return query.build();
204     }
205 
206 }