Skip to content

Commit

Permalink
Merge pull request #274 from NDLANO/new-url-format
Browse files Browse the repository at this point in the history
New url format
  • Loading branch information
gunnarvelle authored Nov 5, 2024
2 parents c4cdeac + a555182 commit 4a4b979
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/main/java/no/ndla/taxonomy/util/PrettyUrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ public static Optional<String> createPrettyUrl(
Optional<String> rootName, String name, String hash, NodeType nodeType) {
if (name == null || hash == null) return Optional.empty();
StringBuilder builder = new StringBuilder();
builder.append(nodeTypeMapping(nodeType));
builder.append("/");
rootName.ifPresent(rn -> {
if (!rn.equals(name)) {
builder.append("/");
buildUrlFragment(builder, cleanString(rootName.get()));
builder.append("/");
}
});
builder.append("/");
buildUrlFragment(builder, cleanString(name));
builder.append(String.format("%s/%s", nodeTypeMapping(nodeType), hash));
builder.append(String.format("/%s", hash));

return Optional.of(builder.toString());
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/no/ndla/taxonomy/rest/v1/NodesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void can_get_nodes_by_contextId() {
final var nodes = testUtils.getObject(NodeDTO[].class, response);
assertEquals(1, nodes.length);
assertEquals("Resource", nodes[0].getName());
assertTrue(nodes[0].getUrl().get().endsWith(String.format("/resource/r/%s", context.contextId())));
assertTrue(nodes[0].getUrl().get().endsWith(String.format("resource/%s", context.contextId())));
assertEquals(context.path(), nodes[0].getPath().get());
assertTrue(nodes[0].getBreadcrumbs()
.containsAll(context.breadcrumbs().get("nb")));
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/no/ndla/taxonomy/util/PrettyUrlUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@ public class PrettyUrlUtilTest {
@Test
void test_create_pretty_url() {
assertEquals(
"/this-is-a-title/r/hash",
"/r/this-is-a-title/hash",
PrettyUrlUtil.createPrettyUrl(Optional.empty(), "This is a title", "hash", NodeType.RESOURCE)
.get());
}

@Test
void test_create_pretty_url_with_root() {
assertEquals(
"/the-root-title/this-is-a-title/e/hash",
"/e/the-root-title/this-is-a-title/hash",
PrettyUrlUtil.createPrettyUrl(Optional.of("The root title"), "This is a title", "hash", NodeType.TOPIC)
.get());
}

@Test
void test_create_pretty_url_with_punctuation() {
assertEquals(
"/this-is-a-title-seriously/r/hash",
"/r/this-is-a-title-seriously/hash",
PrettyUrlUtil.createPrettyUrl(Optional.empty(), "This is a title, seriously", "hash", NodeType.RESOURCE)
.get());
assertEquals(
"/this-is-a-title-and-a-12/r/hash",
"/r/this-is-a-title-and-a-12/hash",
PrettyUrlUtil.createPrettyUrl(Optional.empty(), "This is a title and a 1/2", "hash", NodeType.RESOURCE)
.get());
}

@Test
void test_create_pretty_url_from_html_formatted_text() {
assertEquals(
"/this-is-a-italics-title/f/hash",
"/f/this-is-a-italics-title/hash",
PrettyUrlUtil.createPrettyUrl(
Optional.empty(), "This is a <em>italics title</em>", "hash", NodeType.SUBJECT)
.get());
Expand All @@ -55,12 +55,12 @@ void test_create_pretty_url_from_html_formatted_text() {
@Test
void test_create_pretty_url_with_norwegian_chars() {
assertEquals(
"/nar-kommer-hosten-tror-du-arlig-talt/e/hash",
"/e/nar-kommer-hosten-tror-du-arlig-talt/hash",
PrettyUrlUtil.createPrettyUrl(
Optional.empty(), "Når kommer høsten tror du ærlig talt?", "hash", NodeType.TOPIC)
.get());
assertEquals(
"/utgatt-historie/a-hoppe-etter-wirkola/e/hash",
"/e/utgatt-historie/a-hoppe-etter-wirkola/hash",
PrettyUrlUtil.createPrettyUrl(
Optional.of("Utgått historie"), "Å hoppe etter wirkola", "hash", NodeType.TOPIC)
.get());
Expand Down

0 comments on commit 4a4b979

Please sign in to comment.