create new simplified PluginManager API

This commit is contained in:
Sebastian Sdorra
2019-08-20 10:33:57 +02:00
parent 0aaec1174a
commit 3f1521bcca
19 changed files with 220 additions and 1410 deletions

View File

@@ -16,6 +16,9 @@ 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.AvailablePlugin;
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;
@@ -27,6 +30,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -87,10 +91,10 @@ class AvailablePluginResourceTest {
@Test
void getAvailablePlugins() throws URISyntaxException, UnsupportedEncodingException {
PluginInformation pluginInformation = new PluginInformation();
pluginInformation.setState(PluginState.AVAILABLE);
when(pluginManager.getAvailable()).thenReturn(Collections.singletonList(pluginInformation));
when(collectionMapper.map(Collections.singletonList(pluginInformation))).thenReturn(new MockedResultDto());
AvailablePlugin plugin = createPlugin();
when(pluginManager.getAvailable()).thenReturn(Collections.singletonList(plugin));
when(collectionMapper.mapAvailable(Collections.singletonList(plugin))).thenReturn(new MockedResultDto());
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/available");
request.accept(VndMediaType.PLUGIN_COLLECTION);
@@ -105,16 +109,18 @@ class AvailablePluginResourceTest {
@Test
void getAvailablePlugin() throws UnsupportedEncodingException, URISyntaxException {
PluginInformation pluginInformation = new PluginInformation();
pluginInformation.setState(PluginState.AVAILABLE);
pluginInformation.setName("pluginName");
pluginInformation.setVersion("2.0.0");
when(pluginManager.getAvailable()).thenReturn(Collections.singletonList(pluginInformation));
AvailablePlugin plugin = createPlugin(pluginInformation);
when(pluginManager.getAvailable("pluginName")).thenReturn(Optional.of(plugin));
PluginDto pluginDto = new PluginDto();
pluginDto.setName("pluginName");
when(mapper.map(pluginInformation)).thenReturn(pluginDto);
when(mapper.map(plugin)).thenReturn(pluginDto);
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/available/pluginName/2.0.0");
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/available/pluginName");
request.accept(VndMediaType.PLUGIN);
MockHttpResponse response = new MockHttpResponse();
@@ -126,17 +132,26 @@ class AvailablePluginResourceTest {
@Test
void installPlugin() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.post("/v2/plugins/available/pluginName/2.0.0/install");
MockHttpRequest request = MockHttpRequest.post("/v2/plugins/available/pluginName/install");
request.accept(VndMediaType.PLUGIN);
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
verify(pluginManager).install("pluginName:2.0.0");
verify(pluginManager).install("pluginName");
assertThat(HttpServletResponse.SC_OK).isEqualTo(response.getStatus());
}
}
private AvailablePlugin createPlugin() {
return createPlugin(new PluginInformation());
}
private AvailablePlugin createPlugin(PluginInformation pluginInformation) {
AvailablePluginDescriptor descriptor = new AvailablePluginDescriptor(pluginInformation, new PluginCondition(), Collections.emptySet());
return new AvailablePlugin(descriptor);
}
@Nested
class WithoutAuthorization {
@@ -156,7 +171,7 @@ class AvailablePluginResourceTest {
@Test
void shouldNotGetAvailablePluginIfMissingPermission() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/available/pluginName/2.0.0");
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/available/pluginName");
request.accept(VndMediaType.PLUGIN);
MockHttpResponse response = new MockHttpResponse();
@@ -166,7 +181,7 @@ class AvailablePluginResourceTest {
@Test
void shouldNotInstallPluginIfMissingPermission() throws URISyntaxException {
ThreadContext.unbindSubject();
MockHttpRequest request = MockHttpRequest.post("/v2/plugins/available/pluginName/2.0.0/install");
MockHttpRequest request = MockHttpRequest.post("/v2/plugins/available/pluginName/install");
request.accept(VndMediaType.PLUGIN);
MockHttpResponse response = new MockHttpResponse();

View File

@@ -16,11 +16,10 @@ 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.InstalledPlugin;
import sonia.scm.plugin.InstalledPluginDescriptor;
import sonia.scm.plugin.PluginInformation;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.plugin.PluginState;
import sonia.scm.plugin.InstalledPlugin;
import sonia.scm.plugin.PluginManager;
import sonia.scm.web.VndMediaType;
import javax.inject.Provider;
@@ -28,12 +27,12 @@ import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Optional;
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.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class InstalledPluginResourceTest {
@@ -46,15 +45,15 @@ class InstalledPluginResourceTest {
@Mock
Provider<AvailablePluginResource> availablePluginResourceProvider;
@Mock
private PluginLoader pluginLoader;
@Mock
private PluginDtoCollectionMapper collectionMapper;
@Mock
private PluginDtoMapper mapper;
@Mock
private PluginManager pluginManager;
@InjectMocks
InstalledPluginResource installedPluginResource;
@@ -86,9 +85,9 @@ class InstalledPluginResourceTest {
@Test
void getInstalledPlugins() throws URISyntaxException, UnsupportedEncodingException {
InstalledPlugin installedPlugin = new InstalledPlugin(null, null, null, null);
when(pluginLoader.getInstalledPlugins()).thenReturn(Collections.singletonList(installedPlugin));
when(collectionMapper.map(Collections.singletonList(installedPlugin))).thenReturn(new MockedResultDto());
InstalledPlugin installedPlugin = createPlugin();
when(pluginManager.getInstalled()).thenReturn(Collections.singletonList(installedPlugin));
when(collectionMapper.mapInstalled(Collections.singletonList(installedPlugin))).thenReturn(new MockedResultDto());
MockHttpRequest request = MockHttpRequest.get("/v2/plugins/installed");
request.accept(VndMediaType.PLUGIN_COLLECTION);
@@ -105,10 +104,9 @@ class InstalledPluginResourceTest {
PluginInformation pluginInformation = new PluginInformation();
pluginInformation.setVersion("2.0.0");
pluginInformation.setName("pluginName");
pluginInformation.setState(PluginState.INSTALLED);
InstalledPluginDescriptor plugin = new InstalledPluginDescriptor(2, pluginInformation, null, null, false, null);
InstalledPlugin installedPlugin = new InstalledPlugin(plugin, null, null, null);
when(pluginLoader.getInstalledPlugins()).thenReturn(Collections.singletonList(installedPlugin));
InstalledPlugin installedPlugin = createPlugin(pluginInformation);
when(pluginManager.getInstalled("pluginName")).thenReturn(Optional.of(installedPlugin));
PluginDto pluginDto = new PluginDto();
pluginDto.setName("pluginName");
@@ -125,6 +123,18 @@ class InstalledPluginResourceTest {
}
}
private InstalledPlugin createPlugin() {
return createPlugin(new PluginInformation());
}
private InstalledPlugin createPlugin(PluginInformation information) {
InstalledPlugin plugin = mock(InstalledPlugin.class);
InstalledPluginDescriptor descriptor = mock(InstalledPluginDescriptor.class);
lenient().when(descriptor.getInformation()).thenReturn(information);
lenient().when(plugin.getDescriptor()).thenReturn(descriptor);
return plugin;
}
@Nested
class WithoutAuthorization {

View File

@@ -4,13 +4,19 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.plugin.Plugin;
import sonia.scm.plugin.PluginDescriptor;
import sonia.scm.plugin.PluginInformation;
import sonia.scm.plugin.PluginState;
import java.net.URI;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.in;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class PluginDtoMapperTest {
@@ -25,7 +31,8 @@ class PluginDtoMapperTest {
void shouldMapInformation() {
PluginInformation information = createPluginInformation();
PluginDto dto = mapper.map(information);
PluginDto dto = new PluginDto();
mapper.map(information, dto);
assertThat(dto.getName()).isEqualTo("scm-cas-plugin");
assertThat(dto.getVersion()).isEqualTo("1.0.0");
@@ -48,41 +55,51 @@ class PluginDtoMapperTest {
@Test
void shouldAppendInstalledSelfLink() {
PluginInformation information = createPluginInformation();
information.setState(PluginState.INSTALLED);
Plugin plugin = createPlugin(PluginState.INSTALLED);
PluginDto dto = mapper.map(information);
PluginDto dto = mapper.map(plugin);
assertThat(dto.getLinks().getLinkBy("self").get().getHref())
.isEqualTo("https://hitchhiker.com/v2/plugins/installed/scm-cas-plugin");
}
@Test
void shouldAppendAvailableSelfLink() {
PluginInformation information = createPluginInformation();
information.setState(PluginState.AVAILABLE);
Plugin plugin = createPlugin(PluginState.AVAILABLE);
PluginDto dto = mapper.map(information);
PluginDto dto = mapper.map(plugin);
assertThat(dto.getLinks().getLinkBy("self").get().getHref())
.isEqualTo("https://hitchhiker.com/v2/plugins/available/scm-cas-plugin/1.0.0");
.isEqualTo("https://hitchhiker.com/v2/plugins/available/scm-cas-plugin");
}
@Test
void shouldAppendInstallLink() {
PluginInformation information = createPluginInformation();
information.setState(PluginState.AVAILABLE);
Plugin plugin = createPlugin(PluginState.AVAILABLE);
PluginDto dto = mapper.map(information);
PluginDto dto = mapper.map(plugin);
assertThat(dto.getLinks().getLinkBy("install").get().getHref())
.isEqualTo("https://hitchhiker.com/v2/plugins/available/scm-cas-plugin/1.0.0/install");
.isEqualTo("https://hitchhiker.com/v2/plugins/available/scm-cas-plugin/install");
}
@Test
void shouldReturnMiscellaneousIfCategoryIsNull() {
PluginInformation information = createPluginInformation();
information.setCategory(null);
PluginDto dto = mapper.map(information);
Plugin plugin = createPlugin(information, PluginState.AVAILABLE);
PluginDto dto = mapper.map(plugin);
assertThat(dto.getCategory()).isEqualTo("Miscellaneous");
}
private Plugin createPlugin(PluginState state) {
return createPlugin(createPluginInformation(), state);
}
private Plugin createPlugin(PluginInformation information, PluginState state) {
Plugin plugin = Mockito.mock(Plugin.class);
when(plugin.getState()).thenReturn(state);
PluginDescriptor descriptor = mock(PluginDescriptor.class);
when(descriptor.getInformation()).thenReturn(information);
when(plugin.getDescriptor()).thenReturn(descriptor);
return plugin;
}
}