Search in sources :

Example 6 with TaskConfigProperty

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

the class JsonBasedTaskExtensionHandler_V1 method convertJsonToTaskConfig.

@Override
public TaskConfig convertJsonToTaskConfig(String configJson) {
    final TaskConfig taskConfig = new TaskConfig();
    ArrayList<String> exceptions = new ArrayList<>();
    try {
        Map<String, Object> configMap = (Map) new GsonBuilder().create().fromJson(configJson, Object.class);
        if (configMap.isEmpty()) {
            exceptions.add("The Json for Task Config cannot be empty");
        }
        for (Map.Entry<String, Object> entry : configMap.entrySet()) {
            TaskConfigProperty property = new TaskConfigProperty(entry.getKey(), null);
            property.with(Property.REQUIRED, true);
            Map propertyValue = (Map) entry.getValue();
            if (propertyValue != null) {
                if (propertyValue.containsKey("default-value")) {
                    if (!(propertyValue.get("default-value") instanceof String)) {
                        exceptions.add(String.format("Key: '%s' - The Json for Task Config should contain a not-null 'default-value' of type String", entry.getKey()));
                    } else {
                        property.withDefault((String) propertyValue.get("default-value"));
                    }
                }
                if (propertyValue.containsKey("display-name")) {
                    if (!(propertyValue.get("display-name") instanceof String)) {
                        exceptions.add(String.format("Key: '%s' - 'display-name' should be of type String", entry.getKey()));
                    } else {
                        property.with(Property.DISPLAY_NAME, (String) propertyValue.get("display-name"));
                    }
                }
                if (propertyValue.containsKey("display-order")) {
                    if (!(propertyValue.get("display-order") instanceof String && StringUtil.isInteger((String) propertyValue.get("display-order")))) {
                        exceptions.add(String.format("Key: '%s' - 'display-order' should be a String containing a numerical value", entry.getKey()));
                    } else {
                        property.with(Property.DISPLAY_ORDER, Integer.parseInt((String) propertyValue.get("display-order")));
                    }
                }
                if (propertyValue.containsKey("secure")) {
                    if (!(propertyValue.get("secure") instanceof Boolean)) {
                        exceptions.add(String.format("Key: '%s' - The Json for Task Config should contain a 'secure' field of type Boolean", entry.getKey()));
                    } else {
                        property.with(Property.SECURE, (Boolean) propertyValue.get("secure"));
                    }
                }
                if (propertyValue.containsKey("required")) {
                    if (!(propertyValue.get("required") instanceof Boolean)) {
                        exceptions.add(String.format("Key: '%s' - The Json for Task Config should contain a 'required' field of type Boolean", entry.getKey()));
                    } else {
                        property.with(Property.REQUIRED, (Boolean) propertyValue.get("required"));
                    }
                }
            }
            taskConfig.add(property);
        }
        if (!exceptions.isEmpty()) {
            throw new RuntimeException(StringUtils.join(exceptions, ", "));
        }
        return taskConfig;
    } catch (Exception e) {
        LOGGER.error("Error occurred while converting the Json to Task Config. Error: {}. The Json received was '{}'.", e.getMessage(), configJson);
        throw new RuntimeException(String.format("Error occurred while converting the Json to Task Config. Error: %s.", e.getMessage()));
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) TaskConfigProperty(com.thoughtworks.go.plugin.api.task.TaskConfigProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with TaskConfigProperty

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

the class PluggableTaskServiceTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    taskExtension = mock(TaskExtension.class);
    pluggableTaskService = new PluggableTaskService(taskExtension);
    final TaskPreference preference = mock(TaskPreference.class);
    final TaskConfig taskConfig = new TaskConfig();
    final TaskConfigProperty key1 = taskConfig.addProperty("KEY1");
    key1.with(Property.REQUIRED, true);
    taskConfig.addProperty("KEY2");
    when(preference.getConfig()).thenReturn(taskConfig);
    PluggableTaskConfigStore.store().setPreferenceFor(pluginId, preference);
}
Also used : TaskExtension(com.thoughtworks.go.plugin.access.pluggabletask.TaskExtension) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) TaskConfigProperty(com.thoughtworks.go.plugin.api.task.TaskConfigProperty) TaskPreference(com.thoughtworks.go.plugin.access.pluggabletask.TaskPreference) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with TaskConfigProperty

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

