View Javadoc
1   /**
2    * Copyright 2005-2015 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.impl.repository;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.junit.runner.RunWith;
21  import org.kuali.rice.core.api.criteria.GenericQueryResults;
22  import org.kuali.rice.core.api.criteria.QueryByCriteria;
23  import org.kuali.rice.krad.data.DataObjectService;
24  import org.kuali.rice.krad.data.PersistenceOption;
25  import org.kuali.rice.krms.api.repository.KrmsTypeGenTest;
26  import org.kuali.rice.krms.api.repository.NaturalLanguageUsageGenTest;
27  import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
28  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
29  import org.mockito.Mock;
30  import org.mockito.MockitoAnnotations;
31  import org.mockito.runners.MockitoJUnitRunner;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertNotNull;
38  import static org.junit.Assert.assertNull;
39  import static org.mockito.Matchers.any;
40  import static org.mockito.Mockito.mock;
41  import static org.mockito.Mockito.when;
42  
43  /**
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   * 
46   */
47  @RunWith(MockitoJUnitRunner.class)
48  public class NaturalLanguageUsageBoServiceImplGenTest {
49      private NaturalLanguageUsageBoServiceImpl service;
50      private NaturalLanguageUsage naturalLanguageUsage;
51      private DataObjectService mockDataObjectService;
52  
53      public NaturalLanguageUsageBoServiceImplGenTest() {
54          mockDataObjectService = mock(DataObjectService.class);
55      }
56  
57      @Before
58      public void setUp() {
59          service = new NaturalLanguageUsageBoServiceImpl();
60          service.setDataObjectService(mockDataObjectService);
61      }
62  
63      @Test
64      public void test_createNaturalLanguageUsage() {
65          NaturalLanguageUsage def = NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage();
66          naturalLanguageUsage = service.getNaturalLanguageUsageByName(def.getNamespace(), def.getName());
67          if (naturalLanguageUsage == null) {
68              naturalLanguageUsage = service.createNaturalLanguageUsage(def);
69          }
70      }
71  
72      @Test
73      public void test_createNaturalLanguageUsageGeneratedId() {
74          NaturalLanguageUsage def = NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsageNoId();
75          naturalLanguageUsage = service.createNaturalLanguageUsage(def);
76      }
77  
78      @Test
79      public void test_createNaturalLanguageUsage_GeneratedId_success() {
80          NaturalLanguageUsageBo saveResult = service.from(NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage());
81          when(mockDataObjectService.findMatching(any(Class.class), any(QueryByCriteria.class))).thenReturn(null);
82          when(mockDataObjectService.save(any(NaturalLanguageUsageBo.class), any(PersistenceOption.class))).thenReturn(saveResult);
83          NaturalLanguageUsage def = NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsageNoId();
84          naturalLanguageUsage = service.createNaturalLanguageUsage(def);
85          assertNotNull(naturalLanguageUsage);
86      }
87  
88      @Test
89      public void test_getNaturalLanguageUsageByName_success() {
90          final GenericQueryResults.Builder findMatchingResult = GenericQueryResults.Builder.create();
91          final List<NaturalLanguageUsageBo> resultList = new ArrayList<NaturalLanguageUsageBo>();
92          NaturalLanguageUsageBo e = service.from(NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage());
93          resultList.add(e);
94          findMatchingResult.setResults(resultList);
95          when(mockDataObjectService.findMatching(any(Class.class), any(QueryByCriteria.class))).thenReturn(findMatchingResult);
96          NaturalLanguageUsage def = NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage();
97          naturalLanguageUsage = service.getNaturalLanguageUsageByName(def.getNamespace(), def.getName());
98          assertNotNull(naturalLanguageUsage);
99      }
100 
101     @Test
102     public void test_updateNaturalLanguageUsage_success() {
103         NaturalLanguageUsage data = NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage();
104         NaturalLanguageUsage findResult = NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage();
105         NaturalLanguageUsageBo saveResult = service.from(NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage());
106         when(mockDataObjectService.find(any(Class.class), any(String.class))).thenReturn(service.from(findResult));
107         when(mockDataObjectService.save(any(NaturalLanguageUsageBo.class), any(PersistenceOption.class))).thenReturn(saveResult);
108         NaturalLanguageUsage updatedData = service.updateNaturalLanguageUsage(data);
109         assertNotNull(updatedData);
110     }
111 
112     @Test
113     public void test_from_null_yields_null() {
114         assertNull(service.from(null));
115     }
116 
117     @Test
118     public void test_from() {
119         NaturalLanguageUsage def = NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage();
120         NaturalLanguageUsageBo def2 = service.from(def);
121         assertEquals(def2.getName(), def.getName());
122         assertEquals(def2.getNamespace(),def.getNamespace());
123         assertEquals(def2.getId(), def.getId());
124     }
125 
126     @Test
127     public void test_to() {
128         NaturalLanguageUsage def = NaturalLanguageUsageGenTest.buildFullNaturalLanguageUsage();
129         NaturalLanguageUsageBo naturalLanguageUsageBo = service.from(def);
130         NaturalLanguageUsage def2 = NaturalLanguageUsageBo.to(naturalLanguageUsageBo);
131         assertEquals(def, def2);
132     }
133 
134     @Test(expected = IllegalArgumentException.class)
135     public void test_findNaturalLanguageUsagesByName_null_fail() {
136         service.findNaturalLanguageUsagesByName(null);
137     }
138 
139     @Test(expected = IllegalArgumentException.class)
140     public void test_findNaturalLanguageUsagesByDescription_null_fail() {
141         service.findNaturalLanguageUsagesByDescription(null);
142     }
143 
144     @Test(expected = IllegalArgumentException.class)
145     public void test_findNaturalLanguageUsagesByNamespace_null_fail() {
146         service.findNaturalLanguageUsagesByNamespace(null);
147     }
148 
149     @Test(expected = IllegalArgumentException.class)
150     public void test_createNaturalLanguageUsage_null_fail() {
151         service.createNaturalLanguageUsage(null);
152     }
153 
154     @Test(expected = IllegalArgumentException.class)
155     public void test_updateNaturalLanguageUsage_null_fail() {
156         service.updateNaturalLanguageUsage(null);
157     }
158 
159     @Test(expected = IllegalArgumentException.class)
160     public void test_deleteNaturalLanguageUsage_null_fail() {
161         service.deleteNaturalLanguageUsage(null);
162     }
163 
164     public NaturalLanguageUsage getNaturalLanguageUsage() {
165         return naturalLanguageUsage;
166     }
167 
168     public void setNaturalLanguageUsageBoServiceImpl(NaturalLanguageUsageBoServiceImpl impl) {
169         this.service = impl;
170     }
171 
172     public static NaturalLanguageUsageBoServiceImplGenTest create(NaturalLanguageUsageBoServiceImpl impl) {
173         NaturalLanguageUsageBoServiceImplGenTest test = new NaturalLanguageUsageBoServiceImplGenTest();
174         test.setNaturalLanguageUsageBoServiceImpl(impl);
175         return test;
176     }
177 
178 //    void createNaturalLanguageUsage() { // TODO gen what to do when no FKs?
179 //        // TODO change the Object type of the parameters
180 //        NaturalLanguageUsage def = NaturalLanguageUsageGenTest.buildFullFKNaturalLanguageUsage();
181 //        naturalLanguageUsage = service.createNaturalLanguageUsage(def);
182 //    }
183 
184 }