View Javadoc
1   /**
2    * Copyright 2005-2013 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.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   * LookupResultsServiceTest tests {@link LookupResultsService}
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	 * Tests that lookup ids work
49  	 */
50  	// @Ignore
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  	 * Tests that PersistableBusinessObjectSearches work
68  	 *
69  	 */
70  	// @Ignore
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  		// now try to retrieve
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(); // just in case!!
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  	 * Tests that PersistableBusinessObjectSearches work
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(); // just in case!!
114 		}
115 		org.junit.Assert.assertEquals("LookupResultsDDBo lookup worked as expected", "gorilla", retrievedDDBo.getSomeValue());
116 	}
117 	
118 	/**
119 	 * Tests that BO which doesn't have qualified support strategy throws an exception on search
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 }