Skip to content

Commit

Permalink
EPMRPP-94689 fix project slug (#2057)
Browse files Browse the repository at this point in the history
  • Loading branch information
grabsefx authored Sep 3, 2024
1 parent 3e72a88 commit 435ddda
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private SlugifyUtils() {

public static String slugify(String inputStr) {
var slug = Slugify.builder()
.underscoreSeparator(false)
.customReplacement("_", "-")
.build();
return slug.slugify(inputStr);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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));

}
}
50 changes: 50 additions & 0 deletions src/test/java/com/epam/ta/reportportal/util/SlugifyUtilsTest.java
Original file line number Diff line number Diff line change
@@ -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);
}


}

0 comments on commit 435ddda

Please sign in to comment.