1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.lookup;
17
18 import org.junit.Test;
19 import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
20 import org.kuali.rice.kim.api.identity.Person;
21 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22 import org.kuali.rice.kim.impl.identity.PersonImpl;
23 import org.kuali.rice.kns.KNSTestCase;
24 import org.kuali.rice.kns.lookup.LookupResultsService;
25 import org.kuali.rice.kns.service.KNSServiceLocator;
26 import org.kuali.rice.krad.service.KRADServiceLocator;
27 import org.kuali.rice.krad.test.KRADTestCase;
28
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.Map;
34 import java.util.Set;
35
36
37
38
39
40
41 public class LookupResultsServiceTest extends KNSTestCase {
42 public final static String MOCK_PARAMETER_NMSPC = "KR-NS";
43 public final static String MOCK_PARAMETER_DETAIL_TYPE_CODE = "All";
44 public final static String MOCK_PARAMETER_NAME = "DATE_TO_STRING_FORMAT_FOR_FILE_NAME";
45 public final static String MOCK_PERSON = "quickstart";
46
47
48
49
50
51 @Test public void testLookupIds() {
52 Map<String, String> parameterPK = new HashMap<String, String>();
53 parameterPK.put("namespaceCode", MOCK_PARAMETER_NMSPC);
54 parameterPK.put("componentCode", MOCK_PARAMETER_DETAIL_TYPE_CODE);
55 parameterPK.put("name", MOCK_PARAMETER_NAME);
56 final ParameterBo parameter = (ParameterBo) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(ParameterBo.class, parameterPK);
57 final Person person = KimApiServiceLocator.getPersonService().getPerson(LookupResultsServiceTest.MOCK_PERSON);
58 final LookupResultsDDBo ddBo = new LookupResultsDDBo("horse");
59 final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
60
61 org.junit.Assert.assertEquals("Parameter's lookup id should be its object id", parameter.getObjectId(), lookupResultsService.getLookupId(parameter));
62 org.junit.Assert.assertNull("Person's lookup id should be null", lookupResultsService.getLookupId(person));
63 org.junit.Assert.assertEquals("LookupResultsDDBo's lookup id should be a squashed PK String", "someValue-horse", lookupResultsService.getLookupId(ddBo));
64 }
65
66
67
68
69
70
71 @Test public void testPersistableBusinessObjectSearch() throws Exception {
72 Map<String, String> parameterPK = new HashMap<String, String>();
73 parameterPK.put("namespaceCode", MOCK_PARAMETER_NMSPC);
74 parameterPK.put("componentCode", MOCK_PARAMETER_DETAIL_TYPE_CODE);
75 parameterPK.put("name", MOCK_PARAMETER_NAME);
76 final ParameterBo parameter = (ParameterBo) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(ParameterBo.class, parameterPK);
77 final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
78
79 Set<String> parameterIds = new HashSet<String>();
80 parameterIds.add(lookupResultsService.getLookupId(parameter));
81 lookupResultsService.persistSelectedObjectIds("testPBOSearch", parameterIds, LookupResultsServiceTest.MOCK_PERSON);
82
83
84 Collection<ParameterBo> retrievedParameters = lookupResultsService.retrieveSelectedResultBOs("testPBOSearch", ParameterBo.class, LookupResultsServiceTest.MOCK_PERSON);
85 org.junit.Assert.assertNotNull("We have a collection of retrieved Parameters", retrievedParameters);
86 org.junit.Assert.assertEquals("Retrieved parameters collection size is 1", new Integer(1), new Integer(retrievedParameters.size()));
87 final Iterator<ParameterBo> parameterIterator = retrievedParameters.iterator();
88 final ParameterBo retrievedParameter = parameterIterator.next();
89 while (parameterIterator.hasNext()) {
90 parameterIterator.next();
91 }
92 org.junit.Assert.assertTrue("Parameter was one which was saved", retrievedParameter.getNamespaceCode().equals(MOCK_PARAMETER_NMSPC) && retrievedParameter.getComponentCode().equals(MOCK_PARAMETER_DETAIL_TYPE_CODE) && retrievedParameter.getName().equals(MOCK_PARAMETER_NAME));
93 }
94
95
96
97
98
99 @Test public void testDataDictionaryBusinessObjectSearch() throws Exception {
100 final LookupResultsDDBo ddBo = new LookupResultsDDBo("gorilla");
101 final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
102
103 Set<String> ddBoIds = new HashSet<String>();
104 ddBoIds.add(lookupResultsService.getLookupId(ddBo));
105 lookupResultsService.persistSelectedObjectIds("testDDBOSearch", ddBoIds, LookupResultsServiceTest.MOCK_PERSON);
106
107 Collection<LookupResultsDDBo> retrievedDDBos = lookupResultsService.retrieveSelectedResultBOs("testDDBOSearch", LookupResultsDDBo.class, LookupResultsServiceTest.MOCK_PERSON);
108 org.junit.Assert.assertNotNull("We have a collection of retrieved Parameters", retrievedDDBos);
109 org.junit.Assert.assertEquals("Retrieved parameters collection size is 1", new Integer(1), new Integer(retrievedDDBos.size()));
110 final Iterator<LookupResultsDDBo> ddBosIterator = retrievedDDBos.iterator();
111 final LookupResultsDDBo retrievedDDBo = ddBosIterator.next();
112 while (ddBosIterator.hasNext()) {
113 ddBosIterator.next();
114 }
115 org.junit.Assert.assertEquals("LookupResultsDDBo lookup worked as expected", "gorilla", retrievedDDBo.getSomeValue());
116 }
117
118
119
120
121
122 @Test public void testBadSearch() {
123 boolean threwException = false;
124 final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
125 try {
126 lookupResultsService.retrieveSelectedResultBOs("test data2", PersonImpl.class, LookupResultsServiceTest.MOCK_PERSON);
127 } catch (RuntimeException re) {
128 threwException = true;
129 } catch (Exception e) {
130 e.printStackTrace();
131 }
132 org.junit.Assert.assertTrue("Exception should have been thrown", threwException);
133 }
134 }