EntityServiceImpl implements EntityService { @Override @Transactional public DraftResult saveDraftEntityChanges(Long entityId, DraftData draftData, String username) { EntityDraft draft = getEntityDraft(entityId, username); if (draftData.isCreate()) { createFieldForDraft(draft, draftData); } else if (draftData.isEdit()) { draftEdit(draft, draftData); } else if (draftData.isRemove()) { draftRemove(draft, draftData); } return new DraftResult(draft.isChangesMade(), draft.isOutdated()); }  @Override @Transactional Long getCurrentSchemaVersion(String className); @Override @Transactional void incrementVersion(Long entityId); @Override @Transactional EntityDto createEntity(EntityDto entityDto); @Override @Transactional DraftResult saveDraftEntityChanges(Long entityId, DraftData draftData, String username); @Override @Transactional DraftResult saveDraftEntityChanges(Long entityId, DraftData draftData); @Override @Transactional void abandonChanges(Long entityId); @Override @Transactional List<String> commitChanges(Long entityId, String changesOwner); @Override @Transactional List<String> commitChanges(Long entityId); @Override @Transactional List<EntityDto> listWorkInProgress(); @Override @Transactional AdvancedSettingsDto getAdvancedSettings(Long entityId); @Override @Transactional AdvancedSettingsDto getAdvancedSettings(Long entityId, boolean committed); @Override @Transactional AdvancedSettingsDto safeGetAdvancedSettingsCommitted(String entityClassName); @Override @Transactional void updateRestOptions(Long entityId, RestOptionsDto restOptionsDto); @Override @Transactional void updateTracking(Long entityId, TrackingDto trackingDto); @Override @Transactional void addLookups(EntityDto entityDto, LookupDto... lookups); @Override @Transactional void addLookups(EntityDto entityDto, Collection<LookupDto> lookups); @Override @Transactional void addLookups(Long entityId, LookupDto... lookups); @Override @Transactional void addLookups(Long entityId, Collection<LookupDto> lookups); @Override @Transactional Map<String, FieldDto> getLookupFieldsMapping(Long entityId, String lookupName); @Override @Transactional void deleteEntity(Long entityId); @Override @Transactional List<EntityDto> listEntities(); @Override @Transactional List<EntityDto> listEntities(boolean withSecurityCheck); @Override @Transactional List<EntityDto> listEntitiesByBundle(String bundleSymbolicName); @Override @Transactional EntityDto getEntity(Long entityId); @Override @Transactional EntityDto getEntityByClassName(String className); @Override @Transactional List<EntityDto> findEntitiesByPackage(String packageName); @Override @Transactional List<LookupDto> getEntityLookups(Long entityId); @Override @Transactional List<FieldDto> getFields(Long entityId); @Override @Transactional List<FieldDto> getEntityFields(Long entityId); @Override @Transactional List<FieldDto> getEntityFieldsForUI(Long entityId); @Override @Transactional SchemaHolder getSchema(); @Override @Transactional List<FieldDto> getEntityFieldsByClassName(String className); @Override @Transactional List<FieldDto> getEntityFieldsByClassNameForUI(String className); @Override @Transactional FieldDto findFieldByName(Long entityId, String name); @Override @Transactional FieldDto findEntityFieldByName(Long entityId, String name); @Override @Transactional FieldDto getEntityFieldById(Long entityId, Long fieldId); @Override @Transactional EntityDto getEntityForEdit(Long entityId); @Override @Transactional EntityDraft getEntityDraft(Long entityId); @Override @Transactional EntityDraft getEntityDraft(Long entityId, String username); @Override @Transactional void addFields(EntityDto entity, Collection<FieldDto> fields); @Override @Transactional void addFields(Long entityId, FieldDto... fields); @Override @Transactional void addFields(EntityDto entity, FieldDto... fields); @Override @Transactional void addFields(Long entityId, Collection<FieldDto> fields); @Override @Transactional void addFilterableFields(EntityDto entityDto, Collection<String> fieldNames); @Override @Transactional EntityDto updateDraft(Long entityId); @Override @Transactional LookupDto getLookupByName(Long entityId, String lookupName); @Override @Transactional List<FieldDto> getDisplayFields(Long entityId); @Override @Transactional void addNonEditableFields(EntityDto entityDto, Map<String, Boolean> nonEditableFields); @Override @Transactional void addDisplayedFields(EntityDto entityDto, Map<String, Long> positions); @Override @Transactional void updateSecurityOptions(Long entityId, SecurityMode securityMode, Set<String> securityMembers, SecurityMode readOnlySecurityMode, Set<String> readOnlySecurityMembers); @Override @Transactional void updateMaxFetchDepth(Long entityId, Integer maxFetchDepth); @Autowired void setAllEntities(AllEntities allEntities); @Autowired void setAllTypes(AllTypes allTypes); @Autowired void setAllEntityDrafts(AllEntityDrafts allEntityDrafts); @Autowired void setAllEntityAudits(AllEntityAudits allEntityAudits); @Autowired void setAllUserPreferences(AllUserPreferences allUserPreferences); @Autowired void setMDSConstructor(MDSConstructor mdsConstructor); @Autowired void setBundleContext(BundleContext bundleContext); @Autowired void setEntityValidator(EntityValidator entityValidator); @Autowired void setComboboxDataMigrationHelper(ComboboxDataMigrationHelper comboboxDataMigrationHelper); @Autowired void setUserPreferencesService(UserPreferencesService userPreferencesService);  }
@Test public void shouldMarkUniqueChangesInDraft() { DraftData dd = new DraftData(); dd.setEdit(true); dd.getValues().put(DraftData.VALUE, singletonList(true)); dd.getValues().put(DraftData.PATH, "basic.unique"); dd.getValues().put(DraftData.FIELD_ID, 2L); when(allEntities.retrieveById(1L)).thenReturn(entity); when(allEntityDrafts.retrieve(entity, "motech")).thenReturn(draft); when(draft.getField(2L)).thenReturn(field); when(draft.getParentEntity()).thenReturn(entity); when(entity.getField("fieldName")).thenReturn(field); when(field.getName()).thenReturn("fieldName"); when(field.toDto()).thenReturn(fieldDto); when(fieldDto.getBasic()).thenReturn(basic); when(field.isUnique()).thenReturn(true); entityService.saveDraftEntityChanges(1L, dd); verify(field).update(any(FieldDto.class)); verify(draft, never()).addUniqueToRemove(anyString()); dd.getValues().put(DraftData.VALUE, singletonList(false)); entityService.saveDraftEntityChanges(1L, dd); verify(draft).addUniqueToRemove("fieldName"); }