Search in sources :

Example 26 with Failure

use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.

the class CreateItemEventHandlerTest method shouldReturnFailedFutureIfDuplicatedErrorExists.

@Test(expected = ExecutionException.class)
public void shouldReturnFailedFutureIfDuplicatedErrorExists() throws InterruptedException, ExecutionException, TimeoutException, UnsupportedEncodingException {
    // given
    RecordToEntity recordToItem = RecordToEntity.builder().recordId(RECORD_ID).entityId(ITEM_ID).build();
    when(itemIdStorageService.store(any(), any(), any())).thenReturn(Future.succeededFuture(recordToItem));
    Mockito.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(UNIQUE_ID_ERROR_MESSAGE, 400));
        return null;
    }).when(mockedItemCollection).add(any(), any(), any());
    MappingManager.registerReaderFactory(fakeReaderFactory);
    MappingManager.registerWriterFactory(new ItemWriterFactory());
    String expectedHoldingId = UUID.randomUUID().toString();
    JsonObject holdingAsJson = new JsonObject().put("id", expectedHoldingId);
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(EntityType.MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record()));
    payloadContext.put(EntityType.HOLDINGS.value(), holdingAsJson.encode());
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_SRS_MARC_BIB_RECORD_CREATED.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(payloadContext).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    // when
    CompletableFuture<DataImportEventPayload> future = createItemHandler.handle(dataImportEventPayload);
    future.get(5, TimeUnit.SECONDS);
}
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) ItemWriterFactory(org.folio.inventory.dataimport.ItemWriterFactory) RecordToEntity(org.folio.inventory.domain.relationship.RecordToEntity) 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) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Record(org.folio.rest.jaxrs.model.Record) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Example 27 with Failure

use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.

the class CreateMarcHoldingsEventHandlerTest method shouldNotProcessEventEvenIfDuplicatedInventoryStorageErrorExists.

@Test(expected = Exception.class)
public void shouldNotProcessEventEvenIfDuplicatedInventoryStorageErrorExists() throws IOException, InterruptedException, ExecutionException, TimeoutException {
    when(storage.getHoldingsRecordCollection(any())).thenReturn(holdingsRecordsCollection);
    when(storage.getInstanceCollection(any())).thenReturn(instanceRecordCollection);
    doAnswer(invocationOnMock -> {
        Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
        failureHandler.accept(new Failure(UNIQUE_ID_ERROR_MESSAGE, 400));
        return null;
    }).when(holdingsRecordsCollection).add(any(), any(), any());
    HoldingsRecord holdings = new HoldingsRecord().withId(String.valueOf(UUID.randomUUID())).withHrid(String.valueOf(UUID.randomUUID())).withInstanceId(String.valueOf(UUID.randomUUID())).withSourceId(String.valueOf(UUID.randomUUID())).withHoldingsTypeId(String.valueOf(UUID.randomUUID())).withPermanentLocationId(PERMANENT_LOCATION_ID);
    var parsedHoldingsRecord = new JsonObject(TestUtil.readFileFromPath(PARSED_HOLDINGS_RECORD));
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(parsedHoldingsRecord.encode()));
    record.setId(recordId);
    HashMap<String, String> context = new HashMap<>();
    context.put("HOLDINGS", new JsonObject(new ObjectMapper().writer().withDefaultPrettyPrinter().writeValueAsString(holdings)).encode());
    context.put(MARC_HOLDINGS.value(), Json.encode(record));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_SRS_MARC_HOLDING_RECORD_CREATED.value()).withJobExecutionId(UUID.randomUUID().toString()).withOkapiUrl(mockServer.baseUrl()).withContext(context).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    CompletableFuture<DataImportEventPayload> future = createMarcHoldingsEventHandler.handle(dataImportEventPayload);
    future.get(5, TimeUnit.SECONDS);
}
Also used : HashMap(java.util.HashMap) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) DataImportEventPayload(org.folio.DataImportEventPayload) HoldingsRecord(org.folio.HoldingsRecord) HoldingsRecord(org.folio.HoldingsRecord) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Record(org.folio.rest.jaxrs.model.Record) Failure(org.folio.inventory.common.domain.Failure) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 28 with Failure

