Skip to content

Commit

Permalink
Follow ide-suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarvelle committed Oct 15, 2024
1 parent 71d557d commit 26e5598
Show file tree
Hide file tree
Showing 32 changed files with 156 additions and 198 deletions.
28 changes: 11 additions & 17 deletions src/main/java/no/ndla/taxonomy/config/SwaggerConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public OpenAPI API() {
.info(new Info()
.title("NDLA Taxonomy API")
.description(
"Rest service and graph database for organizing content.\n\n"
+ "When you remove an entity, its associations are also deleted. E.g., if you remove a subject, its associations to any topics are removed. The topics themselves are not affected.")
"""
Rest service and graph database for organizing content.
When you remove an entity, its associations are also deleted. E.g., if you remove a subject, its associations to any topics are removed. The topics themselves are not affected.""")
.version("v1")
.contact(new Contact()
.name(contactName)
Expand All @@ -71,21 +73,13 @@ public OpenAPI API() {
}

private List<Server> servers() {
String url;
switch (environment) {
case "local":
url = "http://api-gateway.ndla-local/taxonomy";
break;
case "prod":
url = "https://api.ndla.no/taxonomy";
break;
case "test":
case "staging":
url = String.format("https://api.%s.ndla.no/taxonomy", environment);
break;
default:
url = String.format("http://localhost:%s", port);
}
String url =
switch (environment) {
case "local" -> "http://api-gateway.ndla-local/taxonomy";
case "prod" -> "https://api.ndla.no/taxonomy";
case "test", "staging" -> String.format("https://api.%s.ndla.no/taxonomy", environment);
default -> String.format("http://localhost:%s", port);
};
return List.of(new Server().url(url));
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/no/ndla/taxonomy/rest/v1/Subjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
@RequestMapping(path = {"/v1/subjects", "/v1/subjects/"})
public class Subjects extends CrudControllerWithMetadata<Node> {
private final Nodes nodes;
private final NodeService nodeService;

public Subjects(
Nodes nodes,
Expand All @@ -46,7 +45,6 @@ public Subjects(
super(nodeRepository, contextUpdaterService, nodeService, qualityEvaluationService);

this.nodes = nodes;
this.nodeService = nodeService;
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,43 @@ private Set<TaxonomyContext> createContexts(Node node) {
}

// Get all parent connections, append all contexts from the parent to the list and return.
node.getParentConnections()
.forEach(parentConnection -> parentConnection.getParent().ifPresent(parent -> {
createContexts(parent).stream()
.map(parentContext -> {
var breadcrumbs = LanguageField.listFromLists(
parentContext.breadcrumbs(), LanguageField.fromNode(parent));
var parentIds = parentContext.parentIds();
parentIds.add(parent.getPublicId().toString());
var parentContextIds = parentContext.parentContextIds();
parentContextIds.add(parentContext.contextId());
var contextId =
HashUtil.mediumHash(parentContext.contextId() + parentConnection.getPublicId());
return new TaxonomyContext(
node.getPublicId().toString(),
LanguageField.fromNode(node),
node.getNodeType(),
parentContext.rootId(),
parentContext.rootName(),
parentContext.path() + node.getPathPart(),
breadcrumbs,
node.getContextType(),
parentIds,
parentContextIds,
parentContext.isVisible() && node.isVisible(),
parentContext.isActive() && activeContext,
parentConnection.isPrimary().orElse(false),
parentConnection
.getRelevance()
.flatMap(relevance -> Optional.of(
relevance.getPublicId().toString()))
.orElse(Relevance.CORE
.getPublicId()
.toString()),
contextId,
parentConnection.getRank(),
parentConnection.getPublicId().toString(),
new ArrayList<>());
})
.forEach(returnedContexts::add);
}));
node.getParentConnections().forEach(parentConnection -> parentConnection
.getParent()
.ifPresent(parent -> createContexts(parent).stream()
.map(parentContext -> {
var breadcrumbs = LanguageField.listFromLists(
parentContext.breadcrumbs(), LanguageField.fromNode(parent));
var parentIds = parentContext.parentIds();
parentIds.add(parent.getPublicId().toString());
var parentContextIds = parentContext.parentContextIds();
parentContextIds.add(parentContext.contextId());
var contextId =
HashUtil.mediumHash(parentContext.contextId() + parentConnection.getPublicId());
return new TaxonomyContext(
node.getPublicId().toString(),
LanguageField.fromNode(node),
node.getNodeType(),
parentContext.rootId(),
parentContext.rootName(),
parentContext.path() + node.getPathPart(),
breadcrumbs,
node.getContextType(),
parentIds,
parentContextIds,
parentContext.isVisible() && node.isVisible(),
parentContext.isActive() && activeContext,
parentConnection.isPrimary().orElse(false),
parentConnection
.getRelevance()
.flatMap(relevance -> Optional.of(
relevance.getPublicId().toString()))
.orElse(Relevance.CORE.getPublicId().toString()),
contextId,
parentConnection.getRank(),
parentConnection.getPublicId().toString(),
new ArrayList<>());
})
.forEach(returnedContexts::add)));

return returnedContexts;
}
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/no/ndla/taxonomy/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,14 @@ public boolean makeAllResourcesPrimary(URI nodePublicId, boolean recursive) {
.findFirstByPublicId(nodePublicId)
.orElseThrow(() -> new NotFoundServiceException("Node was not found"));
if (recursive) {
node.getChildConnections().forEach(nc -> {
nc.getChild()
.filter(n -> n.getNodeType() != NodeType.RESOURCE)
.map(n -> makeAllResourcesPrimary(n.getPublicId(), true));
});
node.getChildConnections().forEach(nc -> nc.getChild()
.filter(n -> n.getNodeType() != NodeType.RESOURCE)
.map(n -> makeAllResourcesPrimary(n.getPublicId(), true)));
}

node.getResourceChildren().forEach(cc -> {
connectionService.updateParentChild(
cc, cc.getRelevance().orElse(null), Optional.of(cc.getRank()), Optional.of(true));
});
node.getResourceChildren()
.forEach(cc -> connectionService.updateParentChild(
cc, cc.getRelevance().orElse(null), Optional.of(cc.getRank()), Optional.of(true)));

return node.getResourceChildren().stream()
.allMatch(resourceConnection -> resourceConnection.isPrimary().orElse(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void addChildIdsRecursively(Set<TreeElement> elements, Set<URI> ids, int
idsThisLevel.add(childId);
});

if (idsThisLevel.size() > 0) {
if (!idsThisLevel.isEmpty()) {
addChildIdsRecursively(elements, idsThisLevel, ttl, nodeTypes);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public UrlResolverServiceImpl(
public Optional<String> resolveOldUrl(String oldUrl) {
final var results = getCachedUrlOldRig(oldUrl);
if (!results.isEmpty()) {
final var result = results.get(0);
final var result = results.getFirst();
final var allPaths = getAllPaths(result.getPublic_id());

if (result.getSubject_id() != null) {
Expand Down Expand Up @@ -187,7 +187,7 @@ public Optional<ResolvedUrl> resolveUrl(String path) {

validateEntityPath(resolvedPathComponents);

final var leafElement = resolvedPathComponents.get(resolvedPathComponents.size() - 1);
final var leafElement = resolvedPathComponents.getLast();

final var resolvedUrl = new ResolvedUrl();
resolvedUrl.setContentUri(leafElement.getContentUri());
Expand Down Expand Up @@ -219,7 +219,7 @@ private List<Node> resolveEntitiesFromPath(String path) {
for (String pathPart : pathParts) {
// Ignore empty parts of the path, including when having leading and/or trailing slashes
// and multiple slashes
if (pathPart.equals("")) {
if (pathPart.isEmpty()) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/no/ndla/taxonomy/domain/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private UrlMappingBuilder getUrlMappingBuilder(String key) {

@Transactional
public static class VersionBuilder {
private Version version;
private final Version version;

public VersionBuilder() {
this.version = new Version();
Expand All @@ -167,7 +167,7 @@ public VersionBuilder locked(Boolean locked) {
}

public static class JsonTranslationBuilder {
private JsonTranslation translation;
private final JsonTranslation translation;

public JsonTranslationBuilder(JsonTranslation nodeTranslation) {
this.translation = nodeTranslation;
Expand All @@ -181,7 +181,7 @@ public JsonTranslationBuilder name(String name) {

@Transactional
public static class UrlMappingBuilder {
private UrlMapping urlMapping;
private final UrlMapping urlMapping;

public UrlMappingBuilder() {
urlMapping = new UrlMapping();
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/no/ndla/taxonomy/domain/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

class MetadataTest {
private Metadata metadata;
private Node parent;

@BeforeEach
void setUp() {
parent = new Node(NodeType.RESOURCE);
Node parent = new Node(NodeType.RESOURCE);
metadata = new Metadata(parent);
}

Expand Down
96 changes: 46 additions & 50 deletions src/test/java/no/ndla/taxonomy/domain/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,24 +447,22 @@ void pickTheCorrectContext() {

@Test
public void qualityEvaluationAverageForDirectChildrenWorks() {
var x = builder.node(n -> {
n.nodeType(NodeType.SUBJECT)
.name("S1")
.publicId("urn:subject:1")
.qualityEvaluation(Grade.Five)
.child(
NodeType.RESOURCE,
t -> t.nodeType(NodeType.RESOURCE).name("R1").qualityEvaluation(Grade.Four))
.child(
NodeType.RESOURCE,
t -> t.nodeType(NodeType.RESOURCE).name("R2").qualityEvaluation(Grade.Three))
.child(
NodeType.RESOURCE,
t -> t.nodeType(NodeType.RESOURCE).name("R3").qualityEvaluation(Grade.Five))
.child(
NodeType.RESOURCE,
t -> t.nodeType(NodeType.RESOURCE).name("R4").qualityEvaluation(Grade.Three));
});
var x = builder.node(n -> n.nodeType(NodeType.SUBJECT)
.name("S1")
.publicId("urn:subject:1")
.qualityEvaluation(Grade.Five)
.child(
NodeType.RESOURCE,
t -> t.nodeType(NodeType.RESOURCE).name("R1").qualityEvaluation(Grade.Four))
.child(
NodeType.RESOURCE,
t -> t.nodeType(NodeType.RESOURCE).name("R2").qualityEvaluation(Grade.Three))
.child(
NodeType.RESOURCE,
t -> t.nodeType(NodeType.RESOURCE).name("R3").qualityEvaluation(Grade.Five))
.child(
NodeType.RESOURCE,
t -> t.nodeType(NodeType.RESOURCE).name("R4").qualityEvaluation(Grade.Three)));

x.updateEntireAverageTree();

Expand All @@ -476,38 +474,36 @@ public void qualityEvaluationAverageForDirectChildrenWorks() {

@Test
public void qualityEvaluationAverageForNestedChildrenWorks() {
var parentNode = builder.node(n -> {
n.nodeType(NodeType.SUBJECT)
.name("S1")
.qualityEvaluation(Grade.Five)
.child(NodeType.TOPIC, t -> t.nodeType(NodeType.TOPIC)
.name("T1")
.qualityEvaluation(Grade.Four)
.child(NodeType.RESOURCE, r -> r.nodeType(NodeType.RESOURCE)
.name("R5")
.qualityEvaluation(Grade.Two))
.child(NodeType.RESOURCE, r -> r.nodeType(NodeType.RESOURCE)
.name("R1")
.qualityEvaluation(Grade.Five)))
.child(NodeType.TOPIC, t -> t.nodeType(NodeType.TOPIC)
.name("T2")
.qualityEvaluation(Grade.Three)
.child(NodeType.RESOURCE, r -> r.nodeType(NodeType.RESOURCE)
.name("R2")
.qualityEvaluation(Grade.Three)))
.child(NodeType.TOPIC, t -> t.nodeType(NodeType.TOPIC)
.name("T3")
.qualityEvaluation(Grade.Five)
.child(NodeType.RESOURCE, r -> r.nodeType(NodeType.RESOURCE)
.name("R3")
.qualityEvaluation(Grade.One)))
.child(NodeType.TOPIC, t -> t.nodeType(NodeType.TOPIC)
.name("T4")
.qualityEvaluation(Grade.Three)
.child(NodeType.RESOURCE, r -> r.nodeType(NodeType.RESOURCE)
.name("R4")
.qualityEvaluation(Grade.Two)));
});
var parentNode = builder.node(n -> n.nodeType(NodeType.SUBJECT)
.name("S1")
.qualityEvaluation(Grade.Five)
.child(NodeType.TOPIC, t -> t.nodeType(NodeType.TOPIC)
.name("T1")
.qualityEvaluation(Grade.Four)
.child(
NodeType.RESOURCE,
r -> r.nodeType(NodeType.RESOURCE).name("R5").qualityEvaluation(Grade.Two))
.child(
NodeType.RESOURCE,
r -> r.nodeType(NodeType.RESOURCE).name("R1").qualityEvaluation(Grade.Five)))
.child(NodeType.TOPIC, t -> t.nodeType(NodeType.TOPIC)
.name("T2")
.qualityEvaluation(Grade.Three)
.child(
NodeType.RESOURCE,
r -> r.nodeType(NodeType.RESOURCE).name("R2").qualityEvaluation(Grade.Three)))
.child(NodeType.TOPIC, t -> t.nodeType(NodeType.TOPIC)
.name("T3")
.qualityEvaluation(Grade.Five)
.child(
NodeType.RESOURCE,
r -> r.nodeType(NodeType.RESOURCE).name("R3").qualityEvaluation(Grade.One)))
.child(NodeType.TOPIC, t -> t.nodeType(NodeType.TOPIC)
.name("T4")
.qualityEvaluation(Grade.Three)
.child(
NodeType.RESOURCE,
r -> r.nodeType(NodeType.RESOURCE).name("R4").qualityEvaluation(Grade.Two))));
var unrelatedNodes = builder.node(
NodeType.SUBJECT, n -> n.name("S2").qualityEvaluation(Grade.One).child(NodeType.TOPIC, t -> t.name("T5")
.qualityEvaluation(Grade.Five)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package no.ndla.taxonomy.domain;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/no/ndla/taxonomy/domain/VersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

package no.ndla.taxonomy.domain;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -23,7 +22,7 @@ public void setUp() {

@Test
public void has_public_id() {
assertTrue(version.getPublicId() != null);
assertNotNull(version.getPublicId());
assertTrue(version.getPublicId().toString().startsWith("urn:version:"));
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/no/ndla/taxonomy/rest/v1/ContextsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void can_get_translated_contexts() throws Exception {
public void root_context_is_more_important_than_primary_parent() throws Exception {
Node topic = builder.node(t -> t.nodeType(NodeType.TOPIC).publicId("urn:topic:1"));

Node subject = builder.node(s -> s.nodeType(NodeType.SUBJECT)
builder.node(s -> s.nodeType(NodeType.SUBJECT)
.isContext(true)
.publicId("urn:subject:1")
.child(topic));
Expand Down
Loading

0 comments on commit 26e5598

Please sign in to comment.