Skip to content

Commit

Permalink
fix: remove regex from page constructor
Browse files Browse the repository at this point in the history
Signed-off-by: jonathan zollinger <[email protected]>
  • Loading branch information
Jonathan-Zollinger committed Aug 22, 2024
1 parent 121385b commit 7b3d4f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/main/java/com/graqr/threshr/model/queryparam/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.graqr.threshr.ThreshrException;
import lombok.Getter;

import java.util.Arrays;

/**
* Query parameter in redsky api. Specifies from where an api call is made in the browser.
*/
Expand All @@ -29,12 +31,17 @@ public Page(String page) throws ThreshrException {
*/
public void setName(String name) throws ThreshrException {
String tempPage = name.trim().toLowerCase();
String threshrException = "Expected non-space-character delimited string of up to 30 words, but got \"%s\".";
if (tempPage.startsWith("/c/")) {
tempPage = tempPage.substring(3);
}
if (!tempPage.matches("^([a-z\\d]+[-_]?){1,31}$")) {
throw new ThreshrException(String.format(
"Expected non-space-character delimited string of up to 30 words, but got \"%s\".", tempPage));
if (tempPage.isEmpty()) {
throw new ThreshrException(String.format(threshrException, tempPage));
}
//regex "^([a-z\\d]+[-_]?){1,21}$" causes perpetual waiting in tests nearing word limit
long pageCount = Arrays.stream(tempPage.split("[-_]")).count();
if (pageCount > 30) {
throw new ThreshrException(String.format(threshrException, tempPage));
}
this.name = "/c/" + tempPage;
}
Expand Down
22 changes: 15 additions & 7 deletions src/test/groovy/com/graqr/threshr/model/queryparam/PageSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ class PageSpec extends Specification {

def "validate page title word count limit"() {
when: "create page title of #size word count"
String delimiter = new Random().nextBoolean() ? "_" : "-"
Random randy = new Random()
String delimiter = randy.nextBoolean() ? "_" : "-"
String titleStart = randy.nextBoolean() ? "/c/" : ""
String pageValue = new Faker().lorem().sentence(size as int)
.replace(" ", delimiter)
.replace(".","") // remove ending punctuation
// weirdly, faker can sometimes add extra words. this is kinda documented, but doesn't make sense to me.
// this removes those extra words.
pageValue = pageValue.split(" ")[0..(size as int) - 1]
.join(delimiter)
.replace(".", "") // remove ending punctuation

and: "create page object whose title is '#pageValue'"
//noinspection GroovyResultOfObjectAllocationIgnored
new Page(pageValue)
new Page("$titleStart$pageValue")

then:
noExceptionThrown()
Expand All @@ -93,15 +98,18 @@ class PageSpec extends Specification {
//noinspection GroovyResultOfObjectAllocationIgnored
new Page(pageValue)


then:
def exception = thrown(ThreshrException)
exception.message.contains("Expected non-space-character delimited string of up to 30 words, but got")


where:
pageValue | _
"" | _
new Faker().lorem().sentence(31) | _
pageValue | _
"" | _
"/c/" | _
"/c/" + new Faker().lorem().sentence(31).replace(" ", "-") | _
new Faker().lorem().sentence(31).replace(" ", "_") | _
}


Expand Down

0 comments on commit 7b3d4f5

Please sign in to comment.