diff --git a/src/test/java/org/folio/ApiTestSuite.java b/src/test/java/org/folio/ApiTestSuite.java index d1520b66e..d2aefbb72 100644 --- a/src/test/java/org/folio/ApiTestSuite.java +++ b/src/test/java/org/folio/ApiTestSuite.java @@ -42,20 +42,24 @@ import org.folio.service.PrefixServiceTest; import org.folio.service.ReasonForClosureServiceTest; import org.folio.service.SuffixServiceTest; +import org.folio.service.TagServiceTest; import org.folio.service.consortium.SharingInstanceServiceTest; import org.folio.service.exchange.ManualExchangeRateProviderTest; import org.folio.service.expenceclass.ExpenseClassValidationServiceTest; +import org.folio.service.finance.FiscalYearServiceTest; import org.folio.service.finance.FundServiceTest; import org.folio.service.finance.budget.BudgetRestrictionServiceTest; import org.folio.service.finance.rollover.LedgerRolloverErrorServiceTest; import org.folio.service.finance.rollover.LedgerRolloverProgressServiceTest; import org.folio.service.finance.rollover.LedgerRolloverServiceTest; +import org.folio.service.finance.transaction.ClosedToOpenEncumbranceStrategyTest; import org.folio.service.finance.transaction.EncumbranceRelationsHoldersBuilderTest; import org.folio.service.finance.transaction.EncumbranceServiceTest; import org.folio.service.finance.transaction.OpenToClosedEncumbranceStrategyTest; import org.folio.service.finance.transaction.OpenToPendingEncumbranceStrategyTest; import org.folio.service.finance.transaction.PendingToOpenEncumbranceStrategyTest; import org.folio.service.finance.transaction.TransactionServiceTest; +import org.folio.service.finance.transaction.TransactionSummariesServiceTest; import org.folio.service.inventory.HoldingsSummaryServiceTest; import org.folio.service.inventory.InventoryManagerTest; import org.folio.service.invoice.InvoiceLineServiceTest; @@ -451,4 +455,19 @@ class LedgerRolloverErrorServiceTestNested extends LedgerRolloverErrorServiceTes class LedgerRolloverProgressServiceTestNested extends LedgerRolloverProgressServiceTest { } + @Nested + class TransactionSummariesServiceTestNested extends TransactionSummariesServiceTest { + } + + @Nested + class ClosedToOpenEncumbranceStrategyTestNested extends ClosedToOpenEncumbranceStrategyTest { + } + + @Nested + class FiscalYearServiceTestNested extends FiscalYearServiceTest { + } + + @Nested + class TagServiceTestNested extends TagServiceTest { + } } diff --git a/src/test/java/org/folio/service/TagServiceTest.java b/src/test/java/org/folio/service/TagServiceTest.java index e08b5c288..df5f1a6a0 100644 --- a/src/test/java/org/folio/service/TagServiceTest.java +++ b/src/test/java/org/folio/service/TagServiceTest.java @@ -1,61 +1,40 @@ package org.folio.service; import static io.vertx.core.Future.succeededFuture; -import static org.folio.TestConfig.mockPort; -import static org.folio.TestConstants.X_OKAPI_TOKEN; -import static org.folio.TestConstants.X_OKAPI_USER_ID; -import static org.folio.rest.RestConstants.OKAPI_URL; -import static org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT; -import static org.folio.rest.impl.MockServer.BASE_MOCK_DATA_PATH; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - +import io.vertx.core.Future; +import io.vertx.junit5.VertxExtension; +import io.vertx.junit5.VertxTestContext; import org.folio.rest.acq.model.tag.Tag; import org.folio.rest.acq.model.tag.TagCollection; import org.folio.rest.core.RestClient; import org.folio.rest.core.models.RequestContext; +import org.folio.rest.core.models.RequestEntry; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import io.restassured.http.Header; -import io.vertx.core.Context; -import io.vertx.core.Future; -import io.vertx.core.Vertx; -import io.vertx.junit5.VertxTestContext; - -class TagServiceTest { - private static final String ORDER_ID = "1ab7ef6a-d1d4-4a4f-90a2-882aed18af14"; - private static final String ORDER_PATH = BASE_MOCK_DATA_PATH + "compositeOrders/" + ORDER_ID + ".json"; - public static final String TENANT_ID = "ordertest"; - public static final Header X_OKAPI_TENANT = new Header(OKAPI_HEADER_TENANT, TENANT_ID); +import java.util.ArrayList; +import java.util.Collections; +@ExtendWith(VertxExtension.class) +public class TagServiceTest { @InjectMocks TagService tagService; - @Mock private RestClient restClient; - + @Mock private RequestContext requestContextMock; @BeforeEach - public void initMocks(){ - Context ctxMock = Vertx.vertx().getOrCreateContext(); - Map okapiHeadersMock = new HashMap<>(); - okapiHeadersMock.put(OKAPI_URL, "http://localhost:" + mockPort); - okapiHeadersMock.put(X_OKAPI_TOKEN.getName(), X_OKAPI_TOKEN.getValue()); - okapiHeadersMock.put(X_OKAPI_TENANT.getName(), X_OKAPI_TENANT.getValue()); - okapiHeadersMock.put(X_OKAPI_USER_ID.getName(), X_OKAPI_USER_ID.getValue()); - requestContextMock = new RequestContext(ctxMock, okapiHeadersMock); + public void initMocks() { MockitoAnnotations.openMocks(this); } @@ -67,15 +46,15 @@ void createTagsIfMissing(VertxTestContext vertxTestContext) { .withTags(new ArrayList<>()) .withTotalRecords(0); - doReturn(succeededFuture(emptyTagCollection)).when(restClient).get(anyString(), any(), any()); - doReturn(succeededFuture(postTagResponse)).when(restClient).post(anyString(), any(), any(), any()); + doReturn(succeededFuture(emptyTagCollection)).when(restClient).get(any(RequestEntry.class), eq(TagCollection.class), any(RequestContext.class)); + doReturn(succeededFuture(postTagResponse)).when(restClient).post(any(RequestEntry.class), any(), eq(Tag.class), any()); Future future = tagService.createTagsIfMissing(Collections.singleton(sampleTag), requestContextMock); + vertxTestContext.assertComplete(future) .onComplete(result -> { Assertions.assertTrue(result.succeeded()); vertxTestContext.completeNow(); }); } - } diff --git a/src/test/java/org/folio/service/finance/FiscalYearServiceTest.java b/src/test/java/org/folio/service/finance/FiscalYearServiceTest.java index 8c36477a3..f18b0ec21 100644 --- a/src/test/java/org/folio/service/finance/FiscalYearServiceTest.java +++ b/src/test/java/org/folio/service/finance/FiscalYearServiceTest.java @@ -1,75 +1,78 @@ package org.folio.service.finance; -import static org.folio.TestConfig.mockPort; +import static io.vertx.core.Future.failedFuture; +import static io.vertx.core.Future.succeededFuture; import static org.folio.TestConstants.ID_DOES_NOT_EXIST; -import static org.folio.TestConstants.X_OKAPI_TOKEN; -import static org.folio.TestConstants.X_OKAPI_USER_ID; -import static org.folio.rest.RestConstants.OKAPI_URL; -import static org.folio.rest.RestVerticle.OKAPI_HEADER_TENANT; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; -import java.util.HashMap; -import java.util.Map; import java.util.UUID; -import java.util.concurrent.CompletionException; +import io.vertx.junit5.VertxTestContext; +import org.folio.rest.core.models.RequestEntry; +import org.folio.rest.core.RestClient; +import io.vertx.junit5.VertxExtension; import org.folio.rest.acq.model.finance.FiscalYear; + import org.folio.rest.core.exceptions.HttpException; import org.folio.rest.core.models.RequestContext; -import org.folio.rest.tools.client.interfaces.HttpClientInterface; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.junit.jupiter.api.extension.ExtendWith; -import io.restassured.http.Header; -import io.vertx.core.Context; import io.vertx.core.Future; -import io.vertx.core.Vertx; +import org.mockito.Spy; +@ExtendWith(VertxExtension.class) public class FiscalYearServiceTest { - public static final String TENANT_ID = "ordertest"; - public static final Header X_OKAPI_TENANT = new Header(OKAPI_HEADER_TENANT, TENANT_ID); - + @InjectMocks private FiscalYearService fiscalYearService; - - private Context ctxMock; - private Map okapiHeadersMock; - private HttpClientInterface httpClient; + @Spy + private RestClient restClientMock; + @Mock private RequestContext requestContextMock; @BeforeEach public void initMocks() { - ctxMock = Vertx.vertx().getOrCreateContext(); - okapiHeadersMock = new HashMap<>(); - okapiHeadersMock.put(OKAPI_URL, "http://localhost:" + mockPort); - okapiHeadersMock.put(X_OKAPI_TOKEN.getName(), X_OKAPI_TOKEN.getValue()); - okapiHeadersMock.put(X_OKAPI_TENANT.getName(), X_OKAPI_TENANT.getValue()); - okapiHeadersMock.put(X_OKAPI_USER_ID.getName(), X_OKAPI_USER_ID.getValue()); - String okapiURL = okapiHeadersMock.getOrDefault(OKAPI_URL, ""); - requestContextMock = new RequestContext(ctxMock, okapiHeadersMock); MockitoAnnotations.openMocks(this); } @Test void testShouldReturnCurrentFiscalYearForLedger() { + String ledgerId = UUID.randomUUID().toString(); + + doReturn(succeededFuture(new FiscalYear())) + .when(restClientMock) + .get(any(RequestEntry.class), eq(FiscalYear.class), any(RequestContext.class)); + + FiscalYear fy = fiscalYearService.getCurrentFiscalYear(ledgerId, requestContextMock).result(); - String ledgerId = UUID.randomUUID() - .toString(); - FiscalYear fy = fiscalYearService.getCurrentFiscalYear(ledgerId, requestContextMock) - .result(); assertNotNull(fy); } @Test - void testShouldThrowHttpException() { + void testShouldThrowHttpException(VertxTestContext vertxTestContext) { + doReturn(failedFuture(new HttpException(404, "Fiscal year not found"))) + .when(restClientMock) + .get(any(RequestEntry.class), eq(FiscalYear.class), any(RequestContext.class)); Future result = fiscalYearService.getCurrentFiscalYear(ID_DOES_NOT_EXIST, requestContextMock); - CompletionException expectedException = assertThrows(CompletionException.class, result::result); - HttpException httpException = (HttpException) expectedException.getCause(); - assertEquals(404, httpException.getCode()); + vertxTestContext.assertFailure(result) + .onComplete(expectedException -> { + HttpException httpException = (HttpException) expectedException.cause(); + assertEquals(404, httpException.getCode()); + assertEquals(result.cause().getMessage(), httpException.getMessage()); + verify(restClientMock).get(any(RequestEntry.class), eq(FiscalYear.class), any(RequestContext.class)); + vertxTestContext.completeNow(); + }); } } diff --git a/src/test/java/org/folio/service/finance/transaction/ClosedToOpenEncumbranceStrategyTest.java b/src/test/java/org/folio/service/finance/transaction/ClosedToOpenEncumbranceStrategyTest.java index 2053d0191..6c22a1e9d 100644 --- a/src/test/java/org/folio/service/finance/transaction/ClosedToOpenEncumbranceStrategyTest.java +++ b/src/test/java/org/folio/service/finance/transaction/ClosedToOpenEncumbranceStrategyTest.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.UUID; +import io.vertx.junit5.VertxExtension; import org.folio.models.EncumbranceRelationsHolder; import org.folio.rest.core.models.RequestContext; import org.folio.rest.jaxrs.model.CompositePoLine; @@ -31,6 +32,7 @@ import org.folio.service.finance.budget.BudgetRestrictionService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -39,6 +41,7 @@ import io.vertx.core.json.JsonObject; import io.vertx.junit5.VertxTestContext; +@ExtendWith(VertxExtension.class) public class ClosedToOpenEncumbranceStrategyTest { @InjectMocks private ClosedToOpenEncumbranceStrategy closedToOpenEncumbranceStrategy; @@ -53,7 +56,6 @@ public class ClosedToOpenEncumbranceStrategyTest { @Mock private RequestContext requestContext; - @BeforeEach public void initMocks() { MockitoAnnotations.openMocks(this); diff --git a/src/test/java/org/folio/service/finance/transaction/TransactionSummariesServiceTest.java b/src/test/java/org/folio/service/finance/transaction/TransactionSummariesServiceTest.java index 6ac199b5a..49f7d0aa3 100644 --- a/src/test/java/org/folio/service/finance/transaction/TransactionSummariesServiceTest.java +++ b/src/test/java/org/folio/service/finance/transaction/TransactionSummariesServiceTest.java @@ -8,28 +8,32 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.util.UUID; +import io.vertx.core.Future; +import io.vertx.core.json.JsonObject; +import io.vertx.junit5.VertxExtension; import org.folio.rest.acq.model.finance.OrderTransactionSummary; import org.folio.rest.acq.model.finance.Transaction; import org.folio.rest.core.RestClient; import org.folio.rest.core.models.RequestContext; +import org.folio.rest.core.models.RequestEntry; import org.folio.rest.jaxrs.model.CompositePoLine; import org.folio.rest.jaxrs.model.CompositePurchaseOrder; import org.folio.rest.jaxrs.model.FundDistribution; import org.folio.service.finance.transaction.summary.OrderTransactionSummariesService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import io.vertx.core.json.JsonObject; - +@ExtendWith(VertxExtension.class) public class TransactionSummariesServiceTest { @InjectMocks @@ -58,7 +62,6 @@ void testShouldNotUpdateTransactionsSummariesWhenNoEncumbrances() { @Test void testShouldTransactionsCreatedForEncumbrances() { // Given - CompositePurchaseOrder order = getMockAsJson(ORDER_PATH).mapTo(CompositePurchaseOrder.class); CompositePoLine line = order.getCompositePoLines() .get(0); @@ -69,7 +72,6 @@ void testShouldTransactionsCreatedForEncumbrances() { .get(0) .getFundDistribution() .get(0); - // When orderTransactionSummariesService.updateTransactionSummary(order.getId(), 1, requestContext); // Then @@ -78,16 +80,19 @@ void testShouldTransactionsCreatedForEncumbrances() { @Test void testShouldCreateTransactionSummaryInStorageTransactions() { - // given - + // Given String uuid = UUID.randomUUID().toString(); - var response = new JsonObject("{\"id\": \"" + uuid + "\"}"); - doReturn(succeededFuture(response)).when(restClient).post(anyString(), any(), any(), any()); + JsonObject response = new JsonObject().put("id", uuid); + when(restClient.post(any(RequestEntry.class), any(), any(), any(RequestContext.class))) + .thenReturn(succeededFuture(response)); + OrderTransactionSummary expectedSummary = new OrderTransactionSummary().withId(uuid).withNumTransactions(2); // When - OrderTransactionSummary summary = orderTransactionSummariesService.createTransactionSummary(new OrderTransactionSummary().withId(uuid).withNumTransactions(2), requestContext) - .result(); + Future result = orderTransactionSummariesService.createTransactionSummary(expectedSummary, requestContext); // Then - assertEquals(uuid, summary.getId()); - verify(restClient).post(anyString(), any(), any(), any()); + verify(restClient).post(any(RequestEntry.class), any(), any(), any(RequestContext.class)); + JsonObject res = JsonObject.mapFrom(result.result()); + String resultId = res.getString("id"); + assertEquals(uuid, resultId); } } +