mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
add tests
This commit is contained in:
@@ -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() {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user