|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dolphinscheduler.api.validator; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 21 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 22 | + |
| 23 | +import org.apache.dolphinscheduler.plugin.task.api.enums.DataType; |
| 24 | +import org.apache.dolphinscheduler.plugin.task.api.enums.Direct; |
| 25 | +import org.apache.dolphinscheduler.plugin.task.api.model.Property; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 33 | +import org.mockito.InjectMocks; |
| 34 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 35 | + |
| 36 | +@ExtendWith(MockitoExtension.class) |
| 37 | +class StartParamListValidatorTest { |
| 38 | + |
| 39 | + @InjectMocks |
| 40 | + private StartParamListValidator startParamListValidator; |
| 41 | + |
| 42 | + @Test |
| 43 | + void testValidate_nullList() { |
| 44 | + assertThatCode(() -> startParamListValidator.validate(null)) |
| 45 | + .doesNotThrowAnyException(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testValidate_emptyList() { |
| 50 | + assertThatCode(() -> startParamListValidator.validate(Collections.emptyList())) |
| 51 | + .doesNotThrowAnyException(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testValidate_validParameters() { |
| 56 | + List<Property> params = Collections.singletonList( |
| 57 | + new Property("workflow_id", Direct.IN, DataType.VARCHAR, "1001")); |
| 58 | + assertThatCode(() -> startParamListValidator.validate(params)) |
| 59 | + .doesNotThrowAnyException(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testValidate_rejectsBlankOrEmptyKeys() { |
| 64 | + assertThrowsIllegalArgument(""); |
| 65 | + |
| 66 | + assertThrowsIllegalArgument(" "); |
| 67 | + |
| 68 | + assertThrowsIllegalArgument("\t"); |
| 69 | + |
| 70 | + assertThrowsIllegalArgument("\n"); |
| 71 | + |
| 72 | + assertThrowsIllegalArgument(" \t\n "); |
| 73 | + } |
| 74 | + |
| 75 | + private void assertThrowsIllegalArgument(String propValue) { |
| 76 | + List<Property> params = Collections.singletonList( |
| 77 | + new Property(propValue, Direct.IN, DataType.VARCHAR, "dummyValue")); |
| 78 | + |
| 79 | + assertThatThrownBy(() -> startParamListValidator.validate(params)) |
| 80 | + .isInstanceOf(IllegalArgumentException.class) |
| 81 | + .hasMessage("Parameter key cannot be empty"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testValidate_duplicateKeys() { |
| 86 | + List<Property> params = new ArrayList<>(); |
| 87 | + params.add(new Property("app_name", Direct.IN, DataType.VARCHAR, "A")); |
| 88 | + params.add(new Property("app_name", Direct.IN, DataType.VARCHAR, "B")); |
| 89 | + |
| 90 | + assertThatThrownBy(() -> startParamListValidator.validate(params)) |
| 91 | + .isInstanceOf(IllegalArgumentException.class) |
| 92 | + .hasMessageContaining("Duplicate parameter key: app_name"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testValidate_duplicateKeysAfterTrim() { |
| 97 | + List<Property> params = new ArrayList<>(); |
| 98 | + params.add(new Property(" app_name ", Direct.IN, DataType.VARCHAR, "A")); |
| 99 | + params.add(new Property("app_name", Direct.IN, DataType.VARCHAR, "B")); |
| 100 | + |
| 101 | + assertThatThrownBy(() -> startParamListValidator.validate(params)) |
| 102 | + .isInstanceOf(IllegalArgumentException.class) |
| 103 | + .hasMessageContaining("Duplicate parameter key: app_name"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testValidate_inTypeEmptyValueAllowed() { |
| 108 | + List<Property> params = Collections.singletonList( |
| 109 | + new Property("input_var", Direct.IN, DataType.VARCHAR, "")); |
| 110 | + |
| 111 | + assertThatCode(() -> startParamListValidator.validate(params)) |
| 112 | + .doesNotThrowAnyException(); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testValidate_outTypeEmptyValueAllowed() { |
| 117 | + List<Property> params = Collections.singletonList( |
| 118 | + new Property("output_var", Direct.OUT, DataType.VARCHAR, "")); |
| 119 | + |
| 120 | + assertThatCode(() -> startParamListValidator.validate(params)) |
| 121 | + .doesNotThrowAnyException(); |
| 122 | + } |
| 123 | +} |
0 commit comments