diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryManager.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryManager.java index 06e7b307146..d35c562ea30 100644 --- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryManager.java +++ b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryManager.java @@ -30,6 +30,7 @@ import java.util.stream.Collectors; import static com.google.common.base.MoreObjects.firstNonNull; +import static com.google.common.base.Strings.isNullOrEmpty; import static java.util.Objects.requireNonNull; import static org.eclipse.che.api.factory.shared.Constants.HTML_SNIPPET_TYPE; import static org.eclipse.che.api.factory.shared.Constants.IFRAME_SNIPPET_TYPE; @@ -42,8 +43,12 @@ @Singleton public class FactoryManager { + private final FactoryDao factoryDao; + @Inject - private FactoryDao factoryDao; + public FactoryManager(FactoryDao factoryDao) { + this.factoryDao = factoryDao; + } /** * Stores {@link Factory} instance. @@ -82,6 +87,9 @@ public Factory saveFactory(Factory factory, Set images) throws Con requireNonNull(factory); final FactoryImpl newFactory = new FactoryImpl(factory, images); newFactory.setId(NameGenerator.generate("factory", 16)); + if (isNullOrEmpty(newFactory.getName())) { + newFactory.setName(NameGenerator.generate("f", 9)); + } return factoryDao.create(newFactory); } diff --git a/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/FactoryManagerTest.java b/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/FactoryManagerTest.java new file mode 100644 index 00000000000..ec4ba6aa695 --- /dev/null +++ b/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/FactoryManagerTest.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2012-2017 Codenvy, S.A. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Codenvy, S.A. - initial API and implementation + *******************************************************************************/ +package org.eclipse.che.api.factory.server; + +import org.eclipse.che.api.factory.server.model.impl.FactoryImpl; +import org.eclipse.che.api.factory.server.spi.FactoryDao; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.testng.MockitoTestNGListener; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +import static com.google.common.base.Strings.isNullOrEmpty; +import static org.mockito.Mockito.verify; +import static org.testng.Assert.assertFalse; + +/** + * @author Max Shaposhnik (mshaposhnik@codenvy.com) on 3/20/17. + */ +@Listeners(value = {MockitoTestNGListener.class}) +public class FactoryManagerTest { + + @Mock + private FactoryDao factoryDao; + + @InjectMocks + private FactoryManager factoryManager; + + @Captor + private ArgumentCaptor factoryCaptor; + + @Test + public void shouldGenerateNameOnFactoryCreation() throws Exception { + final FactoryImpl factory = FactoryImpl.builder().generateId().build(); + factoryManager.saveFactory(factory); + verify(factoryDao).create(factoryCaptor.capture()); + assertFalse(isNullOrEmpty(factoryCaptor.getValue().getName())); + } +} diff --git a/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/spi/tck/FactoryDaoTest.java b/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/spi/tck/FactoryDaoTest.java index dd9d1d2ad2f..5dd1beb6283 100644 --- a/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/spi/tck/FactoryDaoTest.java +++ b/wsmaster/che-core-api-factory/src/test/java/org/eclipse/che/api/factory/server/spi/tck/FactoryDaoTest.java @@ -131,15 +131,14 @@ public void shouldThrowConflictExceptionWhenCreatingFactoryWithExistingId() thro factoryDao.create(factory); } - // TODO fix after issue: https://github.com/eclipse/che/issues/2110 -// @Test(expectedExceptions = ConflictException.class) -// public void shouldThrowConflictExceptionWhenCreatingFactoryWithExistingNameAndUserId() throws Exception { -// final FactoryImpl factory = createFactory(10, users[0].getId()); -// final FactoryImpl existing = factories[0]; -// factory.getCreator().setUserId(existing.getCreator().getUserId()); -// factory.setName(existing.getName()); -// factoryDao.create(factory); -// } + @Test(expectedExceptions = ConflictException.class) + public void shouldThrowConflictExceptionWhenCreatingFactoryWithExistingNameAndUserId() throws Exception { + final FactoryImpl factory = createFactory(10, users[0].getId()); + final FactoryImpl existing = factories[0]; + factory.getCreator().setUserId(existing.getCreator().getUserId()); + factory.setName(existing.getName()); + factoryDao.create(factory); + } @Test public void shouldUpdateFactory() throws Exception { @@ -160,14 +159,13 @@ public void shouldUpdateFactory() throws Exception { assertEquals(factoryDao.getById(update.getId()), update); } -// TODO fix after issue: https://github.com/eclipse/che/issues/2110 -// @Test(expectedExceptions = ConflictException.class) -// public void shouldThrowConflictExceptionWhenUpdateFactoryWithExistingNameAndUserId() throws Exception { -// final FactoryImpl update = factories[0]; -// update.setName(factories[1].getName()); -// update.getCreator().setUserId(factories[1].getCreator().getUserId()); -// factoryDao.update(update); -// } + @Test(expectedExceptions = ConflictException.class) + public void shouldThrowConflictExceptionWhenUpdateFactoryWithExistingNameAndUserId() throws Exception { + final FactoryImpl update = factories[0]; + update.setName(factories[1].getName()); + update.getCreator().setUserId(factories[1].getCreator().getUserId()); + factoryDao.update(update); + } @Test(expectedExceptions = NullPointerException.class) public void shouldThrowNpeWhenFactoryUpdateIsNull() throws Exception { diff --git a/wsmaster/che-core-sql-schema/src/main/resources/che-schema/5.6.0/1__add_factory.sql b/wsmaster/che-core-sql-schema/src/main/resources/che-schema/5.6.0/1__add_factory.sql index 821562cd2d6..1dd19a523d7 100644 --- a/wsmaster/che-core-sql-schema/src/main/resources/che-schema/5.6.0/1__add_factory.sql +++ b/wsmaster/che-core-sql-schema/src/main/resources/che-schema/5.6.0/1__add_factory.sql @@ -150,6 +150,7 @@ ALTER TABLE che_factory ADD CONSTRAINT fk_che_f_user_id FOREIGN KEY (user_id) RE ALTER TABLE che_factory ADD CONSTRAINT fk_che_f_workspace_id FOREIGN KEY (workspace_id) REFERENCES workspaceconfig (id); ALTER TABLE che_factory ADD CONSTRAINT fk_che_f_button_id FOREIGN KEY (button_id) REFERENCES che_factory_button (id); ALTER TABLE che_factory ADD CONSTRAINT fk_che_f_ide_id FOREIGN KEY (ide_id) REFERENCES che_factory_ide (id); +CREATE UNIQUE INDEX index_che_factory_name_user_id ON che_factory (user_id, name); --------------------------------------------------------------------------------