From f2eb03250bd6a6b3f49cdbb93023e36974a5a56f Mon Sep 17 00:00:00 2001 From: Aniket Sonawane Date: Fri, 9 Jun 2023 14:17:22 +0530 Subject: [PATCH 1/2] Introduced New Batch Delete API --- .../AssetAdministrationShellApiDelegate.java | 6 +++ .../registry/repository/ShellRepository.java | 3 ++ .../registry/service/ShellService.java | 12 +++++ .../static/aas-registry-openapi.yaml | 25 ++++++++++ .../AssetAdministrationShellApiTest.java | 50 +++++++++++++++++++ 5 files changed, 96 insertions(+) diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/controller/AssetAdministrationShellApiDelegate.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/controller/AssetAdministrationShellApiDelegate.java index 1607d34d..cbd42c9a 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/controller/AssetAdministrationShellApiDelegate.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/controller/AssetAdministrationShellApiDelegate.java @@ -177,5 +177,11 @@ public ResponseEntity> postQueryAllAssetAdministrationShellIds(Shel List externalIds = shellService.findExternalShellIdsByIdentifiersByAnyMatch(shellMapper.fromApiDto(assetIds)); return new ResponseEntity<>(externalIds, HttpStatus.OK); } + + @Override + public ResponseEntity batchDeleteAssetAdministrationShellDescriptor(List aasIdentifierList) { + shellService.deleteAllShell(aasIdentifierList); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } } diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java index d428de31..b6e9d8dd 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java @@ -38,6 +38,9 @@ public interface ShellRepository extends PagingAndSortingRepository List findShellsByIdExternalIsIn(Set idExternals); + @Query("select s.id from shell s where s.id_external in (:idExternalList)") + Optional> findMinimalRepresentationListByIdExternalList(List idExternalList); + /** * Returns external shell ids for the given keyValueCombinations. * External shell ids matching the conditions below are returned: 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 1d9501df..d8df40a5 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 @@ -234,4 +234,16 @@ public List saveBatch(List shells) { }).collect(Collectors.toList()); } + @Transactional + public void deleteAllShell(List aasIdentifierList) { + List shellListFromDb = findShellMinimalListByExternalIdList(aasIdentifierList); + IterableiterableShellIdList = shellListFromDb; + shellRepository.deleteAllById(iterableShellIdList); + } + + private List findShellMinimalListByExternalIdList(List externalShellIdList) { + return shellRepository.findMinimalRepresentationListByIdExternalList(externalShellIdList) + .orElseThrow(() -> new EntityNotFoundException(String.format("Shell for identifier %s not found", externalShellIdList))); + } + } diff --git a/backend/src/main/resources/static/aas-registry-openapi.yaml b/backend/src/main/resources/static/aas-registry-openapi.yaml index 9d014f0b..6c72ed8b 100644 --- a/backend/src/main/resources/static/aas-registry-openapi.yaml +++ b/backend/src/main/resources/static/aas-registry-openapi.yaml @@ -245,6 +245,31 @@ paths: description: Asset Administration Shell Descriptor deleted successfully x-semanticIds: - https://admin-shell.io/aas/API/DeleteAssetAdministrationShellDescriptorById/1/0/RC02 + /registry/shell-descriptors/batch-delete: + post: + tags: + - Registry and Discovery Interface + summary: Deletes All Asset Administration Shell Descriptor, i.e. de-registers an AAS + operationId: BatchDeleteAssetAdministrationShellDescriptor + requestBody: + description: Delete all shell descriptors for the given identifications. + content: + application/json: + schema: + type: array + minLength: 1 + maxItems: 10000 + items: + $ref: '#/components/schemas/Identifier' + examples: + complete: + $ref: '#/components/examples/asset-administration-shell-descriptor-fetch-request' + required: true + responses: + "201": + description: Asset Administration Shell Descriptors deleted successfully + x-semanticIds: + - https://admin-shell.io/aas/API/BatchDeleteAssetAdministrationShellDescriptor/1/0/RC02 /registry/shell-descriptors/{aasIdentifier}/submodel-descriptors: get: tags: diff --git a/backend/src/test/java/org/eclipse/tractusx/semantics/registry/AssetAdministrationShellApiTest.java b/backend/src/test/java/org/eclipse/tractusx/semantics/registry/AssetAdministrationShellApiTest.java index 4d31cbd5..5371d4da 100644 --- a/backend/src/test/java/org/eclipse/tractusx/semantics/registry/AssetAdministrationShellApiTest.java +++ b/backend/src/test/java/org/eclipse/tractusx/semantics/registry/AssetAdministrationShellApiTest.java @@ -897,6 +897,56 @@ public void testFetchShellsByMultipleIdentificationsExpectSuccessExpectSuccess() .andExpect(jsonPath("$.items[*].identification", hasItems(getId(shellPayload1), getId(shellPayload2)) )); } + + @Test + public void testDeleteAllShellExpectSuccess() throws Exception { + ObjectNode shellPayload1 = createShell(); + performShellCreateRequest(toJson(shellPayload1)); + + ObjectNode shellPayload2 = createShell(); + performShellCreateRequest(toJson(shellPayload2)); + + ArrayNode fetchTwoShellsById = emptyArrayNode() + .add(getId(shellPayload1)) + .add(getId(shellPayload2)); + + mvc.perform( + MockMvcRequestBuilders + .post(SHELL_BASE_PATH+ "/batch-delete") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content((toJson(fetchTwoShellsById))) + .with(jwtTokenFactory.allRoles()) + + ) + .andDo(MockMvcResultHandlers.print()) + .andExpect(status().isNoContent()); + } + + @Test + public void testDeleteAllShellExpectNotFound() throws Exception { + ObjectNode shellPayload1 = createShell(); + performShellCreateRequest(toJson(shellPayload1)); + + ObjectNode shellPayload2 = createShell(); + performShellCreateRequest(toJson(shellPayload2)); + + ArrayNode fetchTwoShellsById = emptyArrayNode() + .add(getId(shellPayload1)) + .add(getId(shellPayload2)); + + mvc.perform( + MockMvcRequestBuilders + .post(SHELL_BASE_PATH+ "/batch-delete") + .accept(MediaType.APPLICATION_JSON) + .contentType(MediaType.APPLICATION_JSON) + .content((toJson(fetchTwoShellsById))) + .with(jwtTokenFactory.allRoles()) + + ) + .andDo(MockMvcResultHandlers.print()) + .andExpect(status().isNoContent()); + } } } From e2bfff42f46e4fbce8e013c004a85269f5a5ba16 Mon Sep 17 00:00:00 2001 From: Aniket Sonawane Date: Fri, 9 Jun 2023 14:40:06 +0530 Subject: [PATCH 2/2] Addressed code smells --- .../tractusx/semantics/registry/service/ShellService.java | 8 +++++--- .../registry/AssetAdministrationShellApiTest.java | 4 ++-- 2 files changed, 7 insertions(+), 5 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 d8df40a5..fc80bd51 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 @@ -50,6 +50,8 @@ @Service public class ShellService { + private static final String SHELL_NOT_FOUND = "Shell for identifier %s not found"; + private final ShellRepository shellRepository; private final ShellIdentifierRepository shellIdentifierRepository; private final SubmodelRepository submodelRepository; @@ -77,7 +79,7 @@ public Shell save(Shell shell) { public Shell findShellByExternalId(String externalShellId) { return shellRepository.findByIdExternal(externalShellId) .map(shell -> shell.withIdentifiers(filterSpecificAssetIdsByTenantId(shell.getIdentifiers(), tenantAware.getTenantId()))) - .orElseThrow(() -> new EntityNotFoundException(String.format("Shell for identifier %s not found", externalShellId))); + .orElseThrow(() -> new EntityNotFoundException(String.format(SHELL_NOT_FOUND, externalShellId))); } @Transactional(readOnly = true) @@ -206,7 +208,7 @@ private SubmodelMinimal findSubmodelMinimalByExternalId(UUID shellId, String ext private ShellMinimal findShellMinimalByExternalId(String externalShellId) { return shellRepository.findMinimalRepresentationByIdExternal(externalShellId) - .orElseThrow(() -> new EntityNotFoundException(String.format("Shell for identifier %s not found", externalShellId))); + .orElseThrow(() -> new EntityNotFoundException(String.format(SHELL_NOT_FOUND, externalShellId))); } /** @@ -243,7 +245,7 @@ public void deleteAllShell(List aasIdentifierList) { private List findShellMinimalListByExternalIdList(List externalShellIdList) { return shellRepository.findMinimalRepresentationListByIdExternalList(externalShellIdList) - .orElseThrow(() -> new EntityNotFoundException(String.format("Shell for identifier %s not found", externalShellIdList))); + .orElseThrow(() -> new EntityNotFoundException(String.format(SHELL_NOT_FOUND, externalShellIdList))); } } diff --git a/backend/src/test/java/org/eclipse/tractusx/semantics/registry/AssetAdministrationShellApiTest.java b/backend/src/test/java/org/eclipse/tractusx/semantics/registry/AssetAdministrationShellApiTest.java index 5371d4da..a518abef 100644 --- a/backend/src/test/java/org/eclipse/tractusx/semantics/registry/AssetAdministrationShellApiTest.java +++ b/backend/src/test/java/org/eclipse/tractusx/semantics/registry/AssetAdministrationShellApiTest.java @@ -899,7 +899,7 @@ public void testFetchShellsByMultipleIdentificationsExpectSuccessExpectSuccess() } @Test - public void testDeleteAllShellExpectSuccess() throws Exception { + void testDeleteAllShellExpectSuccess() throws Exception { ObjectNode shellPayload1 = createShell(); performShellCreateRequest(toJson(shellPayload1)); @@ -924,7 +924,7 @@ public void testDeleteAllShellExpectSuccess() throws Exception { } @Test - public void testDeleteAllShellExpectNotFound() throws Exception { + void testDeleteAllShellExpectNotFound() throws Exception { ObjectNode shellPayload1 = createShell(); performShellCreateRequest(toJson(shellPayload1));