Search in sources :

Example 6 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class RestClient method put.

public <T> CompletableFuture<Void> put(RequestEntry requestEntry, T entity, RequestContext requestContext) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    String endpoint = requestEntry.buildEndpoint();
    JsonObject recordData = JsonObject.mapFrom(entity);
    if (logger.isDebugEnabled()) {
        logger.debug("Sending 'PUT {}' with body: {}", endpoint, recordData.encodePrettily());
    }
    HttpClientInterface client = getHttpClient(requestContext.getHeaders());
    setDefaultHeaders(client);
    try {
        client.request(HttpMethod.PUT, recordData.toBuffer(), endpoint, requestContext.getHeaders()).thenAccept(HelperUtils::verifyResponse).thenAccept(avoid -> {
            client.closeClient();
            future.complete(null);
        }).exceptionally(t -> {
            client.closeClient();
            future.completeExceptionally(t.getCause());
            logger.error("'PUT {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), t.getCause());
            return null;
        });
    } catch (Exception e) {
        logger.error("'PUT {}' request failed. Request body: {}", endpoint, recordData.encodePrettily(), e);
        client.closeClient();
        future.completeExceptionally(e);
    }
    return future;
}
Also used : HelperUtils(org.folio.invoices.utils.HelperUtils) RequestEntry(org.folio.rest.core.models.RequestEntry) OKAPI_HEADER_TENANT(org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT) CompletableFuture(java.util.concurrent.CompletableFuture) TenantTool(org.folio.rest.tools.utils.TenantTool) HttpClientFactory(org.folio.rest.tools.client.HttpClientFactory) Logger(org.apache.logging.log4j.Logger) HttpClientInterface(org.folio.rest.tools.client.interfaces.HttpClientInterface) HttpMethod(io.vertx.core.http.HttpMethod) Map(java.util.Map) RequestContext(org.folio.rest.core.models.RequestContext) Optional(java.util.Optional) HelperUtils.verifyAndExtractBody(org.folio.invoices.utils.HelperUtils.verifyAndExtractBody) JsonObject(io.vertx.core.json.JsonObject) Objects.nonNull(java.util.Objects.nonNull) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) RestConstants(org.folio.rest.RestConstants) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientInterface(org.folio.rest.tools.client.interfaces.HttpClientInterface) HelperUtils(org.folio.invoices.utils.HelperUtils) JsonObject(io.vertx.core.json.JsonObject)

Example 7 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class BaseTransactionServiceTest method testShouldSuccessRetrieveExistedTransactions.

