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

Fix more bugs and add tests for ARDC processes #50

Merged
merged 4 commits into from
Apr 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import au.org.aodn.ogcapi.server.core.model.CategoryVocabModel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -108,6 +107,34 @@ protected Map<String, List<CategoryVocabModel>> getLeafNodeOfParameterCategory(S
return result;
}

protected CategoryVocabModel buildCategoryVocabModel(JsonNode currentNode, JsonNode outerNode) {
if (currentNode instanceof ObjectNode objectNode) {
if (objectNode.has("prefLabel") && objectNode.has("_about")) {
return CategoryVocabModel.builder()
.about(about.apply(currentNode))
.label(label.apply(currentNode))
.build();
}
} else if (currentNode instanceof TextNode textNode) {
if (textNode.asText().contains("parameter_classes")) {
return CategoryVocabModel.builder()
.about(textNode.asText())
.label(this.findLabelByAbout(outerNode, textNode.asText()))
.build();
}
}
return null;
}

protected String findLabelByAbout(JsonNode node, String c) {
for (JsonNode item : node.get("items")) {
if (about.apply(item).contains(c)) {
return label.apply(item);
}
}
return null;
}

public List<CategoryVocabModel> getParameterCategory(String vocabApiBase) {
Map<String, List<CategoryVocabModel>> leaves = getLeafNodeOfParameterCategory(vocabApiBase);
List<CategoryVocabModel> result = new ArrayList<>();
Expand All @@ -128,46 +155,25 @@ public List<CategoryVocabModel> getParameterCategory(String vocabApiBase) {
List<CategoryVocabModel> broader = new ArrayList<>();
List<CategoryVocabModel> narrower = new ArrayList<>();


log.debug("Processing label {}", label.apply(j));

if (j.has("broader")) {
for (JsonNode b : j.get("broader")) {
CategoryVocabModel c = null;
if (b instanceof ObjectNode objectNode) {
if (objectNode.has("prefLabel") && objectNode.has("_about")) {
c = CategoryVocabModel
.builder()
.about(about.apply(b))
.label(label.apply(b))
.build();
}
}
if (b instanceof TextNode textNode && textNode.asText().contains("parameter_classes")) {
c = CategoryVocabModel.builder()
.about(textNode.asText())
.build();
}
broader.add(c);
broader.add(this.buildCategoryVocabModel(b, node));
}
}

if (j.has("narrower")) {
for (JsonNode b : j.get("narrower")) {
if (b.has("prefLabel") && b.has("_about")) {
CategoryVocabModel c = CategoryVocabModel
.builder()
.about(about.apply(b))
.label(label.apply(b))
.build();

narrower.add(c);

// The record comes from ardc have two levels only, so the second level for sure
// is empty, but the third level info comes form another link (aka the leaves)
// and therefore we can attach it to the second level to for the third.
if(leaves.containsKey(about.apply(b))) {
c.setNarrower(leaves.get(about.apply(b)));
}
CategoryVocabModel c = this.buildCategoryVocabModel(b, node);
// The record comes from ardc have two levels only, so the second level for sure
// is empty, but the third level info comes form another link (aka the leaves)
// and therefore we can attach it to the second level to for the third.
if(leaves.containsKey(c.getAbout())) {
c.setNarrower(leaves.get(c.getAbout()));
}
narrower.add(c);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,60 @@ public void verifyGetParameterCategory() throws IOException {
List<CategoryVocabModel> categoryVocabModelList = restExtService.getParameterCategory("");
assertEquals("Total equals", 33, categoryVocabModelList.size());

Optional<CategoryVocabModel> m = categoryVocabModelList

Optional<CategoryVocabModel> c = categoryVocabModelList
.stream()
.filter(p -> p.getBroader().isEmpty() && !p.getNarrower().isEmpty() && p.getLabel().equals("Chemical"))
.findFirst();

assertTrue("Find target Chemical", c.isPresent());
assertEquals("Have narrower equals", 5, c.get().getNarrower().size());


Optional<CategoryVocabModel> b = categoryVocabModelList
.stream()
.filter(p -> !p.getNarrower().isEmpty() && p.getLabel().equals("Physical-Atmosphere"))
.filter(p -> p.getBroader().isEmpty() && !p.getNarrower().isEmpty() && p.getLabel().equals("Biological"))
.findFirst();

assertTrue("Find target Physical-Atmosphere", m.isPresent());
assertEquals("Have narrower equals", 6, m.get().getNarrower().size());
assertTrue("Find target Biological", b.isPresent());
assertEquals("Have narrower equals", 5, b.get().getNarrower().size());


Optional<CategoryVocabModel> visibility = m.get().getNarrower()
Optional<CategoryVocabModel> pa = categoryVocabModelList
.stream()
.filter(p -> p.getBroader().isEmpty() && !p.getNarrower().isEmpty() && p.getLabel().equals("Physical-Atmosphere"))
.findFirst();

assertTrue("Find target Physical-Atmosphere", pa.isPresent());
assertEquals("Have narrower equals", 8, pa.get().getNarrower().size());

Optional<CategoryVocabModel> airTemperature = pa.get().getNarrower()
.stream()
.filter(p -> p.getLabel().equals("Air temperature"))
.findFirst();
assertTrue("Find target Air temperature", airTemperature.isPresent());

Optional<CategoryVocabModel> visibility = pa.get().getNarrower()
.stream()
.filter(p -> p.getLabel().equals("Visibility"))
.findFirst();

assertTrue("Find target Visibility", visibility.isPresent());

Optional<CategoryVocabModel> airSeaLevel = visibility.get().getNarrower()
Optional<CategoryVocabModel> horizontalVisibilityInTheAtmosphere = visibility.get().getNarrower()
.stream()
.filter(p -> p.getLabel().equals("Horizontal visibility in the atmosphere"))
.findFirst();

assertTrue("Horizontal visibility in the atmosphere found", airSeaLevel.isPresent());
assertTrue("Horizontal visibility in the atmosphere found", horizontalVisibilityInTheAtmosphere.isPresent());

Optional<CategoryVocabModel> pw = categoryVocabModelList
.stream()
.filter(p -> p.getBroader().isEmpty() && !p.getNarrower().isEmpty() && p.getLabel().equals("Physical-Water"))
.findFirst();

assertTrue("Find target Physical-Water", pw.isPresent());
assertEquals("Have narrower equals", 14, pw.get().getNarrower().size());

}
}
Loading