diff --git a/build.gradle b/build.gradle index 47c3a7bdb5..16493aa68a 100644 --- a/build.gradle +++ b/build.gradle @@ -92,7 +92,7 @@ dependencies { implementation 'org.springframework:spring-web:5.3.39' implementation 'com.opencsv:opencsv:5.8' - implementation 'com.github.slugify:slugify:3.0.6' + implementation 'com.github.slugify:slugify:3.0.7' // Fix CVE-2023-46589, CVE-2024-24549 diff --git a/src/main/java/com/epam/ta/reportportal/util/SlugifyUtils.java b/src/main/java/com/epam/ta/reportportal/util/SlugifyUtils.java index 93e3a01659..ca900876e8 100644 --- a/src/main/java/com/epam/ta/reportportal/util/SlugifyUtils.java +++ b/src/main/java/com/epam/ta/reportportal/util/SlugifyUtils.java @@ -9,7 +9,7 @@ private SlugifyUtils() { public static String slugify(String inputStr) { var slug = Slugify.builder() - .underscoreSeparator(false) + .customReplacement("_", "-") .build(); return slug.slugify(inputStr); } diff --git a/src/test/java/com/epam/ta/reportportal/util/RegexpAssertionUtil.java b/src/test/java/com/epam/ta/reportportal/util/RegexpAssertionUtil.java new file mode 100644 index 0000000000..e0db1369b3 --- /dev/null +++ b/src/test/java/com/epam/ta/reportportal/util/RegexpAssertionUtil.java @@ -0,0 +1,46 @@ +/* + * Copyright 2024 EPAM Systems + * + * 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.epam.ta.reportportal.util; + +import java.lang.reflect.Method; +import java.util.regex.Matcher; +import javax.validation.constraints.Pattern; +import lombok.extern.log4j.Log4j2; +import org.junit.jupiter.api.Assertions; + + +@Log4j2 +public class RegexpAssertionUtil { + + private RegexpAssertionUtil() { + } + + public static void checkRegexpPattern(Class clazz, String method, String value) + throws NoSuchMethodException { + Method getSlugMethod = clazz.getMethod(method); + Pattern patternAnnotation = getSlugMethod.getAnnotation(Pattern.class); + String regexp = patternAnnotation.regexp(); + + java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regexp); + Matcher matcher = pattern.matcher(value); + + log.info("Value: '{}')", value); + Assertions.assertTrue(matcher.matches(), + String.format("Value '%s' does not match the pattern", value)); + + } +} diff --git a/src/test/java/com/epam/ta/reportportal/util/SlugifyUtilsTest.java b/src/test/java/com/epam/ta/reportportal/util/SlugifyUtilsTest.java new file mode 100644 index 0000000000..d61f7a2606 --- /dev/null +++ b/src/test/java/com/epam/ta/reportportal/util/SlugifyUtilsTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2024 EPAM Systems + * + * 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.epam.ta.reportportal.util; + +import static com.epam.ta.reportportal.util.RegexpAssertionUtil.checkRegexpPattern; + +import com.epam.reportportal.api.model.ProjectDetails; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SlugifyUtilsTest { + + @ParameterizedTest + @CsvSource( + value = { + "test", + "test test", + "tEst teST", + "test !@#$%^&*()_+", + "test test", + "test - -test", + "test-test", + "test--test", + "test_test", + "test__test", + "test__test123", + } + ) + void slugify(String prjName) throws NoSuchMethodException { + var slug = SlugifyUtils.slugify(prjName); + + checkRegexpPattern(ProjectDetails.class, "getSlug", slug); + } + + +}