1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class EnvironmentVariableValidator {
  4. /**
  5. * Validates environment variables for data migration.
  6. *
  7. * @param expectedVariables A map of expected environment variable names to their expected values.
  8. * @return A map of variable names that are missing or have invalid values. Returns an empty map if all variables are valid.
  9. */
  10. public static Map<String, String> validateEnvironmentVariables(Map<String, String> expectedVariables) {
  11. Map<String, String> validationErrors = new HashMap<>();
  12. if (expectedVariables == null || expectedVariables.isEmpty()) {
  13. return validationErrors; // Return empty map if no variables to validate
  14. }
  15. for (Map.Entry<String, String> entry : expectedVariables.entrySet()) {
  16. String variableName = entry.getKey();
  17. String expectedValue = entry.getValue();
  18. String actualValue = System.getenv(variableName);
  19. if (actualValue == null) {
  20. validationErrors.put(variableName, "Missing environment variable");
  21. } else if (!actualValue.equals(expectedValue)) {
  22. validationErrors.put(variableName, "Incorrect value. Expected: " + expectedValue + ", Actual: " + actualValue);
  23. }
  24. //Handle empty string as a potential valid value if defined in the requirements.
  25. else if (actualValue.trim().isEmpty() && expectedValue.trim().isEmpty()) {
  26. continue; // Skip if both are empty
  27. }
  28. else if (actualValue.trim().isEmpty() && !expectedValue.trim().isEmpty()) {
  29. validationErrors.put(variableName, "Missing environment variable");
  30. }
  31. }
  32. return validationErrors;
  33. }
  34. public static void main(String[] args) {
  35. //Example Usage
  36. Map<String, String> expectedVars = new HashMap<>();
  37. expectedVars.put("DATABASE_URL", "jdbc:mysql://localhost:3306/mydatabase");
  38. expectedVars.put("API_KEY", "abcdef123456");
  39. expectedVars.put("DEBUG_MODE", "true");
  40. Map<String, String> errors = validateEnvironmentVariables(expectedVars);
  41. if (errors.isEmpty()) {
  42. System.out.println("All environment variables are valid.");
  43. } else {
  44. System.out.println("Validation errors:");
  45. for (Map.Entry<String, String> error : errors.entrySet()) {
  46. System.out.println(error.getKey() + ": " + error.getValue());
  47. }
  48. }
  49. //Edge case: Empty expected variables
  50. Map<String, String> emptyExpected = new HashMap<>();
  51. Map<String, String> emptyResult = validateEnvironmentVariables(emptyExpected);
  52. System.out.println("Empty Expected Result size: " + emptyResult.size()); //Expect 0
  53. //Edge case: null expected variables
  54. Map<String, String> nullExpected = null;
  55. Map<String, String> nullResult = validateEnvironmentVariables(nullExpected);
  56. System.out.println("Null Expected Result size: " + nullResult.size()); //Expect 0
  57. //Edge case: Empty String
  58. Map<String, String> emptyStringExpected = new HashMap<>();
  59. emptyStringExpected.put("EMPTY_VAR", "");
  60. Map<String, String> emptyStringResult = validateEnvironmentVariables(emptyStringExpected);
  61. System.out.println("Empty String Result size: " + emptyStringResult.size()); //Expect 0
  62. }
  63. }

Add your comment