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.exception.RiceIllegalArgumentException;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
23  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
24  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
25  import org.kuali.rice.krms.impl.repository.TermBoService;
26  import org.kuali.rice.test.BaselineTestCase;
27  
28  import java.util.Arrays;
29  import java.util.Collections;
30  
31  import static org.junit.Assert.*;
32  
33  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
34  public class TermBoServiceTest extends AbstractBoTest {
35  	
36  	TermBoService termBoService = null;
37  
38      @Override
39  	@Before
40  	public void setUp() throws Exception {
41          super.setUp();
42  		termBoService = KrmsRepositoryServiceLocator.getTermBoService();
43  	}
44  
45      /**
46       * Tests whether {@code getTermSpecificationById} correctly returns
47       * {@link org.kuali.rice.core.api.exception.RiceIllegalArgumentException} when given null or empty IDs.
48       */
49      @Test
50      public void testGetTermSpecificationById_nullOrBlank() {
51          for (String id : Arrays.asList(null, "", " ")) {
52              try {
53                  termBoService.getTermSpecificationById(id);
54                  fail("getTermSpecificationById should have thrown " + RiceIllegalArgumentException.class.getSimpleName()
55                          + " for invalid id=" + id + ".");
56              } catch (RiceIllegalArgumentException e) {
57                  // correct behavior
58              }
59          }
60      }
61  
62      /**
63       * Tests whether {@code getTermSpecificationById} correctly returns null with no error when given the ID of an
64       * object that does not exist in the database.
65       */
66      @Test
67      public void testGetTermSpecificationById_invalid() {
68          TermSpecificationDefinition termSpecificationDefinition = termBoService.getTermSpecificationById("1");
69          assertNull("getTermSpecificationById should have returned null with no error", termSpecificationDefinition);
70      }
71  
72      /**
73       * Tests whether {@code getTermSpecificationById} correctly returns a non-null object when given the ID of an
74       * object that exists in the database.
75       */
76      @Test
77      public void testGetTermSpecificationById_valid() {
78          TermSpecificationDefinition.Builder termSpecBuilder =
79                  TermSpecificationDefinition.Builder.create(null, "1", "testTermSpec", "java.lang.String");
80          TermSpecificationDefinition termSpecificationDefinition
81                  = termBoService.createTermSpecification(termSpecBuilder.build());
82  
83          TermSpecificationDefinition fetchedTermSpec
84                  = termBoService.getTermSpecificationById(termSpecificationDefinition.getId());
85  
86          assertNotNull("getTermSpecificationById should not have returned null", fetchedTermSpec);
87      }
88  
89  	@Test
90  	public void testPersistTermSpecificationContextIds() {
91          ContextDefinition context1 = createContextDefinition("KR-SAP", "TermBoServiceTest-Context1", Collections.<String,String>emptyMap());
92          ContextDefinition context2 = createContextDefinition("KR-SAP", "TermBoServiceTest-Context2", Collections.<String,String>emptyMap());
93  
94          termBoService = GlobalResourceLoader.getService("termBoService");
95  
96  		TermSpecificationDefinition.Builder termSpecBuilder =
97  			TermSpecificationDefinition.Builder.create(null, "1", "testTermSpec", "java.lang.String");
98  
99          termSpecBuilder.getContextIds().add(context1.getId());
100         termSpecBuilder.getContextIds().add(context2.getId());
101 
102         TermSpecificationDefinition termSpecificationDefinition = termBoService.createTermSpecification(termSpecBuilder.build());
103 
104         assertNotNull(termSpecificationDefinition);
105         assertTrue(termSpecificationDefinition.getContextIds().size() == 2);
106         for (String contextId : Arrays.asList(context1.getId(), context2.getId())) {
107             assertTrue(termSpecificationDefinition.getContextIds().contains(contextId));
108         }
109 
110         TermSpecificationDefinition fetchedTermSpec = termBoService.getTermSpecificationById(termSpecificationDefinition.getId());
111 
112         for (String contextId : Arrays.asList(context1.getId(), context2.getId())) {
113             assertTrue("https://jira.kuali.org/browse/KULRICE-9850 ", fetchedTermSpec.getContextIds().contains(contextId));
114         }
115 	}
116 	
117 }