diff --git a/src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java b/src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java index 2ed41fc1..f95c7b2a 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java @@ -166,27 +166,23 @@ public HttpResponse bulkDelete(@QueryValue(defaultValue = "*") String name return HttpResponse.notFound(); } - Map> validationErrors = new HashMap<>(); - - namespaces.forEach(namespace -> - validationErrors.put( - namespace.getMetadata().getName(), - namespaceService.findAllResourcesByNamespace(namespace) - .stream() - .map(FormatErrorUtils::invalidNamespaceDeleteOperation) - .toList()) - ); + List namespaceResources = namespaces + .stream() + .flatMap(namespace -> namespaceService.findAllResourcesByNamespace(namespace) + .stream()) + .toList(); + + if (!namespaceResources.isEmpty()) { + List validationErrors = namespaceResources + .stream() + .map(FormatErrorUtils::invalidNamespaceDeleteOperation) + .toList(); - if (validationErrors.values().stream() - .anyMatch(list -> !list.isEmpty())) { throw new ResourceValidationException( NAMESPACE, - validationErrors.keySet().stream() - .map(Object::toString) - .collect(Collectors.joining("/")), - validationErrors.values().stream() - .flatMap(Collection::stream) - .toList()); + String.join(",", namespaces.stream().map(namespace -> namespace.getMetadata().getName()).toList()), + validationErrors + ); } if (dryrun) { diff --git a/src/test/java/com/michelin/ns4kafka/controller/NamespaceControllerTest.java b/src/test/java/com/michelin/ns4kafka/controller/NamespaceControllerTest.java index ac505078..61685c15 100644 --- a/src/test/java/com/michelin/ns4kafka/controller/NamespaceControllerTest.java +++ b/src/test/java/com/michelin/ns4kafka/controller/NamespaceControllerTest.java @@ -481,6 +481,7 @@ void shouldNotDeleteNamespacesWhenResourcesAreStillLinkedWithIt() { @Test void shouldNotDeleteNamespacesWhenPatternMatchesNothing() { + when(namespaceService.findByWildcardName("namespace*")).thenReturn(List.of()); var result = namespaceController.bulkDelete("namespace*", false); assertEquals(HttpResponse.notFound().getStatus(), result.getStatus()); }