mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
inject scmEventBus with constructor injection
This commit is contained in:
@@ -66,19 +66,21 @@ public class RepositoryResource {
|
||||
private final SingleResourceManagerAdapter<Repository, RepositoryDto> adapter;
|
||||
private final RepositoryBasedResourceProvider resourceProvider;
|
||||
private final ScmConfiguration scmConfiguration;
|
||||
private final ScmEventBus scmEventBus;
|
||||
|
||||
@Inject
|
||||
public RepositoryResource(
|
||||
RepositoryToRepositoryDtoMapper repositoryToDtoMapper,
|
||||
RepositoryDtoToRepositoryMapper dtoToRepositoryMapper, RepositoryManager manager,
|
||||
RepositoryBasedResourceProvider resourceProvider,
|
||||
ScmConfiguration scmConfiguration) {
|
||||
ScmConfiguration scmConfiguration, ScmEventBus scmEventBus) {
|
||||
this.dtoToRepositoryMapper = dtoToRepositoryMapper;
|
||||
this.manager = manager;
|
||||
this.repositoryToDtoMapper = repositoryToDtoMapper;
|
||||
this.adapter = new SingleResourceManagerAdapter<>(manager, Repository.class);
|
||||
this.resourceProvider = resourceProvider;
|
||||
this.scmConfiguration = scmConfiguration;
|
||||
this.scmEventBus = scmEventBus;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,7 +232,7 @@ public class RepositoryResource {
|
||||
return adapter.update(
|
||||
repoSupplier,
|
||||
existing -> {
|
||||
ScmEventBus.getInstance().post(new RepositoryRenamedEvent(HandlerEventType.MODIFY, changedRepo, unchangedRepo));
|
||||
scmEventBus.post(new RepositoryRenamedEvent(HandlerEventType.MODIFY, changedRepo, unchangedRepo));
|
||||
return changedRepo;
|
||||
},
|
||||
changed -> true,
|
||||
|
||||
@@ -42,10 +42,12 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.PageResult;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryInitializer;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.RepositoryRenamedEvent;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
import sonia.scm.user.User;
|
||||
@@ -108,6 +110,8 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
private RepositoryInitializer repositoryInitializer;
|
||||
@Mock
|
||||
private ScmConfiguration scmConfiguration;
|
||||
@Mock
|
||||
private ScmEventBus scmEventBus;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<Predicate<Repository>> filterCaptor;
|
||||
@@ -127,6 +131,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
super.dtoToRepositoryMapper = dtoToRepositoryMapper;
|
||||
super.manager = repositoryManager;
|
||||
super.scmConfiguration = scmConfiguration;
|
||||
super.scmEventBus = scmEventBus;
|
||||
RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper = new RepositoryCollectionToDtoMapper(repositoryToDtoMapper, resourceLinks);
|
||||
super.repositoryCollectionResource = new RepositoryCollectionResource(repositoryManager, repositoryCollectionToDtoMapper, dtoToRepositoryMapper, resourceLinks, repositoryInitializer);
|
||||
dispatcher.addSingletonResource(getRepositoryRootResource());
|
||||
@@ -380,15 +385,15 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotRenameRepositoryIfNamespaceStrategyIsNotCustom() throws Exception {
|
||||
mockRepository("space", "repo");
|
||||
public void shouldNotRenameRepositoryNamespaceIfNamespaceStrategyIsNotCustom() throws Exception {
|
||||
mockRepository("hitchhiker", "heart-of-gold");
|
||||
when(scmConfiguration.getNamespaceStrategy()).thenReturn("UsernameNamespaceStrategy");
|
||||
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/rename-repo.json");
|
||||
byte[] repository = Resources.toByteArray(url);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.post("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/rename")
|
||||
.post("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "hitchhiker/heart-of-gold/rename")
|
||||
.contentType(VndMediaType.REPOSITORY)
|
||||
.content(repository);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
@@ -433,6 +438,27 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_NO_CONTENT, response.getStatus());
|
||||
verify(repositoryManager).modify(any(Repository.class));
|
||||
verify(scmEventBus).post(any(RepositoryRenamedEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRenameRepositoryNameIfNamespaceStrategyNotCustom() throws Exception {
|
||||
mockRepository("space", "repo");
|
||||
when(scmConfiguration.getNamespaceStrategy()).thenReturn("UsernameNamespaceStrategy");
|
||||
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/rename-repo.json");
|
||||
byte[] repository = Resources.toByteArray(url);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.post("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/rename")
|
||||
.contentType(VndMediaType.REPOSITORY)
|
||||
.content(repository);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_NO_CONTENT, response.getStatus());
|
||||
verify(repositoryManager).modify(any(Repository.class));
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
|
||||
import static com.google.inject.util.Providers.of;
|
||||
@@ -48,6 +49,7 @@ abstract class RepositoryTestBase {
|
||||
RepositoryCollectionResource repositoryCollectionResource;
|
||||
AnnotateResource annotateResource;
|
||||
ScmConfiguration scmConfiguration;
|
||||
ScmEventBus scmEventBus;
|
||||
|
||||
|
||||
RepositoryRootResource getRepositoryRootResource() {
|
||||
@@ -69,7 +71,7 @@ abstract class RepositoryTestBase {
|
||||
dtoToRepositoryMapper,
|
||||
manager,
|
||||
repositoryBasedResourceProvider,
|
||||
scmConfiguration)),
|
||||
scmConfiguration, scmEventBus)),
|
||||
of(repositoryCollectionResource));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user