Search in sources :

Example 11 with InternalComponentState

use of com.qcadoo.view.internal.api.InternalComponentState in project qcadoo by qcadoo.

the class AbstractContainerState method render.

@Override
public JSONObject render() throws JSONException {
    JSONObject json = super.render();
    JSONObject childerJson = new JSONObject();
    for (Map.Entry<String, InternalComponentState> child : children.entrySet()) {
        childerJson.put(child.getKey(), child.getValue().render());
    }
    json.put(JSON_CHILDREN, childerJson);
    return json;
}
Also used : JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState)

Example 12 with InternalComponentState

use of com.qcadoo.view.internal.api.InternalComponentState in project qcadoo by qcadoo.

the class CrudControllerTest method shouldPerformEvent.

@Test
public void shouldPerformEvent() throws Exception {
    // given
    InternalViewDefinitionState state = mock(InternalViewDefinitionState.class, Mockito.withSettings().extraInterfaces(InternalComponentState.class));
    InternalViewDefinition viewDefinition = mock(InternalViewDefinition.class);
    ViewDefinitionService viewDefinitionService = mock(ViewDefinitionService.class);
    given(viewDefinitionService.get("testPlugin", "testView")).willReturn(viewDefinition);
    JSONObject jsonBody = new JSONObject();
    jsonBody.put("test", "testVal1");
    JSONObject jsonResult = new JSONObject();
    jsonResult.put("test", "testVal2");
    CrudService crud = new CrudServiceImpl();
    ReflectionTestUtils.setField(crud, "viewDefinitionService", viewDefinitionService);
    given(viewDefinition.performEvent(jsonBody, Locale.ENGLISH)).willReturn(state);
    given(((InternalComponentState) state).render()).willReturn(jsonResult);
    // when
    Object result = crud.invokeEventAndRenderView("testPlugin", "testView", jsonBody, Locale.ENGLISH);
    // then
    assertEquals(jsonResult, result);
}
Also used : CrudService(com.qcadoo.view.api.crud.CrudService) ViewDefinitionService(com.qcadoo.view.internal.api.ViewDefinitionService) JSONObject(org.json.JSONObject) InternalViewDefinitionState(com.qcadoo.view.internal.api.InternalViewDefinitionState) InternalViewDefinition(com.qcadoo.view.internal.api.InternalViewDefinition) JSONObject(org.json.JSONObject) CrudServiceImpl(com.qcadoo.view.internal.crud.CrudServiceImpl) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState) Test(org.junit.Test)

Example 13 with InternalComponentState

use of com.qcadoo.view.internal.api.InternalComponentState in project qcadoo by qcadoo.

the class ViewDefinitionStateTest method shouldPerformEventOnAllComponent.

@Test
public void shouldPerformEventOnAllComponent() throws Exception {
    // given
    ViewDefinitionStateImpl viewDefinitionState = new ViewDefinitionStateImpl();
    ContainerState state1 = mock(ContainerState.class);
    given(state1.getName()).willReturn("name1");
    InternalComponentState state2 = mock(InternalComponentState.class);
    viewDefinitionState.addChild(state1);
    given(state1.getChildren()).willReturn(Collections.singletonMap("name2", state2));
    // when
    viewDefinitionState.performEvent((String) null, "event", new String[] { "arg1", "arg2" });
    // then
    Mockito.verify(state1).performEvent(viewDefinitionState, "event", "arg1", "arg2");
    Mockito.verify(state2).performEvent(viewDefinitionState, "event", "arg1", "arg2");
}
Also used : ViewDefinitionStateImpl(com.qcadoo.view.internal.internal.ViewDefinitionStateImpl) ContainerState(com.qcadoo.view.internal.api.ContainerState) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState) Test(org.junit.Test) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest)

Example 14 with InternalComponentState

use of com.qcadoo.view.internal.api.InternalComponentState in project qcadoo by qcadoo.

the class ViewDefinitionTest method shouldCallEvent.

