use of com.qcadoo.view.internal.xml.ViewDefinitionParserImpl in project qcadoo by qcadoo.
the class GridComponentPatternTest method shouldParsePredefinedFilters.
@Test
public void shouldParsePredefinedFilters() throws Exception {
// given
DataDefinition dataDefinition = mock(DataDefinition.class);
InternalViewDefinition viewDefinition = mock(InternalViewDefinition.class);
SecurityRolesService securityRolesService = mock(SecurityRolesService.class);
given(viewDefinition.getDataDefinition()).willReturn(dataDefinition);
ComponentDefinition componentDefinition = getComponentDefinition(QcadooViewConstants.L_GRID, viewDefinition);
componentDefinition.setTranslationService(translationService);
componentDefinition.setApplicationContext(applicationContext);
GridComponentPattern pattern = new GridComponentPattern(componentDefinition);
pattern.addOption(new ComponentOption("order", ImmutableMap.of("column", "name", "direction", "asc")));
ViewDefinitionParser parser = new ViewDefinitionParserImpl();
setField(parser, "securityRolesService", securityRolesService);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Node componentNode = doc.createElement("root");
Node predefinedFiltersNode = doc.createElement("predefinedFilters");
componentNode.appendChild(predefinedFiltersNode);
Element predefinedFilter1 = doc.createElement("predefinedFilter");
predefinedFilter1.setAttribute("name", "filter1");
predefinedFiltersNode.appendChild(predefinedFilter1);
Element predefinedFilter2 = doc.createElement("predefinedFilter");
predefinedFilter2.setAttribute("name", "filter2");
predefinedFiltersNode.appendChild(predefinedFilter2);
Element orderNode = doc.createElement("filterOrder");
orderNode.setAttribute("column", "testCol");
orderNode.setAttribute("direction", "desc");
predefinedFilter2.appendChild(orderNode);
Element filterNode1 = doc.createElement("filterRestriction");
filterNode1.setAttribute("column", "testCol1");
filterNode1.setAttribute("value", "testVal1");
predefinedFilter2.appendChild(filterNode1);
Element filterNode2 = doc.createElement("filterRestriction");
filterNode2.setAttribute("column", "testCol2");
filterNode2.setAttribute("value", "testVal2");
predefinedFilter2.appendChild(filterNode2);
pattern.initialize();
// when
pattern.parse(componentNode, parser);
// then
JSONObject options = getJsOptions(pattern);
JSONArray filtersArray = options.getJSONArray("predefinedFilters");
assertNotNull(filtersArray);
assertEquals(2, filtersArray.length());
assertEquals("filter1", filtersArray.getJSONObject(0).get("label"));
assertEquals("name", filtersArray.getJSONObject(0).get("orderColumn"));
assertEquals("asc", filtersArray.getJSONObject(0).get("orderDirection"));
assertEquals(0, filtersArray.getJSONObject(0).getJSONObject("filter").length());
assertEquals("filter2", filtersArray.getJSONObject(1).get("label"));
assertEquals("testCol", filtersArray.getJSONObject(1).get("orderColumn"));
assertEquals("desc", filtersArray.getJSONObject(1).get("orderDirection"));
assertEquals(2, filtersArray.getJSONObject(1).getJSONObject("filter").length());
assertEquals("testVal1", filtersArray.getJSONObject(1).getJSONObject("filter").getString("testCol1"));
assertEquals("testVal2", filtersArray.getJSONObject(1).getJSONObject("filter").getString("testCol2"));
}
Aggregations