add tests

This commit is contained in:
Eduard Heimbuch
2019-07-31 16:59:13 +02:00
parent 8a8942cbc4
commit c2e7ecd164
8 changed files with 504 additions and 12 deletions

View File

@@ -27,13 +27,11 @@ import static sonia.scm.NotFoundException.notFound;
public class AvailablePluginResource {
private final PluginDtoCollectionMapper collectionMapper;
private PluginDtoMapper dtoMapper;
private final PluginManager pluginManager;
@Inject
public AvailablePluginResource(PluginDtoCollectionMapper collectionMapper, PluginDtoMapper dtoMapper, PluginManager pluginManager) {
public AvailablePluginResource(PluginDtoCollectionMapper collectionMapper, PluginManager pluginManager) {
this.collectionMapper = collectionMapper;
this.dtoMapper = dtoMapper;
this.pluginManager = pluginManager;
}

View File

@@ -1,7 +1,9 @@
package sonia.scm.plugin;
package sonia.scm.api.v2.resources;
import com.google.common.collect.ImmutableList;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@@ -24,7 +26,7 @@ public final class PluginCenterDto implements Serializable {
@XmlRootElement(name = "_embedded")
@XmlAccessorType(XmlAccessType.FIELD)
static class Embedded {
public static class Embedded {
@XmlElement(name = "plugins")
private List<Plugin> plugins;
@@ -40,7 +42,8 @@ public final class PluginCenterDto implements Serializable {
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "plugins")
@Getter
static class Plugin {
@AllArgsConstructor
public static class Plugin {
private String name;
private String displayName;
@@ -63,7 +66,8 @@ public final class PluginCenterDto implements Serializable {
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "conditions")
@Getter
static class Condition {
@AllArgsConstructor
public static class Condition {
private String os;
private String arch;
@@ -73,6 +77,7 @@ public final class PluginCenterDto implements Serializable {
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "dependencies")
@Getter
@AllArgsConstructor
static class Dependency {
private String name;
}

View File

@@ -1,4 +1,7 @@
package sonia.scm.plugin;
package sonia.scm.api.v2.resources;
import sonia.scm.plugin.PluginCondition;
import sonia.scm.plugin.PluginInformation;
import java.util.Collections;
import java.util.HashSet;

View File

@@ -46,6 +46,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.SCMContextProvider;
import sonia.scm.api.v2.resources.PluginCenterDto;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.config.ScmConfiguration;
@@ -67,21 +68,18 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.xml.bind.JAXB;
import sonia.scm.net.ahc.AdvancedHttpClient;
import static sonia.scm.plugin.PluginCenterDtoMapper.*;
import static sonia.scm.api.v2.resources.PluginCenterDtoMapper.*;
/**
* TODO replace aether stuff.

View File

@@ -0,0 +1,124 @@
package sonia.scm.api.v2.resources;
import com.google.common.io.Resources;
import de.otto.edison.hal.HalRepresentation;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.plugin.Plugin;
import sonia.scm.plugin.PluginInformation;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.plugin.PluginManager;
import sonia.scm.plugin.PluginState;
import sonia.scm.plugin.PluginWrapper;
import sonia.scm.web.VndMediaType;
import javax.inject.Provider;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class InstalledPluginResourceTest {
private Dispatcher dispatcher;
private URL resources = Resources.getResource("sonia/scm/api/v2/installedPlugins-001.json");
@Mock
Provider<InstalledPluginResource> installedPluginResourceProvider;
@Mock
Provider<AvailablePluginResource> availablePluginResourceProvider;
@Mock
private PluginManager pluginManager;
@Mock
private PluginLoader pluginLoader;
@Mock
private PluginDtoCollectionMapper collectionMapper;
@Mock
private PluginDtoMapper mapper;
@InjectMocks
InstalledPluginResource installedPluginResource;
PluginRootResource pluginRootResource;
private final Subject subject = mock(Subject.class);
@BeforeEach
void prepareEnvironment() {
dispatcher = MockDispatcherFactory.createDispatcher();
pluginRootResource = new PluginRootResource(installedPluginResourceProvider, availablePluginResourceProvider);
when(installedPluginResourceProvider.get()).thenReturn(installedPluginResource);
dispatcher.getRegistry().addSingletonResource(pluginRootResource);
}
@Nested
class withAuthorization {
@BeforeEach
void bindSubject() {
ThreadContext.bind(subject);
when(subject.isPermitted(any(String.class))).thenReturn(true);
}
@AfterEach
public void unbindSubject() {
ThreadContext.unbindSubject();
}
@Test
void getInstalledPlugins() throws URISyntaxException, UnsupportedEncodingException {
PluginInformation pluginInformation = new PluginInformation();
pluginInformation.setVersion("2.0.0");
pluginInformation.setName("plugin-name");
pluginInformation.setState(PluginState.INSTALLED);
Plugin plugin = new Plugin(2, pluginInformation, null, null, false, null);
PluginWrapper pluginWrapper = new PluginWrapper(plugin, null, null, null);
when(pluginLoader.getInstalledPlugins()).thenReturn(Collections.singletonList(pluginWrapper));
PluginDto pluginDto = new PluginDto();
pluginDto.setName("plugin-name");
pluginDto.setVersion("2.0.0");
//TODO How to mock this?
when(collectionMapper.map(Collections.singletonList(pluginWrapper))).thenReturn(new HalRepresentation());
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/installed");
request.accept(VndMediaType.PLUGIN_COLLECTION);
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/plugins/installed\"}"));
}
@Test
void getInstalledPlugin() {
}
}
}

View File

@@ -0,0 +1,91 @@
package sonia.scm.api.v2.resources;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import sonia.scm.plugin.PluginInformation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static sonia.scm.api.v2.resources.PluginCenterDto.*;
class PluginCenterDtoMapperTest {
private PluginCenterDtoMapper pluginCenterDtoMapper;
@BeforeEach
void initMapper() {
pluginCenterDtoMapper = new PluginCenterDtoMapper();
}
@Test
void shouldMapSinglePlugin() {
Plugin plugin = new Plugin(
"scm-hitchhiker-plugin",
"SCM Hitchhiker Plugin",
"plugin for hitchhikers",
"Travel",
"2.0.0",
"trillian",
"555000444",
new Condition("linux", "amd64","2.0.0"),
new Dependency("scm-review-plugin"),
new HashMap<>());
PluginInformation result = PluginCenterDtoMapper.map(Collections.singletonList(plugin)).iterator().next();
assertThat(result.getAuthor()).isEqualTo(plugin.getAuthor());
assertThat(result.getCategory()).isEqualTo(plugin.getCategory());
assertThat(result.getVersion()).isEqualTo(plugin.getVersion());
assertThat(result.getCondition().getArch()).isEqualTo(plugin.getConditions().getArch());
assertThat(result.getCondition().getMinVersion()).isEqualTo(plugin.getConditions().getMinVersion());
assertThat(result.getCondition().getOs().iterator().next()).isEqualTo(plugin.getConditions().getOs());
assertThat(result.getDescription()).isEqualTo(plugin.getDescription());
assertThat(result.getName()).isEqualTo(plugin.getName());
}
@Test
void shouldMapMultiplePlugins() {
Plugin plugin1 = new Plugin(
"scm-hitchhiker-plugin",
"SCM Hitchhiker Plugin",
"plugin for hitchhikers",
"Travel",
"2.0.0",
"dent",
"555000444",
new Condition("linux", "amd64","2.0.0"),
new Dependency("scm-review-plugin"),
new HashMap<>());
Plugin plugin2 = new Plugin(
"scm-review-plugin",
"SCM Hitchhiker Plugin",
"plugin for hitchhikers",
"Travel",
"2.1.0",
"trillian",
"12345678aa",
new Condition("linux", "amd64","2.0.0"),
new Dependency("scm-review-plugin"),
new HashMap<>());
Set<PluginInformation> resultSet = PluginCenterDtoMapper.map(Arrays.asList(plugin1, plugin2));
List pluginsList = new ArrayList(resultSet);
PluginInformation pluginInformation1 = (PluginInformation) pluginsList.get(1);
PluginInformation pluginInformation2 = (PluginInformation) pluginsList.get(0);
assertThat(pluginInformation1.getAuthor()).isEqualTo(plugin1.getAuthor());
assertThat(pluginInformation1.getVersion()).isEqualTo(plugin1.getVersion());
assertThat(pluginInformation2.getAuthor()).isEqualTo(plugin2.getAuthor());
assertThat(pluginInformation2.getVersion()).isEqualTo(plugin2.getVersion());
assertThat(resultSet.size()).isEqualTo(2);
}
}

View File

@@ -0,0 +1,201 @@
{
"_links": {
"self": { "href": "http://localhost:8081/scm/api/v2/plugins/installed" }
},
"_embedded": {
"plugins": [
{
"name": "scm-issuetracker-plugin",
"category": "Library Plugin",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "Helper classes for issuetracker plugins.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-issuetracker-plugin"
}
}
},
{
"name": "scm-webhook-plugin",
"category": "Miscellaneous",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "Notify a remote webserver whenever a repository is pushed to.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-webhook-plugin"
}
}
},
{
"name": "scm-jenkins-plugin",
"category": "Continuous Integration",
"version": "2.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "This plugin will ping your Jenkins CI server when a new commit is pushed to SCM-Manager.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-jenkins-plugin"
}
}
},
{
"name": "scm-legacy-plugin",
"category": "Miscellaneous",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "The easiest way to share your Git, Mercurial\n and Subversion repositories over http.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-legacy-plugin"
}
}
},
{
"name": "scm-jira-plugin",
"category": "Issue-Tracking",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "This plugin integrates Atlassian JIRA to SCM-Manager.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-jira-plugin"
}
}
},
{
"name": "scm-ci-plugin",
"category": "Miscellaneous",
"version": "2.0.0-SNAPSHOT",
"author": "Cloudogu GmbH",
"description": "SCM-Manager CI Plugin",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-ci-plugin"
}
}
},
{
"name": "scm-svn-plugin",
"category": "Subversion",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "Plugin for the version control system Subversion",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-svn-plugin"
}
}
},
{
"name": "scm-hg-plugin",
"category": "Mercurial",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "Plugin for the version control system Mercurial",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-hg-plugin"
}
}
},
{
"name": "scm-review-plugin",
"category": "Miscellaneous",
"version": "2.0.0-SNAPSHOT",
"author": "Cloudogu GmbH",
"description": "SCM-Manager Review Plugin",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-review-plugin"
}
}
},
{
"name": "scm-git-plugin",
"category": "Git",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "Plugin for the version control system Git",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-git-plugin"
}
}
},
{
"name": "scm-script-plugin",
"category": "Development",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "Script support for scm-manager.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-script-plugin"
}
}
},
{
"name": "scm-mail-plugin",
"category": "Library Plugin",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "The mail plugin provides an api for sending e-mails.\n This api can be used by other plugins.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-mail-plugin"
}
}
},
{
"name": "scm-authormapping-plugin",
"category": "Miscellaneous",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "Lookup and transform usernames to the real names stored in the scm-manager user database or in a mapping table.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-authormapping-plugin"
}
}
},
{
"name": "scm-pushlog-plugin",
"category": "Miscellaneous",
"version": "2.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "Tracks who pushed what to a repository.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-pushlog-plugin"
}
}
},
{
"name": "scm-branchwp-plugin",
"category": "Miscellaneous",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "This plugin adds branch write protection for repositories.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-branchwp-plugin"
}
}
},
{
"name": "scm-notify-plugin",
"category": "Miscellaneous",
"version": "2.0.0-SNAPSHOT",
"author": "Sebastian Sdorra",
"description": "This plugin sends email notifications to a list of subscribed addresses whenever a repo has changes.",
"_links": {
"self": {
"href": "http://localhost:8081/scm/api/v2/plugins/installed/scm-notify-plugin"
}
}
}
]
}
}

View File

@@ -0,0 +1,72 @@
{
"_embedded": {
"plugins": [
{
"name": "scm-branchwp-plugin",
"displayName": "SCM Branch WritePermission Plugin",
"description": "This plugin adds branch write protection for repositories.",
"category": "Miscellaneous",
"version": "1.0.0",
"author": "Sebastian Sdorra",
"sha256sum": "f6585d369d3737415f05ea06e99048ac602fadcb76f5056b32b6cc232b9e8331",
"_links": {
"download": {
"href": "https://oss.cloudogu.com/jenkins/job/scm-manager/job/scm-manager-bitbucket/job/scm-branchwp-plugin/job/2.0.0/lastSuccessfulBuild/artifact/target/scm-branchwp-plugin-2.0.0-SNAPSHOT.smp"
}
}
},
{
"name": "scm-pathwp-plugin",
"displayName": "SCM Path WritePermission Plugin",
"description": "This plugin adds path write protection for repositories.",
"category": "Miscellaneous",
"version": "2.1.0",
"author": "Sebastian Sdorra",
"conditions": {
"os": "linux",
"minVersion": "2.1.0"
},
"sha256sum": "abc85d369d3737415f05ea06e99048ac602fadcb76f5056b32b6cc232b9e8331",
"_links": {
"download": {
"href": "https://oss.cloudogu.com/jenkins/job/scm-manager/job/scm-manager-bitbucket/job/scm-pathwp-plugin/job/2.0.0/lastSuccessfulBuild/artifact/target/scm-pathwp-plugin-2.0.0-SNAPSHOT.smp"
}
}
},
{
"name": "scm-mail-plugin",
"displayName": "SCM Mail Plugin",
"description": "The mail plugin provides an api for sending e-mails. This api can be used by other plugins.",
"category": "Library Plugin",
"version": "2.6.7",
"author": "Sebastian Sdorra",
"conditions": {
"os": "windows"
},
"sha256sum": "def85d369d3737415f05ea06e99048ac602fadcb76f5056b32b6cc232b9e8331",
"_links": {
"download": {
"href": "https://oss.cloudogu.com/jenkins/job/scm-manager/job/scm-manager-bitbucket/job/scm-mail-plugin/job/2.0.0/lastSuccessfulBuild/artifact/target/scm-mail-plugin-2.0.0-SNAPSHOT.smp"
}
}
},
{
"name": "scm-review-plugin",
"displayName": "SCM Review Plugin",
"description": "SCM-Manager Review Plugin",
"category": "Miscellaneous",
"version": "2.4.0",
"author": "Sebastian Sdorra",
"conditions": {
"arch": "armv7"
},
"sha256sum": "12385d369d3737415f05ea06e99048ac602fadcb76f5056b32b6cc232b9e8331",
"_links": {
"download": {
"href": "https://oss.cloudogu.com/jenkins/job/scm-manager/job/scm-manager-bitbucket/job/scm-review-plugin/job/develop/lastSuccessfulBuild/artifact/target/scm-review-plugin-2.0.0-SNAPSHOT.smp"
}
}
}
]
}
}