WidgetRepository extends AbstractRepository<Widget> { public Widget create(Widget widget) throws IllegalArgumentException { String id = "custom" + trimToEmpty(capitalize(widget.getName())); try { Widget existingWidget = get(id); throw new NotAllowedException("Widget with name " + existingWidget.getName() + " already exists"); } catch (NotFoundException e) { widget.setCustom(true); widget.setId(id); createComponentDirectory(widget); updateLastUpdateAndSave(widget); return widget; } } @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(expected = ConstraintValidationException.class) public void should_not_allow_to_save_a_widget_with_name_containing_non_alphanumeric_chars() throws Exception { Widget widget = aWidget().name("héllo").build(); widgetRepository.create(widget); }
@Test(expected = ConstraintValidationException.class) public void should_not_allow_to_save_as_widget_with_an_invalid_property() throws Exception { Widget widget = aWidget().property(aProperty().name("ze invalid name")).custom().build(); widgetRepository.create(widget); }
@Test(expected = ConstraintValidationException.class) public void should_allow_to_save_a_custom_widget_with_name_containing_space() throws Exception { Widget widget = aWidget().name("hello world").custom().build(); widgetRepository.create(widget); }
@Test(expected = ConstraintValidationException.class) public void should_not_allow_to_create_a_widget_without_name() throws Exception { Widget widget = aWidget().name(" ").build(); widgetRepository.create(widget); }
@Test(expected = ConstraintValidationException.class) public void should_not_allow_to_create_a_widget_with_name_containing_non_alphanumeric_chars() throws Exception { Widget widget = aWidget().name("héllo").build(); widgetRepository.create(widget); }
@Test(expected = ConstraintValidationException.class) public void should_not_allow_to_create_a_custom_widget_with_name_containing_non_space() throws Exception { Widget widget = aWidget().name("hello world").custom().build(); widgetRepository.create(widget); }
@Test(expected = ConstraintValidationException.class) public void should_allow_create_a_widget_with_name_containing_space_for_normal_widget() throws Exception { Widget widget = aWidget().name("hello world").build(); widgetRepository.create(widget); }
@Test public void should_create_a_widget_and_set_his_id() throws Exception { Widget expectedWidget = aWidget().name("aName").build(); Widget createdWidget = widgetRepository.create(expectedWidget); expectedWidget.setId("customAName"); expectedWidget.setCustom(true); assertThat(expectedWidget).isEqualTo(createdWidget); assertThat(jsonFile(createdWidget)).exists(); }
@Test(expected = NotAllowedException.class) public void should_not_allow_to_create_a_widget_with_an_already_existing_name() throws Exception { Widget widget = aWidget().name("existingName").id("customExistingName").build(); addToRepository(widget); widgetRepository.create(aWidget().name("existingName").build()); }