Skip to content

Commit

Permalink
Merge branch 'master' into MODINV-709
Browse files Browse the repository at this point in the history
  • Loading branch information
JavokhirAbdullayev authored Aug 27, 2024
2 parents 35f6eb1 + afecb4e commit 765a11e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
4 changes: 4 additions & 0 deletions ramls/holdings_update_ownership.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"$ref": "uuid.json"
}
},
"targetLocationId": {
"description": "Id of the location at the target tenant where Holdings will be created.",
"$ref": "uuid.json"
},
"targetTenantId": {
"description": "Target tenant Id, where selected Holdings will be created.",
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private CompletableFuture<List<String>> updateOwnershipOfHoldingsRecords(Holding
if (!jsons.isEmpty()) {
List<JsonObject> validatedHoldingsRecords = validateHoldingsRecords(jsons, holdingsUpdateOwnership.getToInstanceId(), notUpdatedEntities);
List<UpdateOwnershipHoldingsRecordWrapper> holdingsRecordWrappers =
validatedHoldingsRecords.stream().map(this::mapHoldingsRecordWrapper).toList();
validatedHoldingsRecords.stream().map(h -> mapHoldingsRecordWrapper(h, holdingsUpdateOwnership)).toList();

return validateHoldingsRecordsBoundWith(holdingsRecordWrappers, notUpdatedEntities, routingContext, context)
.thenCompose(wrappersWithoutBoundWith -> createHoldings(wrappersWithoutBoundWith, notUpdatedEntities, targetTenantHoldingsRecordCollection))
Expand Down Expand Up @@ -571,10 +571,13 @@ private String getTargetHoldingId(JsonObject itemJson, List<UpdateOwnershipHoldi
.orElse(null);
}

private UpdateOwnershipHoldingsRecordWrapper mapHoldingsRecordWrapper(JsonObject holdingsRecordJson) {
private UpdateOwnershipHoldingsRecordWrapper mapHoldingsRecordWrapper(JsonObject holdingsRecordJson, HoldingsUpdateOwnership holdingsUpdateOwnership) {
MoveApiUtil.removeExtraRedundantFields(holdingsRecordJson);

HoldingsRecord holdingsRecord = holdingsRecordJson.mapTo(HoldingsRecord.class).withHrid(null);
HoldingsRecord holdingsRecord = holdingsRecordJson.mapTo(HoldingsRecord.class)
.withHrid(null)
.withPermanentLocationId(holdingsUpdateOwnership.getTargetLocationId());

String sourceHoldingsRecordId = holdingsRecord.getId();

return new UpdateOwnershipHoldingsRecordWrapper(sourceHoldingsRecordId, holdingsRecord.withId(UUID.randomUUID().toString()));
Expand Down
56 changes: 39 additions & 17 deletions src/test/java/api/holdings/HoldingsUpdateOwnershipApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.TimeoutException;

import static api.ApiTestSuite.createConsortiumTenant;
import static api.ApiTestSuite.getMainLibraryLocation;
import static api.BoundWithTests.makeObjectBoundWithPart;
import static api.support.InstanceSamples.smallAngryPlanet;
import static org.folio.inventory.domain.instances.InstanceSource.CONSORTIUM_FOLIO;
Expand All @@ -37,6 +38,7 @@
import static org.folio.inventory.resources.UpdateOwnershipApi.HOLDINGS_RECORD_NOT_LINKED_TO_SHARED_INSTANCE;
import static org.folio.inventory.resources.UpdateOwnershipApi.HOLDING_BOUND_WITH_PARTS_ERROR;
import static org.folio.inventory.support.ItemUtil.HOLDINGS_RECORD_ID;
import static org.folio.inventory.support.ItemUtil.PERMANENT_LOCATION_ID_KEY;
import static org.folio.inventory.support.http.ContentType.APPLICATION_JSON;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -74,7 +76,7 @@ public void canUpdateHoldingsOwnershipToDifferentTenant() throws MalformedURLExc
final UUID createHoldingsRecord2 = createHoldingForInstance(instanceId);

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand All @@ -90,12 +92,14 @@ public void canUpdateHoldingsOwnershipToDifferentTenant() throws MalformedURLExc

assertEquals(HttpStatus.SC_NOT_FOUND, sourceTenantHoldingsRecord1.getStatusCode());
assertEquals(instanceId.toString(), targetTenantHoldingsRecord1.getString(INSTANCE_ID));
assertEquals(getMainLibraryLocation(), targetTenantHoldingsRecord1.getString(PERMANENT_LOCATION_ID_KEY));

Response sourceTenantHoldingsRecord2 = holdingsStorageClient.getById(createHoldingsRecord2);
JsonObject targetTenantHoldingsRecord2 = targetTenantHoldings.get(1);

assertEquals(HttpStatus.SC_NOT_FOUND, sourceTenantHoldingsRecord2.getStatusCode());
assertEquals(instanceId.toString(), targetTenantHoldingsRecord2.getString(INSTANCE_ID));
assertEquals(getMainLibraryLocation(), targetTenantHoldingsRecord2.getString(PERMANENT_LOCATION_ID_KEY));
assertNull(targetTenantHoldingsRecord2.getString("hrid"));
}

Expand Down Expand Up @@ -125,7 +129,7 @@ public void canUpdateHoldingsOwnershipWithRelatedItemsToDifferentTenant() throws
.withStatus(ItemStatusName.AVAILABLE.value()));

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand All @@ -143,13 +147,15 @@ public void canUpdateHoldingsOwnershipWithRelatedItemsToDifferentTenant() throws
assertEquals(HttpStatus.SC_NOT_FOUND, sourceTenantHoldingsRecord1.getStatusCode());
assertEquals(instanceId.toString(), targetTenantHoldingsRecord1.getString(INSTANCE_ID));
assertNotEquals(createHoldingsRecord1.toString(), targetTenantHoldingsRecord1.getString(ID));
assertEquals(getMainLibraryLocation(), targetTenantHoldingsRecord1.getString(PERMANENT_LOCATION_ID_KEY));

Response sourceTenantHoldingsRecord2 = holdingsStorageClient.getById(createHoldingsRecord2);
JsonObject targetTenantHoldingsRecord2 = targetTenantHoldings.get(1);

assertEquals(HttpStatus.SC_NOT_FOUND, sourceTenantHoldingsRecord2.getStatusCode());
assertEquals(instanceId.toString(), targetTenantHoldingsRecord2.getString(INSTANCE_ID));
assertNotEquals(createHoldingsRecord2.toString(), targetTenantHoldingsRecord2.getString(ID));
assertEquals(getMainLibraryLocation(), targetTenantHoldingsRecord2.getString(PERMANENT_LOCATION_ID_KEY));

// Verify related Items ownership updated
Response sourceTenantItem1 = itemsClient.getById(firstItem.getId());
Expand Down Expand Up @@ -209,7 +215,7 @@ public void canUpdateHoldingsOwnershipIfErrorUpdatingRelatedItemsToDifferentTena
.setMethod(HttpMethod.DELETE.name()));

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down Expand Up @@ -271,7 +277,7 @@ public void canUpdateHoldingsOwnershipIfErrorDeletingRelatedItems() throws Malfo
.setMethod(HttpMethod.DELETE.name()));

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down Expand Up @@ -323,7 +329,7 @@ public void shouldReportErrorsWhenOnlySomeRequestedHoldingsRecordsCouldNotBeUpda
assertNotEquals(createHoldingsRecord1, createHoldingsRecord2);

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down Expand Up @@ -380,7 +386,7 @@ public void shouldReportErrorWhenOnlySomeRequestedHoldingsRecordHasRelatedBoundW
boundWithPartsStorageClient.create(boundWithPart);

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down Expand Up @@ -447,7 +453,7 @@ public void shouldReportErrorsWhenOnlySomeRequestedHoldingsRecordsNotLinkedToSha
assertNotEquals(createHoldingsRecord1, createHoldingsRecord2);

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId1,
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down Expand Up @@ -476,7 +482,7 @@ public void shouldReportErrorsWhenOnlySomeRequestedHoldingsRecordsNotLinkedToSha
public void cannotUpdateHoldingsRecordsOwnershipToUnspecifiedInstance()
throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
JsonObject holdingsRecordUpdateOwnershipWithoutToInstanceId = new HoldingsRecordUpdateOwnershipRequestBuilder(null,
new JsonArray(List.of(UUID.randomUUID())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(UUID.randomUUID())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipWithoutToInstanceId);

Expand All @@ -492,7 +498,7 @@ public void cannotUpdateHoldingsRecordsOwnershipToUnspecifiedInstance()
public void cannotUpdateHoldingsRecordsOwnershipToUnspecifiedTenant()
throws InterruptedException, MalformedURLException, TimeoutException, ExecutionException {
JsonObject holdingsRecordUpdateOwnershipWithoutTenantId = new HoldingsRecordUpdateOwnershipRequestBuilder(UUID.randomUUID(),
new JsonArray(List.of(UUID.randomUUID())), null).create();
new JsonArray(List.of(UUID.randomUUID())), UUID.randomUUID(), null).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipWithoutTenantId);

Expand All @@ -508,7 +514,7 @@ public void cannotUpdateHoldingsRecordsOwnershipToUnspecifiedTenant()
public void cannotUpdateHoldingsRecordOwnershipToSameTenant()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(UUID.randomUUID(),
new JsonArray(List.of(UUID.randomUUID().toString())), ApiTestSuite.TENANT_ID).create();
new JsonArray(List.of(UUID.randomUUID().toString())), UUID.randomUUID(), ApiTestSuite.TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand All @@ -524,7 +530,7 @@ public void cannotUpdateHoldingsRecordOwnershipToSameTenant()
public void cannotUpdateUnspecifiedHoldingsRecordsOwnership()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
JsonObject holdingsRecordUpdateOwnershipWithoutHoldingsRecordIds = new HoldingsRecordUpdateOwnershipRequestBuilder(UUID.randomUUID(),
new JsonArray(), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipWithoutHoldingsRecordIds);

Expand All @@ -536,13 +542,29 @@ public void cannotUpdateUnspecifiedHoldingsRecordsOwnership()
));
}

