Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.3 kB
3
Indexable
Never
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Scanner;

public class RestApiComparator {

    public static void main(String[] args) {
        try {
            // Get user input
            String clusterId = getClusterIdFromUser();
            String projectId = getUserInput("Enter ProjectId: ");
            String namespaceId = getUserInput("Enter NamespaceId: ");
            String appLabel = getUserInput("Enter AppLabel: ");

            // Build query parameters
            Map<String, String> queryParams = Map.of(
                    "clusterId", clusterId,
                    "projectId", projectId,
                    "namespaceId", namespaceId,
                    "appLabel", appLabel
            );

            // Build API URLs for prod and non-prod environments
            String prodApiUrl = buildApiUrl("https://your-prod-api-url", queryParams);
            String nonProdApiUrl = buildApiUrl("https://your-non-prod-api-url", queryParams);

            // Make GET requests to prod and non-prod environments
            String prodResponse = makeGetRequest(prodApiUrl);
            String nonProdResponse = makeGetRequest(nonProdApiUrl);

            // Compare responses
            if (prodResponse.equals(nonProdResponse)) {
                System.out.println("Responses from prod and non-prod environments are identical.");
            } else {
                System.out.println("Responses from prod and non-prod environments are different.");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getClusterIdFromUser() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Select ClusterId:");
        System.out.println("1. ocp-test1");
        System.out.println("2. ocp-prod1");
        System.out.println("3. ocp-beta1");
        int choice = getUserChoice(scanner, "Enter your choice (1/2/3): ", 1, 3);
        switch (choice) {
            case 1:
                return "ocp-test1";
            case 2:
                return "ocp-prod1";
            case 3:
                return "ocp-beta1";
            default:
                throw new IllegalArgumentException("Invalid choice");
        }
    }

    private static String getUserInput(String prompt) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(prompt);
        return scanner.nextLine();
    }

    private static int getUserChoice(Scanner scanner, String prompt, int min, int max) {
        int choice;
        do {
            System.out.print(prompt);
            while (!scanner.hasNextInt()) {
                System.out.println("Invalid input. Please enter a number.");
                scanner.next(); // consume non-int token
            }
            choice = scanner.nextInt();
        } while (choice < min || choice > max);
        return choice;
    }

    private static String buildApiUrl(String baseUrl, Map<String, String> queryParams) throws MalformedURLException {
        StringBuilder apiUrl = new StringBuilder(baseUrl);
        if (!queryParams.isEmpty()) {
            apiUrl.append("?");
            for (Map.Entry<String, String> entry : queryParams.entrySet()) {
                apiUrl.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            apiUrl.deleteCharAt(apiUrl.length() - 1); // Remove the last "&"
        }
        return apiUrl.toString();
    }

    private static String makeGetRequest(String apiUrl) throws IOException {
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String line;

        while ((line = reader.readLine()) != null) {
            response.append(line);
        }

        reader.close();
        connection.disconnect();

        return response.toString();
    }
}
Leave a Comment