use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.
the class HookTest method shouldSaveHookCanOverrideValueOfReadOnlyField.
@Test
public void shouldSaveHookCanOverrideValueOfReadOnlyField() throws Exception {
// given
Entity entity = new DefaultEntity(dataDefinition, 1L);
entity.setField("name", null);
entity.setField("age", null);
entity.setField("readOnly", "youCanNotSeeMe!");
SampleSimpleDatabaseObject databaseObject = new SampleSimpleDatabaseObject(1L);
given(session.get(any(Class.class), Matchers.anyInt())).willReturn(databaseObject);
dataDefinition.addSaveHook(new EntityHookDefinitionImpl(CustomEntityService.class.getName(), "overrideReadOnlyField", PLUGIN_IDENTIFIER, applicationContext));
// when
entity = dataDefinition.save(entity);
// then
assertEquals("overrided", entity.getField("readOnly"));
}
use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.
the class HookTest method shouldBeTriggeredInOrderOfAdding.
@Test
public final void shouldBeTriggeredInOrderOfAdding() {
// given
Entity entity = new DefaultEntity(dataDefinition);
entity.setField("name", "a");
dataDefinition.addSaveHook(buildHook("appendB"));
dataDefinition.addSaveHook(buildHook("appendC"));
dataDefinition.addSaveHook(buildHook("appendD"));
// when
entity = dataDefinition.save(entity);
// then
assertEquals("abcd", entity.getStringField("name"));
}
use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.
the class HookTest method shouldCallAllDefinedHooksWhileCreating.
@Test
public void shouldCallAllDefinedHooksWhileCreating() throws Exception {
// given
Entity entity = new DefaultEntity(dataDefinition);
entity.setField("name", null);
entity.setField("age", null);
dataDefinition.addCreateHook(new EntityHookDefinitionImpl(CustomEntityService.class.getName(), "onCreate", PLUGIN_IDENTIFIER, applicationContext));
dataDefinition.addSaveHook(new EntityHookDefinitionImpl(CustomEntityService.class.getName(), "onSave", PLUGIN_IDENTIFIER, applicationContext));
// when
entity = dataDefinition.save(entity);
// then
assertEquals("create", entity.getField("name"));
assertEquals(Integer.valueOf(11), entity.getField("age"));
}
use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.
the class FormComponentStateTest method shouldReturnEntityWithFormValuesIfEntityDoesNotHaveId.
@Test
public void shouldReturnEntityWithFormValuesIfEntityDoesNotHaveId() throws Exception {
// given
String nameFieldName = "name";
String nameFieldFormValue = "new name";
String numberFieldName = "number";
Entity formEntity = new DefaultEntity(dataDefinition, null, Maps.<String, Object>newHashMap());
formEntity.setField(nameFieldName, nameFieldFormValue);
given(dataDefinition.get(anyLong())).willReturn(null);
form.setEntity(formEntity);
// when
Entity resultEntity = form.getPersistedEntityWithIncludedFormValues();
// then
verify(dataDefinition, never()).get(anyLong());
verify(dataDefinition, never()).get(null);
assertNull(resultEntity.getId());
assertEquals(nameFieldFormValue, resultEntity.getStringField(nameFieldName));
assertNull(resultEntity.getStringField(numberFieldName));
assertEquals(1, resultEntity.getFields().size());
}
use of com.qcadoo.model.internal.DefaultEntity 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();
}
Aggregations