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.student.krms;
17  
18  import org.kuali.rice.core.api.lifecycle.Lifecycle;
19  import org.kuali.rice.core.framework.resourceloader.SpringResourceLoader;
20  import org.kuali.rice.krad.service.BusinessObjectService;
21  import org.kuali.rice.krad.service.KRADServiceLocator;
22  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
23  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
24  import org.kuali.rice.krms.api.repository.term.TermDefinition;
25  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
26  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
27  import org.kuali.rice.krms.impl.repository.*;
28  import org.kuali.rice.test.BaselineTestCase;
29  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
30  import org.kuali.rice.test.BaselineTestCase.Mode;
31  
32  import javax.xml.namespace.QName;
33  import java.util.ArrayList;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  @BaselineMode(Mode.NONE)
39  public class KSKRMSTestCase extends BaselineTestCase {
40  
41      private static final String KRMS_MODULE_NAME = "krms";
42  
43  	protected ContextBoService contextRepository;
44  	protected KrmsTypeRepositoryService krmsTypeRepository;
45  	protected AgendaBoService agendaBoService;
46  	protected RuleBoService ruleBoService;
47  
48  	// Services needed for creation:
49  	protected TermBoService termBoService;
50  	protected SpringResourceLoader krmsTestResourceLoader;
51  
52      private static String KRMS_TEST_SPRING_BEANS = "classpath:KRMSTestHarnessSpringBeans.xml";
53  
54  	public KSKRMSTestCase() {
55          super(KRMS_MODULE_NAME);
56  		this.setClearTables(false);
57  	}
58  
59      /**
60       * Overridden to set dirty=true each time
61       * @see org.kuali.rice.test.RiceTestCase#setUp()
62       */
63      @Override
64      public void setUp() throws Exception {
65          super.setUp();
66          termBoService = KrmsRepositoryServiceLocator.getTermBoService();
67          agendaBoService = KrmsRepositoryServiceLocator.getAgendaBoService();
68          contextRepository = KrmsRepositoryServiceLocator.getContextBoService();
69          ruleBoService = KrmsRepositoryServiceLocator.getRuleBoService();
70          krmsTypeRepository = KrmsRepositoryServiceLocator
71                  .getKrmsTypeRepositoryService();
72      }
73  
74  	@Override
75  	protected List<String> getPerTestTablesNotToClear() {
76  		List<String> tablesNotToClear = super.getPerTestTablesNotToClear();
77  		// tablesNotToClear.add("KRMS_.*");
78  		return tablesNotToClear;
79  	}
80  
81  	@Override
82  	protected Lifecycle getLoadApplicationLifecycle() {
83  		if (krmsTestResourceLoader == null) {
84              List<String> files = new ArrayList<String>();
85              files.add(KRMS_TEST_SPRING_BEANS);
86              if (this.getAdditionalSpringFile() != null){
87                  files.add(getAdditionalSpringFile());
88              }
89              
90  			krmsTestResourceLoader = new SpringResourceLoader(new QName(
91  					"KRMSTestHarnessApplicationResourceLoader"), files, null);
92  			krmsTestResourceLoader
93  					.setParentSpringResourceLoader(getTestHarnessSpringResourceLoader());
94  			getTestHarnessSpringResourceLoader().addResourceLoader(
95  					krmsTestResourceLoader);
96  		}
97  		return krmsTestResourceLoader;
98  	}
99      
100     protected String getAdditionalSpringFile(){
101         return null;
102     }
103 
104 	protected BusinessObjectService getBoService() {
105 		return KRADServiceLocator.getBusinessObjectService();
106 	}
107 	
108 	//@Test
109 	/*public void testHello() {
110 			System.out.println("Test hello");
111 			assertNotNull(termBoService);
112 			assertNotNull(agendaBoService);
113 			assertNotNull(contextRepository);
114 			assertNotNull(ruleBoService);
115 			assertNotNull(krmsTypeRepository);
116 	}*/
117 	
118 	protected AgendaDefinition getKRMSAgenda(String agendaName, ContextDefinition contextDef) {
119 		AgendaDefinition agendaDef = agendaBoService
120 				.getAgendaByNameAndContextId(agendaName,
121 						contextDef.getId());
122 		return agendaDef;
123 	}
124 
125 	//@Test
126 	/*public void testKRMSContext() {
127 		ContextDefinition cnt = getKRMSContext(KSKRMSConstants.CONTEXT_STUD_ELIGIBILITY);
128 		assertNotNull(cnt);
129 		System.out.println(cnt.getDescription());
130 		AgendaDefinition adef = getKRMSAgenda(KSKRMSConstants.AGENDA1, cnt);
131 		assertNotNull(adef);
132 		System.out.println(adef.getName());
133 	}*/
134 
135 	protected ContextDefinition getKRMSContext(String context) {
136 		return contextRepository.getContextByNameAndNamespace(
137 				context, KSKRMSConstants.KSNAMESPACE);
138 	}
139 	
140 	protected TermDefinition krmsTermLookup(String termName) {
141 		// this may be called more than once, we only want to create one though
142 		Map<String, String> queryArgs = new HashMap<String, String>();
143 		queryArgs.put("desc_txt", termName);
144 		TermBo termBo = getBoService()
145 				.findByPrimaryKey(TermBo.class, queryArgs);
146 		if (termBo != null) {
147 			return TermBo.to(termBo);
148 		}
149 		return null;
150 	}
151 	
152 	protected TermResolverDefinition krmsTermResolverLookup(String termResolverName) {
153 		// this may be called more than once, we only want to create one though
154 		Map<String, String> queryArgs = new HashMap<String, String>();
155 		queryArgs.put("nm", termResolverName);
156 		TermResolverBo termBo = getBoService()
157 				.findByPrimaryKey(TermResolverBo.class, queryArgs);
158 		if (termBo != null) {
159 			return TermResolverBo.to(termBo);
160 		}
161 		return null;
162 	}
163 	
164 }