Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-4372; add unique constraing for factory name + user #4521

Merged
merged 8 commits into from
Mar 23, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -82,6 +87,9 @@ public Factory saveFactory(Factory factory, Set<FactoryImage> 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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<FactoryImpl> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
--------------------------------------------------------------------------------


Expand Down