Fix injection for unit tests

This commit is contained in:
René Pfeuffer
2018-10-26 14:54:55 +02:00
parent 9279bfca5f
commit 22069914b0
10 changed files with 36 additions and 30 deletions

View File

@@ -1,13 +1,16 @@
package sonia.scm.api.rest;
import sonia.scm.AlreadyExistsException;
import sonia.scm.api.v2.resources.ExceptionWithContextToErrorDtoMapper;
import javax.inject.Inject;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;
@Provider
public class AlreadyExistsExceptionMapper extends ContextualExceptionMapper<AlreadyExistsException> {
public AlreadyExistsExceptionMapper() {
super(AlreadyExistsException.class, Status.CONFLICT);
@Inject
public AlreadyExistsExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) {
super(AlreadyExistsException.class, Status.CONFLICT, mapper);
}
}

View File

@@ -1,13 +1,16 @@
package sonia.scm.api.rest;
import sonia.scm.ConcurrentModificationException;
import sonia.scm.api.v2.resources.ExceptionWithContextToErrorDtoMapper;
import javax.inject.Inject;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
@Provider
public class ConcurrentModificationExceptionMapper extends ContextualExceptionMapper<ConcurrentModificationException> {
public ConcurrentModificationExceptionMapper() {
super(ConcurrentModificationException.class, Response.Status.CONFLICT);
@Inject
public ConcurrentModificationExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) {
super(ConcurrentModificationException.class, Response.Status.CONFLICT, mapper);
}
}

View File

