Skip to content
Open
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 @@ -36,16 +36,13 @@ public enum ResourceTypeDictionary implements ResourceType {
LANGUAGE_CATEGORY("http://bibfra.me/vocab/lite/LanguageCategory"),
MEETING("http://bibfra.me/vocab/lite/Meeting"),
ORGANIZATION("http://bibfra.me/vocab/lite/Organization"),
PARALLEL_TITLE("http://bibfra.me/vocab/marc/ParallelTitle"),
PERSON("http://bibfra.me/vocab/lite/Person"),
PLACE("http://bibfra.me/vocab/lite/Place"),
PROVIDER_EVENT("http://bibfra.me/vocab/lite/ProviderEvent"),
STATUS("http://bibfra.me/vocab/marc/Status"),
SUPPLEMENTARY_CONTENT("http://bibfra.me/vocab/marc/SupplementaryContent"),
TEMPORAL("http://bibfra.me/vocab/lite/Temporal"),
TITLE("http://bibfra.me/vocab/marc/Title"),
TOPIC("http://bibfra.me/vocab/lite/Topic"),
VARIANT_TITLE("http://bibfra.me/vocab/marc/VariantTitle"),
WORK("http://bibfra.me/vocab/lite/Work");

private final String uri;
Expand Down
46 changes: 44 additions & 2 deletions src/main/java/org/folio/ld/dictionary/model/Resource.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package org.folio.ld.dictionary.model;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toCollection;
import static org.folio.ld.dictionary.PredicateDictionary.TITLE;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.folio.ld.dictionary.ResourceTypeDictionary;
import org.folio.ld.dictionary.model.title.Title;

@Data
@NoArgsConstructor
Expand All @@ -24,7 +32,7 @@ public class Resource {

private String label;

private JsonNode doc;
private ObjectNode doc;

private FolioMetadata folioMetadata;

Expand Down Expand Up @@ -56,4 +64,38 @@ public Set<String> getTypeNames() {
return types.stream().map(Enum::name).collect(toCollection(LinkedHashSet::new));
}

public ObjectNode getDocWithoutTitles() {
if (isNull(doc) || !doc.has(TITLE.getUri())) {
return doc;
}
var docCopy = doc.deepCopy();
docCopy.remove(TITLE.getUri());
return docCopy;
}

public void addTitle(Title title, ObjectMapper objectMapper) throws JsonProcessingException {
if (nonNull(title)) {
var currentTitles = getTitles(objectMapper);
currentTitles.add(title);
var titleArrayNode = objectMapper.createArrayNode();
for (Title currentTitle : currentTitles) {
var node = objectMapper.valueToTree(currentTitle);
titleArrayNode.add(node);
}
if (isNull(doc)) {
doc = objectMapper.createObjectNode();
}
doc.set(TITLE.getUri(), titleArrayNode);
}
}

public List<Title> getTitles(ObjectMapper objectMapper) throws JsonProcessingException {
if (isNull(doc) || !doc.has(TITLE.getUri())) {
return new ArrayList<>();
}
var titlesNode = doc.get(TITLE.getUri());
return objectMapper.treeToValue(titlesNode, new TypeReference<>() {
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.folio.ld.dictionary.model.title;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class ParallelTitle extends Title {

@JsonProperty("http://bibfra.me/vocab/lite/note")
public final List<String> notes = new ArrayList<>();

@JsonProperty("http://bibfra.me/vocab/lite/date")
public final List<String> dates = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.folio.ld.dictionary.model.title;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class PrimaryTitle extends Title {

@JsonProperty("http://bibfra.me/vocab/bflc/nonSortNum")
public final List<String> nonSortNumbers = new ArrayList<>();

}
33 changes: 33 additions & 0 deletions src/main/java/org/folio/ld/dictionary/model/title/Title.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.folio.ld.dictionary.model.title;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.util.ArrayList;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@ToString
@EqualsAndHashCode
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ParallelTitle.class, name = "Parallel"),
@JsonSubTypes.Type(value = PrimaryTitle.class, name = "Primary"),
@JsonSubTypes.Type(value = VariantTitle.class, name = "Variant")
})
public abstract class Title {

@JsonProperty("http://bibfra.me/vocab/marc/partName")
public final List<String> partNames = new ArrayList<>();

@JsonProperty("http://bibfra.me/vocab/marc/partNumber")
public final List<String> partNumbers = new ArrayList<>();

@JsonProperty("http://bibfra.me/vocab/marc/mainTitle")
public final List<String> mainTitles = new ArrayList<>();

@JsonProperty("http://bibfra.me/vocab/marc/subTitle")
public final List<String> subTitles = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.folio.ld.dictionary.model.title;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class VariantTitle extends Title {

@JsonProperty("http://bibfra.me/vocab/lite/note")
public final List<String> notes = new ArrayList<>();

@JsonProperty("http://bibfra.me/vocab/lite/date")
public final List<String> dates = new ArrayList<>();

@JsonProperty("http://bibfra.me/vocab/marc/variantType")
public final List<String> variantTypes = new ArrayList<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ void fromUri_shouldNotContainValue(String uri) {
"http://bibfra.me/vocab/lite/Jurisdiction, 4072387342118066928",
"http://bibfra.me/vocab/lite/Meeting, 8312970713564449340",
"http://bibfra.me/vocab/lite/Organization, -2724163952136761329",
"http://bibfra.me/vocab/marc/ParallelTitle, -7668047008344689015",
"http://bibfra.me/vocab/lite/Person, 3745950457458968285",
"http://bibfra.me/vocab/lite/Place, 7862842856693929565",
"http://bibfra.me/vocab/lite/ProviderEvent, -1414513180123539856",
"http://bibfra.me/vocab/marc/Status, -5546355039147771611",
"http://bibfra.me/vocab/marc/SupplementaryContent, 3849426639441431097",
"http://bibfra.me/vocab/lite/Temporal, 4805333230697475193",
"http://bibfra.me/vocab/marc/Title, 1422402142581052159",
"http://bibfra.me/vocab/lite/Topic, -3096999901507785420",
"http://bibfra.me/vocab/marc/VariantTitle, 4846800426734929825",
"http://bibfra.me/vocab/lite/Work, -3114907246856331309",
})
void fromUri_shouldContainValue(String uri, Long hash) {
Expand Down
179 changes: 179 additions & 0 deletions src/test/java/org/folio/ld/dictionary/model/ResourceTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.folio.ld.dictionary.model;

import static org.assertj.core.api.Assertions.assertThat;
import static org.folio.ld.dictionary.PredicateDictionary.TITLE;
import static org.folio.ld.dictionary.test.TestUtil.OBJECT_MAPPER;
import static org.folio.ld.dictionary.test.TestUtil.getParallelTitle;
import static org.folio.ld.dictionary.test.TestUtil.getPrimaryTitle;
import static org.folio.ld.dictionary.test.TestUtil.getVariantTitle;
import static org.folio.ld.dictionary.test.TestUtil.loadResourceAsString;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.folio.ld.dictionary.PredicateDictionary;
import org.folio.ld.dictionary.ResourceTypeDictionary;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -41,4 +48,176 @@ void addOutgoingEdge_shouldAddOutgoingEdge() {
assertThat(sourceResource.getOutgoingEdges())
.containsExactlyInAnyOrder(edge1, edge2);
}

@Test
void addTitle_shouldWorkCorrectly_withEmptyDoc() throws JsonProcessingException {
// given
var resource = new Resource();
var primaryTitle = getPrimaryTitle();
var parallelTitle = getParallelTitle();
var variantTitle = getVariantTitle();

// when
resource.addTitle(primaryTitle, OBJECT_MAPPER);
resource.addTitle(parallelTitle, OBJECT_MAPPER);
resource.addTitle(variantTitle, OBJECT_MAPPER);

// then
assertThat(resource.getDoc()).isNotEmpty();
assertThat(resource.getDoc().has(TITLE.getUri())).isTrue();
assertThat(resource.getDoc().get(TITLE.getUri())).hasSize(3);
assertThat(resource.getDoc().get(TITLE.getUri()).get(0).toString())
.isEqualTo(loadResourceAsString("primary_title.json"));
assertThat(resource.getDoc().get(TITLE.getUri()).get(1).toString())
.isEqualTo(loadResourceAsString("parallel_title.json"));
assertThat(resource.getDoc().get(TITLE.getUri()).get(2).toString())
.isEqualTo(loadResourceAsString("variant_title.json"));
}

@Test
void addTitle_shouldWorkCorrectly_withNotEmptyDoc() throws JsonProcessingException {
// given
var resource = new Resource().setDoc(OBJECT_MAPPER.createObjectNode());
var someProperty = "some property";
var someValue = "some value";
resource.getDoc().put(someProperty, someValue);
var primaryTitle = getPrimaryTitle();
var parallelTitle = getParallelTitle();
var variantTitle = getVariantTitle();

// when
resource.addTitle(primaryTitle, OBJECT_MAPPER);
resource.addTitle(parallelTitle, OBJECT_MAPPER);
resource.addTitle(variantTitle, OBJECT_MAPPER);

// then
assertThat(resource.getDoc()).isNotEmpty();
assertThat(resource.getDoc().has(TITLE.getUri())).isTrue();
assertThat(resource.getDoc().get(TITLE.getUri())).hasSize(3);
assertThat(resource.getDoc().get(TITLE.getUri()).get(0).toString())
.isEqualTo(loadResourceAsString("primary_title.json"));
assertThat(resource.getDoc().get(TITLE.getUri()).get(1).toString())
.isEqualTo(loadResourceAsString("parallel_title.json"));
assertThat(resource.getDoc().get(TITLE.getUri()).get(2).toString())
.isEqualTo(loadResourceAsString("variant_title.json"));
assertThat(resource.getDoc().get(someProperty).textValue()).isEqualTo(someValue);
}

@Test
void getTitles_shouldReturnAllTitles() throws JsonProcessingException {
// given
var resource = new Resource().setDoc(OBJECT_MAPPER.createObjectNode());
var someProperty = "some property";
var someValue = "some value";
resource.getDoc().put(someProperty, someValue);
var titleArrayNode = OBJECT_MAPPER.createArrayNode();
var primaryTitle = getPrimaryTitle();
titleArrayNode.add(OBJECT_MAPPER.valueToTree(primaryTitle));
var parallelTitle = getParallelTitle();
titleArrayNode.add(OBJECT_MAPPER.valueToTree(parallelTitle));
var variantTitle = getVariantTitle();
titleArrayNode.add(OBJECT_MAPPER.valueToTree(variantTitle));
resource.getDoc().set(TITLE.getUri(), titleArrayNode);

// when
var titles = resource.getTitles(OBJECT_MAPPER);

// then
assertThat(titles).hasSize(3);
assertThat(titles.get(0)).isEqualTo(primaryTitle);
assertThat(titles.get(1)).isEqualTo(parallelTitle);
assertThat(titles.get(2)).isEqualTo(variantTitle);
assertThat(resource.getDoc().get(someProperty).textValue()).isEqualTo(someValue);
}

@Test
void getTitles_shouldReturnEmptyList_IfNoTitles() throws JsonProcessingException {
// given
var resource = new Resource().setDoc(OBJECT_MAPPER.createObjectNode());
var someProperty = "some property";
var someValue = "some value";
resource.getDoc().put(someProperty, someValue);

// when
var titles = resource.getTitles(OBJECT_MAPPER);

// then
assertThat(titles).isEmpty();
assertThat(resource.getDoc().get(someProperty).textValue()).isEqualTo(someValue);
}

@Test
void getTitles_shouldReturnEmptyList_IfNoDoc() throws JsonProcessingException {
// given
var resource = new Resource();

// when
var titles = resource.getTitles(OBJECT_MAPPER);

// then
assertThat(titles).isEmpty();
}

@Test
void getDocWithoutTitles_shouldReturnDocAsIs_ifNoDoc() {
// given
var resource = new Resource();

// when
var docWithoutTitles = resource.getDocWithoutTitles();

// then
assertThat(docWithoutTitles).isNull();
}

@Test
void getDocWithoutTitles_shouldReturnDocAsIs_ifEmptyDoc() {
// given
var resource = new Resource().setDoc(OBJECT_MAPPER.createObjectNode());

// when
var docWithoutTitles = resource.getDocWithoutTitles();

// then
assertThat(docWithoutTitles).isEqualTo(resource.getDoc());
}

@Test
void getDocWithoutTitles_shouldReturnDocAsIs_ifNoTitles() {
// given
var resource = new Resource().setDoc(OBJECT_MAPPER.createObjectNode());
var someProperty = "some property";
var someValue = "some value";
resource.getDoc().put(someProperty, someValue);

// when
var docWithoutTitles = resource.getDocWithoutTitles();

// then
assertThat(docWithoutTitles).isEqualTo(resource.getDoc());
}

@Test
void getDocWithoutTitles_shouldReturnDocWithNoTitles_ifThereAreTitles() {
// given
var resource = new Resource().setDoc(OBJECT_MAPPER.createObjectNode());
var someProperty = "some property";
var someValue = "some value";
resource.getDoc().put(someProperty, someValue);
var titleArrayNode = OBJECT_MAPPER.createArrayNode();
var primaryTitle = getPrimaryTitle();
titleArrayNode.add(OBJECT_MAPPER.valueToTree(primaryTitle));
var parallelTitle = getParallelTitle();
titleArrayNode.add(OBJECT_MAPPER.valueToTree(parallelTitle));
var variantTitle = getVariantTitle();
titleArrayNode.add(OBJECT_MAPPER.valueToTree(variantTitle));
resource.getDoc().set(TITLE.getUri(), titleArrayNode);

// when
var docWithoutTitles = resource.getDocWithoutTitles();

// then
assertThat(docWithoutTitles).hasSize(1);
assertThat(docWithoutTitles.get(someProperty).textValue()).isEqualTo(someValue);
}
}
Loading