use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.

the class MarcBibModifiedPostProcessingEventHandlerTest method shouldNotUpdateInstanceIfOLErrorExist.

@Test(expected = ExecutionException.class)
public void shouldNotUpdateInstanceIfOLErrorExist() throws InterruptedException, ExecutionException, TimeoutException {
    HashMap<String, String> payloadContext = new HashMap<>();
    payloadContext.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(record));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_SRS_MARC_BIB_RECORD_MODIFIED_READY_FOR_POST_PROCESSING.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(payloadContext).withOkapiUrl(OKAPI_URL).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    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(mockedInstanceCollection).update(any(), any(), any());
    CompletableFuture<DataImportEventPayload> future = marcBibModifiedEventHandler.handle(dataImportEventPayload);
    future.get(5, TimeUnit.SECONDS);
}
Also used : HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Failure(org.folio.inventory.common.domain.Failure) DataImportEventPayload(org.folio.DataImportEventPayload) Test(org.junit.Test)

Example 29 with Failure

use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.

the class UpdateHoldingEventHandlerTest method shouldNotProcessEventIfOLErrorExist.

@Test(expected = ExecutionException.class)
public void shouldNotProcessEventIfOLErrorExist() throws InterruptedException, ExecutionException, TimeoutException {
    Reader fakeReader = mock(Reader.class);
    String permanentLocationId = UUID.randomUUID().toString();
    String instanceId = String.valueOf(UUID.randomUUID());
    String holdingId = UUID.randomUUID().toString();
    String hrid = UUID.randomUUID().toString();
    HoldingsRecord holdingsRecord = new HoldingsRecord().withId(holdingId).withInstanceId(instanceId).withHrid(hrid).withPermanentLocationId(permanentLocationId);
    when(fakeReader.read(any(MappingRule.class))).thenReturn(StringValue.of(permanentLocationId));
    HoldingsRecord returnedHoldings = new HoldingsRecord().withId(holdingId).withHrid(hrid).withInstanceId(instanceId).withPermanentLocationId(permanentLocationId).withVersion(1);
    when(holdingsRecordsCollection.findById(anyString())).thenReturn(CompletableFuture.completedFuture(returnedHoldings));
    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(holdingsRecordsCollection).update(any(), any(), any());
    when(fakeReaderFactory.createReader()).thenReturn(fakeReader);
    when(storage.getHoldingsRecordCollection(any())).thenReturn(holdingsRecordsCollection);
    MappingManager.registerReaderFactory(fakeReaderFactory);
    MappingManager.registerWriterFactory(new HoldingWriterFactory());
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(PARSED_CONTENT_WITH_INSTANCE_ID));
    HashMap<String, String> context = new HashMap<>();
    context.put(HOLDINGS.value(), Json.encode(holdingsRecord));
    context.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(record));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_HOLDING_UPDATED.value()).withJobExecutionId(UUID.randomUUID().toString()).withContext(context).withProfileSnapshot(profileSnapshotWrapper).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0));
    CompletableFuture<DataImportEventPayload> future = updateHoldingEventHandler.handle(dataImportEventPayload);
    future.get(5, TimeUnit.MILLISECONDS);
}
Also used : HashMap(java.util.HashMap) Reader(org.folio.processing.mapping.mapper.reader.Reader) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) MappingRule(org.folio.rest.jaxrs.model.MappingRule) DataImportEventPayload(org.folio.DataImportEventPayload) HoldingsRecord(org.folio.HoldingsRecord) HoldingWriterFactory(org.folio.inventory.dataimport.HoldingWriterFactory) HoldingsRecord(org.folio.HoldingsRecord) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Record(org.folio.rest.jaxrs.model.Record) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Example 30 with Failure

use of org.folio.inventory.common.domain.Failure in project mod-inventory by folio-org.

the class CreateInstanceEventHandlerTest method shouldNotProcessEventEvenIfInventoryStorageErrorExists.

