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;
}
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));
}
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);
}
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();
});
}
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());
}
Aggregations