Search in sources :

Example 11 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class FelixGoPluginOSGiFrameworkIntegrationTest method shouldLoadAValidPluginWithMultipleExtensions_ImplementingDifferentExtensions.

@Test
void shouldLoadAValidPluginWithMultipleExtensions_ImplementingDifferentExtensions() throws Exception {
    final GoPluginBundleDescriptor bundleDescriptor = new GoPluginBundleDescriptor(getPluginDescriptor("valid-plugin-with-multiple-extensions", validMultipleExtensionPluginBundleDir));
    registry.loadPlugin(bundleDescriptor);
    Bundle bundle = pluginOSGiFramework.loadPlugin(bundleDescriptor);
    assertThat(bundle.getState()).isEqualTo(Bundle.ACTIVE);
    BundleContext context = bundle.getBundleContext();
    String taskExtensionFilter = String.format("(&(%s=%s)(%s=%s))", "PLUGIN_ID", "valid-plugin-with-multiple-extensions", Constants.BUNDLE_CATEGORY, "task");
    String analyticsExtensionFilter = String.format("(&(%s=%s)(%s=%s))", "PLUGIN_ID", "valid-plugin-with-multiple-extensions", Constants.BUNDLE_CATEGORY, "analytics");
    ServiceReference<?>[] taskExtensionServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), taskExtensionFilter);
    assertThat(taskExtensionServiceReferences.length).isEqualTo(1);
    assertThat(((GoPlugin) context.getService(taskExtensionServiceReferences[0])).pluginIdentifier().getExtension()).isEqualTo("task");
    ServiceReference<?>[] analyticsExtensionServiceReferences = context.getServiceReferences(GoPlugin.class.getCanonicalName(), analyticsExtensionFilter);
    assertThat(analyticsExtensionServiceReferences.length).isEqualTo(1);
    assertThat(((GoPlugin) context.getService(analyticsExtensionServiceReferences[0])).pluginIdentifier().getExtension()).isEqualTo("analytics");
}
Also used : GoPluginBundleDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor) Bundle(org.osgi.framework.Bundle) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.jupiter.api.Test)

Example 12 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class DefaultPluginManagerTest method shouldSayPluginIsOfGivenExtensionTypeWhenReferenceIsFound.

@Test
void shouldSayPluginIsOfGivenExtensionTypeWhenReferenceIsFound() {
    String pluginId = "plugin-id";
    String extensionType = "sample-extension";
    GoPluginIdentifier pluginIdentifier = new GoPluginIdentifier(extensionType, asList("1.0"));
    final GoPlugin goPlugin = mock(GoPlugin.class);
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    when(goPluginOSGiFramework.hasReferenceFor(GoPlugin.class, pluginId, extensionType)).thenReturn(true);
    lenient().doAnswer(invocationOnMock -> {
        ActionWithReturn<GoPlugin, GoPluginApiResponse> action = (ActionWithReturn<GoPlugin, GoPluginApiResponse>) invocationOnMock.getArguments()[2];
        return action.execute(goPlugin, descriptor);
    }).when(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq(pluginId), eq(extensionType), any(ActionWithReturn.class));
    lenient().when(goPlugin.pluginIdentifier()).thenReturn(pluginIdentifier);
    DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, goPluginOSGiFramework, jarChangeListener, pluginRequestProcessorRegistry, systemEnvironment, pluginLoader);
    assertThat(pluginManager.isPluginOfType(extensionType, pluginId)).isTrue();
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Test(org.junit.jupiter.api.Test)

Example 13 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class DefaultPluginManagerTest method shouldThrowExceptionIfMatchingExtensionVersionNotFound.

@Test
void shouldThrowExceptionIfMatchingExtensionVersionNotFound() {
    String pluginId = "plugin-id";
    String extensionType = "sample-extension";
    GoPlugin goPlugin = mock(GoPlugin.class);
    GoPlugginOSGiFrameworkStub osGiFrameworkStub = new GoPlugginOSGiFrameworkStub(goPlugin);
    osGiFrameworkStub.addHasReferenceFor(GoPlugin.class, pluginId, extensionType, true);
    when(goPlugin.pluginIdentifier()).thenReturn(new GoPluginIdentifier(extensionType, asList("1.0", "2.0")));
    DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, osGiFrameworkStub, jarChangeListener, pluginRequestProcessorRegistry, systemEnvironment, pluginLoader);
    try {
        pluginManager.resolveExtensionVersion(pluginId, extensionType, asList("3.0", "4.0"));
        fail("should have thrown exception for not finding matching extension version");
    } catch (Exception e) {
        assertThat(e.getMessage()).isEqualTo("Could not find matching extension version between Plugin[plugin-id] and Go");
    }
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 14 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class DefaultPluginManagerTest method shouldResolveToCorrectExtensionVersion.

