Skip to content

Commit

Permalink
[MODORDERS-1223] Avoid queries to retrieve user tenants if central or…
Browse files Browse the repository at this point in the history
…dering is disabled (#1066)

* MODORDERS-1183. Introduce new error code for missed affiliations

* Optimize performance to not make an additional queries

* Fix unit tests
  • Loading branch information
SerhiiNosko authored Dec 12, 2024
1 parent 86f4c46 commit 647fcff
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
"invoice.invoice-lines.item.put",
"invoices.acquisition-units.bypass.execute",
"orders-storage.order-invoice-relationships.collection.get",
"orders-storage.settings.collection.get",
"consortia.user-tenants.collection.get",
"user-tenants.collection.get",
"consortia.sharing-instances.item.post"
Expand Down Expand Up @@ -2151,6 +2152,7 @@
"organizations-storage.organizations.collection.get",
"organizations-storage.organizations.item.get",
"isbn-utils.convert-to-13.get",
"orders-storage.settings.collection.get",
"consortia.user-tenants.collection.get",
"user-tenants.collection.get",
"consortia.sharing-instances.collection.get",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,16 @@ public Future<RequestContext> overrideContextToCentralTenantIfNeeded(RequestCont
return Future.succeededFuture(requestContext);
}
RequestContext centralContext = createContextWithNewTenantId(requestContext, configuration.centralTenantId());
return settingsRetriever.getSettingByKey(SettingKey.CENTRAL_ORDERING_ENABLED, centralContext)
.map(centralOrdering -> {
logger.info("overrideContextToCentralTenantIdNeeded:: central ordering enabled: {}", centralOrdering);
return centralOrdering.map(Setting::getValue).orElse(null);
})
.compose(orderingEnabled -> Future.succeededFuture(Boolean.parseBoolean(orderingEnabled) ? centralContext : requestContext));
return isCentralOrderingEnabled(requestContext)
.compose(isCentralOrderingEnabled -> Future.succeededFuture(isCentralOrderingEnabled ? centralContext : requestContext));
});
}

public Future<Boolean> isCentralOrderingEnabled(RequestContext requestContext) {
return settingsRetriever.getSettingByKey(SettingKey.CENTRAL_ORDERING_ENABLED, requestContext)
.map(centralOrdering -> {
logger.info("isCentralOrderingEnabled:: central ordering enabled: {}", centralOrdering);
return centralOrdering.map(setting -> Boolean.parseBoolean(setting.getValue())).orElse(false);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,10 @@ private Future<List<String>> getUserTenantsIfNeeded(RequestContext requestContex
return consortiumConfigurationService.getConsortiumConfiguration(requestContext)
.compose(consortiumConfiguration ->
consortiumConfiguration
.map(configuration -> consortiumUserTenantsRetriever.getUserTenants(configuration.consortiumId(), configuration.centralTenantId(), requestContext))
.map(configuration -> consortiumConfigurationService.isCentralOrderingEnabled(requestContext)
.compose(isCentralOrderingEnabled -> isCentralOrderingEnabled
? consortiumUserTenantsRetriever.getUserTenants(configuration.consortiumId(), configuration.centralTenantId(), requestContext)
: Future.succeededFuture()))
.orElse(Future.succeededFuture())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.util.List;
Expand Down Expand Up @@ -411,6 +412,8 @@ void testValidateUserUnaffiliatedLocationsAllValidLocations(VertxTestContext tes

when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext)))
.thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId"))));
when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext)))
.thenReturn(Future.succeededFuture(true));
when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext)))
.thenReturn(Future.succeededFuture(List.of("tenant1", "tenant2")));

Expand All @@ -428,13 +431,36 @@ void testValidateUserUnaffiliatedLocationsAllValidDuplicateTenantLocations(Vertx

when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext)))
.thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId"))));
when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext)))
.thenReturn(Future.succeededFuture(true));
when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext)))
.thenReturn(Future.succeededFuture(List.of("tenant1", "tenant2")));

compositePoLineValidationService.validateUserUnaffiliatedLocations(updatedPoLineId, locationsUpdated, requestContext)
.onComplete(testContext.succeeding(result -> testContext.verify(testContext::completeNow)));
}

@Test
void testValidateUserUnaffiliatedLocationsWhenCentralOrderingIsDisabled(VertxTestContext testContext) {
var locationsUpdated = List.of(
new Location().withLocationId(UUID.randomUUID().toString()).withTenantId("tenant1"),
new Location().withLocationId(UUID.randomUUID().toString()).withTenantId("tenant2"));
var updatedPoLineId = UUID.randomUUID().toString();

when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext)))
.thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId"))));
when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext)))
.thenReturn(Future.succeededFuture(false));
when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext)))
.thenReturn(Future.succeededFuture(List.of("tenant1")));

compositePoLineValidationService.validateUserUnaffiliatedLocations(updatedPoLineId, locationsUpdated, requestContext)
.onComplete(testContext.succeeding(result -> {
testContext.verify(testContext::completeNow);
verifyNoInteractions(consortiumUserTenantsRetriever);
}));
}

@Test
void testValidateUserUnaffiliatedLocationsOneValidAndOneInvalidLocations(VertxTestContext testContext) {
var locationsUpdated = List.of(
Expand All @@ -444,6 +470,8 @@ void testValidateUserUnaffiliatedLocationsOneValidAndOneInvalidLocations(VertxTe

when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext)))
.thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId"))));
when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext)))
.thenReturn(Future.succeededFuture(true));
when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext)))
.thenReturn(Future.succeededFuture(List.of("tenant1")));

Expand All @@ -464,6 +492,8 @@ void testValidateUserUnaffiliatedLocationsTwoInvalidLocations(VertxTestContext t

when(consortiumConfigurationService.getConsortiumConfiguration(eq(requestContext)))
.thenReturn(Future.succeededFuture(Optional.of(new ConsortiumConfiguration("tenant1", "consortiumId"))));
when(consortiumConfigurationService.isCentralOrderingEnabled(eq(requestContext)))
.thenReturn(Future.succeededFuture(true));
when(consortiumUserTenantsRetriever.getUserTenants(eq("consortiumId"), anyString(), eq(requestContext)))
.thenReturn(Future.succeededFuture(List.of("tenant3")));

Expand Down

0 comments on commit 647fcff

Please sign in to comment.