WidgetRepository extends AbstractRepository<Widget> { public List<Property> addProperty(String widgetId, Property property) { Widget widget = get(widgetId); Property existingProperty = widget.getProperty(property.getName()); if (existingProperty != null) { throw new NotAllowedException(format("Widget [ %s ] has already a property named %s", widgetId, property.getName())); } widget.addProperty(property); updateLastUpdateAndSave(widget); return widget.getProperties(); } @Inject  WidgetRepository(
            @Named("widgetPath") Path path,
            @Named("widgetFileBasedPersister") JsonFileBasedPersister<Widget> fileBasedRepository,
            @Named("widgetFileBasedLoader") JsonFileBasedLoader<Widget> widgetLoader,
            BeanValidator validator,
            Watcher watcher); @Override String getComponentName(); @Override void delete(String widgetId); Widget create(Widget widget); List<Widget> getByIds(Set<String> widgetIds); List<Property> addProperty(String widgetId, Property property); List<Property> updateProperty(String widgetId, String propertyName, Property property); List<Property> deleteProperty(String widgetId, String propertyName);  }
@Test public void should_save_a_new_property() throws Exception { Widget aWidget = addToRepository(aWidget().custom().build()); Property expectedProperty = aProperty().build(); widgetRepository.addProperty(aWidget.getId(), expectedProperty); Widget widget = getFromRepository(aWidget.getId()); assertThat(widget.getProperties()).contains(expectedProperty); }
@Test(expected = NotAllowedException.class) public void should_not_allow_to_save_a_new_property_when_property_with_same_name_already_exists() throws Exception { Property alreadyAddedProperty = aProperty().build(); Widget aWidget = addToRepository(aWidget().custom().property(alreadyAddedProperty).build()); widgetRepository.addProperty(aWidget.getId(), alreadyAddedProperty); }