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