Search in sources :

Example 1 with GoPluginExtension

use of com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension in project gocd by gocd.

the class PluginServiceTest method shouldNotSavePluginSettingsIfPluginDoesNotExist.

@Test
public void shouldNotSavePluginSettingsIfPluginDoesNotExist() {
    PluginSettings pluginSettings = new PluginSettings("non-existent-plugin");
    Username currentUser = new Username("admin");
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    when(securityService.isUserAdmin(currentUser)).thenReturn(true);
    for (GoPluginExtension extension : extensions) {
        when(extension.canHandlePlugin("non-existent-plugin")).thenReturn(false);
    }
    pluginService.savePluginSettings(currentUser, result, pluginSettings);
    assertThat(result.httpCode(), is(422));
    assertThat(result.toString(), containsString("Plugin 'non-existent-plugin' does not exist or does not implement settings validation"));
}
Also used : GoPluginExtension(com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) PluginSettings(com.thoughtworks.go.server.domain.PluginSettings) Test(org.junit.Test)

Example 2 with GoPluginExtension

use of com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension in project gocd by gocd.

the class PluginService method validatePluginSettingsFor.

public void validatePluginSettingsFor(PluginSettings pluginSettings) {
    String pluginId = pluginSettings.getPluginId();
    PluginSettingsConfiguration configuration = pluginSettings.toPluginSettingsConfiguration();
    GoPluginExtension extension = findExtensionWhichCanHandleSettingsFor(pluginId);
    if (extension == null)
        throw new IllegalArgumentException(String.format("Plugin '%s' does not exist or does not implement settings validation.", pluginId));
    ValidationResult result = extension.validatePluginSettings(pluginId, configuration);
    if (!result.isSuccessful()) {
        for (ValidationError error : result.getErrors()) {
            pluginSettings.populateErrorMessageFor(error.getKey(), error.getMessage());
        }
    }
}
Also used : GoPluginExtension(com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension) PluginSettingsConfiguration(com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

Example 3 with GoPluginExtension

use of com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension in project gocd by gocd.

the class PluginService method validatePluginSettings.

void validatePluginSettings(PluginSettings pluginSettings) {
    pluginSettings.validateTree();
    if (pluginSettings.hasErrors()) {
        return;
    }
    final String pluginId = pluginSettings.getPluginId();
    final PluginSettingsConfiguration configuration = pluginSettings.toPluginSettingsConfiguration();
    final GoPluginExtension extension = findExtensionWhichCanHandleSettingsFor(pluginId);
    if (extension == null) {
        throw new IllegalArgumentException(String.format("Plugin '%s' does not exist or does not implement settings validation.", pluginId));
    }
    final ValidationResult result = extension.validatePluginSettings(pluginId, configuration);
    if (!result.isSuccessful()) {
        for (ValidationError error : result.getErrors()) {
            pluginSettings.populateErrorMessageFor(error.getKey(), error.getMessage());
        }
    }
}
Also used : GoPluginExtension(com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension) PluginSettingsConfiguration(com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

Example 4 with GoPluginExtension

use of com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension in project gocd by gocd.

the class PluginServiceTest method shouldCallValidationOnPlugin.

@Test
public void shouldCallValidationOnPlugin() throws Exception {
    for (GoPluginExtension extension : extensions) {
        String pluginId = UUID.randomUUID().toString();
        PluginSettingsMetadataStore.getInstance().addMetadataFor(pluginId, extension.extensionName(), null, null);
        when(extension.canHandlePlugin(pluginId)).thenReturn(true);
        when(extension.validatePluginSettings(eq(pluginId), any(PluginSettingsConfiguration.class))).thenReturn(new ValidationResult());
        PluginSettings pluginSettings = new PluginSettings(pluginId);
        pluginService.validatePluginSettingsFor(pluginSettings);
        verify(extension).validatePluginSettings(eq(pluginId), any(PluginSettingsConfiguration.class));
    }
}
Also used : GoPluginExtension(com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension) PluginSettings(com.thoughtworks.go.server.domain.PluginSettings) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PluginSettingsConfiguration(com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 5 with GoPluginExtension

use of com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension in project gocd by gocd.

the class PluginSettingsRequestProcessorTest method shouldRouteToTheRightExtensionBasedOnTheRequest_WhenAPluginHasMultipleExtensions.

@Test
public void shouldRouteToTheRightExtensionBasedOnTheRequest_WhenAPluginHasMultipleExtensions() throws Exception {
    GoPluginExtension anotherPluginExtension = mock(GoPluginExtension.class);
    String pluginId = "plugin-foo-id";
    Map<String, String> pluginSettings = m("k1", "v1", "k2", "v2");
    when(pluginDescriptor.id()).thenReturn(pluginId);
    when(pluginSqlMapDao.findPlugin(pluginId)).thenReturn(new Plugin(pluginId, "{\"k1\": \"v1\",\"k2\": \"v2\"}"));
    processor = new PluginSettingsRequestProcessor(applicationAccessor, pluginSqlMapDao, Arrays.asList(pluginExtension, anotherPluginExtension));
    DefaultGoApiRequest requestForExtension1 = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", singletonList("1.0")));
    setupExpectationsFor(pluginExtension, pluginId, "extension1");
    setupExpectationsFor(anotherPluginExtension, pluginId, "extension2");
    processor.process(pluginDescriptor, requestForExtension1);
    verify(pluginExtension).pluginSettingsJSON(eq(pluginId), anyMapOf(String.class, String.class));
    verify(anotherPluginExtension, never()).pluginSettingsJSON(eq(pluginId), anyMapOf(String.class, String.class));
    Mockito.reset(pluginExtension, anotherPluginExtension);
    DefaultGoApiRequest requestForExtension2 = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension2", singletonList("1.0")));
    setupExpectationsFor(pluginExtension, pluginId, "extension1");
    setupExpectationsFor(anotherPluginExtension, pluginId, "extension2");
    processor.process(pluginDescriptor, requestForExtension2);
    verify(pluginExtension, never()).pluginSettingsJSON(eq(pluginId), anyMapOf(String.class, String.class));
    verify(anotherPluginExtension).pluginSettingsJSON(eq(pluginId), anyMapOf(String.class, String.class));
    Mockito.reset(pluginExtension, anotherPluginExtension);
}
Also used : GoPluginExtension(com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension) GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) DefaultGoApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest) NullPlugin(com.thoughtworks.go.domain.NullPlugin) Plugin(com.thoughtworks.go.domain.Plugin) Test(org.junit.Test)

Aggregations

GoPluginExtension (com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension)7 Test (org.junit.Test)4 PluginSettingsConfiguration (com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration)3 ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)3 GoPluginIdentifier (com.thoughtworks.go.plugin.api.GoPluginIdentifier)2 DefaultGoApiRequest (com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest)2 ValidationError (com.thoughtworks.go.plugin.api.response.validation.ValidationError)2 PluginSettings (com.thoughtworks.go.server.domain.PluginSettings)2 NullPlugin (com.thoughtworks.go.domain.NullPlugin)1 Plugin (com.thoughtworks.go.domain.Plugin)1 Username (com.thoughtworks.go.server.domain.Username)1 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1