Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue/152 #154

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Threshr is available to jvm projects via [maven central].
<dependency>
<groupId>com.graqr</groupId>
<artifactId>threshr</artifactId>
<version>0.0.14</version>
<version>0</version>
</dependency>
```

Expand All @@ -64,14 +64,14 @@ Threshr is available to jvm projects via [maven central].
<details><summary>Gradle</summary>

```groovy
implementation group: 'com.graqr', name: 'threshr', version: '0.0.14'
implementation group: 'com.graqr', name: 'threshr', version: '0.0.15-SNAPSHOT'
```
</details>

<details><summary>Gradle Kotlin</summary>

```kotlin
implementation("com.graqr:threshr:0.0.14")
implementation("com.graqr:threshr:0.0.15-SNAPSHOT")
```
</details>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.graqr</groupId>
<artifactId>threshr</artifactId>
<version>0.0.14</version>
<version>0.0.15-SNAPSHOT</version>
<packaging>${packaging}</packaging>

<parent>
Expand Down
16 changes: 12 additions & 4 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 @@ -21,19 +23,25 @@ public Page(String page) throws ThreshrException {
}

/**
* Sets string value as "/c/" + provided value.
* Sets string value as "/c/" + provided value. As a means of security, value is permitted to be 26 alphanumeric
* words delimited with either - or _
*
* @param name Query parameter in redsky api to specify from where an api call is made in the browser
* @throws ThreshrException if string contains anything other than letters or is empty
*/
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|\\-)]).+") || tempPage.isEmpty()) {
throw new ThreshrException(String.format(
"Expected only letters for the page value, but received \"%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
43 changes: 43 additions & 0 deletions src/test/groovy/com/graqr/threshr/model/queryparam/PageSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.micronaut.context.annotation.Value
import io.micronaut.core.io.ResourceLoader
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import net.datafaker.Faker
import spock.lang.Shared
import spock.lang.Specification

Expand Down Expand Up @@ -69,6 +70,48 @@ class PageSpec extends Specification {
expectedPages = getLinesFromFile("classpath:" + pagesFilepath)
}

def "validate page title word count limit"() {
when: "create page title of #size word count"
Random randy = new Random()
String delimiter = randy.nextBoolean() ? "_" : "-"
String titleStart = randy.nextBoolean() ? "/c/" : ""
String pageValue = new Faker().lorem().sentence(size as int)
// 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("$titleStart$pageValue")

then:
noExceptionThrown()

where:
size << (1..30)
}

def "Creating Page with title word count under or over limit fails"() {
when: "creating page with '#pageValue' title"
//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 | _
"" | _
"/c/" | _
"/c/" + new Faker().lorem().sentence(31).replace(" ", "-") | _
new Faker().lorem().sentence(31).replace(" ", "_") | _
}


def "test create new Page from #pageValue seed data creates object with expected value"() {
given:
Expand Down
Loading