Search in sources :

Example 16 with Item

use of org.folio.inventory.domain.items.Item in project mod-inventory by folio-org.

the class UpdateItemEventHandlerTest method shouldReturnFailedWhenOLError.

@Test(expected = ExecutionException.class)
public void shouldReturnFailedWhenOLError() throws UnsupportedEncodingException, InterruptedException, ExecutionException, TimeoutException {
    // given
    doAnswer(invocationOnMock -> {
        MultipleRecords<Item> result = new MultipleRecords<>(new ArrayList<>(), 0);
        Consumer<Success<MultipleRecords<Item>>> successHandler = invocationOnMock.getArgument(2);
        successHandler.accept(new Success<>(result));
        return null;
    }).when(mockedItemCollection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    doAnswer(invocationOnMock -> {
        Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
        failureHandler.accept(new Failure("Cannot update record 601a8dc4-dee7-48eb-b03f-d02fdf0debd0 because it has been changed (optimistic locking): Stored _version is 2, _version of request is 1", 409));
        return null;
    }).when(mockedItemCollection).update(any(), any(), any());
    Item returnedItem = new Item(existingItemJson.getString("id"), "2", UUID.randomUUID().toString(), "test", new Status(AVAILABLE), UUID.randomUUID().toString(), UUID.randomUUID().toString(), new JsonObject());
    when(mockedItemCollection.findById(anyString())).thenReturn(CompletableFuture.completedFuture(returnedItem));
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record()));
    payloadContext.put(ITEM.value(), existingItemJson.encode());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_ITEM_MATCHED.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(payloadContext).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    // when
    CompletableFuture<DataImportEventPayload> future = updateItemHandler.handle(dataImportEventPayload);
    // then
    future.get(5, TimeUnit.SECONDS);
}
Also used : Status(org.folio.inventory.domain.items.Status) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) Item(org.folio.inventory.domain.items.Item) Consumer(java.util.function.Consumer) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) HoldingsRecord(org.folio.HoldingsRecord) Record(org.folio.rest.jaxrs.model.Record) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Example 17 with Item

use of org.folio.inventory.domain.items.Item in project mod-inventory by folio-org.

the class UpdateItemEventHandlerTest method shouldReturnFailedFutureAndUpdateItemExceptStatusWhenStatusCanNotBeUpdated.

@Test
@Parameters({ "Aged to lost", "Awaiting delivery", "Awaiting pickup", "Checked out", "Claimed returned", "Declared lost", "Paged" })
public void shouldReturnFailedFutureAndUpdateItemExceptStatusWhenStatusCanNotBeUpdated(String protectedItemStatus) throws UnsupportedEncodingException {
    // given
    doAnswer(invocationOnMock -> {
        MultipleRecords<Item> result = new MultipleRecords<>(new ArrayList<>(), 0);
        Consumer<Success<MultipleRecords<Item>>> successHandler = invocationOnMock.getArgument(2);
        successHandler.accept(new Success<>(result));
        return null;
    }).when(mockedItemCollection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    String expectedItemBarcode = "BC-123123";
    MappingRule barcodeMappingRule = mappingProfile.getMappingDetails().getMappingFields().get(1);
    when(fakeReader.read(eq(barcodeMappingRule))).thenReturn(StringValue.of(expectedItemBarcode));
    existingItemJson.put(STATUS_KEY, new JsonObject().put("name", protectedItemStatus));
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record()));
    payloadContext.put(ITEM.value(), existingItemJson.encode());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_ITEM_MATCHED.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(payloadContext).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    // when
    CompletableFuture<DataImportEventPayload> future = updateItemHandler.handle(dataImportEventPayload);
    // then
    Assert.assertTrue(future.isCompletedExceptionally());
    JsonObject updatedItem = new JsonObject(payloadContext.get(ITEM.value()));
    Assert.assertEquals(protectedItemStatus, updatedItem.getJsonObject(STATUS_KEY).getString("name"));
    Assert.assertEquals(expectedItemBarcode, updatedItem.getString(ItemUtil.BARCODE));
}
Also used : PagingParameters(org.folio.inventory.common.api.request.PagingParameters) HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MappingRule(org.folio.rest.jaxrs.model.MappingRule) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) Item(org.folio.inventory.domain.items.Item) Consumer(java.util.function.Consumer) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) HoldingsRecord(org.folio.HoldingsRecord) Record(org.folio.rest.jaxrs.model.Record) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) Parameters(junitparams.Parameters) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) Test(org.junit.Test)

Example 18 with Item

use of org.folio.inventory.domain.items.Item in project mod-inventory by folio-org.

the class UpdateItemEventHandlerTest method shouldReturnFailedFutureWhenBarcodeToUpdatedAssignedToAnotherItem.

