Extract plugin creation for unit tests to util class

This commit is contained in:
Rene Pfeuffer
2019-09-16 09:55:38 +02:00
parent 70cfbf294b
commit 0142258705
4 changed files with 49 additions and 69 deletions

View File

@@ -33,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static sonia.scm.plugin.PluginTestHelper.createInstalled;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class InstalledPluginResourceTest { class InstalledPluginResourceTest {
@@ -85,7 +86,7 @@ class InstalledPluginResourceTest {
@Test @Test
void getInstalledPlugins() throws URISyntaxException, UnsupportedEncodingException { void getInstalledPlugins() throws URISyntaxException, UnsupportedEncodingException {
InstalledPlugin installedPlugin = createPlugin(); InstalledPlugin installedPlugin = createInstalled("");
when(pluginManager.getInstalled()).thenReturn(Collections.singletonList(installedPlugin)); when(pluginManager.getInstalled()).thenReturn(Collections.singletonList(installedPlugin));
when(collectionMapper.mapInstalled(Collections.singletonList(installedPlugin))).thenReturn(new MockedResultDto()); when(collectionMapper.mapInstalled(Collections.singletonList(installedPlugin))).thenReturn(new MockedResultDto());
@@ -104,7 +105,7 @@ class InstalledPluginResourceTest {
PluginInformation pluginInformation = new PluginInformation(); PluginInformation pluginInformation = new PluginInformation();
pluginInformation.setVersion("2.0.0"); pluginInformation.setVersion("2.0.0");
pluginInformation.setName("pluginName"); pluginInformation.setName("pluginName");
InstalledPlugin installedPlugin = createPlugin(pluginInformation); InstalledPlugin installedPlugin = createInstalled(pluginInformation);
when(pluginManager.getInstalled("pluginName")).thenReturn(Optional.of(installedPlugin)); when(pluginManager.getInstalled("pluginName")).thenReturn(Optional.of(installedPlugin));
@@ -123,18 +124,6 @@ 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 @Nested
class WithoutAuthorization { class WithoutAuthorization {

View File

@@ -21,6 +21,8 @@ import java.net.URI;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static sonia.scm.plugin.PluginTestHelper.createAvailable;
import static sonia.scm.plugin.PluginTestHelper.createInstalled;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class PluginDtoMapperTest { class PluginDtoMapperTest {
@@ -72,22 +74,16 @@ class PluginDtoMapperTest {
@Test @Test
void shouldAppendInstalledSelfLink() { void shouldAppendInstalledSelfLink() {
InstalledPlugin plugin = createInstalled(); InstalledPlugin plugin = createInstalled(createPluginInformation());
PluginDto dto = mapper.mapInstalled(plugin); PluginDto dto = mapper.mapInstalled(plugin);
assertThat(dto.getLinks().getLinkBy("self").get().getHref()) assertThat(dto.getLinks().getLinkBy("self").get().getHref())
.isEqualTo("https://hitchhiker.com/v2/plugins/installed/scm-cas-plugin"); .isEqualTo("https://hitchhiker.com/v2/plugins/installed/scm-cas-plugin");
} }
private InstalledPlugin createInstalled(PluginInformation information) {
InstalledPlugin plugin = mock(InstalledPlugin.class, Answers.RETURNS_DEEP_STUBS);
when(plugin.getDescriptor().getInformation()).thenReturn(information);
return plugin;
}
@Test @Test
void shouldAppendAvailableSelfLink() { void shouldAppendAvailableSelfLink() {
AvailablePlugin plugin = createAvailable(); AvailablePlugin plugin = createAvailable(createPluginInformation());
PluginDto dto = mapper.mapAvailable(plugin); PluginDto dto = mapper.mapAvailable(plugin);
assertThat(dto.getLinks().getLinkBy("self").get().getHref()) assertThat(dto.getLinks().getLinkBy("self").get().getHref())
@@ -96,7 +92,7 @@ class PluginDtoMapperTest {
@Test @Test
void shouldNotAppendInstallLinkWithoutPermissions() { void shouldNotAppendInstallLinkWithoutPermissions() {
AvailablePlugin plugin = createAvailable(); AvailablePlugin plugin = createAvailable(createPluginInformation());
PluginDto dto = mapper.mapAvailable(plugin); PluginDto dto = mapper.mapAvailable(plugin);
assertThat(dto.getLinks().getLinkBy("install")).isEmpty(); assertThat(dto.getLinks().getLinkBy("install")).isEmpty();
@@ -105,7 +101,7 @@ class PluginDtoMapperTest {
@Test @Test
void shouldAppendInstallLink() { void shouldAppendInstallLink() {
when(subject.isPermitted("plugin:manage")).thenReturn(true); when(subject.isPermitted("plugin:manage")).thenReturn(true);
AvailablePlugin plugin = createAvailable(); AvailablePlugin plugin = createAvailable(createPluginInformation());
PluginDto dto = mapper.mapAvailable(plugin); PluginDto dto = mapper.mapAvailable(plugin);
assertThat(dto.getLinks().getLinkBy("install").get().getHref()) assertThat(dto.getLinks().getLinkBy("install").get().getHref())
@@ -123,25 +119,10 @@ class PluginDtoMapperTest {
@Test @Test
void shouldAppendDependencies() { void shouldAppendDependencies() {
AvailablePlugin plugin = createAvailable(); AvailablePlugin plugin = createAvailable(createPluginInformation());
when(plugin.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("one", "two")); when(plugin.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("one", "two"));
PluginDto dto = mapper.mapAvailable(plugin); PluginDto dto = mapper.mapAvailable(plugin);
assertThat(dto.getDependencies()).containsOnly("one", "two"); assertThat(dto.getDependencies()).containsOnly("one", "two");
} }
private InstalledPlugin createInstalled() {
return createInstalled(createPluginInformation());
}
private AvailablePlugin createAvailable() {
return createAvailable(createPluginInformation());
}
private AvailablePlugin createAvailable(PluginInformation information) {
AvailablePluginDescriptor descriptor = mock(AvailablePluginDescriptor.class);
when(descriptor.getInformation()).thenReturn(information);
return new AvailablePlugin(descriptor);
}
} }

View File

@@ -26,6 +26,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.in; import static org.assertj.core.api.Assertions.in;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static sonia.scm.plugin.PluginTestHelper.createAvailable;
import static sonia.scm.plugin.PluginTestHelper.createInstalled;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class DefaultPluginManagerTest { class DefaultPluginManagerTest {
@@ -354,33 +356,4 @@ class DefaultPluginManagerTest {
} }
} }
private AvailablePlugin createAvailable(String name) {
PluginInformation information = new PluginInformation();
information.setName(name);
return createAvailable(information);
}
private InstalledPlugin createInstalled(String name) {
PluginInformation information = new PluginInformation();
information.setName(name);
return createInstalled(information);
}
private InstalledPlugin createInstalled(PluginInformation information) {
InstalledPlugin plugin = mock(InstalledPlugin.class, Answers.RETURNS_DEEP_STUBS);
returnInformation(plugin, information);
return plugin;
}
private AvailablePlugin createAvailable(PluginInformation information) {
AvailablePluginDescriptor descriptor = mock(AvailablePluginDescriptor.class);
lenient().when(descriptor.getInformation()).thenReturn(information);
return new AvailablePlugin(descriptor);
}
private void returnInformation(Plugin mockedPlugin, PluginInformation information) {
when(mockedPlugin.getDescriptor().getInformation()).thenReturn(information);
}
} }

View File

@@ -0,0 +1,37 @@
package sonia.scm.plugin;
import org.mockito.Answers;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class PluginTestHelper {
public static AvailablePlugin createAvailable(String name) {
PluginInformation information = new PluginInformation();
information.setName(name);
return createAvailable(information);
}
public static InstalledPlugin createInstalled(String name) {
PluginInformation information = new PluginInformation();
information.setName(name);
return createInstalled(information);
}
public static InstalledPlugin createInstalled(PluginInformation information) {
InstalledPlugin plugin = mock(InstalledPlugin.class, Answers.RETURNS_DEEP_STUBS);
returnInformation(plugin, information);
return plugin;
}
public static AvailablePlugin createAvailable(PluginInformation information) {
AvailablePluginDescriptor descriptor = mock(AvailablePluginDescriptor.class);
lenient().when(descriptor.getInformation()).thenReturn(information);
return new AvailablePlugin(descriptor);
}
private static void returnInformation(Plugin mockedPlugin, PluginInformation information) {
when(mockedPlugin.getDescriptor().getInformation()).thenReturn(information);
}
}