diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginEventType.java b/scm-core/src/main/java/sonia/scm/plugin/PluginCenterEvent.java similarity index 94% rename from scm-webapp/src/main/java/sonia/scm/plugin/PluginEventType.java rename to scm-core/src/main/java/sonia/scm/plugin/PluginCenterEvent.java index 2cde7c35f8..fe0896c344 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginEventType.java +++ b/scm-core/src/main/java/sonia/scm/plugin/PluginCenterEvent.java @@ -24,6 +24,7 @@ package sonia.scm.plugin; -enum PluginEventType { - INSTALLED, INSTALLATION_FAILED -} +import sonia.scm.event.Event; + +@Event +public class PluginCenterEvent {} diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginEvent.java b/scm-core/src/main/java/sonia/scm/plugin/PluginEvent.java similarity index 95% rename from scm-webapp/src/main/java/sonia/scm/plugin/PluginEvent.java rename to scm-core/src/main/java/sonia/scm/plugin/PluginEvent.java index 45716ae04b..7802b3954d 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginEvent.java +++ b/scm-core/src/main/java/sonia/scm/plugin/PluginEvent.java @@ -36,4 +36,8 @@ public class PluginEvent { this.eventType = eventType; this.plugin = plugin; } + + public enum PluginEventType { + INSTALLED, INSTALLATION_FAILED + } } diff --git a/scm-webapp/pom.xml b/scm-webapp/pom.xml index 1d85d6a2df..a95599aa08 100644 --- a/scm-webapp/pom.xml +++ b/scm-webapp/pom.xml @@ -24,7 +24,8 @@ SOFTWARE. --> - + 4.0.0 @@ -462,16 +463,6 @@ 2.1.1 provided - - - - - sonia.scm.plugins - scm-landingpage-plugin - 1.0.0-SNAPSHOT - true - - diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java b/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java index 40b6230261..bb6dc1491f 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/DefaultPluginManager.java @@ -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 dependencies = plugin.getDescriptor().getDependencies(); if (dependencies != null) { - for (String dependency: dependencies){ + for (String dependency : dependencies) { collectPluginsToInstall(plugins, dependency, false); } } diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginCenterLoader.java b/scm-webapp/src/main/java/sonia/scm/plugin/PluginCenterLoader.java index 16f3123157..d48a6468a3 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginCenterLoader.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/PluginCenterLoader.java @@ -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 {} - } diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstallationFailedEventSubscriber.java b/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstallationFailedEventSubscriber.java deleted file mode 100644 index 5fd4188606..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstallationFailedEventSubscriber.java +++ /dev/null @@ -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; - } - } - -} diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstalledEventSubscriber.java b/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstalledEventSubscriber.java deleted file mode 100644 index 4913eb80cf..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstalledEventSubscriber.java +++ /dev/null @@ -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 = 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; - } - } - -} diff --git a/scm-webapp/src/main/java/sonia/scm/repository/RepositoryCreatedEventSubscriber.java b/scm-webapp/src/main/java/sonia/scm/repository/RepositoryCreatedEventSubscriber.java deleted file mode 100644 index 746d73b2d2..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/repository/RepositoryCreatedEventSubscriber.java +++ /dev/null @@ -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; - } - } - -} diff --git a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/PluginDtoMapperTest.java b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/PluginDtoMapperTest.java index 51a7001140..8301e6fb76 100644 --- a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/PluginDtoMapperTest.java +++ b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/PluginDtoMapperTest.java @@ -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; diff --git a/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java b/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java index aa4d5cb5b1..03b12f92cf 100644 --- a/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java +++ b/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java @@ -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); } diff --git a/scm-webapp/src/test/java/sonia/scm/plugin/PluginCenterLoaderTest.java b/scm-webapp/src/test/java/sonia/scm/plugin/PluginCenterLoaderTest.java index b00241eebf..d05cbc0107 100644 --- a/scm-webapp/src/test/java/sonia/scm/plugin/PluginCenterLoaderTest.java +++ b/scm-webapp/src/test/java/sonia/scm/plugin/PluginCenterLoaderTest.java @@ -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)); } } diff --git a/scm-webapp/src/test/java/sonia/scm/plugin/PluginInstallationFailedEventSubscriberTest.java b/scm-webapp/src/test/java/sonia/scm/plugin/PluginInstallationFailedEventSubscriberTest.java deleted file mode 100644 index 88dff091cf..0000000000 --- a/scm-webapp/src/test/java/sonia/scm/plugin/PluginInstallationFailedEventSubscriberTest.java +++ /dev/null @@ -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 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()); - } - -} diff --git a/scm-webapp/src/test/java/sonia/scm/plugin/PluginInstalledEventSubscriberTest.java b/scm-webapp/src/test/java/sonia/scm/plugin/PluginInstalledEventSubscriberTest.java deleted file mode 100644 index 61348c96c3..0000000000 --- a/scm-webapp/src/test/java/sonia/scm/plugin/PluginInstalledEventSubscriberTest.java +++ /dev/null @@ -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 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()); - } - -} diff --git a/scm-webapp/src/test/java/sonia/scm/repository/RepositoryCreatedEventSubscriberTest.java b/scm-webapp/src/test/java/sonia/scm/repository/RepositoryCreatedEventSubscriberTest.java deleted file mode 100644 index 59930d1709..0000000000 --- a/scm-webapp/src/test/java/sonia/scm/repository/RepositoryCreatedEventSubscriberTest.java +++ /dev/null @@ -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 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()); - } - -}