### Question:
BaseRestService implements RestService<T> { @NonNull public T createItem(@NonNull final T input) { commonCreateItemValidations(input); final T slugifiedInput = this.createSlug(input); return this.repository.save(slugifiedInput); } protected  BaseRestService(@NonNull final BaseSlugRepository<T> repository); @NonNull T createItem(@NonNull final T input); @NonNull Collection<T> getItems(); @NonNull T getItem(@NonNull final String slug); @NonNull T getItem(@NonNull final Long id); @NonNull T updateItem(@NonNull final T input); void deleteItem(String slug); void deleteItem(Long id); static final String GENERIC_PREFIX; }### Answer:
@Test(expected = ValidationException.class) public void forbidsCreatingEntityWithId() throws Exception { MockEntity mockEntity = MockEntity.builder().id(1L).build(); when(mockRepository.save(mockEntity)).thenReturn(mockEntity); assertSame(this.put.createItem(mockEntity), mockEntity); }
@Test(expected = ValidationException.class) public void forbidsCreatingEntityWithSlug() throws Exception { MockEntity mockEntity = MockEntity.builder().slug(TEST_SLUG).build(); when(mockRepository.save(mockEntity)).thenReturn(mockEntity); assertSame(mockEntity, this.put.createItem(mockEntity)); }
@Test public void fillsSlugWhenCreating() { MockEntity mockEntity = MockEntity.builder().label(TEST_LABEL).build(); when(mockRepository.save(mockEntity)).thenReturn(mockEntity); final MockEntity savedEntity = this.put.createItem(mockEntity); assertEquals(TEST_LABEL, savedEntity.getLabel()); assertEquals(TEST_SLUG, savedEntity.getSlug()); }
@Test public void createSpecialSlugWhenLabelIsANumber() { final String LABEL = "123"; final String SLUG = "item-123"; MockEntity mockEntity = MockEntity.builder().label(LABEL).build(); when(mockRepository.save(mockEntity)).thenReturn(mockEntity); final MockEntity savedEntity = this.put.createItem(mockEntity); assertEquals(SLUG, savedEntity.getSlug()); }
@Test public void handlesCollidingSlugsWhenCreating() { final MockEntity mockEntity = MockEntity.builder() .label(TEST_LABEL) .build(); when(mockRepository.save(mockEntity)).thenReturn(mockEntity); when(mockRepository.findOneBySlug(TEST_SLUG)).thenReturn( MockEntity.builder() .label(TEST_LABEL) .slug(TEST_SLUG) .build()); final MockEntity savedEntity = this.put.createItem(mockEntity); assertEquals(TEST_LABEL, savedEntity.getLabel()); assertEquals(TEST_SLUG + "-1", savedEntity.getSlug()); }
@Test public void handlesSecondSlugCollisionWhenCreating() { final MockEntity mockEntity = MockEntity.builder() .label(TEST_LABEL) .build(); when(mockRepository.findOneBySlug(TEST_SLUG)).thenReturn( MockEntity.builder() .label(TEST_LABEL) .slug(TEST_SLUG) .build()); final MockEntity firstConflictedEntity = MockEntity.builder() .id(2L) .label(TEST_LABEL) .slug(TEST_SLUG + "-1") .build(); when(mockRepository.findOneBySlug(firstConflictedEntity.getSlug())).thenReturn( firstConflictedEntity); when(mockRepository.save(mockEntity)).thenReturn(mockEntity); final MockEntity savedEntity = this.put.createItem(mockEntity); assertEquals(TEST_LABEL, savedEntity.getLabel()); assertEquals(TEST_SLUG + "-2", savedEntity.getSlug()); }