|
23 | 23 | */ |
24 | 24 | package io.jenkins.plugins.validating_yaml_parameter; |
25 | 25 |
|
| 26 | +import hudson.AbortException; |
26 | 27 | import hudson.cli.CLICommand; |
27 | 28 | import hudson.model.Failure; |
| 29 | +import hudson.model.Item; |
| 30 | +import hudson.model.StringParameterValue; |
| 31 | +import hudson.util.FormValidation; |
28 | 32 | import net.sf.json.JSONObject; |
29 | 33 | import org.junit.jupiter.api.Test; |
30 | 34 | import org.junit.jupiter.api.extension.ExtendWith; |
|
33 | 37 | import org.mockito.Mockito; |
34 | 38 | import org.mockito.junit.jupiter.MockitoExtension; |
35 | 39 |
|
36 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
37 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
| 40 | +import java.io.IOException; |
| 41 | +import static org.junit.jupiter.api.Assertions.*; |
| 42 | +import static org.mockito.Mockito.mock; |
38 | 43 |
|
39 | 44 | /** |
40 | 45 | * |
@@ -80,4 +85,225 @@ void failedCreateValueJSONObject() { |
80 | 85 | assertThrows(Failure.class, () -> d.createValue(req, jo)); |
81 | 86 | } |
82 | 87 |
|
| 88 | + @Test |
| 89 | + void testGetDefaultParameterValue() { |
| 90 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 91 | + "DUMMY", "default: value", "error", "description"); |
| 92 | + ValidatingYamlParameterValue v = d.getDefaultParameterValue(); |
| 93 | + assertEquals("DUMMY", v.getName()); |
| 94 | + assertEquals("default: value", v.getValue()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testCreateValueWithNullCLICommand() throws IOException, InterruptedException { |
| 99 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 100 | + "DUMMY", "default: value", "error", "description"); |
| 101 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(cliCommand, null); |
| 102 | + assertEquals(d.getDefaultParameterValue().getValue(), v.getValue()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testCreateValueWithEmptyCLICommand() throws IOException, InterruptedException { |
| 107 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 108 | + "DUMMY", "default: value", "error", "description"); |
| 109 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(cliCommand, ""); |
| 110 | + assertEquals(d.getDefaultParameterValue().getValue(), v.getValue()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testCreateValueWithValidCLICommand() throws IOException, InterruptedException { |
| 115 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 116 | + "DUMMY", "default: value", "error", "description"); |
| 117 | + String yaml = "key: value"; |
| 118 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(cliCommand, yaml); |
| 119 | + assertEquals(yaml, v.getValue()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void testCreateValueWithInvalidCLICommand() { |
| 124 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 125 | + "DUMMY", "default: value", "error", "description"); |
| 126 | + String invalidYaml = "key: : value"; |
| 127 | + assertThrows(AbortException.class, () -> |
| 128 | + d.createValue(cliCommand, invalidYaml) |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testCopyWithDefaultValue() { |
| 134 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 135 | + "DUMMY", "default: value", "error", "description"); |
| 136 | + ValidatingYamlParameterValue v = new ValidatingYamlParameterValue("DUMMY", "new: value"); |
| 137 | + ValidatingYamlParameterDefinition copied = (ValidatingYamlParameterDefinition) d.copyWithDefaultValue(v); |
| 138 | + |
| 139 | + assertEquals(d.getName(), copied.getName()); |
| 140 | + assertEquals("new: value", copied.getDefaultValue()); |
| 141 | + assertEquals(d.getFailedValidationMessage(), copied.getFailedValidationMessage()); |
| 142 | + assertEquals(d.getDescription(), copied.getDescription()); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void testCopyWithNonMatchingDefaultValue() { |
| 147 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 148 | + "DUMMY", "default: value", "error", "description"); |
| 149 | + StringParameterValue v = new StringParameterValue("DUMMY", "new value"); |
| 150 | + ValidatingYamlParameterDefinition copied = (ValidatingYamlParameterDefinition) d.copyWithDefaultValue(v); |
| 151 | + |
| 152 | + assertSame(d, copied); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void testDescriptorDisplayName() { |
| 157 | + ValidatingYamlParameterDefinition.DescriptorImpl descriptor = new ValidatingYamlParameterDefinition.DescriptorImpl(); |
| 158 | + assertEquals("Validating Yaml Parameter", descriptor.getDisplayName()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void testDescriptorValidateWithNullItem() { |
| 163 | + ValidatingYamlParameterDefinition.DescriptorImpl descriptor = new ValidatingYamlParameterDefinition.DescriptorImpl(); |
| 164 | + FormValidation validation = descriptor.doValidate("key: value", "error", null); |
| 165 | + assertEquals(FormValidation.Kind.OK, validation.kind); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + void testDescriptorValidateWithValidYaml() { |
| 170 | + ValidatingYamlParameterDefinition.DescriptorImpl descriptor = new ValidatingYamlParameterDefinition.DescriptorImpl(); |
| 171 | + Item item = mock(Item.class); |
| 172 | + FormValidation validation = descriptor.doValidate("key: value", "error", item); |
| 173 | + assertEquals(FormValidation.Kind.OK, validation.kind); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + void testDescriptorValidateWithInvalidYaml() { |
| 178 | + ValidatingYamlParameterDefinition.DescriptorImpl descriptor = new ValidatingYamlParameterDefinition.DescriptorImpl(); |
| 179 | + Item item = mock(Item.class); |
| 180 | + FormValidation validation = descriptor.doValidate("key: : value", "Custom error", item); |
| 181 | + assertEquals(FormValidation.Kind.ERROR, validation.kind); |
| 182 | + assertEquals("Custom error", validation.getMessage()); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + void testDescriptorValidateWithInvalidYamlNoCustomMessage() { |
| 187 | + ValidatingYamlParameterDefinition.DescriptorImpl descriptor = new ValidatingYamlParameterDefinition.DescriptorImpl(); |
| 188 | + Item item = mock(Item.class); |
| 189 | + FormValidation validation = descriptor.doValidate("key: : value", "", item); |
| 190 | + assertEquals(FormValidation.Kind.ERROR, validation.kind); |
| 191 | + assertTrue(validation.getMessage().startsWith("Invalid yaml string:")); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + void testCreateValueWithNullParameterValues() { |
| 196 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 197 | + "DUMMY", "default: value", "error", "description"); |
| 198 | + Mockito.when(req.getParameterValues("DUMMY")).thenReturn(null); |
| 199 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(req); |
| 200 | + assertEquals(d.getDefaultParameterValue().getValue(), v.getValue()); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + void testCreateValueWithEmptyParameterValues() { |
| 205 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 206 | + "DUMMY", "default: value", "error", "description"); |
| 207 | + Mockito.when(req.getParameterValues("DUMMY")).thenReturn(new String[0]); |
| 208 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(req); |
| 209 | + assertEquals(d.getDefaultParameterValue().getValue(), v.getValue()); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + void testDescriptorValidateWithNullValue() { |
| 214 | + ValidatingYamlParameterDefinition.DescriptorImpl descriptor = new ValidatingYamlParameterDefinition.DescriptorImpl(); |
| 215 | + Item item = mock(Item.class); |
| 216 | + FormValidation validation = descriptor.doValidate(null, "error", item); |
| 217 | + assertEquals(FormValidation.Kind.ERROR, validation.kind); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + void testGetValue() { |
| 222 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 223 | + "DUMMY", "default: value", "error", "description"); |
| 224 | + assertEquals("default: value", d.getValue()); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + void testDescriptorValidateWithComplexYaml() { |
| 229 | + ValidatingYamlParameterDefinition.DescriptorImpl descriptor = new ValidatingYamlParameterDefinition.DescriptorImpl(); |
| 230 | + Item item = mock(Item.class); |
| 231 | + String complexYaml = "---\n" + |
| 232 | + "key1: value1\n" + |
| 233 | + "key2:\n" + |
| 234 | + " nested: value2\n" + |
| 235 | + " array:\n" + |
| 236 | + " - item1\n" + |
| 237 | + " - item2\n"; |
| 238 | + FormValidation validation = descriptor.doValidate(complexYaml, "error", item); |
| 239 | + assertEquals(FormValidation.Kind.OK, validation.kind); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + void testDescriptorValidateWithInvalidComplexYaml() { |
| 244 | + ValidatingYamlParameterDefinition.DescriptorImpl descriptor = new ValidatingYamlParameterDefinition.DescriptorImpl(); |
| 245 | + Item item = mock(Item.class); |
| 246 | + String invalidComplexYaml = "---\n" + |
| 247 | + "key1: value1\n" + |
| 248 | + "key2:\n" + |
| 249 | + " nested: value2\n" + |
| 250 | + " array:\n" + |
| 251 | + " - item1\n" + |
| 252 | + " - item2\n" + |
| 253 | + " invalid: : value\n"; |
| 254 | + FormValidation validation = descriptor.doValidate(invalidComplexYaml, "Custom error", item); |
| 255 | + assertEquals(FormValidation.Kind.ERROR, validation.kind); |
| 256 | + assertEquals("Custom error", validation.getMessage()); |
| 257 | + } |
| 258 | + |
| 259 | + @Test |
| 260 | + void testCreateValueWithSpecialCharacters() throws IOException, InterruptedException { |
| 261 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 262 | + "DUMMY", "default: value", "error", "description"); |
| 263 | + String yamlWithSpecialChars = "key: 'value with special chars: !@#$%^&*()'"; |
| 264 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(cliCommand, yamlWithSpecialChars); |
| 265 | + assertEquals(yamlWithSpecialChars, v.getValue()); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + void testCreateValueWithUnicodeCharacters() throws IOException, InterruptedException { |
| 270 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 271 | + "DUMMY", "default: value", "error", "description"); |
| 272 | + String yamlWithUnicode = "key: 'value with unicode: 你好世界'"; |
| 273 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(cliCommand, yamlWithUnicode); |
| 274 | + assertEquals(yamlWithUnicode, v.getValue()); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + void testCreateValueWithComments() throws IOException, InterruptedException { |
| 279 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 280 | + "DUMMY", "default: value", "error", "description"); |
| 281 | + String yamlWithComments = "# This is a comment\nkey: value # Inline comment"; |
| 282 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(cliCommand, yamlWithComments); |
| 283 | + assertEquals(yamlWithComments, v.getValue()); |
| 284 | + } |
| 285 | + |
| 286 | + @Test |
| 287 | + void testCreateValueWithEmptyOrNullInput() throws IOException, InterruptedException { |
| 288 | + ValidatingYamlParameterDefinition d = new ValidatingYamlParameterDefinition( |
| 289 | + "DUMMY", "default: value", "error", "description"); |
| 290 | + |
| 291 | + // Test null CLI command |
| 292 | + ValidatingYamlParameterValue v = (ValidatingYamlParameterValue) d.createValue(cliCommand, null); |
| 293 | + assertEquals(d.getDefaultParameterValue().getValue(), v.getValue()); |
| 294 | + |
| 295 | + // Test empty CLI command |
| 296 | + v = (ValidatingYamlParameterValue) d.createValue(cliCommand, ""); |
| 297 | + assertEquals(d.getDefaultParameterValue().getValue(), v.getValue()); |
| 298 | + |
| 299 | + // Test null parameter values |
| 300 | + Mockito.when(req.getParameterValues("DUMMY")).thenReturn(null); |
| 301 | + v = (ValidatingYamlParameterValue) d.createValue(req); |
| 302 | + assertEquals(d.getDefaultParameterValue().getValue(), v.getValue()); |
| 303 | + |
| 304 | + // Test empty parameter values |
| 305 | + Mockito.when(req.getParameterValues("DUMMY")).thenReturn(new String[0]); |
| 306 | + v = (ValidatingYamlParameterValue) d.createValue(req); |
| 307 | + assertEquals(d.getDefaultParameterValue().getValue(), v.getValue()); |
| 308 | + } |
83 | 309 | } |
0 commit comments