make scm-webapp depend optional of scm-landingpage-plugin // add some events for landingpage

This commit is contained in:
Eduard Heimbuch
2020-04-01 16:01:26 +02:00
parent 1c21562c96
commit 548bf97c57
14 changed files with 741 additions and 12 deletions

View File

@@ -36,16 +36,19 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
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.NotFoundException;
import sonia.scm.ScmConstraintViolationException;
import sonia.scm.event.ScmEventBus;
import sonia.scm.lifecycle.Restarter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -53,6 +56,7 @@ import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.in;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing;
@@ -83,6 +87,12 @@ class DefaultPluginManagerTest {
@Mock
private Restarter restarter;
@Mock
private ScmEventBus eventBus;
@Captor
private ArgumentCaptor<PluginEvent> eventCaptor;
@InjectMocks
private DefaultPluginManager manager;
@@ -537,8 +547,36 @@ class DefaultPluginManagerTest {
verify(installer, never()).install(oldScriptPlugin);
}
@Test
void shouldFirePluginEventOnInstallation() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review));
manager.install("scm-review-plugin", false);
verify(eventBus).post(eventCaptor.capture());
assertThat(eventCaptor.getValue().getEventType()).isEqualTo(PluginEventType.INSTALLED);
assertThat(eventCaptor.getValue().getPlugin()).isEqualTo(review);
}
@Test
void shouldFirePluginEventOnFailedInstallation() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review));
doThrow(new PluginDownloadException(review, new IOException())).when(installer).install(review);
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().getPlugin()).isEqualTo(review);
}
}
@Nested
class WithoutReadPermissions {

View File

@@ -24,12 +24,16 @@
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;
import sonia.scm.event.ScmEventBus;
import sonia.scm.net.ahc.AdvancedHttpClient;
import java.io.IOException;
@@ -37,6 +41,10 @@ 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;
@ExtendWith(MockitoExtension.class)
@@ -50,6 +58,9 @@ class PluginCenterLoaderTest {
@Mock
private PluginCenterDtoMapper mapper;
@Mock
private ScmEventBus eventBus;
@InjectMocks
private PluginCenterLoader loader;
@@ -71,4 +82,13 @@ class PluginCenterLoaderTest {
Set<AvailablePlugin> fetch = loader.load(PLUGIN_URL);
assertThat(fetch).isEmpty();
}
@Test
void shouldThrowExceptionAndFirePluginCenterNotAvailableEvent() throws IOException {
when(client.get(PLUGIN_URL).request()).thenThrow(new IOException("failed to fetch"));
loader.load(PLUGIN_URL);
verify(eventBus).post(any(PluginCenterLoader.PluginCenterNotAvailableEvent.class));
}
}

View File

@@ -0,0 +1,98 @@
/*
* 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());
}
}

View File

@@ -0,0 +1,106 @@
/*
* 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());
}
}

View File

@@ -0,0 +1,109 @@
/*
* 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());
}
}