From a9df3698195d8771827695649aca0d64319fc1dd Mon Sep 17 00:00:00 2001 From: Kriti Jain Date: Thu, 23 Jan 2025 11:39:24 -0600 Subject: [PATCH] MODFQMMGR-635:Add Query Parameter to retrieve all the ET --- .../fqm/resource/EntityTypeController.java | 4 +- .../folio/fqm/service/EntityTypeService.java | 4 +- .../swagger.api/mod-fqm-manager.yaml | 8 ++++ .../controller/EntityTypeControllerTest.java | 26 +++++++++--- .../fqm/service/EntityTypeServiceTest.java | 40 +++++++++++++++++-- 5 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/folio/fqm/resource/EntityTypeController.java b/src/main/java/org/folio/fqm/resource/EntityTypeController.java index 8095ca67a..1dfb50bf6 100644 --- a/src/main/java/org/folio/fqm/resource/EntityTypeController.java +++ b/src/main/java/org/folio/fqm/resource/EntityTypeController.java @@ -31,12 +31,12 @@ public ResponseEntity getEntityType(UUID entityTypeId, Boolean inclu } @Override - public ResponseEntity getEntityTypeSummary(List entityTypeIds, Boolean includeInaccessible) { + public ResponseEntity getEntityTypeSummary(List entityTypeIds, Boolean includeInaccessible, Boolean includeAll) { Set idsSet = entityTypeIds == null ? Set.of() : Set.copyOf(entityTypeIds); // Permissions are handled in the service layer// Permissions are handled in the service layer return ResponseEntity.ok( new EntityTypeSummaries() - .entityTypes(entityTypeService.getEntityTypeSummary(idsSet, Boolean.TRUE.equals(includeInaccessible))) + .entityTypes(entityTypeService.getEntityTypeSummary(idsSet, Boolean.TRUE.equals(includeInaccessible), Boolean.TRUE.equals(includeAll))) .version(migrationService.getLatestVersion()) ); } diff --git a/src/main/java/org/folio/fqm/service/EntityTypeService.java b/src/main/java/org/folio/fqm/service/EntityTypeService.java index 775cd0bdf..ea62bcc26 100644 --- a/src/main/java/org/folio/fqm/service/EntityTypeService.java +++ b/src/main/java/org/folio/fqm/service/EntityTypeService.java @@ -72,11 +72,11 @@ public class EntityTypeService { * @param entityTypeIds If provided, only the entity types having the provided Ids will be included in the results */ @Transactional(readOnly = true) - public List getEntityTypeSummary(Set entityTypeIds, boolean includeInaccessible) { + public List getEntityTypeSummary(Set entityTypeIds, boolean includeInaccessible, boolean includeAll) { Set userPermissions = permissionsService.getUserPermissions(); return entityTypeRepository .getEntityTypeDefinitions(entityTypeIds, null) - .filter(entityType -> !Boolean.TRUE.equals(entityType.getPrivate())) + .filter(entityType -> includeAll || !Boolean.TRUE.equals(entityType.getPrivate())) .filter(entityType -> includeInaccessible || userPermissions.containsAll(permissionsService.getRequiredPermissions(entityType))) .map(entityType -> { EntityTypeSummary result = new EntityTypeSummary() diff --git a/src/main/resources/swagger.api/mod-fqm-manager.yaml b/src/main/resources/swagger.api/mod-fqm-manager.yaml index b6f1be112..60f9f6842 100644 --- a/src/main/resources/swagger.api/mod-fqm-manager.yaml +++ b/src/main/resources/swagger.api/mod-fqm-manager.yaml @@ -17,6 +17,7 @@ paths: parameters: - $ref: '#/components/parameters/entity-type-ids' - $ref: '#/components/parameters/include-inaccessible' + - $ref: '#/components/parameters/include-All' responses: '200': description: 'Entity type summaries' @@ -135,6 +136,13 @@ components: description: Include inaccessible entity types in the result schema: type: boolean + include-All: + name: includeAll + in: query + required: false + description: Include all the entity types regardless of being private + schema: + type: boolean schemas: errorResponse: $ref: schemas/errors.json diff --git a/src/test/java/org/folio/fqm/controller/EntityTypeControllerTest.java b/src/test/java/org/folio/fqm/controller/EntityTypeControllerTest.java index ef86fc2b1..2e903064f 100644 --- a/src/test/java/org/folio/fqm/controller/EntityTypeControllerTest.java +++ b/src/test/java/org/folio/fqm/controller/EntityTypeControllerTest.java @@ -106,7 +106,7 @@ void shouldGetEntityTypeSummaryForValidIds() throws Exception { .header(XOkapiHeaders.TENANT, "tenant_01") .queryParam("ids", id1.toString(), id2.toString()); - when(entityTypeService.getEntityTypeSummary(ids, false)).thenReturn(expectedSummary); + when(entityTypeService.getEntityTypeSummary(ids, false, false)).thenReturn(expectedSummary); when(migrationService.getLatestVersion()).thenReturn("newest coolest version"); mockMvc @@ -120,7 +120,7 @@ void shouldGetEntityTypeSummaryForValidIds() throws Exception { .andExpect(jsonPath("$.entityTypes.[1].missingPermissions").doesNotExist()) .andExpect(jsonPath("$._version", is("newest coolest version"))); - verify(entityTypeService, times(1)).getEntityTypeSummary(ids, false); + verify(entityTypeService, times(1)).getEntityTypeSummary(ids, false, false); verifyNoMoreInteractions(entityTypeService); } @@ -131,13 +131,29 @@ void testSummaryIncludesMissingPermissionsIfRequested() throws Exception { .header(XOkapiHeaders.TENANT, "tenant_01") .queryParam("includeInaccessible", "true"); - when(entityTypeService.getEntityTypeSummary(Set.of(), true)).thenReturn(List.of()); + when(entityTypeService.getEntityTypeSummary(Set.of(), true, false)).thenReturn(List.of()); // all we really want to check here is that the includeInaccessible parameter is correctly unboxed // no sense making fake data to pass through to ourself; that's redundant with shouldGetEntityTypeSummaryForValidIds mockMvc.perform(requestBuilder).andExpect(status().isOk()); - verify(entityTypeService, times(1)).getEntityTypeSummary(Set.of(), true); + verify(entityTypeService, times(1)).getEntityTypeSummary(Set.of(), true, false); + verifyNoMoreInteractions(entityTypeService); + } + + @Test + void testSummaryIncludesAllEntityTypesIfRequested() throws Exception { + RequestBuilder requestBuilder = MockMvcRequestBuilders + .get("/entity-types") + .header(XOkapiHeaders.TENANT, "tenant_01") + .queryParam("includeAll", "true"); + + when(entityTypeService.getEntityTypeSummary(Set.of(), false, true)).thenReturn(List.of()); + + mockMvc.perform(requestBuilder) + .andExpect(status().isOk()); + + verify(entityTypeService, times(1)).getEntityTypeSummary(Set.of(), false, true); verifyNoMoreInteractions(entityTypeService); } @@ -152,7 +168,7 @@ void shouldReturnEmptyListWhenEntityTypeSummaryNotFound() throws Exception { .header(XOkapiHeaders.TENANT, "tenant_01") .queryParam("ids", id1.toString(), id2.toString()); - when(entityTypeService.getEntityTypeSummary(ids, false)).thenReturn(expectedSummary); + when(entityTypeService.getEntityTypeSummary(ids, false, false)).thenReturn(expectedSummary); mockMvc.perform(requestBuilder).andExpect(status().isOk()).andExpect(jsonPath("$.entityTypes", is(expectedSummary))); } diff --git a/src/test/java/org/folio/fqm/service/EntityTypeServiceTest.java b/src/test/java/org/folio/fqm/service/EntityTypeServiceTest.java index e8326dcbf..73b4de12b 100644 --- a/src/test/java/org/folio/fqm/service/EntityTypeServiceTest.java +++ b/src/test/java/org/folio/fqm/service/EntityTypeServiceTest.java @@ -108,7 +108,7 @@ void shouldGetEntityTypeSummaryForValidIds() { when(localizationService.getEntityTypeLabel("translation_label_01")).thenReturn("label_01"); when(localizationService.getEntityTypeLabel("translation_label_02")).thenReturn("label_02"); - List actualSummary = entityTypeService.getEntityTypeSummary(ids, false); + List actualSummary = entityTypeService.getEntityTypeSummary(ids, false, false); assertEquals(expectedSummary, actualSummary, "Expected Summary should equal Actual Summary"); @@ -136,7 +136,7 @@ void shouldIncludeCrossTenantEntityTypesWhenInCentralTenant() { when(localizationService.getEntityTypeLabel("translation_label_02")).thenReturn("label_02"); when(crossTenantQueryService.isCentralTenant()).thenReturn(true); - List actualSummary = entityTypeService.getEntityTypeSummary(ids, false); + List actualSummary = entityTypeService.getEntityTypeSummary(ids, false, false); assertEquals(expectedSummary, actualSummary); } @@ -155,7 +155,7 @@ void testEntityTypeSummaryDoesNotIncludeInaccessibleWhenNotRequested() { .then(invocationOnMock -> new HashSet<>(invocationOnMock.getArgument(0).getRequiredPermissions())); when(localizationService.getEntityTypeLabel("translation_label_02")).thenReturn("label_02"); - List actualSummary = entityTypeService.getEntityTypeSummary(ids, false); + List actualSummary = entityTypeService.getEntityTypeSummary(ids, false, false); assertEquals(expectedSummary, actualSummary, "Expected Summary should equal Actual Summary"); @@ -166,6 +166,38 @@ void testEntityTypeSummaryDoesNotIncludeInaccessibleWhenNotRequested() { verifyNoMoreInteractions(repo, localizationService); } + @Test + void testEntityTypeSummaryIncludesAllWhenRequested() { + UUID id1 = UUID.randomUUID(); + UUID id2 = UUID.randomUUID(); + Set ids = Set.of(id1, id2); + + List expectedSummary = List.of( + new EntityTypeSummary().id(id1).label("label_01"), + new EntityTypeSummary().id(id2).label("label_02") + ); + + when(repo.getEntityTypeDefinitions(ids, null)).thenReturn(Stream.of( + new EntityType(id1.toString(), "translation_label_01", true, true).requiredPermissions(List.of("perm1")), // Private entity + new EntityType(id2.toString(), "translation_label_02", true, false).requiredPermissions(List.of("perm2")) // Non-private entity + )); + + when(permissionsService.getUserPermissions()).thenReturn(Set.of("perm2", "perm1")); + when(permissionsService.getRequiredPermissions(any(EntityType.class))) + .then(invocationOnMock -> new HashSet<>(invocationOnMock.getArgument(0).getRequiredPermissions())); + + when(localizationService.getEntityTypeLabel("translation_label_01")).thenReturn("label_01"); + when(localizationService.getEntityTypeLabel("translation_label_02")).thenReturn("label_02"); + + List actualSummary = entityTypeService.getEntityTypeSummary(ids, false, true); + + assertEquals(expectedSummary, actualSummary, "Expected Summary should equal Actual Summary"); + + verify(repo, times(1)).getEntityTypeDefinitions(ids, null); + verify(localizationService, times(1)).getEntityTypeLabel("translation_label_01"); + verify(localizationService, times(1)).getEntityTypeLabel("translation_label_02"); + verifyNoMoreInteractions(repo, localizationService); + } @Test void testEntityTypeSummaryIncludesInaccessible() { @@ -185,7 +217,7 @@ void testEntityTypeSummaryIncludesInaccessible() { when(localizationService.getEntityTypeLabel("translation_label_01")).thenReturn("label_01"); when(localizationService.getEntityTypeLabel("translation_label_02")).thenReturn("label_02"); - List actualSummary = entityTypeService.getEntityTypeSummary(ids, true); + List actualSummary = entityTypeService.getEntityTypeSummary(ids, true, false); assertEquals(expectedSummary, actualSummary, "Expected Summary should equal Actual Summary");