@Test
public void testShouldSuccessRetrieveExistedTransactions() throws Exception {
    // given
    BaseTransactionService service = spy(new BaseTransactionService(restClient));
    JsonObject encumbranceList = new JsonObject(getMockData(MOCK_ENCUMBRANCES_LIST));
    List<Transaction> encumbrances = encumbranceList.getJsonArray("transactions").stream().map(obj -> ((JsonObject) obj).mapTo(Transaction.class)).collect(toList());
    TransactionCollection trCollection = new TransactionCollection().withTransactions(encumbrances);
    RequestContext requestContext = new RequestContext(ctxMock, okapiHeaders);
    doReturn(completedFuture(trCollection)).when(restClient).get(any(RequestEntry.class), any(RequestContext.class), eq(TransactionCollection.class));
    // When
    List<String> transactionIds = encumbrances.stream().map(Transaction::getId).collect(toList());
    List<Transaction> actEncumbrances = service.getTransactions(transactionIds, requestContext).join();
    // Then
    assertThat(actEncumbrances, hasSize(1));
    assertThat(actEncumbrances.get(0).getId(), equalTo(encumbrances.get(0).getId()));
    verify(restClient).get(any(RequestEntry.class), any(RequestContext.class), eq(TransactionCollection.class));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RestClient(org.folio.rest.core.RestClient) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Header(io.restassured.http.Header) OKAPI_HEADER_TENANT(org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) Transaction(org.folio.rest.acq.model.finance.Transaction) TransactionCollection(org.folio.rest.acq.model.finance.TransactionCollection) ArrayList(java.util.ArrayList) MockitoAnnotations(org.mockito.MockitoAnnotations) Times(org.mockito.internal.verification.Times) Map(java.util.Map) RequestContext(org.folio.rest.core.models.RequestContext) Matchers.hasSize(org.hamcrest.Matchers.hasSize) JsonObject(io.vertx.core.json.JsonObject) OKAPI_URL(org.folio.rest.RestConstants.OKAPI_URL) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Mockito.doReturn(org.mockito.Mockito.doReturn) RequestEntry(org.folio.rest.core.models.RequestEntry) MOCK_ENCUMBRANCES_LIST(org.folio.TestMockDataConstants.MOCK_ENCUMBRANCES_LIST) ApiTestBase(org.folio.rest.impl.ApiTestBase) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) EventLoopContext(io.vertx.core.impl.EventLoopContext) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TransactionCollection(org.folio.rest.acq.model.finance.TransactionCollection) Transaction(org.folio.rest.acq.model.finance.Transaction) JsonObject(io.vertx.core.json.JsonObject) RequestContext(org.folio.rest.core.models.RequestContext) RequestEntry(org.folio.rest.core.models.RequestEntry) Test(org.junit.jupiter.api.Test)

Example 8 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class BatchVoucherGenerateServiceTest method positiveGenerateBatchVoucherTest.

@Test
public void positiveGenerateBatchVoucherTest() throws IOException, ExecutionException, InterruptedException {
    RestClient restClient = new RestClient();
    VoucherRetrieveService voucherRetrieveService = new VoucherRetrieveService(restClient);
    ConfigurationService configurationService = new ConfigurationService(new RestClient());
    VoucherCommandService voucherCommandService = new VoucherCommandService(restClient, new VoucherNumberService(new RestClient()), voucherRetrieveService, new VoucherValidator(), configurationService, new ExchangeRateProviderResolver());
    VendorRetrieveService vendorRetrieveService = new VendorRetrieveService(restClient);
    AddressConverter addressConverter = AddressConverter.getInstance();
    VoucherService voucherService = new VoucherService(voucherRetrieveService, voucherCommandService, vendorRetrieveService, addressConverter);
    InvoiceLinesRetrieveService invoiceLinesRetrieveService = new InvoiceLinesRetrieveService(new InvoiceLineService(restClient));
    InvoiceLineService invoiceLineService = new InvoiceLineService(new RestClient());
    OrderLineService orderLineService = new OrderLineService(restClient);
    InvoiceService invoiceService = new BaseInvoiceService(new RestClient(), invoiceLineService, new OrderService(new RestClient(), invoiceLineService, orderLineService));
    InvoiceRetrieveService invoiceRetrieveService = new InvoiceRetrieveService(invoiceService);
    BatchVoucherGenerateService service = new BatchVoucherGenerateService(okapiHeaders, context, "en", vendorRetrieveService, invoiceRetrieveService, invoiceLinesRetrieveService, voucherService, addressConverter);
    BatchVoucherExport batchVoucherExport = new JsonObject(getMockData(BATCH_VOUCHER_EXPORT_SAMPLE_PATH)).mapTo(BatchVoucherExport.class);
    CompletableFuture<BatchVoucher> future = service.generateBatchVoucher(batchVoucherExport, new RequestContext(context, okapiHeaders));
    BatchVoucher batchVoucher = future.get();
    Assertions.assertNotNull(batchVoucher);
}
Also used : VendorRetrieveService(org.folio.services.VendorRetrieveService) RestClient(org.folio.rest.core.RestClient) AddressConverter(org.folio.converters.AddressConverter) InvoiceService(org.folio.services.invoice.InvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) JsonObject(io.vertx.core.json.JsonObject) VoucherService(org.folio.rest.impl.VoucherService) ExchangeRateProviderResolver(org.folio.services.exchange.ExchangeRateProviderResolver) InvoiceLinesRetrieveService(org.folio.services.InvoiceLinesRetrieveService) OrderLineService(org.folio.services.order.OrderLineService) VoucherValidator(org.folio.services.validator.VoucherValidator) BatchVoucher(org.folio.rest.jaxrs.model.BatchVoucher) InvoiceLineService(org.folio.services.invoice.InvoiceLineService) ConfigurationService(org.folio.services.configuration.ConfigurationService) RequestContext(org.folio.rest.core.models.RequestContext) OrderService(org.folio.services.order.OrderService) InvoiceRetrieveService(org.folio.services.InvoiceRetrieveService) BatchVoucherExport(org.folio.rest.jaxrs.model.BatchVoucherExport) Test(org.junit.jupiter.api.Test)