@@ -12,15 +12,15 @@ import javax.ws.rs.ext.ExceptionMapper;
public class ContextualExceptionMapper<E extends Throwable & ExceptionWithContext> implements ExceptionMapper<E> {
@Inject
private ExceptionWithContextToErrorDtoMapper mapper;
private static final Logger logger = LoggerFactory.getLogger(ContextualExceptionMapper.class);
private ExceptionWithContextToErrorDtoMapper mapper;
private final Response.Status status;
private final Class<E> type;
public ContextualExceptionMapper(Class<E> type, Response.Status status) {
public ContextualExceptionMapper(Class<E> type, Response.Status status, ExceptionWithContextToErrorDtoMapper mapper) {
this.mapper = mapper;
this.type = type;
this.status = status;
}

View File

@@ -33,7 +33,9 @@ package sonia.scm.api.v2;
import sonia.scm.NotFoundException;
import sonia.scm.api.rest.ContextualExceptionMapper;
import sonia.scm.api.v2.resources.ExceptionWithContextToErrorDtoMapper;
import javax.inject.Inject;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
@@ -42,7 +44,8 @@ import javax.ws.rs.ext.Provider;
*/
@Provider
public class NotFoundExceptionMapper extends ContextualExceptionMapper<NotFoundException> {
public NotFoundExceptionMapper() {
super(NotFoundException.class, Response.Status.NOT_FOUND);
@Inject
public NotFoundExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) {
super(NotFoundException.class, Response.Status.NOT_FOUND, mapper);
}
}

View File

@@ -51,7 +51,8 @@ public class ChangesetRootResourceTest extends RepositoryTestBase {
public static final String CHANGESET_PATH = "space/repo/changesets/";
public static final String CHANGESET_URL = "/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + CHANGESET_PATH;
private final Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
private Dispatcher dispatcher;
private final URI baseUri = URI.create("/");
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
@@ -82,12 +83,10 @@ public class ChangesetRootResourceTest extends RepositoryTestBase {
changesetCollectionToDtoMapper = new ChangesetCollectionToDtoMapper(changesetToChangesetDtoMapper, resourceLinks);
changesetRootResource = new ChangesetRootResource(serviceFactory, changesetCollectionToDtoMapper, changesetToChangesetDtoMapper);
super.changesetRootResource = Providers.of(changesetRootResource);
dispatcher.getRegistry().addSingletonResource(getRepositoryRootResource());
dispatcher = DispatcherMock.createDispatcher(getRepositoryRootResource());
when(serviceFactory.create(new NamespaceAndName("space", "repo"))).thenReturn(repositoryService);
when(serviceFactory.create(any(Repository.class))).thenReturn(repositoryService);
when(repositoryService.getRepository()).thenReturn(new Repository("repoId", "git", "space", "repo"));
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
when(repositoryService.getLogCommand()).thenReturn(logCommandBuilder);
subjectThreadState.bind();
ThreadContext.bind(subject);

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.rest.ContextualExceptionMapper;
import sonia.scm.api.v2.NotFoundExceptionMapper;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
@@ -69,7 +70,8 @@ public class DiffResourceTest extends RepositoryTestBase {
when(serviceFactory.create(new NamespaceAndName("space", "repo"))).thenReturn(service);
when(serviceFactory.create(any(Repository.class))).thenReturn(service);
when(service.getRepository()).thenReturn(new Repository("repoId", "git", "space", "repo"));
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
ExceptionWithContextToErrorDtoMapperImpl mapper = new ExceptionWithContextToErrorDtoMapperImpl();
dispatcher.getProviderFactory().register(new NotFoundExceptionMapper(mapper));
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(CRLFInjectionExceptionMapper.class);
when(service.getDiffCommand()).thenReturn(diffCommandBuilder);

View File

@@ -12,10 +12,11 @@ public class DispatcherMock {
public static Dispatcher createDispatcher(Object resource) {
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addSingletonResource(resource);
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(AlreadyExistsExceptionMapper.class);
ExceptionWithContextToErrorDtoMapperImpl mapper = new ExceptionWithContextToErrorDtoMapperImpl();
dispatcher.getProviderFactory().register(new NotFoundExceptionMapper(mapper));
dispatcher.getProviderFactory().register(new AlreadyExistsExceptionMapper(mapper));
dispatcher.getProviderFactory().register(new ConcurrentModificationExceptionMapper(mapper));
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(ConcurrentModificationExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(InternalRepositoryExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(ChangePasswordNotAllowedExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(InvalidPasswordExceptionMapper.class);

View File

@@ -52,7 +52,6 @@ public class FileHistoryResourceTest extends RepositoryTestBase {
public static final String FILE_HISTORY_PATH = "space/repo/history/";
public static final String FILE_HISTORY_URL = "/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + FILE_HISTORY_PATH;
private final Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
private final URI baseUri = URI.create("/");
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
@@ -73,23 +72,21 @@ public class FileHistoryResourceTest extends RepositoryTestBase {
private FileHistoryRootResource fileHistoryRootResource;
private Dispatcher dispatcher;
private final Subject subject = mock(Subject.class);
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
@Before
public void prepareEnvironment() {
fileHistoryCollectionToDtoMapper = new FileHistoryCollectionToDtoMapper(changesetToChangesetDtoMapper, resourceLinks);
fileHistoryRootResource = new FileHistoryRootResource(serviceFactory, fileHistoryCollectionToDtoMapper);
super.fileHistoryRootResource = Providers.of(fileHistoryRootResource);
dispatcher.getRegistry().addSingletonResource(getRepositoryRootResource());
dispatcher = DispatcherMock.createDispatcher(getRepositoryRootResource());
when(serviceFactory.create(new NamespaceAndName("space", "repo"))).thenReturn(service);
when(serviceFactory.create(any(Repository.class))).thenReturn(service);
when(service.getRepository()).thenReturn(new Repository("repoId", "git", "space", "repo"));
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(InternalRepositoryExceptionMapper.class);
ExceptionWithContextToErrorDtoMapperImpl mapper = new ExceptionWithContextToErrorDtoMapperImpl();
when(service.getLogCommand()).thenReturn(logCommandBuilder);
subjectThreadState.bind();
ThreadContext.bind(subject);

View File

@@ -62,7 +62,7 @@ public class GroupRootResourceTest {
private ArgumentCaptor<Group> groupCaptor = ArgumentCaptor.forClass(Group.class);
@Before
public void prepareEnvironment() throws Exception {
public void prepareEnvironment() {
initMocks(this);
when(groupManager.create(groupCaptor.capture())).thenAnswer(invocation -> invocation.getArguments()[0]);
doNothing().when(groupManager).modify(groupCaptor.capture());

View File

@@ -46,7 +46,8 @@ public class ModificationsResourceTest extends RepositoryTestBase {
public static final String MODIFICATIONS_PATH = "space/repo/modifications/";
public static final String MODIFICATIONS_URL = "/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + MODIFICATIONS_PATH;
private final Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
private Dispatcher dispatcher;
private final URI baseUri = URI.create("/");
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
@@ -74,13 +75,10 @@ public class ModificationsResourceTest extends RepositoryTestBase {
public void prepareEnvironment() throws Exception {
modificationsRootResource = new ModificationsRootResource(serviceFactory, modificationsToDtoMapper);
super.modificationsRootResource = Providers.of(modificationsRootResource);
dispatcher.getRegistry().addSingletonResource(getRepositoryRootResource());
dispatcher = DispatcherMock.createDispatcher(getRepositoryRootResource());
when(serviceFactory.create(new NamespaceAndName("space", "repo"))).thenReturn(repositoryService);
when(serviceFactory.create(any(Repository.class))).thenReturn(repositoryService);
when(repositoryService.getRepository()).thenReturn(new Repository("repoId", "git", "space", "repo"));
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
dispatcher.getProviderFactory().registerProvider(InternalRepositoryExceptionMapper.class);
when(repositoryService.getModificationsCommand()).thenReturn(modificationsCommandBuilder);
subjectThreadState.bind();
ThreadContext.bind(subject);