the class PluggableTaskPluginInfoBuilderTest method pluginInfoFor_ShouldProvidePluginInfoForAPlugin.

@Test
public void pluginInfoFor_ShouldProvidePluginInfoForAPlugin() throws Exception {
    GoPluginDescriptor.About about = new GoPluginDescriptor.About("Plugin Descriptor Validator", "1.0.1", "12.4", "Validates its own plugin descriptor", new GoPluginDescriptor.Vendor("ThoughtWorks Go Team", "www.thoughtworks.com"), Arrays.asList("Linux", "Windows", "Mac OS X"));
    GoPluginDescriptor plugin = new GoPluginDescriptor("docker-plugin", "1.0", about, null, null, false);
    PluginManager pluginManager = mock(PluginManager.class);
    PluggableTaskConfigStore packageMetadataStore = mock(PluggableTaskConfigStore.class);
    when(packageMetadataStore.pluginIds()).thenReturn(Collections.singleton(plugin.id()));
    when(pluginManager.getPluginDescriptorFor(plugin.id())).thenReturn(plugin);
    JsonBasedPluggableTask jsonBasedPluggableTask = mock(JsonBasedPluggableTask.class);
    TaskView taskView = new TaskView() {

        @Override
        public String displayValue() {
            return "task display value";
        }

        @Override
        public String template() {
            return "pluggable task view template";
        }
    };
    TaskConfig taskConfig = new TaskConfig();
    taskConfig.add(new TaskConfigProperty("key1", null));
    taskConfig.add(new TaskConfigProperty("key2", null));
    when(jsonBasedPluggableTask.config()).thenReturn(taskConfig);
    when(jsonBasedPluggableTask.view()).thenReturn(taskView);
    TaskPreference taskPreference = new TaskPreference(jsonBasedPluggableTask);
    when(packageMetadataStore.preferenceFor(plugin.id())).thenReturn(taskPreference);
    PluggableTaskPluginInfoBuilder builder = new PluggableTaskPluginInfoBuilder(pluginManager, packageMetadataStore);
    PluggableTaskPluginInfo pluginInfo = builder.pluginInfoFor(plugin.id());
    PluggableTaskPluginInfo expectedPluginInfo = new PluggableTaskPluginInfo(plugin, taskView.displayValue(), new PluggableInstanceSettings(configurations(taskConfig), new PluginView(taskView.template())));
    assertEquals(expectedPluginInfo, pluginInfo);
}
Also used : TaskView(com.thoughtworks.go.plugin.api.task.TaskView) JsonBasedPluggableTask(com.thoughtworks.go.plugin.access.pluggabletask.JsonBasedPluggableTask) PluggableTaskPluginInfo(com.thoughtworks.go.server.ui.plugins.PluggableTaskPluginInfo) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) TaskConfigProperty(com.thoughtworks.go.plugin.api.task.TaskConfigProperty) PluginManager(com.thoughtworks.go.plugin.infra.PluginManager) PluggableInstanceSettings(com.thoughtworks.go.server.ui.plugins.PluggableInstanceSettings) PluginView(com.thoughtworks.go.server.ui.plugins.PluginView) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) PluggableTaskConfigStore(com.thoughtworks.go.plugin.access.pluggabletask.PluggableTaskConfigStore) TaskPreference(com.thoughtworks.go.plugin.access.pluggabletask.TaskPreference) Test(org.junit.Test)

Example 9 with TaskConfigProperty

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

the class PluggableTaskViewModelBuilderTest method setUp.

