Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/master' into MODINV…
Browse files Browse the repository at this point in the history
…STOR-1205

# Conflicts:
#	descriptors/ModuleDescriptor-template.json
#	src/test/java/api/InstancesApiExamples.java
  • Loading branch information
dmytrokrutii committed Sep 2, 2024
2 parents b28b3b0 + 8e019f9 commit 1b7503a
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Disallow updating holdings ownership with related boundwith [MODINV-1051](https://folio-org.atlassian.net/browse/MODINV-1051)
* Disallow updating ownership of boundwith item [MODINV-1052](https://folio-org.atlassian.net/browse/MODINV-1052)
* InstanceIngress update events consumption [MODINV-1008](https://folio-org.atlassian.net/browse/MODINV-1008)
* Apply new date type fields to instance schema [MODINV-1067](https://folio-org.atlassian.net/browse/MODINV-1067)

## 20.2.0 2023-03-20
* Inventory cannot process Holdings with virtual fields ([MODINV-941](https://issues.folio.org/browse/MODINV-941))
Expand Down
4 changes: 2 additions & 2 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"provides": [
{
"id": "inventory",
"version": "13.1",
"version": "13.3",
"handlers": [
{
"methods": ["GET"],
Expand Down Expand Up @@ -602,7 +602,7 @@
},
{
"id": "instance-storage",
"version": "10.0 10.3"
"version": "10.3"
},
{
"id": "instance-storage-batch",
Expand Down
21 changes: 21 additions & 0 deletions ramls/instance.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,27 @@
]
}
},
"dates": {
"type": "object",
"description": "Instance Dates",
"properties": {
"dateTypeId": {
"type": "string",
"description": "Date type ID",
"$ref": "uuid.json"
},
"date1": {
"type": "string",
"description": "Date 1",
"maxLength": 4
},
"date2": {
"type": "string",
"description": "Date 2",
"maxLength": 4
}
}
},
"instanceTypeId": {
"type": "string",
"description": "The unique term for the resource type whether it's from the RDA content term list of locally defined"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class InventoryConfigurationImpl implements InventoryConfiguration {
Instance.INSTANCE_TYPE_ID_KEY,
Instance.MODE_OF_ISSUANCE_ID_KEY,
Instance.PRECEDING_TITLES_KEY,
Instance.SUCCEEDING_TITLES_KEY
Instance.SUCCEEDING_TITLES_KEY,
Instance.DATES_KEY
);

private static final Set<String> HOLDINGS_BLOCKED_FIELDS = Sets.newHashSet(
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/folio/inventory/domain/instances/Dates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.folio.inventory.domain.instances;

import io.vertx.core.json.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Getter;

import static org.apache.commons.lang3.ObjectUtils.anyNotNull;
import static org.folio.inventory.support.JsonHelper.includeIfPresent;

@Getter
@AllArgsConstructor
public class Dates {
// JSON property names
public static final String DATE_TYPE_ID_KEY = "dateTypeId";
public static final String DATE1_KEY = "date1";
public static final String DATE2_KEY = "date2";

public final String dateTypeId;
public final String date1;
public final String date2;

public static JsonObject datesToJson(Dates dates) {
if (dates == null || (dates.getDate1() == null && dates.getDate2() == null && dates.getDateTypeId() == null)) {
return null;
}
var json = new JsonObject();
includeIfPresent(json, DATE_TYPE_ID_KEY, dates.getDateTypeId());
includeIfPresent(json, DATE1_KEY, dates.getDate1());
includeIfPresent(json, DATE2_KEY, dates.getDate2());
return json;
}

public static Dates datesFromJson(JsonObject datesJson) {
if (datesJson == null) {
return null;
}
var dateTypeId = datesJson.getString(DATE_TYPE_ID_KEY);
var date1 = datesJson.getString(DATE1_KEY);
var date2 = datesJson.getString(DATE2_KEY);
return anyNotNull(dateTypeId, date1, date2) ? new Dates(dateTypeId, date1, date2) : null;
}
}
20 changes: 17 additions & 3 deletions src/main/java/org/folio/inventory/domain/instances/Instance.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.folio.inventory.domain.instances;

import static org.folio.inventory.domain.instances.Dates.datesFromJson;
import static org.folio.inventory.domain.instances.Dates.datesToJson;
import static org.folio.inventory.domain.instances.PublicationPeriod.publicationPeriodFromJson;
import static org.folio.inventory.domain.instances.PublicationPeriod.publicationPeriodToJson;
import static org.folio.inventory.support.JsonArrayHelper.toListOfStrings;
Expand Down Expand Up @@ -73,6 +75,7 @@ public class Instance {
public static final String TAG_LIST_KEY = "tagList";
public static final String NATURE_OF_CONTENT_TERM_IDS_KEY = "natureOfContentTermIds";
public static final String PUBLICATION_PERIOD_KEY = "publicationPeriod";
public static final String DATES_KEY = "dates";

private final String id;
@JsonProperty("_version")
Expand Down Expand Up @@ -117,6 +120,7 @@ public class Instance {
private List<String> tags;
private List<String> natureOfContentTermIds = new ArrayList<>();
private PublicationPeriod publicationPeriod;
private Dates dates;

protected static final String INVENTORY_PATH = "/inventory";
protected static final String INSTANCES_PATH = INVENTORY_PATH + "/instances";
Expand Down Expand Up @@ -188,7 +192,8 @@ public static Instance fromJson(JsonObject instanceJson) {
.setStatusUpdatedDate(instanceJson.getString(STATUS_UPDATED_DATE_KEY))
.setTags(getTags(instanceJson))
.setNatureOfContentTermIds(toListOfStrings(instanceJson.getJsonArray(NATURE_OF_CONTENT_TERM_IDS_KEY)))
.setPublicationPeriod(publicationPeriodFromJson(instanceJson.getJsonObject(PUBLICATION_PERIOD_KEY)));
.setPublicationPeriod(publicationPeriodFromJson(instanceJson.getJsonObject(PUBLICATION_PERIOD_KEY)))
.setDates(datesFromJson(instanceJson.getJsonObject(DATES_KEY)));
}

/**
Expand Down Expand Up @@ -237,6 +242,7 @@ public JsonObject getJsonForStorage() {
json.put(TAGS_KEY, new JsonObject().put(TAG_LIST_KEY, new JsonArray(getTags() == null ? Collections.emptyList() : getTags())));
json.put(NATURE_OF_CONTENT_TERM_IDS_KEY, natureOfContentTermIds);
putIfNotNull(json, PUBLICATION_PERIOD_KEY, publicationPeriodToJson(publicationPeriod));
putIfNotNull(json, DATES_KEY, datesToJson(dates));

return json;
}
Expand Down Expand Up @@ -290,6 +296,7 @@ public JsonObject getJsonForResponse(WebContext context) {
putIfNotNull(json, TAGS_KEY, new JsonObject().put(TAG_LIST_KEY, new JsonArray(getTags())));
putIfNotNull(json, NATURE_OF_CONTENT_TERM_IDS_KEY, getNatureOfContentTermIds());
putIfNotNull(json, PUBLICATION_PERIOD_KEY, publicationPeriodToJson(publicationPeriod));
putIfNotNull(json, DATES_KEY, datesToJson(dates));

if (precedingTitles != null) {
JsonArray precedingTitlesJsonArray = new JsonArray();
Expand Down Expand Up @@ -461,6 +468,11 @@ public Instance setElectronicAccess (JsonArray array) {
return this;
}

public Instance setDates(Dates dates) {
this.dates = dates;
return this;
}

public Instance setInstanceFormatIds(List<String> instanceFormatIds) {
this.instanceFormatIds = instanceFormatIds;
return this;
Expand Down Expand Up @@ -747,7 +759,8 @@ public Instance copyWithNewId(String newId) {
.setMetadata(metadata)
.setTags(tags)
.setNatureOfContentTermIds(natureOfContentTermIds)
.setPublicationPeriod(publicationPeriod);
.setPublicationPeriod(publicationPeriod)
.setDates(dates);
}

public Instance copyInstance() {
Expand Down Expand Up @@ -781,7 +794,8 @@ public Instance copyInstance() {
.setMetadata(metadata)
.setTags(tags)
.setNatureOfContentTermIds(natureOfContentTermIds)
.setPublicationPeriod(publicationPeriod);
.setPublicationPeriod(publicationPeriod)
.setDates(dates);
}

public Instance addIdentifier(Identifier identifier) {
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/api/InstancesApiExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import static io.vertx.core.http.HttpMethod.PUT;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.folio.inventory.domain.instances.Dates.DATE_TYPE_ID_KEY;
import static org.folio.inventory.domain.instances.Dates.DATE1_KEY;
import static org.folio.inventory.domain.instances.Dates.DATE2_KEY;
import static org.folio.inventory.domain.instances.Dates.datesToJson;
import static org.folio.inventory.domain.instances.Instance.DATES_KEY;
import static org.folio.inventory.domain.instances.Instance.PRECEDING_TITLES_KEY;
import static org.folio.inventory.domain.instances.Instance.PUBLICATION_PERIOD_KEY;
import static org.folio.inventory.domain.instances.Instance.TAGS_KEY;
Expand Down Expand Up @@ -40,6 +45,7 @@
import org.folio.HttpStatus;
import org.folio.inventory.config.InventoryConfiguration;
import org.folio.inventory.config.InventoryConfigurationImpl;
import org.folio.inventory.domain.instances.Dates;
import org.folio.inventory.domain.instances.PublicationPeriod;
import org.folio.inventory.domain.instances.Subject;
import org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle;
Expand All @@ -64,6 +70,9 @@ public class InstancesApiExamples extends ApiTests {
private static final InventoryConfiguration config = new InventoryConfigurationImpl();
private final String tagNameOne = "important";
private final String tagNameTwo = "very important";
private final String dateTypeId = "0750f52b-3bfc-458d-9307-e9afc8bcdffa";
private final String date1 = "2014";
private final String date2 = "2016";

@After
public void disableFailureEmulation() throws Exception {
Expand Down Expand Up @@ -94,6 +103,7 @@ public void canCreateInstanceWithoutAnIDAndHRID()
.put("instanceTypeId", ApiTestSuite.getTextInstanceType())
.put(TAGS_KEY, new JsonObject().put(TAG_LIST_KEY, new JsonArray().add(tagNameOne)))
.put(PUBLICATION_PERIOD_KEY, publicationPeriodToJson(new PublicationPeriod(1000, 2000)))
.put(DATES_KEY, datesToJson(new Dates(dateTypeId, date1, date2)))
.put("natureOfContentTermIds",
new JsonArray(asList(
ApiTestSuite.getAudiobookNatureOfContentTermId(),
Expand Down Expand Up @@ -162,6 +172,11 @@ public void canCreateInstanceWithoutAnIDAndHRID()
var publicationPeriod = createdInstance.getJsonObject(PUBLICATION_PERIOD_KEY);
assertThat(publicationPeriod.getInteger("start"), is(1000));
assertThat(publicationPeriod.getInteger("end"), is(2000));

var dates = createdInstance.getJsonObject(DATES_KEY);
assertThat(dates.getString(DATE_TYPE_ID_KEY), is(dateTypeId));
assertThat(dates.getString(DATE1_KEY), is(date1));
assertThat(dates.getString(DATE2_KEY), is(date2));
}

@Test
Expand Down Expand Up @@ -395,13 +410,15 @@ public void canUpdateAnExistingInstance()
smallAngryPlanet.put("natureOfContentTermIds",
new JsonArray().add(ApiTestSuite.getBibliographyNatureOfContentTermId()));
smallAngryPlanet.put(PUBLICATION_PERIOD_KEY, publicationPeriodToJson(new PublicationPeriod(1000, 2000)));
smallAngryPlanet.put(DATES_KEY, datesToJson(new Dates(null, date1, date2)));

JsonObject newInstance = createInstance(smallAngryPlanet);

JsonObject updateInstanceRequest = newInstance.copy()
.put("title", "The Long Way to a Small, Angry Planet")
.put(TAGS_KEY, new JsonObject().put(TAG_LIST_KEY, new JsonArray().add(tagNameTwo)))
.put(PUBLICATION_PERIOD_KEY, publicationPeriodToJson(new PublicationPeriod(2000, 2012)))
.put(DATES_KEY, datesToJson(new Dates(dateTypeId, date1, date2)))
.put("natureOfContentTermIds",
new JsonArray().add(ApiTestSuite.getAudiobookNatureOfContentTermId()))
.put("subjects", new JsonArray().add(
Expand Down Expand Up @@ -441,6 +458,11 @@ public void canUpdateAnExistingInstance()
assertThat(publicationPeriod.getInteger("start"), is(2000));
assertThat(publicationPeriod.getInteger("end"), is(2012));

var dates = updatedInstance.getJsonObject(DATES_KEY);
assertThat(dates.getString(DATE_TYPE_ID_KEY), is(dateTypeId));
assertThat(dates.getString(DATE1_KEY), is(date1));
assertThat(dates.getString(DATE2_KEY), is(date2));

var subjects = updatedInstance.getJsonArray("subjects");
var subject = subjects.getJsonObject(0);
assertThat(subjects.size(), is(1));
Expand Down Expand Up @@ -624,7 +646,7 @@ public void canNotUpdateAnExistingMARCInstanceIfBlockedFieldsAreChangedToNulls()
assertThat(errors.size(), is(1));
assertThat(errors.getJsonObject(0).getString("message"), is(
"Instance is controlled by MARC record, these fields are blocked and can not be updated: " +
"physicalDescriptions,notes,languages,precedingTitles,identifiers,instanceTypeId,modeOfIssuanceId,subjects," +
"physicalDescriptions,notes,languages,precedingTitles,identifiers,instanceTypeId,modeOfIssuanceId,subjects,dates," +
"source,title,indexTitle,publicationFrequency,electronicAccess,publicationRange," +
"classifications,succeedingTitles,editions,hrid,series,instanceFormatIds,publication,contributors," +
"alternativeTitles"));
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/folio/inventory/domain/instances/DatesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.folio.inventory.domain.instances;

import io.vertx.core.json.JsonObject;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import junitparams.converters.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.folio.inventory.domain.instances.Dates.datesFromJson;
import static org.folio.inventory.domain.instances.Dates.datesToJson;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

@RunWith(JUnitParamsRunner.class)
public class DatesTest {

@Parameters({
"1, 1990, 2002",
"1, 1990, null",
"1, null, 2022",
"null, 1990, 2002",
"null, 1990, null",
})
@Test
public void shouldCreateDatesFromJson(@Nullable String dateTypeId, @Nullable String date1, @Nullable String date2) {
var dates = datesFromJson(datesJson(dateTypeId, date1, date2));

assertThat(dates.getDateTypeId(), is(dateTypeId));
assertThat(dates.getDate1(), is(date1));
assertThat(dates.getDate2(), is(date2));
}

@Test
public void shouldNotCreateDatesFromJsonWhenJsonIsNull() {
assertThat(datesFromJson(null), nullValue());
}

@Test
public void shouldNotCreateDatesFromJsonWhenAllFieldsAreNull() {
assertThat(datesFromJson(datesJson(null, null, null)), nullValue());
}

@Parameters({
"1, 1990, 2002",
"1, 1990, null",
"1, null, 2022",
"null, 1990, 2002",
"null, 1990, null",
})
@Test
public void shouldConvertDatesToJson(@Nullable String dateTypeId, @Nullable String date1, @Nullable String date2) {
var json = datesToJson(new Dates(dateTypeId, date1, date2));

assertThat(json.getString("dateTypeId"), is(dateTypeId));
assertThat(json.getString("date1"), is(date1));
assertThat(json.getString("date2"), is(date2));
}

@Test
public void shouldNotConvertDatesToJsonWhenItIsNull() {
assertThat(datesToJson(null), nullValue());
}

@Test
public void shouldNotConvertDatesToJsonWhenAllFieldsAreNull() {
assertThat(datesToJson(new Dates(null, null, null)), nullValue());
}

private JsonObject datesJson(String dateTypeId, String date1, String date2) {
return new JsonObject().put("dateTypeId", dateTypeId).put("date1", date1).put("date2", date2);
}
}

0 comments on commit 1b7503a

Please sign in to comment.