Skip to content

Commit

Permalink
Fixed acqUnitsRestriction and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
azizbekxm committed Nov 20, 2024
1 parent 8c92034 commit 727d9bf
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FinanceDataService(RestClient restClient, AcqUnitsService acqUnitsService
}

public Future<FyFinanceDataCollection> getFinanceDataWithAcqUnitsRestriction(String query, int offset, int limit,
RequestContext requestContext) {
RequestContext requestContext) {
return acqUnitsService.buildAcqUnitsCqlClauseForFinanceData(requestContext)
.map(clause -> StringUtils.isEmpty(query) ? clause : combineCqlExpressions("and", clause, query))
.compose(effectiveQuery -> getFinanceData(effectiveQuery, offset, limit, requestContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public final class AcqUnitConstants {
public static final String FD_FUND_ACQUISITIONS_UNIT_IDS = "fundAcqUnitIds"; // for finance data view table
public static final String FD_BUDGET_ACQUISITIONS_UNIT_IDS = "budgetAcqUnitIds"; // for finance data view table
public static final String NO_ACQ_UNIT_ASSIGNED_CQL = "cql.allRecords=1 not " + ACQUISITIONS_UNIT_IDS + " <> []";
public static final String FD_NO_ACQ_UNIT_ASSIGNED_CQL = "cql.allRecords=1 not " +
FD_BUDGET_ACQUISITIONS_UNIT_IDS + " <> []" + FD_FUND_ACQUISITIONS_UNIT_IDS + " <> []";
public static final String FD_NO_ACQ_UNIT_ASSIGNED_CQL = "cql.allRecords=1 not (" +
FD_BUDGET_ACQUISITIONS_UNIT_IDS + " <> [] and " + FD_FUND_ACQUISITIONS_UNIT_IDS + " <> [])";
public static final String IS_DELETED_PROP = "isDeleted";
public static final String ALL_UNITS_CQL = IS_DELETED_PROP + "=*";
public static final String ACTIVE_UNITS_CQL = IS_DELETED_PROP + "==false";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Future<String> buildAcqUnitsCqlClauseForFinanceData(RequestContext reques
return getAcqUnitIdsForSearch(requestContext)
.map(ids -> {
if (ids.isEmpty()) {
return NO_ACQ_UNIT_ASSIGNED_CQL;
return FD_NO_ACQ_UNIT_ASSIGNED_CQL;
}
return String.format("(%s and %s) or (%s)",
convertIdsToCqlQuery(ids, FD_FUND_ACQUISITIONS_UNIT_IDS, false),
Expand Down
41 changes: 28 additions & 13 deletions src/test/java/org/folio/rest/impl/FinanceDataApiTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package org.folio.rest.impl;

import static io.vertx.core.Future.failedFuture;
import static io.vertx.core.Future.succeededFuture;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static javax.ws.rs.core.Response.Status.OK;
import static org.folio.rest.util.ErrorCodes.GENERIC_ERROR_CODE;
import static org.folio.rest.util.RestTestUtils.verifyGet;
import static org.folio.rest.util.RestTestUtils.verifyGetWithParam;
import static org.folio.rest.util.TestConfig.autowireDependencies;
import static org.folio.rest.util.TestConfig.clearVertxContext;
import static org.folio.rest.util.TestConfig.initSpringContext;
import static org.folio.rest.util.TestConfig.isVerticleNotDeployed;
import static org.folio.services.protection.AcqUnitConstants.NO_ACQ_UNIT_ASSIGNED_CQL;
import static org.folio.services.protection.AcqUnitConstants.FD_NO_ACQ_UNIT_ASSIGNED_CQL;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

Expand All @@ -32,7 +39,6 @@
import org.folio.rest.jaxrs.model.Errors;
import org.folio.rest.jaxrs.model.FyFinanceData;
import org.folio.rest.jaxrs.model.FyFinanceDataCollection;
import org.folio.rest.util.RestTestUtils;
import org.folio.services.financedata.FinanceDataService;
import org.folio.services.protection.AcqUnitsService;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -51,7 +57,7 @@ public class FinanceDataApiTest {
public AcqUnitsService acqUnitsService;

private static boolean runningOnOwn;
private static final String FINANCE_DATA_ENDPOINT = "/finance-storage/finance-data";
private static final String FINANCE_DATA_ENDPOINT = "/finance/finance-data";

@BeforeAll
static void init() throws InterruptedException, ExecutionException, TimeoutException {
Expand Down Expand Up @@ -82,24 +88,34 @@ void resetMocks() {
}

@Test
void testGetFinanceFinanceDataSuccess() {
void positive_testGetFinanceFinanceDataSuccess() {
var fiscalYearId = "123e4567-e89b-12d3-a456-426614174004";
var financeDataCollection = new FyFinanceDataCollection()
.withFyFinanceData(List.of(new FyFinanceData().withFiscalYearId(fiscalYearId)))
.withTotalRecords(1);
String query = "fiscalYearId==" + fiscalYearId;
int limit = 5;
int offset = 1;

when(financeDataService.getFinanceDataWithAcqUnitsRestriction(any(), anyInt(), anyInt(), any(RequestContext.class)))
Map<String, Object> params = new HashMap<>();
params.put("query", query);
params.put("limit", limit);
params.put("offset", offset);

when(acqUnitsService.buildAcqUnitsCqlClauseForFinanceData(any())).thenReturn(succeededFuture(FD_NO_ACQ_UNIT_ASSIGNED_CQL));
when(financeDataService.getFinanceDataWithAcqUnitsRestriction(anyString(), anyInt(), anyInt(), any()))
.thenReturn(succeededFuture(financeDataCollection));
when(acqUnitsService.buildAcqUnitsCqlClause(any())).thenReturn(succeededFuture(NO_ACQ_UNIT_ASSIGNED_CQL));
var actualFinanceDataCollection = RestTestUtils.verifyGet(FINANCE_DATA_ENDPOINT, APPLICATION_JSON, OK.getStatusCode())

var response = verifyGetWithParam(FINANCE_DATA_ENDPOINT, APPLICATION_JSON, OK.getStatusCode(), params)
.as(FyFinanceDataCollection.class);

assertThat(fiscalYearId, equalTo(actualFinanceDataCollection.getFyFinanceData().get(0).getFiscalYearId()));
assertEquals(financeDataCollection, response);
verify(financeDataService).getFinanceDataWithAcqUnitsRestriction(eq(query), eq(offset), eq(limit), any(RequestContext.class));
}

@Test
void testGetFinanceFinanceDataFailure() {
Future<FyFinanceDataCollection> financeDataFuture = Future.failedFuture(new HttpException(500, INTERNAL_SERVER_ERROR.getReasonPhrase()));
void negative_testGetFinanceFinanceDataFailure() {
Future<FyFinanceDataCollection> financeDataFuture = failedFuture(new HttpException(500, INTERNAL_SERVER_ERROR.getReasonPhrase()));

when(financeDataService.getFinanceDataWithAcqUnitsRestriction(any(), anyInt(), anyInt(), any(RequestContext.class)))
.thenReturn(financeDataFuture);
Expand All @@ -112,8 +128,7 @@ void testGetFinanceFinanceDataFailure() {

static class ContextConfiguration {

@Bean
public FinanceDataService financeDataService() {
@Bean public FinanceDataService financeDataService() {
return mock(FinanceDataService.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static io.vertx.core.Future.succeededFuture;
import static org.folio.rest.util.TestUtils.assertQueryContains;
import static org.folio.services.protection.AcqUnitConstants.FD_NO_ACQ_UNIT_ASSIGNED_CQL;
import static org.folio.services.protection.AcqUnitConstants.NO_ACQ_UNIT_ASSIGNED_CQL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -63,7 +65,7 @@ void closeMocks() throws Exception {
}

@Test
void shouldCallGetForRestClientWhenCalledGetFinanceDataWithAcqUnitsRestriction(VertxTestContext vertxTestContext) {
void positive_shouldGetFinanceDataWithAcqUnitsRestriction(VertxTestContext vertxTestContext) {
String query = "fiscalYearId==db9c1ad6-026e-4b1a-9a99-032f41e7099b";
String acqUnitIdsQuery = "fundAcqUnitIds=(1ee4b4e5-d621-4e43-8a76-0d904b0f491b) and budgetAcqUnitIds=(1ee4b4e5-d621-4e43-8a76-0d904b0f491b) or (" + NO_ACQ_UNIT_ASSIGNED_CQL + ")";
String expectedQuery = "(" + acqUnitIdsQuery + ") and (" + query + ")";
Expand All @@ -83,4 +85,25 @@ void shouldCallGetForRestClientWhenCalledGetFinanceDataWithAcqUnitsRestriction(V
});
}

@Test
void negative_shouldReturnEmptyCollectionWhenFinanceDataNotFound(VertxTestContext vertxTestContext) {
String query = "fiscalYearId==non-existent-id";
String expectedQuery = "(" + FD_NO_ACQ_UNIT_ASSIGNED_CQL + ") and (" + query + ")";
int offset = 0;
int limit = 10;
FyFinanceDataCollection emptyCollection = new FyFinanceDataCollection().withTotalRecords(0);

when(acqUnitsService.buildAcqUnitsCqlClauseForFinanceData(any())).thenReturn(succeededFuture(FD_NO_ACQ_UNIT_ASSIGNED_CQL));
when(restClient.get(anyString(), eq(FyFinanceDataCollection.class), any())).thenReturn(succeededFuture(emptyCollection));

var future = financeDataService.getFinanceDataWithAcqUnitsRestriction(query, offset, limit, requestContextMock);
vertxTestContext.assertComplete(future)
.onComplete(result -> {
assertTrue(result.succeeded());
assertEquals(0, (int) result.result().getTotalRecords());
verify(restClient).get(assertQueryContains(expectedQuery), eq(FyFinanceDataCollection.class), eq(requestContextMock));
vertxTestContext.completeNow();
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.folio.rest.util.TestConstants.X_OKAPI_TENANT;
import static org.folio.rest.util.TestConstants.X_OKAPI_TOKEN;
import static org.folio.rest.util.TestConstants.X_OKAPI_USER_ID;
import static org.folio.services.protection.AcqUnitConstants.FD_NO_ACQ_UNIT_ASSIGNED_CQL;
import static org.folio.services.protection.AcqUnitConstants.NO_ACQ_UNIT_ASSIGNED_CQL;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -216,7 +217,7 @@ void testShouldBuildCqlClauseForFinanceDataWhenIdsIsNotEmpty(VertxTestContext ve
assertTrue(result.succeeded());

var actClause = result.result();
assertThat(actClause, equalTo("fundAcqUnitIds=(" + unitId + ") and budgetAcqUnitIds=(" + unitId + ") or (" + NO_ACQ_UNIT_ASSIGNED_CQL + ")"));
assertThat(actClause, equalTo("(fundAcqUnitIds=(" + unitId + ") and budgetAcqUnitIds=(" + unitId + ")) or (" + FD_NO_ACQ_UNIT_ASSIGNED_CQL + ")"));
verify(restClient).get(anyString(), eq(AcquisitionsUnitCollection.class), eq(requestContext));
verify(acqUnitMembershipsService).getAcquisitionsUnitsMemberships("userId==" + X_OKAPI_USER_ID.getValue(), 0, Integer.MAX_VALUE, requestContext);

Expand Down
24 changes: 0 additions & 24 deletions src/test/resources/mockdata/financedata/fy-finance-data.json

This file was deleted.

0 comments on commit 727d9bf

Please sign in to comment.