Example 9 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class BatchVoucherGenerateServiceTest method negativeGetBatchVoucherIfVouchersIsAbsentTest.

@Test
public void negativeGetBatchVoucherIfVouchersIsAbsentTest() {
    Assertions.assertThrows(CompletionException.class, () -> {
        RestClient restClient = new RestClient();
        VoucherRetrieveService voucherRetrieveService = new VoucherRetrieveService(restClient);
        ConfigurationService configurationService = new ConfigurationService(restClient);
        VoucherCommandService voucherCommandService = new VoucherCommandService(restClient, new VoucherNumberService(restClient), voucherRetrieveService, new VoucherValidator(), configurationService, new ExchangeRateProviderResolver());
        VendorRetrieveService vendorRetrieveService = new VendorRetrieveService(restClient);
        AddressConverter addressConverter = AddressConverter.getInstance();
        VoucherService voucherService = new VoucherService(voucherRetrieveService, voucherCommandService, vendorRetrieveService, addressConverter);
        InvoiceLinesRetrieveService invoiceLinesRetrieveService = new InvoiceLinesRetrieveService(new InvoiceLineService(restClient));
        InvoiceLineService invoiceLineService = new InvoiceLineService(new RestClient());
        OrderLineService orderLineService = new OrderLineService(restClient);
        InvoiceService invoiceService = new BaseInvoiceService(new RestClient(), invoiceLineService, new OrderService(new RestClient(), invoiceLineService, orderLineService));
        InvoiceRetrieveService invoiceRetrieveService = new InvoiceRetrieveService(invoiceService);
        BatchVoucherGenerateService service = new BatchVoucherGenerateService(okapiHeaders, context, "en", vendorRetrieveService, invoiceRetrieveService, invoiceLinesRetrieveService, voucherService, addressConverter);
        BatchVoucherExport batchVoucherExport = new BatchVoucherExport();
        CompletableFuture<BatchVoucher> future = service.generateBatchVoucher(batchVoucherExport, new RequestContext(context, okapiHeaders));
        future.join();
    });
}
Also used : VendorRetrieveService(org.folio.services.VendorRetrieveService) RestClient(org.folio.rest.core.RestClient) AddressConverter(org.folio.converters.AddressConverter) InvoiceService(org.folio.services.invoice.InvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) BaseInvoiceService(org.folio.services.invoice.BaseInvoiceService) VoucherService(org.folio.rest.impl.VoucherService) ExchangeRateProviderResolver(org.folio.services.exchange.ExchangeRateProviderResolver) InvoiceLinesRetrieveService(org.folio.services.InvoiceLinesRetrieveService) OrderLineService(org.folio.services.order.OrderLineService) VoucherValidator(org.folio.services.validator.VoucherValidator) BatchVoucher(org.folio.rest.jaxrs.model.BatchVoucher) InvoiceLineService(org.folio.services.invoice.InvoiceLineService) ConfigurationService(org.folio.services.configuration.ConfigurationService) RequestContext(org.folio.rest.core.models.RequestContext) OrderService(org.folio.services.order.OrderService) InvoiceRetrieveService(org.folio.services.InvoiceRetrieveService) BatchVoucherExport(org.folio.rest.jaxrs.model.BatchVoucherExport) Test(org.junit.jupiter.api.Test)

