From 7f376a04742f9ef0c2f1f150c55ed8b18ee42d40 Mon Sep 17 00:00:00 2001 From: aman-agrawal <9412470@gmail.com> Date: Mon, 22 Jul 2024 14:39:46 +0530 Subject: [PATCH] refactor(validator): moved validator groovy unit tests to java --- .../ApplicationNameValidatorSpec.groovy | 103 ----------------- .../HasCanonicalPluginIdValidatorSpec.groovy | 50 --------- .../validator/HasEmailValidatorSpec.groovy | 51 --------- .../validator/HasNameValidatorSpec.groovy | 55 --------- ...HasValidRequiresFieldsValidatorSpec.groovy | 53 --------- .../ApplicationNameValidatorTest.java | 104 ++++++++++++++++++ .../HasCanonicalPluginIdValidatorTest.java | 51 +++++++++ .../validator/HasEmailValidatorTest.java | 53 +++++++++ .../validator/HasNameValidatorTest.java | 53 +++++++++ .../HasValidRequiresFieldsValidatorTest.java | 58 ++++++++++ 10 files changed, 319 insertions(+), 312 deletions(-) delete mode 100644 front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/ApplicationNameValidatorSpec.groovy delete mode 100644 front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasCanonicalPluginIdValidatorSpec.groovy delete mode 100644 front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasEmailValidatorSpec.groovy delete mode 100644 front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasNameValidatorSpec.groovy delete mode 100644 front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasValidRequiresFieldsValidatorSpec.groovy create mode 100644 front50-core/src/test/java/com/netflix/spinnaker/front50/validator/ApplicationNameValidatorTest.java create mode 100644 front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasCanonicalPluginIdValidatorTest.java create mode 100644 front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasEmailValidatorTest.java create mode 100644 front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasNameValidatorTest.java create mode 100644 front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasValidRequiresFieldsValidatorTest.java diff --git a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/ApplicationNameValidatorSpec.groovy b/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/ApplicationNameValidatorSpec.groovy deleted file mode 100644 index f1575efda..000000000 --- a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/ApplicationNameValidatorSpec.groovy +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2020 Netflix, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -package com.netflix.spinnaker.front50.validator - -import com.netflix.spinnaker.front50.model.application.Application -import spock.lang.Specification -import spock.lang.Unroll - -class ApplicationNameValidatorSpec extends Specification { - - @Unroll - def "validates #appName isValid: #isValid"() { - setup: - ApplicationNameValidatorConfigurationProperties properties = new ApplicationNameValidatorConfigurationProperties() - properties.setValidationRegex('^[a-zA-Z0-9.\\-_]*$') - ApplicationNameValidator validator = new ApplicationNameValidator(properties) - def application = new Application() - def errors = new ApplicationValidationErrors(application) - - when: - application.name = appName - validator.validate(application, errors) - - then: - errors.getAllErrors().size() == (isValid ? 0 : 1) - - where: - appName || isValid - "validname" || true - "valid1name" || true - "valid-name" || true - "valid1.name" || true - "valid-1_name" || true - "invalid!name" || false - "invalid name" || false - "invalid.имя" || false - } - - @Unroll - def "does not validate when no regex supplied"() { - setup: - def application = new Application() - application.name = 'søme wéird name!' - def errors = new ApplicationValidationErrors(application) - - when: - ApplicationNameValidatorConfigurationProperties properties = new ApplicationNameValidatorConfigurationProperties() - properties.setValidationRegex('') - ApplicationNameValidator validator = new ApplicationNameValidator(properties) - validator.validate(application, errors) - - then: - errors.getAllErrors().size() == 0 - - when: - validator = new ApplicationNameValidator(new ApplicationNameValidatorConfigurationProperties()) - validator.validate(application, errors) - - then: - errors.getAllErrors().size() == 0 - } - - def "uses optional error message"() { - setup: - def application = new Application() - application.name = 'noncompliantname!' - def errors = new ApplicationValidationErrors(application) - - when: - ApplicationNameValidatorConfigurationProperties properties = new ApplicationNameValidatorConfigurationProperties() - properties.setValidationRegex('strictname') - ApplicationNameValidator validator = new ApplicationNameValidator(properties) - validator.validate(application, errors) - - then: - errors.getAllErrors().size() == 1 - errors.getAllErrors()[0].defaultMessage == "Application name doesn't satisfy the validation regex: " + properties.getValidationRegex() - - when: - errors = new ApplicationValidationErrors(application) - properties.setValidationMessage("a validation message") - validator.validate(application, errors) - - then: - errors.getAllErrors().size() == 1 - errors.getAllErrors()[0].defaultMessage == properties.getValidationMessage() - } -} diff --git a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasCanonicalPluginIdValidatorSpec.groovy b/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasCanonicalPluginIdValidatorSpec.groovy deleted file mode 100644 index e6d18417e..000000000 --- a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasCanonicalPluginIdValidatorSpec.groovy +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2019 Netflix, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.netflix.spinnaker.front50.validator - -import com.netflix.spinnaker.front50.model.plugins.PluginInfo -import org.springframework.validation.Errors -import spock.lang.Specification -import spock.lang.Subject -import spock.lang.Unroll - -class HasCanonicalPluginIdValidatorSpec extends Specification { - - @Subject - HasCanonicalPluginIdValidator subject = new HasCanonicalPluginIdValidator() - - @Unroll - def "requires a canonical plugin id"() { - setup: - PluginInfo pluginInfo = new PluginInfo(id: id) - Errors errors = new GenericValidationErrors(pluginInfo) - - when: - subject.validate(pluginInfo, errors) - - then: - errors.hasErrors() == hasErrors - - where: - id || hasErrors - "foo" || true - "foo/bar" || true - "foo.bar" || false - "." || true - ".bar" || true - "foo." || true - } -} diff --git a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasEmailValidatorSpec.groovy b/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasEmailValidatorSpec.groovy deleted file mode 100644 index 174912aff..000000000 --- a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasEmailValidatorSpec.groovy +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2014 Netflix, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -package com.netflix.spinnaker.front50.validator - -import com.netflix.spinnaker.front50.model.application.Application -import spock.lang.Specification -import spock.lang.Subject -import spock.lang.Unroll - -class HasEmailValidatorSpec extends Specification { - @Subject - ApplicationValidator validator = new HasEmailValidator() - - @Unroll - def "'#email' is #description email"() { - setup: - def application = new Application() - def errors = new ApplicationValidationErrors(application) - - when: - application.email = email - validator.validate(application, errors) - - then: - errors.getAllErrors().size() == numberOfErrors - - where: - email | numberOfErrors - null | 1 - "" | 1 - " " | 1 - "email@netflix.com" | 0 - - description = (numberOfErrors > 0) ? "an empty" : "a non-empty" - } -} \ No newline at end of file diff --git a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasNameValidatorSpec.groovy b/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasNameValidatorSpec.groovy deleted file mode 100644 index 3705822eb..000000000 --- a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasNameValidatorSpec.groovy +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2014 Netflix, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -package com.netflix.spinnaker.front50.validator - -import com.netflix.spinnaker.front50.model.application.Application -import spock.lang.Specification -import spock.lang.Subject - -class HasNameValidatorSpec extends Specification { - @Subject - HasNameValidator validator = new HasNameValidator() - - def "requires a non-null name"() { - setup: - def application = new Application() - def errors = new ApplicationValidationErrors(application) - - when: - validator.validate(application, errors) - - then: - errors.getAllErrors().size() == 1 - - when: - application.name = " " - errors = new ApplicationValidationErrors(application) - validator.validate(application, errors) - - then: - errors.getAllErrors().size() == 1 - - when: - application.name = "application" - errors = new ApplicationValidationErrors(application) - validator.validate(application, errors) - - then: - !errors.hasErrors() - } -} \ No newline at end of file diff --git a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasValidRequiresFieldsValidatorSpec.groovy b/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasValidRequiresFieldsValidatorSpec.groovy deleted file mode 100644 index 4b96d28d6..000000000 --- a/front50-core/src/test/groovy/com/netflix/spinnaker/front50/validator/HasValidRequiresFieldsValidatorSpec.groovy +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2019 Netflix, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.netflix.spinnaker.front50.validator - -import com.netflix.spinnaker.front50.model.plugins.PluginInfo -import spock.lang.Specification -import spock.lang.Subject -import spock.lang.Unroll - -class HasValidRequiresFieldsValidatorSpec extends Specification { - - @Subject - HasValidRequiresFieldsValidator subject = new HasValidRequiresFieldsValidator() - - @Unroll - def "requires release with valid requires field formatting #requiresValue has errors #hasErrors"() { - setup: - def pluginInfo = new PluginInfo( - releases: [new PluginInfo.Release(requires: requiresValue)] - ) - def errors = new GenericValidationErrors(pluginInfo) - - when: - subject.validate(pluginInfo, errors) - println errors - - then: - errors.hasErrors() == hasErrors - - where: - requiresValue || hasErrors - "gate<=1.0.0,echo>=1.0.0" || false - "gate<=1.0.0, echo>=1.0.0" || false - "gate>=1.0.0" || false - "gate<1.0.0" || false - "hello-world=1.0.0" || false - "gate=1.0.0" || false - "gate=foo" || true - } -} diff --git a/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/ApplicationNameValidatorTest.java b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/ApplicationNameValidatorTest.java new file mode 100644 index 000000000..477609f58 --- /dev/null +++ b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/ApplicationNameValidatorTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2020 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.spinnaker.front50.validator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.netflix.spinnaker.front50.model.application.Application; +import java.util.stream.Stream; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class ApplicationNameValidatorTest { + + @ParameterizedTest(name = "validates {0} isValid: {1}") + @MethodSource("appNameProvider") + @DisplayName("AppName validation test") + public void testAppNameValidation(String appName, boolean isValid) { + ApplicationNameValidatorConfigurationProperties properties = + new ApplicationNameValidatorConfigurationProperties(); + properties.setValidationRegex("^[a-zA-Z0-9.\\-_]*$"); + ApplicationNameValidator validator = new ApplicationNameValidator(properties); + Application application = new Application(); + application.setName(appName); + ApplicationValidationErrors errors = new ApplicationValidationErrors(application); + + validator.validate(application, errors); + + assertEquals(isValid ? 0 : 1, errors.getAllErrors().size()); + } + + @Test + void doesNotValidateWhenNoRegexSupplied() { + Application application = new Application(); + application.setName("søme wéird name!"); + ApplicationValidationErrors errors = new ApplicationValidationErrors(application); + + ApplicationNameValidatorConfigurationProperties properties = + new ApplicationNameValidatorConfigurationProperties(); + properties.setValidationRegex(""); + ApplicationNameValidator validator = new ApplicationNameValidator(properties); + validator.validate(application, errors); + + assertEquals(0, errors.getAllErrors().size()); + + validator = new ApplicationNameValidator(new ApplicationNameValidatorConfigurationProperties()); + validator.validate(application, errors); + + assertEquals(0, errors.getAllErrors().size()); + } + + @Test + void usesOptionalErrorMessage() { + Application application = new Application(); + application.setName("noncompliantname!"); + ApplicationValidationErrors errors = new ApplicationValidationErrors(application); + + ApplicationNameValidatorConfigurationProperties properties = + new ApplicationNameValidatorConfigurationProperties(); + properties.setValidationRegex("strictname"); + ApplicationNameValidator validator = new ApplicationNameValidator(properties); + validator.validate(application, errors); + + assertEquals(1, errors.getAllErrors().size()); + assertEquals( + "Application name doesn't satisfy the validation regex: " + properties.getValidationRegex(), + errors.getAllErrors().get(0).getDefaultMessage()); + + errors = new ApplicationValidationErrors(application); + properties.setValidationMessage("a validation message"); + validator.validate(application, errors); + + assertEquals(1, errors.getAllErrors().size()); + assertEquals( + properties.getValidationMessage(), errors.getAllErrors().get(0).getDefaultMessage()); + } + + private static Stream appNameProvider() { + return Stream.of( + new Object[] {"validname", true}, + new Object[] {"valid1name", true}, + new Object[] {"valid-name", true}, + new Object[] {"valid1.name", true}, + new Object[] {"valid-1_name", true}, + new Object[] {"invalid!name", false}, + new Object[] {"invalid name", false}, + new Object[] {"invalid.имя", false}); + } +} diff --git a/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasCanonicalPluginIdValidatorTest.java b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasCanonicalPluginIdValidatorTest.java new file mode 100644 index 000000000..6f6603da7 --- /dev/null +++ b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasCanonicalPluginIdValidatorTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2019 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.spinnaker.front50.validator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.netflix.spinnaker.front50.model.plugins.PluginInfo; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.validation.Errors; + +public class HasCanonicalPluginIdValidatorTest { + private final HasCanonicalPluginIdValidator validator = new HasCanonicalPluginIdValidator(); + + @ParameterizedTest + @MethodSource("pluginIdProvider") + public void requiresCanonicalPluginId(final String id, final boolean hasErrors) { + PluginInfo pluginInfo = new PluginInfo(); + pluginInfo.setId(id); + Errors errors = new GenericValidationErrors(pluginInfo); + + validator.validate(pluginInfo, errors); + + assertEquals(hasErrors, errors.hasErrors()); + } + + private static Stream pluginIdProvider() { + return Stream.of( + new Object[] {"foo", true}, + new Object[] {"foo/bar", true}, + new Object[] {"foo.bar", false}, + new Object[] {".", true}, + new Object[] {".bar", true}, + new Object[] {"foo.", true}); + } +} diff --git a/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasEmailValidatorTest.java b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasEmailValidatorTest.java new file mode 100644 index 000000000..77f8c9bbd --- /dev/null +++ b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasEmailValidatorTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.spinnaker.front50.validator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.netflix.spinnaker.front50.model.application.Application; +import java.util.stream.Stream; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class HasEmailValidatorTest { + private final ApplicationValidator validator = new HasEmailValidator(); + + @ParameterizedTest(name = "{0} is {2} email") + @MethodSource("emailProvider") + @DisplayName("Email validation test") + public void testEmailValidation(String email, int numberOfErrors) { + Application application = new Application(); + ApplicationValidationErrors errors = new ApplicationValidationErrors(application); + + application.setEmail(email); + validator.validate(application, errors); + + assertEquals( + numberOfErrors, + errors.getAllErrors().size(), + String.format("Expected %d error(s) for email: '%s'", numberOfErrors, email)); + } + + private static Stream emailProvider() { + return Stream.of( + new Object[] {null, 1, "an empty"}, + new Object[] {"", 1, "an empty"}, + new Object[] {" ", 1, "an empty"}, + new Object[] {"email@netflix.com", 0, "a non-empty"}); + } +} diff --git a/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasNameValidatorTest.java b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasNameValidatorTest.java new file mode 100644 index 000000000..f39795baa --- /dev/null +++ b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasNameValidatorTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.spinnaker.front50.validator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.netflix.spinnaker.front50.model.application.Application; +import java.util.stream.Stream; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class HasNameValidatorTest { + private final HasNameValidator validator = new HasNameValidator(); + + @ParameterizedTest(name = "Name validation: {0} should result in {1} error(s)") + @MethodSource("nameProvider") + @DisplayName("Name validation test") + public void testNameValidation(String name, int numberOfErrors) { + Application application = new Application(); + ApplicationValidationErrors errors = new ApplicationValidationErrors(application); + + application.setName(name); + validator.validate(application, errors); + + assertEquals( + numberOfErrors, + errors.getAllErrors().size(), + String.format("Expected %d error(s) for name: '%s'", numberOfErrors, name)); + } + + private static Stream nameProvider() { + return Stream.of( + new Object[] {null, 1}, + new Object[] {"", 1}, + new Object[] {" ", 1}, + new Object[] {"application", 0}); + } +} diff --git a/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasValidRequiresFieldsValidatorTest.java b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasValidRequiresFieldsValidatorTest.java new file mode 100644 index 000000000..505592749 --- /dev/null +++ b/front50-core/src/test/java/com/netflix/spinnaker/front50/validator/HasValidRequiresFieldsValidatorTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2019 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.spinnaker.front50.validator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.netflix.spinnaker.front50.model.plugins.PluginInfo; +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +public class HasValidRequiresFieldsValidatorTest { + private HasValidRequiresFieldsValidator validator = new HasValidRequiresFieldsValidator(); + + @ParameterizedTest( + name = "requires release with valid requires field formatting {0} has errors {1}") + @MethodSource("fieldsProvider") + @DisplayName("Field validation test") + public void testFieldsValidation(String requiresValue, boolean hasErrors) { + PluginInfo pluginInfo = new PluginInfo(); + PluginInfo.Release release = new PluginInfo.Release(); + release.setRequires(requiresValue); + pluginInfo.setReleases(List.of(release)); + GenericValidationErrors errors = new GenericValidationErrors(pluginInfo); + + validator.validate(pluginInfo, errors); + System.out.println(errors); + + assertEquals(hasErrors, errors.hasErrors()); + } + + private static Stream fieldsProvider() { + return Stream.of( + new Object[] {"gate<=1.0.0,echo>=1.0.0", false}, + new Object[] {"gate<=1.0.0, echo>=1.0.0", false}, + new Object[] {"gate>=1.0.0", false}, + new Object[] {"gate<1.0.0", false}, + new Object[] {"hello-world=1.0.0", false}, + new Object[] {"gate=1.0.0", false}, + new Object[] {"gate=foo", true}); + } +}