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  
17  package org.kuali.rice.kew.useroptions;
18  
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.assertFalse;
21  
22  import org.junit.Test;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.kew.service.KEWServiceLocator;
25  import org.kuali.rice.kew.test.KEWTestCase;
26  import org.kuali.rice.kim.api.identity.principal.Principal;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Test class for {link@ UserOptionsServiceImpl}
36   * Note not all methods for UserOptionsServiceImpl are tested in this test class, some methods are tested within the
37   *  {@link org.kuali.rice.kew.preferences.PreferencesServiceTest} test class
38   */
39  public class UserOptionsServiceTest extends KEWTestCase {
40  
41      /* Static string for the refresh rate user option */
42      private final static String REFRESH_RATE = "REFRESH_RATE";
43  
44      /* Static String for setting the refresh rate value */
45      private final static String ELEVEN = "11";
46  
47      /**
48       * Test save method when a principal id, option id, and option value are given
49       * @throws Exception if errors while validating save method.
50       */
51      @Test
52      public void testUserOptionsSaveWithPrincipalIdOptionIdOptionVal() throws Exception {
53          final UserOptionsService userOptionsService = this.getUserOptionsService();
54          Principal principal = this.getPrincipalByPrincipalName("ewestfal");
55          Collection<UserOptions> userOptions = userOptionsService.findByWorkflowUser(principal.getPrincipalId());
56          assertTrue("UserOptions should by empty", userOptions.isEmpty());
57  
58          // save a user option to the datasource
59          userOptionsService.save(principal.getPrincipalId(), REFRESH_RATE, "11");
60  
61          // now ensure we get the same user option back from the datasource
62          userOptions = userOptionsService.findByWorkflowUser(principal.getPrincipalId());
63          assertFalse("UserOptions should not by empty", userOptions.isEmpty());
64          UserOptions firstUsrOpt = new ArrayList<UserOptions>(userOptions).get(0);
65          assertTrue("UserOption option id should be " + REFRESH_RATE, REFRESH_RATE.equals(firstUsrOpt.getOptionId()));
66          assertTrue("UserOption option value should be 11", ELEVEN.equals(firstUsrOpt.getOptionVal()));
67      }
68  
69      /**
70       * Test save method when a principal id and a {@link Map} keyed by option ids and option values as the mapped value
71       * @throws Exception if errors while validating save method.
72       */
73      @Test
74      public void testUserOptionsSaveWithOptionMap() throws Exception {
75          final UserOptionsService userOptionsService = this.getUserOptionsService();
76          Principal principal = this.getPrincipalByPrincipalName("ewestfal");
77          Collection<UserOptions> userOptions = userOptionsService.findByWorkflowUser(principal.getPrincipalId());
78          assertTrue("UserOptions should by empty", userOptions.isEmpty());
79  
80          // build a option map and persist it to the datasource
81          Map<String, String> userOptMap = new HashMap<String, String>();
82          userOptMap.put("favoriteColor", "blue");
83          userOptMap.put("backgroundColor","white");
84          userOptMap.put(REFRESH_RATE, "30");
85          userOptionsService.save(principal.getPrincipalId(), userOptMap);
86  
87          userOptions = userOptionsService.findByWorkflowUser(principal.getPrincipalId());
88          assertFalse("UserOptions should not be empty", userOptions.isEmpty());
89          assertTrue("UserOptions should contain three items", userOptions.size() == 3);
90      }
91  
92      /**
93       * Test to ensure findByUserQualified method is filtering results with the given principal id and likeString.
94       * @throws Exception if errors while validating findByUserQualified method
95       */
96      @Test
97      public void testFindByUserQualified() throws Exception {
98          final UserOptionsService userOptionsService = this.getUserOptionsService();
99          Principal principal = this.getPrincipalByPrincipalName("ewestfal");
100         Collection<UserOptions> userOptions = userOptionsService.findByWorkflowUser(principal.getPrincipalId());
101         assertTrue("UserOptions should by empty", userOptions.isEmpty());
102 
103         // build a option map and persist it to the datasource
104         Map<String, String> userOptMap = new HashMap<String, String>();
105         userOptMap.put("favoriteColor", "blue");
106         userOptMap.put("favoriteFood","pizza");
107         userOptMap.put("favoriteTimeOfYear", "summer");
108         userOptMap.put(REFRESH_RATE, "30");
109         userOptMap.put("lastSaveDate","now");
110         userOptionsService.save(principal.getPrincipalId(), userOptMap);
111 
112         userOptions = userOptionsService.findByWorkflowUser(principal.getPrincipalId());
113         assertFalse("UserOptions should not be empty", userOptions.isEmpty());
114         assertTrue("UserOptions should contain five items", userOptions.size() == 5);
115 
116         userOptions = userOptionsService.findByUserQualified(principal.getPrincipalId(), "favorite%");
117         assertFalse("UserOptions should not be empty", userOptions.isEmpty());
118         assertTrue("UserOptions should contain 3 items.", userOptions.size() == 3);
119         for (UserOptions opt: userOptions) {
120             assertTrue("UserOptionId (" + opt.getOptionId() + ") should start with favorite",
121                     opt.getOptionId().startsWith("favorite"));
122         }
123     }
124 
125     /**
126      * Test retrieveEmailPreferenceUserOptions method is filtering results with the given principal id and email setting
127      * @throws Exception if errors while validating retrieveEmailPreferenceUserOptions method
128      */
129     @Test
130     public void testRetrieveEmailPreferenceUserOptions() throws Exception {
131         final UserOptionsService userOptionsService = this.getUserOptionsService();
132         Principal principal = this.getPrincipalByPrincipalName("ewestfal");
133         Collection<UserOptions> userOptions = userOptionsService.findByWorkflowUser(principal.getPrincipalId());
134         assertTrue("UserOptions should by empty", userOptions.isEmpty());
135 
136         // test with user options not related to email preferences
137         Map<String, String> userOptMap1 = new HashMap<String, String>();
138         userOptMap1.put("favoriteColor", "blue");
139         userOptMap1.put("favoriteFood","pizza");
140         userOptMap1.put("favoriteTimeOfYear", "summer");
141         userOptMap1.put(REFRESH_RATE, "30");
142         List<UserOptions> optList = userOptionsService.retrieveEmailPreferenceUserOptions("testValue");
143         assertTrue("OptList should be empty.", optList.isEmpty());
144 
145         // test with user options related to email preferences
146         Map<String, String> userOptMap2 = new HashMap<String, String>();
147         userOptMap2.put("favoriteColor", "blue");
148         userOptMap2.put("favoriteFood","pizza");
149         userOptMap2.put("favoriteTimeOfYear", "summer");
150         userOptMap2.put(REFRESH_RATE, "30");
151         userOptMap2.put(KewApiConstants.EMAIL_RMNDR_KEY, KewApiConstants.EMAIL_RMNDR_DAY_VAL);
152         userOptMap2.put("Error" + KewApiConstants.DOCUMENT_TYPE_NOTIFICATION_PREFERENCE_SUFFIX,
153                 KewApiConstants.EMAIL_RMNDR_DAY_VAL);
154         userOptMap2.put("Warning" + KewApiConstants.DOCUMENT_TYPE_NOTIFICATION_PREFERENCE_SUFFIX,
155                 KewApiConstants.EMAIL_RMNDR_DAY_VAL);
156         userOptionsService.save(principal.getPrincipalId(), userOptMap2);
157         // query for email preferences user options only
158         optList = userOptionsService.retrieveEmailPreferenceUserOptions(KewApiConstants.EMAIL_RMNDR_DAY_VAL);
159         assertFalse("OptList should not be empty", optList.isEmpty());
160         assertTrue("OptList should contain 3 items, optList size is: " + optList.size(), optList.size() == 3);
161     }
162 
163     /**
164      * Helper method for test class returns an implementation of the {@link UserOptionsService}.
165      * @return an implementation of the {@link UserOptionsService}
166      */
167     private UserOptionsService getUserOptionsService() {
168         return KEWServiceLocator.getUserOptionsService();
169     }
170 
171     /**
172      * Helper method for test class returns a {@link Principal} for the given principal name.
173      * @param principleName the unique identifier of the user to search for
174      * @return a {@link Principal} for the given principal name.
175      */
176     private Principal getPrincipalByPrincipalName(String principleName) {
177         return  KEWServiceLocator.getIdentityHelperService().getPrincipalByPrincipalName(principleName);
178     }
179 }