View Javadoc

1   /*
2    * Copyright 2007-2010 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 java.util.Collection;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.junit.Test;
26  import org.kuali.rice.kim.bo.Person;
27  import org.kuali.rice.kim.bo.impl.PersonImpl;
28  import org.kuali.rice.kim.service.KIMServiceLocator;
29  import org.kuali.rice.kns.bo.Parameter;
30  import org.kuali.rice.kns.service.KNSServiceLocator;
31  import org.kuali.test.KNSTestCase;
32  
33  /**
34   * Tests the LookupResultsService
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class LookupResultsServiceTest extends KNSTestCase {
40  	public final static String MOCK_PARAMETER_NMSPC = "KR-NS";
41  	public final static String MOCK_PARAMETER_DETAIL_TYPE_CODE = "All";
42  	public final static String MOCK_PARAMETER_NAME = "DATE_TO_STRING_FORMAT_FOR_FILE_NAME";
43  	public final static String MOCK_PERSON = "quickstart";
44  
45  	/**
46  	 * Tests that lookup ids work
47  	 *
48  	 */
49  	@Test public void testLookupIds() {
50  		Map<String, String> parameterPK = new HashMap<String, String>();
51  		parameterPK.put("parameterNamespaceCode", MOCK_PARAMETER_NMSPC);
52  		parameterPK.put("parameterDetailTypeCode", MOCK_PARAMETER_DETAIL_TYPE_CODE);
53  		parameterPK.put("parameterName", MOCK_PARAMETER_NAME);
54  		final Parameter parameter = (Parameter)KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(Parameter.class, parameterPK);
55  		final Person person = KIMServiceLocator.getPersonService().getPerson(LookupResultsServiceTest.MOCK_PERSON);
56  		final LookupResultsDDBo ddBo = new LookupResultsDDBo("horse");
57  		final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
58  		
59  		org.junit.Assert.assertEquals("Parameter's lookup id should be its object id", parameter.getObjectId(), lookupResultsService.getLookupId(parameter));
60  		org.junit.Assert.assertNull("Person's lookup id should be null", lookupResultsService.getLookupId(person));
61  		org.junit.Assert.assertEquals("LookupResultsDDBo's lookup id should be a squashed PK String", "someValue-horse", lookupResultsService.getLookupId(ddBo));
62  	}
63  	
64  	/**
65  	 * Tests that PersistableBusinessObjectSearches work
66  	 *
67  	 */
68  	@Test public void testPersistableBusinessObjectSearch() {
69  		Map<String, String> parameterPK = new HashMap<String, String>();
70  		parameterPK.put("parameterNamespaceCode", MOCK_PARAMETER_NMSPC);
71  		parameterPK.put("parameterDetailTypeCode", MOCK_PARAMETER_DETAIL_TYPE_CODE);
72  		parameterPK.put("parameterName", MOCK_PARAMETER_NAME);
73  		final Parameter parameter = (Parameter)KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(Parameter.class, parameterPK);
74  		final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
75  		
76  		try {
77  			Set<String> parameterIds = new HashSet<String>();
78  			parameterIds.add(lookupResultsService.getLookupId(parameter));
79  			lookupResultsService.persistSelectedObjectIds("testPBOSearch", parameterIds, LookupResultsServiceTest.MOCK_PERSON);
80  			
81  			// now try to retrieve
82  			Collection<Parameter> retrievedParameters = lookupResultsService.retrieveSelectedResultBOs("testPBOSearch", Parameter.class, LookupResultsServiceTest.MOCK_PERSON);
83  			org.junit.Assert.assertNotNull("We have a collection of retrieved Parameters", retrievedParameters);
84  			org.junit.Assert.assertEquals("Retrieved parameters collection size is 1", new Integer(1), new Integer(retrievedParameters.size()));
85  			final Iterator<Parameter> parameterIterator = retrievedParameters.iterator();
86  			final Parameter retrievedParameter = parameterIterator.next();
87  			while (parameterIterator.hasNext()) {
88  				parameterIterator.next(); // just in case!!
89  			}
90  			org.junit.Assert.assertTrue("Parameter was one which was saved", retrievedParameter.getParameterNamespaceCode().equals(MOCK_PARAMETER_NMSPC) && retrievedParameter.getParameterDetailTypeCode().equals(MOCK_PARAMETER_DETAIL_TYPE_CODE) && retrievedParameter.getParameterName().equals(MOCK_PARAMETER_NAME));
91  		} catch (Exception e) {
92  			org.junit.Assert.fail("Exception was thrown: "+e.toString());
93  		}
94  	}
95  	
96  	/**
97  	 * Tests that PersistableBusinessObjectSearches work
98  	 *
99  	 */
100 	@Test public void testDataDictionaryBusinessObjectSearch() {
101 		final LookupResultsDDBo ddBo = new LookupResultsDDBo("gorilla");
102 		final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
103 		
104 		try {
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 		} catch (Exception e) {
119 			org.junit.Assert.fail("Exception was thrown: "+e.toString());
120 		}
121 	}
122 	
123 	/**
124 	 * Tests that BO which doesn't have qualified support strategy throws an exception on search
125 	 *
126 	 */
127 	@Test public void testBadSearch() {
128 		boolean threwException = false;
129 		final LookupResultsService lookupResultsService = KNSServiceLocator.getLookupResultsService();
130 		try {
131 			lookupResultsService.retrieveSelectedResultBOs("test data2", PersonImpl.class, LookupResultsServiceTest.MOCK_PERSON);
132 		} catch (RuntimeException re) {
133 			threwException = true;
134 		} catch (Exception e) {
135 			e.printStackTrace();
136 		}
137 		org.junit.Assert.assertTrue("Exception should have been thrown", threwException);
138 	}
139 }