implemented plugin installation

This commit is contained in:
Sebastian Sdorra
2019-08-20 14:43:48 +02:00
parent 65b59d1aec
commit e24673be0a
12 changed files with 276 additions and 15 deletions

View File

@@ -21,7 +21,6 @@ import sonia.scm.plugin.AvailablePluginDescriptor;
import sonia.scm.plugin.PluginCondition;
import sonia.scm.plugin.PluginInformation;
import sonia.scm.plugin.PluginManager;
import sonia.scm.plugin.PluginState;
import sonia.scm.web.VndMediaType;
import javax.inject.Provider;
@@ -148,7 +147,9 @@ class AvailablePluginResourceTest {
}
private AvailablePlugin createPlugin(PluginInformation pluginInformation) {
AvailablePluginDescriptor descriptor = new AvailablePluginDescriptor(pluginInformation, new PluginCondition(), Collections.emptySet());
AvailablePluginDescriptor descriptor = new AvailablePluginDescriptor(
pluginInformation, new PluginCondition(), Collections.emptySet(), "https://download.hitchhiker.com", null
);
return new AvailablePlugin(descriptor);
}

View File

@@ -2,7 +2,6 @@ package sonia.scm.plugin;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.checkerframework.checker.nullness.Opt;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
@@ -10,16 +9,11 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class DefaultPluginManagerTest {
@@ -30,6 +24,9 @@ class DefaultPluginManagerTest {
@Mock
private PluginCenter center;
@Mock
private PluginInstaller installer;
@InjectMocks
private DefaultPluginManager manager;
@@ -118,6 +115,44 @@ class DefaultPluginManagerTest {
assertThat(available).isEmpty();
}
@Test
void shouldInstallThePlugin() {
AvailablePlugin git = createAvailable("scm-git-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(git));
manager.install("scm-git-plugin");
verify(installer).install(git);
}
@Test
void shouldInstallDependingPlugins() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(review.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-mail-plugin"));
AvailablePlugin mail = createAvailable("scm-mail-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail));
manager.install("scm-review-plugin");
verify(installer).install(mail);
verify(installer).install(review);
}
@Test
void shouldNotInstallAlreadyInstalledDependencies() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(review.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-mail-plugin"));
AvailablePlugin mail = createAvailable("scm-mail-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail));
InstalledPlugin installedMail = createInstalled("scm-mail-plugin");
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(installedMail));
manager.install("scm-review-plugin");
verify(installer).install(review);
}
private AvailablePlugin createAvailable(String name) {
PluginInformation information = new PluginInformation();
information.setName(name);

View File

@@ -1,5 +1,6 @@
package sonia.scm.plugin;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -42,13 +43,17 @@ class PluginCenterDtoMapperTest {
"555000444",
new Condition(Collections.singletonList("linux"), "amd64","2.0.0"),
ImmutableSet.of("scm-review-plugin"),
new HashMap<>());
ImmutableMap.of("download", new Link("http://download.hitchhiker.com"))
);
when(dto.getEmbedded().getPlugins()).thenReturn(Collections.singletonList(plugin));
AvailablePluginDescriptor descriptor = mapper.map(dto).iterator().next().getDescriptor();
PluginInformation information = descriptor.getInformation();
PluginCondition condition = descriptor.getCondition();
assertThat(descriptor.getUrl()).isEqualTo("http://download.hitchhiker.com");
assertThat(descriptor.getChecksum()).contains("555000444");
assertThat(information.getAuthor()).isEqualTo(plugin.getAuthor());
assertThat(information.getCategory()).isEqualTo(plugin.getCategory());
assertThat(information.getVersion()).isEqualTo(plugin.getVersion());
@@ -72,7 +77,8 @@ class PluginCenterDtoMapperTest {
"12345678aa",
new Condition(Collections.singletonList("linux"), "amd64","2.0.0"),
ImmutableSet.of("scm-review-plugin"),
new HashMap<>());
ImmutableMap.of("download", new Link("http://download.hitchhiker.com/review"))
);
Plugin plugin2 = new Plugin(
"scm-hitchhiker-plugin",
@@ -85,7 +91,8 @@ class PluginCenterDtoMapperTest {
"555000444",
new Condition(Collections.singletonList("linux"), "amd64","2.0.0"),
ImmutableSet.of("scm-review-plugin"),
new HashMap<>());
ImmutableMap.of("download", new Link("http://download.hitchhiker.com/hitchhiker"))
);
when(dto.getEmbedded().getPlugins()).thenReturn(Arrays.asList(plugin1, plugin2));

View File

@@ -0,0 +1,90 @@
package sonia.scm.plugin;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.SCMContextProvider;
import sonia.scm.net.ahc.AdvancedHttpClient;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.in;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
@ExtendWith({MockitoExtension.class, TempDirectory.class})
class PluginInstallerTest {
@Mock
private SCMContextProvider context;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private AdvancedHttpClient client;
@InjectMocks
private PluginInstaller installer;
private Path directory;
@BeforeEach
void setUpContext(@TempDirectory.TempDir Path directory) {
this.directory = directory;
when(context.getBaseDirectory()).thenReturn(directory.toFile());
}
@Test
void shouldDownloadPlugin() throws IOException {
mockContent("42");
installer.install(createGitPlugin());
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).hasContent("42");
}
private void mockContent(String content) throws IOException {
when(client.get("https://download.hitchhiker.com").request().contentAsStream())
.thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
}
private AvailablePlugin createGitPlugin() {
return createPlugin(
"scm-git-plugin",
"https://download.hitchhiker.com",
"73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049" // 42
);
}
@Test
void shouldThrowPluginDownloadException() throws IOException {
when(client.get("https://download.hitchhiker.com").request()).thenThrow(new IOException("failed to download"));
assertThrows(PluginDownloadException.class, () -> installer.install(createGitPlugin()));
}
@Test
void shouldThrowPluginChecksumMismatchException() throws IOException {
mockContent("21");
assertThrows(PluginChecksumMismatchException.class, () -> installer.install(createGitPlugin()));
}
private AvailablePlugin createPlugin(String name, String url, String checksum) {
PluginInformation information = new PluginInformation();
information.setName(name);
AvailablePluginDescriptor descriptor = new AvailablePluginDescriptor(
information, null, Collections.emptySet(), url, checksum
);
return new AvailablePlugin(descriptor);
}
}