@Test
public void shouldCallEvent() throws Exception {
    // given
    InternalViewDefinition viewDefinition = new ViewDefinitionImpl("name", "plugin", mock(DataDefinition.class), true, null);
    TestEvent event = mock(TestEvent.class);
    ComponentStateMock state = new ComponentStateMock(new JSONObject(of("asd", "123")));
    state.registerTestEvent("eventName", event);
    ComponentPatternMock pattern = new ComponentPatternMock(getComponentDefinition("componentName", viewDefinition), state);
    viewDefinition.addComponentPattern(pattern);
    JSONObject eventJson = new JSONObject();
    eventJson.put(InternalViewDefinition.JSON_EVENT_NAME, "eventName");
    eventJson.put(InternalViewDefinition.JSON_EVENT_COMPONENT, "componentName");
    eventJson.put(InternalViewDefinition.JSON_EVENT_ARGS, new JSONArray(newArrayList("arg1", "arg2")));
    JSONObject contentJson = new JSONObject();
    contentJson.put("asd", "qwe");
    JSONObject componentJson = new JSONObject();
    componentJson.put(AbstractComponentState.JSON_CONTENT, contentJson);
    JSONObject json = new JSONObject();
    json.put(InternalViewDefinition.JSON_EVENT, eventJson);
    json.put(InternalViewDefinition.JSON_COMPONENTS, new JSONObject(of("componentName", componentJson)));
    // when
    JSONObject result = ((InternalComponentState) viewDefinition.performEvent(json, Locale.ENGLISH)).render();
    // then
    assertEquals(contentJson, state.getContent());
    verify(event).invoke(new String[] { "arg1", "arg2" });
    Assert.assertEquals("123", result.getJSONObject("components").getJSONObject("componentName").getJSONObject("content").get("asd"));
}
Also used : ViewDefinitionImpl(com.qcadoo.view.internal.internal.ViewDefinitionImpl) JSONObject(org.json.JSONObject) TestEvent(com.qcadoo.view.internal.states.ComponentStateMock.TestEvent) InternalViewDefinition(com.qcadoo.view.internal.api.InternalViewDefinition) JSONArray(org.json.JSONArray) ComponentPatternMock(com.qcadoo.view.internal.patterns.ComponentPatternMock) DataDefinition(com.qcadoo.model.api.DataDefinition) ComponentStateMock(com.qcadoo.view.internal.states.ComponentStateMock) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState) Test(org.junit.Test) AbstractPatternTest(com.qcadoo.view.internal.patterns.AbstractPatternTest)

Example 15 with InternalComponentState

use of com.qcadoo.view.internal.api.InternalComponentState in project qcadoo by qcadoo.

the class FormComponentStateTest method shouldInitialeFormWithEntityId.

@Test
public void shouldInitialeFormWithEntityId() throws Exception {
    // given
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn(null);
    setField(pattern, "applicationContext", applicationContext);
    InternalComponentState componentState = new FormComponentState(pattern);
    JSONObject json = new JSONObject();
    JSONObject jsonContent = new JSONObject();
    jsonContent.put(FormComponentState.JSON_ENTITY_ID, 13L);
    json.put(AbstractComponentState.JSON_CONTENT, jsonContent);
    JSONObject jsonChildren = new JSONObject();
    json.put(AbstractComponentState.JSON_CHILDREN, jsonChildren);
    // when
    componentState.initialize(json, Locale.ENGLISH);
    // then
    assertEquals(13L, componentState.getFieldValue());
}
Also used : FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) JSONObject(org.json.JSONObject) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Aggregations

InternalComponentState (com.qcadoo.view.internal.api.InternalComponentState)29 Test (org.junit.Test)20 JSONObject (org.json.JSONObject)17 SimpleComponentState (com.qcadoo.view.internal.components.SimpleComponentState)8 FormComponentPattern (com.qcadoo.view.internal.components.form.FormComponentPattern)8 FormComponentState (com.qcadoo.view.internal.components.form.FormComponentState)7 ContainerState (com.qcadoo.view.internal.api.ContainerState)4 AbstractStateTest (com.qcadoo.view.internal.states.AbstractStateTest)4 Map (java.util.Map)4 ComponentState (com.qcadoo.view.api.ComponentState)3 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)3 JSONArray (org.json.JSONArray)3 ComponentPattern (com.qcadoo.view.internal.api.ComponentPattern)2 InternalViewDefinitionState (com.qcadoo.view.internal.api.InternalViewDefinitionState)2 ViewDefinitionStateImpl (com.qcadoo.view.internal.internal.ViewDefinitionStateImpl)2 AbstractContainerState (com.qcadoo.view.internal.states.AbstractContainerState)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 DataDefinition (com.qcadoo.model.api.DataDefinition)1 CrudService (com.qcadoo.view.api.crud.CrudService)1