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