@Test(expected = ExecutionException.class)
public void shouldReturnFailedFutureWhenBarcodeToUpdatedAssignedToAnotherItem() throws InterruptedException, ExecutionException, TimeoutException, UnsupportedEncodingException {
    // given
    doAnswer(invocationOnMock -> {
        Item itemByCql = new Item(null, null, null, new Status(AVAILABLE), null, null, null);
        MultipleRecords<Item> result = new MultipleRecords<>(Collections.singletonList(itemByCql), 0);
        Consumer<Success<MultipleRecords<Item>>> successHandler = invocationOnMock.getArgument(2);
        successHandler.accept(new Success<>(result));
        return null;
    }).when(mockedItemCollection).findByCql(anyString(), any(PagingParameters.class), any(Consumer.class), any(Consumer.class));
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record()));
    payloadContext.put(ITEM.value(), existingItemJson.encode());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_ITEM_MATCHED.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(payloadContext).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    // when
    CompletableFuture<DataImportEventPayload> future = updateItemHandler.handle(dataImportEventPayload);
    // then
    future.get(5, TimeUnit.SECONDS);
}
Also used : Status(org.folio.inventory.domain.items.Status) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Success(org.folio.inventory.common.domain.Success) DataImportEventPayload(org.folio.DataImportEventPayload) Item(org.folio.inventory.domain.items.Item) Consumer(java.util.function.Consumer) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) HoldingsRecord(org.folio.HoldingsRecord) Record(org.folio.rest.jaxrs.model.Record) Test(org.junit.Test)

Example 19 with Item

use of org.folio.inventory.domain.items.Item in project mod-inventory by folio-org.

the class ItemRepresentationTest method jsonContainsVersion.

@Test
public void jsonContainsVersion() throws Throwable {
    WebContext webContext = when(mock(WebContext.class).absoluteUrl(any())).thenReturn(new URL("http://localhost")).getMock();
    JsonObject instance = new JsonObject().put("contributors", new JsonArray());
    Item item = new Item(UUID.randomUUID().toString(), "123", null, new Status(ItemStatusName.AVAILABLE), null, null, null);
    JsonObject json = new ItemRepresentation("items").toJson(item, null, instance, null, null, null, null, null, null, webContext);
    assertThat(json.getString("_version"), is("123"));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Status(org.folio.inventory.domain.items.Status) Item(org.folio.inventory.domain.items.Item) WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject) URL(java.net.URL) Test(org.junit.Test)

Example 20 with Item

use of org.folio.inventory.domain.items.Item in project mod-inventory by folio-org.

the class ExternalItemCollectionExamples method anItemCanBeDeleted.

@Test
public void anItemCanBeDeleted() throws InterruptedException, ExecutionException, TimeoutException {
    addSomeExamples(collection);
    CompletableFuture<Item> itemToBeDeletedFuture = new CompletableFuture<>();
    collection.add(temeraire(), succeed(itemToBeDeletedFuture), fail(itemToBeDeletedFuture));
    Item itemToBeDeleted = itemToBeDeletedFuture.get();
    CompletableFuture<Void> deleted = new CompletableFuture<>();
    collection.delete(itemToBeDeleted.id, succeed(deleted), fail(deleted));
    waitForCompletion(deleted);
    CompletableFuture<Item> findFuture = new CompletableFuture<>();
    collection.findById(itemToBeDeleted.id, succeed(findFuture), fail(findFuture));
    assertThat(findFuture.get(), is(nullValue()));
    CompletableFuture<MultipleRecords<Item>> findAllFuture = new CompletableFuture<>();
    collection.findAll(PagingParameters.defaults(), succeed(findAllFuture), fail(findAllFuture));
    MultipleRecords<Item> wrappedItems = getOnCompletion(findAllFuture);
    List<Item> allItems = wrappedItems.records;
    assertThat(allItems.size(), is(3));
    assertThat(wrappedItems.totalRecords, is(3));
}
Also used : Item(org.folio.inventory.domain.items.Item) CompletableFuture(java.util.concurrent.CompletableFuture) MultipleRecords(org.folio.inventory.common.domain.MultipleRecords) Test(org.junit.Test)

Aggregations

Item (org.folio.inventory.domain.items.Item)74 Test (org.junit.Test)59 Status (org.folio.inventory.domain.items.Status)41 Parameters (junitparams.Parameters)32 JsonObject (io.vertx.core.json.JsonObject)28 PagingParameters (org.folio.inventory.common.api.request.PagingParameters)28 MultipleRecords (org.folio.inventory.common.domain.MultipleRecords)26 Success (org.folio.inventory.common.domain.Success)24 DataImportEventPayload (org.folio.DataImportEventPayload)23 HashMap (java.util.HashMap)20 Consumer (java.util.function.Consumer)20 CompletableFuture (java.util.concurrent.CompletableFuture)16 Record (org.folio.rest.jaxrs.model.Record)14 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)13 ItemCollection (org.folio.inventory.domain.items.ItemCollection)12 Storage (org.folio.inventory.storage.Storage)11 EventHandler (org.folio.processing.events.services.handler.EventHandler)11 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 HoldingsRecord (org.folio.HoldingsRecord)9