CategoriesService extends ConfigurationService<Categories> { public void create(Category category) { validateNotNull(category, "Must provide a category."); validateRequired(category.getName(), "Category name"); String name = category.getName().trim(); Categories categories = retrieve(); validate(categories.getCategoryByName(name) == null, format("Category with name \"{0}\" cannot be created since a category with the same name already exists.", name)); Categories newCategories = new Categories( ImmutableList.<Category> builder().addAll(categories.getCategories()).add(new Category(name)).build()); update(newCategories); } @Inject  CategoriesService(CategoriesPersistenceService persistenceService, EntityEventSupport eventSupport); void create(Category category); void deleteByName(String name); void addRuleByName(String name, MatchRule matchRule);  }
@Test public void createNull() { thrown.expect(ValidationException.class); thrown.expectMessage("Must provide a category."); categoriesService.create(null); }
@Test public void createNoName() { thrown.expect(ValidationException.class); thrown.expectMessage("Category name must be specified."); categoriesService.create(new Category("")); }
@Test public void createDuplicateName() { thrown.expect(ValidationException.class); thrown.expectMessage( "Category with name \"a\" cannot be created since a category with the same name already exists."); categoriesService.create(new Category("a ")); }
@Test public void create() { categoriesService.create(new Category(" B")); assertNotNull(savedCategories); assertEquals(2, savedCategories.getCategories().size()); assertNotNull(savedCategories.getCategoryByName("B")); assertNotNull(savedCategories.getCategoryByName("A")); }