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.krad.data.provider.impl;
17  
18  import com.google.common.collect.Lists;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.kuali.rice.core.api.criteria.GenericQueryResults;
23  import org.kuali.rice.core.api.criteria.QueryByCriteria;
24  import org.kuali.rice.core.api.criteria.QueryResults;
25  import org.kuali.rice.krad.data.CompoundKey;
26  import org.kuali.rice.krad.data.metadata.MetadataRepository;
27  import org.kuali.rice.krad.data.provider.PersistenceProvider;
28  import org.kuali.rice.krad.data.provider.ProviderRegistry;
29  import org.mockito.Mock;
30  import org.mockito.runners.MockitoJUnitRunner;
31  import org.springframework.dao.IncorrectResultSizeDataAccessException;
32  
33  import java.io.Serializable;
34  import java.util.HashMap;
35  import java.util.Map;
36  
37  import static org.junit.Assert.*;
38  import static org.mockito.Mockito.*;
39  
40  /**
41   * Tests basic ProviderBasedDataObjectService implementation
42   */
43  @RunWith(MockitoJUnitRunner.class)
44  public class ProviderBasedDataObjectServiceTest {
45      @Mock PersistenceProvider mockProvider;
46  
47      @Mock ProviderRegistry providerRegistry;
48      @Mock
49      MetadataRepository metadataRepository;
50  
51      ProviderBasedDataObjectService service = new ProviderBasedDataObjectService();
52  
53      @Before
54      public void setup() {
55          when(providerRegistry.getPersistenceProvider(any(Class.class))).thenReturn(mockProvider);
56  
57          service.setMetadataRepository(metadataRepository);
58          service.setProviderRegistry(providerRegistry);
59      }
60  
61      @Test
62      public void testGetMetadataRepository() {
63          assertSame(metadataRepository, service.getMetadataRepository());
64      }
65  
66      @Test
67      public void testFind() {
68          Object findResult = new Object();
69          when(mockProvider.find(any(Class.class), any())).thenReturn(findResult);
70  
71          assertSame(findResult, service.find(Object.class, "id"));
72  
73          verify(mockProvider).find(any(Class.class), eq("id"));
74      }
75  
76      @Test
77      public void testFindUnique_NoResults() {
78          QueryByCriteria criteria = QueryByCriteria.Builder.create().build();
79          QueryResults<Object> emptyResults = GenericQueryResults.Builder.<Object>create().build();
80          when(mockProvider.findMatching(Object.class, criteria)).thenReturn(emptyResults);
81  
82          Object singleResult = service.findUnique(Object.class, criteria);
83          assertNull(singleResult);
84      }
85  
86      @Test
87      public void testFindUnique_OneResult() {
88          QueryByCriteria criteria = QueryByCriteria.Builder.create().build();
89  
90          // create results that contain a single object
91          Object theResult = new Object();
92          GenericQueryResults.Builder<Object> resultsBuilder =
93                  GenericQueryResults.Builder.<Object>create();
94          resultsBuilder.setResults(Lists.newArrayList(theResult));
95          QueryResults<Object> results = resultsBuilder.build();
96          when(mockProvider.findMatching(Object.class, criteria)).thenReturn(results);
97  
98          // now we should just get the one result back
99          Object singleResult = service.findUnique(Object.class, criteria);
100         assertNotNull(singleResult);
101         assertEquals(theResult, singleResult);
102     }
103 
104     @Test(expected = IncorrectResultSizeDataAccessException.class)
105     public void testFindUnique_TooManyResults() {
106         QueryByCriteria criteria = QueryByCriteria.Builder.create().build();
107 
108         // create results that contains multiple objects
109         Object result1 = new Object();
110         Object result2 = new Object();
111         GenericQueryResults.Builder<Object> resultsBuilder =
112                 GenericQueryResults.Builder.<Object>create();
113         resultsBuilder.setResults(Lists.newArrayList(result1, result2));
114         QueryResults<Object> results = resultsBuilder.build();
115         when(mockProvider.findMatching(Object.class, criteria)).thenReturn(results);
116 
117         // now when we invoke this, we should get the data access exception
118         // (see the "expected" exception on the @Test annotation)
119         service.findUnique(Object.class, criteria);
120     }
121 
122     @Test
123     public void testReduceCompoundKey_Null() {
124         // test null, should just return null
125         assertNull(service.reduceCompoundKey(null));
126     }
127 
128     @Test
129     public void testReduceCompoundKey_NonCompoundKey() {
130         assertEquals("1234", service.reduceCompoundKey("1234"));
131     }
132 
133     @Test
134     public void testReduceCompoundKey_CompoundKeyOneValue() {
135         Map<String, Object> singleKeyMap = new HashMap<String, Object>();
136         singleKeyMap.put("myAwesomeId", 123456L);
137         CompoundKey singleKey = new CompoundKey(singleKeyMap);
138         assertEquals(123456L, service.reduceCompoundKey(singleKey));
139     }
140 
141     @Test
142     public void testReduceCompoundKey_CompoutKeyMultiValue() {
143         Map<String, Object> multiKeyMap = new HashMap<String, Object>();
144         multiKeyMap.put("myAwesomeId", 123456L);
145         multiKeyMap.put("myOtherAwesomeId", "abcdefg");
146         CompoundKey multiKey = new CompoundKey(multiKeyMap);
147         assertEquals(multiKey, service.reduceCompoundKey(multiKey));
148     }
149 
150     @Test
151     public void testFindClass() {
152         Object findResult = new Object();
153         when(mockProvider.find(any(Class.class), any())).thenReturn(findResult);
154 
155         assertSame(findResult, service.find(Object.class, "id"));
156 
157         verify(mockProvider).find(any(Class.class), eq("id"));
158     }
159 
160     @Test
161     public void testFindMatching() {
162         QueryResults findMatchingResult = mock(QueryResults.class);
163         QueryByCriteria query = QueryByCriteria.Builder.create().build();
164         when(mockProvider.findMatching(any(Class.class), any(QueryByCriteria.class))).thenReturn(findMatchingResult);
165 
166         assertSame(findMatchingResult, service.findMatching(Object.class, query));
167 
168         verify(mockProvider).findMatching(any(Class.class), eq(query));
169     }
170 
171     @Test
172     public void testFindMatchingClass() {
173         QueryResults findMatchingResult = mock(QueryResults.class);
174         QueryByCriteria query = QueryByCriteria.Builder.create().build();
175         when(mockProvider.findMatching(any(Class.class), any(QueryByCriteria.class))).thenReturn(findMatchingResult);
176 
177         assertSame(findMatchingResult, service.findMatching(Object.class, query));
178 
179         verify(mockProvider).findMatching(any(Class.class), eq(query));
180     }
181 
182     @Test
183     public void testDelete() {
184         Object dataObject = new Object();
185         service.delete(dataObject);
186 
187         verify(mockProvider).delete(eq(dataObject));
188     }
189 
190     @Test
191     public void testSave() {
192         Serializable dataObject = new Serializable() {};
193         service.save(dataObject);
194 
195         verify(mockProvider).save(eq(dataObject));
196     }
197 
198     @Test
199     public void testSupportsSupportedType() {
200         // should support a type our mock is configured to support
201         assertTrue(service.supports(String.class));
202     }
203 
204     @Test
205     public void testDoesNotSupportUnsupportedType() {
206         // disavow any persistenceprovider mapping for this type
207         when(providerRegistry.getPersistenceProvider(String.class)).thenReturn(null);
208         assertFalse(service.supports(String.class));
209     }
210 }