Skip to content

Commit

Permalink
MODORDERS-852-Some unit tests are not executed (#824)
Browse files Browse the repository at this point in the history
* TransactionSummariesServiceTest is fixed
TagServiceTest Broken Function Committed
FiscalYearServiceTest Broken Function Committed

* MODORDERS-852-Some unit tests are not executed

changes are made accordingly

* MODORDERS-852-Some unit tests are not executed

changes are made accordingly

* Update ApiTestSuite.java

* Update TagServiceTest.java

* Update TransactionSummariesServiceTest.java

* Update ApiTestSuite.java

* Update FiscalYearServiceTest.java

* MODORDERS-852-Some unit tests are not executed

* MODORDERS-852-Some unit tests are not executed

TransactionSummariesServiceTest is fixed
TagServiceTest is fixed
FiscalYearServiceTest is fixed

* Update ApiTestSuite.java

* MODORDERS-852-Some unit tests are not executed

TransactionSummariesServiceTest is fixed
TagServiceTest is fixed
FiscalYearServiceTest is fixed

* Changes after code review

* [MODORDERS-852] - Made some improvements

---------

Co-authored-by: Serhii Nosko <[email protected]>
Co-authored-by: azizbekxm <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2024
1 parent 9d8fc2f commit caa4a85
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 86 deletions.
19 changes: 19 additions & 0 deletions src/test/java/org/folio/ApiTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
}
}
51 changes: 15 additions & 36 deletions src/test/java/org/folio/service/TagServiceTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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);
}

Expand All @@ -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<Void> future = tagService.createTagsIfMissing(Collections.singleton(sampleTag), requestContextMock);

vertxTestContext.assertComplete(future)
.onComplete(result -> {
Assertions.assertTrue(result.succeeded());
vertxTestContext.completeNow();
});
}

}
75 changes: 39 additions & 36 deletions src/test/java/org/folio/service/finance/FiscalYearServiceTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<FiscalYear> 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();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -53,7 +56,6 @@ public class ClosedToOpenEncumbranceStrategyTest {
@Mock
private RequestContext requestContext;


@BeforeEach
public void initMocks() {
MockitoAnnotations.openMocks(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,7 +62,6 @@ void testShouldNotUpdateTransactionsSummariesWhenNoEncumbrances() {
@Test
void testShouldTransactionsCreatedForEncumbrances() {
// Given

CompositePurchaseOrder order = getMockAsJson(ORDER_PATH).mapTo(CompositePurchaseOrder.class);
CompositePoLine line = order.getCompositePoLines()
.get(0);
Expand All @@ -69,7 +72,6 @@ void testShouldTransactionsCreatedForEncumbrances() {
.get(0)
.getFundDistribution()
.get(0);

// When
orderTransactionSummariesService.updateTransactionSummary(order.getId(), 1, requestContext);
// Then
Expand All @@ -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<OrderTransactionSummary> 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);
}
}

0 comments on commit caa4a85

Please sign in to comment.