Skip to content

Commit

Permalink
[MODFQMMGR-333] Expose endpoint to fetch entity types with missing pe…
Browse files Browse the repository at this point in the history
…rmissions
  • Loading branch information
ncovercash committed Jun 17, 2024
1 parent 5d837f3 commit f922b7c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Optional<EntityType> getEntityTypeDefinition(UUID entityTypeId) {
});
}

public List<RawEntityTypeSummary> getEntityTypeSummary(Set<UUID> entityTypeIds) {
public List<RawEntityTypeSummary> getEntityTypeSummaries(Set<UUID> entityTypeIds) {
log.info("Fetching entityTypeSummary for ids: {}", entityTypeIds);
Field<String> definitionField = field(DEFINITION_FIELD_NAME, String.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public ResponseEntity<EntityType> getEntityType(UUID entityTypeId) {
}

@Override
public ResponseEntity<List<EntityTypeSummary>> getEntityTypeSummary(List<UUID> entityTypeIds) {
public ResponseEntity<List<EntityTypeSummary>> getEntityTypeSummary(List<UUID> entityTypeIds, Boolean includeInaccessible) {
Set<UUID> idsSet = entityTypeIds == null ? Set.of() : Set.copyOf(entityTypeIds);
// Permissions are handled in the service layer
return ResponseEntity.ok(entityTypeService.getEntityTypeSummary(idsSet));
return ResponseEntity.ok(entityTypeService.getEntityTypeSummary(idsSet, includeInaccessible));
}

@EntityTypePermissionsRequired
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/org/folio/fqm/service/EntityTypeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,31 @@ 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<EntityTypeSummary> getEntityTypeSummary(Set<UUID> entityTypeIds) {
public List<EntityTypeSummary> getEntityTypeSummary(Set<UUID> entityTypeIds, Boolean includeInaccessible) {
Set<String> userPermissions = permissionsService.getUserPermissions();
return entityTypeRepository
.getEntityTypeSummary(entityTypeIds)
.getEntityTypeSummaries(entityTypeIds)
.stream()
.filter(entityTypeSummary -> userPermissions.containsAll(entityTypeSummary.requiredPermissions()))
.map(rawEntityTypeSummary ->
new EntityTypeSummary()
.id(rawEntityTypeSummary.id())
.label(localizationService.getEntityTypeLabel(rawEntityTypeSummary.name()))
.filter(entityTypeSummary ->
Boolean.TRUE.equals(includeInaccessible) || userPermissions.containsAll(entityTypeSummary.requiredPermissions())
)
.map(rawEntityTypeSummary -> {
EntityTypeSummary result = new EntityTypeSummary()
.id(rawEntityTypeSummary.id())
.label(localizationService.getEntityTypeLabel(rawEntityTypeSummary.name()));

if (Boolean.TRUE.equals(includeInaccessible)) {
return result.missingPermissions(
rawEntityTypeSummary
.requiredPermissions()
.stream()
.filter(permission -> !userPermissions.contains(permission))
.toList()
);
}

return result;
})
.sorted(Comparator.comparing(EntityTypeSummary::getLabel, String.CASE_INSENSITIVE_ORDER))
.toList();
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/swagger.api/mod-fqm-manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ paths:
description: Get names for a list of entity type ids.
parameters:
- $ref: '#/components/parameters/entity-type-ids'
- $ref: '#/components/parameters/include-inaccessible'
responses:
'200':
description: 'Entity type summaries'
Expand Down Expand Up @@ -79,6 +80,13 @@ components:
items:
type: string
format: UUID
include-inaccessible:
name: includeInaccessible
in: query
required: false
description: Include inaccessible entity types in the result
schema:
type: boolean
schemas:
errorResponse:
$ref: schemas/errors.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
"label" : {
"description": "Entity type label",
"type": "string"
},
"missingPermissions": {
"description": "List of missing permissions",
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.folio.fqm.controller;


import org.folio.fqm.exception.EntityTypeNotFoundException;
import org.folio.fqm.exception.FieldNotFoundException;
import org.folio.fqm.resource.EntityTypeController;
Expand All @@ -15,11 +14,9 @@
import org.folio.spring.integration.XOkapiHeaders;
import org.junit.jupiter.api.Test;

import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
Expand Down Expand Up @@ -82,7 +79,7 @@ void shouldGetEntityTypeSummaryForValidIds() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/entity-types")
.header(XOkapiHeaders.TENANT, "tenant_01")
.queryParam("ids", id1.toString(), id2.toString());
when(entityTypeService.getEntityTypeSummary(ids)).thenReturn(expectedSummary);
when(entityTypeService.getEntityTypeSummary(ids, null)).thenReturn(expectedSummary);
mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(jsonPath("$.[0].id", is(expectedSummary.get(0).getId().toString())))
Expand All @@ -100,7 +97,7 @@ void shouldReturnEmptyListWhenEntityTypeSummaryNotFound() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/entity-types")
.header(XOkapiHeaders.TENANT, "tenant_01")
.queryParam("ids", id1.toString(), id2.toString());
when(entityTypeService.getEntityTypeSummary(ids)).thenReturn(expectedSummary);
when(entityTypeService.getEntityTypeSummary(ids, false)).thenReturn(expectedSummary);
mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(jsonPath("$", is(expectedSummary)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void shouldFetchAllPublicEntityTypes() {
new RawEntityTypeSummary(ENTITY_TYPE_02_ID, ENTITY_TYPE_02_LABEL, List.of())
);

List<RawEntityTypeSummary> actualSummary = repo.getEntityTypeSummary(Set.of());
List<RawEntityTypeSummary> actualSummary = repo.getEntityTypeSummaries(Set.of());
assertEquals(expectedSummary, actualSummary, "Expected Summary should equal Actual Summary");
}

Expand All @@ -59,7 +59,7 @@ void shouldFetchEntityTypesOfGivenIds() {
List<RawEntityTypeSummary> expectedSummary = List.of(
new RawEntityTypeSummary(ENTITY_TYPE_01_ID, ENTITY_TYPE_01_LABEL, List.of()));

List<RawEntityTypeSummary> actualSummary = repo.getEntityTypeSummary(ids);
List<RawEntityTypeSummary> actualSummary = repo.getEntityTypeSummaries(ids);
assertEquals(expectedSummary, actualSummary, "Expected Summary should equal Actual Summary");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ void shouldGetEntityTypeSummaryForValidIds() {
new EntityTypeSummary().id(id1).label("label_01"),
new EntityTypeSummary().id(id2).label("label_02"));

when(repo.getEntityTypeSummary(ids)).thenReturn(List.of(
when(repo.getEntityTypeSummaries(ids)).thenReturn(List.of(
new RawEntityTypeSummary(id1, "translation_label_01", List.of()),
new RawEntityTypeSummary(id2, "translation_label_02", List.of())));
when(localizationService.getEntityTypeLabel("translation_label_01")).thenReturn("label_01");
when(localizationService.getEntityTypeLabel("translation_label_02")).thenReturn("label_02");

List<EntityTypeSummary> actualSummary = entityTypeService.getEntityTypeSummary(ids);
List<EntityTypeSummary> actualSummary = entityTypeService.getEntityTypeSummary(ids, false);

assertEquals(expectedSummary, actualSummary, "Expected Summary should equal Actual Summary");

verify(repo, times(1)).getEntityTypeSummary(ids);
verify(repo, times(1)).getEntityTypeSummaries(ids);

verify(localizationService, times(1)).getEntityTypeLabel("translation_label_01");
verify(localizationService, times(1)).getEntityTypeLabel("translation_label_02");
Expand Down

0 comments on commit f922b7c

Please sign in to comment.