@Test
void shouldResolveToCorrectExtensionVersion() {
    String pluginId = "plugin-id";
    String extensionType = "sample-extension";
    GoPlugin goPlugin = mock(GoPlugin.class);
    GoPlugginOSGiFrameworkStub osGiFrameworkStub = new GoPlugginOSGiFrameworkStub(goPlugin);
    osGiFrameworkStub.addHasReferenceFor(GoPlugin.class, pluginId, extensionType, true);
    when(goPlugin.pluginIdentifier()).thenReturn(new GoPluginIdentifier(extensionType, asList("1.0", "2.0")));
    DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, osGiFrameworkStub, jarChangeListener, pluginRequestProcessorRegistry, systemEnvironment, pluginLoader);
    assertThat(pluginManager.resolveExtensionVersion(pluginId, extensionType, asList("1.0", "2.0", "3.0"))).isEqualTo("2.0");
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) Test(org.junit.jupiter.api.Test)

Example 15 with GoPlugin

use of com.thoughtworks.go.plugin.api.GoPlugin in project gocd by gocd.

the class DefaultPluginManagerTest method shouldSubmitPluginApiRequestToGivenPlugin.

@Test
void shouldSubmitPluginApiRequestToGivenPlugin() throws Exception {
    String extensionType = "sample-extension";
    GoPluginApiRequest request = mock(GoPluginApiRequest.class);
    GoPluginApiResponse expectedResponse = mock(GoPluginApiResponse.class);
    final GoPlugin goPlugin = mock(GoPlugin.class);
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    when(goPlugin.handle(request)).thenReturn(expectedResponse);
    ArgumentCaptor<PluginAwareDefaultGoApplicationAccessor> captor = ArgumentCaptor.forClass(PluginAwareDefaultGoApplicationAccessor.class);
    doNothing().when(goPlugin).initializeGoApplicationAccessor(captor.capture());
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) {
            ActionWithReturn<GoPlugin, GoPluginApiResponse> action = (ActionWithReturn<GoPlugin, GoPluginApiResponse>) invocationOnMock.getArguments()[3];
            return action.execute(goPlugin, descriptor);
        }
    }).when(goPluginOSGiFramework).doOn(eq(GoPlugin.class), eq("plugin-id"), eq(extensionType), any(ActionWithReturn.class));
    DefaultPluginManager pluginManager = new DefaultPluginManager(monitor, registry, goPluginOSGiFramework, jarChangeListener, pluginRequestProcessorRegistry, systemEnvironment, pluginLoader);
    GoPluginApiResponse actualResponse = pluginManager.submitTo("plugin-id", extensionType, request);
    assertThat(actualResponse).isEqualTo(expectedResponse);
    PluginAwareDefaultGoApplicationAccessor accessor = captor.getValue();
    assertThat(accessor.pluginDescriptor()).isEqualTo(descriptor);
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) GoPlugin(com.thoughtworks.go.plugin.api.GoPlugin) GoPluginApiResponse(com.thoughtworks.go.plugin.api.response.GoPluginApiResponse) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

GoPlugin (com.thoughtworks.go.plugin.api.GoPlugin)15 Test (org.junit.jupiter.api.Test)12 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)9 GoPluginBundleDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginBundleDescriptor)8 IOException (java.io.IOException)8 Bundle (org.osgi.framework.Bundle)8 BundleContext (org.osgi.framework.BundleContext)8 ServiceReference (org.osgi.framework.ServiceReference)8 GoPluginIdentifier (com.thoughtworks.go.plugin.api.GoPluginIdentifier)5 FileHelper (com.thoughtworks.go.plugin.FileHelper)4 GoPluginApiResponse (com.thoughtworks.go.plugin.api.response.GoPluginApiResponse)4 DefaultPluginRegistry (com.thoughtworks.go.plugin.infra.plugininfo.DefaultPluginRegistry)4 ReflectionUtil (com.thoughtworks.go.util.ReflectionUtil)4 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)4 ZipUtil (com.thoughtworks.go.util.ZipUtil)4 File (java.io.File)4 HashMap (java.util.HashMap)4 ZipInputStream (java.util.zip.ZipInputStream)4 FileUtils (org.apache.commons.io.FileUtils)4 BundleClassLoader (org.apache.felix.framework.BundleWiringImpl.BundleClassLoader)4