use of org.folio.rest.jaxrs.model.InstancesBatchResponse in project mod-inventory-storage by folio-org.
the class InstanceStorageBatchAPI method postInstanceStorageBatchInstances.
@Validate
@Override
public void postInstanceStorageBatchInstances(Instances entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
final String statusUpdatedDate = generateStatusUpdatedDate();
for (Instance instance : entity.getInstances()) {
instance.setStatusUpdatedDate(statusUpdatedDate);
}
vertxContext.runOnContext(v -> {
try {
PostgresClient postgresClient = PgUtil.postgresClient(vertxContext, okapiHeaders);
final InstanceDomainEventPublisher instanceDomainEventPublisher = new InstanceDomainEventPublisher(vertxContext, okapiHeaders);
MetadataUtil.populateMetadata(entity.getInstances(), okapiHeaders);
executeInBatch(entity.getInstances(), (instances) -> saveInstances(instances, postgresClient)).onComplete(ar -> {
InstancesBatchResponse response = constructResponse(ar.result());
if (!response.getInstances().isEmpty()) {
instanceDomainEventPublisher.publishInstancesCreated(response.getInstances()).onSuccess(notUsed -> asyncResultHandler.handle(succeededFuture(respond201WithApplicationJson(response)))).onFailure(error -> {
log.error("Failed to send events for instances", error);
asyncResultHandler.handle(succeededFuture(respond500WithTextPlain(error.getMessage())));
});
} else {
// return 500 response with the list of errors - not one Instance was created
log.error("Failed to create some of the Instances: " + response.getErrorMessages());
asyncResultHandler.handle(succeededFuture(respond500WithApplicationJson(response)));
}
});
} catch (Exception e) {
log.error("Failed to create Instances", e);
asyncResultHandler.handle(succeededFuture(respond500WithTextPlain(e.getMessage())));
}
});
}
use of org.folio.rest.jaxrs.model.InstancesBatchResponse in project mod-inventory-storage by folio-org.
the class InstanceStorageBatchAPI method constructResponse.
/**
* Iterates through list of completed result futures and constructs InstancesBatchResponse
*
* @param saveFutures list of completed individual result futures
* @return InstancesBatchResponse
*/
private InstancesBatchResponse constructResponse(List<Future> saveFutures) {
InstancesBatchResponse response = new InstancesBatchResponse();
saveFutures.forEach(save -> {
if (save.failed()) {
response.getErrorMessages().add(save.cause().getMessage());
} else {
response.getInstances().add((Instance) save.result());
}
});
response.setTotalRecords(response.getInstances().size());
return response;
}
use of org.folio.rest.jaxrs.model.InstancesBatchResponse in project mod-inventory-storage by folio-org.
the class InstanceStorageTest method cannotCreateACollectionOfInstancesWithHRIDFailure.
@Test
public void cannotCreateACollectionOfInstancesWithHRIDFailure() throws Exception {
log.info("Starting cannotCreateACollectionOfInstancesWithHRIDFailure");
final JsonArray instancesArray = new JsonArray();
final int numberOfInstances = 2;
final UUID[] uuids = new UUID[numberOfInstances];
instancesArray.add(uprooted(uuids[0] = UUID.randomUUID()));
final JsonObject t = temeraire(uuids[1] = UUID.randomUUID());
t.put("hrid", "");
instancesArray.add(t);
final JsonObject instanceCollection = new JsonObject().put(INSTANCES_KEY, instancesArray).put(TOTAL_RECORDS_KEY, numberOfInstances);
setInstanceSequence(99_999_999_999L);
final CompletableFuture<Response> createCompleted = new CompletableFuture<>();
client.post(instancesStorageBatchInstancesUrl(StringUtils.EMPTY), instanceCollection, TENANT_ID, json(createCompleted));
final Response response = createCompleted.get(5, SECONDS);
assertThat(response.getStatusCode(), is(201));
final InstancesBatchResponse ibr = response.getJson().mapTo(InstancesBatchResponse.class);
assertThat(ibr.getErrorMessages(), notNullValue());
assertThat(ibr.getErrorMessages().get(0), isMaximumSequenceValueError("hrid_instances_seq"));
log.info("Finished cannotCreateACollectionOfInstancesWithHRIDFailure");
}
use of org.folio.rest.jaxrs.model.InstancesBatchResponse in project mod-inventory-storage by folio-org.
the class InstanceStorageTest method cannotCreateACollectionOfInstancesWithDuplicatedHRIDs.
@Test
public void cannotCreateACollectionOfInstancesWithDuplicatedHRIDs() throws Exception {
log.info("Starting cannotCreateACollectionOfInstancesWithDuplicatedHRIDs");
final JsonArray instancesArray = new JsonArray();
final int numberOfInstances = 2;
final UUID[] uuids = new UUID[numberOfInstances];
instancesArray.add(uprooted(uuids[0] = UUID.randomUUID()));
final JsonObject t = temeraire(uuids[1] = UUID.randomUUID());
t.put("hrid", "in00000000001");
instancesArray.add(t);
final JsonObject instanceCollection = new JsonObject().put(INSTANCES_KEY, instancesArray).put(TOTAL_RECORDS_KEY, numberOfInstances);
setInstanceSequence(1);
final CompletableFuture<Response> createCompleted = new CompletableFuture<>();
client.post(instancesStorageBatchInstancesUrl(StringUtils.EMPTY), instanceCollection, TENANT_ID, json(createCompleted));
final Response response = createCompleted.get(5, SECONDS);
assertThat(response.getStatusCode(), is(201));
final InstancesBatchResponse ibr = response.getJson().mapTo(InstancesBatchResponse.class);
assertThat(ibr.getErrorMessages(), hasSize(1));
assertThat(ibr.getErrorMessages().get(0), isUniqueViolation("instance_hrid_idx_unique"));
log.info("Finished cannotCreateACollectionOfInstancesWithDuplicatedHRIDs");
}
Aggregations