From 92a62a7951fce16b5bc6238b68577b51c4341d5e Mon Sep 17 00:00:00 2001
From: Dmytro Krutii <dmytro_krutii@epam.com>
Date: Thu, 29 Aug 2024 17:25:50 +0300
Subject: [PATCH] MODINVSTOR-1205 Update mapping of the Instance from/to json

---
 descriptors/ModuleDescriptor-template.json    |  2 +-
 .../inventory/domain/instances/Subject.java   | 27 ++++++++++++++++---
 src/test/java/api/InstancesApiExamples.java   | 14 +++++++++-
 3 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/descriptors/ModuleDescriptor-template.json b/descriptors/ModuleDescriptor-template.json
index 4b65f2502..70d375c20 100644
--- a/descriptors/ModuleDescriptor-template.json
+++ b/descriptors/ModuleDescriptor-template.json
@@ -594,7 +594,7 @@
     },
     {
       "id": "instance-storage",
-      "version": "10.0"
+      "version": "10.0 10.3"
     },
     {
       "id": "instance-storage-batch",
diff --git a/src/main/java/org/folio/inventory/domain/instances/Subject.java b/src/main/java/org/folio/inventory/domain/instances/Subject.java
index 27950f639..e3fd4dab8 100644
--- a/src/main/java/org/folio/inventory/domain/instances/Subject.java
+++ b/src/main/java/org/folio/inventory/domain/instances/Subject.java
@@ -4,17 +4,30 @@
 
 public class Subject extends Authorized {
   // JSON property names
-  public static final String VALUE_KEY = "value";
+  private static final String VALUE_KEY = "value";
+  private static final String SUBJECT_SOURCE_KEY = "subjectSourceId";
+  private static final String SUBJECT_TYPE_KEY = "subjectTypeId";
 
   private final String value;
 
-  public Subject(String value, String authorityId) {
+  private final String subjectSourceId;
+
+  private final String subjectTypeId;
+
+  public Subject(String value, String authorityId, String subjectSourceId, String subjectTypeId) {
     super(authorityId);
     this.value = value;
+    this.subjectSourceId = subjectSourceId;
+    this.subjectTypeId = subjectTypeId;
   }
 
   public Subject(JsonObject json) {
-    this(json.getString(VALUE_KEY), json.getString(AUTHORITY_ID_KEY));
+    this(
+      json.getString(VALUE_KEY),
+      json.getString(AUTHORITY_ID_KEY),
+      json.getString(SUBJECT_SOURCE_KEY),
+      json.getString(SUBJECT_TYPE_KEY)
+    );
   }
 
   public String getAuthorityId() {
@@ -24,4 +37,12 @@ public String getAuthorityId() {
   public String getValue() {
     return value;
   }
+
+  public String getSubjectSourceId() {
+    return subjectSourceId;
+  }
+
+  public String getSubjectTypeId() {
+    return subjectTypeId;
+  }
 }
diff --git a/src/test/java/api/InstancesApiExamples.java b/src/test/java/api/InstancesApiExamples.java
index c33a7bb76..45371f19f 100644
--- a/src/test/java/api/InstancesApiExamples.java
+++ b/src/test/java/api/InstancesApiExamples.java
@@ -41,6 +41,7 @@
 import org.folio.inventory.config.InventoryConfiguration;
 import org.folio.inventory.config.InventoryConfigurationImpl;
 import org.folio.inventory.domain.instances.PublicationPeriod;
+import org.folio.inventory.domain.instances.Subject;
 import org.folio.inventory.domain.instances.titles.PrecedingSucceedingTitle;
 import org.folio.inventory.support.JsonArrayHelper;
 import org.folio.inventory.support.http.ContentType;
@@ -387,6 +388,8 @@ public void canUpdateAnExistingInstance()
     ExecutionException {
 
     UUID id = UUID.randomUUID();
+    final var subjectSourceId = "subjectSourceId";
+    final var subjectTypeId = "subjectTypeId";
 
     JsonObject smallAngryPlanet = smallAngryPlanet(id);
     smallAngryPlanet.put("natureOfContentTermIds",
@@ -400,7 +403,10 @@ public void canUpdateAnExistingInstance()
       .put(TAGS_KEY, new JsonObject().put(TAG_LIST_KEY, new JsonArray().add(tagNameTwo)))
       .put(PUBLICATION_PERIOD_KEY, publicationPeriodToJson(new PublicationPeriod(2000, 2012)))
       .put("natureOfContentTermIds",
-        new JsonArray().add(ApiTestSuite.getAudiobookNatureOfContentTermId()));
+        new JsonArray().add(ApiTestSuite.getAudiobookNatureOfContentTermId()))
+      .put("subjects", new JsonArray().add(
+        new Subject(null,  null, subjectSourceId, subjectTypeId)
+      ));
 
     URL instanceLocation = new URL(String.format("%s/%s", ApiRoot.instances(),
       newInstance.getString("id")));
@@ -434,6 +440,12 @@ public void canUpdateAnExistingInstance()
     var publicationPeriod = updatedInstance.getJsonObject(PUBLICATION_PERIOD_KEY);
     assertThat(publicationPeriod.getInteger("start"), is(2000));
     assertThat(publicationPeriod.getInteger("end"), is(2012));
+
+    var subjects = updatedInstance.getJsonArray("subjects");
+    var subject = subjects.getJsonObject(0);
+    assertThat(subjects.size(), is(1));
+    assertThat(subject.getString(subjectSourceId), is(subjectSourceId));
+    assertThat(subject.getString(subjectTypeId), is(subjectTypeId));
   }
 
   @Test