mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-05 04:55:50 +01:00
move events to scm-core / move subscriber to landingpage-plugin
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
enum PluginEventType {
|
||||
INSTALLED, INSTALLATION_FAILED
|
||||
}
|
||||
import sonia.scm.event.Event;
|
||||
|
||||
@Event
|
||||
public class PluginCenterEvent {}
|
||||
@@ -36,4 +36,8 @@ public class PluginEvent {
|
||||
this.eventType = eventType;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public enum PluginEventType {
|
||||
INSTALLED, INSTALLATION_FAILED
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,8 @@
|
||||
SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -462,16 +463,6 @@
|
||||
<version>2.1.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- optional dependencies -->
|
||||
<dependency>
|
||||
<groupId>sonia.scm.plugins</groupId>
|
||||
<artifactId>scm-landingpage-plugin</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
@@ -51,7 +51,6 @@ import static sonia.scm.ScmConstraintViolationException.Builder.doThrow;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
@@ -173,10 +172,10 @@ public class DefaultPluginManager implements PluginManager {
|
||||
PendingPluginInstallation pending = installer.install(plugin);
|
||||
dependencyTracker.addInstalled(plugin.getDescriptor());
|
||||
pendingInstallations.add(pending);
|
||||
eventBus.post(new PluginEvent(PluginEventType.INSTALLED, plugin));
|
||||
eventBus.post(new PluginEvent(PluginEvent.PluginEventType.INSTALLED, plugin));
|
||||
} catch (PluginInstallException ex) {
|
||||
cancelPending(pendingInstallations);
|
||||
eventBus.post(new PluginEvent(PluginEventType.INSTALLATION_FAILED, plugin));
|
||||
eventBus.post(new PluginEvent(PluginEvent.PluginEventType.INSTALLATION_FAILED, plugin));
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
@@ -258,7 +257,7 @@ public class DefaultPluginManager implements PluginManager {
|
||||
|
||||
Set<String> dependencies = plugin.getDescriptor().getDependencies();
|
||||
if (dependencies != null) {
|
||||
for (String dependency: dependencies){
|
||||
for (String dependency : dependencies) {
|
||||
collectPluginsToInstall(plugins, dependency, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ package sonia.scm.plugin;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.event.Event;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.net.ahc.AdvancedHttpClient;
|
||||
|
||||
@@ -62,12 +61,8 @@ class PluginCenterLoader {
|
||||
return mapper.map(pluginCenterDto);
|
||||
} catch (Exception ex) {
|
||||
LOG.error("failed to load plugins from plugin center, returning empty list", ex);
|
||||
eventBus.post(new PluginCenterNotAvailableEvent());
|
||||
eventBus.post(new PluginCenterEvent());
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
||||
@Event
|
||||
class PluginCenterNotAvailableEvent {}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import com.cloudogu.scm.myevents.MyEvent;
|
||||
import com.github.legman.Subscribe;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import sonia.scm.EagerSingleton;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.xml.XmlInstantAdapter;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
||||
import java.time.Instant;
|
||||
|
||||
@Extension(requires = "scm-landingpage-plugin")
|
||||
@EagerSingleton
|
||||
public class PluginInstallationFailedEventSubscriber {
|
||||
|
||||
private final ScmEventBus eventBus;
|
||||
|
||||
@Inject
|
||||
public PluginInstallationFailedEventSubscriber(ScmEventBus eventBus) {
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void handleEvent(PluginEvent pluginEvent) {
|
||||
if (pluginEvent.getEventType() == PluginEventType.INSTALLATION_FAILED) {
|
||||
AvailablePlugin newPlugin = pluginEvent.getPlugin();
|
||||
|
||||
String permission = PluginPermissions.manage().asShiroString();
|
||||
String pluginName = newPlugin.getDescriptor().getInformation().getDisplayName();
|
||||
|
||||
String pluginVersion = newPlugin.getDescriptor().getInformation().getVersion();
|
||||
|
||||
eventBus.post(new PluginInstallationFailedEvent(permission, pluginName, pluginVersion, Instant.now()));
|
||||
}
|
||||
}
|
||||
|
||||
@XmlRootElement
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
static class PluginInstallationFailedEvent extends MyEvent {
|
||||
private String pluginName;
|
||||
private String pluginVersion;
|
||||
@XmlJavaTypeAdapter(XmlInstantAdapter.class)
|
||||
private Instant date;
|
||||
|
||||
PluginInstallationFailedEvent(String permission, String pluginName, String pluginVersion, Instant date) {
|
||||
super(PluginInstalledEventSubscriber.PluginInstalledEvent.class.getSimpleName(), permission);
|
||||
|
||||
this.pluginName = pluginName;
|
||||
this.pluginVersion = pluginVersion;
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import com.cloudogu.scm.myevents.MyEvent;
|
||||
import com.github.legman.Subscribe;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import sonia.scm.EagerSingleton;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.xml.XmlInstantAdapter;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
@Extension(requires = "scm-landingpage-plugin")
|
||||
@EagerSingleton
|
||||
public class PluginInstalledEventSubscriber {
|
||||
|
||||
private final ScmEventBus eventBus;
|
||||
private final PluginManager pluginManager;
|
||||
|
||||
@Inject
|
||||
public PluginInstalledEventSubscriber(ScmEventBus eventBus, PluginManager pluginManager) {
|
||||
this.eventBus = eventBus;
|
||||
this.pluginManager = pluginManager;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void handleEvent(PluginEvent pluginEvent) {
|
||||
if (pluginEvent.getEventType() == PluginEventType.INSTALLED) {
|
||||
AvailablePlugin newPlugin = pluginEvent.getPlugin();
|
||||
Optional<InstalledPlugin> installedPlugin = pluginManager.getInstalled(newPlugin.getDescriptor().getInformation().getDisplayName());
|
||||
|
||||
String permission = PluginPermissions.manage().asShiroString();
|
||||
|
||||
String pluginName = newPlugin.getDescriptor().getInformation().getName();
|
||||
|
||||
String previousPluginVersion = null;
|
||||
if (installedPlugin.isPresent()) {
|
||||
previousPluginVersion = installedPlugin.get().getDescriptor().getInformation().getVersion();
|
||||
}
|
||||
|
||||
String newPluginVersion = newPlugin.getDescriptor().getInformation().getVersion();
|
||||
|
||||
eventBus.post(new PluginInstalledEvent(permission, pluginName, previousPluginVersion, newPluginVersion, Instant.now()));
|
||||
}
|
||||
}
|
||||
|
||||
@XmlRootElement
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
static class PluginInstalledEvent extends MyEvent {
|
||||
private String pluginName;
|
||||
private String previousPluginVersion;
|
||||
private String newPluginVersion;
|
||||
@XmlJavaTypeAdapter(XmlInstantAdapter.class)
|
||||
private Instant date;
|
||||
|
||||
PluginInstalledEvent(String permission, String pluginName, String previousPluginVersion, String newPluginVersion, Instant date) {
|
||||
super(PluginInstalledEventSubscriber.PluginInstalledEvent.class.getSimpleName(), permission);
|
||||
|
||||
this.pluginName = pluginName;
|
||||
this.previousPluginVersion = previousPluginVersion;
|
||||
this.newPluginVersion = newPluginVersion;
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.cloudogu.scm.myevents.MyEvent;
|
||||
import com.github.legman.Subscribe;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import sonia.scm.EagerSingleton;
|
||||
import sonia.scm.HandlerEventType;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.xml.XmlInstantAdapter;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
||||
import java.time.Instant;
|
||||
|
||||
@Extension(requires = "scm-landingpage-plugin")
|
||||
@EagerSingleton
|
||||
public class RepositoryCreatedEventSubscriber {
|
||||
|
||||
private final ScmEventBus eventBus;
|
||||
|
||||
@Inject
|
||||
public RepositoryCreatedEventSubscriber(ScmEventBus eventBus) {
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void handleEvent(RepositoryEvent event) {
|
||||
if (event.getEventType() == HandlerEventType.CREATE) {
|
||||
Repository eventRepo = event.getItem();
|
||||
String permission = RepositoryPermissions.read(event.getItem()).asShiroString();
|
||||
String repository = eventRepo.getNamespace() + "/" + eventRepo.getName();
|
||||
String creator = SecurityUtils.getSubject().getPrincipals().oneByType(User.class).getDisplayName();
|
||||
Instant date = Instant.now();
|
||||
|
||||
eventBus.post(new RepositoryCreatedEvent(permission, repository, creator, date));
|
||||
}
|
||||
}
|
||||
|
||||
@XmlRootElement
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
static class RepositoryCreatedEvent extends MyEvent {
|
||||
private String repository;
|
||||
private String creator;
|
||||
@XmlJavaTypeAdapter(XmlInstantAdapter.class)
|
||||
private Instant date;
|
||||
|
||||
RepositoryCreatedEvent(String permission, String repository, String creator, Instant date) {
|
||||
super(RepositoryCreatedEvent.class.getSimpleName(), permission);
|
||||
this.repository = repository;
|
||||
this.creator = creator;
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,22 +31,18 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.lifecycle.Restarter;
|
||||
import sonia.scm.plugin.AvailablePlugin;
|
||||
import sonia.scm.plugin.AvailablePluginDescriptor;
|
||||
import sonia.scm.plugin.InstalledPlugin;
|
||||
import sonia.scm.plugin.PluginInformation;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static sonia.scm.plugin.PluginTestHelper.createAvailable;
|
||||
import static sonia.scm.plugin.PluginTestHelper.createInstalled;
|
||||
|
||||
@@ -557,7 +557,7 @@ class DefaultPluginManagerTest {
|
||||
|
||||
verify(eventBus).post(eventCaptor.capture());
|
||||
|
||||
assertThat(eventCaptor.getValue().getEventType()).isEqualTo(PluginEventType.INSTALLED);
|
||||
assertThat(eventCaptor.getValue().getEventType()).isEqualTo(PluginEvent.PluginEventType.INSTALLED);
|
||||
assertThat(eventCaptor.getValue().getPlugin()).isEqualTo(review);
|
||||
}
|
||||
|
||||
@@ -570,7 +570,7 @@ class DefaultPluginManagerTest {
|
||||
assertThrows(PluginDownloadException.class, () -> manager.install("scm-review-plugin", false));
|
||||
|
||||
verify(eventBus).post(eventCaptor.capture());
|
||||
assertThat(eventCaptor.getValue().getEventType()).isEqualTo(PluginEventType.INSTALLATION_FAILED);
|
||||
assertThat(eventCaptor.getValue().getEventType()).isEqualTo(PluginEvent.PluginEventType.INSTALLATION_FAILED);
|
||||
assertThat(eventCaptor.getValue().getPlugin()).isEqualTo(review);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,15 +21,12 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import com.sun.mail.iap.Argument;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
@@ -41,9 +38,7 @@ import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -89,6 +84,6 @@ class PluginCenterLoaderTest {
|
||||
|
||||
loader.load(PLUGIN_URL);
|
||||
|
||||
verify(eventBus).post(any(PluginCenterLoader.PluginCenterNotAvailableEvent.class));
|
||||
verify(eventBus).post(any(PluginCenterEvent.class));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PluginInstallationFailedEventSubscriberTest {
|
||||
|
||||
private AvailablePlugin newPlugin = PluginTestHelper.createAvailable("scm-hitchhiker-plugin");
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@Mock
|
||||
private ScmEventBus eventBus;
|
||||
|
||||
@Mock
|
||||
private PluginEvent event;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<PluginInstallationFailedEventSubscriber.PluginInstallationFailedEvent> eventCaptor;
|
||||
|
||||
@InjectMocks
|
||||
private PluginInstallationFailedEventSubscriber subscriber;
|
||||
|
||||
@BeforeEach
|
||||
void bindSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDownSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFireMyEvent() {
|
||||
when(event.getEventType()).thenReturn(PluginEventType.INSTALLATION_FAILED);
|
||||
when(event.getPlugin()).thenReturn(newPlugin);
|
||||
|
||||
subscriber.handleEvent(event);
|
||||
|
||||
verify(eventBus).post(eventCaptor.capture());
|
||||
|
||||
PluginInstallationFailedEventSubscriber.PluginInstallationFailedEvent pluginInstalledEvent = eventCaptor.getValue();
|
||||
assertThat(pluginInstalledEvent.getPermission()).isEqualTo("plugin:manage");
|
||||
assertThat(pluginInstalledEvent.getPluginVersion()).isEqualTo("1.0");
|
||||
assertThat(pluginInstalledEvent.getPluginName()).isEqualTo(newPlugin.getDescriptor().getInformation().getDisplayName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotFireMyEventWhenNotCreatedEvent() {
|
||||
when(event.getEventType()).thenReturn(PluginEventType.INSTALLED);
|
||||
subscriber.handleEvent(event);
|
||||
|
||||
verify(eventBus, never()).post(eventCaptor.capture());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PluginInstalledEventSubscriberTest {
|
||||
|
||||
private InstalledPlugin oldPlugin = PluginTestHelper.createInstalled("scm-hitchhiker-plugin");
|
||||
private AvailablePlugin newPlugin = PluginTestHelper.createAvailable("scm-hitchhiker-plugin", "1.1");
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@Mock
|
||||
private ScmEventBus eventBus;
|
||||
|
||||
@Mock
|
||||
private PluginEvent event;
|
||||
|
||||
@Mock
|
||||
private PluginManager pluginManager;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<PluginInstalledEventSubscriber.PluginInstalledEvent> eventCaptor;
|
||||
|
||||
@InjectMocks
|
||||
private PluginInstalledEventSubscriber subscriber;
|
||||
|
||||
@BeforeEach
|
||||
void bindSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDownSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFireMyEvent() {
|
||||
when(event.getEventType()).thenReturn(PluginEventType.INSTALLED);
|
||||
when(event.getPlugin()).thenReturn(newPlugin);
|
||||
when(pluginManager.getInstalled("scm-hitchhiker-plugin")).thenReturn(Optional.of(oldPlugin));
|
||||
|
||||
subscriber.handleEvent(event);
|
||||
|
||||
verify(eventBus).post(eventCaptor.capture());
|
||||
|
||||
PluginInstalledEventSubscriber.PluginInstalledEvent pluginInstalledEvent = eventCaptor.getValue();
|
||||
assertThat(pluginInstalledEvent.getPermission()).isEqualTo("plugin:manage");
|
||||
assertThat(pluginInstalledEvent.getPreviousPluginVersion()).isEqualTo("1.0");
|
||||
assertThat(pluginInstalledEvent.getNewPluginVersion()).isEqualTo("1.1");
|
||||
assertThat(pluginInstalledEvent.getPluginName()).isEqualTo(newPlugin.getDescriptor().getInformation().getDisplayName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotFireMyEventWhenNotCreatedEvent() {
|
||||
when(event.getEventType()).thenReturn(PluginEventType.INSTALLATION_FAILED);
|
||||
subscriber.handleEvent(event);
|
||||
|
||||
verify(eventBus, never()).post(eventCaptor.capture());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
import org.apache.shiro.subject.PrincipalCollection;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.HandlerEventType;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.user.User;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RepositoryCreatedEventSubscriberTest {
|
||||
|
||||
private Repository REPOSITORY = new Repository("1", "git", "nicest", "repo");
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@Mock
|
||||
private ScmEventBus eventBus;
|
||||
|
||||
@Mock
|
||||
private RepositoryEvent event;
|
||||
|
||||
@Mock
|
||||
private PrincipalCollection principalCollection;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<RepositoryCreatedEventSubscriber.RepositoryCreatedEvent> eventCaptor;
|
||||
|
||||
@InjectMocks
|
||||
private RepositoryCreatedEventSubscriber subscriber;
|
||||
|
||||
@BeforeEach
|
||||
void bindSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDownSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFireMyEvent() {
|
||||
User trillian = new User("trillian", "Trillian", "tricia@hitchhiker.org");
|
||||
|
||||
when(event.getEventType()).thenReturn(HandlerEventType.CREATE);
|
||||
when(event.getItem()).thenReturn(REPOSITORY);
|
||||
when(subject.getPrincipals()).thenReturn(principalCollection);
|
||||
when(principalCollection.oneByType(User.class)).thenReturn(trillian);
|
||||
|
||||
subscriber.handleEvent(event);
|
||||
|
||||
verify(eventBus).post(eventCaptor.capture());
|
||||
|
||||
RepositoryCreatedEventSubscriber.RepositoryCreatedEvent repositoryCreatedEvent = eventCaptor.getValue();
|
||||
assertThat(repositoryCreatedEvent.getPermission()).isEqualTo("repository:read:1");
|
||||
assertThat(repositoryCreatedEvent.getRepository()).isEqualTo(REPOSITORY.getNamespace() + "/" + REPOSITORY.getName());
|
||||
assertThat(repositoryCreatedEvent.getType()).isEqualTo(RepositoryCreatedEventSubscriber.RepositoryCreatedEvent.class.getSimpleName());
|
||||
assertThat(repositoryCreatedEvent.getCreator()).isEqualTo("Trillian");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotFireMyEventWhenNotCreatedEvent() {
|
||||
when(event.getEventType()).thenReturn(HandlerEventType.BEFORE_CREATE);
|
||||
subscriber.handleEvent(event);
|
||||
|
||||
verify(eventBus, never()).post(eventCaptor.capture());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user