View Javadoc

1   /**
2    * Copyright 2005-2013 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.uif.widget;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.config.property.ConfigurationService;
21  import org.kuali.rice.krad.datadictionary.validation.Employee;
22  import org.kuali.rice.krad.uif.UifConstants;
23  import org.kuali.rice.krad.uif.component.Component;
24  import org.kuali.rice.krad.uif.container.CollectionGroup;
25  import org.kuali.rice.krad.uif.field.DataField;
26  import org.kuali.rice.krad.uif.layout.TableLayoutManager;
27  import org.kuali.rice.krad.uif.service.ViewHelperService;
28  import org.kuali.rice.krad.uif.view.View;
29  import org.kuali.rice.krad.web.form.UifFormBase;
30  
31  import java.util.ArrayList;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Set;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.mockito.Mockito.*;
38  
39  /**
40   * test the RichTable widget
41   */
42  
43  public class RichTableTest {
44  
45      public static final String S_TYPE = "{\"bSortable\" : false, \"sType\" : \"numeric\", \"aTargets\": [0]}";
46      public static final String S_SORT_DATA_TARGETS_1 = "{\"sType\" : \"string\", \"sSortDataType\" : \"dom-text\", \"aTargets\": [1]}";
47      public static final String S_SORT_DATA_TARGETS_2 = S_SORT_DATA_TARGETS_1.replace("1", "2");
48      public static final String S_SORT_DATA_TARGETS_3 = S_SORT_DATA_TARGETS_1.replace("1", "3");
49  
50      public static final String EXPECTED = S_TYPE + ", " +
51              S_SORT_DATA_TARGETS_1 + " , " +
52              S_SORT_DATA_TARGETS_2 + " , " +
53              S_SORT_DATA_TARGETS_3;
54  
55      public static final String B_VISIBLE_FALSE_TARGETS_1 = "{bVisible: false, \"aTargets\": [1]}";
56      public static final String B_SORTABLE_FALSE_TARGETS_3 = "{'bSortable': false, \"aTargets\": [3]}";
57      
58      private RichTable richTable;
59      private CollectionGroup group;
60      private View mockView;
61  
62      //private
63      @Before
64      public void setup() {
65          richTable = new RichTable();
66  
67          richTable = spy(richTable);
68  
69          ConfigurationService configurationService = mock(ConfigurationService.class);
70          doReturn(configurationService).when(richTable).getConfigurationService();
71  
72          group = new CollectionGroup();
73          group.setCollectionObjectClass(Employee.class);
74  
75          TableLayoutManager layoutManager = new TableLayoutManager();
76          layoutManager.setRenderSequenceField(true);
77  
78          List<Component> items = new ArrayList<Component>(1);
79          DataField name = new DataField();
80          name.setPropertyName("employeeId");
81          items.add(name);
82          DataField number = new DataField();
83          number.setPropertyName("positionTitle");
84          items.add(number);
85          DataField contactEmail = new DataField();
86          contactEmail.setPropertyName("contactEmail");
87          items.add(contactEmail);
88  
89          layoutManager = spy(layoutManager);
90          doReturn(items).when(layoutManager).getFirstRowFields();
91  
92          group.setLayoutManager(layoutManager);
93          group.setIncludeLineSelectionField(false);
94          group.setRenderLineActions(false);
95  
96          group.setItems(items);
97  
98          mockView =  mock(View.class);
99          ViewHelperService mockViewHelperService = mock(ViewHelperService.class);
100         when(mockView.getViewHelperService()).thenReturn(mockViewHelperService);
101     }
102 
103     @Test
104     /**
105      * test that without aoColumns being set explicitly, the default behaviour continues
106      */
107     public void testComponentOptionsDefault() throws Exception {
108         assertRichTableComponentOptions(null, "[" + EXPECTED + " ]", UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
109     }
110 
111     @Test
112     /**
113      * test that when aoColumns is explicitly set, it is integrated into the rich table rendering logic
114      */
115     public void testComponentOptionsAoColumnsJSOptions() throws Exception {
116         String innerColValues = "{bVisible: false}, null, null";
117         assertRichTableComponentOptions("[" + innerColValues + "]", "[" + EXPECTED + " ," + innerColValues + "]", UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
118     }
119 
120     @Test
121     /**
122      * test whether a hidden column, when marked as sortable is still hidden
123      */
124     public void testComponentOptionsHideColumnOnRichTable() {
125         Set<String> hiddenColumns = new HashSet<String>();
126         hiddenColumns.add("employeeId");
127         Set<String> sortableColumns = new HashSet<String>();
128         sortableColumns.add("positionTitle");
129         richTable.setSortableColumns(sortableColumns);
130         richTable.setHiddenColumns(hiddenColumns);
131         String expected = "[" + S_TYPE + ", " +
132                 B_VISIBLE_FALSE_TARGETS_1 + ", " +
133                 S_SORT_DATA_TARGETS_2 +", " +
134                 B_SORTABLE_FALSE_TARGETS_3 + "]";
135         assertRichTableComponentOptions(null, expected, UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
136     }
137 
138     @Test
139     /**
140      * test that sortableColumns and hiddenColumns, when set on layoutManager, will not override those properties on the richTable
141      */
142     public void testComponentOptionsHideColumnOnLayoutManager() {
143         // set rich table properties
144         Set<String> richTableHiddenColumns = new HashSet<String>();
145         richTableHiddenColumns.add("employeeId");
146         Set<String> sortableColumns = new HashSet<String>();
147         sortableColumns.add("positionTitle");
148         richTable.setSortableColumns(sortableColumns);
149         richTable.setHiddenColumns(richTableHiddenColumns);
150         // set layout manager properties
151         Set<String> lmHiddenColumns = new HashSet<String>();
152         lmHiddenColumns.add("contactEmail");
153         Set<String> lmSortableColumns = new HashSet<String>();
154         lmSortableColumns.add("employeeId");
155         ((TableLayoutManager) group.getLayoutManager()).setSortableColumns(lmSortableColumns);
156         ((TableLayoutManager) group.getLayoutManager()).setHiddenColumns(lmHiddenColumns);
157         // Watch out for spaces
158         String expected = "[" + EXPECTED.replace(S_SORT_DATA_TARGETS_1 + " ,", B_VISIBLE_FALSE_TARGETS_1 + ",") + "]";
159         expected = expected.replace(S_SORT_DATA_TARGETS_2 + " ,", S_SORT_DATA_TARGETS_2 + ",");
160         expected = expected.replace(S_SORT_DATA_TARGETS_3, B_SORTABLE_FALSE_TARGETS_3);
161         assertRichTableComponentOptions(null, expected, UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
162     }
163 
164     /**
165      * a common method to test rich table options
166      *
167      * @param optionsOnGroup - a string in JSON format of the options set on the collection group
168      * @param optionsOnRichTable - a string in JSON format of the options set on the rich table
169      * @param optionKey - a string with the rich table option key being tested
170      */
171     private void assertRichTableComponentOptions(String optionsOnGroup, String optionsOnRichTable, String optionKey) {
172         richTable.getTemplateOptions().put(optionKey, optionsOnGroup);
173         richTable.performFinalize(mockView, new UifFormBase(), group);
174         assertEquals(optionsOnRichTable, richTable.getTemplateOptions().get(optionKey));
175     }
176 }