Skip to content

Commit 2e9274f

Browse files
Adds missing tests, fixes typo in message.
1 parent 2546f30 commit 2e9274f

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

structurizr-dsl/src/main/java/com/structurizr/dsl/AbstractParser.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@
44
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
55
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
66
import org.apache.hc.client5.http.impl.classic.HttpClients;
7-
import org.apache.hc.core5.http.ContentType;
8-
import org.apache.hc.core5.http.Header;
97
import org.apache.hc.core5.http.io.entity.EntityUtils;
108

11-
import java.util.regex.Pattern;
12-
139
abstract class AbstractParser {
1410

1511
private static final int HTTP_OK_STATUS = 200;
1612

17-
private static final Pattern VIEW_KEY_PATTERN = Pattern.compile("[\\w-]+");
18-
19-
void validateViewKey(String key) {
20-
if (!VIEW_KEY_PATTERN.matcher(key).matches()) {
21-
throw new RuntimeException("View keys can only contain the following characters: a-zA-0-9_-");
22-
}
23-
}
24-
2513
String removeNonWordCharacters(String name) {
2614
return name.replaceAll("\\W", "");
2715
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
package com.structurizr.dsl;
22

3+
import java.util.regex.Pattern;
4+
35
abstract class AbstractViewParser extends AbstractParser {
6+
7+
private static final String PERMITTED_CHARACTERS_IN_VIEW_KEY = "a-zA-Z0-9_-";
8+
private static final Pattern VIEW_KEY_PATTERN = Pattern.compile("[" + PERMITTED_CHARACTERS_IN_VIEW_KEY + "]+");
9+
10+
void validateViewKey(String key) {
11+
if (!VIEW_KEY_PATTERN.matcher(key).matches()) {
12+
throw new RuntimeException("View keys can only contain the following characters: a-zA-Z0-9_-");
13+
}
14+
}
15+
416
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.structurizr.dsl;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
class AbstractViewParserTests {
9+
10+
private final AbstractViewParser parser = new SystemLandscapeViewParser();
11+
12+
@Test
13+
void test_validateViewKey() {
14+
parser.validateViewKey("key");
15+
parser.validateViewKey("key123");
16+
parser.validateViewKey("Key123");
17+
parser.validateViewKey("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-");
18+
19+
try {
20+
parser.validateViewKey("abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/0123456789");
21+
fail();
22+
} catch (Exception e) {
23+
assertEquals("View keys can only contain the following characters: a-zA-Z0-9_-", e.getMessage());
24+
}
25+
26+
try {
27+
parser.validateViewKey("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789");
28+
fail();
29+
} catch (Exception e) {
30+
assertEquals("View keys can only contain the following characters: a-zA-Z0-9_-", e.getMessage());
31+
}
32+
}
33+
34+
}

0 commit comments

Comments
 (0)