View Javadoc
1   /**
2    * Copyright 2005-2014 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.krms.test;
17  
18  import org.junit.Before;
19  import org.kuali.rice.core.api.criteria.QueryByCriteria;
20  import org.kuali.rice.core.api.criteria.QueryResults;
21  import org.kuali.rice.kns.service.KNSServiceLocator;
22  import org.kuali.rice.krad.data.DataObjectService;
23  import org.kuali.rice.krad.service.BusinessObjectService;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
26  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
27  import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
28  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
29  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
30  import org.kuali.rice.krms.impl.repository.ContextAttributeBo;
31  import org.kuali.rice.krms.impl.repository.ContextBo;
32  import org.kuali.rice.krms.impl.repository.ContextBoService;
33  import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionBo;
34  import org.kuali.rice.krms.impl.repository.KrmsAttributeDefinitionService;
35  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
36  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
37  import org.kuali.rice.test.BaselineTestCase.Mode;
38  import org.springframework.util.CollectionUtils;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  import java.util.Collections;
43  import java.util.List;
44  import java.util.Map;
45  
46  @BaselineMode(Mode.ROLLBACK)
47  public abstract class AbstractBoTest extends KRMSTestCase {
48  	
49      protected ContextBoService contextRepository;
50      protected KrmsTypeRepositoryService krmsTypeRepository;
51      protected KrmsAttributeDefinitionService krmsAttributeDefinitionService;
52  
53      @Override
54      @Before
55      public void setUp() throws Exception {
56          super.setUp();
57          contextRepository = KrmsRepositoryServiceLocator.getContextBoService();
58          krmsTypeRepository = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
59          krmsAttributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
60      }
61  
62      protected BusinessObjectService getBoService() {
63  		return KNSServiceLocator.getBusinessObjectService();
64  	}
65  
66      protected DataObjectService getDataObjectService() {
67          return KRADServiceLocator.getDataObjectService();
68      }
69  
70      protected ContextDefinition createContextDefinition(String nameSpace, String name,
71              Map<String, String> contextAttributes) {
72          // Attributes for context;
73          List<KrmsTypeAttribute.Builder> contextAttributeBuilders = new ArrayList<KrmsTypeAttribute.Builder>();
74          int contextAttrSequenceIndex = 0;
75  
76          List<KrmsAttributeDefinition> attributeDefintions = new ArrayList<KrmsAttributeDefinition>();
77  
78          if (contextAttributes != null) for (Map.Entry<String,String> entry : contextAttributes.entrySet()) {
79              KrmsAttributeDefinition.Builder contextTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null, entry.getKey(), nameSpace);
80              contextTypeAttributeDefnBuilder.setLabel(entry.getKey() + " attribute label");
81              KrmsAttributeDefinition contextTypeAttributeDefinition = krmsAttributeDefinitionService.createAttributeDefinition(contextTypeAttributeDefnBuilder.build());
82              attributeDefintions.add(contextTypeAttributeDefinition);
83  
84              // Attr for context;
85              contextAttributeBuilders.add(KrmsTypeAttribute.Builder.create(null, contextTypeAttributeDefinition.getId(),
86                      contextAttrSequenceIndex));
87              contextAttrSequenceIndex += 1;
88          }
89  
90          // KrmsType for context
91          KrmsTypeDefinition krmsContextTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "KrmsTestContextType");
92  
93          if (krmsContextTypeDefinition == null) {
94              KrmsTypeDefinition.Builder krmsContextTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestContextType", nameSpace);
95              krmsContextTypeDefnBuilder.setAttributes(contextAttributeBuilders);
96              krmsContextTypeDefinition = krmsContextTypeDefnBuilder.build();
97              krmsContextTypeDefinition = krmsTypeRepository.createKrmsType(krmsContextTypeDefinition);
98          }
99  
100         // Context
101         ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create(nameSpace, name);
102         contextBuilder.setTypeId(krmsContextTypeDefinition.getId());
103         ContextDefinition contextDefinition = contextBuilder.build();
104         contextDefinition = contextRepository.createContext(contextDefinition);
105 
106         // Context Attribute
107         // TODO: do this fur eel
108         for (KrmsAttributeDefinition contextTypeAttributeDefinition : attributeDefintions) {
109             ContextAttributeBo contextAttribute = new ContextAttributeBo();
110             contextAttribute.setAttributeDefinition(KrmsAttributeDefinitionBo.from(contextTypeAttributeDefinition));
111             contextAttribute.setContext(ContextBo.from(contextDefinition));
112             contextAttribute.setValue(contextAttributes.get(contextTypeAttributeDefinition.getName()));
113 
114             contextAttribute = getDataObjectService().save(contextAttribute);
115 //            // TODO: inject properly...
116 //            KRADServiceLocator.getDataObjectService().save(contextAttribute);
117         }
118 
119         return contextDefinition;
120     }
121 
122     protected KrmsTypeDefinition createKrmsGenericTypeDefinition(String nameSpace, String serviceName,
123             String attributeDefinitionLabel, String attributeDefinitionName) {
124         KrmsTypeDefinition krmsGenericTypeDefinition = krmsTypeRepository.getTypeByName(nameSpace, "KrmsTestGenericType");
125 
126         if (null == krmsGenericTypeDefinition) {
127 
128             // Attribute Defn for generic type;
129             KrmsAttributeDefinition.Builder genericTypeAttributeDefnBuilder = KrmsAttributeDefinition.Builder.create(null,
130                     attributeDefinitionName, nameSpace);
131             genericTypeAttributeDefnBuilder.setLabel(attributeDefinitionLabel);
132             KrmsAttributeDefinition genericTypeAttributeDefinition1 = krmsAttributeDefinitionService.createAttributeDefinition(genericTypeAttributeDefnBuilder.build());
133 
134             // Attr for generic type;
135             KrmsTypeAttribute.Builder genericTypeAttrBuilder = KrmsTypeAttribute.Builder.create(null, genericTypeAttributeDefinition1.getId(), 1);
136 
137             // Can use this generic type for KRMS bits that don't actually rely on services on the bus at this point in time
138             KrmsTypeDefinition.Builder krmsGenericTypeDefnBuilder = KrmsTypeDefinition.Builder.create("KrmsTestGenericType", nameSpace);
139             krmsGenericTypeDefnBuilder.setServiceName(serviceName);
140             krmsGenericTypeDefnBuilder.setAttributes(Collections.singletonList(genericTypeAttrBuilder));
141             krmsGenericTypeDefinition = krmsTypeRepository.createKrmsType(krmsGenericTypeDefnBuilder.build());
142 
143         }
144 
145         return krmsGenericTypeDefinition;
146     }
147 
148 
149     public String getNamespaceByContextName(String name) {
150         QueryByCriteria contextCrit = QueryByCriteria.Builder.forAttribute("name", name).build();
151         QueryResults<ContextBo> results = getDataObjectService().findMatching(ContextBo.class, contextCrit);
152         if (CollectionUtils.isEmpty(results.getResults())) {
153             return null;
154         }
155         if (results.getResults().size() != 1) {
156             throw new IllegalStateException(
157                     "getNamespaceByContextName can't handle a universe where multiple contexts have the same name");
158         }
159         return results.getResults().iterator().next().getNamespace();
160     }
161 
162     public ContextBoService getContextRepository() {
163         return contextRepository;
164     }
165 
166     public KrmsTypeRepositoryService getKrmsTypeRepository() {
167         return krmsTypeRepository;
168     }
169 
170     public KrmsAttributeDefinitionService getKrmsAttributeDefinitionService() {
171         return krmsAttributeDefinitionService;
172     }
173 
174 }