From 4b261870a7430b4317ef1fee5caf1150af44da26 Mon Sep 17 00:00:00 2001 From: Istvan Zoltan Nagy Date: Mon, 5 Feb 2024 11:23:43 +0100 Subject: [PATCH] Extend DTR APIs for Access management - Addresses some Sonar findings --- .../registry/service/ShellService.java | 17 ++++++++++------- ...AssetAdministrationShellApiSecurityTest.java | 6 +++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java index ba086964..6288ec42 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java @@ -185,8 +185,7 @@ public Shell findShellByExternalIdAndExternalSubjectId( String externalShellId, @Transactional public Shell findShellByExternalIdWithoutFiltering( String externalShellId ) { - return shellRepository.findByIdExternal( externalShellId ) - .orElseThrow( () -> new EntityNotFoundException( String.format( "Shell for identifier %s not found", externalShellId ) ) ); + return doFindShellByExternalIdWithoutFiltering( externalShellId ); } @Transactional( readOnly = true ) @@ -202,7 +201,7 @@ public ShellCollectionDto findAllShells( Integer pageSize, String cursorVal, Str currentPage.stream() .map( shell -> shellAccessHandler.filterShellProperties( shell, externalSubjectId ) ) .filter( Objects::nonNull ) - .limit( pageSize - resultList.size() ) + .limit( (long) pageSize - resultList.size() ) .forEach( resultList::add ); hasNext = currentPage.hasNext(); } @@ -351,7 +350,7 @@ public void deleteAllIdentifiers( String externalShellId ) { @Transactional public Set save( String externalShellId, Set shellIdentifiers, String externalSubjectId ) { - Shell shellFromDb = findShellByExternalIdWithoutFiltering( externalShellId ); + Shell shellFromDb = doFindShellByExternalIdWithoutFiltering( externalShellId ); List identifiersToUpdate = shellIdentifiers.stream().map( identifier -> identifier.withShellId( shellFromDb ) ) .collect( Collectors.toList() ); @@ -387,7 +386,7 @@ private static void mapShellIdentifier( Stream identifiersToUpd @Transactional public Submodel save( String externalShellId, Submodel submodel, String externalSubjectId ) { - Shell shellFromDb = findShellByExternalIdWithoutFiltering( externalShellId ); + Shell shellFromDb = doFindShellByExternalIdWithoutFiltering( externalShellId ); submodel.setShellId( shellFromDb ); //uniqueness on shellId and idShort @@ -411,7 +410,7 @@ public Submodel saveSubmodel( Submodel submodel ) { @Transactional public void update( String externalShellId, Submodel submodel, String externalSubjectId ) { - Shell shellFromDb = findShellByExternalIdWithoutFiltering( externalShellId ); + Shell shellFromDb = doFindShellByExternalIdWithoutFiltering( externalShellId ); shellFromDb.add( submodel ); submodel.setShellId( shellFromDb ); mapSubmodel( shellFromDb.getSubmodels() ); @@ -420,7 +419,7 @@ public void update( String externalShellId, Submodel submodel, String externalSu @Transactional public void deleteSubmodel( String externalShellId, String externalSubModelId, String externalSubjectId ) { - Shell shellFromDb = findShellByExternalIdWithoutFiltering( externalShellId ); + Shell shellFromDb = doFindShellByExternalIdWithoutFiltering( externalShellId ); Submodel submodelId = findSubmodelMinimalByExternalId( shellFromDb.getId(), externalSubModelId ); shellFromDb.getSubmodels().remove( submodelId ); submodelRepository.deleteById( submodelId.getId() ); @@ -468,5 +467,9 @@ public List saveBatch( List shells ) { } } ).collect( Collectors.toList() ); } + private Shell doFindShellByExternalIdWithoutFiltering( String externalShellId ) { + return shellRepository.findByIdExternal( externalShellId ) + .orElseThrow( () -> new EntityNotFoundException( String.format( "Shell for identifier %s not found", externalShellId ) ) ); + } } diff --git a/backend/src/test/java/org/eclipse/tractusx/semantics/registry/GranularAssetAdministrationShellApiSecurityTest.java b/backend/src/test/java/org/eclipse/tractusx/semantics/registry/GranularAssetAdministrationShellApiSecurityTest.java index cf4d1bb1..6f1808ed 100644 --- a/backend/src/test/java/org/eclipse/tractusx/semantics/registry/GranularAssetAdministrationShellApiSecurityTest.java +++ b/backend/src/test/java/org/eclipse/tractusx/semantics/registry/GranularAssetAdministrationShellApiSecurityTest.java @@ -189,7 +189,7 @@ public void testFindExternalShellIdsBySpecificAssetIdsWithTenantBasedVisibilityE super.testFindExternalShellIdsBySpecificAssetIdsWithTenantBasedVisibilityExpectSuccess(); } - @Test + //@Test public void testFindExternalShellIdsBySpecificAssetIdsWithTenantBasedVisibilityAndWildcardExpectSuccess() throws Exception { //TODO: enable when we are no longer using the file based access rules // This test is using a random prefix in the the name of the specificAssetId that has to match @@ -211,7 +211,7 @@ public void testGetAllShellsByOwningTenantId() throws Exception { super.testGetAllShellsByOwningTenantId(); } - @Test + //@Test public void testGetAllShellsWithPublicAccessByTenantId() throws Exception { //TODO: enable when public access is implemented //super.testGetAllShellsWithPublicAccessByTenantId(); @@ -222,7 +222,7 @@ public void testGetShellByExternalIdByOwningTenantId() throws Exception { super.testGetShellByExternalIdByOwningTenantId(); } - @Test + //@Test public void testGetAllShellByExternalIdWithPublicAccessByTenantId() throws Exception { //TODO: enable when public access is implemented //super.testGetAllShellByExternalIdWithPublicAccessByTenantId();