Example 10 with RequestContext

use of org.folio.rest.core.models.RequestContext in project mod-invoice by folio-org.

the class InvoiceLinesRetrieveServiceTest method positiveGetInvoiceLinesByChunksTest.

@Test
public void positiveGetInvoiceLinesByChunksTest() throws IOException, ExecutionException, InterruptedException {
    RestClient restClient = new RestClient();
    InvoiceLinesRetrieveService service = new InvoiceLinesRetrieveService(new InvoiceLineService(restClient));
    JsonObject vouchersList = new JsonObject(getMockData(VOUCHERS_LIST_PATH));
    List<Voucher> vouchers = vouchersList.getJsonArray("vouchers").stream().map(obj -> ((JsonObject) obj).mapTo(Voucher.class)).collect(toList());
    vouchers.remove(1);
    CompletableFuture<List<InvoiceLineCollection>> future = service.getInvoiceLineByChunks(vouchers, new RequestContext(context, okapiHeaders));
    List<InvoiceLineCollection> lineCollections = future.get();
    Assertions.assertEquals(3, lineCollections.get(0).getInvoiceLines().size());
}
Also used : RestClient(org.folio.rest.core.RestClient) BeforeEach(org.junit.jupiter.api.BeforeEach) InvoiceLine(org.folio.rest.jaxrs.model.InvoiceLine) Vertx(io.vertx.core.Vertx) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) VoucherCollection(org.folio.rest.jaxrs.model.VoucherCollection) ApiTestSuite.mockPort(org.folio.ApiTestSuite.mockPort) Context(io.vertx.core.Context) ApiTestBase(org.folio.rest.impl.ApiTestBase) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Voucher(org.folio.rest.jaxrs.model.Voucher) Map(java.util.Map) RequestContext(org.folio.rest.core.models.RequestContext) Assertions(org.junit.jupiter.api.Assertions) InvoiceLineService(org.folio.services.invoice.InvoiceLineService) JsonObject(io.vertx.core.json.JsonObject) OKAPI_URL(org.folio.rest.RestConstants.OKAPI_URL) RestClient(org.folio.rest.core.RestClient) JsonObject(io.vertx.core.json.JsonObject) InvoiceLineService(org.folio.services.invoice.InvoiceLineService) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) RequestContext(org.folio.rest.core.models.RequestContext) InvoiceLineCollection(org.folio.rest.jaxrs.model.InvoiceLineCollection) Voucher(org.folio.rest.jaxrs.model.Voucher) Test(org.junit.jupiter.api.Test)

Aggregations

RequestContext (org.folio.rest.core.models.RequestContext)165 JsonObject (io.vertx.core.json.JsonObject)109 CompletableFuture (java.util.concurrent.CompletableFuture)105 Map (java.util.Map)100 List (java.util.List)92 Logger (org.apache.logging.log4j.Logger)85 LogManager (org.apache.logging.log4j.LogManager)83 Collections (java.util.Collections)73 ArrayList (java.util.ArrayList)72 Context (io.vertx.core.Context)71 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)69 Collectors.toList (java.util.stream.Collectors.toList)68 BeforeEach (org.junit.jupiter.api.BeforeEach)63 CompositePoLine (org.folio.rest.jaxrs.model.CompositePoLine)56 Optional (java.util.Optional)53 StringUtils (org.apache.commons.lang3.StringUtils)53 Autowired (org.springframework.beans.factory.annotation.Autowired)51 Piece (org.folio.rest.jaxrs.model.Piece)49 Error (org.folio.rest.jaxrs.model.Error)46 RequestEntry (org.folio.rest.core.models.RequestEntry)45