1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
106
107 public void testComponentOptionsDefault() throws Exception {
108 assertRichTableComponentOptions(null, "[" + EXPECTED + " ]", UifConstants.TableToolsKeys.AO_COLUMN_DEFS);
109 }
110
111 @Test
112
113
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
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
141
142 public void testComponentOptionsHideColumnOnLayoutManager() {
143
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
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
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
166
167
168
169
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 }