<commit_before>package com.sequenceiq.it.util.cleanup;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.ws.rs.NotFoundException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.StackViewV4Response;
import com.sequenceiq.cloudbreak.client.CloudbreakClient;
import com.sequenceiq.environment.api.v1.credential.model.response.CredentialResponse;
import com.sequenceiq.environment.api.v1.environment.model.response.EnvironmentBaseResponse;
import com.sequenceiq.environment.client.EnvironmentClient;
import com.sequenceiq.it.cloudbreak.MicroserviceClient;
import com.sequenceiq.it.cloudbreak.util.WaitResult;
import com.sequenceiq.sdx.api.model.SdxClusterResponse;
import com.sequenceiq.sdx.client.SdxClient;

@Component
public class CleanupUtil extends CleanupClientUtil {
    private static final Logger LOG = LoggerFactory.getLogger(CleanupUtil.class);

    private final Map<String, Map<Class<? extends MicroserviceClient>, MicroserviceClient>> clients = new HashMap<>();

    private final MultiValueMap<String, String> deletedResources = new LinkedMultiValueMap<>();

    @Value("${integrationtest.outputdir:.}")
    private String outputDirectory;

    @Inject
    private CleanupWaitUtil waitUtil;

    public void cleanupAllResources() {
        if (resourceFilesArePresent()) {
            cleanupDistroxes();
            cleanupSdxes();
            cleanupEnvironments();
            cleanupCredentials();
        } else {
            EnvironmentClient environmentClient = createEnvironmentClient();
            List<String> foundChildEnvironmentNames = new ArrayList<>(getChildEnvironments(environmentClient).values());
            List<String> foundEnvironmentNames = new ArrayList<>(getEnvironments(environmentClient).values());
            List<String> foundCredentialNames = getCredentials(environmentClient);

            if (!foundChildEnvironmentNames.isEmpty()) {
                LOG.info("Found child environments: '{}'", foundChildEnvironmentNames);
                deleteEnvironments(environmentClient, foundChildEnvironmentNames);
            }
            if (!foundEnvironmentNames.isEmpty()) {
                LOG.info("Found environments: '{}'", foundEnvironmentNames);
                deleteEnvironments(environmentClient, foundEnvironmentNames);
            } else {
                LOG.info("Cannot find any environment!");<commit_msg>CB-15782 Cleanup every resources in case of Jenkins Abort<commit_after>package com.sequenceiq.it.util.cleanup;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.ws.rs.NotFoundException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.StackViewV4Response;
import com.sequenceiq.cloudbreak.client.CloudbreakClient;
import com.sequenceiq.environment.api.v1.credential.model.response.CredentialResponse;
import com.sequenceiq.environment.api.v1.environment.model.response.EnvironmentBaseResponse;
import com.sequenceiq.environment.client.EnvironmentClient;
import com.sequenceiq.it.cloudbreak.MicroserviceClient;
import com.sequenceiq.it.cloudbreak.util.WaitResult;
import com.sequenceiq.sdx.api.model.SdxClusterResponse;
import com.sequenceiq.sdx.client.SdxClient;

@Component
public class CleanupUtil extends CleanupClientUtil {
    private static final Logger LOG = LoggerFactory.getLogger(CleanupUtil.class);

    private final Map<String, Map<Class<? extends MicroserviceClient>, MicroserviceClient>> clients = new HashMap<>();

    private final MultiValueMap<String, String> deletedResources = new LinkedMultiValueMap<>();

    @Value("${integrationtest.outputdir:.}")
    private String outputDirectory;

    @Value("${integrationtest.cleanup.afterAbort:false}")
    private boolean cleanupAfterAbort;

    @Inject
    private CleanupWaitUtil waitUtil;

    public void cleanupAllResources() {
        if (resourceFilesArePresent() && !cleanupAfterAbort) {
            cleanupDistroxes();
            cleanupSdxes();
            cleanupEnvironments();
            cleanupCredentials();
        } else {
            EnvironmentClient environmentClient = createEnvironmentClient();
            List<String> foundChildEnvironmentNames = new ArrayList<>(getChildEnvironments(environmentClient).values());
            List<String> foundEnvironmentNames = new ArrayList<>(getEnvironments(environmentClient).values());
            List<String> foundCredentialNames = getCredentials(environmentClient);

            if (!foundChildEnvironmentNames.isEmpty()) {
                LOG.info("Found child environments: '{}'", foundChildEnvironmentNames);
                deleteEnvironments(environmentClient, foundChildEnvironmentNames);
            }
            if (!foundEnvironmentNames.isEmpty()) {
                LOG.info("Found environments: '{}'", foundEnvironmentNames);
                deleteEnvironments(environmentClient, foundEnvironmentNames);
            } else {
                LOG.info("Cannot find any environment!");
{'fraction_non_alphanumeric': 0.10021651716671821, 'fraction_numerical': 0.002629137024435509, 'mean_word_length': 4.439024390243903, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6819243', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1657, 'n_words': 314}