View Javadoc
1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.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  package org.kuali.rice.krad.lookup;
17  
18  import org.junit.Ignore;
19  import org.junit.Test;
20  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
21  import org.kuali.rice.kim.api.identity.Person;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.kim.impl.identity.PersonImpl;
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.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   * Tests the LookupResultsService
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class LookupResultsServiceTest extends KRADTestCase {
43  	public final static String MOCK_PARAMETER_NMSPC = "KR-NS";
44  	public final static String MOCK_PARAMETER_DETAIL_TYPE_CODE = "All";
45  	public final static String MOCK_PARAMETER_NAME = "DATE_TO_STRING_FORMAT_FOR_FILE_NAME";
46  	public final static String MOCK_PERSON = "quickstart";
47  
48  	/**
49  	 * Tests that lookup ids work
50  	 *
51  	 */
52  	@Ignore
53  	@Test public void testLookupIds() {
54  		Map<String, String> parameterPK = new HashMap<String, String>();
55  		parameterPK.put("namespaceCode", MOCK_PARAMETER_NMSPC);
56  		parameterPK.put("componentCode", MOCK_PARAMETER_DETAIL_TYPE_CODE);
57  		parameterPK.put("name", MOCK_PARAMETER_NAME);
58  		final ParameterBo parameter = (ParameterBo) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(ParameterBo.class, parameterPK);
59  		final Person person = KimApiServiceLocator.getPersonService().getPerson(LookupResultsServiceTest.MOCK_PERSON);
60  		final LookupResultsDDBo ddBo = new LookupResultsDDBo("horse");
61  		final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
62  		
63  		org.junit.Assert.assertEquals("Parameter's lookup id should be its object id", parameter.getObjectId(), lookupResultsService.getLookupId(parameter));
64  		org.junit.Assert.assertNull("Person's lookup id should be null", lookupResultsService.getLookupId(person));
65  		org.junit.Assert.assertEquals("LookupResultsDDBo's lookup id should be a squashed PK String", "someValue-horse", lookupResultsService.getLookupId(ddBo));
66  	}
67  	
68  	/**
69  	 * Tests that PersistableBusinessObjectSearches work
70  	 *
71  	 */
72  	@Ignore
73  	@Test public void testPersistableBusinessObjectSearch() throws Exception {
74  		Map<String, String> parameterPK = new HashMap<String, String>();
75  		parameterPK.put("namespaceCode", MOCK_PARAMETER_NMSPC);
76  		parameterPK.put("componentCode", MOCK_PARAMETER_DETAIL_TYPE_CODE);
77  		parameterPK.put("name", MOCK_PARAMETER_NAME);
78  		final ParameterBo parameter = (ParameterBo) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(ParameterBo.class, parameterPK);
79  		final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
80  		
81  		Set<String> parameterIds = new HashSet<String>();
82  		parameterIds.add(lookupResultsService.getLookupId(parameter));
83  		lookupResultsService.persistSelectedObjectIds("testPBOSearch", parameterIds, LookupResultsServiceTest.MOCK_PERSON);
84  			
85  		// now try to retrieve
86  		Collection<ParameterBo> retrievedParameters = lookupResultsService.retrieveSelectedResultBOs("testPBOSearch", ParameterBo.class, LookupResultsServiceTest.MOCK_PERSON);
87  		org.junit.Assert.assertNotNull("We have a collection of retrieved Parameters", retrievedParameters);
88  		org.junit.Assert.assertEquals("Retrieved parameters collection size is 1", new Integer(1), new Integer(retrievedParameters.size()));
89  		final Iterator<ParameterBo> parameterIterator = retrievedParameters.iterator();
90  		final ParameterBo retrievedParameter = parameterIterator.next();
91  		while (parameterIterator.hasNext()) {
92  			parameterIterator.next(); // just in case!!
93  		}
94  		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));
95  	}
96  	
97  	/**
98  	 * Tests that PersistableBusinessObjectSearches work
99  	 *
100 	 */
101 	@Test public void testDataDictionaryBusinessObjectSearch() throws Exception {
102 		final LookupResultsDDBo ddBo = new LookupResultsDDBo("gorilla");
103 		final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
104 		
105 		Set<String> ddBoIds = new HashSet<String>();
106 		ddBoIds.add(lookupResultsService.getLookupId(ddBo));
107 		lookupResultsService.persistSelectedObjectIds("testDDBOSearch", ddBoIds, LookupResultsServiceTest.MOCK_PERSON);
108 			
109 		Collection<LookupResultsDDBo> retrievedDDBos = lookupResultsService.retrieveSelectedResultBOs("testDDBOSearch", LookupResultsDDBo.class, LookupResultsServiceTest.MOCK_PERSON);
110 		org.junit.Assert.assertNotNull("We have a collection of retrieved Parameters", retrievedDDBos);
111 		org.junit.Assert.assertEquals("Retrieved parameters collection size is 1", new Integer(1), new Integer(retrievedDDBos.size()));
112 		final Iterator<LookupResultsDDBo> ddBosIterator = retrievedDDBos.iterator();
113 		final LookupResultsDDBo retrievedDDBo = ddBosIterator.next();
114 		while (ddBosIterator.hasNext()) {
115 			ddBosIterator.next(); // just in case!!
116 		}
117 		org.junit.Assert.assertEquals("LookupResultsDDBo lookup worked as expected", "gorilla", retrievedDDBo.getSomeValue());
118 	}
119 	
120 	/**
121 	 * Tests that BO which doesn't have qualified support strategy throws an exception on search
122 	 *
123 	 */
124 	@Test public void testBadSearch() {
125 		boolean threwException = false;
126 		final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
127 		try {
128 			lookupResultsService.retrieveSelectedResultBOs("test data2", PersonImpl.class, LookupResultsServiceTest.MOCK_PERSON);
129 		} catch (RuntimeException re) {
130 			threwException = true;
131 		} catch (Exception e) {
132 			e.printStackTrace();
133 		}
134 		org.junit.Assert.assertTrue("Exception should have been thrown", threwException);
135 	}
136 }