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.junit.Test;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.krad.data.DataObjectService;
22  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
23  import org.kuali.rice.krms.api.repository.term.TermDefinition;
24  import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
25  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
26  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
27  import org.kuali.rice.krms.api.repository.type.KrmsTypeBoService;
28  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
29  import org.kuali.rice.krms.impl.repository.ContextBoService;
30  import org.kuali.rice.krms.impl.repository.ContextBoServiceImpl;
31  import org.kuali.rice.krms.impl.repository.KrmsTypeBoServiceImpl;
32  import org.kuali.rice.krms.impl.repository.TermBoService;
33  import org.kuali.rice.krms.impl.repository.TermBoServiceImpl;
34  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
35  import org.kuali.rice.test.BaselineTestCase.Mode;
36  
37  import java.util.Arrays;
38  import java.util.Collections;
39  import java.util.List;
40  
41  import static org.junit.Assert.assertFalse;
42  import static org.junit.Assert.assertTrue;
43  
44  @BaselineMode(Mode.CLEAR_DB)
45  public class TermRelatedBoTest extends AbstractBoTest {
46  
47      private TermBoService termBoService;
48      private ContextBoService contextRepository;
49      private KrmsTypeBoService krmsTypeBoService;
50  
51      @Before
52      public void setup() {
53  
54          // wire up BO services
55  
56          termBoService = new TermBoServiceImpl();
57  
58          // TODO: fix
59          ((TermBoServiceImpl)termBoService).setDataObjectService(GlobalResourceLoader.<DataObjectService>getService(
60                  "dataObjectService"));
61  
62          contextRepository = new ContextBoServiceImpl();
63          ((ContextBoServiceImpl)contextRepository).setDataObjectService(getDataObjectService());
64  
65          krmsTypeBoService = new KrmsTypeBoServiceImpl();
66          ((KrmsTypeBoServiceImpl)krmsTypeBoService).setDataObjectService(getDataObjectService());
67      }
68  
69      @Test
70      public void creationTest() {
71  
72          // create prerequisite objects
73          ContextDefinition contextDefinition = createContext();
74          KrmsTypeDefinition krmsTermResolverTypeDefinition = createTermResolverType();
75  
76          // output TermSpec
77          TermSpecificationDefinition outputTermSpec =
78                  TermSpecificationDefinition.Builder.create(null, "outputTermSpec", contextDefinition.getId(),
79                          "java.lang.String").build();
80          outputTermSpec = termBoService.createTermSpecification(outputTermSpec);
81  
82          // prereq TermSpec
83          TermSpecificationDefinition prereqTermSpec =
84                  TermSpecificationDefinition.Builder.create(null, "prereqTermSpec", contextDefinition.getId(),
85                          "java.lang.String").build();
86          prereqTermSpec = termBoService.createTermSpecification(prereqTermSpec);
87  
88          // TermResolver
89          TermResolverDefinition termResolverDef =
90                  TermResolverDefinition.Builder.create(null, "KRMS", "testResolver", krmsTermResolverTypeDefinition.getId(),
91                          TermSpecificationDefinition.Builder.create(outputTermSpec),
92                          Collections.singleton(TermSpecificationDefinition.Builder.create(prereqTermSpec)),
93                          null,
94                          Collections.singleton("testParamName")).build();
95          termResolverDef = termBoService.createTermResolver(termResolverDef);
96  
97          // Term Param
98          TermParameterDefinition.Builder termParamBuilder =
99                  TermParameterDefinition.Builder.create(null, null, "testParamName", "testParamValue");
100 
101         // Term
102         TermDefinition termDefinition =
103                 TermDefinition.Builder.create(null, TermSpecificationDefinition.Builder.create(outputTermSpec), Collections.singletonList(termParamBuilder)).build();
104         termBoService.createTerm(termDefinition);
105     }
106 
107     /**
108      * Verify that Terms can be updated (including modification of an existing TermParameter) by the TermBoServiceImpl
109      */
110     @Test
111     public void updateTermChangeParameterTest() {
112         TermDefinition termDefinition = createTermForUpdate();
113 
114         // Change some things so that we can verify the updates occurred
115         TermDefinition.Builder updateTermBuilder = TermDefinition.Builder.create(termDefinition);
116         updateTermBuilder.setDescription("updated description");
117         assertTrue(updateTermBuilder.getParameters().size() == 1);
118         updateTermBuilder.getParameters().get(0).setValue("updatedParamValue");
119 
120         termBoService.updateTerm(updateTermBuilder.build());
121 
122         // check that the update stuck
123         TermDefinition updatedTerm = termBoService.getTerm(termDefinition.getId());
124 
125         assertFalse("descriptions should not be equal after update",
126                 updatedTerm.getDescription().equals(termDefinition.getDescription()));
127 
128         assertTrue(updatedTerm.getParameters().size() == 1);
129 
130         TermParameterDefinition updatedParam = updatedTerm.getParameters().get(0);
131         assertTrue(updatedParam.getValue().equals("updatedParamValue"));
132     }
133 
134     /**
135      * Verify that Terms can be updated (including replacement of a TermParameter) by the TermBoServiceImpl
136      */
137     @Test
138     public void updateTermReplaceParameterTest() {
139         TermDefinition termDefinition = createTermForUpdate();
140 
141         // Change some things so that we can verify the updates occurred
142         TermDefinition.Builder updateTermBuilder = TermDefinition.Builder.create(termDefinition);
143         updateTermBuilder.setDescription("updated description");
144         assertTrue(updateTermBuilder.getParameters().size() == 1);
145         updateTermBuilder.getParameters().clear();
146         updateTermBuilder.getParameters().add(TermParameterDefinition.Builder.create(null, termDefinition.getId(), "secondParamName", "secondParamValue"));
147 
148         termBoService.updateTerm(updateTermBuilder.build());
149 
150         // check that the update stuck
151         TermDefinition updatedTerm = termBoService.getTerm(termDefinition.getId());
152 
153         assertFalse("descriptions should not be equal after update",
154                 updatedTerm.getDescription().equals(termDefinition.getDescription()));
155 
156         assertTrue(updatedTerm.getParameters().size() == 1);
157 
158         TermParameterDefinition secondParam = updatedTerm.getParameters().get(0);
159         assertTrue(secondParam.getValue().equals("secondParamValue"));
160     }
161 
162     private ContextDefinition createContext() {
163 
164         // KrmsType for context
165         KrmsTypeDefinition krmsContextTypeDefinition = KrmsTypeDefinition.Builder.create("KrmsTestContextType", "KRMS").build();
166         krmsContextTypeDefinition = krmsTypeBoService.createKrmsType(krmsContextTypeDefinition);
167 
168         // Context
169         ContextDefinition.Builder contextBuilder = ContextDefinition.Builder.create("KRMS", "testContext");
170         contextBuilder.setTypeId(krmsContextTypeDefinition.getId());
171         ContextDefinition contextDefinition = contextBuilder.build();
172         contextDefinition = contextRepository.createContext(contextDefinition);
173 
174         return contextDefinition;
175     }
176 
177     private TermDefinition createTermForUpdate() {
178         ContextDefinition contextDefinition = createContext();
179 
180         // TermSpec -- we need one to create a term
181         TermSpecificationDefinition termSpec =
182                 TermSpecificationDefinition.Builder.create(null, "TermUpdateTestTermSpec", contextDefinition.getId(),
183                         "java.lang.String").build();
184         termSpec = termBoService.createTermSpecification(termSpec);
185 
186         // Term -- create the term that we'll update
187         List<TermParameterDefinition.Builder> paramBuilders =
188                 Arrays.asList(TermParameterDefinition.Builder.create(null, null, "paramName", "paramValue"));
189         TermDefinition termDefinition =
190                 TermDefinition.Builder.create(null, TermSpecificationDefinition.Builder.create(termSpec), paramBuilders).build();
191         termDefinition = termBoService.createTerm(termDefinition);
192         return termDefinition;
193     }
194 
195     private KrmsTypeDefinition createTermResolverType() {
196         // KrmsType for TermResolver
197         KrmsTypeDefinition krmsTermResolverTypeDefinition = KrmsTypeDefinition.Builder.create("KrmsTestResolverType", "KRMS").build();
198         krmsTermResolverTypeDefinition = krmsTypeBoService.createKrmsType(krmsTermResolverTypeDefinition);
199 
200         return krmsTermResolverTypeDefinition;
201     }
202 }