Create error response for already existing entities

This commit is contained in:
René Pfeuffer
2018-10-25 15:31:42 +02:00
parent d185743ef0
commit 6eb3c38655
58 changed files with 249 additions and 226 deletions

View File

@@ -20,6 +20,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.api.v2.NotFoundExceptionMapper;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.NamespaceAndName;

View File

@@ -19,6 +19,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.NotFoundException;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.api.v2.NotFoundExceptionMapper;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.DiffCommandBuilder;

View File

@@ -6,6 +6,7 @@ import sonia.scm.api.rest.AlreadyExistsExceptionMapper;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.api.rest.ConcurrentModificationExceptionMapper;
import sonia.scm.api.rest.IllegalArgumentExceptionMapper;
import sonia.scm.api.v2.NotFoundExceptionMapper;
public class DispatcherMock {
public static Dispatcher createDispatcher(Object resource) {

View File

@@ -20,6 +20,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.NotFoundException;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.api.v2.NotFoundExceptionMapper;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.InternalRepositoryException;

View File

@@ -19,6 +19,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.api.v2.NotFoundExceptionMapper;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Modifications;
import sonia.scm.repository.NamespaceAndName;

View File

@@ -164,10 +164,7 @@ public class PermissionRootResourceTest extends RepositoryTestBase {
@TestFactory
@DisplayName("test endpoints on missing permissions and user is Admin")
Stream<DynamicTest> missedPermissionTestFactory() {
Repository mockRepository = mock(Repository.class);
when(mockRepository.getId()).thenReturn(REPOSITORY_NAME);
when(mockRepository.getNamespace()).thenReturn(REPOSITORY_NAMESPACE);
when(mockRepository.getName()).thenReturn(REPOSITORY_NAME);
Repository mockRepository = new Repository(REPOSITORY_NAME, "git", REPOSITORY_NAMESPACE, REPOSITORY_NAME);
when(repositoryManager.get(any(NamespaceAndName.class))).thenReturn(mockRepository);
return createDynamicTestsToAssertResponses(
requestGETPermission.expectedResponseStatus(404),
@@ -180,10 +177,6 @@ public class PermissionRootResourceTest extends RepositoryTestBase {
@TestFactory
@DisplayName("test endpoints on missing permissions and user is not Admin")
Stream<DynamicTest> missedPermissionUserForbiddenTestFactory() {
Repository mockRepository = mock(Repository.class);
when(mockRepository.getId()).thenReturn(REPOSITORY_NAME);
when(mockRepository.getNamespace()).thenReturn(REPOSITORY_NAMESPACE);
when(mockRepository.getName()).thenReturn(REPOSITORY_NAME);
doThrow(AuthorizationException.class).when(repositoryManager).get(any(NamespaceAndName.class));
return createDynamicTestsToAssertResponses(
requestGETPermission.expectedResponseStatus(403),
@@ -413,6 +406,7 @@ public class PermissionRootResourceTest extends RepositoryTestBase {
when(mockRepository.getId()).thenReturn(REPOSITORY_NAME);
when(mockRepository.getNamespace()).thenReturn(REPOSITORY_NAMESPACE);
when(mockRepository.getName()).thenReturn(REPOSITORY_NAME);
when(mockRepository.getNamespaceAndName()).thenReturn(new NamespaceAndName(REPOSITORY_NAMESPACE, REPOSITORY_NAME));
when(repositoryManager.get(any(NamespaceAndName.class))).thenReturn(mockRepository);
when(subject.isPermitted(userPermission != null ? eq(userPermission) : any(String.class))).thenReturn(true);
return mockRepository;

View File

@@ -18,6 +18,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.api.v2.NotFoundExceptionMapper;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.Tag;

View File

@@ -110,7 +110,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
private String mockedNamespace = "default_namespace";
@Test
public void testCreate() throws AlreadyExistsException {
public void testCreate() {
Repository heartOfGold = createTestRepository();
Repository dbRepo = manager.get(heartOfGold.getId());
@@ -122,18 +122,18 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
username = "unpriv"
)
@Test(expected = UnauthorizedException.class)
public void testCreateWithoutPrivileges() throws AlreadyExistsException {
public void testCreateWithoutPrivileges() {
createTestRepository();
}
@Test(expected = AlreadyExistsException.class)
public void testCreateExisting() throws AlreadyExistsException {
public void testCreateExisting() {
createTestRepository();
createTestRepository();
}
@Test
public void testDelete() throws Exception {
public void testDelete() {
delete(manager, createTestRepository());
}
@@ -141,12 +141,12 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
username = "unpriv"
)
@Test(expected = UnauthorizedException.class)
public void testDeleteWithoutPrivileges() throws Exception {
public void testDeleteWithoutPrivileges() {
delete(manager, createTestRepository());
}
@Test(expected = RepositoryIsNotArchivedException.class)
public void testDeleteNonArchived() throws Exception {
public void testDeleteNonArchived() {
configuration.setEnableRepositoryArchive(true);
delete(manager, createTestRepository());
}
@@ -157,7 +157,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void testDeleteWithEnabledArchive() throws Exception {
public void testDeleteWithEnabledArchive() {
Repository repository = createTestRepository();
repository.setArchived(true);
@@ -167,7 +167,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void testGet() throws AlreadyExistsException {
public void testGet() {
Repository heartOfGold = createTestRepository();
String id = heartOfGold.getId();
String description = heartOfGold.getDescription();
@@ -185,7 +185,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
@SubjectAware(
username = "crato"
)
public void testGetWithoutRequiredPrivileges() throws AlreadyExistsException {
public void testGetWithoutRequiredPrivileges() {
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
manager.create(heartOfGold);
@@ -194,7 +194,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void testGetAll() throws AlreadyExistsException {
public void testGetAll() {
Repository heartOfGold = createTestRepository();
Repository happyVerticalPeopleTransporter = createSecondTestRepository();
boolean foundHeart = false;
@@ -232,7 +232,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
@Test
@SuppressWarnings("unchecked")
@SubjectAware(username = "dent")
public void testGetAllWithPermissionsForTwoOrThreeRepos() throws AlreadyExistsException {
public void testGetAllWithPermissionsForTwoOrThreeRepos() {
// mock key generator
KeyGenerator keyGenerator = mock(KeyGenerator.class);
Stack<String> keys = new Stack<>();
@@ -273,7 +273,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void testEvents() throws Exception {
public void testEvents() {
RepositoryManager repoManager = createRepositoryManager(false);
repoManager.init(contextProvider);
TestListener listener = new TestListener();
@@ -304,7 +304,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void testModify() throws AlreadyExistsException {
public void testModify() {
Repository heartOfGold = createTestRepository();
heartOfGold.setDescription("prototype ship");
@@ -318,7 +318,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
@Test
@SubjectAware(username = "crato")
public void testModifyWithoutRequiredPermissions() throws AlreadyExistsException, NotFoundException {
public void testModifyWithoutRequiredPermissions() {
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
manager.create(heartOfGold);
heartOfGold.setDescription("prototype ship");
@@ -333,7 +333,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void testRefresh() throws AlreadyExistsException {
public void testRefresh() {
Repository heartOfGold = createTestRepository();
String description = heartOfGold.getDescription();
@@ -344,7 +344,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
@Test
@SubjectAware(username = "crato")
public void testRefreshWithoutRequiredPermissions() throws AlreadyExistsException, NotFoundException {
public void testRefreshWithoutRequiredPermissions() {
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
manager.create(heartOfGold);
heartOfGold.setDescription("prototype ship");
@@ -359,7 +359,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void testRepositoryHook() throws AlreadyExistsException {
public void testRepositoryHook() {
CountingReceiveHook hook = new CountingReceiveHook();
RepositoryManager repoManager = createRepositoryManager(false);
@@ -379,7 +379,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void testNamespaceSet() throws Exception {
public void testNamespaceSet() {
RepositoryManager repoManager = createRepositoryManager(false);
Repository repository = spy(createTestRepository());
repository.setName("Testrepo");
@@ -388,14 +388,14 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
}
@Test
public void shouldSetNamespace() throws AlreadyExistsException {
public void shouldSetNamespace() {
Repository repository = new Repository(null, "hg", null, "scm");
manager.create(repository);
assertNotNull(repository.getId());
assertNotNull(repository.getNamespace());
}
private void createUriTestRepositories(RepositoryManager m) throws AlreadyExistsException {
private void createUriTestRepositories(RepositoryManager m) {
mockedNamespace = "namespace";
createRepository(m, new Repository("1", "hg", "namespace", "scm"));
createRepository(m, new Repository("2", "hg", "namespace", "scm-test"));
@@ -448,7 +448,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
keyGenerator, repositoryDAO, handlerSet, namespaceStrategy);
}
private void createRepository(RepositoryManager m, Repository repository) throws AlreadyExistsException {
private void createRepository(RepositoryManager m, Repository repository) {
m.create(repository);
}
@@ -471,7 +471,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
assertEquals(repo.getLastModified(), other.getLastModified());
}
private Repository createRepository(Repository repository) throws AlreadyExistsException {
private Repository createRepository(Repository repository) {
manager.create(repository);
assertNotNull(repository.getId());
assertNotNull(manager.get(repository.getId()));
@@ -486,12 +486,12 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
return repository;
}
private Repository createSecondTestRepository() throws AlreadyExistsException {
private Repository createSecondTestRepository() {
return createRepository(
RepositoryTestData.createHappyVerticalPeopleTransporter());
}
private Repository createTestRepository() throws AlreadyExistsException {
private Repository createTestRepository() {
return createRepository(RepositoryTestData.createHeartOfGold());
}

View File

@@ -40,7 +40,6 @@ import com.github.sdorra.shiro.SubjectAware;
import com.google.common.collect.Lists;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -48,7 +47,6 @@ import org.mockito.ArgumentCaptor;
import sonia.scm.NotFoundException;
import sonia.scm.store.JAXBConfigurationStoreFactory;
import sonia.scm.user.xml.XmlUserDAO;
import sonia.scm.util.MockUtil;
import static org.mockito.Mockito.*;
@@ -57,7 +55,6 @@ import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.List;
import org.junit.Rule;
import sonia.scm.store.ConfigurationStoreFactory;
/**
*