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());
}
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"));
}
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);
}
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);
}
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());
}
Aggregations