Search in sources :

Example 11 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class FormComponentStateTest method shouldUseContextWhileSaving.

@Test
public void shouldUseContextWhileSaving() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition, 13L, Collections.singletonMap("name", (Object) "text2"));
    Entity savedEntity = new DefaultEntity(dataDefinition, 13L, Collections.singletonMap("name", (Object) "text2"));
    given(dataDefinition.create(13L)).willReturn(new DefaultEntity(dataDefinition, 13L));
    given(dataDefinition.save(eq(entity))).willReturn(savedEntity);
    given(dataDefinition.getFields().keySet()).willReturn(Collections.singleton("name"));
    name.setFieldValue("text");
    JSONObject json = new JSONObject();
    JSONObject jsonContext = new JSONObject();
    jsonContext.put("id", 14L);
    jsonContext.put("name", "text2");
    JSONObject jsonContent = new JSONObject();
    jsonContent.put(FormComponentState.JSON_ENTITY_ID, 13L);
    json.put(AbstractComponentState.JSON_CONTEXT, jsonContext);
    json.put(AbstractComponentState.JSON_CONTENT, jsonContent);
    json.put(AbstractComponentState.JSON_CHILDREN, new JSONObject());
    form.initialize(json, Locale.ENGLISH);
    // when
    form.performEvent(viewDefinitionState, "save", new String[0]);
    // then
    verify(dataDefinition).save(eq(entity));
    assertEquals("text2", name.getFieldValue());
    assertEquals(13L, form.getFieldValue());
    assertTrue(form.isValid());
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) JSONObject(org.json.JSONObject) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) JSONObject(org.json.JSONObject) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Example 12 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class FormComponentStateTest method shouldHaveValidationErrors.

@Test
public void shouldHaveValidationErrors() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition, null, Collections.singletonMap("name", (Object) "text"));
    Entity savedEntity = new DefaultEntity(dataDefinition, null, Collections.singletonMap("name", (Object) "text2"));
    savedEntity.addGlobalError("global.error");
    savedEntity.addError(fieldDefinition, "field.error");
    given(translationService.translate(eq("global.error"), any(Locale.class))).willReturn("translated global error");
    given(translationService.translate(eq("field.error"), any(Locale.class))).willReturn("translated field error");
    given(dataDefinition.create(null)).willReturn(new DefaultEntity(dataDefinition));
    given(dataDefinition.save(eq(entity))).willReturn(savedEntity);
    name.setFieldValue("text");
    form.setFieldValue(null);
    // when
    form.performEvent(viewDefinitionState, "save", new String[0]);
    // then
    verify(dataDefinition).save(eq(entity));
    assertFalse(form.isValid());
    assertTrue(form.render().toString().contains("translated global error"));
}
Also used : Locale(java.util.Locale) Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) JSONObject(org.json.JSONObject) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Example 13 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class GridComponentStateTest method shouldGetValueUsingExpression.

@Test
public void shouldGetValueUsingExpression() throws Exception {
    // given
    FieldDefinition nameFieldDefinition = mock(FieldDefinition.class);
    given(productDataDefinition.getField("name")).willReturn(nameFieldDefinition);
    FieldType nameFieldType = mock(FieldType.class);
    given(nameFieldDefinition.getType()).willReturn(nameFieldType);
    given(nameFieldType.toString(anyString(), any(Locale.class))).willAnswer(invocation -> Objects.toString(invocation.getArguments()[0]));
    GridComponentColumn column = new GridComponentColumn("name");
    column.setExpression("#name + ' ' + #id");
    Entity entity = new DefaultEntity(productDataDefinition, 13L, ImmutableMap.of("name", (Object) "John"));
    // when
    String value = column.getValue(entity, Locale.ENGLISH);
    // then
    assertEquals("John 13", value);
}
Also used : Locale(java.util.Locale) Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) GridComponentColumn(com.qcadoo.view.internal.components.grid.GridComponentColumn) JSONObject(org.json.JSONObject) Matchers.anyString(org.mockito.Matchers.anyString) FieldType(com.qcadoo.model.api.types.FieldType) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Example 14 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class GridComponentStateTest method shouldGetValueUsingFields.

@Test
public void shouldGetValueUsingFields() throws Exception {
    // given
    FieldDefinition field1 = mock(FieldDefinition.class);
    given(field1.getName()).willReturn("name");
    given(field1.getValue("John", Locale.ENGLISH)).willReturn("Johny");
    FieldDefinition field2 = mock(FieldDefinition.class);
    given(field2.getName()).willReturn("lastname");
    given(field2.getValue("Smith", Locale.ENGLISH)).willReturn("Smithy");
    GridComponentColumn column = new GridComponentColumn("name");
    column.addField(field1);
    column.addField(field2);
    Entity entity = new DefaultEntity(productDataDefinition, 13L, ImmutableMap.of("name", (Object) "John", "lastname", (Object) "Smith"));
    // when
    String value = column.getValue(entity, Locale.ENGLISH);
    // then
    assertEquals("Johny, Smithy", value);
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) GridComponentColumn(com.qcadoo.view.internal.components.grid.GridComponentColumn) JSONObject(org.json.JSONObject) Matchers.anyString(org.mockito.Matchers.anyString) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Example 15 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class FieldTypeFactoryTest method shouldReturnBelongToType.

@Test
public void shouldReturnBelongToType() throws Exception {
    // when
    FieldType fieldType = new BelongsToEntityType("parent", "entity", dataDefinitionService, false, true);
    // then
    assertEquals(Object.class, fieldType.getType());
    assertTrue(fieldType.toObject(fieldDefinition, new DefaultEntity(dataDefinition)).isValid());
}
Also used : DefaultEntity(com.qcadoo.model.internal.DefaultEntity) BelongsToEntityType(com.qcadoo.model.internal.types.BelongsToEntityType) FieldType(com.qcadoo.model.api.types.FieldType) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Aggregations

DefaultEntity (com.qcadoo.model.internal.DefaultEntity)92 Test (org.junit.Test)91 Entity (com.qcadoo.model.api.Entity)90 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)66 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)53 EntityHookDefinitionImpl (com.qcadoo.model.internal.hooks.EntityHookDefinitionImpl)16 AbstractStateTest (com.qcadoo.view.internal.states.AbstractStateTest)12 DataDefinition (com.qcadoo.model.api.DataDefinition)10 RangeValidator (com.qcadoo.model.internal.validators.RangeValidator)9 CustomEntityService (com.qcadoo.model.beans.sample.CustomEntityService)8 LengthValidator (com.qcadoo.model.internal.validators.LengthValidator)8 UnscaledValueValidator (com.qcadoo.model.internal.validators.UnscaledValueValidator)8 JSONObject (org.json.JSONObject)8 BigDecimal (java.math.BigDecimal)7 FieldDefinition (com.qcadoo.model.api.FieldDefinition)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ScaleValidator (com.qcadoo.model.internal.validators.ScaleValidator)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 FieldHookDefinitionImpl (com.qcadoo.model.internal.hooks.FieldHookDefinitionImpl)4 CustomEntityValidator (com.qcadoo.model.internal.validators.CustomEntityValidator)4