View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation
3    *
4    * Licensed under the the Educational Community License, Version 1.0
5    * (the "License"); you may not use this file except in compliance
6    * with the License.  You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.kuali.student.enrollment.class1.lui.service.impl;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.util.ArrayList;
26  import java.util.Calendar;
27  import java.util.Date;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import javax.annotation.Resource;
33  import javax.persistence.Query;
34  
35  import junit.framework.Assert;
36  
37  import org.junit.Before;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  import org.junit.runner.RunWith;
41  import org.kuali.rice.core.api.criteria.GenericQueryResults;
42  import org.kuali.rice.core.api.criteria.PredicateFactory;
43  import org.kuali.rice.core.api.criteria.QueryByCriteria;
44  import org.kuali.student.enrollment.class1.lui.dao.LuiDao;
45  import org.kuali.student.enrollment.class1.lui.dao.LuiLuiRelationDao;
46  import org.kuali.student.enrollment.class1.lui.dao.LuiSetDao;
47  import org.kuali.student.enrollment.class1.lui.model.LuiEntity;
48  import org.kuali.student.enrollment.lui.dto.LuiIdentifierInfo;
49  import org.kuali.student.enrollment.lui.dto.LuiInfo;
50  import org.kuali.student.enrollment.lui.dto.LuiLuiRelationInfo;
51  import org.kuali.student.enrollment.lui.dto.LuiSetInfo;
52  import org.kuali.student.enrollment.lui.service.LuiService;
53  import org.kuali.student.r2.common.criteria.CriteriaLookupService;
54  import org.kuali.student.r2.common.dto.AttributeInfo;
55  import org.kuali.student.r2.common.dto.ContextInfo;
56  import org.kuali.student.r2.common.dto.RichTextInfo;
57  import org.kuali.student.r2.common.dto.StatusInfo;
58  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
59  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
60  import org.kuali.student.r2.lum.clu.dto.LuCodeInfo;
61  import org.springframework.test.context.ContextConfiguration;
62  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
63  import org.springframework.test.context.transaction.TransactionConfiguration;
64  import org.springframework.transaction.annotation.Transactional;
65  
66  @RunWith(SpringJUnit4ClassRunner.class)
67  @ContextConfiguration(locations = {"classpath:lui-test-context.xml"})
68  @TransactionConfiguration(transactionManager = "JtaTxManager", defaultRollback = true)
69  @Transactional
70  public class TestLuiServiceImpl {
71  
72      @Resource(name = "LuiService")
73      private LuiService luiService;
74  
75      @Resource(name = "luiDao")
76      private LuiDao luiDao;
77  
78      @Resource(name = "luiSetDao")
79      private LuiSetDao luiSetDao;
80  
81      @Resource(name= "criteriaLookupService")
82      private CriteriaLookupService criteriaLookupService;
83  
84  
85      public LuiLuiRelationDao getLuiLuiRelationDao() {
86          return luiLuiRelationDao;
87      }
88  
89      public void setLuiLuiRelationDao(LuiLuiRelationDao luiLuiRelationDao) {
90          this.luiLuiRelationDao = luiLuiRelationDao;
91      }
92  
93      @Resource(name = "luiLuiRelationDao")
94      private LuiLuiRelationDao luiLuiRelationDao;
95  
96      public LuiService getLuiService() {
97          return  luiService;
98      }
99  
100     public void setLuiService(LuiService luiService) {
101         this.luiService = luiService;
102     }
103 
104     public LuiDao getLuiDao() {
105         return luiDao;
106     }
107 
108     public void setLuiDao(LuiDao luiDao) {
109         this.luiDao = luiDao;
110     }
111 
112     public LuiSetDao getLuiSetDao() {
113         return luiSetDao;
114     }
115 
116     public void setLuiSetDao(LuiSetDao luiSetDao) {
117         this.luiSetDao = luiSetDao;
118     }
119 
120     public static String principalId = "123";
121     public ContextInfo callContext = null;
122 
123     @Before
124     public void setUp() {
125         principalId = "123";
126         callContext = new ContextInfo();
127         callContext.setPrincipalId(principalId);
128         try {
129             new LuiTestDataLoader(luiDao, luiLuiRelationDao).loadData();
130             
131             luiDao.getEm().flush();
132             
133         } catch (Exception ex) {
134             throw new RuntimeException (ex);
135         }
136     }
137 
138     @Test
139     public void testLuiServiceSetup() {
140         assertNotNull(luiService);
141     }
142 
143     @Test
144     public void testGetLui() throws Exception {
145 
146         try {
147             luiService.getLui("Lui-blah", callContext);
148             fail("should not exist");
149         } catch (DoesNotExistException ex) {
150             // expected
151         }
152 
153         LuiInfo obj = luiService.getLui("Lui-1", callContext);
154         assertNotNull(obj);
155         assertEquals("Lui one", obj.getName());
156         assertEquals(LuiServiceConstants.LUI_DRAFT_STATE_KEY, obj.getStateKey());
157         assertEquals(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY, obj.getTypeKey());
158 
159         assertEquals("Lui Desc 101", obj.getDescr().getPlain());
160 
161         assertTrue(obj.getResultValuesGroupKeys().contains("Lu-Rvgr-Lui-1"));
162 
163         assertNotNull(obj.getOfficialIdentifier());
164         assertEquals("Chem 123", obj.getOfficialIdentifier().getShortName());
165         assertNotNull(obj.getAlternateIdentifiers());
166         assertEquals("LUI-IDENT-2", obj.getAlternateIdentifiers().get(0).getId());
167 
168         assertNotNull(obj.getEffectiveDate());
169 
170         assertEquals("cluId1", obj.getCluId());
171         assertEquals("atpId1", obj.getAtpId());
172         assertEquals(1, obj.getLuiCodes().size());
173         assertEquals("Lu-Code-Lui-1", obj.getLuiCodes().get(0).getId());
174 
175         assertNotNull(obj.getMaximumEnrollment());
176         assertEquals(200, obj.getMaximumEnrollment().intValue());
177         assertNotNull(obj.getMinimumEnrollment());
178         assertEquals(50, obj.getMinimumEnrollment().intValue());
179         assertEquals("ref.url", obj.getReferenceURL());
180 
181         //Attributes
182         HashMap<String, String> attributes = new HashMap<String, String>();
183         for (AttributeInfo attributeInfo:obj.getAttributes()){
184             attributes.put(attributeInfo.getKey(), attributeInfo.getValue());
185         }
186         assertTrue(attributes.containsKey("attr1"));
187         assertTrue(attributes.containsKey("attr2"));
188 
189 //        assertNotNull(obj.getCluCluRelationIds());
190 //        assertEquals(2, obj.getCluCluRelationIds().size());
191 //        assertTrue(obj.getCluCluRelationIds().contains("CluClu-2"));
192 //        assertNotNull(obj.getUnitsContentOwnerOrgIds());
193 //        assertEquals(1, obj.getUnitsContentOwnerOrgIds().size());
194 //        assertTrue(obj.getUnitsContentOwnerOrgIds().contains("Org-2"));
195 //        assertNotNull(obj.getUnitsDeploymentOrgIds());
196 //        assertEquals(1, obj.getUnitsDeploymentOrgIds().size());
197 //        assertTrue(obj.getUnitsDeploymentOrgIds().contains("Org-1"));
198 //        assertNotNull(obj.getResultValuesGroupKeys());
199 //        assertEquals(3, obj.getResultValuesGroupKeys().size());
200 //        assertTrue(obj.getResultValuesGroupKeys().contains("Val-Group-3"));
201 //
202 //        assertNotNull(obj.getFeeIds());
203 //        assertEquals(3, obj.getFeeIds().size());
204 //        assertNotNull(obj.getRevenues());
205 //        assertEquals(2, obj.getRevenues().size());
206 //        assertNotNull(obj.getExpenditure());
207 //        assertEquals("LUI-Expen-1", obj.getExpenditure().getId());
208     }
209 
210     @Test
211     public void testGetLuiIdsByRelation() throws Exception {
212         try {
213             List<String> luiIds = luiService.getLuiIdsByRelatedLuiAndRelationType("Lui-2", "kuali.lui.lui.relation.associated", callContext);
214             assertNotNull(luiIds);
215             assertEquals(2, luiIds.size());
216             assertEquals("Lui-1", luiIds.get(0));
217             assertEquals("Lui-5", luiIds.get(1));
218         } catch (Exception e) {
219             fail(e.getMessage());
220         }
221     }
222 
223     @Test
224 //    @Ignore
225     public void testGetLuisByRelation() throws Exception {
226         try {
227             List<LuiInfo> luis = luiService.getLuisByRelatedLuiAndRelationType("Lui-2", "kuali.lui.lui.relation.associated", callContext);
228             assertNotNull(luis);
229             assertEquals(2, luis.size());
230             assertEquals("Lui-1", luis.get(0).getId());
231             assertEquals("Lui-5", luis.get(1).getId());
232         } catch (Exception e) {
233             e.printStackTrace();
234             fail(e.getMessage());
235         }
236     }
237 
238     // TODO: KSENROLL-3677 fix this thing
239     @Test
240     public void testGenericLookup() throws Exception {
241         QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
242         qbcBuilder.setPredicates(PredicateFactory.like("luiType", "kuali.lui.type.course.offering"));
243         QueryByCriteria criteria = qbcBuilder.build();
244 
245         try{
246             ArrayList<String> fields = new ArrayList<String>();
247             fields.add("name");
248             GenericQueryResults<List<String>> results = criteriaLookupService.genericLookup(LuiEntity.class, criteria, fields);
249             Iterator it = results.getResults().iterator();
250             while(it.hasNext()) {
251                 assertTrue(it.next() instanceof String);
252             }
253 
254             fields.add("luiType");
255             results = criteriaLookupService.genericLookup(LuiEntity.class, criteria, fields);
256             it = results.getResults().iterator();
257             while(it.hasNext()) {
258                 Object[] result = (Object[])it.next();
259                 int length = result.length;
260                 String[] resultsString = new String[length];
261                 for (int i = 0; i < length; i++) {
262                     resultsString[i] = result[i].toString();
263                 }
264                 assertTrue(length==2);
265             }
266         }   catch (Exception e){
267             e.printStackTrace();
268             fail(e.getMessage());
269         }
270 
271     }
272 
273     @Test
274     public void testCreateLui() throws Exception {
275         LuiInfo orig = new LuiInfo();
276         orig.setName("Test lui one");
277         orig.setStateKey(LuiServiceConstants.LUI_DRAFT_STATE_KEY);
278         orig.setTypeKey(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY);
279         RichTextInfo rtInfo = new RichTextInfo();
280         rtInfo.setFormatted("<p>Test lui one description</p>");
281         rtInfo.setPlain("Test lui one description");
282         orig.setDescr(rtInfo);
283         orig.setEffectiveDate(Calendar.getInstance().getTime());
284         orig.setMaximumEnrollment(25);
285         orig.setMinimumEnrollment(10);
286         orig.setReferenceURL("ref.create.url");
287 
288         orig.getResultValuesGroupKeys().add("Rvgr-2");
289 
290         LuiIdentifierInfo identifier = new LuiIdentifierInfo();
291         identifier.setShortName("identifier.shortname");
292         identifier.setTypeKey(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY);
293         identifier.setStateKey(LuiServiceConstants.LUI_IDENTIFIER_ACTIVE_STATE_KEY);
294         orig.setOfficialIdentifier(identifier);
295 
296         LuiIdentifierInfo identifier2 = new LuiIdentifierInfo();
297         identifier2.setShortName("alternate.identifier.shortname");
298         identifier2.setTypeKey(LuiServiceConstants.LUI_IDENTIFIER_CROSSLISTED_TYPE_KEY);
299         identifier2.setStateKey(LuiServiceConstants.LUI_IDENTIFIER_ACTIVE_STATE_KEY);
300         orig.getAlternateIdentifiers().add(identifier2);
301 
302         orig.setCluId("testCluId");
303         orig.setAtpId("testAtpId1");
304 
305         List<String> unitsContentOwner = new ArrayList<String>();
306         unitsContentOwner.add("Org-2");
307         orig.setUnitsContentOwner(unitsContentOwner);
308 
309         List<String> unitsDeployment = new ArrayList<String>();
310         unitsDeployment.add("Org-1");
311         orig.setUnitsDeployment(unitsDeployment);
312 
313         List<String> resultValueGroupKeys = new ArrayList<String>();
314         resultValueGroupKeys.add("Val-Group-3");
315         orig.setResultValuesGroupKeys(resultValueGroupKeys);
316 
317         LuCodeInfo luCode = new LuCodeInfo();
318         RichTextInfo rt = new RichTextInfo();
319         rt.setPlain("fee.plain");
320         rt.setFormatted("fee.formatted");
321         luCode.setDescr(rt);
322         orig.getLuiCodes().add(luCode);
323 
324         LuiInfo info = luiService.createLui(orig.getCluId(), orig.getAtpId(), orig.getTypeKey(), orig, callContext);
325         assertNotNull(info);
326         assertEquals(orig.getName(), info.getName());
327         assertEquals(orig.getStateKey(), info.getStateKey());
328         assertEquals(orig.getTypeKey(), info.getTypeKey());
329         assertEquals(orig.getEffectiveDate(), info.getEffectiveDate());
330         assertEquals(orig.getExpirationDate(), info.getExpirationDate());
331 
332         assertNotNull(info.getOfficialIdentifier());
333         assertEquals(orig.getOfficialIdentifier().getShortName(), info.getOfficialIdentifier().getShortName());
334         assertEquals(orig.getAlternateIdentifiers().size(), info.getAlternateIdentifiers().size());
335         assertEquals(orig.getAlternateIdentifiers().get(0).getShortName(), info.getAlternateIdentifiers().get(0).getShortName());
336 
337         assertEquals(orig.getMaximumEnrollment(), info.getMaximumEnrollment());
338         assertEquals(orig.getMinimumEnrollment(), info.getMinimumEnrollment());
339         assertEquals(orig.getCluId(), info.getCluId());
340         assertEquals(orig.getAtpId(), info.getAtpId());
341         assertEquals(orig.getReferenceURL(), info.getReferenceURL());
342 
343         assertTrue(info.getUnitsContentOwner().contains("Org-2"));
344         assertTrue(info.getUnitsDeployment().contains("Org-1"));
345         assertTrue(info.getResultValuesGroupKeys().contains("Val-Group-3"));
346 
347         assertEquals(1, info.getLuiCodes().size());
348 
349         info = luiService.getLui(info.getId(), callContext);
350         assertNotNull(info);
351         assertEquals(orig.getName(), info.getName());
352         assertEquals(orig.getStateKey(), info.getStateKey());
353         assertEquals(orig.getTypeKey(), info.getTypeKey());
354         assertEquals(orig.getEffectiveDate(), info.getEffectiveDate());
355         assertEquals(orig.getExpirationDate(), info.getExpirationDate());
356 
357         assertNotNull(info.getOfficialIdentifier());
358         assertEquals(orig.getOfficialIdentifier().getShortName(), info.getOfficialIdentifier().getShortName());
359         assertEquals(orig.getAlternateIdentifiers().size(), info.getAlternateIdentifiers().size());
360         assertEquals(orig.getAlternateIdentifiers().get(0).getShortName(), info.getAlternateIdentifiers().get(0).getShortName());
361 
362         assertEquals(orig.getMaximumEnrollment(), info.getMaximumEnrollment());
363         assertEquals(orig.getMinimumEnrollment(), info.getMinimumEnrollment());
364         assertEquals(orig.getCluId(), info.getCluId());
365         assertEquals(orig.getAtpId(), info.getAtpId());
366         assertEquals(orig.getReferenceURL(), info.getReferenceURL());
367     }
368 
369     @Test
370     public void  testGetLuisByAtpAndClu() throws Exception{
371         List<LuiInfo> luis =  luiService.getLuisByAtpAndClu("cluId1", "atpId1", callContext)  ;
372         assertTrue(luis.size()>0);
373         assertNotNull(luis);
374         assertEquals(1, luis.size());
375         LuiInfo onlyLui = luis.get(0);
376         assertNotNull(onlyLui);
377         assertEquals("Lui-1",  onlyLui.getId() );
378         assertEquals("Lui one", onlyLui.getName());
379         assertEquals("cluId1", onlyLui.getCluId());
380         assertEquals("atpId1", onlyLui.getAtpId());
381     }
382 
383     @Test
384     public void testUpdateLui() throws Exception {
385         LuiInfo info = luiService.getLui("Lui-1", callContext);
386         assertNotNull(info);
387         assertEquals("Lui-1", info.getId());
388         assertEquals("Lui one", info.getName());
389 
390         LuiInfo modified = new LuiInfo(info);
391         modified.setName("Lui one modified");
392         modified.setStateKey(LuiServiceConstants.LUI_AO_STATE_APPROVED_KEY);
393         modified.setMaximumEnrollment(25);
394         modified.setMinimumEnrollment(10);
395         assertNotNull(modified.getOfficialIdentifier());
396         modified.getOfficialIdentifier().setTypeKey(LuiServiceConstants.LUI_IDENTIFIER_OFFICIAL_TYPE_KEY);
397         modified.getOfficialIdentifier().setStateKey(LuiServiceConstants.LUI_IDENTIFIER_ACTIVE_STATE_KEY);
398         modified.getOfficialIdentifier().setShortName("identifier.shortname");
399 
400         modified.getAlternateIdentifiers().clear();
401         LuiIdentifierInfo identifier2 = new LuiIdentifierInfo();
402         identifier2.setShortName("alternate.identifier.shortname");
403         identifier2.setTypeKey(LuiServiceConstants.LUI_IDENTIFIER_CROSSLISTED_TYPE_KEY);
404         identifier2.setStateKey(LuiServiceConstants.LUI_IDENTIFIER_ACTIVE_STATE_KEY);
405         modified.getAlternateIdentifiers().add(identifier2);
406 
407         modified.setReferenceURL("ref.update.url");
408         modified.setCluId("updateCluId");
409         modified.setAtpId("updateAtpId1");
410 
411         modified.getResultValuesGroupKeys().add("Val-Group-11");
412 
413         //Attributes
414         List<AttributeInfo> attributes = new ArrayList<AttributeInfo>();
415         for (AttributeInfo attrInfo:modified.getAttributes()){
416             if (attrInfo.getKey().equals("attr2")){
417                 attributes.add(attrInfo);
418             }
419         }
420         AttributeInfo modAttr1 = new AttributeInfo();
421         modAttr1.setKey("modattr1");
422         modAttr1.setValue("modattr1");
423         attributes.add(modAttr1);
424 
425         AttributeInfo modAttr2 = new AttributeInfo();
426         modAttr2.setKey("modattr2");
427         modAttr2.setValue("modattr2");
428         attributes.add(modAttr2);
429         modified.setAttributes(attributes);
430 
431         LuCodeInfo luCode = new LuCodeInfo();
432         RichTextInfo rt = new RichTextInfo();
433         rt.setPlain("fee.plain");
434         rt.setFormatted("fee.formatted");
435         luCode.setDescr(rt);
436         luCode.setId("Modified Lu Code");
437         modified.getLuiCodes().clear();
438         modified.getLuiCodes().add(luCode);
439 
440         modified.getUnitsContentOwner().add("Org-22");
441 
442         modified.getUnitsDeployment().add("Org-11");
443 
444         LuiInfo updated = luiService.updateLui("Lui-1", modified, callContext);
445 
446         assertNotNull(updated);
447         assertEquals("Lui one modified", updated.getName());
448         assertEquals(LuiServiceConstants.LUI_AO_STATE_APPROVED_KEY, updated.getStateKey());
449         assertEquals(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY, updated.getTypeKey());
450         assertEquals(Integer.valueOf(25), updated.getMaximumEnrollment());
451         assertEquals(Integer.valueOf(10), updated.getMinimumEnrollment());
452         assertEquals("ref.update.url", updated.getReferenceURL());
453         assertNotNull(updated.getEffectiveDate());
454 
455         assertEquals(2, updated.getResultValuesGroupKeys().size());
456         assertTrue(updated.getResultValuesGroupKeys().contains("Val-Group-11"));
457 
458         assertNotNull(updated.getOfficialIdentifier());
459         assertEquals("identifier.shortname", updated.getOfficialIdentifier().getShortName());
460         assertEquals(1, updated.getAlternateIdentifiers().size());
461         assertEquals("alternate.identifier.shortname", updated.getAlternateIdentifiers().get(0).getShortName());
462 
463         //Attributes
464         HashMap<String, String> updatedAttributes = new HashMap<String, String>();
465         for (AttributeInfo attributeInfo:updated.getAttributes()){
466             updatedAttributes.put(attributeInfo.getKey(), attributeInfo.getValue());
467         }
468         assertFalse(updatedAttributes.containsKey("attr1"));
469         assertTrue(updatedAttributes.containsKey("attr2"));
470         assertTrue(updatedAttributes.containsKey("modattr1"));
471         assertTrue(updatedAttributes.containsKey("modattr2"));
472 
473         assertTrue(updated.getUnitsContentOwner().contains("Org-22"));
474         assertTrue(updated.getUnitsDeployment().contains("Org-11"));
475 
476         assertEquals(1, info.getLuiCodes().size());
477         assertEquals("Lu-Code-Lui-1", info.getLuiCodes().get(0).getId());
478         assertEquals(1, updated.getLuiCodes().size());
479         assertEquals("Modified Lu Code", updated.getLuiCodes().get(0).getId());
480     }
481 
482     @Test
483     public void testUpdateLuiLuiRelation() throws Exception{
484         LuiLuiRelationInfo info = luiService.getLuiLuiRelation("LUILUIREL-2", callContext);
485         assertNotNull(info);
486         info.setStateKey(LuiServiceConstants.LUI_LUI_RELATION_INACTIVE_STATE_KEY);
487         luiService.updateLuiLuiRelation(info.getId(), info, callContext);
488         assertEquals(info.getStateKey(), LuiServiceConstants.LUI_LUI_RELATION_INACTIVE_STATE_KEY);
489     }
490 
491     @Test
492     public void testGetLuiLuiRelation() throws Exception {
493         try {
494             LuiLuiRelationInfo obj = luiService.getLuiLuiRelation("LUILUIREL-1", callContext);
495             assertNotNull(obj);
496             assertEquals("Lui-1", obj.getLuiId());
497             assertEquals("Lui-2", obj.getRelatedLuiId());
498 
499         } catch (Exception ex) {
500             fail("exception from service call :" + ex.getMessage());
501         }
502     }
503 
504     @Test
505     public void testGetLuiLuiRelationsByLui() throws Exception {
506 
507         luiService.getLuiLuiRelationsByLui("Lui-Lui-Blah", callContext);
508         List<LuiLuiRelationInfo> objs = luiService.getLuiLuiRelationsByLui("Lui-1", callContext);
509         assertNotNull(objs);
510         assertEquals(1, objs.size());
511     }
512 
513     @Test
514     public void testGetLuiLuiRelationsByIds() throws Exception {
515         List<String> luiLuiRelationIds = new ArrayList<String>();
516         luiLuiRelationIds.add("LUILUIREL-1");
517         luiLuiRelationIds.add("LUILUIREL-2");
518         luiLuiRelationIds.add("LUILUIREL-3");
519 
520         List<LuiLuiRelationInfo> objs = luiService.getLuiLuiRelationsByIds(luiLuiRelationIds, callContext);
521         assertNotNull(objs);
522         assertEquals(3, objs.size());
523     }
524 
525     @Test
526     public void testCreateLuiLuiRelation() throws Exception{
527         LuiInfo info = new LuiInfo();
528         info.setCluId("testCluId");
529         info.setAtpId("testAtpId1");
530         info.setName("Test lui-Lui relation");
531         info.setStateKey(LuiServiceConstants.LUI_DRAFT_STATE_KEY);
532         info.setTypeKey(LuiServiceConstants.LAB_ACTIVITY_OFFERING_TYPE_KEY);
533         info.setEffectiveDate(Calendar.getInstance().getTime());
534         info.setMaximumEnrollment(25);
535         info.setMinimumEnrollment(10);
536         RichTextInfo rtInfo = new RichTextInfo();
537         rtInfo.setFormatted("<p>Test lui-Lui relation description</p>");
538         rtInfo.setPlain("Test lui-Lui relation description");
539         info.setDescr(rtInfo);
540 
541         LuiInfo newLui = luiService.createLui(info.getCluId(), info.getAtpId(), info.getTypeKey(), info, callContext);
542         LuiLuiRelationInfo created = null;
543         LuiLuiRelationInfo rel = new LuiLuiRelationInfo();
544         rel.setLuiId("Lui-1");
545         rel.setRelatedLuiId(newLui.getId());
546         rel.setStateKey(LuiServiceConstants.LUI_LUI_RELATION_ACTIVE_STATE_KEY);
547         rel.setTypeKey(LuiServiceConstants.LUI_LUI_RELATION_DELIVERED_VIA_FO_TO_AO_TYPE_KEY);
548         rel.setEffectiveDate(Calendar.getInstance().getTime());
549         rtInfo = new RichTextInfo();
550         rtInfo.setFormatted("<p>Test lui-Lui relation description</p>");
551         rtInfo.setPlain("Test lui-Lui relation description");
552         rel.setDescr(rtInfo);
553         created = luiService.createLuiLuiRelation("Lui-1", newLui.getId(), LuiServiceConstants.LUI_LUI_RELATION_DELIVERED_VIA_FO_TO_AO_TYPE_KEY, rel, callContext);
554 
555         assertNotNull(created);
556         assertEquals(LuiServiceConstants.LUI_LUI_RELATION_ACTIVE_STATE_KEY, created.getStateKey());
557         assertEquals(LuiServiceConstants.LUI_LUI_RELATION_DELIVERED_VIA_FO_TO_AO_TYPE_KEY, created.getTypeKey());
558 
559 
560 
561         LuiLuiRelationInfo retrieved = luiService.getLuiLuiRelation(created.getId(), callContext);
562         assertNotNull(retrieved);
563         assertEquals(LuiServiceConstants.LUI_LUI_RELATION_ACTIVE_STATE_KEY, retrieved.getStateKey());
564         assertEquals(LuiServiceConstants.LUI_LUI_RELATION_DELIVERED_VIA_FO_TO_AO_TYPE_KEY, retrieved.getTypeKey());
565 
566         List<LuiLuiRelationInfo> objs = luiService.getLuiLuiRelationsByLui("Lui-1", callContext);
567         assertNotNull(objs);
568         assertEquals(2, objs.size());
569         for (LuiLuiRelationInfo obj : objs) {
570             assertTrue(obj.getRelatedLuiId().equals("Lui-2") || obj.getRelatedLuiId().equals(newLui.getId()));
571         }
572     }
573 
574     @Test
575     public void testDeleteLui()
576             throws Exception{
577         LuiInfo info = luiService.getLui("Lui-3", callContext);
578         assertNotNull(info);
579 
580         List<LuiLuiRelationInfo> objs = luiService.getLuiLuiRelationsByLui("Lui-3", callContext);
581         assertNotNull(objs);
582         assertEquals(1, objs.size());
583 
584         try {
585             luiService.deleteLui("Lui-3-blah", callContext);
586         } catch (DoesNotExistException ee) {
587         }
588 
589         StatusInfo status = luiService.deleteLui("Lui-3", callContext);
590         assertTrue(status.getIsSuccess());
591 
592         luiService.getLuiLuiRelationsByLui("Lui-3", callContext);
593 
594         try {
595             luiService.getLui("Lui-3", callContext);
596         } catch (DoesNotExistException ee) {
597         }
598     }
599 
600     @Test
601     public void testGetLuisByIds() throws Exception {
602        List<String> luiIds = new ArrayList<String>();
603         luiIds.add("Lui-1");
604         luiIds.add("Lui-2");
605         luiIds.add("Lui-3");
606         List<LuiInfo> luis =  luiService.getLuisByIds(luiIds, callContext);
607 
608         assertNotNull(luis);
609         assertTrue(luis.size() == 3);
610 
611         try{
612             luiIds.add("Lui-3b");
613             luis =  luiService.getLuisByIds(luiIds, callContext);
614         }catch (DoesNotExistException ex) {
615             assertNotNull(ex.getMessage());
616         }
617 
618     }
619 
620 
621 
622     @Test
623     public void testGetLuiIdsByType() throws Exception {
624 
625         List<String> luis =  luiService.getLuiIdsByType("kuali.lui.type.course.offering", callContext);
626 
627         assertNotNull(luis);
628         assertTrue(luis.size() == 2);
629 
630     }
631 
632 
633     @Test
634     public void testGetLuiIdsByAtpAndType() throws Exception{
635         List<String> luiIds =  luiService.getLuiIdsByAtpAndType( "atpId1", "kuali.lui.type.course.offering", callContext);
636         assertNotNull(luiIds);
637         assertEquals(luiIds.size(), 1);
638         assertEquals(luiIds.get(0) ,"Lui-1");
639         List<String> luiIdsNonExistent =  luiService.getLuiIdsByAtpAndType( "atpId21", "kuali.lui.type.course.offering", callContext);
640         assertNotNull(luiIdsNonExistent);
641         assertEquals(luiIdsNonExistent.size(),0);
642 
643     }
644 
645     @Test
646     public void testGetLuisByAtpAndType() throws Exception{
647         List<LuiInfo> luis = luiService.getLuisByAtpAndType("atpId1", "kuali.lui.type.course.offering", callContext);
648         assertNotNull(luis);
649         assertEquals(luis.size(), 1);
650         assertEquals(luis.get(0).getId(), "Lui-1");
651 
652         List<LuiInfo> luisNonExistent =  luiService.getLuisByAtpAndType( "atpId21", "kuali.lui.type.course.offering", callContext);
653         assertNotNull(luisNonExistent);
654         assertEquals(luisNonExistent.size(),0);
655 
656     }
657 
658     @Test
659     public void testGetRelatedLuiIdsByLui() throws Exception{
660         List<String> luiRelationIds =  luiService.getLuiIdsByLuiAndRelationType("Lui-1", "kuali.lui.lui.relation.associated", callContext);
661         assertNotNull(luiRelationIds);
662         assertEquals( 1, luiRelationIds.size());
663         assertEquals("Lui-2", luiRelationIds.get(0) );
664 
665     }
666     @Test
667     public void testGetRelatedLuisByLui() throws Exception{
668         List<LuiInfo> luiRelations =  luiService.getRelatedLuisByLuiAndRelationType("Lui-3", "kuali.lui.lui.relation.associated", callContext);
669         assertNotNull(luiRelations);
670         assertEquals( 1, luiRelations.size());
671         assertEquals("Lui-4", luiRelations.get(0).getId() );
672 
673     }
674 
675     private LuiSetInfo createLuiSetInfo(){
676         LuiSetInfo luiSetInfo = new LuiSetInfo();
677         luiSetInfo.setName("Lui Set");
678         luiSetInfo.setTypeKey(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY);
679         luiSetInfo.setStateKey(LuiServiceConstants.LUI_SET_ACTIVE_STATE_KEY);
680         luiSetInfo.getLuiIds().add("Lui-1");
681         luiSetInfo.getLuiIds().add("Lui-2");
682         RichTextInfo descr = new RichTextInfo();
683         descr.setPlain("test");
684         luiSetInfo.setDescr(descr);
685         AttributeInfo attributeInfo = new AttributeInfo();
686         attributeInfo.setKey("test.key");
687         attributeInfo.setValue("test.value");
688         luiSetInfo.getAttributes().add(attributeInfo);
689         luiSetInfo.setEffectiveDate(new Date());
690         luiSetInfo.setExpirationDate(new Date());
691         return luiSetInfo;
692     }
693 
694     @Test
695     public void testCreateLuiSet() throws Exception {
696 
697         LuiSetInfo luiSetInfo = createLuiSetInfo();
698 
699         LuiSetInfo newLuiSet = luiService.createLuiSet(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY,luiSetInfo,callContext);
700 
701         assertNotNull(newLuiSet);
702         assertNotNull(newLuiSet.getId());
703         assertEquals(luiSetInfo.getName(),newLuiSet.getName());
704         assertEquals(luiSetInfo.getTypeKey(),newLuiSet.getTypeKey());
705         assertEquals(luiSetInfo.getStateKey(),newLuiSet.getStateKey());
706         assertEquals(luiSetInfo.getDescr().getPlain(),newLuiSet.getDescr().getPlain());
707         assertEquals(2,newLuiSet.getLuiIds().size());
708         assertEquals(1,newLuiSet.getAttributes().size());
709 
710         newLuiSet = luiService.getLuiSet(newLuiSet.getId(), callContext);
711         assertNotNull(newLuiSet);
712         assertNotNull(newLuiSet.getId());
713         assertEquals(luiSetInfo.getName(),newLuiSet.getName());
714         assertEquals(luiSetInfo.getTypeKey(),newLuiSet.getTypeKey());
715         assertEquals(luiSetInfo.getStateKey(),newLuiSet.getStateKey());
716         assertEquals(luiSetInfo.getDescr().getPlain(),newLuiSet.getDescr().getPlain());
717         assertEquals(2,newLuiSet.getLuiIds().size());
718     }
719 
720     @Test
721     public void testUpdateLuiSet() throws Exception {
722         LuiSetInfo luiSetInfo1 = createLuiSetInfo();
723 
724         LuiSetInfo newLuiSet = luiService.createLuiSet(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY,luiSetInfo1,callContext);
725         assertNotNull(newLuiSet);
726 
727         newLuiSet.setName("UpdateName");
728         newLuiSet.getLuiIds().add("Lui-3");
729         LuiSetInfo luiSetInfo2 = luiService.updateLuiSet(newLuiSet.getId(),newLuiSet,callContext);
730         assertNotNull(luiSetInfo2);
731         assertEquals(3,luiSetInfo2.getLuiIds().size());
732 
733         LuiSetInfo luiSetInfo3 = luiService.getLuiSet(luiSetInfo2.getId(),callContext);
734         assertNotNull(luiSetInfo3);
735         assertEquals(luiSetInfo2.getName(),luiSetInfo3.getName());
736         assertEquals(luiSetInfo2.getTypeKey(),luiSetInfo3.getTypeKey());
737         assertEquals(luiSetInfo2.getStateKey(),luiSetInfo3.getStateKey());
738         assertEquals(luiSetInfo2.getDescr().getPlain(), luiSetInfo3.getDescr().getPlain());
739         assertEquals(3,luiSetInfo3.getLuiIds().size());
740 
741         luiSetInfo3.getLuiIds().remove(0);
742         luiSetInfo3.getLuiIds().remove(1);
743         LuiSetInfo luiSetInfo4 = luiService.updateLuiSet(luiSetInfo3.getId(),luiSetInfo3,callContext);
744         assertNotNull(luiSetInfo4);
745         assertEquals(1,luiSetInfo4.getLuiIds().size());
746 
747     }
748 
749     @Test
750     public void testDeleteLuiSet() throws Exception {
751         LuiSetInfo luiSetInfo = createLuiSetInfo();
752         LuiSetInfo newLuiSet = luiService.createLuiSet(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY,luiSetInfo,callContext);
753         assertNotNull(newLuiSet);
754 
755         StatusInfo statusInfo = luiService.deleteLuiSet(newLuiSet.getId(),callContext);
756         assertTrue(statusInfo.getIsSuccess());
757 
758     }
759 
760     @Test
761     public void testGetLuiSetsByLui() throws Exception{
762         LuiSetInfo luiSetInfo = createLuiSetInfo();
763         LuiSetInfo newLuiSet = luiService.createLuiSet(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY,luiSetInfo,callContext);
764         assertNotNull(newLuiSet);
765 
766         List<LuiSetInfo> luiSetInfos = luiService.getLuiSetsByLui("Lui-1",callContext);
767         assertEquals(1,luiSetInfos.size());
768 
769         luiSetInfos = luiService.getLuiSetsByLui("Lui-5",callContext);
770         assertEquals(0,luiSetInfos.size());
771     }
772 
773     @Test
774     public void testGetLuiSetIdsByType() throws Exception{
775         LuiSetInfo luiSetInfo = createLuiSetInfo();
776         LuiSetInfo newLuiSet = luiService.createLuiSet(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY,luiSetInfo,callContext);
777         assertNotNull(newLuiSet);
778 
779         List<String> luiSetIdsByType = luiService.getLuiSetIdsByType(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY, callContext);
780         assertEquals(1,luiSetIdsByType.size());
781 
782         luiSetIdsByType = luiService.getLuiSetIdsByType("test.type.invalid",callContext);
783         assertEquals(0,luiSetIdsByType.size());
784     }
785 
786     @Test
787     public void testGetLuiIdsFromLuiSet() throws Exception{
788         LuiSetInfo luiSetInfo = createLuiSetInfo();
789         LuiSetInfo newLuiSet = luiService.createLuiSet(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY,luiSetInfo,callContext);
790         assertNotNull(newLuiSet);
791 
792         List<String> luiIds = luiService.getLuiIdsFromLuiSet(newLuiSet.getId(),callContext);
793         assertEquals(2,luiIds.size());
794     }
795 
796     @Test
797     public void testGetLuiSetsByIds() throws Exception{
798         LuiSetInfo luiSetInfo = createLuiSetInfo();
799         LuiSetInfo newLuiSet1 = luiService.createLuiSet(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY,luiSetInfo,callContext);
800         assertNotNull(newLuiSet1);
801 
802         luiSetInfo = createLuiSetInfo();
803         LuiSetInfo newLuiSet2 = luiService.createLuiSet(LuiServiceConstants.LUI_SET_COLOCATED_OFFERING_TYPE_KEY,luiSetInfo,callContext);
804         assertNotNull(newLuiSet2);
805 
806         List<String> luiSetIds = new ArrayList<String>();
807         luiSetIds.add(newLuiSet1.getId());
808         luiSetIds.add(newLuiSet2.getId());
809 
810         List<LuiSetInfo> luiSetInfos = luiService.getLuiSetsByIds(luiSetIds,callContext);
811         assertEquals(2,luiSetInfos.size());
812     }
813 
814     @Test
815     public void testAttributeQuery() {
816     	/*
817     	 * Used to test the JPA join syntax.
818     	 */
819     	Query q = luiDao.getEm().createQuery("select lui.id from LuiEntity lui inner join lui.attributes a where a.key=:key").setParameter("key", "attr1");
820     	
821     	List<?> results = q.getResultList();
822 
823     	assertEquals(1, results.size());
824     }
825 }