-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Renderer for ExtensibleChoiceParameter
- Loading branch information
Showing
7 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...n/java/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.codinjutsu.tools.jenkins.view.parameter; | ||
|
||
import org.codinjutsu.tools.jenkins.model.JobParameter; | ||
import org.codinjutsu.tools.jenkins.model.JobParameterType; | ||
import org.codinjutsu.tools.jenkins.view.extension.JobParameterRenderer; | ||
import org.codinjutsu.tools.jenkins.view.extension.JobParameterRenderers; | ||
import org.jetbrains.annotations.NonNls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiFunction; | ||
|
||
public class ExtensibleChoiceParameterRenderer implements JobParameterRenderer { | ||
|
||
@NonNls | ||
private static final String TYPE_CLASS = "jp.ikedam.jenkins.plugins.extensible_choice_parameter.ExtensibleChoiceParameterDefinition"; | ||
|
||
static final JobParameterType TYPE = new JobParameterType("ExtensibleChoiceParameterDefinition", TYPE_CLASS); | ||
|
||
private final Map<JobParameterType, BiFunction<JobParameter, String, JobParameterComponent<String>>> converter = | ||
new HashMap<>(); | ||
|
||
public ExtensibleChoiceParameterRenderer() { | ||
converter.put(TYPE, JobParameterRenderers::createComboBoxIfChoicesExists); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public JobParameterComponent<String> render(@NotNull JobParameter jobParameter) { | ||
return converter.getOrDefault(jobParameter.getJobParameterType(), JobParameterRenderers::createErrorLabel) | ||
.apply(jobParameter, jobParameter.getDefaultValue()); | ||
} | ||
|
||
@Override | ||
public boolean isForJobParameter(@NotNull JobParameter jobParameter) { | ||
return converter.containsKey(jobParameter.getJobParameterType()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...va/org/codinjutsu/tools/jenkins/view/parameter/ExtensibleChoiceParameterRendererTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.codinjutsu.tools.jenkins.view.parameter; | ||
|
||
import com.intellij.openapi.ui.ComboBox; | ||
import org.codinjutsu.tools.jenkins.model.BuildInJobParameter; | ||
import org.codinjutsu.tools.jenkins.model.JobParameter; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
public class ExtensibleChoiceParameterRendererTest implements JobParameterTest { | ||
|
||
private final ExtensibleChoiceParameterRenderer jobParameterRenderer = new ExtensibleChoiceParameterRenderer(); | ||
|
||
@Test | ||
public void renderAsCombobox() { | ||
JobParameter jobParameter = createJobParameterChoices(ExtensibleChoiceParameterRenderer.TYPE, | ||
"Value1", "Selected", "Value3"); | ||
JobParameterComponent<?> jobParameterComponent = jobParameterRenderer.render(jobParameter); | ||
assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); | ||
assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameter); | ||
|
||
JobParameter jobParameterWithDefault = createJobParameterChoices(ExtensibleChoiceParameterRenderer.TYPE, | ||
"Value1", "Selected", "Value3").toBuilder().defaultValue("Selected").build(); | ||
jobParameterComponent = jobParameterRenderer.render(jobParameterWithDefault); | ||
assertThat(jobParameterComponent.getViewElement()).isInstanceOf(ComboBox.class); | ||
assertThat(jobParameterComponent.getJobParameter()).isEqualTo(jobParameterWithDefault); | ||
final ComboBox<?> comboBox = (ComboBox<?>) jobParameterComponent.getViewElement(); | ||
assertThat(comboBox.getSelectedItem()).isEqualTo("Selected"); | ||
} | ||
|
||
@Test | ||
public void isForJobParameter() { | ||
assertThat(jobParameterRenderer.isForJobParameter(createJobParameter(ExtensibleChoiceParameterRenderer.TYPE))) | ||
.isTrue(); | ||
assertThat(jobParameterRenderer.isForJobParameter(createJobParameter( | ||
BuildInJobParameter.ChoiceParameterDefinition))).isFalse(); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
src/test/resources/org/codinjutsu/tools/jenkins/logic/JobWithExtensibleChoiceParameter.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"_class": "hudson.model.FreeStyleProject", | ||
"description": "", | ||
"displayName": "ExtensibleChoiceParameter", | ||
"displayNameOrNull": null, | ||
"fullDisplayName": "ExtensibleChoiceParameter", | ||
"fullName": "ExtensibleChoiceParameter", | ||
"name": "ExtensibleChoiceParameter", | ||
"url": "https://localhost:8443/view/Parameter%20Builds/job/ExtensibleChoiceParameter/", | ||
"buildable": true, | ||
"builds": [], | ||
"color": "notbuilt", | ||
"firstBuild": null, | ||
"healthReport": [], | ||
"inQueue": false, | ||
"keepDependencies": false, | ||
"lastBuild": null, | ||
"lastCompletedBuild": null, | ||
"lastFailedBuild": null, | ||
"lastStableBuild": null, | ||
"lastSuccessfulBuild": null, | ||
"lastUnstableBuild": null, | ||
"lastUnsuccessfulBuild": null, | ||
"nextBuildNumber": 1, | ||
"property": [ | ||
{ | ||
"_class": "hudson.model.ParametersDefinitionProperty", | ||
"parameterDefinitions": [ | ||
{ | ||
"_class": "jp.ikedam.jenkins.plugins.extensible_choice_parameter.ExtensibleChoiceParameterDefinition", | ||
"defaultParameterValue": { | ||
"_class": "hudson.model.StringParameterValue", | ||
"name": "ExtensibleChoice", | ||
"value": "Test" | ||
}, | ||
"description": null, | ||
"name": "ExtensibleChoice", | ||
"type": "ExtensibleChoiceParameterDefinition", | ||
"choices": [ | ||
"Test", | ||
"Foo", | ||
"Bar" | ||
] | ||
}, | ||
{ | ||
"_class": "hudson.model.ChoiceParameterDefinition", | ||
"defaultParameterValue": { | ||
"_class": "hudson.model.StringParameterValue", | ||
"name": "SimpleChoice", | ||
"value": "Hello" | ||
}, | ||
"description": null, | ||
"name": "SimpleChoice", | ||
"type": "ChoiceParameterDefinition", | ||
"choices": [ | ||
"Hello", | ||
"World" | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"queueItem": null, | ||
"concurrentBuild": false, | ||
"disabled": false, | ||
"downstreamProjects": [], | ||
"labelExpression": null, | ||
"scm": { | ||
"_class": "hudson.scm.NullSCM" | ||
}, | ||
"upstreamProjects": [] | ||
} |