Source:
private void deleteResources(List<String> foundResources, String resourceNameType) { List<Path> fileList = new ArrayList<>(); List<String> deletedCredentials = new ArrayList<>(); AtomicBoolean e2eCleanupFailed = new AtomicBoolean(false); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(outputDirectory))) { for (Path path : stream) { String fileName = path.getFileName().toString(); if (fileName.startsWith("resource_names") && fileName.endsWith(".json")) { LOG.info("Found resource file: '{}' is going to be added to resource files' list", path.getFileName().toAbsolutePath().normalize()); fileList.add(path); } } } catch (Exception e) { LOG.error("Cannot find resource file at path: '{}', because of: {}", Paths.get(outputDirectory).toAbsolutePath().normalize(), e.getMessage(), e); throw new RuntimeException(String.format("Cannot find resource file at path: '%s', because of: %s", Paths.get(outputDirectory).toAbsolutePath().normalize(), e.getMessage())); } fileList.forEach(filePath -> { LOG.info("Processing resource file: '{}'", filePath.getFileName()); List<String> resourcesName = Optional.ofNullable(getResourcesFromFile(resourceNameType, filePath)) .orElse(List.of("")); resourcesName.forEach(resourceName -> { if (foundResources.stream() .anyMatch(resourceName::equals)) { LOG.info("{}:{} will be deleted!", resourceNameType, foundResources.stream() .filter(resourceName::equals).findAny().orElse(null)); switch (resourceNameType) { case "distroxName": case "stackName": deleteDistrox(getCloudbreakClient(), resourceName); break; case "sdxName": deleteSdx(getSdxClient(), resourceName); break; case "credentialName": deleteCredential(getEnvironmentClient(), resourceName); e2eCleanupFailed.set(true); deletedCredentials.add(resourceName); break; default: deleteEnvironment(getEnvironmentClient(), resourceName); break; } } else { LOG.info("Cannot find '{}:{}'! So End To End cleanup have been done successfully.", resourceNameType, resourceName); } }); }); validateE2ECleanup(e2eCleanupFailed, deletedCredentials); }

Target:
private void deleteResources(List<String> foundResources, String resourceNameType) { List<Path> fileList = new ArrayList<>(); List<String> deletedCredentials = new ArrayList<>(); AtomicBoolean e2eCleanupFailed = new AtomicBoolean(false); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(outputDirectory))) { for (Path path : stream) { String fileName = path.getFileName().toString(); if (fileName.startsWith("resource_names") && fileName.endsWith(".json")) { LOG.info("Found resource file: '{}' is going to be added to resource files' list", path.getFileName().toAbsolutePath().normalize()); fileList.add(path); } } } catch (Exception e) { LOG.error("Cannot find resource file at path: '{}', because of: {}", Paths.get(outputDirectory).toAbsolutePath().normalize(), e.getMessage(), e); throw new RuntimeException(String.format("Cannot find resource file at path: '%s', because of: %s", Paths.get(outputDirectory).toAbsolutePath().normalize(), e.getMessage())); } fileList.forEach(filePath -> { LOG.info("Processing resource file: '{}'", filePath.getFileName()); List<String> resourcesName = Optional.ofNullable(getResourcesFromFile(resourceNameType, filePath)) .orElse(List.of()); resourcesName.forEach(resourceName -> { if (foundResources.contains(resourceName)) { LOG.info("{}:{} will be deleted!", resourceNameType, foundResources.stream() .filter(resourceName::equals).findAny().orElse(null)); switch (resourceNameType) { case "distroxName": case "stackName": deleteDistrox(getCloudbreakClient(), resourceName); break; case "sdxName": deleteSdx(getSdxClient(), resourceName); break; case "credentialName": deleteCredential(getEnvironmentClient(), resourceName); e2eCleanupFailed.set(true); deletedCredentials.add(resourceName); break; default: deleteEnvironment(getEnvironmentClient(), resourceName); break; } } else { LOG.info("Cannot find '{}:{}'! So End To End cleanup have been done successfully.", resourceNameType, resourceName); } }); }); validateE2ECleanup(e2eCleanupFailed, deletedCredentials); }