@Test
public void cannotUpdateHoldingsRecordOwnershipToUnspecifiedTargetLocation()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
JsonObject holdingsRecordUpdateOwnershipWithoutHoldingsRecordIds = new HoldingsRecordUpdateOwnershipRequestBuilder(UUID.randomUUID(),
new JsonArray(List.of(UUID.randomUUID().toString())), null, ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipWithoutHoldingsRecordIds);

assertThat(postHoldingsUpdateOwnershipResponse.getStatusCode(), is(422));
assertThat(postHoldingsUpdateOwnershipResponse.getContentType(), containsString(APPLICATION_JSON));

assertThat(postHoldingsUpdateOwnershipResponse, hasValidationError(
"targetLocationId is a required field", "targetLocationId", null
));
}

@Test
public void cannotUpdateHoldingsRecordOwnershipIfTenantNotInConsortium()
throws MalformedURLException, InterruptedException, ExecutionException, TimeoutException {
userTenantsClient.deleteAll();

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(UUID.randomUUID(),
new JsonArray(List.of(UUID.randomUUID().toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(UUID.randomUUID().toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand All @@ -566,7 +588,7 @@ public void cannotUpdateHoldingsRecordOwnershipOfNonExistedInstance()
final UUID createHoldingsRecord1 = createHoldingForInstance(instanceId);

JsonObject holdingsRecordUpdateOwnershipWithoutHoldingsRecordIds = new HoldingsRecordUpdateOwnershipRequestBuilder(invalidInstanceId,
new JsonArray(List.of(createHoldingsRecord1)), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1)), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipWithoutHoldingsRecordIds);

Expand All @@ -588,7 +610,7 @@ public void cannotUpdateHoldingsRecordOwnershipOfNonSharedInstance()
final UUID createHoldingsRecord2 = createHoldingForInstance(instanceId);

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down Expand Up @@ -617,7 +639,7 @@ public void cannotUpdateHoldingsRecordOwnershipDueToHoldingsRecordCreateError()
.setMethod(HttpMethod.POST.name()));

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down Expand Up @@ -660,7 +682,7 @@ public void cannotUpdateHoldingsRecordOwnershipDueToHoldingsRecordDeleteError()
.setMethod(HttpMethod.DELETE.name()));

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down Expand Up @@ -707,7 +729,7 @@ public void canUpdateHoldingsRecordOwnershipToDifferentInstanceWithExtraRedundan
.getId();

JsonObject holdingsRecordUpdateOwnershipRequestBody = new HoldingsRecordUpdateOwnershipRequestBuilder(instanceId,
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), ApiTestSuite.COLLEGE_TENANT_ID).create();
new JsonArray(List.of(createHoldingsRecord1.toString(), createHoldingsRecord2.toString())), UUID.fromString(getMainLibraryLocation()), ApiTestSuite.COLLEGE_TENANT_ID).create();

