Skip to content

Commit

Permalink
Merge pull request #229 from NDLANO/always-return-language-for-title
Browse files Browse the repository at this point in the history
Return language for name. Add field for stored name.
  • Loading branch information
gunnarvelle authored Oct 25, 2023
2 parents 12eda72 + 31ecd44 commit 6f7f85c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
29 changes: 25 additions & 4 deletions src/main/java/no/ndla/taxonomy/service/dtos/NodeDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import no.ndla.taxonomy.config.Constants;
import no.ndla.taxonomy.domain.*;
import no.ndla.taxonomy.rest.v1.dtos.searchapi.LanguageFieldDTO;
import no.ndla.taxonomy.rest.v1.dtos.searchapi.SearchableTaxonomyResourceType;
Expand All @@ -24,7 +25,10 @@ public class NodeDTO {
@Schema(description = "Node id", example = "urn:topic:234")
private URI id;

@Schema(description = "The name of the node", example = "Trigonometry")
@Schema(description = "The stored name of the node", example = "Trigonometry")
private String baseName;

@Schema(description = "The possibly translated name of the node", example = "Trigonometry")
private String name;

@Schema(
Expand Down Expand Up @@ -74,6 +78,9 @@ public class NodeDTO {
@Schema(description = "A list of all contexts this node is part of")
private List<TaxonomyContextDTO> contexts = new ArrayList<>();

@Schema(description = "The language code for which name is returned", example = "nb")
private String language;

public NodeDTO() {}

public NodeDTO(
Expand Down Expand Up @@ -102,13 +109,19 @@ public NodeDTO(
this.supportedLanguages =
this.translations.stream().map(t -> t.language).collect(Collectors.toCollection(TreeSet::new));

this.name = entity.getTranslatedName(languageCode);
this.language = supportedLanguages.contains(languageCode)
? languageCode
: supportedLanguages.isEmpty() ? Constants.DefaultLanguage : supportedLanguages.first();

this.baseName = entity.getName();

this.name = entity.getTranslatedName(this.language);

this.metadata = new MetadataDTO(entity.getMetadata());

this.resourceTypes = entity.getResourceResourceTypes().stream()
.sorted()
.map(resourceType -> new ResourceTypeWithConnectionDTO(resourceType, languageCode))
.map(resourceType -> new ResourceTypeWithConnectionDTO(resourceType, this.language))
.collect(Collectors.toCollection(TreeSet::new));

this.nodeType = entity.getNodeType();
Expand All @@ -118,7 +131,7 @@ public NodeDTO(
this.path = ctx.path();
// TODO: this changes the content in context breadcrumbs
this.breadcrumbs = LanguageField.listFromLists(ctx.breadcrumbs(), LanguageField.fromNode(entity))
.get(languageCode);
.get(this.language);
this.relevanceId = Optional.of(URI.create(ctx.relevanceId()));
this.contextId = Optional.of(ctx.contextId());
this.url = TitleUtil.createPrettyUrl(this.name, ctx.contextId());
Expand Down Expand Up @@ -171,6 +184,10 @@ public String getName() {
return name;
}

public String getBaseName() {
return baseName;
}

public Optional<URI> getContentUri() {
return contentUri;
}
Expand Down Expand Up @@ -214,4 +231,8 @@ public List<TaxonomyContextDTO> getContexts() {
public Optional<String> getUrl() {
return url;
}

public String getLanguage() {
return language;
}
}
18 changes: 15 additions & 3 deletions src/test/java/no/ndla/taxonomy/rest/v1/NodeTranslationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void can_get_all_nodes() throws Exception {
assertEquals(2, nodes.length);
assertAnyTrue(nodes, s -> s.getName().equals("Trigonometri"));
assertAnyTrue(nodes, s -> s.getName().equals("Integrasjon"));
assertAllTrue(nodes, s -> s.getLanguage().equals("nb"));
}

@Test
Expand All @@ -47,14 +48,25 @@ public void can_get_single_node() throws Exception {
}

@Test
public void fallback_to_default_language() throws Exception {
public void fallback_to_default_language_if_no_translation() throws Exception {
URI id = builder.node(NodeType.TOPIC, t -> t.name("Trigonometry")).getPublicId();
final var topic = getNode(id, "XX");
assertEquals("Trigonometry", topic.getName());
assertEquals("nb", topic.getLanguage());
}

@Test
public void can_get_default_language() throws Exception {
public void fallback_to_translated_name() throws Exception {
URI id = builder.node(NodeType.TOPIC, t -> t.name("Trignometri").translation("Trignometry", "en"))
.getPublicId();
final var topic = getNode(id, "nb");
assertEquals("Trignometry", topic.getName());
assertEquals("en", topic.getLanguage());
assertEquals("Trignometri", topic.getBaseName());
}

@Test
public void defaults_to_default_language() throws Exception {
URI id = builder.node(
NodeType.TOPIC, t -> t.name("Trigonometry").translation("nb", l -> l.name("Trigonometri")))
.getPublicId();
Expand Down Expand Up @@ -166,7 +178,7 @@ public void can_get_resources_for_a_node_recursively_with_translation() throws E
}

@Test
public void can_get_resources_for_a_node_without_childrem_resources_with_translation() throws Exception {
public void can_get_resources_for_a_node_without_child_resources_with_translation() throws Exception {
builder.resourceType("article", rt -> rt.name("Article").translation("nb", tr -> tr.name("Artikkel")));

builder.node(NodeType.SUBJECT, s -> s.isContext(true).child(NodeType.TOPIC, t -> t.publicId("urn:topic:1")
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/no/ndla/taxonomy/service/NodeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package no.ndla.taxonomy.service;

import static no.ndla.taxonomy.TestUtils.assertAnyTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -205,8 +206,8 @@ public void contentUriSearch() throws URISyntaxException {
var result = nodeService.search(
Optional.empty(), Optional.empty(), Optional.of(idList), Optional.empty(), Optional.empty(), 10, 1);

assertEquals(result.getResults().get(0).getId(), new URI("urn:topic:1"));
assertEquals(result.getResults().get(1).getId(), new URI("urn:topic:4"));
assertAnyTrue(result.getResults(), res -> res.getId().equals(URI.create("urn:topic:1")));
assertAnyTrue(result.getResults(), res -> res.getId().equals(URI.create("urn:topic:4")));
assertEquals(result.getTotalCount(), 2);
}
}
2 changes: 2 additions & 0 deletions typescript/taxonomy-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Metadata {
}

export interface Node {
baseName: string;
breadcrumbs: string[];
contentUri?: string;
/**
Expand All @@ -32,6 +33,7 @@ export interface Node {
contextId?: string;
contexts: TaxonomyContext[];
id: string;
language: string;
metadata: Metadata;
name: string;
/**
Expand Down

0 comments on commit 6f7f85c

Please sign in to comment.