Search in sources :

Example 1 with SecurityRolesService

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"));
}
Also used : InternalViewDefinition(com.qcadoo.view.internal.api.InternalViewDefinition) ComponentOption(com.qcadoo.view.internal.ComponentOption) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) JSONArray(org.json.JSONArray) Document(org.w3c.dom.Document) DataDefinition(com.qcadoo.model.api.DataDefinition) ViewDefinitionParserImpl(com.qcadoo.view.internal.xml.ViewDefinitionParserImpl) GridComponentPattern(com.qcadoo.view.internal.components.grid.GridComponentPattern) JSONObject(org.json.JSONObject) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SecurityRolesService(com.qcadoo.security.api.SecurityRolesService) ViewDefinitionParser(com.qcadoo.view.internal.xml.ViewDefinitionParser) ComponentDefinition(com.qcadoo.view.internal.ComponentDefinition) AbstractPatternTest(com.qcadoo.view.internal.patterns.AbstractPatternTest) Test(org.junit.Test)

Example 2 with SecurityRolesService

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();
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) StringType(com.qcadoo.model.internal.types.StringType) FieldComponentPattern(com.qcadoo.view.internal.components.FieldComponentPattern) FieldDefinition(com.qcadoo.model.api.FieldDefinition) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) DataDefinition(com.qcadoo.model.api.DataDefinition) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) TranslationService(com.qcadoo.localization.api.TranslationService) FieldComponentState(com.qcadoo.view.internal.components.FieldComponentState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) SecurityRolesService(com.qcadoo.security.api.SecurityRolesService) Matchers.anyLong(org.mockito.Matchers.anyLong) ExpressionServiceImpl(com.qcadoo.model.internal.ExpressionServiceImpl) Before(org.junit.Before)

Example 3 with SecurityRolesService

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());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) GridComponentOrderColumn(com.qcadoo.view.internal.components.grid.GridComponentOrderColumn) GridComponentPattern(com.qcadoo.view.internal.components.grid.GridComponentPattern) JSONObject(org.json.JSONObject) GridComponentState(com.qcadoo.view.internal.components.grid.GridComponentState) SecurityRolesService(com.qcadoo.security.api.SecurityRolesService) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Example 4 with SecurityRolesService

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);
}
Also used : SecurityRole(com.qcadoo.security.api.SecurityRole) CustomViewService(com.qcadoo.view.beans.sample.CustomViewService) StringType(com.qcadoo.model.internal.types.StringType) ViewDefinitionServiceImpl(com.qcadoo.view.internal.ViewDefinitionServiceImpl) FieldDefinition(com.qcadoo.model.api.FieldDefinition) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) DataDefinition(com.qcadoo.model.api.DataDefinition) RibbonParserService(com.qcadoo.view.internal.ribbon.RibbonParserService) ApplicationContext(org.springframework.context.ApplicationContext) HasManyType(com.qcadoo.model.api.types.HasManyType) BelongsToType(com.qcadoo.model.api.types.BelongsToType) TranslationService(com.qcadoo.localization.api.TranslationService) SecurityRolesService(com.qcadoo.security.api.SecurityRolesService) Before(org.junit.Before)

Example 5 with SecurityRolesService

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();
}
Also used : Locale(java.util.Locale) Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) JSONArray(org.json.JSONArray) GridComponentColumn(com.qcadoo.view.internal.components.grid.GridComponentColumn) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) Matchers.anyString(org.mockito.Matchers.anyString) DataDefinition(com.qcadoo.model.api.DataDefinition) HasManyType(com.qcadoo.model.api.types.HasManyType) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) ApplicationContext(org.springframework.context.ApplicationContext) GridComponentPattern(com.qcadoo.view.internal.components.grid.GridComponentPattern) JSONObject(org.json.JSONObject) TranslationService(com.qcadoo.localization.api.TranslationService) GridComponentState(com.qcadoo.view.internal.components.grid.GridComponentState) SecurityRolesService(com.qcadoo.security.api.SecurityRolesService) ExpressionServiceImpl(com.qcadoo.model.internal.ExpressionServiceImpl) Before(org.junit.Before)

Aggregations

SecurityRolesService (com.qcadoo.security.api.SecurityRolesService)5 DataDefinition (com.qcadoo.model.api.DataDefinition)4 JSONObject (org.json.JSONObject)4 ApplicationContext (org.springframework.context.ApplicationContext)4 TranslationService (com.qcadoo.localization.api.TranslationService)3 FieldDefinition (com.qcadoo.model.api.FieldDefinition)3 GridComponentPattern (com.qcadoo.view.internal.components.grid.GridComponentPattern)3 Before (org.junit.Before)3 Entity (com.qcadoo.model.api.Entity)2 HasManyType (com.qcadoo.model.api.types.HasManyType)2 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)2 ExpressionServiceImpl (com.qcadoo.model.internal.ExpressionServiceImpl)2 StringType (com.qcadoo.model.internal.types.StringType)2 ViewDefinitionState (com.qcadoo.view.api.ViewDefinitionState)2 GridComponentState (com.qcadoo.view.internal.components.grid.GridComponentState)2 JSONArray (org.json.JSONArray)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 DataDefinitionService (com.qcadoo.model.api.DataDefinitionService)1 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)1