Response postHoldingsUpdateOwnershipResponse = updateHoldingsRecordsOwnership(holdingsRecordUpdateOwnershipRequestBody);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
import static org.folio.inventory.support.MoveApiUtil.TARGET_TENANT_ID;

public class HoldingsRecordUpdateOwnershipRequestBuilder extends AbstractBuilder {

private static final String TARGET_LOCATION_ID = "targetLocationId";
private final UUID toInstanceId;
private final JsonArray holdingsRecordsIds;
private final String tenantId;
private final UUID targetLocationId;

public HoldingsRecordUpdateOwnershipRequestBuilder(UUID toInstanceId, JsonArray holdingsRecordsIds, String tenantId) {
public HoldingsRecordUpdateOwnershipRequestBuilder(UUID toInstanceId, JsonArray holdingsRecordsIds, UUID targetLocationId, String tenantId) {
this.toInstanceId = toInstanceId;
this.holdingsRecordsIds = holdingsRecordsIds;
this.tenantId = tenantId;
this.targetLocationId = targetLocationId;
}

public JsonObject create() {
Expand All @@ -28,6 +30,7 @@ public JsonObject create() {
includeWhenPresent(holdingsRecordUpdateOwnershipRequest, TO_INSTANCE_ID, toInstanceId);
includeWhenPresent(holdingsRecordUpdateOwnershipRequest, HOLDINGS_RECORD_IDS, holdingsRecordsIds);
includeWhenPresent(holdingsRecordUpdateOwnershipRequest, TARGET_TENANT_ID, tenantId);
includeWhenPresent(holdingsRecordUpdateOwnershipRequest, TARGET_LOCATION_ID, targetLocationId);

return holdingsRecordUpdateOwnershipRequest;
}
Expand Down

0 comments on commit 765a11e

Please sign in to comment.