Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Dec 5, 2024
1 parent 670f006 commit 1bf6b8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private void validateFinanceDataFields(FyFinanceData financeData) {
}
}

public Future<Void> processAllocationTransaction(FyFinanceDataCollection fyFinanceDataCollection,
private Future<Void> processAllocationTransaction(FyFinanceDataCollection fyFinanceDataCollection,
RequestContext requestContext) {
var transactionsFuture = fyFinanceDataCollection.getFyFinanceData().stream()
.map(financeData -> createAllocationTransaction(financeData, requestContext))
Expand All @@ -142,7 +142,7 @@ public Future<Void> processAllocationTransaction(FyFinanceDataCollection fyFinan
});
}

public Future<Transaction> createAllocationTransaction(FyFinanceData financeData, RequestContext requestContext) {
private Future<Transaction> createAllocationTransaction(FyFinanceData financeData, RequestContext requestContext) {
var transaction = new Transaction()
.withTransactionType(Transaction.TransactionType.ALLOCATION)
.withId(UUID.randomUUID().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.UUID;

import io.vertx.core.Future;
import org.folio.rest.core.RestClient;
import org.folio.rest.core.models.RequestContext;
import org.folio.rest.jaxrs.model.FiscalYear;
Expand Down Expand Up @@ -162,7 +163,9 @@ void negative_testPutFinanceData_FailureInProcessAllocationTransaction(VertxTest
FyFinanceDataCollection financeData = new FyFinanceDataCollection();
FyFinanceData data = createValidFyFinanceData();
financeData.setFyFinanceData(singletonList(data));
FiscalYear fiscalYear = new FiscalYear().withCurrency("USD");

when(fiscalYearService.getFiscalYearById(any(), any())).thenReturn(succeededFuture(fiscalYear));
when(transactionApiService.processBatch(any(), any())).thenReturn(failedFuture("Process failed"));

financeDataService.putFinanceData(financeData, requestContextMock)
Expand All @@ -175,20 +178,24 @@ void negative_testPutFinanceData_FailureInProcessAllocationTransaction(VertxTest
}

@Test
void testCreateAllocationTransaction(VertxTestContext vertxTestContext) {
void testCreateAllocationTransactionUsingReflection(VertxTestContext vertxTestContext) throws Exception {
FyFinanceData data = createValidFyFinanceData();
FiscalYear fiscalYear = new FiscalYear().withCurrency("USD");

when(fiscalYearService.getFiscalYearById(any(), any())).thenReturn(succeededFuture(fiscalYear));

financeDataService.createAllocationTransaction(data, requestContextMock)
.onComplete(vertxTestContext.succeeding(transaction -> {
assertEquals(Transaction.TransactionType.ALLOCATION, transaction.getTransactionType());
assertEquals(data.getFundId(), transaction.getToFundId());
assertEquals(fiscalYear.getCurrency(), transaction.getCurrency());
assertEquals(150.0, transaction.getAmount()); // Assuming initial allocation is 100 and change is 50
vertxTestContext.completeNow();
}));
// Use reflection to access the private method
var method = FinanceDataService.class.getDeclaredMethod("createAllocationTransaction", FyFinanceData.class, RequestContext.class);
method.setAccessible(true);

Future<Transaction> future = (Future<Transaction>) method.invoke(financeDataService, data, requestContextMock);
future.onComplete(vertxTestContext.succeeding(transaction -> {
assertEquals(Transaction.TransactionType.ALLOCATION, transaction.getTransactionType());
assertEquals(data.getFundId(), transaction.getToFundId());
assertEquals(fiscalYear.getCurrency(), transaction.getCurrency());
assertEquals(150.0, transaction.getAmount()); // Assuming initial allocation is 100 and change is 50
vertxTestContext.completeNow();
}));
}

private FyFinanceData createValidFyFinanceData() {
Expand Down

0 comments on commit 1bf6b8a

Please sign in to comment.