@Before
public void setUp() {
    initMocks(this);
    builder = new PluggableTaskViewModelBuilder(manager);
    xunitConvertor = new GoPluginDescriptor("xunit.convertor", "version1", new GoPluginDescriptor.About("Xunit Convertor", "1.0", null, null, null, null), null, null, false);
    powershellTask = new GoPluginDescriptor("powershell.task", "version1", new GoPluginDescriptor.About("Powershell Task", "2.0", null, null, null, null), null, null, false);
    JsonBasedPluggableTask jsonBasedPluggableTask = mock(JsonBasedPluggableTask.class);
    TaskView taskView = new TaskView() {

        @Override
        public String displayValue() {
            return "task display value";
        }

        @Override
        public String template() {
            return "pluggable task view template";
        }
    };
    TaskConfig taskConfig = new TaskConfig();
    taskConfig.add(new TaskConfigProperty("key1", null));
    taskConfig.add(new TaskConfigProperty("key2", null));
    when(jsonBasedPluggableTask.config()).thenReturn(taskConfig);
    when(jsonBasedPluggableTask.view()).thenReturn(taskView);
    taskPreference = new TaskPreference(jsonBasedPluggableTask);
    PluggableTaskConfigStore.store().setPreferenceFor("xunit.convertor", taskPreference);
    PluggableTaskConfigStore.store().setPreferenceFor("powershell.task", taskPreference);
}
Also used : TaskView(com.thoughtworks.go.plugin.api.task.TaskView) JsonBasedPluggableTask(com.thoughtworks.go.plugin.access.pluggabletask.JsonBasedPluggableTask) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) TaskConfigProperty(com.thoughtworks.go.plugin.api.task.TaskConfigProperty) TaskPreference(com.thoughtworks.go.plugin.access.pluggabletask.TaskPreference) Before(org.junit.Before)

Example 10 with TaskConfigProperty

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

the class PluggableTaskPluginInfoBuilderTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    extension = mock(TaskExtension.class);
    TaskConfig taskConfig = new TaskConfig();
    taskConfig.add(new TaskConfigProperty("username", null).with(Property.REQUIRED, true).with(Property.SECURE, false));
    taskConfig.add(new TaskConfigProperty("password", null).with(Property.REQUIRED, true).with(Property.SECURE, true));
    TaskView taskView = new TaskView() {

        @Override
        public String displayValue() {
            return "my task plugin";
        }

        @Override
        public String template() {
            return "some html";
        }
    };
    final Task task = mock(Task.class);
    when(task.config()).thenReturn(taskConfig);
    when(task.view()).thenReturn(taskView);
    final GoPluginDescriptor descriptor = mock(GoPluginDescriptor.class);
    String pluginId = "plugin1";
    when(descriptor.id()).thenReturn(pluginId);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            final Action<Task> action = (Action<Task>) invocation.getArguments()[1];
            action.execute(task, descriptor);
            return null;
        }
    }).when(extension).doOnTask(eq("plugin1"), any(Action.class));
}
Also used : TaskView(com.thoughtworks.go.plugin.api.task.TaskView) Task(com.thoughtworks.go.plugin.api.task.Task) Action(com.thoughtworks.go.plugin.infra.Action) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) TaskConfigProperty(com.thoughtworks.go.plugin.api.task.TaskConfigProperty) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoPluginDescriptor(com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

TaskConfigProperty (com.thoughtworks.go.plugin.api.task.TaskConfigProperty)12 TaskConfig (com.thoughtworks.go.plugin.api.task.TaskConfig)8 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)6 TaskPreference (com.thoughtworks.go.plugin.access.pluggabletask.TaskPreference)5 TaskView (com.thoughtworks.go.plugin.api.task.TaskView)4 Test (org.junit.Test)4 JsonBasedPluggableTask (com.thoughtworks.go.plugin.access.pluggabletask.JsonBasedPluggableTask)3 Property (com.thoughtworks.go.plugin.api.config.Property)3 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)2 PluginConfiguration (com.thoughtworks.go.domain.config.PluginConfiguration)2 PluggableTaskConfigStore (com.thoughtworks.go.plugin.access.pluggabletask.PluggableTaskConfigStore)2 ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)2 PluginManager (com.thoughtworks.go.plugin.infra.PluginManager)2 PluggableInstanceSettings (com.thoughtworks.go.server.ui.plugins.PluggableInstanceSettings)2 PluggableTaskPluginInfo (com.thoughtworks.go.server.ui.plugins.PluggableTaskPluginInfo)2 PluginView (com.thoughtworks.go.server.ui.plugins.PluginView)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 GsonBuilder (com.google.gson.GsonBuilder)1 Configuration (com.thoughtworks.go.domain.config.Configuration)1 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)1