@Test(expected = Exception.class)
public void shouldNotProcessEventEvenIfInventoryStorageErrorExists() throws InterruptedException, ExecutionException, TimeoutException {
    Reader fakeReader = Mockito.mock(Reader.class);
    String instanceTypeId = "fe19bae4-da28-472b-be90-d442e2428ead";
    String recordId = "567859ad-505a-400d-a699-0028a1fdbf84";
    String instanceId = "4d4545df-b5ba-4031-a031-70b1c1b2fc5d";
    String title = "titleValue";
    RecordToEntity recordToInstance = RecordToEntity.builder().recordId(recordId).entityId(instanceId).build();
    when(fakeReader.read(any(MappingRule.class))).thenReturn(StringValue.of(instanceTypeId), StringValue.of(title));
    when(fakeReaderFactory.createReader()).thenReturn(fakeReader);
    when(storage.getInstanceCollection(any())).thenReturn(instanceRecordCollection);
    when(instanceIdStorageService.store(any(), any(), any())).thenReturn(Future.succeededFuture(recordToInstance));
    doAnswer(invocationOnMock -> {
        Consumer<Failure> failureHandler = invocationOnMock.getArgument(2);
        failureHandler.accept(new Failure("Smth error", 400));
        return null;
    }).when(instanceRecordCollection).add(any(), any(), any());
    MappingManager.registerReaderFactory(fakeReaderFactory);
    MappingManager.registerWriterFactory(new InstanceWriterFactory());
    HashMap<String, String> context = new HashMap<>();
    Record record = new Record().withParsedRecord(new ParsedRecord().withContent(PARSED_CONTENT));
    record.setId(recordId);
    context.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(record));
    DataImportEventPayload dataImportEventPayload = new DataImportEventPayload().withEventType(DI_INVENTORY_INSTANCE_CREATED.value()).withContext(context).withCurrentNode(profileSnapshotWrapper.getChildSnapshotWrappers().get(0)).withTenant(TENANT_ID).withOkapiUrl(mockServer.baseUrl()).withToken(TOKEN).withJobExecutionId(UUID.randomUUID().toString()).withOkapiUrl(mockServer.baseUrl());
    CompletableFuture<DataImportEventPayload> future = createInstanceEventHandler.handle(dataImportEventPayload);
    future.get(5, TimeUnit.SECONDS);
}
Also used : HashMap(java.util.HashMap) Reader(org.folio.processing.mapping.mapper.reader.Reader) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) MappingRule(org.folio.rest.jaxrs.model.MappingRule) RecordToEntity(org.folio.inventory.domain.relationship.RecordToEntity) DataImportEventPayload(org.folio.DataImportEventPayload) InstanceWriterFactory(org.folio.inventory.dataimport.InstanceWriterFactory) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) Record(org.folio.rest.jaxrs.model.Record) Failure(org.folio.inventory.common.domain.Failure) Test(org.junit.Test)

Aggregations

Failure (org.folio.inventory.common.domain.Failure)50 Test (org.junit.Test)44 CompletableFuture (java.util.concurrent.CompletableFuture)26 HashMap (java.util.HashMap)21 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 DataImportEventPayload (org.folio.DataImportEventPayload)17 JsonObject (io.vertx.core.json.JsonObject)16 PagingParameters (org.folio.inventory.common.api.request.PagingParameters)10 Record (org.folio.rest.jaxrs.model.Record)10 ParsedRecord (org.folio.rest.jaxrs.model.ParsedRecord)9 Async (io.vertx.ext.unit.Async)8 Consumer (java.util.function.Consumer)7 HoldingsRecord (org.folio.HoldingsRecord)7 AuthorityRecordCollection (org.folio.inventory.domain.AuthorityRecordCollection)7 InstanceCollection (org.folio.inventory.domain.instances.InstanceCollection)7 ItemCollection (org.folio.inventory.domain.items.ItemCollection)6 EventHandler (org.folio.processing.events.services.handler.EventHandler)6 MultipleRecords (org.folio.inventory.common.domain.MultipleRecords)4 Instance (org.folio.inventory.domain.instances.Instance)4 Reader (org.folio.processing.mapping.mapper.reader.Reader)4