### Question:
EntityServiceImpl implements EntityService { @Override public void setField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Object value) { if (!shouldAllowToSetField(fieldDefinition, value)) { return; } if (fieldDefinition.getType() instanceof BelongsToType && value != null) { Object belongsToValue = getBelongsToFieldValue( (InternalDataDefinition) ((BelongsToType) fieldDefinition.getType()).getDataDefinition(), value); setField(databaseEntity, fieldDefinition.getName(), belongsToValue); } else if (fieldDefinition.getType() instanceof HasManyType) { setField(databaseEntity, fieldDefinition.getName(), null); } else if (fieldDefinition.getType() instanceof TreeType) { setField(databaseEntity, fieldDefinition.getName(), null); } else { setField(databaseEntity, fieldDefinition.getName(), value); } }  @Override Long getId(final Object databaseEntity); Boolean getActive(final Object databaseEntity); void setActive(final Object databaseEntity, final boolean active); @Override void setId(final Object databaseEntity, final Long id); @Override void setField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Object value); @Override Object getField(final Object databaseEntity, final FieldDefinition fieldDefinition); Object getField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Entity performer); @Override Entity convertToGenericEntity(final InternalDataDefinition dataDefinition, final Object databaseEntity); Entity convertToGenericEntity(final InternalDataDefinition dataDefinition, final Object databaseEntity,
            final Entity performer); @SuppressWarnings("unchecked") @Override Object convertToDatabaseEntity(final InternalDataDefinition dataDefinition, final Entity genericEntity,
            final Object existingDatabaseEntity);  }### Answer:
@Test(expected = IllegalStateException.class) public void shouldThrownAnExceptionWhileSettingNotExistingField() throws Exception { SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(1L); FieldDefinition fieldDefinition = new FieldDefinitionImpl(new DataDefinitionImpl("", "", null), "unknown").withType(new StringType()); entityService.setField(databaseEntity, fieldDefinition, "XXX"); }
@Test public void shouldSetEmptyField() throws Exception { SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(1L); databaseEntity.setName("name"); entityService.setField(databaseEntity, fieldDefinitionName, null); assertNull(databaseEntity.getName()); }
@Test public void shouldSetNotEmptyField() throws Exception { SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(1L); databaseEntity.setName("name"); entityService.setField(databaseEntity, fieldDefinitionName, "XXX"); assertEquals("XXX", databaseEntity.getName()); }
@Test public void shouldSetBelongsToField() throws Exception { Entity parentEntity = new DefaultEntity(dataDefinition, 1L); parentEntity.setField("name", "Mr X"); SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L); parentDatabaseEntity.setName("Mr X"); SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(2L); given(session.load(SampleParentDatabaseObject.class, 1L)).willReturn(parentDatabaseEntity); entityService.setField(databaseEntity, fieldDefinitionBelongsTo, parentEntity); assertNotNull(databaseEntity.getBelongsTo()); assertEquals(parentDatabaseEntity, databaseEntity.getBelongsTo()); }
@Test public void shouldSetNullIfBelongsToFieldIsEmpty() throws Exception { SampleParentDatabaseObject parentDatabaseEntity = new SampleParentDatabaseObject(1L); parentDatabaseEntity.setName("Mr X"); SampleSimpleDatabaseObject databaseEntity = new SampleSimpleDatabaseObject(2L); databaseEntity.setBelongsTo(parentDatabaseEntity); entityService.setField(databaseEntity, fieldDefinitionBelongsTo, null); assertNull(databaseEntity.getBelongsTo()); }