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()));
}
}
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);
}
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);
}
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);
}
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));
}
Aggregations