001    /**
002     * Copyright 2005-2015 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.uif.util;
017    
018    import java.util.List;
019    import java.util.Map;
020    import java.util.Set;
021    
022    import javax.xml.namespace.QName;
023    
024    import org.kuali.rice.core.api.config.property.ConfigContext;
025    import org.kuali.rice.core.api.reflect.ObjectDefinition;
026    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
027    import org.kuali.rice.core.api.resourceloader.ResourceLoader;
028    import org.kuali.rice.core.api.util.type.KualiDecimal;
029    import org.kuali.rice.core.framework.config.property.SimpleConfig;
030    import org.kuali.rice.kim.api.identity.Person;
031    import org.kuali.rice.kim.api.identity.PersonService;
032    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
033    import org.kuali.rice.krad.UserSession;
034    import org.kuali.rice.krad.uif.container.CollectionGroup;
035    import org.kuali.rice.krad.uif.container.Group;
036    import org.kuali.rice.krad.uif.element.Action;
037    import org.kuali.rice.krad.uif.field.DataField;
038    import org.kuali.rice.krad.uif.field.Field;
039    import org.kuali.rice.krad.uif.view.View;
040    import org.kuali.rice.krad.uif.view.ViewAuthorizer;
041    import org.kuali.rice.krad.uif.view.ViewModel;
042    import org.kuali.rice.krad.uif.widget.Widget;
043    import org.kuali.rice.krad.util.GlobalVariables;
044    
045    /**
046     * Utilities class for establishing a minimal environment for testing operations involving Uif
047     * components.
048     * 
049     * @author Kuali Rice Team (rice.collab@kuali.org)
050     */
051    public class UifUnitTestUtils {
052    
053        /**
054         * Mock person implementation.
055         */
056        private static class MockPerson implements Person {
057    
058            private static final long serialVersionUID = 5330488987382249417L;
059    
060            private final String id;
061    
062            private MockPerson(String id) {
063                this.id = id;
064            }
065    
066            @Override
067            public void refresh() {}
068    
069            @Override
070            public String getPrincipalId() {
071                return id;
072            }
073    
074            @Override
075            public String getPrincipalName() {
076                return id;
077            }
078    
079            @Override
080            public String getEntityId() {
081                return id;
082            }
083    
084            @Override
085            public String getEntityTypeCode() {
086                return null;
087            }
088    
089            @Override
090            public String getFirstName() {
091                return "Test";
092            }
093    
094            @Override
095            public String getFirstNameUnmasked() {
096                return "Test";
097            }
098    
099            @Override
100            public String getMiddleName() {
101                return "User";
102            }
103    
104            @Override
105            public String getMiddleNameUnmasked() {
106                return "User";
107            }
108    
109            @Override
110            public String getLastName() {
111                return id;
112            }
113    
114            @Override
115            public String getLastNameUnmasked() {
116                return id;
117            }
118    
119            @Override
120            public String getName() {
121                return "Test User " + id;
122            }
123    
124            @Override
125            public String getNameUnmasked() {
126                return "Test User " + id;
127            }
128    
129            @Override
130            public String getEmailAddress() {
131                return null;
132            }
133    
134            @Override
135            public String getEmailAddressUnmasked() {
136                return null;
137            }
138    
139            @Override
140            public String getAddressLine1() {
141                return null;
142            }
143    
144            @Override
145            public String getAddressLine1Unmasked() {
146                return null;
147            }
148    
149            @Override
150            public String getAddressLine2() {
151                return null;
152            }
153    
154            @Override
155            public String getAddressLine2Unmasked() {
156                return null;
157            }
158    
159            @Override
160            public String getAddressLine3() {
161                return null;
162            }
163    
164            @Override
165            public String getAddressLine3Unmasked() {
166                return null;
167            }
168    
169            @Override
170            public String getAddressCity() {
171                return null;
172            }
173    
174            @Override
175            public String getAddressCityUnmasked() {
176                return null;
177            }
178    
179            @Override
180            public String getAddressStateProvinceCode() {
181                return null;
182            }
183    
184            @Override
185            public String getAddressStateProvinceCodeUnmasked() {
186                return null;
187            }
188    
189            @Override
190            public String getAddressPostalCode() {
191                return null;
192            }
193    
194            @Override
195            public String getAddressPostalCodeUnmasked() {
196                return null;
197            }
198    
199            @Override
200            public String getAddressCountryCode() {
201                return null;
202            }
203    
204            @Override
205            public String getAddressCountryCodeUnmasked() {
206                return null;
207            }
208    
209            @Override
210            public String getPhoneNumber() {
211                return null;
212            }
213    
214            @Override
215            public String getPhoneNumberUnmasked() {
216                return null;
217            }
218    
219            @Override
220            public String getCampusCode() {
221                return null;
222            }
223    
224            @Override
225            public Map<String, String> getExternalIdentifiers() {
226                return null;
227            }
228    
229            @Override
230            public boolean hasAffiliationOfType(String affiliationTypeCode) {
231                return false;
232            }
233    
234            @Override
235            public List<String> getCampusCodesForAffiliationOfType(String affiliationTypeCode) {
236                return null;
237            }
238    
239            @Override
240            public String getEmployeeStatusCode() {
241                return null;
242            }
243    
244            @Override
245            public String getEmployeeTypeCode() {
246                return null;
247            }
248    
249            @Override
250            public KualiDecimal getBaseSalaryAmount() {
251                return null;
252            }
253    
254            @Override
255            public String getExternalId(String externalIdentifierTypeCode) {
256                return null;
257            }
258    
259            @Override
260            public String getPrimaryDepartmentCode() {
261                return null;
262            }
263    
264            @Override
265            public String getEmployeeId() {
266                return null;
267            }
268    
269            @Override
270            public boolean isActive() {
271                return false;
272            }
273        }
274    
275        /**
276         * Mock person service implementation.
277         */
278        private static class MockPersonService implements PersonService {
279    
280            @Override
281            public Person getPerson(String principalId) {
282                return getMockPerson(principalId);
283            }
284    
285            @Override
286            public List<Person> getPersonByExternalIdentifier(String externalIdentifierTypeCode,
287                    String externalId) {
288                return null;
289            }
290    
291            @Override
292            public Person getPersonByPrincipalName(String principalName) {
293                return getMockPerson(principalName);
294            }
295    
296            @Override
297            public Person getPersonByEmployeeId(String employeeId) {
298                return null;
299            }
300    
301            @Override
302            public List<Person> findPeople(Map<String, String> criteria) {
303                return null;
304            }
305    
306            @Override
307            public List<Person> findPeople(Map<String, String> criteria, boolean unbounded) {
308                return null;
309            }
310    
311            @Override
312            public Class<? extends Person> getPersonImplementationClass() {
313                return null;
314            }
315    
316            @SuppressWarnings("deprecation")
317            @Override
318            public Map<String, String> resolvePrincipalNamesToPrincipalIds(
319                    org.kuali.rice.krad.bo.BusinessObject businessObject,
320                    Map<String, String> fieldValues) {
321                return null;
322            }
323    
324            @Override
325            public Person updatePersonIfNecessary(String sourcePrincipalId, Person currentPerson) {
326                return null;
327            }
328        }
329    
330        /**
331         * Get a mock person object for use in a JUnit test case.
332         * 
333         * @param id The ID to use for principal name, principal ID, and entity ID.
334         * @return A mock person with the supplied ID.
335         */
336        public static Person getMockPerson(String id) {
337            return new MockPerson(id);
338        }
339    
340        /**
341         * Establish a Rice configuration providing enough mock services via
342         * {@link GlobalResourceLoader} to support the use of KRAD UIF components in unit tests.
343         * 
344         * @param applicationId The application ID for the fake environment.
345         */
346        public static void establishMockConfig(String applicationId) {
347            SimpleConfig config = new SimpleConfig();
348            config.putProperty("application.id", applicationId);
349            ConfigContext.init(config);
350            GlobalResourceLoader.addResourceLoader(new ResourceLoader() {
351    
352                @Override
353                public <T> T getObject(ObjectDefinition definition) {
354                    return null;
355                }
356    
357                @SuppressWarnings("unchecked")
358                @Override
359                public <T> T getService(QName qname) {
360                    if (KimApiServiceLocator.KIM_PERSON_SERVICE.equals(qname.getLocalPart())) {
361                        return (T) new MockPersonService();
362                    }
363                    return null;
364                }
365    
366                @Override
367                public void start() throws Exception {}
368    
369                @Override
370                public void stop() throws Exception {}
371    
372                @Override
373                public boolean isStarted() {
374                    return true;
375                }
376    
377                @Override
378                public void addResourceLoader(ResourceLoader resourceLoader) {}
379    
380                @Override
381                public void addResourceLoaderFirst(ResourceLoader resourceLoader) {}
382    
383                @Override
384                public ResourceLoader getResourceLoader(QName name) {
385                    return null;
386                }
387    
388                @Override
389                public List<QName> getResourceLoaderNames() {
390                    return null;
391                }
392    
393                @Override
394                public List<ResourceLoader> getResourceLoaders() {
395                    return null;
396                }
397    
398                @Override
399                public void removeResourceLoader(QName name) {}
400    
401                @Override
402                public void setName(QName name) {}
403    
404                @Override
405                public QName getName() {
406                    return new QName("TEST");
407                }
408    
409                @Override
410                public String getContents(String indent, boolean servicePerLine) {
411                    return null;
412                }
413            });
414        }
415    
416        private static class MockViewAuthorizer implements ViewAuthorizer {
417    
418            @Override
419            public Set<String> getActionFlags(View view, ViewModel model, Person user, Set<String> actions) {
420                return new java.util.HashSet<String>();
421            }
422    
423            @Override
424            public Set<String> getEditModes(View view, ViewModel model, Person user, Set<String> editModes) {
425                return new java.util.HashSet<String>();
426            }
427    
428            @Override
429            public boolean canOpenView(View view, ViewModel model, Person user) {
430                return true;
431            }
432    
433            @Override
434            public boolean canEditView(View view, ViewModel model, Person user) {
435                return true;
436            }
437    
438            @Override
439            public boolean canUnmaskField(View view, ViewModel model, DataField field, String propertyName, Person user) {
440                return true;
441            }
442    
443            @Override
444            public boolean canPartialUnmaskField(View view, ViewModel model, DataField field, String propertyName,
445                    Person user) {
446                return true;
447            }
448    
449            @Override
450            public boolean canEditField(View view, ViewModel model, Field field, String propertyName, Person user) {
451                return true;
452            }
453    
454            @Override
455            public boolean canViewField(View view, ViewModel model, Field field, String propertyName, Person user) {
456                return true;
457            }
458    
459            @Override
460            public boolean canEditGroup(View view, ViewModel model, Group group, String groupId, Person user) {
461                return true;
462            }
463    
464            @Override
465            public boolean canViewGroup(View view, ViewModel model, Group group, String groupId, Person user) {
466                return true;
467            }
468    
469            @Override
470            public boolean canEditWidget(View view, ViewModel model, Widget widget, String widgetId, Person user) {
471                return true;
472            }
473    
474            @Override
475            public boolean canViewWidget(View view, ViewModel model, Widget widget, String widgetId, Person user) {
476                return true;
477            }
478    
479            @Override
480            public boolean canPerformAction(View view, ViewModel model, Action action, String actionEvent, String actionId,
481                    Person user) {
482                return true;
483            }
484    
485            @Override
486            public boolean canEditLine(View view, ViewModel model, CollectionGroup collectionGroup,
487                    String collectionPropertyName, Object line, Person user) {
488                return true;
489            }
490    
491            @Override
492            public boolean canViewLine(View view, ViewModel model, CollectionGroup collectionGroup,
493                    String collectionPropertyName, Object line, Person user) {
494                return true;
495            }
496    
497            @Override
498            public boolean canEditLineField(View view, ViewModel model, CollectionGroup collectionGroup,
499                    String collectionPropertyName, Object line, Field field, String propertyName, Person user) {
500                return true;
501            }
502    
503            @Override
504            public boolean canViewLineField(View view, ViewModel model, CollectionGroup collectionGroup,
505                    String collectionPropertyName, Object line, Field field, String propertyName, Person user) {
506                return true;
507            }
508    
509            @Override
510            public boolean canPerformLineAction(View view, ViewModel model, CollectionGroup collectionGroup,
511                    String collectionPropertyName, Object line, Action action, String actionEvent, String actionId,
512                    Person user) {
513                return true;
514            }
515        }
516        
517        /**
518         * Establish a user session with the given principal name.
519         * 
520         * <p>
521         * This method will use KIM API calls to look up a person with the provided principal name. Use
522         * {@link #establishMockConfig(String)} to set up a mock KIM environment if needed.
523         * </p>
524         * 
525         * @param principalName The principal name of the user to establish a session with.
526         */
527        public static void establishMockUserSession(String principalName) {
528            UserSession session = new UserSession(principalName);
529            GlobalVariables.setUserSession(session);
530        }
531        
532        /**
533         * Get a view authorizer allowing most operations.
534         * @return A view authorizer allowing most operations.
535         */
536        public static ViewAuthorizer getAllowMostViewAuthorizer() {
537            return new MockViewAuthorizer();
538        }
539    
540    }