Skip to content

Commit

Permalink
MODFQMMGR-456: Check whether entity type is cross-tenant when retriev…
Browse files Browse the repository at this point in the history
…ing definition
  • Loading branch information
bvsharp committed Sep 24, 2024
1 parent ada3d35 commit f1558af
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/main/java/org/folio/fqm/service/CrossTenantQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class CrossTenantQueryService {
private static final String SIMPLE_INSTANCES_ID = "8fc4a9d2-7ccf-4233-afb8-796911839862";

public List<String> getTenantsToQuery(EntityType entityType, boolean forceCrossTenantQuery) {
// Sitation 1: any non-instance non-cross-tenant entity type
// Situation 2: non-cross-tenant instance entity type
// Situation 2: any entity type in a non-ECS environment
// Situation 3: any non-instance entity type in a member tenant
// Situation 4: instance entity type in a member tenant
// Situation 5: cross-tenant entity type in central tenant
if (!forceCrossTenantQuery && !Boolean.TRUE.equals(entityType.getCrossTenantQueriesEnabled())) {
return List.of(executionContext.getTenantId());
}
Expand Down Expand Up @@ -100,10 +106,6 @@ private Map<String, String> getEcsTenantInfo() {
.orElse(null);
}

private String getCentralTenantId(Map<String, String> ecsTenantInfo) {
return ecsTenantInfo != null ? ecsTenantInfo.get("centralTenantId") : null;
}

public String getCentralTenantId() {
return getCentralTenantId(getEcsTenantInfo());
}
Expand All @@ -115,4 +117,12 @@ private boolean ecsEnabled(Map<String, String> ecsTenantInfo) {
public boolean ecsEnabled() {
return ecsEnabled(getEcsTenantInfo());
}

public boolean isCentralTenant() {
return executionContext.getTenantId().equals(getCentralTenantId());
}

private String getCentralTenantId(Map<String, String> ecsTenantInfo) {
return ecsTenantInfo != null ? ecsTenantInfo.get("centralTenantId") : null;
}
}
9 changes: 7 additions & 2 deletions src/main/java/org/folio/fqm/service/EntityTypeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public List<EntityTypeSummary> getEntityTypeSummary(Set<UUID> entityTypeIds, boo
.map(entityType -> {
EntityTypeSummary result = new EntityTypeSummary()
.id(UUID.fromString(entityType.getId()))
.label(localizationService.getEntityTypeLabel(entityType.getName()));
.label(localizationService.getEntityTypeLabel(entityType.getName()))
.crossTenantQueriesEnabled(entityType.getCrossTenantQueriesEnabled());
if (includeInaccessible) {
return result.missingPermissions(
permissionsService.getRequiredPermissions(entityType)
Expand All @@ -86,6 +87,8 @@ public List<EntityTypeSummary> getEntityTypeSummary(Set<UUID> entityTypeIds, boo
*/
public EntityType getEntityTypeDefinition(UUID entityTypeId, boolean includeHidden, boolean sortColumns) {
EntityType entityType = entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null);
boolean crossTenantEnabled = Boolean.TRUE.equals(entityType.getCrossTenantQueriesEnabled())
&& crossTenantQueryService.isCentralTenant();
List<EntityTypeColumn> columns = entityType
.getColumns()
.stream()
Expand All @@ -96,7 +99,9 @@ public EntityType getEntityTypeDefinition(UUID entityTypeId, boolean includeHidd
.sorted(nullsLast(comparing(Field::getLabelAlias, String.CASE_INSENSITIVE_ORDER)))
.toList();
}
return entityType.columns(columns);
return entityType
.columns(columns)
.crossTenantQueriesEnabled(crossTenantEnabled);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/swagger.api/schemas/EntityTypeSummary.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"description": "Entity type label",
"type": "string"
},
"crossTenantQueriesEnabled": {
"description": "Indicates if this entity type supports cross-tenant queries",
"type": "boolean",
"default": false
},
"missingPermissions": {
"description": "List of missing permissions",
"type": "array",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,20 @@ class CrossTenantQueryServiceTest {
{
"id": "06192681-0df7-4f33-a38f-48e017648d69",
"userId": "a5e7895f-503c-4335-8828-f507bc8d1c45",
"tenantId": "tenant_01"
"tenantId": "tenant_01",
"centralTenantId": "tenant_01"
},
{
"id": "3c1bfbe9-7d64-41fe-a358-cdaced6a631f",
"userId": "a5e7895f-503c-4335-8828-f507bc8d1c45",
"tenantId": "tenant_02"
"tenantId": "tenant_02",
"centralTenantId": "tenant_01"
},
{
"id": "b167837a-ecdd-482b-b5d3-79a391a1dbf1",
"userId": "a5e7895f-503c-4335-8828-f507bc8d1c45",
"tenantId": "tenant_03",
"centralTenantId": "tenant_01"
}
]
}
Expand Down Expand Up @@ -182,4 +185,12 @@ void shouldHandleErrorWhenGettingCentralTenantId() {
when(ecsClient.get(eq("user-tenants"), anyMap())).thenReturn(ECS_TENANT_INFO_FOR_NON_ECS_ENV);
assertNull(crossTenantQueryService.getCentralTenantId());
}

@Test
void testIsCentralTenant() {
String expectedId = "tenant_01";
when(ecsClient.get(eq("user-tenants"), anyMap())).thenReturn(USER_TENANT_JSON);
when(executionContext.getTenantId()).thenReturn(expectedId);
assertTrue(crossTenantQueryService.isCentralTenant());
}
}
40 changes: 40 additions & 0 deletions src/test/java/org/folio/fqm/service/EntityTypeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,46 @@ void shouldReturnEntityTypeDefinition() {
assertEquals(expectedEntityType, actualDefinition);
}

@Test
void shouldReturnCrossTenantDefinitionWhenEcsEnabled() {
UUID entityTypeId = UUID.randomUUID();
EntityType entityType = new EntityType()
.id(entityTypeId.toString())
.crossTenantQueriesEnabled(true);
EntityType expectedEntityType = new EntityType()
.id(entityTypeId.toString())
.crossTenantQueriesEnabled(true);

when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null))
.thenReturn(entityType);
when(crossTenantQueryService.isCentralTenant()).thenReturn(true);

EntityType actualEntityType = entityTypeService
.getEntityTypeDefinition(entityTypeId, false, false);

assertEquals(expectedEntityType, actualEntityType);
}

@Test
void shouldReturnNonCrossTenantDefinitionWhenEcsNotEnabled() {
UUID entityTypeId = UUID.randomUUID();
EntityType entityType = new EntityType()
.id(entityTypeId.toString())
.crossTenantQueriesEnabled(true);
EntityType expectedEntityType = new EntityType()
.id(entityTypeId.toString())
.crossTenantQueriesEnabled(false);

when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null))
.thenReturn(entityType);
when(crossTenantQueryService.isCentralTenant()).thenReturn(false);

EntityType actualEntityType = entityTypeService
.getEntityTypeDefinition(entityTypeId, false, false);

assertEquals(expectedEntityType, actualEntityType);
}

@Test
void shouldReturnCurrencies() {
UUID entityTypeId = UUID.randomUUID();
Expand Down

0 comments on commit f1558af

Please sign in to comment.