use of com.qcadoo.security.api.SecurityRolesService 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"));
}
use of com.qcadoo.security.api.SecurityRolesService in project qcadoo by qcadoo.
the class FormComponentStateTest method init.
@Before
public void init() throws Exception {
entity = mock(Entity.class);
given(entity.getField("name")).willReturn("text");
viewDefinitionState = mock(ViewDefinitionState.class);
translationService = mock(TranslationService.class);
fieldDefinition = mock(FieldDefinition.class);
given(fieldDefinition.getType()).willReturn(new StringType());
given(fieldDefinition.getName()).willReturn("name");
dataDefinition = mock(DataDefinition.class, RETURNS_DEEP_STUBS);
given(dataDefinition.get(12L)).willReturn(null);
given(dataDefinition.get(13L)).willReturn(entity);
given(dataDefinition.getPluginIdentifier()).willReturn("plugin");
given(dataDefinition.getName()).willReturn("name");
given(dataDefinition.getField("name")).willReturn(fieldDefinition);
given(dataDefinition.delete(any(Long.class))).willReturn(EntityOpResult.successfull());
given(dataDefinition.create(anyLong())).willAnswer(new Answer<Entity>() {
@Override
public Entity answer(final InvocationOnMock invocation) throws Throwable {
Long id = (Long) invocation.getArguments()[0];
return new DefaultEntity(dataDefinition, id);
}
});
FieldComponentPattern namePattern = mock(FieldComponentPattern.class);
given(namePattern.isRequired()).willReturn(false);
given(namePattern.isPersistent()).willReturn(true);
name = new FieldComponentState(namePattern);
name.setTranslationService(translationService);
name.setName("name");
name.initialize(new JSONObject(), Locale.ENGLISH);
FormComponentPattern pattern = mock(FormComponentPattern.class);
given(pattern.getExpressionNew()).willReturn(null);
given(pattern.getExpressionEdit()).willReturn("'static expression'");
applicationContext = mock(ApplicationContext.class);
setField(pattern, "applicationContext", applicationContext);
SecurityRolesService securityRolesService = mock(SecurityRolesService.class);
given(applicationContext.getBean(SecurityRolesService.class)).willReturn(securityRolesService);
form = new FormComponentState(pattern);
form.setDataDefinition(dataDefinition);
form.setTranslationService(translationService);
form.addFieldEntityIdChangeListener("name", name);
form.initialize(new JSONObject(ImmutableMap.of("components", new JSONObject())), Locale.ENGLISH);
new ExpressionServiceImpl().init();
}
use of com.qcadoo.security.api.SecurityRolesService in project qcadoo by qcadoo.
the class GridComponentStateTest method shouldInitializeWithoutData.
@Test
@SuppressWarnings("unchecked")
public void shouldInitializeWithoutData() throws Exception {
// given
GridComponentPattern pattern = mock(GridComponentPattern.class);
given(pattern.getColumns()).willReturn(columns);
given(pattern.getBelongsToFieldDefinition()).willReturn(null);
given(pattern.isActivable()).willReturn(false);
given(pattern.isWeakRelation()).willReturn(false);
ApplicationContext applicationContext = mock(ApplicationContext.class);
setField(pattern, "applicationContext", applicationContext);
SecurityRolesService securityRolesService = mock(SecurityRolesService.class);
given(applicationContext.getBean(SecurityRolesService.class)).willReturn(securityRolesService);
grid = new GridComponentState(productDataDefinition, pattern);
grid.setDataDefinition(substituteDataDefinition);
JSONObject json = new JSONObject(Collections.singletonMap(AbstractComponentState.JSON_CONTENT, new JSONObject()));
// when
grid.initialize(json, Locale.ENGLISH);
// then
assertNull(getField(grid, "belongsToFieldDefinition"));
assertNull(getField(grid, "selectedEntityId"));
assertNull(getField(grid, "belongsToEntityId"));
assertNull(getField(grid, "entities"));
assertEquals(0, getField(grid, "totalEntities"));
assertEquals(0, getField(grid, "firstResult"));
assertEquals(Integer.MAX_VALUE, getField(grid, "maxResults"));
assertTrue((Boolean) getField(grid, "filtersEnabled"));
List<GridComponentOrderColumn> orderColumns = (List<GridComponentOrderColumn>) getField(grid, "orderColumns");
assertEquals(0, orderColumns.size());
Map<String, String> filters = (Map<String, String>) getField(grid, "filters");
assertEquals(0, filters.size());
}
use of com.qcadoo.security.api.SecurityRolesService in project qcadoo by qcadoo.
the class ViewDefinitionParserImplTest method init.
@Before
public void init() throws Exception {
applicationContext = mock(ApplicationContext.class);
dataDefinitionService = mock(DataDefinitionService.class);
translationService = mock(TranslationService.class);
RibbonParserService ribbonService = new RibbonParserService();
contextualHelpService = mock(ContextualHelpService.class);
securityRolesService = mock(SecurityRolesService.class);
viewDefinitionService = new ViewDefinitionServiceImpl();
hookFactory = new HookFactory();
setField(hookFactory, "applicationContext", applicationContext);
viewDefinitionParser = new ViewDefinitionParserImpl();
setField(viewDefinitionParser, "dataDefinitionService", dataDefinitionService);
setField(viewDefinitionParser, "viewDefinitionService", viewDefinitionService);
setField(viewDefinitionParser, "hookFactory", hookFactory);
setField(viewDefinitionParser, "translationService", translationService);
setField(viewDefinitionParser, "applicationContext", applicationContext);
setField(viewDefinitionParser, "contextualHelpService", contextualHelpService);
setField(viewDefinitionParser, "viewComponentsResolver", viewComponentsResolver);
setField(viewDefinitionParser, "ribbonService", ribbonService);
setField(viewDefinitionParser, "securityRolesService", securityRolesService);
userRoleMock = mock(SecurityRole.class);
given(securityRolesService.getRoleByIdentifier("ROLE_USER")).willReturn(userRoleMock);
adminRoleMock = mock(SecurityRole.class);
given(securityRolesService.getRoleByIdentifier("ROLE_ADMIN")).willReturn(adminRoleMock);
xml1 = "view/test1.xml";
xml2 = "view/test2.xml";
given(applicationContext.getBean(CustomViewService.class)).willReturn(new CustomViewService());
dataDefinitionA = mock(DataDefinition.class);
dataDefinitionB = mock(DataDefinition.class);
FieldDefinition nameA = mock(FieldDefinition.class, "nameA");
FieldDefinition nameB = mock(FieldDefinition.class, "nameB");
FieldDefinition hasManyB = mock(FieldDefinition.class, "hasManyB");
FieldDefinition belongToA = mock(FieldDefinition.class, "belongsToA");
HasManyType hasManyBType = mock(HasManyType.class);
BelongsToType belongToAType = mock(BelongsToType.class);
given(nameA.getDataDefinition()).willReturn(dataDefinitionA);
given(nameA.getType()).willReturn(new StringType());
given(nameB.getType()).willReturn(new StringType());
given(nameB.getDataDefinition()).willReturn(dataDefinitionA);
given(hasManyB.getType()).willReturn(hasManyBType);
given(hasManyB.getDataDefinition()).willReturn(dataDefinitionB);
given(belongToA.getType()).willReturn(belongToAType);
given(belongToA.getDataDefinition()).willReturn(dataDefinitionB);
given(hasManyBType.getDataDefinition()).willReturn(dataDefinitionB);
given(belongToAType.getDataDefinition()).willReturn(dataDefinitionA);
given(dataDefinitionA.getField("beansB")).willReturn(hasManyB);
given(dataDefinitionA.getField("name")).willReturn(nameA);
given(dataDefinitionB.getField("activeA")).willReturn(nameA);
given(dataDefinitionB.getField("activeB")).willReturn(nameA);
given(dataDefinitionB.getField("activeC")).willReturn(nameA);
given(dataDefinitionB.getField("beanA")).willReturn(belongToA);
given(dataDefinitionB.getField("beanM")).willReturn(belongToA);
given(dataDefinitionB.getField("beanB")).willReturn(belongToA);
given(dataDefinitionB.getField("name")).willReturn(nameB);
given(dataDefinitionA.getName()).willReturn("beanA");
given(dataDefinitionB.getName()).willReturn("beanB");
given(dataDefinitionA.getFields()).willReturn(ImmutableMap.of("name", nameA, "beansB", hasManyB));
given(dataDefinitionB.getFields()).willReturn(ImmutableMap.of("name", nameB, "beanA", belongToA));
given(dataDefinitionService.get("sample", "beanA")).willReturn(dataDefinitionA);
given(dataDefinitionService.get("sample", "beanB")).willReturn(dataDefinitionB);
}
use of com.qcadoo.security.api.SecurityRolesService in project qcadoo by qcadoo.
the class GridComponentStateTest method init.
@Before
public void init() throws Exception {
JSONObject jsonContent = new JSONObject();
jsonContent.put(GridComponentState.JSON_SELECTED_ENTITY_ID, 13L);
jsonContent.put(GridComponentState.JSON_MULTISELECT_MODE, false);
JSONObject jsonSelected = new JSONObject();
jsonSelected.put("13", true);
jsonContent.put(GridComponentState.JSON_SELECTED_ENTITIES, jsonSelected);
jsonContent.put(GridComponentState.JSON_BELONGS_TO_ENTITY_ID, 1L);
jsonContent.put(GridComponentState.JSON_FIRST_ENTITY, 60);
jsonContent.put(GridComponentState.JSON_MAX_ENTITIES, 30);
jsonContent.put(GridComponentState.JSON_FILTERS_ENABLED, true);
JSONArray jsonOrder = new JSONArray();
jsonOrder.put(ImmutableMap.of("column", "asd", "direction", "asc"));
jsonContent.put(GridComponentState.JSON_ORDER, jsonOrder);
JSONObject jsonFilters = new JSONObject();
jsonFilters.put("asd", "test");
jsonFilters.put("qwe", "test2");
jsonContent.put(GridComponentState.JSON_FILTERS, jsonFilters);
json = new JSONObject(Collections.singletonMap(AbstractComponentState.JSON_CONTENT, jsonContent));
Entity entity = mock(Entity.class);
given(entity.getField("name")).willReturn("text");
viewDefinitionState = mock(ViewDefinitionState.class);
productDataDefinition = mock(DataDefinition.class, RETURNS_DEEP_STUBS);
substituteDataDefinition = mock(DataDefinition.class, "substituteDataDefinition");
HasManyType substitutesFieldType = mock(HasManyType.class);
given(substitutesFieldType.getDataDefinition()).willReturn(substituteDataDefinition);
given(substitutesFieldType.getJoinFieldName()).willReturn("product");
substitutesFieldDefinition = mock(FieldDefinition.class);
given(substitutesFieldDefinition.getType()).willReturn(substitutesFieldType);
given(substitutesFieldDefinition.getName()).willReturn("substitutes");
given(substitutesFieldDefinition.getDataDefinition()).willReturn(substituteDataDefinition);
substituteCriteria = mock(SearchCriteriaBuilder.class);
given(substituteDataDefinition.getPluginIdentifier()).willReturn("plugin");
given(substituteDataDefinition.getName()).willReturn("substitute");
given(substituteDataDefinition.find()).willReturn(substituteCriteria);
given(productDataDefinition.getPluginIdentifier()).willReturn("plugin");
given(productDataDefinition.getName()).willReturn("product");
given(productDataDefinition.getField("substitutes")).willReturn(substitutesFieldDefinition);
columns = new LinkedHashMap<String, GridComponentColumn>();
TranslationService translationService = mock(TranslationService.class);
given(translationService.translate(Mockito.anyString(), Mockito.any(Locale.class))).willReturn("i18n");
given(translationService.translate(Mockito.anyString(), Mockito.anyString(), Mockito.any(Locale.class))).willReturn("i18n");
given(translationService.translate(Mockito.anyString(), Mockito.any(Locale.class))).willReturn("i18n");
GridComponentPattern pattern = mock(GridComponentPattern.class);
given(pattern.getColumns()).willReturn(columns);
given(pattern.getBelongsToFieldDefinition()).willReturn(substitutesFieldDefinition);
given(pattern.isActivable()).willReturn(false);
given(pattern.isWeakRelation()).willReturn(false);
ApplicationContext applicationContext = mock(ApplicationContext.class);
setField(pattern, "applicationContext", applicationContext);
SecurityRolesService securityRolesService = mock(SecurityRolesService.class);
given(applicationContext.getBean(SecurityRolesService.class)).willReturn(securityRolesService);
grid = new GridComponentState(productDataDefinition, pattern);
grid.setDataDefinition(substituteDataDefinition);
grid.setTranslationService(translationService);
new ExpressionServiceImpl().init();
}
Aggregations